This repository has been archived by the owner on Sep 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
helpers.go
198 lines (161 loc) · 4.68 KB
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/sha256"
"encoding/csv"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
"time"
"github.com/kristjank/ark-go/core"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
func floatEquals(a, b float64) bool {
EPSILON := 0.000000000000001
if (a-b) < EPSILON && (b-a) < EPSILON {
return true
}
return false
}
func checkConfigSharingRatio() bool {
a1 := viper.GetFloat64("voters.shareratio")
a2 := viper.GetFloat64("costs.shareratio")
a3 := viper.GetFloat64("reserve.shareratio")
a4 := viper.GetFloat64("personal.shareratio")
if !floatEquals(a1+a2+a3+a4, 1.0) {
log.Info("Wrong config. Check share ration percentages!")
return false
}
return true
}
func log2csv(payload core.TransactionPayload, txids []string, filecsv *os.File, status string) {
records := [][]string{
{"ADDRESS", "SENT AMOUNT", "TimeStamp", "TxId", "ApiResponse"},
}
for ix, el := range payload.Transactions {
// sAmount := fmt.Sprintf("%15.8f", float64(el.Amount)/float64(core.SATOSHI))
timeTx := core.GetTransactionTime(el.Timestamp)
localTime := timeTx.Local()
var line []string
if txids != nil {
line = []string{el.RecipientID, strconv.FormatFloat(float64(el.Amount)/float64(core.SATOSHI), 'f', -1, 64), localTime.Format("2006-01-02 15:04:05"), txids[ix], status}
} else {
line = []string{el.RecipientID, strconv.FormatFloat(float64(el.Amount)/float64(core.SATOSHI), 'f', -1, 64), localTime.Format("2006-01-02 15:04:05"), "N/A", status}
}
records = append(records, line)
}
w := csv.NewWriter(filecsv)
defer w.Flush()
w.WriteAll(records)
}
func getSystemEnv() string {
var buffer bytes.Buffer
buffer.WriteString(os.Getenv("OS"))
buffer.WriteString(os.Getenv("PROCESSOR_ARCHITECTURE"))
buffer.WriteString(os.Getenv("PROCESSOR_IDENTIFIER"))
buffer.WriteString(os.Getenv("COMPUTERNAME"))
buffer.WriteString(os.Getenv("ComSpec"))
buffer.WriteString(os.Getenv("OS"))
buffer.WriteString(os.Getenv("PROCESSOR_ARCHITECTURE"))
buffer.WriteString(os.Getenv("PROCESSOR_IDENTIFIER"))
buffer.WriteString(os.Getenv("COMPUTERNAME"))
buffer.WriteString(os.Getenv("ComSpec"))
return buffer.String()
}
func save(p1, p2 string) {
ciphertext, _ := encrypt([]byte(p1), getRandHash())
ioutil.WriteFile("assembly.ark", ciphertext, 0644)
if p2 != "" {
ciphertext, err := encrypt([]byte(p2), getRandHash())
if err != nil {
log.Info("Error encrypting")
}
ioutil.WriteFile("assembly1.ark", ciphertext, 0644)
} else {
os.Remove("assembly1.ark")
}
}
/*func read() (*arkcoin.PrivateKey, *arkcoin.PrivateKey) {
dat, err := ioutil.ReadFile("assembly.ark")
if err != nil {
log.Info(err.Error())
}
plaintext, _ := decrypt(dat, getRandHash())
key1 := arkcoin.NewPrivateKeyFromPassword(string(plaintext), arkcoin.ActiveCoinConfig)
var key2 *arkcoin.PrivateKey
if _, err := os.Stat("assembly1.ark"); err == nil {
dat, _ = ioutil.ReadFile("assembly1.ark")
plaintext, _ = decrypt(dat, getRandHash())
key2 = arkcoin.NewPrivateKeyFromPassword(string(plaintext), arkcoin.ActiveCoinConfig)
}
return key1, key2
}*/
func read() (string, string) {
dat, err := ioutil.ReadFile("assembly.ark")
if err != nil {
log.Info(err.Error())
}
p1, _ := decrypt(dat, getRandHash())
var p2 []byte
if _, err := os.Stat("assembly1.ark"); err == nil {
dat, _ = ioutil.ReadFile("assembly1.ark")
p2, _ = decrypt(dat, getRandHash())
}
return string(p1), string(p2)
}
func encrypt(plaintext []byte, key []byte) ([]byte, error) {
c, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(c)
if err != nil {
return nil, err
}
nonce := make([]byte, gcm.NonceSize())
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
return nil, err
}
return gcm.Seal(nonce, nonce, plaintext, nil), nil
}
func decrypt(ciphertext []byte, key []byte) ([]byte, error) {
c, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(c)
if err != nil {
return nil, err
}
nonceSize := gcm.NonceSize()
if len(ciphertext) < nonceSize {
return nil, errors.New("ciphertext too short")
}
nonce, ciphertext := ciphertext[:nonceSize], ciphertext[nonceSize:]
return gcm.Open(nil, nonce, ciphertext, nil)
}
func getRandHash() []byte {
a := getSystemEnv()
trHashBytes := sha256.New()
trHashBytes.Write([]byte(a))
return trHashBytes.Sum(nil)
}
func createLogFolder() string {
tt := time.Now()
folderName := fmt.Sprintf("%d-%02d-%02dT%02d-%02d-%02d",
tt.Year(), tt.Month(), tt.Day(),
tt.Hour(), tt.Minute(), tt.Second())
log.Println("log/" + folderName)
err := os.MkdirAll("log/"+folderName, os.ModePerm)
if err != nil {
log.Error(err.Error())
}
return folderName
}