-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
hd_key_test.go
1117 lines (963 loc) · 40.2 KB
/
hd_key_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
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package bitcoin
import (
"encoding/hex"
"fmt"
"testing"
"github.com/libsv/go-bk/bec"
"github.com/libsv/go-bk/bip32"
"github.com/libsv/go-bt/v2/bscript"
"github.com/stretchr/testify/assert"
)
// TestGenerateHDKey will test the method GenerateHDKey()
func TestGenerateHDKey(t *testing.T) {
t.Parallel()
var tests = []struct {
inputSeed uint8
expectedNil bool
expectedError bool
}{
{0, false, false},
{1, true, true},
{15, true, true},
{65, true, true},
{RecommendedSeedLength, false, false},
{SecureSeedLength, false, false},
}
for _, test := range tests {
if hdKey, err := GenerateHDKey(test.inputSeed); err != nil && !test.expectedError {
t.Fatalf("%s Failed: [%d] inputted and error not expected but got: %s", t.Name(), test.inputSeed, err.Error())
} else if err == nil && test.expectedError {
t.Fatalf("%s Failed: [%d] inputted and error was expected", t.Name(), test.inputSeed)
} else if hdKey == nil && !test.expectedNil {
t.Fatalf("%s Failed: [%d] inputted and was nil but not expected", t.Name(), test.inputSeed)
} else if hdKey != nil && test.expectedNil {
t.Fatalf("%s Failed: [%d] inputted and was NOT nil but expected to be nil", t.Name(), test.inputSeed)
}
}
}
// ExampleGenerateHDKey example using GenerateHDKey()
func ExampleGenerateHDKey() {
hdKey, err := GenerateHDKey(SecureSeedLength)
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
// Cannot show the private/public key since they change each time
fmt.Printf("created HD key successfully! (length: %d)", len(hdKey.String()))
// Output:created HD key successfully! (length: 111)
}
// BenchmarkGenerateHDKey benchmarks the method GenerateHDKey()
func BenchmarkGenerateHDKey(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = GenerateHDKey(RecommendedSeedLength)
}
}
// BenchmarkGenerateHDKeySecure benchmarks the method GenerateHDKey()
func BenchmarkGenerateHDKeySecure(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = GenerateHDKey(SecureSeedLength)
}
}
// TestGenerateHDKeyPair will test the method GenerateHDKeyPair()
func TestGenerateHDKeyPair(t *testing.T) {
t.Parallel()
var tests = []struct {
inputSeed uint8
expectedError bool
}{
{0, false},
{1, true},
{15, true},
{65, true},
{RecommendedSeedLength, false},
{SecureSeedLength, false},
}
for _, test := range tests {
if privateKey, publicKey, err := GenerateHDKeyPair(test.inputSeed); err != nil && !test.expectedError {
t.Fatalf("%s Failed: [%d] inputted and error not expected but got: %s", t.Name(), test.inputSeed, err.Error())
} else if err == nil && test.expectedError {
t.Fatalf("%s Failed: [%d] inputted and error was expected", t.Name(), test.inputSeed)
} else if err == nil && len(privateKey) == 0 {
t.Fatalf("%s Failed: [%d] inputted and private key was empty", t.Name(), test.inputSeed)
} else if err == nil && len(publicKey) == 0 {
t.Fatalf("%s Failed: [%d] inputted and pubic key was empty", t.Name(), test.inputSeed)
}
}
}
// ExampleGenerateHDKeyPair example using GenerateHDKeyPair()
func ExampleGenerateHDKeyPair() {
xPrivateKey, xPublicKey, err := GenerateHDKeyPair(SecureSeedLength)
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
// Cannot show the private/public key since they change each time
fmt.Printf("created HD key successfully! (xPrivateKey length: %d) (xPublicKey length: %d)", len(xPrivateKey), len(xPublicKey))
// Output:created HD key successfully! (xPrivateKey length: 111) (xPublicKey length: 111)
}
// BenchmarkGenerateHDKeyPair benchmarks the method GenerateHDKeyPair()
func BenchmarkGenerateHDKeyPair(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _, _ = GenerateHDKeyPair(RecommendedSeedLength)
}
}
// BenchmarkGenerateHDKeyPairSecure benchmarks the method GenerateHDKeyPair()
func BenchmarkGenerateHDKeyPairSecure(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _, _ = GenerateHDKeyPair(SecureSeedLength)
}
}
// TestGetPrivateKeyByPath will test the method GetPrivateKeyByPath()
func TestGetPrivateKeyByPath(t *testing.T) {
t.Parallel()
// Generate a valid key
validKey, err := GenerateHDKey(RecommendedSeedLength)
if err != nil {
t.Fatalf("error occurred: %s", err.Error())
}
var tests = []struct {
inputHDKey *bip32.ExtendedKey
inputChain uint32
inputNum uint32
expectedNil bool
expectedError bool
}{
// {nil, 0, 0, true, true},
{validKey, 0, 0, false, false},
{validKey, 10, 10, false, false},
{validKey, 100, 100, false, false},
{validKey, 2 ^ 31 + 1, 2 ^ 32 - 1, false, false},
{validKey, 1 << 8, 1 << 8, false, false},
{validKey, 1 << 9, 1 << 9, false, false},
{validKey, 1 << 10, 1 << 10, false, false},
{validKey, 1 << 11, 1 << 11, false, false},
{validKey, 1 << 12, 1 << 12, false, false},
{validKey, 1 << 16, 1 << 16, false, false},
{validKey, 1<<32 - 1, 1<<32 - 1, false, false},
}
var privateKey *bec.PrivateKey
for _, test := range tests {
if privateKey, err = GetPrivateKeyByPath(test.inputHDKey, test.inputChain, test.inputNum); err != nil && !test.expectedError {
t.Fatalf("%s Failed: [%v] [%d] [%d] inputted and error not expected but got: %s", t.Name(), test.inputHDKey, test.inputChain, test.inputNum, err.Error())
} else if err == nil && test.expectedError {
t.Fatalf("%s Failed: [%v] [%d] [%d] inputted and error was expected", t.Name(), test.inputHDKey, test.inputChain, test.inputNum)
} else if privateKey == nil && !test.expectedNil {
t.Fatalf("%s Failed: [%v] [%d] [%d] inputted and was nil but not expected", t.Name(), test.inputHDKey, test.inputChain, test.inputNum)
} else if privateKey != nil && test.expectedNil {
t.Fatalf("%s Failed: [%v] [%d] [%d] inputted and was NOT nil but expected to be nil", t.Name(), test.inputHDKey, test.inputChain, test.inputNum)
} else if privateKey != nil && len(hex.EncodeToString(privateKey.Serialise())) == 0 {
t.Fatalf("%s Failed: [%v] [%d] [%d] inputted and should not be empty", t.Name(), test.inputHDKey, test.inputChain, test.inputNum)
}
}
}
// TestGetPrivateKeyByPathPanic tests for nil case in GetPrivateKeyByPath()
func TestGetPrivateKeyByPathPanic(t *testing.T) {
t.Parallel()
assert.Panics(t, func() {
_, err := GetPrivateKeyByPath(nil, 0, 1)
assert.Error(t, err)
})
}
// ExampleGetPrivateKeyByPath example using GetPrivateKeyByPath()
func ExampleGetPrivateKeyByPath() {
hdKey, err := GenerateHDKey(SecureSeedLength)
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
// Get a private key at the path
var privateKey *bec.PrivateKey
privateKey, err = GetPrivateKeyByPath(hdKey, 0, 1)
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
fmt.Printf("private key (%d) found at path %d/%d", len(privateKey.Serialise()), 0, 1)
// Output:private key (32) found at path 0/1
}
// BenchmarkGetPrivateKeyByPath benchmarks the method GetPrivateKeyByPath()
func BenchmarkGetPrivateKeyByPath(b *testing.B) {
hdKey, _ := GenerateHDKey(SecureSeedLength)
for i := 0; i < b.N; i++ {
_, _ = GetPrivateKeyByPath(hdKey, 0, 1)
}
}
// TestGetHDKeyByPath will test the method GetHDKeyByPath()
func TestGetHDKeyByPath(t *testing.T) {
t.Parallel()
// Generate a valid key
validKey, err := GenerateHDKey(RecommendedSeedLength)
assert.NoError(t, err)
assert.NotNil(t, validKey)
// Max depth key
/*
var maxKey *bip32.ExtendedKey
maxKey, err = GetHDKeyByPath(validKey, 1<<9, 1<<9)
if err != nil {
t.Fatalf("error occurred: %s", err.Error())
}
*/
// Test depth limit
// todo: make a better test (after 126 maxKey is now nil)
/*
for i := 0; i < 1<<8-1; i++ {
maxKey, err = GetHDKeyByPath(maxKey, uint32(i), uint32(i))
if i >= 126 && err == nil {
t.Fatalf("expected to hit depth limit on HD key index: %d", i)
}
}
*/
var tests = []struct {
inputHDKey *bip32.ExtendedKey
inputChain uint32
inputNum uint32
expectedNil bool
expectedError bool
}{
{validKey, 0, 0, false, false},
{validKey, 10, 10, false, false},
{validKey, 100, 100, false, false},
{validKey, 2 ^ 31 + 1, 2 ^ 32 - 1, false, false},
{validKey, 1 << 8, 1 << 8, false, false},
{validKey, 1 << 9, 1 << 9, false, false},
{validKey, 1 << 10, 1 << 10, false, false},
{validKey, 1 << 11, 1 << 11, false, false},
{validKey, 1 << 12, 1 << 12, false, false},
{validKey, 1 << 16, 1 << 16, false, false},
{validKey, 1<<32 - 1, 1<<32 - 1, false, false},
}
for _, test := range tests {
if hdKey, err := GetHDKeyByPath(test.inputHDKey, test.inputChain, test.inputNum); err != nil && !test.expectedError {
t.Fatalf("%s Failed: [%v] [%d] [%d] inputted and error not expected but got: %s", t.Name(), test.inputHDKey, test.inputChain, test.inputNum, err.Error())
} else if err == nil && test.expectedError {
t.Fatalf("%s Failed: [%v] [%d] [%d] inputted and error was expected", t.Name(), test.inputHDKey, test.inputChain, test.inputNum)
} else if hdKey == nil && !test.expectedNil {
t.Fatalf("%s Failed: [%v] [%d] [%d] inputted and was nil but not expected", t.Name(), test.inputHDKey, test.inputChain, test.inputNum)
} else if hdKey != nil && test.expectedNil {
t.Fatalf("%s Failed: [%v] [%d] [%d] inputted and was NOT nil but expected to be nil", t.Name(), test.inputHDKey, test.inputChain, test.inputNum)
} else if hdKey != nil && len(hdKey.String()) == 0 {
t.Fatalf("%s Failed: [%v] [%d] [%d] inputted and should not be empty", t.Name(), test.inputHDKey, test.inputChain, test.inputNum)
}
}
}
// TestGetHDKeyByPathPanic tests for nil case in GetHDKeyByPath()
func TestGetHDKeyByPathPanic(t *testing.T) {
t.Parallel()
assert.Panics(t, func() {
_, err := GetHDKeyByPath(nil, 0, 1)
assert.Error(t, err)
})
}
// ExampleGetHDKeyByPath example using GetHDKeyByPath()
func ExampleGetHDKeyByPath() {
hdKey, err := GenerateHDKey(SecureSeedLength)
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
// Get a child key
var childKey *bip32.ExtendedKey
childKey, err = GetHDKeyByPath(hdKey, 0, 1)
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
fmt.Printf("hd key (%d) found at path %d/%d", len(childKey.String()), 0, 1)
// Output:hd key (111) found at path 0/1
}
// BenchmarkGetHDKeyByPath benchmarks the method GetHDKeyByPath()
func BenchmarkGetHDKeyByPath(b *testing.B) {
hdKey, _ := GenerateHDKey(SecureSeedLength)
for i := 0; i < b.N; i++ {
_, _ = GetHDKeyByPath(hdKey, 0, 1)
}
}
// TestGetHDKeyChild will test the method GetHDKeyChild()
func TestGetHDKeyChild(t *testing.T) {
t.Parallel()
// Generate a valid key
validKey, err := GenerateHDKey(RecommendedSeedLength)
assert.NoError(t, err)
assert.NotNil(t, validKey)
// Max depth key
/*
var maxKey *bip32.ExtendedKey
maxKey, err = GetHDKeyByPath(validKey, 1<<9, 1<<9)
if err != nil {
t.Fatalf("error occurred: %s", err.Error())
}
*/
// Test depth limit
// todo: make a better test (after 126 maxKey is now nil)
/*
for i := 0; i < 1<<8-1; i++ {
maxKey, err = GetHDKeyChild(maxKey, uint32(i))
if i < 126 && err != nil {
t.Fatalf("error occurred: %s", err.Error())
}
// TODO: make this better rather than grabbing the child twice. This is
// basically a copy of the GetHDKeyByPath test
maxKey, err = GetHDKeyChild(maxKey, uint32(i))
if i >= 126 && err == nil {
t.Fatalf("expected to hit depth limit on HD key index: %d", i)
}
}
*/
var tests = []struct {
inputHDKey *bip32.ExtendedKey
inputNum uint32
expectedNil bool
expectedError bool
}{
// {nil, 0, true, true},
{validKey, 0, false, false},
{validKey, 10, false, false},
{validKey, 100, false, false},
{validKey, 2 ^ 31 + 1, false, false},
{validKey, 1 << 8, false, false},
{validKey, 1 << 9, false, false},
{validKey, 1 << 10, false, false},
{validKey, 1 << 11, false, false},
{validKey, 1 << 12, false, false},
{validKey, 1 << 16, false, false},
{validKey, 1<<32 - 1, false, false},
}
for _, test := range tests {
if hdKey, err := GetHDKeyChild(test.inputHDKey, test.inputNum); err != nil && !test.expectedError {
t.Fatalf("%s Failed: [%v] [%d] inputted and error not expected but got: %s", t.Name(), test.inputHDKey, test.inputNum, err.Error())
} else if err == nil && test.expectedError {
t.Fatalf("%s Failed: [%v] [%d] inputted and error was expected", t.Name(), test.inputHDKey, test.inputNum)
} else if hdKey == nil && !test.expectedNil {
t.Fatalf("%s Failed: [%v] [%d] inputted and was nil but not expected", t.Name(), test.inputHDKey, test.inputNum)
} else if hdKey != nil && test.expectedNil {
t.Fatalf("%s Failed: [%v] [%d] inputted and was NOT nil but expected to be nil", t.Name(), test.inputHDKey, test.inputNum)
} else if hdKey != nil && len(hdKey.String()) == 0 {
t.Fatalf("%s Failed: [%v] [%d] inputted and should not be empty", t.Name(), test.inputHDKey, test.inputNum)
}
}
}
// TestGetHDKeyChildPanic tests for nil case in GetHDKeyChild()
func TestGetHDKeyChildPanic(t *testing.T) {
t.Parallel()
assert.Panics(t, func() {
_, err := GetHDKeyChild(nil, 1)
assert.Error(t, err)
})
}
// ExampleGetHDKeyChild example using GetHDKeyChild()
func ExampleGetHDKeyChild() {
hdKey, err := GenerateHDKey(SecureSeedLength)
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
// Get a child key
var childKey *bip32.ExtendedKey
childKey, err = GetHDKeyChild(hdKey, 0)
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
fmt.Printf("hd key (%d) found at path %d", len(childKey.String()), 0)
// Output:hd key (111) found at path 0
}
// BenchmarkGetHDKeyChild benchmarks the method GetHDKeyChild()
func BenchmarkGetHDKeyChild(b *testing.B) {
hdKey, _ := GenerateHDKey(SecureSeedLength)
for i := 0; i < b.N; i++ {
_, _ = GetHDKeyChild(hdKey, 0)
}
}
// TestGenerateHDKeyFromString will test the method GenerateHDKeyFromString()
func TestGenerateHDKeyFromString(t *testing.T) {
t.Parallel()
var tests = []struct {
input string
expectedNil bool
expectedError bool
}{
{"", true, true},
{"0", true, true},
{"1234567", true, true},
{"xprv9s21ZrQH143K3PZSwbEeXEYq74EbnfMngzAiMCZcfjzyRpUvt2vQJnaHRTZjeuEmLXeN6BzYRoFsEckfobxE9XaRzeLGfQoxzPzTRyRb6oE", false, false},
{"xprv9s21ZrQH143K3PZSwbEeXEYq74EbnfMngzAiMCZcfjzyRpUv", true, true},
{"xprv9s21ZrQH143K3XJueaaswvbJ38UX3FhnXkcA7xF8kqeN62qEu116M1XnqaDpSE7SoKp8NxejVJG9dfpuvBC314VZNdB7W1kQN3Viwgkjr8L", false, false},
}
for _, test := range tests {
if hdKey, err := GenerateHDKeyFromString(test.input); err != nil && !test.expectedError {
t.Fatalf("%s Failed: [%s] inputted and error not expected but got: %s", t.Name(), test.input, err.Error())
} else if err == nil && test.expectedError {
t.Fatalf("%s Failed: [%s] inputted and error was expected", t.Name(), test.input)
} else if hdKey == nil && !test.expectedNil {
t.Fatalf("%s Failed: [%s] inputted and was nil but not expected", t.Name(), test.input)
} else if hdKey != nil && test.expectedNil {
t.Fatalf("%s Failed: [%s] inputted and was NOT nil but expected to be nil", t.Name(), test.input)
} else if hdKey != nil && hdKey.String() != test.input {
t.Fatalf("%s Failed: [%s] inputted [%s] expected but got: %s", t.Name(), test.input, test.input, hdKey.String())
}
}
}
// ExampleGenerateHDKeyFromString example using GenerateHDKeyFromString()
func ExampleGenerateHDKeyFromString() {
hdKey, err := GenerateHDKeyFromString("xprv9s21ZrQH143K3PZSwbEeXEYq74EbnfMngzAiMCZcfjzyRpUvt2vQJnaHRTZjeuEmLXeN6BzYRoFsEckfobxE9XaRzeLGfQoxzPzTRyRb6oE")
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
fmt.Printf("hd key generated from: %s", hdKey.String())
// Output:hd key generated from: xprv9s21ZrQH143K3PZSwbEeXEYq74EbnfMngzAiMCZcfjzyRpUvt2vQJnaHRTZjeuEmLXeN6BzYRoFsEckfobxE9XaRzeLGfQoxzPzTRyRb6oE
}
// BenchmarkGenerateHDKeyFromString benchmarks the method GenerateHDKeyFromString()
func BenchmarkGenerateHDKeyFromString(b *testing.B) {
xPriv, _, _ := GenerateHDKeyPair(SecureSeedLength)
for i := 0; i < b.N; i++ {
_, _ = GenerateHDKeyFromString(xPriv)
}
}
// TestGetPrivateKeyFromHDKey will test the method GetPrivateKeyFromHDKey()
func TestGetPrivateKeyFromHDKey(t *testing.T) {
t.Parallel()
validHdKey, err := GenerateHDKeyFromString("xprv9s21ZrQH143K4FdJCmPQe1CFUvK3PKVrcp3b5xVr5Bs3cP5ab6ytszeHggTmHoqTXpaa8CgYPxZZzigSGCDjtyWdUDJqPogb1JGWAPkBLdF")
assert.NoError(t, err)
assert.NotNil(t, validHdKey)
var tests = []struct {
input *bip32.ExtendedKey
expectedKey string
expectedNil bool
expectedError bool
}{
{new(bip32.ExtendedKey), "", true, true},
{validHdKey, "8511f5e1e35ab748e7639aa68666df71857866af13fda1d081d5917948a6cd34", false, false},
}
for _, test := range tests {
if privateKey, err := GetPrivateKeyFromHDKey(test.input); err != nil && !test.expectedError {
t.Fatalf("%s Failed: [%v] inputted and error not expected but got: %s", t.Name(), test.input, err.Error())
} else if err == nil && test.expectedError {
t.Fatalf("%s Failed: [%v] inputted and error was expected", t.Name(), test.input)
} else if privateKey == nil && !test.expectedNil {
t.Fatalf("%s Failed: [%v] inputted and was nil but not expected", t.Name(), test.input)
} else if privateKey != nil && test.expectedNil {
t.Fatalf("%s Failed: [%v] inputted and was NOT nil but expected to be nil", t.Name(), test.input)
} else if privateKey != nil && hex.EncodeToString(privateKey.Serialise()) != test.expectedKey {
t.Fatalf("%s Failed: [%v] inputted [%s] expected but got: %s", t.Name(), test.input, test.expectedKey, hex.EncodeToString(privateKey.Serialise()))
}
}
}
// TestGetPrivateKeyFromHDKeyPanic tests for nil case in GetPrivateKeyFromHDKey()
func TestGetPrivateKeyFromHDKeyPanic(t *testing.T) {
t.Parallel()
assert.Panics(t, func() {
_, err := GetPrivateKeyFromHDKey(nil)
assert.Error(t, err)
})
}
// ExampleGetPrivateKeyFromHDKey example using GetPrivateKeyFromHDKey()
func ExampleGetPrivateKeyFromHDKey() {
hdKey, err := GenerateHDKeyFromString("xprv9s21ZrQH143K3PZSwbEeXEYq74EbnfMngzAiMCZcfjzyRpUvt2vQJnaHRTZjeuEmLXeN6BzYRoFsEckfobxE9XaRzeLGfQoxzPzTRyRb6oE")
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
var privateKey *bec.PrivateKey
if privateKey, err = GetPrivateKeyFromHDKey(hdKey); err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
fmt.Printf("private key: %s", hex.EncodeToString(privateKey.Serialise()))
// Output:private key: 0ccf07f2cbe10dbe6f6034b7efbf62fc83cac3d44f49d67aa22ac8893d294e7a
}
// BenchmarkGetPrivateKeyFromHDKey benchmarks the method GetPrivateKeyFromHDKey()
func BenchmarkGetPrivateKeyFromHDKey(b *testing.B) {
hdKey, _ := GenerateHDKey(SecureSeedLength)
for i := 0; i < b.N; i++ {
_, _ = GetPrivateKeyFromHDKey(hdKey)
}
}
// TestGetPrivateKeyStringFromHDKey will test the method GetPrivateKeyStringFromHDKey()
func TestGetPrivateKeyStringFromHDKey(t *testing.T) {
t.Parallel()
validHdKey, err := GenerateHDKeyFromString("xprv9s21ZrQH143K4FdJCmPQe1CFUvK3PKVrcp3b5xVr5Bs3cP5ab6ytszeHggTmHoqTXpaa8CgYPxZZzigSGCDjtyWdUDJqPogb1JGWAPkBLdF")
assert.NoError(t, err)
assert.NotNil(t, validHdKey)
var tests = []struct {
input *bip32.ExtendedKey
expectedKey string
expectedError bool
}{
{new(bip32.ExtendedKey), "", true},
{validHdKey, "8511f5e1e35ab748e7639aa68666df71857866af13fda1d081d5917948a6cd34", false},
}
var privateKey string
for _, test := range tests {
if privateKey, err = GetPrivateKeyStringFromHDKey(test.input); err != nil && !test.expectedError {
t.Fatalf("%s Failed: [%v] inputted and error not expected but got: %s", t.Name(), test.input, err.Error())
} else if err == nil && test.expectedError {
t.Fatalf("%s Failed: [%v] inputted and error was expected", t.Name(), test.input)
} else if privateKey != test.expectedKey {
t.Fatalf("%s Failed: [%v] inputted [%s] expected but got: %s", t.Name(), test.input, test.expectedKey, privateKey)
}
}
}
// TestGetPrivateKeyStringFromHDKeyPanic tests for nil case in GetPrivateKeyStringFromHDKey()
func TestGetPrivateKeyStringFromHDKeyPanic(t *testing.T) {
t.Parallel()
assert.Panics(t, func() {
_, err := GetPrivateKeyStringFromHDKey(nil)
assert.Error(t, err)
})
}
// ExampleGetPrivateKeyStringFromHDKey example using GetPrivateKeyStringFromHDKey()
func ExampleGetPrivateKeyStringFromHDKey() {
hdKey, err := GenerateHDKeyFromString("xprv9s21ZrQH143K3PZSwbEeXEYq74EbnfMngzAiMCZcfjzyRpUvt2vQJnaHRTZjeuEmLXeN6BzYRoFsEckfobxE9XaRzeLGfQoxzPzTRyRb6oE")
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
var privateKey string
if privateKey, err = GetPrivateKeyStringFromHDKey(hdKey); err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
fmt.Printf("private key: %s", privateKey)
// Output:private key: 0ccf07f2cbe10dbe6f6034b7efbf62fc83cac3d44f49d67aa22ac8893d294e7a
}
// BenchmarkGetPrivateKeyStringFromHDKey benchmarks the method GetPrivateKeyStringFromHDKey()
func BenchmarkGetPrivateKeyStringFromHDKey(b *testing.B) {
hdKey, _ := GenerateHDKey(SecureSeedLength)
for i := 0; i < b.N; i++ {
_, _ = GetPrivateKeyStringFromHDKey(hdKey)
}
}
// TestGetPublicKeyFromHDKey will test the method GetPublicKeyFromHDKey()
func TestGetPublicKeyFromHDKey(t *testing.T) {
t.Parallel()
validHdKey, err := GenerateHDKeyFromString("xprv9s21ZrQH143K4FdJCmPQe1CFUvK3PKVrcp3b5xVr5Bs3cP5ab6ytszeHggTmHoqTXpaa8CgYPxZZzigSGCDjtyWdUDJqPogb1JGWAPkBLdF")
assert.NoError(t, err)
assert.NotNil(t, validHdKey)
var tests = []struct {
input *bip32.ExtendedKey
expectedKey string
expectedNil bool
expectedError bool
}{
{new(bip32.ExtendedKey), "", true, true},
{validHdKey, "02f2a2942b9d1dba033d36ab0c193e680415f5c8c1ff5d854f805c8c42ed9dd1fd", false, false},
}
var publicKey *bec.PublicKey
for _, test := range tests {
if publicKey, err = GetPublicKeyFromHDKey(test.input); err != nil && !test.expectedError {
t.Fatalf("%s Failed: [%v] inputted and error not expected but got: %s", t.Name(), test.input, err.Error())
} else if err == nil && test.expectedError {
t.Fatalf("%s Failed: [%v] inputted and error was expected", t.Name(), test.input)
} else if publicKey == nil && !test.expectedNil {
t.Fatalf("%s Failed: [%v] inputted and was nil but not expected", t.Name(), test.input)
} else if publicKey != nil && test.expectedNil {
t.Fatalf("%s Failed: [%v] inputted and was NOT nil but expected to be nil", t.Name(), test.input)
} else if publicKey != nil && hex.EncodeToString(publicKey.SerialiseCompressed()) != test.expectedKey {
t.Fatalf("%s Failed: [%v] inputted [%s] expected but got: %s", t.Name(), test.input, test.expectedKey, hex.EncodeToString(publicKey.SerialiseCompressed()))
}
}
}
// TestGetPublicKeyFromHDKeyPanic tests for nil case in GetPublicKeyFromHDKey()
func TestGetPublicKeyFromHDKeyPanic(t *testing.T) {
t.Parallel()
assert.Panics(t, func() {
_, err := GetPublicKeyFromHDKey(nil)
assert.Error(t, err)
})
}
// ExampleGetPublicKeyFromHDKey example using GetPublicKeyFromHDKey()
func ExampleGetPublicKeyFromHDKey() {
hdKey, err := GenerateHDKeyFromString("xprv9s21ZrQH143K3PZSwbEeXEYq74EbnfMngzAiMCZcfjzyRpUvt2vQJnaHRTZjeuEmLXeN6BzYRoFsEckfobxE9XaRzeLGfQoxzPzTRyRb6oE")
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
var publicKey *bec.PublicKey
if publicKey, err = GetPublicKeyFromHDKey(hdKey); err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
fmt.Printf("public key: %s", hex.EncodeToString(publicKey.SerialiseCompressed()))
// Output:public key: 03a25f6c10eedcd41eebac22c6bbc5278690fa1aab3afc2bbe8f2277c85e5c5def
}
// BenchmarkGetPublicKeyFromHDKey benchmarks the method GetPublicKeyFromHDKey()
func BenchmarkGetPublicKeyFromHDKey(b *testing.B) {
hdKey, _ := GenerateHDKey(SecureSeedLength)
for i := 0; i < b.N; i++ {
_, _ = GetPublicKeyFromHDKey(hdKey)
}
}
// TestGetAddressFromHDKey will test the method GetAddressFromHDKey()
func TestGetAddressFromHDKey(t *testing.T) {
t.Parallel()
validHdKey, err := GenerateHDKeyFromString("xprv9s21ZrQH143K4FdJCmPQe1CFUvK3PKVrcp3b5xVr5Bs3cP5ab6ytszeHggTmHoqTXpaa8CgYPxZZzigSGCDjtyWdUDJqPogb1JGWAPkBLdF")
assert.NoError(t, err)
assert.NotNil(t, validHdKey)
var tests = []struct {
input *bip32.ExtendedKey
expectedAddress string
expectedNil bool
expectedError bool
}{
{new(bip32.ExtendedKey), "", true, true},
{validHdKey, "13xHrMdZuqa2gpweHf37w8hu6tfv3JrnaW", false, false},
}
var address *bscript.Address
for _, test := range tests {
if address, err = GetAddressFromHDKey(test.input); err != nil && !test.expectedError {
t.Fatalf("%s Failed: [%v] inputted and error not expected but got: %s", t.Name(), test.input, err.Error())
} else if err == nil && test.expectedError {
t.Fatalf("%s Failed: [%v] inputted and error was expected", t.Name(), test.input)
} else if address == nil && !test.expectedNil {
t.Fatalf("%s Failed: [%v] inputted and was nil but not expected", t.Name(), test.input)
} else if address != nil && test.expectedNil {
t.Fatalf("%s Failed: [%v] inputted and was NOT nil but expected to be nil", t.Name(), test.input)
} else if address != nil && address.AddressString != test.expectedAddress {
t.Fatalf("%s Failed: [%v] inputted [%s] expected but got: %s", t.Name(), test.input, test.expectedAddress, address.AddressString)
}
}
}
// TestGetAddressFromHDKeyPanic tests for nil case in GetAddressFromHDKey()
func TestGetAddressFromHDKeyPanic(t *testing.T) {
t.Parallel()
assert.Panics(t, func() {
_, err := GetAddressFromHDKey(nil)
assert.Error(t, err)
})
}
// ExampleGetAddressFromHDKey example using GetAddressFromHDKey()
func ExampleGetAddressFromHDKey() {
hdKey, err := GenerateHDKeyFromString("xprv9s21ZrQH143K3PZSwbEeXEYq74EbnfMngzAiMCZcfjzyRpUvt2vQJnaHRTZjeuEmLXeN6BzYRoFsEckfobxE9XaRzeLGfQoxzPzTRyRb6oE")
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
var address *bscript.Address
if address, err = GetAddressFromHDKey(hdKey); err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
fmt.Printf("address: %s", address.AddressString)
// Output:address: 18G2YRH3nRKRx8pnqVFUM5nAJhTZJ3YA4W
}
// BenchmarkGetAddressFromHDKey benchmarks the method GetAddressFromHDKey()
func BenchmarkGetAddressFromHDKey(b *testing.B) {
hdKey, _ := GenerateHDKey(SecureSeedLength)
for i := 0; i < b.N; i++ {
_, _ = GetAddressFromHDKey(hdKey)
}
}
// TestGetAddressStringFromHDKey will test the method GetAddressStringFromHDKey()
func TestGetAddressStringFromHDKey(t *testing.T) {
t.Parallel()
validHdKey, err := GenerateHDKeyFromString("xprv9s21ZrQH143K4FdJCmPQe1CFUvK3PKVrcp3b5xVr5Bs3cP5ab6ytszeHggTmHoqTXpaa8CgYPxZZzigSGCDjtyWdUDJqPogb1JGWAPkBLdF")
assert.NoError(t, err)
assert.NotNil(t, validHdKey)
var tests = []struct {
input *bip32.ExtendedKey
expectedAddress string
expectedError bool
}{
{new(bip32.ExtendedKey), "", true},
{validHdKey, "13xHrMdZuqa2gpweHf37w8hu6tfv3JrnaW", false},
}
var address string
for _, test := range tests {
if address, err = GetAddressStringFromHDKey(test.input); err != nil && !test.expectedError {
t.Fatalf("%s Failed: [%v] inputted and error not expected but got: %s", t.Name(), test.input, err.Error())
} else if err == nil && test.expectedError {
t.Fatalf("%s Failed: [%v] inputted and error was expected", t.Name(), test.input)
} else if address != test.expectedAddress {
t.Fatalf("%s Failed: [%v] inputted [%s] expected but got: %s", t.Name(), test.input, test.expectedAddress, address)
}
}
}
// TestGetAddressStringFromHDKeyPanic tests for nil case in GetAddressStringFromHDKey()
func TestGetAddressStringFromHDKeyPanic(t *testing.T) {
t.Parallel()
assert.Panics(t, func() {
_, err := GetAddressStringFromHDKey(nil)
assert.Error(t, err)
})
}
// ExampleGetAddressStringFromHDKey example using GetAddressStringFromHDKey()
func ExampleGetAddressStringFromHDKey() {
hdKey, err := GenerateHDKeyFromString("xprv9s21ZrQH143K3PZSwbEeXEYq74EbnfMngzAiMCZcfjzyRpUvt2vQJnaHRTZjeuEmLXeN6BzYRoFsEckfobxE9XaRzeLGfQoxzPzTRyRb6oE")
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
var address string
if address, err = GetAddressStringFromHDKey(hdKey); err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
fmt.Printf("address: %s", address)
// Output:address: 18G2YRH3nRKRx8pnqVFUM5nAJhTZJ3YA4W
}
// BenchmarkGetAddressStringFromHDKey benchmarks the method GetAddressStringFromHDKey()
func BenchmarkGetAddressStringFromHDKey(b *testing.B) {
hdKey, _ := GenerateHDKey(SecureSeedLength)
for i := 0; i < b.N; i++ {
_, _ = GetAddressStringFromHDKey(hdKey)
}
}
// TestGetPublicKeysForPath will test the method GetPublicKeysForPath()
func TestGetPublicKeysForPath(t *testing.T) {
t.Parallel()
validHdKey, err := GenerateHDKeyFromString("xprv9s21ZrQH143K4FdJCmPQe1CFUvK3PKVrcp3b5xVr5Bs3cP5ab6ytszeHggTmHoqTXpaa8CgYPxZZzigSGCDjtyWdUDJqPogb1JGWAPkBLdF")
assert.NoError(t, err)
assert.NotNil(t, validHdKey)
var tests = []struct {
input *bip32.ExtendedKey
inputNum uint32
expectedPubKey1 string
expectedPubKey2 string
expectedNil bool
expectedError bool
}{
{new(bip32.ExtendedKey), 1, "", "", true, true},
{validHdKey, 1, "03cc3334f0a6f0fae0420d1442ca0ce64fad0da76d652f2cc3b333e7ed95b97259", "02ceb23902f8dcf6fbff656597ee0343e05c907c6dfcdd8aaf6d033e14e85fd955", false, false},
{validHdKey, 2, "020cb908e3b9f3de7c9b40e7bcce63708c5617536d85cf4ab5635e3d3819c02c37", "030007ae60fc6eef98ea17b4f80f9b791e61ea94936e8a9e6ec343eeaa50a875e0", false, false},
{validHdKey, 3, "0342593453c476ac6c78eb1b1e586df00b20352e61c42536fe1b33c9fdf3bfbb6f", "03786a41dbf0b099256da26cb0019e10063628f6ce31b96801703f1bb2e1b17724", false, false},
{validHdKey, 4, "0366dcdebfc8abfd34bffc181ccb54f1706839a80ad4f0842ae5a43f39fdd35c1e", "03a095db29ae9ee0b22c775118b4444b59db40acdea137fd9ecd9c68dacf50a644", false, false},
}
var pubKeys []*bec.PublicKey
for _, test := range tests {
if pubKeys, err = GetPublicKeysForPath(test.input, test.inputNum); err != nil && !test.expectedError {
t.Fatalf("%s Failed: [%v] [%d] inputted and error not expected but got: %s", t.Name(), test.input, test.inputNum, err.Error())
} else if err == nil && test.expectedError {
t.Fatalf("%s Failed: [%v] [%d] inputted and error was expected", t.Name(), test.input, test.inputNum)
} else if pubKeys == nil && !test.expectedNil {
t.Fatalf("%s Failed: [%v] [%d] inputted and was nil but not expected", t.Name(), test.input, test.inputNum)
} else if pubKeys != nil && test.expectedNil {
t.Fatalf("%s Failed: [%v] [%d] inputted and was NOT nil but expected to be nil", t.Name(), test.input, test.inputNum)
} else if pubKeys != nil && hex.EncodeToString(pubKeys[0].SerialiseCompressed()) != test.expectedPubKey1 {
t.Fatalf("%s Failed: [%v] [%d] inputted key 1 [%s] expected but got: %s", t.Name(), test.input, test.inputNum, test.expectedPubKey1, hex.EncodeToString(pubKeys[0].SerialiseCompressed()))
} else if pubKeys != nil && hex.EncodeToString(pubKeys[1].SerialiseCompressed()) != test.expectedPubKey2 {
t.Fatalf("%s Failed: [%v] [%d] inputted key 2 [%s] expected but got: %s", t.Name(), test.input, test.inputNum, test.expectedPubKey2, hex.EncodeToString(pubKeys[1].SerialiseCompressed()))
}
}
}
// TestGetPublicKeysForPathPanic tests for nil case in GetPublicKeysForPath()
func TestGetPublicKeysForPathPanic(t *testing.T) {
t.Parallel()
assert.Panics(t, func() {
_, err := GetPublicKeysForPath(nil, 1)
assert.Error(t, err)
})
}
// ExampleGetPublicKeysForPath example using GetPublicKeysForPath()
func ExampleGetPublicKeysForPath() {
hdKey, err := GenerateHDKeyFromString("xprv9s21ZrQH143K3PZSwbEeXEYq74EbnfMngzAiMCZcfjzyRpUvt2vQJnaHRTZjeuEmLXeN6BzYRoFsEckfobxE9XaRzeLGfQoxzPzTRyRb6oE")
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
var publicKeys []*bec.PublicKey
publicKeys, err = GetPublicKeysForPath(hdKey, 5)
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
fmt.Printf("found [%d] keys! Key 1: %s Key 2: %s", len(publicKeys), hex.EncodeToString(publicKeys[0].SerialiseCompressed()), hex.EncodeToString(publicKeys[1].SerialiseCompressed()))
// Output:found [2] keys! Key 1: 03f87ac38fb0cfca12988b51a2f1cd3e85bb4aeb1b05f549682190ac8205a67d30 Key 2: 02e78303aeef1acce1347c6493fadc1914e6d85ef3189a8856afb3accd53fbd9c5
}
// BenchmarkGetPublicKeysForPath benchmarks the method GetPublicKeysForPath()
func BenchmarkGetPublicKeysForPath(b *testing.B) {
hdKey, _ := GenerateHDKey(SecureSeedLength)
for i := 0; i < b.N; i++ {
_, _ = GetPublicKeysForPath(hdKey, 5)
}
}
// TestGetAddressesForPath will test the method GetAddressesForPath()
func TestGetAddressesForPath(t *testing.T) {
t.Parallel()
validHdKey, err := GenerateHDKeyFromString("xprv9s21ZrQH143K4FdJCmPQe1CFUvK3PKVrcp3b5xVr5Bs3cP5ab6ytszeHggTmHoqTXpaa8CgYPxZZzigSGCDjtyWdUDJqPogb1JGWAPkBLdF")
assert.NoError(t, err)
assert.NotNil(t, validHdKey)
var tests = []struct {
input *bip32.ExtendedKey
inputNum uint32
expectedAddress1 string
expectedAddress2 string
expectedNil bool
expectedError bool
}{
{new(bip32.ExtendedKey), 1, "", "", true, true},
{validHdKey, 1, "1KMxfSfRCkC1jrBAuYaLde4XBzdsWApbdH", "174DL9ZbBWx568ssAg8w2YwW6FTTBwXGEu", false, false},
{validHdKey, 2, "18s3peTU7fMSwgui54avpnqm1126pRVccw", "1KgZZ3NsJDw3v1GPHBj8ASnxutA1kFxo2i", false, false},
}
var addresses []string
for _, test := range tests {
if addresses, err = GetAddressesForPath(test.input, test.inputNum); err != nil && !test.expectedError {
t.Fatalf("%s Failed: [%v] [%d] inputted and error not expected but got: %s", t.Name(), test.input, test.inputNum, err.Error())
} else if err == nil && test.expectedError {
t.Fatalf("%s Failed: [%v] [%d] inputted and error was expected", t.Name(), test.input, test.inputNum)
} else if addresses == nil && !test.expectedNil {
t.Fatalf("%s Failed: [%v] [%d] inputted and was nil but not expected", t.Name(), test.input, test.inputNum)
} else if addresses != nil && test.expectedNil {
t.Fatalf("%s Failed: [%v] [%d] inputted and was NOT nil but expected to be nil", t.Name(), test.input, test.inputNum)
} else if addresses != nil && addresses[0] != test.expectedAddress1 {
t.Fatalf("%s Failed: [%v] [%d] inputted address 1 [%s] expected but got: %s", t.Name(), test.input, test.inputNum, test.expectedAddress1, addresses[0])
} else if addresses != nil && addresses[1] != test.expectedAddress2 {
t.Fatalf("%s Failed: [%v] [%d] inputted address 2 [%s] expected but got: %s", t.Name(), test.input, test.inputNum, test.expectedAddress2, addresses[1])
}
}
}
// TestGetAddressesForPathPanic tests for nil case in GetAddressesForPath()
func TestGetAddressesForPathPanic(t *testing.T) {
t.Parallel()
assert.Panics(t, func() {
_, err := GetAddressesForPath(nil, 1)
assert.Error(t, err)
})
}
// ExampleGetAddressesForPath example using GetAddressesForPath()
func ExampleGetAddressesForPath() {
hdKey, err := GenerateHDKeyFromString("xprv9s21ZrQH143K3PZSwbEeXEYq74EbnfMngzAiMCZcfjzyRpUvt2vQJnaHRTZjeuEmLXeN6BzYRoFsEckfobxE9XaRzeLGfQoxzPzTRyRb6oE")
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
var addresses []string
addresses, err = GetAddressesForPath(hdKey, 5)
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
fmt.Printf("found [%d] addresses! Address 1: %s Address 2: %s", len(addresses), addresses[0], addresses[1])
// Output:found [2] addresses! Address 1: 1JHGJTqsiFHo4yQYJ1WbTvbxYMZC7nZKYb Address 2: 1DTHBcGeJFRmS26S11tt2EddhSkFM8tmze
}
// BenchmarkGetAddressesForPath benchmarks the method GetAddressesForPath()
func BenchmarkGetAddressesForPath(b *testing.B) {
hdKey, _ := GenerateHDKey(SecureSeedLength)
for i := 0; i < b.N; i++ {
_, _ = GetAddressesForPath(hdKey, 5)
}
}
// TestGetExtendedPublicKey will test the method GetExtendedPublicKey()
func TestGetExtendedPublicKey(t *testing.T) {
t.Parallel()
validHdKey, err := GenerateHDKeyFromString("xprv9s21ZrQH143K4FdJCmPQe1CFUvK3PKVrcp3b5xVr5Bs3cP5ab6ytszeHggTmHoqTXpaa8CgYPxZZzigSGCDjtyWdUDJqPogb1JGWAPkBLdF")
assert.NoError(t, err)
assert.NotNil(t, validHdKey)
var tests = []struct {
input *bip32.ExtendedKey
expectedKey string
expectedError bool
}{
{validHdKey, "xpub661MyMwAqRbcGjhmJnvR198z2x9XnnDhz2yBtLuTdXQ2VBQj8eJ9RnxmXxKnRPhYy6nLsmabmUfVkbajvP7aZASrrnoZkzmwgyjiNskiefG", false},
{new(bip32.ExtendedKey), "zeroed extended key", false},
}
var xPub string
for _, test := range tests {
if xPub, err = GetExtendedPublicKey(test.input); err != nil && !test.expectedError {
t.Fatalf("%s Failed: [%v] inputted and error not expected but got: %s", t.Name(), test.input, err.Error())
} else if err == nil && test.expectedError {