forked from BitcoinSchema/go-bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprivate_key_test.go
373 lines (325 loc) · 14.6 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
package bitcoin
import (
"encoding/hex"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
// 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.Serialize()))
}
// ExampleCreatePrivateKey example using CreatePrivateKey()
func ExampleCreatePrivateKey() {
rawKey, err := CreatePrivateKey()
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
} else if len(rawKey.Serialize()) > 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.Serialize()) != 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.Serialize()))
}
}
}
// 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.Serialize()))
// 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.Serialize()) != 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.Serialize()))
}
}
}
// 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.Serialize()), hex.EncodeToString(publicKey.SerializeCompressed()))
// 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 wif, 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 wif == nil && !test.expectedNil {
t.Fatalf("%s Failed: [%s] inputted and was nil but not expected", t.Name(), test.input)
} else if wif != nil && test.expectedNil {
t.Fatalf("%s Failed: [%s] inputted and was NOT nil but expected to be nil", t.Name(), test.input)
} else if wif != nil && wif.String() != test.expectedWif {
t.Fatalf("%s Failed: [%s] inputted [%s] expected but failed comparison of keys, got: %s", t.Name(), test.input, test.expectedWif, wif.String())
}
}
}
// ExamplePrivateKeyToWif example using PrivateKeyToWif()
func ExamplePrivateKeyToWif() {
wif, err := PrivateKeyToWif("54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd")
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
fmt.Printf("converted wif: %s", wif.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 wif, 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 wif != test.expectedWif {
t.Fatalf("%s Failed: [%s] inputted [%s] expected but failed comparison of keys, got: %s", t.Name(), test.input, test.expectedWif, wif)
}
}
}
// ExamplePrivateKeyToWifString example using PrivateKeyToWifString()
func ExamplePrivateKeyToWifString() {
wif, err := PrivateKeyToWifString("54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd")
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
fmt.Printf("converted wif: %s", wif)
// 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.Serialize()) != 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.Serialize()))
}
}
}
// 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.Serialize()))
// 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")
}
}