forked from FactomProject/factomd
/
simWallet.go
379 lines (307 loc) · 9.73 KB
/
simWallet.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package testHelper
// test helpers for Transaction & entry creations
import (
"bytes"
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"text/template"
"time"
"github.com/FactomProject/factom"
"github.com/FactomProject/factomd/common/entryBlock"
"github.com/FactomProject/factomd/common/entryCreditBlock"
"github.com/FactomProject/factomd/common/factoid"
"github.com/FactomProject/factomd/common/interfaces"
"github.com/FactomProject/factomd/common/messages"
"github.com/FactomProject/factomd/common/primitives"
"github.com/FactomProject/factomd/engine"
"github.com/FactomProject/factomd/state"
)
// struct to generate FCT or EC addresses
// from the same private key
type testAccount struct {
Priv *primitives.PrivateKey
}
var logName string = "simTest"
func (d *testAccount) FctPriv() string {
x, _ := primitives.PrivateKeyStringToHumanReadableFactoidPrivateKey(d.Priv.PrivateKeyString())
return x
}
func (d *testAccount) FctPub() string {
s, _ := factoid.PublicKeyStringToFactoidAddressString(d.Priv.PublicKeyString())
return s
}
func (d *testAccount) EcPub() string {
s, _ := factoid.PublicKeyStringToECAddressString(d.Priv.PublicKeyString())
return s
}
func (d *testAccount) EcPriv() string {
s, _ := primitives.PrivateKeyStringToHumanReadableECPrivateKey(d.Priv.PrivateKeyString())
return s
}
func (d *testAccount) FctPrivHash() interfaces.IHash {
a := primitives.ConvertUserStrToAddress(d.FctPriv())
x, _ := primitives.HexToHash(hex.EncodeToString(a))
return x
}
func (d *testAccount) FctAddr() interfaces.IHash {
a := primitives.ConvertUserStrToAddress(d.FctPub())
x, _ := primitives.HexToHash(hex.EncodeToString(a))
return x
}
func (d *testAccount) EcPrivHash() interfaces.IHash {
a := primitives.ConvertUserStrToAddress(d.EcPriv())
x, _ := primitives.HexToHash(hex.EncodeToString(a))
return x
}
func (d *testAccount) EcAddr() interfaces.IHash {
a := primitives.ConvertUserStrToAddress(d.EcPub())
x, _ := primitives.HexToHash(hex.EncodeToString(a))
return x
}
// buy EC from coinbase 'bank'
func (d *testAccount) FundEC(amt uint64) {
state0 := engine.GetFnodes()[0].State
engine.FundECWallet(state0, GetBankAccount().FctPrivHash(), d.EcAddr(), uint64(amt)*state0.GetFactoshisPerEC())
}
// buy EC from account
func (d *testAccount) ConvertEC(amt uint64) {
state0 := engine.GetFnodes()[0].State
engine.FundECWallet(state0, d.FctPrivHash(), d.EcAddr(), uint64(amt)*state0.GetFactoshisPerEC())
}
// get FCT from coinbase 'bank'
func (d *testAccount) FundFCT(amt uint64) {
state0 := engine.GetFnodes()[0].State
_, err := engine.SendTxn(state0, uint64(amt), GetBankAccount().FctPriv(), d.FctPub(), state0.GetFactoshisPerEC())
if err != nil {
panic(err)
}
}
// transfer FCT from account
func (d *testAccount) SendFCT(a *testAccount, amt uint64) {
state0 := engine.GetFnodes()[0].State
engine.SendTxn(state0, uint64(amt), d.FctPriv(), a.FctPub(), state0.GetFactoshisPerEC())
}
// check EC balance
func (d *testAccount) GetECBalance() int64 {
state0 := engine.GetFnodes()[0].State
return engine.GetBalanceEC(state0, d.EcPub())
}
var testFormat string = `
FCT
FctPriv: {{ .FctPriv }}
FctPub: {{ .FctPub }}
FctPrivHash: {{ .FctPrivHash }}
FctAddr: {{ .FctAddr }}
EC
EcPriv: {{ .EcPriv }}
EcPub: {{ .EcPub }}
EcPrivHash: {{ .EcPrivHash }}
EcAddr: {{ .EcAddr }}
`
var testTemplate *template.Template = template.Must(
template.New("").Parse(testFormat),
)
func (d *testAccount) String() string {
b := &bytes.Buffer{}
testTemplate.Execute(b, d)
return b.String()
}
func AccountFromFctSecret(s string) *testAccount {
d := new(testAccount)
h, _ := primitives.HumanReadableFactoidPrivateKeyToPrivateKey(s)
d.Priv = primitives.NewPrivateKeyFromHexBytes(h)
return d
}
// This account has a balance from initial coinbase
func GetBankAccount() *testAccount {
return AccountFromFctSecret("Fs3E9gV6DXsYzf7Fqx1fVBQPQXV695eP3k5XbmHEZVRLkMdD9qCK")
}
// build addresses from random key
func GetRandomAccount() *testAccount {
d := new(testAccount)
d.Priv = primitives.RandomPrivateKey()
return d
}
// KLUDGE duplicates code from: factom lib
// TODO: refactor factom package to export these functions
func milliTime() (r []byte) {
buf := new(bytes.Buffer)
t := time.Now().UnixNano()
m := t / 1e6
binary.Write(buf, binary.BigEndian, m)
return buf.Bytes()[2:]
}
// KLUDGE duplicates code from: factom.ComposeEntryCommit()
// TODO: refactor factom package to export these functions
func commitEntryMsg(addr *factom.ECAddress, e *factom.Entry) (*bytes.Buffer, error) {
buf := new(bytes.Buffer)
// 1 byte version
buf.Write([]byte{0})
// 6 byte milliTimestamp (truncated unix time)
buf.Write(milliTime())
// 32 byte Entry Hash
buf.Write(e.Hash())
// 1 byte number of entry credits to pay
if c, err := factom.EntryCost(e); err != nil {
return nil, err
} else {
buf.WriteByte(byte(c))
}
// 32 byte Entry Credit Address Public Key + 64 byte Signature
sig := addr.Sign(buf.Bytes())
buf.Write(addr.PubBytes())
buf.Write(sig[:])
return buf, nil
}
// KLUDGE: copy from factom lib
// shad Double Sha256 Hash; sha256(sha256(data))
func shad(data []byte) []byte {
h1 := sha256.Sum256(data)
h2 := sha256.Sum256(h1[:])
return h2[:]
}
// KLUDGE copy from factom
func composeChainCommitMsg(c *factom.Chain, ec *factom.ECAddress) (*bytes.Buffer, error) {
buf := new(bytes.Buffer)
// 1 byte version
buf.Write([]byte{0})
// 6 byte milliTimestamp
buf.Write(milliTime())
e := c.FirstEntry
// 32 byte ChainID Hash
if p, err := hex.DecodeString(c.ChainID); err != nil {
return nil, err
} else {
// double sha256 hash of ChainID
buf.Write(shad(p))
}
// 32 byte Weld; sha256(sha256(EntryHash + ChainID))
if cid, err := hex.DecodeString(c.ChainID); err != nil {
return nil, err
} else {
s := append(e.Hash(), cid...)
buf.Write(shad(s))
}
// 32 byte Entry Hash of the First Entry
buf.Write(e.Hash())
// 1 byte number of Entry Credits to pay
if d, err := factom.EntryCost(e); err != nil {
return nil, err
} else {
buf.WriteByte(byte(d + 10))
}
// 32 byte Entry Credit Address Public Key + 64 byte Signature
sig := ec.Sign(buf.Bytes())
buf.Write(ec.PubBytes())
buf.Write(sig[:])
return buf, nil
}
func PrivateKeyToECAddress(key *primitives.PrivateKey) *factom.ECAddress {
// KLUDGE is there a better way to do this?
ecPub, _ := factoid.PublicKeyStringToECAddress(key.PublicKeyString())
addr := factom.ECAddress{&[32]byte{}, &[64]byte{}}
copy(addr.Pub[:], ecPub.Bytes())
copy(addr.Sec[:], key.Key[:])
return &addr
}
func ComposeCommitEntryMsg(pkey *primitives.PrivateKey, e factom.Entry) (*messages.CommitEntryMsg, error) {
msg, err := commitEntryMsg(PrivateKeyToECAddress(pkey), &e)
commit := entryCreditBlock.NewCommitEntry()
commit.UnmarshalBinaryData(msg.Bytes())
m := new(messages.CommitEntryMsg)
m.CommitEntry = commit
m.SetValid()
return m, err
}
func ComposeRevealEntryMsg(pkey *primitives.PrivateKey, e *factom.Entry) (*messages.RevealEntryMsg, error) {
entry := entryBlock.NewEntry()
entry.Content = primitives.ByteSlice{Bytes: e.Content}
id, _ := primitives.HexToHash(e.ChainID)
entry.ChainID = id
for _, extID := range e.ExtIDs {
entry.ExtIDs = append(entry.ExtIDs, primitives.ByteSlice{Bytes: extID})
}
m := new(messages.RevealEntryMsg)
m.Entry = entry
m.Timestamp = primitives.NewTimestampNow()
m.SetValid()
return m, nil
}
func ComposeChainCommit(pkey *primitives.PrivateKey, c *factom.Chain) (*messages.CommitChainMsg, error) {
msg, _ := composeChainCommitMsg(c, PrivateKeyToECAddress(pkey))
e := entryCreditBlock.NewCommitChain()
_, err := e.UnmarshalBinaryData(msg.Bytes())
if err != nil {
return nil, err
}
m := new(messages.CommitChainMsg)
m.CommitChain = e
m.SetValid()
return m, nil
}
// wait for non-zero EC balance
func WaitForAnyEcBalance(s *state.State, ecPub string) int64 {
s.LogPrintf(logName, "WaitForAnyEcBalance %v", ecPub)
return WaitForEcBalanceOver(s, ecPub, 0)
}
// wait for non-zero FCT balance
func WaitForAnyFctBalance(s *state.State, fctPub string) int64 {
s.LogPrintf(logName, "WaitForAnyFctBalance %v", fctPub)
return WaitForFctBalanceOver(s, fctPub, 0)
}
// wait for exactly Zero EC balance
// REVIEW: should we ditch this?
func WaitForZeroEC(s *state.State, ecPub string) int64 {
s.LogPrintf(logName, "WaitingForZeroEcBalance")
return WaitForEcBalanceUnder(s, ecPub, 1)
}
const balanceWaitInterval = time.Millisecond * 20
// loop until balance is < target
func WaitForEcBalanceUnder(s *state.State, ecPub string, target int64) int64 {
s.LogPrintf(logName, "WaitForEcBalanceUnder%v: %v", target, ecPub)
for {
bal := engine.GetBalanceEC(s, ecPub)
time.Sleep(balanceWaitInterval)
if bal < target {
s.LogPrintf(logName, "FoundEcBalanceUnder%v: %v", target, bal)
return bal
}
}
}
// loop until balance is >= target
func WaitForEcBalanceOver(s *state.State, ecPub string, target int64) int64 {
s.LogPrintf(logName, "WaitForEcBalanceOver%v: %v", target, ecPub)
for {
bal := engine.GetBalanceEC(s, ecPub)
time.Sleep(balanceWaitInterval)
if bal > target {
s.LogPrintf(logName, "FoundEcBalancerOver%v: %v", target, bal)
return bal
}
}
}
// loop until balance is >= target
func WaitForFctBalanceUnder(s *state.State, fctPub string, target int64) int64 {
s.LogPrintf(logName, "WaitForFctBalanceUnder%v: %v", target, fctPub)
for {
bal := engine.GetBalance(s, fctPub)
time.Sleep(balanceWaitInterval)
if bal < target {
s.LogPrintf(logName, "FoundFctBalanceUnder%v: %v", target, bal)
return bal
}
}
}
// loop until balance is <= target
func WaitForFctBalanceOver(s *state.State, fctPub string, target int64) int64 {
s.LogPrintf(logName, "WaitForFctBalanceOver%v: %v", target, fctPub)
for {
bal := engine.GetBalance(s, fctPub)
time.Sleep(balanceWaitInterval)
if bal > target {
s.LogPrintf(logName, "FoundMaxFctBalanceOver%v: %v", target, bal)
return bal
}
}
}