-
Notifications
You must be signed in to change notification settings - Fork 10
/
util.go
257 lines (220 loc) · 5.98 KB
/
util.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Copyright 2017 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package main
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"strconv"
"strings"
"time"
"github.com/FactomProject/btcutil/base58"
"github.com/FactomProject/factom"
)
// GetFactomdServer returns the current factomd server set in the factom package.
// This is used for compose functions to put the appropriate url in the sample curl
// localhost:8088 is returned if the factom package does not have one set
func GetFactomdServer() string {
if factom.RpcConfig != nil && factom.RpcConfig.FactomdServer != "" {
return factom.RpcConfig.FactomdServer
}
return "localhost:8088"
}
// exidCollector accumulates the external ids from the command line -e and -E
// flags
var exidCollector [][]byte
// extids will be a flag receiver for adding chains and entries
// In ASCII
type extidsASCII []string
func (e *extidsASCII) String() string {
return fmt.Sprint(*e)
}
func (e *extidsASCII) Set(s string) error {
*e = append(*e, s)
exidCollector = append(exidCollector[:], []byte(s))
return nil
}
// extids will be a flag receiver for adding chains and entries
// In HEX
type extidsHex []string
func (e *extidsHex) String() string {
return fmt.Sprint(*e)
}
func (e *extidsHex) Set(s string) error {
*e = append(*e, s)
b, err := hex.DecodeString(s)
if err != nil {
return err
}
exidCollector = append(exidCollector[:], b)
return nil
}
// nameCollector accumulates the components of a chain name from the command
// line -n and -N flags
var nameCollector [][]byte
// namesASCII will be a flag receiver for ASCII chain names.
type namesASCII []string
func (n *namesASCII) String() string {
return fmt.Sprint(*n)
}
func (n *namesASCII) Set(s string) error {
*n = append(*n, s)
nameCollector = append(nameCollector[:], []byte(s))
return nil
}
// namesHex will be a flag receiver for HEX encoded chain names.
type namesHex []string
func (n *namesHex) String() string {
return fmt.Sprint(*n)
}
func (n *namesHex) Set(s string) error {
*n = append(*n, s)
b, err := hex.DecodeString(s)
if err != nil {
return err
}
nameCollector = append(nameCollector[:], b)
return nil
}
// keysASCII will be a flag receiver for ASCII identity keys.
type keysASCII []string
func (n *keysASCII) String() string {
return fmt.Sprint(*n)
}
func (k *keysASCII) Set(s string) error {
*k = append(*k, s)
if factom.IdentityKeyStringType(s) != factom.IDPub {
return fmt.Errorf("Provided key string not a valid public identity key: %s", s)
}
b := base58.Decode(s)
key := factom.NewIdentityKey()
copy(key.Pub[:], b[factom.IDKeyPrefixLength:factom.IDKeyBodyLength])
return nil
}
func factoshiToFactoid(v interface{}) string {
value, err := strconv.Atoi(fmt.Sprint(v))
if err != nil {
return ""
}
sign := ""
if value < 0 {
sign = "-"
value = -value
}
d := value / 1e8
r := value % 1e8
ds := fmt.Sprintf("%s%d", sign, d)
rs := fmt.Sprintf("%08d", r)
rs = strings.TrimRight(rs, "0")
if len(rs) > 0 {
ds = ds + "."
}
return fmt.Sprintf("%s%s", ds, rs)
}
// nametoid computes a chainid from the chain name components
func nametoid(name [][]byte) string {
hs := sha256.New()
for _, v := range name {
h := sha256.Sum256(v)
hs.Write(h[:])
}
return hex.EncodeToString(hs.Sum(nil))
}
// waitOnFctAck blocks while waiting for a factom ack message and returns the
// ack status or times out after 10 seconds.
func waitOnFctAck(txid string) (string, error) {
stat := make(chan string, 1)
errchan := make(chan error, 1)
// poll for the acknowledgement
go func() {
for {
s, err := factom.FactoidACK(txid, "")
if err != nil {
errchan <- err
break
}
if (s.Status != "Unknown") && (s.Status != "NotConfirmed") {
stat <- s.Status
break
}
time.Sleep(time.Second / 2)
}
}()
// wait for the acknowledgement or timeout after 10 sec
select {
case err := <-errchan:
return "", err
case s := <-stat:
return s, nil
case <-time.After(60 * time.Second):
return "", fmt.Errorf("timeout: no acknowledgement found")
}
// code should not reach this point
return "", fmt.Errorf("unknown error")
}
// waitOnCommitAck blocks while waiting for an ack message for an Entry Commit
// and returns the ack status or times out after 10 seconds.
func waitOnCommitAck(txid string) (string, error) {
stat := make(chan string, 1)
errchan := make(chan error, 1)
// poll for the acknowledgement
go func() {
for {
s, err := factom.EntryCommitACK(txid, "")
if err != nil {
errchan <- err
break
}
if (s.CommitData.Status != "Unknown") && (s.CommitData.Status != "NotConfirmed") {
stat <- s.CommitData.Status
break
}
time.Sleep(time.Second / 2)
}
}()
// wait for the acknowledgement or timeout after 10 sec
select {
case err := <-errchan:
return "", err
case s := <-stat:
return s, nil
case <-time.After(60 * time.Second):
return "", fmt.Errorf("timeout: no acknowledgement found")
}
// code should not reach this point
return "", fmt.Errorf("unknown error")
}
// waitOnRevealAck blocks while waiting for an ack message for an Entry Reveal
// and returns the ack status or times out after 10 seconds.
func waitOnRevealAck(txid string) (string, error) {
stat := make(chan string, 1)
errchan := make(chan error, 1)
// poll for the acknowledgement
go func() {
for {
// All 0s signals an entry
s, err := factom.EntryRevealACK(txid, "", "0000000000000000000000000000000000000000000000000000000000000000")
if err != nil {
errchan <- err
break
}
if (s.EntryData.Status != "Unknown") && (s.EntryData.Status != "NotConfirmed") {
stat <- s.EntryData.Status
break
}
time.Sleep(time.Second / 2)
}
}()
// wait for the acknowledgement or timeout after 10 sec
select {
case err := <-errchan:
return "", err
case s := <-stat:
return s, nil
case <-time.After(60 * time.Second):
return "", fmt.Errorf("timeout: no acknowledgement found")
}
// code should not reach this point
return "", fmt.Errorf("unknown error")
}