-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
private_key_test.go
576 lines (499 loc) · 20.5 KB
/
private_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
package bitcoin
import (
"encoding/hex"
"fmt"
"testing"
"github.com/libsv/go-bk/bec"
"github.com/libsv/go-bk/wif"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestCreatePrivateKey will test the method CreatePrivateKey()
func TestCreatePrivateKey(t *testing.T) {
rawKey, err := CreatePrivateKey()
assert.NoError(t, err)
assert.NotNil(t, rawKey)
assert.Equal(t, 32, len(rawKey.Serialise()))
}
// ExampleCreatePrivateKey example using CreatePrivateKey()
func ExampleCreatePrivateKey() {
rawKey, err := CreatePrivateKey()
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
} else if len(rawKey.Serialise()) > 0 {
fmt.Printf("key created successfully!")
}
// Output:key created successfully!
}
// BenchmarkCreatePrivateKey benchmarks the method CreatePrivateKey()
func BenchmarkCreatePrivateKey(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = CreatePrivateKey()
}
}
// TestCreatePrivateKeyString will test the method CreatePrivateKeyString()
func TestCreatePrivateKeyString(t *testing.T) {
key, err := CreatePrivateKeyString()
assert.NoError(t, err)
assert.Equal(t, 64, len(key))
}
// ExampleCreatePrivateKeyString example using CreatePrivateKeyString()
func ExampleCreatePrivateKeyString() {
key, err := CreatePrivateKeyString()
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
} else if len(key) > 0 {
fmt.Printf("key created successfully!")
}
// Output:key created successfully!
}
// BenchmarkCreatePrivateKeyString benchmarks the method CreatePrivateKeyString()
func BenchmarkCreatePrivateKeyString(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = CreatePrivateKeyString()
}
}
// TestPrivateKeyFromString will test the method PrivateKeyFromString()
func TestPrivateKeyFromString(t *testing.T) {
t.Parallel()
var tests = []struct {
input string
expectedKey string
expectedNil bool
expectedError bool
}{
{"54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd", "54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd", false, false},
{"E83385AF76B2B1997326B567461FB73DD9C27EAB9E1E86D26779F4650C5F2B75", "e83385af76b2b1997326b567461fb73dd9c27eab9e1e86d26779f4650c5f2b75", false, false},
{"E83385AF76B2B1997326B567461FB73DD9C27EAB9E1E86D26779F4650C5F", "0000e83385af76b2b1997326b567461fb73dd9c27eab9e1e86d26779f4650c5f", false, false},
{"E83385AF76B2B1997326B567461FB73DD9C27EAB9E1E86D26779F", "", true, true},
{"1234567", "", true, true},
{"0", "", true, true},
{"", "", true, true},
}
for _, test := range tests {
if rawKey, err := PrivateKeyFromString(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 rawKey == nil && !test.expectedNil {
t.Fatalf("%s Failed: [%s] inputted and was nil but not expected", t.Name(), test.input)
} else if rawKey != nil && test.expectedNil {
t.Fatalf("%s Failed: [%s] inputted and was NOT nil but expected to be nil", t.Name(), test.input)
} else if rawKey != nil && hex.EncodeToString(rawKey.Serialise()) != test.expectedKey {
t.Fatalf("%s Failed: [%s] inputted [%s] expected but failed comparison of keys, got: %s", t.Name(), test.input, test.expectedKey, hex.EncodeToString(rawKey.Serialise()))
}
}
}
// ExamplePrivateKeyFromString example using PrivateKeyFromString()
func ExamplePrivateKeyFromString() {
key, err := PrivateKeyFromString("54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd")
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
fmt.Printf("key converted: %s", hex.EncodeToString(key.Serialise()))
// Output:key converted: 54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd
}
// BenchmarkPrivateKeyFromString benchmarks the method PrivateKeyFromString()
func BenchmarkPrivateKeyFromString(b *testing.B) {
key, _ := CreatePrivateKeyString()
for i := 0; i < b.N; i++ {
_, _ = PrivateKeyFromString(key)
}
}
// TestPrivateAndPublicKeys will test the method PrivateAndPublicKeys()
func TestPrivateAndPublicKeys(t *testing.T) {
t.Parallel()
var tests = []struct {
input string
expectedPrivateKey string
expectedNil bool
expectedError bool
}{
{"", "", true, true},
{"0", "", true, true},
{"00000", "", true, true},
{"0-0-0-0-0", "", true, true},
{"z4035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abz", "", true, true},
{"54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd", "54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd", false, false},
}
for _, test := range tests {
if privateKey, publicKey, err := PrivateAndPublicKeys(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 (privateKey == nil || publicKey == nil) && !test.expectedNil {
t.Fatalf("%s Failed: [%s] inputted and was nil but not expected", t.Name(), test.input)
} else if (privateKey != nil || publicKey != nil) && test.expectedNil {
t.Fatalf("%s Failed: [%s] inputted and was NOT nil but expected to be nil", t.Name(), test.input)
} else if privateKey != nil && hex.EncodeToString(privateKey.Serialise()) != test.expectedPrivateKey {
t.Fatalf("%s Failed: [%s] inputted [%s] expected but failed comparison of keys, got: %s", t.Name(), test.input, test.expectedPrivateKey, hex.EncodeToString(privateKey.Serialise()))
}
}
}
// ExamplePrivateAndPublicKeys example using PrivateAndPublicKeys()
func ExamplePrivateAndPublicKeys() {
privateKey, publicKey, err := PrivateAndPublicKeys("54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd")
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
fmt.Printf("private key: %s public key: %s", hex.EncodeToString(privateKey.Serialise()), hex.EncodeToString(publicKey.SerialiseCompressed()))
// Output:private key: 54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd public key: 031b8c93100d35bd448f4646cc4678f278351b439b52b303ea31ec9edb5475e73f
}
// BenchmarkPrivateAndPublicKeys benchmarks the method PrivateAndPublicKeys()
func BenchmarkPrivateAndPublicKeys(b *testing.B) {
key, _ := CreatePrivateKeyString()
for i := 0; i < b.N; i++ {
_, _, _ = PrivateAndPublicKeys(key)
}
}
// TestPrivateKeyToWif will test the method PrivateKeyToWif()
func TestPrivateKeyToWif(t *testing.T) {
t.Parallel()
var tests = []struct {
input string
expectedWif string
expectedNil bool
expectedError bool
}{
{"", "", true, true},
{"0", "", true, true},
{"000000", "5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAbuatmU", false, false},
{"6D792070726976617465206B6579", "5HpHagT65TZzG1PH3CSu63k8DbuTZnNJf6HgyQNymvXmALAsm9s", false, false},
{"54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8azz", "", true, true},
{"54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd", "5JTHas7yTFMBLqgFogxZFf8Vc5uKEbkE7yQAQ2g3xPHo2sNG1Ei", false, false},
}
for _, test := range tests {
if privateWif, err := PrivateKeyToWif(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 privateWif == nil && !test.expectedNil {
t.Fatalf("%s Failed: [%s] inputted and was nil but not expected", t.Name(), test.input)
} else if privateWif != nil && test.expectedNil {
t.Fatalf("%s Failed: [%s] inputted and was NOT nil but expected to be nil", t.Name(), test.input)
} else if privateWif != nil && privateWif.String() != test.expectedWif {
t.Fatalf("%s Failed: [%s] inputted [%s] expected but failed comparison of keys, got: %s", t.Name(), test.input, test.expectedWif, privateWif.String())
}
}
}
// ExamplePrivateKeyToWif example using PrivateKeyToWif()
func ExamplePrivateKeyToWif() {
privateWif, err := PrivateKeyToWif("54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd")
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
fmt.Printf("converted wif: %s", privateWif.String())
// Output:converted wif: 5JTHas7yTFMBLqgFogxZFf8Vc5uKEbkE7yQAQ2g3xPHo2sNG1Ei
}
// BenchmarkPrivateKeyToWif benchmarks the method PrivateKeyToWif()
func BenchmarkPrivateKeyToWif(b *testing.B) {
key, _ := CreatePrivateKeyString()
for i := 0; i < b.N; i++ {
_, _ = PrivateKeyToWif(key)
}
}
// TestPrivateKeyToWifString will test the method PrivateKeyToWifString()
func TestPrivateKeyToWifString(t *testing.T) {
t.Parallel()
var tests = []struct {
input string
expectedWif string
expectedError bool
}{
{"", "", true},
{"0", "", true},
{"000000", "5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAbuatmU", false},
{"6D792070726976617465206B6579", "5HpHagT65TZzG1PH3CSu63k8DbuTZnNJf6HgyQNymvXmALAsm9s", false},
{"54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8azz", "", true},
{"54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd", "5JTHas7yTFMBLqgFogxZFf8Vc5uKEbkE7yQAQ2g3xPHo2sNG1Ei", false},
}
for _, test := range tests {
if privateWif, err := PrivateKeyToWifString(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 privateWif != test.expectedWif {
t.Fatalf("%s Failed: [%s] inputted [%s] expected but failed comparison of keys, got: %s", t.Name(), test.input, test.expectedWif, privateWif)
}
}
}
// ExamplePrivateKeyToWifString example using PrivateKeyToWifString()
func ExamplePrivateKeyToWifString() {
privateWif, err := PrivateKeyToWifString("54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd")
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
fmt.Printf("converted wif: %s", privateWif)
// Output:converted wif: 5JTHas7yTFMBLqgFogxZFf8Vc5uKEbkE7yQAQ2g3xPHo2sNG1Ei
}
// BenchmarkPrivateKeyToWifString benchmarks the method PrivateKeyToWifString()
func BenchmarkPrivateKeyToWifString(b *testing.B) {
key, _ := CreatePrivateKeyString()
for i := 0; i < b.N; i++ {
_, _ = PrivateKeyToWifString(key)
}
}
// TestWifToPrivateKey will test the method WifToPrivateKey()
func TestWifToPrivateKey(t *testing.T) {
t.Parallel()
var tests = []struct {
input string
expectedKey string
expectedNil bool
expectedError bool
}{
{"", "", true, true},
{"0", "", true, true},
{"5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAbuatmU", "0000000000000000000000000000000000000000000000000000000000000000", false, false},
{"5HpHagT65TZzG1PH3CSu63k8DbuTZnNJf6HgyQNymvXmALAsm9s", "0000000000000000000000000000000000006d792070726976617465206b6579", false, false},
{"54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8azz", "", true, true},
{"5JTHas7yTFMBLqgFogxZFf8Vc5uKEbkE7yQAQ2g3xPHo2sNG1Ei", "54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd", false, false},
}
for _, test := range tests {
if privateKey, err := WifToPrivateKey(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 privateKey == nil && !test.expectedNil {
t.Fatalf("%s Failed: [%s] inputted and was nil but not expected", t.Name(), test.input)
} else if privateKey != nil && test.expectedNil {
t.Fatalf("%s Failed: [%s] 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: [%s] inputted [%s] expected but failed comparison of keys, got: %s", t.Name(), test.input, test.expectedKey, hex.EncodeToString(privateKey.Serialise()))
}
}
}
// ExampleWifToPrivateKey example using WifToPrivateKey()
func ExampleWifToPrivateKey() {
privateKey, err := WifToPrivateKey("5JTHas7yTFMBLqgFogxZFf8Vc5uKEbkE7yQAQ2g3xPHo2sNG1Ei")
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
fmt.Printf("private key: %s", hex.EncodeToString(privateKey.Serialise()))
// Output:private key: 54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd
}
// BenchmarkWifToPrivateKey benchmarks the method WifToPrivateKey()
func BenchmarkWifToPrivateKey(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = WifToPrivateKey("5JTHas7yTFMBLqgFogxZFf8Vc5uKEbkE7yQAQ2g3xPHo2sNG1Ei")
}
}
// TestWifToPrivateKeyString will test the method WifToPrivateKeyString()
func TestWifToPrivateKeyString(t *testing.T) {
t.Parallel()
var tests = []struct {
input string
expectedKey string
expectedError bool
}{
{"", "", true},
{"0", "", true},
{"5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAbuatmU", "0000000000000000000000000000000000000000000000000000000000000000", false},
{"5HpHagT65TZzG1PH3CSu63k8DbuTZnNJf6HgyQNymvXmALAsm9s", "0000000000000000000000000000000000006d792070726976617465206b6579", false},
{"54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8azz", "", true},
{"5JTHas7yTFMBLqgFogxZFf8Vc5uKEbkE7yQAQ2g3xPHo2sNG1Ei", "54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd", false},
}
for _, test := range tests {
if privateKey, err := WifToPrivateKeyString(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 privateKey != test.expectedKey {
t.Fatalf("%s Failed: [%s] inputted [%s] expected but failed comparison of keys, got: %s", t.Name(), test.input, test.expectedKey, privateKey)
}
}
}
// ExampleWifToPrivateKeyString example using WifToPrivateKeyString()
func ExampleWifToPrivateKeyString() {
privateKey, err := WifToPrivateKeyString("5JTHas7yTFMBLqgFogxZFf8Vc5uKEbkE7yQAQ2g3xPHo2sNG1Ei")
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
fmt.Printf("private key: %s", privateKey)
// Output:private key: 54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd
}
// BenchmarkWifToPrivateKeyString benchmarks the method WifToPrivateKeyString()
func BenchmarkWifToPrivateKeyString(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = WifToPrivateKeyString("5JTHas7yTFMBLqgFogxZFf8Vc5uKEbkE7yQAQ2g3xPHo2sNG1Ei")
}
}
// TestCreateWif will test the method CreateWif()
func TestCreateWif(t *testing.T) {
t.Run("TestCreateWif", func(t *testing.T) {
t.Parallel()
// Create a WIF
wifKey, err := CreateWif()
require.NoError(t, err)
require.NotNil(t, wifKey)
// t.Log("WIF:", wifKey.String())
require.Equalf(t, 51, len(wifKey.String()), "WIF should be 51 characters long, got: %d", len(wifKey.String()))
})
t.Run("TestWifToPrivateKey", func(t *testing.T) {
t.Parallel()
// Create a WIF
wifKey, err := CreateWif()
require.NoError(t, err)
require.NotNil(t, wifKey)
// t.Log("WIF:", wifKey.String())
require.Equalf(t, 51, len(wifKey.String()), "WIF should be 51 characters long, got: %d", len(wifKey.String()))
// Convert WIF to Private Key
var privateKey *bec.PrivateKey
privateKey, err = WifToPrivateKey(wifKey.String())
require.NoError(t, err)
require.NotNil(t, privateKey)
privateKeyString := hex.EncodeToString(privateKey.Serialise())
// t.Log("Private Key:", privateKeyString)
require.Equalf(t, 64, len(privateKeyString), "Private Key should be 64 characters long, got: %d", len(privateKeyString))
})
}
// ExampleCreateWif example using CreateWif()
func ExampleCreateWif() {
wifKey, err := CreateWif()
if err != nil {
fmt.Println(err)
return
}
fmt.Println("WIF Key Generated Length:", len(wifKey.String()))
// Output: WIF Key Generated Length: 51
}
// BenchmarkCreateWif benchmarks the method CreateWif()
func BenchmarkCreateWif(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = CreateWif()
}
}
// TestCreateWifString will test the method CreateWifString()
func TestCreateWifString(t *testing.T) {
t.Run("TestCreateWifString", func(t *testing.T) {
t.Parallel()
// Create a WIF
wifKey, err := CreateWifString()
require.NoError(t, err)
require.NotNil(t, wifKey)
// t.Log("WIF:", wifKey)
require.Equalf(t, 51, len(wifKey), "WIF should be 51 characters long, got: %d", len(wifKey))
})
t.Run("TestWifToPrivateKeyString", func(t *testing.T) {
t.Parallel()
// Create a WIF
wifKey, err := CreateWifString()
require.NoError(t, err)
require.NotNil(t, wifKey)
// t.Log("WIF:", wifKey)
require.Equalf(t, 51, len(wifKey), "WIF should be 51 characters long, got: %d", len(wifKey))
// Convert WIF to Private Key
var privateKeyString string
privateKeyString, err = WifToPrivateKeyString(wifKey)
require.NoError(t, err)
require.NotNil(t, privateKeyString)
// t.Log("Private Key:", privateKeyString)
require.Equalf(t, 64, len(privateKeyString), "Private Key should be 64 characters long, got: %d", len(privateKeyString))
})
}
// ExampleCreateWifString example using CreateWifString()
func ExampleCreateWifString() {
wifKey, err := CreateWifString()
if err != nil {
fmt.Println(err)
return
}
fmt.Println("WIF Key Generated Length:", len(wifKey))
// Output: WIF Key Generated Length: 51
}
// BenchmarkCreateWifString benchmarks the method CreateWifString()
func BenchmarkCreateWifString(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = CreateWifString()
}
}
// TestWifFromString will test the method WifFromString()
func TestWifFromString(t *testing.T) {
t.Run("TestCreateWifFromPrivateKey", func(t *testing.T) {
t.Parallel()
// Create a Private Key
privateKey, err := CreatePrivateKeyString()
require.NoError(t, err)
require.NotNil(t, privateKey)
// Create a WIF
var wifKey *wif.WIF
wifKey, err = PrivateKeyToWif(privateKey)
require.NoError(t, err)
require.NotNil(t, wifKey)
wifKeyString := wifKey.String()
t.Log("WIF:", wifKeyString)
require.Equalf(t, 51, len(wifKeyString), "WIF should be 51 characters long, got: %d", len(wifKeyString))
// Convert WIF to Private Key
var privateKeyString string
privateKeyString, err = WifToPrivateKeyString(wifKeyString)
require.NoError(t, err)
require.NotNil(t, privateKeyString)
t.Log("Private Key:", privateKeyString)
require.Equalf(t, 64, len(privateKeyString), "Private Key should be 64 characters long, got: %d", len(privateKeyString))
// Compare Private Keys
require.Equalf(t, privateKey, privateKeyString, "Private Key should be equal, got: %s", privateKeyString)
// Decode WIF
var decodedWif *wif.WIF
decodedWif, err = WifFromString(wifKeyString)
require.NoError(t, err)
require.NotNil(t, decodedWif)
require.Equalf(t, wifKeyString, decodedWif.String(), "WIF should be equal, got: %s", decodedWif.String())
})
t.Run("TestWifFromStringMissingWIF", func(t *testing.T) {
t.Parallel()
_, err := WifFromString("")
require.Error(t, err)
require.Equal(t, ErrWifMissing, err)
})
t.Run("TestWifFromStringInvalidWIF", func(t *testing.T) {
t.Parallel()
_, err := WifFromString("invalid")
require.Error(t, err)
require.Equal(t, "malformed private key", err.Error())
})
}
// ExampleWifFromString example using WifFromString()
func ExampleWifFromString() {
// Create a Private Key
privateKey, err := CreatePrivateKeyString()
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Private Key Generated Length:", len(privateKey))
// Create a WIF
var wifKey *wif.WIF
wifKey, err = PrivateKeyToWif(privateKey)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("WIF Key Generated Length:", len(wifKey.String()))
// Decode WIF
var decodedWif *wif.WIF
decodedWif, err = WifFromString(wifKey.String())
if err != nil {
fmt.Println(err)
return
}
fmt.Println("WIF Key Decoded Length:", len(decodedWif.String()))
// Output: Private Key Generated Length: 64
// WIF Key Generated Length: 51
// WIF Key Decoded Length: 51
}
// BenchmarkWifFromString benchmarks the method WifFromString()
func BenchmarkWifFromString(b *testing.B) {
wifKey, _ := CreateWif()
wifString := wifKey.String()
for i := 0; i < b.N; i++ {
_, _ = WifFromString(wifString)
}
}