-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool_unit_test.go
695 lines (590 loc) · 24.4 KB
/
pool_unit_test.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
/*
*
*
* MIT NON-AI License
*
* Copyright (c) 2022-2024 Aleksei Kotelnikov([email protected])
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of the software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions.
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* In addition, the following restrictions apply:
*
* 1. The Software and any modifications made to it may not be used for the purpose of training or improving machine learning algorithms,
* including but not limited to artificial intelligence, natural language processing, or data mining. This condition applies to any derivatives,
* modifications, or updates based on the Software code. Any usage of the Software in an AI-training dataset is considered a breach of this License.
*
* 2. The Software may not be included in any dataset used for training or improving machine learning algorithms,
* including but not limited to artificial intelligence, natural language processing, or data mining.
*
* 3. Any person or organization found to be in violation of these restrictions will be subject to legal action and may be held liable
* for any damages resulting from such use.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package main
import (
"bytes"
"context"
"crypto/sha256"
"fmt"
"testing"
pbCommon "github.com/crypto-bundle/bc-wallet-common-hdwallet-controller/pkg/grpc/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/google/uuid"
"github.com/tyler-smith/go-bip39"
"google.golang.org/protobuf/types/known/anypb"
)
func TestNewPoolUnit(t *testing.T) {
entropy, err := bip39.NewEntropy(256)
if err != nil {
t.Fatalf("%s: %e", "unable to create entropy:", err)
}
mnemonic, err := bip39.NewMnemonic(entropy)
if err != nil {
t.Fatalf("%s: %e", "unable to create mnemonic pharase from entory:", err)
}
_, err = NewPoolUnit(uuid.NewString(), mnemonic)
if err != nil {
t.Fatalf("%s: %e", "unable to create mnemonic wallet pool unit:", err)
}
}
func TestMnemonicWalletUnit_GetWalletUUID(t *testing.T) {
type testCase struct {
WalletUUID string
Mnemonic string
AddressPath *pbCommon.DerivationAddressIdentity
ExpectedAddress string
}
// WARN: DO NOT USE THESE MNEMONIC IN MAINNET OR TESTNET. Usage only in unit-tests
// WARN: DO NOT USE THESE MNEMONIC IN MAINNET OR TESTNET. Usage only in unit-tests
// WARN: DO NOT USE THESE MNEMONIC IN MAINNET OR TESTNET. Usage only in unit-tests
tCase := &testCase{
WalletUUID: uuid.NewString(),
Mnemonic: "seven kitten wire trap family giraffe globe access dinosaur upper forum aerobic dash segment cruise concert giant upon sniff armed rain royal firm state",
AddressPath: &pbCommon.DerivationAddressIdentity{
AccountIndex: 5,
InternalIndex: 5,
AddressIndex: 55,
},
ExpectedAddress: "TRZUb6GVH922CHYty9NaFpVZWuf8GZJ3va",
}
poolUnitIntrf, loopErr := NewPoolUnit(tCase.WalletUUID, tCase.Mnemonic)
if loopErr != nil {
t.Fatalf("%s: %e", "unable to create mnemonic wallet pool unit:", loopErr)
}
poolUnit, ok := poolUnitIntrf.(*mnemonicWalletUnit)
if !ok {
t.Fatalf("%s", "unable to cast interface to pool unit worker")
}
accountIdentity := &anypb.Any{}
_ = accountIdentity.MarshalFrom(tCase.AddressPath)
addr, loopErr := poolUnit.GetAccountAddress(context.Background(), accountIdentity)
if loopErr != nil {
t.Fatalf("%s: %e", "unable to get address from pool unit", loopErr)
}
if addr == nil {
t.Fatalf("%s", "missing address in pool unit result")
}
if tCase.ExpectedAddress != *addr {
t.Fatalf("%s", "address not equal with expected")
}
resultUUID := poolUnit.GetWalletUUID()
if tCase.WalletUUID != resultUUID {
t.Fatalf("%s", "wallet uuid not equal with expected")
}
}
func TestMnemonicWalletUnit_GetAccountAddress(t *testing.T) {
type testCase struct {
Mnemonic string
AddressPath *pbCommon.DerivationAddressIdentity
ExpectedAddress string
}
// WARN: DO NOT USE THESE MNEMONICS IN MAINNET OR TESTNET. Usage only in unit-tests
// WARN: DO NOT USE THESE MNEMONICS IN MAINNET OR TESTNET. Usage only in unit-tests
// WARN: DO NOT USE THESE MNEMONICS IN MAINNET OR TESTNET. Usage only in unit-tests
testCases := []*testCase{
{
Mnemonic: "unfair silver dune air rib enforce protect limit jazz dinner thumb drift spring warrior bonus snack argue flavor wild faculty derive open dynamic carpet",
AddressPath: &pbCommon.DerivationAddressIdentity{
AccountIndex: 3,
InternalIndex: 13,
AddressIndex: 114,
},
ExpectedAddress: "TFyMUdJsREv3Q1ooMhV5r2UDGFSL4xgFeC",
},
{
Mnemonic: "obscure town quick bundle north message want sketch brass tone vast spoil home gentle field ozone mushroom current math cat canvas plunge stay truly",
AddressPath: &pbCommon.DerivationAddressIdentity{
AccountIndex: 1020,
InternalIndex: 10300,
AddressIndex: 104000,
},
ExpectedAddress: "TBKmbAG6JefDEg741YpsMPTB7MegySqs45",
},
{
Mnemonic: "beach large spray gentle buyer hover flock dream hybrid match whip ten mountain pitch enemy lobster afford barrel patrol desk trigger output excuse truck",
AddressPath: &pbCommon.DerivationAddressIdentity{
AccountIndex: 2,
InternalIndex: 104,
AddressIndex: 1005,
},
ExpectedAddress: "TWW7CQdsogbqfc5FrSb6MKu22QS4Reg3mH",
},
}
for _, tCase := range testCases {
poolUnitIntrf, loopErr := NewPoolUnit(uuid.NewString(), tCase.Mnemonic)
if loopErr != nil {
t.Fatalf("%s: %e", "unable to create mnemonic wallet pool unit:", loopErr)
}
poolUnit, ok := poolUnitIntrf.(*mnemonicWalletUnit)
if !ok {
t.Fatalf("%s", "unable to cast interface to pool unit worker")
}
accountIdentity := &anypb.Any{}
_ = accountIdentity.MarshalFrom(tCase.AddressPath)
addr, loopErr := poolUnit.GetAccountAddress(context.Background(), accountIdentity)
if loopErr != nil {
t.Fatalf("%s: %e", "unable to get address from pool unit:", loopErr)
}
if addr == nil {
t.Fatalf("%s", "missing address in pool unit result")
}
if tCase.ExpectedAddress != *addr {
t.Fatalf("%s", "address not equal with expected")
}
loopErr = poolUnit.Shutdown(context.Background())
if loopErr != nil {
t.Fatalf("%s", "unable to shurdown pool unit")
}
}
}
func TestMnemonicWalletUnit_GetMultipleAccounts_3_by_50(t *testing.T) {
type testCase struct {
Mnemonic string
AddressPathList *pbCommon.RangeUnitsList
}
// WARN: DO NOT USE THESE MNEMONICS IN MAINNET OR TESTNET. Usage only in unit-tests
// WARN: DO NOT USE THESE MNEMONICS IN MAINNET OR TESTNET. Usage only in unit-tests
// WARN: DO NOT USE THESE MNEMONICS IN MAINNET OR TESTNET. Usage only in unit-tests
testCases := []*testCase{
{
Mnemonic: "web account soft juice relief green account rebel rifle gun follow thunder ski credit judge off educate round advice allow wink bitter first color",
AddressPathList: &pbCommon.RangeUnitsList{
RangeUnits: []*pbCommon.RangeRequestUnit{
{AccountIndex: 3, InternalIndex: 8, AddressIndexFrom: 150, AddressIndexTo: 200},
{AccountIndex: 155, InternalIndex: 5, AddressIndexFrom: 4, AddressIndexTo: 54},
{AccountIndex: 2555, InternalIndex: 50, AddressIndexFrom: 250, AddressIndexTo: 300},
},
},
},
}
for _, tCase := range testCases {
poolUnitIntrf, loopErr := NewPoolUnit(uuid.NewString(), tCase.Mnemonic)
if loopErr != nil {
t.Fatalf("%s: %e", "unable to create mnemonic wallet pool unit:", loopErr)
}
poolUnit, ok := poolUnitIntrf.(*mnemonicWalletUnit)
if !ok {
t.Fatalf("%s", "unable to cast interface to pool unit worker")
}
anyRangeUnit := &anypb.Any{}
err := anyRangeUnit.MarshalFrom(tCase.AddressPathList)
if err != nil {
t.Fatalf("%s", "unable to marshal request units list")
}
count, addrList, loopErr := poolUnit.GetMultipleAccounts(context.Background(), anyRangeUnit)
if loopErr != nil {
t.Fatalf("%s: %e", "unable to get address from pool unit:", loopErr)
}
if count == 0 {
t.Fatalf("%s", "addr list count not equal with expected")
}
if addrList == nil {
t.Fatalf("%s", "missing addr lsit in pool unit result")
}
if count != uint(len(addrList)) {
t.Fatalf("%s", "length of addrlist count not equal with count rseult value")
}
if count != 153 {
t.Fatalf("%s", "count value or getAccount result not equal with values count of expected map")
}
err = poolUnit.Shutdown(context.Background())
if err != nil {
t.Fatalf("%s", "unable to shurdown pool unit")
}
}
}
func TestMnemonicWalletUnit_GetMultipleAccounts(t *testing.T) {
type testCase struct {
Mnemonic string
AddressPathList *pbCommon.RangeUnitsList
ExpectedAddress map[string]*pbCommon.DerivationAddressIdentity
}
// WARN: DO NOT USE THESE MNEMONICS IN MAINNET OR TESTNET. Usage only in unit-tests
// WARN: DO NOT USE THESE MNEMONICS IN MAINNET OR TESTNET. Usage only in unit-tests
// WARN: DO NOT USE THESE MNEMONICS IN MAINNET OR TESTNET. Usage only in unit-tests
testCases := []*testCase{
{
Mnemonic: "web account soft juice relief green account rebel rifle gun follow thunder ski credit judge off educate round advice allow wink bitter first color",
AddressPathList: &pbCommon.RangeUnitsList{
RangeUnits: []*pbCommon.RangeRequestUnit{
{AccountIndex: 3, InternalIndex: 8, AddressIndexFrom: 114, AddressIndexTo: 124},
{AccountIndex: 155, InternalIndex: 5, AddressIndexFrom: 4, AddressIndexTo: 8},
{AccountIndex: 2555, InternalIndex: 50, AddressIndexFrom: 250, AddressIndexTo: 255},
},
},
ExpectedAddress: map[string]*pbCommon.DerivationAddressIdentity{
"TAMcYXxBevDjYK1wVBoUsGr9F1Gc1BvgYX": {AccountIndex: 3, InternalIndex: 8, AddressIndex: 114},
"TAT4RTK3FKQbgevkiTATT9EnFar8hK3hzK": {AccountIndex: 3, InternalIndex: 8, AddressIndex: 115},
"TU88zp2gvowrvxxuskzbZhRWPcdB96yQNi": {AccountIndex: 3, InternalIndex: 8, AddressIndex: 116},
"THDKHgZxjEDfUWh7bQuF3eB3q2cipeqWrM": {AccountIndex: 3, InternalIndex: 8, AddressIndex: 117},
"TMkLVEeyLBcWPTqkxvLBpq37U7ZXBfHDhZ": {AccountIndex: 3, InternalIndex: 8, AddressIndex: 118},
"TSq5zADYT1GK4zedGCQT2Gh8EtWDn61iEd": {AccountIndex: 3, InternalIndex: 8, AddressIndex: 119},
"THtKSpFy4owX3NjNzsqobLdWda37Z5B7Gv": {AccountIndex: 3, InternalIndex: 8, AddressIndex: 120},
"TAAnRtjKHzhvkC1XGfDuVxkfMt6u5UdJ7P": {AccountIndex: 3, InternalIndex: 8, AddressIndex: 121},
"TUBsnau67pNS5yqpYSJ1SzQ1CsjKJsTPwS": {AccountIndex: 3, InternalIndex: 8, AddressIndex: 122},
"TKdbf5oMFYxWSLSUKnZbbdBGLg1zcypKzj": {AccountIndex: 3, InternalIndex: 8, AddressIndex: 123},
"TUc2Q73GuLxupQ7BCZnxwjhDiTPC6sro4A": {AccountIndex: 3, InternalIndex: 8, AddressIndex: 124},
//
"TE6JoSwxS1EdannPwasPPWrt6GcjjucDAU": {AccountIndex: 155, InternalIndex: 5, AddressIndex: 4},
"TFjUm8k4BKpVAG9iqhJhihbRW2Gn454Z3r": {AccountIndex: 155, InternalIndex: 5, AddressIndex: 5},
"TTLjtMRL2V3v2NUGbigFGejCkF5DDtLvVp": {AccountIndex: 155, InternalIndex: 5, AddressIndex: 6},
"TGDoLJNa5oT46wSsVvFiY18YcoNXE2AaMQ": {AccountIndex: 155, InternalIndex: 5, AddressIndex: 7},
"TZ8z3nS98FBQiZSFifLF6m9h23EDFrgA6e": {AccountIndex: 155, InternalIndex: 5, AddressIndex: 8},
//
"TDA3ZnCuYPJNdGiax5XfVFVh4fm3beayuk": {AccountIndex: 2555, InternalIndex: 50, AddressIndex: 250},
"TWMgaesoun1GVbhjfKBYqvRb5PJU6eW9xk": {AccountIndex: 2555, InternalIndex: 50, AddressIndex: 251},
"TRQdPDaeHd2ZbZGKGkASx6oX3jTPHsjkwf": {AccountIndex: 2555, InternalIndex: 50, AddressIndex: 252},
"TCALJChrKN5Q9FjCZpYGTL5KGipE66ZF94": {AccountIndex: 2555, InternalIndex: 50, AddressIndex: 253},
"TDpavWFBnCUbTSZF6yxk4UdGa4yPCNpaiY": {AccountIndex: 2555, InternalIndex: 50, AddressIndex: 254},
"TXeLYYR6bE29aRA8pAfPqbu1Svx3hZgdVg": {AccountIndex: 2555, InternalIndex: 50, AddressIndex: 255},
},
},
}
for _, tCase := range testCases {
poolUnitIntrf, loopErr := NewPoolUnit(uuid.NewString(), tCase.Mnemonic)
if loopErr != nil {
t.Fatalf("%s: %e", "unable to create mnemonic wallet pool unit:", loopErr)
}
poolUnit, ok := poolUnitIntrf.(*mnemonicWalletUnit)
if !ok {
t.Fatalf("%s", "unable to cast interface to pool unit worker")
}
anyRangeUnit := &anypb.Any{}
err := anyRangeUnit.MarshalFrom(tCase.AddressPathList)
if err != nil {
t.Fatalf("%s", "unable to marshal request units list")
}
count, addrList, loopErr := poolUnit.GetMultipleAccounts(context.Background(), anyRangeUnit)
if loopErr != nil {
t.Fatalf("%s: %e", "unable to get address from pool unit:", loopErr)
}
if count == 0 {
t.Fatalf("%s", "addr list count not equal with expected")
}
if addrList == nil {
t.Fatalf("%s", "missing addr lsit in pool unit result")
}
if count != uint(len(addrList)) {
t.Fatalf("%s", "length of addrlist count not equal with count rseult value")
}
if count != uint(len(tCase.ExpectedAddress)) {
t.Fatalf("%s", "count value or getAccount result not equal with values count of expected map")
}
for i := 0; i != len(addrList); i++ {
addr := addrList[i]
accIdentifier, isExists := tCase.ExpectedAddress[addr.Address]
if !isExists {
t.Fatalf("%s", "missing addr in expected map")
}
marshaledAccIdentifier := &pbCommon.DerivationAddressIdentity{}
marshalErr := addr.Parameters.UnmarshalTo(marshaledAccIdentifier)
if marshalErr != nil {
t.Fatalf("%s: %e", "unable to unmarshal anydata to account identity", marshalErr)
}
if accIdentifier.AddressIndex != marshaledAccIdentifier.AddressIndex {
t.Fatalf("%s", "marshaled address index not equal with expected")
}
if accIdentifier.InternalIndex != marshaledAccIdentifier.InternalIndex {
t.Fatalf("%s", "marshaled internal index not equal with expected")
}
if accIdentifier.AccountIndex != marshaledAccIdentifier.AccountIndex {
t.Fatalf("%s", "marshaled account index not equal with expected")
}
}
err = poolUnit.Shutdown(context.Background())
if err != nil {
t.Fatalf("%s", "unable to shurdown pool unit")
}
}
}
func TestMnemonicWalletUnit_LoadAddressByPath(t *testing.T) {
type testCase struct {
Mnemonic string
AddressPath *pbCommon.DerivationAddressIdentity
ExpectedAddress string
}
// WARN: DO NOT USE THESE MNEMONICS IN MAINNET OR TESTNET. Usage only in unit-tests
// WARN: DO NOT USE THESE MNEMONICS IN MAINNET OR TESTNET. Usage only in unit-tests
// WARN: DO NOT USE THESE MNEMONICS IN MAINNET OR TESTNET. Usage only in unit-tests
testCases := []*testCase{
{
Mnemonic: "umbrella uphold security hill monkey skin either immense kid afraid sense desk extend twenty doctor odor buzz reject derive frame hub much once suffer",
AddressPath: &pbCommon.DerivationAddressIdentity{
AccountIndex: 5,
InternalIndex: 12,
AddressIndex: 3,
},
ExpectedAddress: "TS98RrhGNPeXNXqFYhYfeFd2AAUK7z5aED",
},
{
Mnemonic: "slogan follow oil world head protect patrol wagon toddler fly kangaroo kite dash essay shoulder worth one grace shift good disease biology magic pottery",
AddressPath: &pbCommon.DerivationAddressIdentity{
AccountIndex: 1000,
InternalIndex: 10000,
AddressIndex: 100000,
},
ExpectedAddress: "TZ8Tenb9okzq4x626vuux3p3SMXahu3LyG",
},
{
Mnemonic: "image video differ dumb later child gather smart supply mountain salon ring boy mystery hope secret present bar then joke latin guitar view devote",
AddressPath: &pbCommon.DerivationAddressIdentity{
AccountIndex: 1,
InternalIndex: 102,
AddressIndex: 1003,
},
ExpectedAddress: "TLFMBiQvLjhp9AK9N2wvgc77dsuKBuLsiV",
},
}
for _, tCase := range testCases {
poolUnitIntrf, err := NewPoolUnit(uuid.NewString(), tCase.Mnemonic)
if err != nil {
t.Fatalf("%s: %e", "unable to create mnemonic wallet pool unit:", err)
}
poolUnit, ok := poolUnitIntrf.(*mnemonicWalletUnit)
if !ok {
t.Fatalf("%s", "unable to cast interface to pool unit worker")
}
accountIdentity := &anypb.Any{}
_ = accountIdentity.MarshalFrom(tCase.AddressPath)
addr, err := poolUnit.LoadAccount(context.Background(), accountIdentity)
if err != nil {
t.Fatalf("%s: %e", "unable to get address from pool unit:", err)
}
if addr == nil {
t.Fatalf("%s: %e", "missing address in pool unit result:", err)
}
if len(poolUnit.addressPool) == 0 {
t.Fatalf("%s", "address in pool not loaded")
}
key := fmt.Sprintf(addrPatKeyTemplate, tCase.AddressPath.AccountIndex,
tCase.AddressPath.InternalIndex, tCase.AddressPath.AddressIndex)
addrData, ok := poolUnit.addressPool[key]
if !ok || addrData == nil {
t.Fatalf("%s", "missing data by key in address pool")
}
if addrData.privateKey == nil {
t.Fatalf("%s", "missing private key in address pool unit")
}
if tCase.ExpectedAddress != addrData.address {
t.Fatalf("%s", "address not equal with expected")
}
if tCase.ExpectedAddress != *addr {
t.Fatalf("%s", "address not equal with expected")
}
}
}
func TestMnemonicWalletUnit_SignData(t *testing.T) {
type testCase struct {
Mnemonic string
AddressPath *pbCommon.DerivationAddressIdentity
AddressPublicKey string
DataForSign []byte
ExpectedAddress string
ExpectedSignedData []byte
}
// WARN: DO NOT USE THESE MNEMONICS IN MAINNET OR TESTNET. Usage only in unit-tests
// WARN: DO NOT USE THESE MNEMONICS IN MAINNET OR TESTNET. Usage only in unit-tests
// WARN: DO NOT USE THESE MNEMONICS IN MAINNET OR TESTNET. Usage only in unit-tests
testCases := []*testCase{
{
Mnemonic: "unknown valid carbon hat echo funny artist letter desk absorb unit fatigue foil skirt stay case path rescue hawk remember aware arch regular cry",
AddressPath: &pbCommon.DerivationAddressIdentity{
AccountIndex: 7,
InternalIndex: 8,
AddressIndex: 9,
},
DataForSign: []byte{0x0, 0x2, 0x3, 0x4},
ExpectedAddress: "TS5nGhVnjSVudb58XLUCqYTgYtjY7abJJH",
},
{
Mnemonic: "laundry file mystery rate absorb wrist despair cook near afraid account mirror name chair lake regular vicious oblige release vicious identify glimpse flight help",
AddressPath: &pbCommon.DerivationAddressIdentity{
AccountIndex: 909,
InternalIndex: 8008,
AddressIndex: 70007,
},
DataForSign: []byte{0x5, 0x6, 0x7, 0x8},
ExpectedAddress: "THjwZivHc9kyosKYTF7MJLTqeCq2xdMgL6",
},
{
Mnemonic: "busy spawn solar december element round wild buddy furnace help clog tired object camera resist maze fuel need stock rule spot diagram aisle expect",
AddressPath: &pbCommon.DerivationAddressIdentity{
AccountIndex: 9,
InternalIndex: 8,
AddressIndex: 7,
},
AddressPublicKey: "030ba1318a2d4258cecce5725c393e3a6ab7d60cde9e6f39106cd99cf63aa36032",
DataForSign: []byte{0x9, 0x10, 0x11, 0x12},
ExpectedAddress: "TPuC6Y4aFGZPu1kfiiWgkPVSUgi2oYynRz",
},
}
for _, tCase := range testCases {
poolUnitIntrf, loopErr := NewPoolUnit(uuid.NewString(), tCase.Mnemonic)
if loopErr != nil {
t.Fatalf("%s: %e", "unable to create mnemonic wallet pool unit:", loopErr)
}
poolUnit, ok := poolUnitIntrf.(*mnemonicWalletUnit)
if !ok {
t.Fatalf("%s", "unable to cast interface to pool unit worker")
}
accountIdentity := &anypb.Any{}
_ = accountIdentity.MarshalFrom(tCase.AddressPath)
addr, signedData, loopErr := poolUnit.SignData(context.Background(), accountIdentity, tCase.DataForSign)
if loopErr != nil {
t.Fatalf("%s: %e", "unable to sign data:", loopErr)
}
if addr == nil {
t.Fatalf("%s", "missing address in result of sign method")
}
if signedData == nil {
t.Fatalf("%s", "missing signed data in result of sign method")
}
if len(poolUnit.addressPool) == 0 {
t.Fatalf("%s", "address in pool not loaded")
}
key := fmt.Sprintf(addrPatKeyTemplate, tCase.AddressPath.AccountIndex,
tCase.AddressPath.InternalIndex, tCase.AddressPath.AddressIndex)
addrData, ok := poolUnit.addressPool[key]
if !ok || addrData == nil {
t.Fatalf("%s", "missing data by key in address pool")
}
if addrData.privateKey == nil {
t.Fatalf("%s", "missing private key in address pool unit")
}
if tCase.ExpectedAddress != addrData.address {
t.Fatalf("%s", "address not equal with expected")
}
if tCase.ExpectedAddress != *addr {
t.Fatalf("%s", "address not equal with expected")
}
signed := bytes.Clone(signedData)
// DIRTY HACK
// https://stackoverflow.com/questions/49085737/geth-ecrecover-invalid-signature-recovery-id
// https://gist.github.com/dcb9/385631846097e1f59e3cba3b1d42f3ed#file-eth_sign_verify-go
if signed[crypto.RecoveryIDOffset] == 27 || signed[crypto.RecoveryIDOffset] == 28 {
signed[crypto.RecoveryIDOffset] -= 27 // Transform yellow paper V from 27/28 to 0/1
}
h256h := sha256.New()
h256h.Write(tCase.DataForSign)
hash := h256h.Sum(nil)
h256h.Reset()
h256h = nil
pubKey, loopErr := crypto.SigToPub(hash, signed)
if loopErr != nil {
t.Fatalf("%s: %e", "unable to get public key from signed message", loopErr)
}
tronAddr := pubKeyToTronAddress(*pubKey)
if tCase.ExpectedAddress != tronAddr {
t.Fatalf("%s", "tron addr from pubKey not equal with expected")
}
}
}
func TestMnemonicWalletUnit_UnloadWallet(t *testing.T) {
type testCase struct {
Mnemonic string
AddressPath *pbCommon.DerivationAddressIdentity
ExpectedAddress string
}
// WARN: DO NOT USE THESE MNEMONICS IN MAINNET OR TESTNET. Usage only in unit-tests
// WARN: DO NOT USE THESE MNEMONICS IN MAINNET OR TESTNET. Usage only in unit-tests
// WARN: DO NOT USE THESE MNEMONICS IN MAINNET OR TESTNET. Usage only in unit-tests
testCases := []*testCase{
{
Mnemonic: "input erase buzz crew miss auction habit cargo wrestle perfect like midnight buddy chase grit only treat stuff rival worth alien tennis parent artist",
AddressPath: &pbCommon.DerivationAddressIdentity{
AccountIndex: 5,
InternalIndex: 8,
AddressIndex: 11,
},
ExpectedAddress: "TNnvBFnjrsdTqCnjPRsZZP24pPA1VYUqAi",
},
{
Mnemonic: "empower plate axis divorce neither noodle above flight very indoor zone mango sand exhaust nominee solid combine picnic gospel myth stem raw garage veteran",
AddressPath: &pbCommon.DerivationAddressIdentity{
AccountIndex: 2,
InternalIndex: 4,
AddressIndex: 8,
},
ExpectedAddress: "TWnoUuXdREoJFFc2vAuwHPUz33tuKJaonK",
},
{
Mnemonic: "sea vault tattoo laugh ugly where saddle six usage install one cube affair sick used actress zebra fuel sunny tackle can siege develop drop",
AddressPath: &pbCommon.DerivationAddressIdentity{
AccountIndex: 8,
InternalIndex: 64,
AddressIndex: 4096,
},
ExpectedAddress: "TDXRtZoqjkJxtr68deRyKJ5Kkkf8u4kJS1",
},
}
for _, tCase := range testCases {
poolUnitIntrf, loopErr := NewPoolUnit(uuid.NewString(), tCase.Mnemonic)
if loopErr != nil {
t.Fatalf("%s: %e", "unable to create mnemonic wallet pool unit:", loopErr)
}
poolUnit, ok := poolUnitIntrf.(*mnemonicWalletUnit)
if !ok {
t.Fatalf("%s", "unable to cast interface to pool unit worker")
}
accountIdentity := &anypb.Any{}
_ = accountIdentity.MarshalFrom(tCase.AddressPath)
addr, loopErr := poolUnit.LoadAccount(context.Background(), accountIdentity)
if loopErr != nil {
t.Fatalf("%s: %e", "unable to sign data:", loopErr)
}
if addr == nil {
t.Fatalf("%s", "missing address in result of sign method")
}
if len(poolUnit.addressPool) == 0 {
t.Fatalf("%s", "address in pool not loaded")
}
key := fmt.Sprintf(addrPatKeyTemplate, tCase.AddressPath.AccountIndex,
tCase.AddressPath.InternalIndex, tCase.AddressPath.AddressIndex)
addrData, ok := poolUnit.addressPool[key]
if !ok || addrData == nil {
t.Fatalf("%s", "missing data by key in address pool")
}
loopErr = poolUnit.UnloadWallet()
if loopErr != nil {
t.Fatalf("%s: %e", "unable to unload wallet", loopErr)
}
if len(poolUnit.addressPool) != 0 {
t.Fatalf("%s", "address pool is not empty")
}
if poolUnit.hdWalletSvc != nil {
t.Fatalf("%s", "hdwallet service is not nil")
}
if poolUnit.mnemonicHash != "0" {
t.Fatalf("%s", "mnemonicHash is not equal zero")
}
}
}