-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
pubkey_test.go
160 lines (138 loc) · 5.81 KB
/
pubkey_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
package bitcoin
import (
"encoding/hex"
"fmt"
"testing"
"github.com/libsv/go-bk/bec"
"github.com/stretchr/testify/assert"
)
// TestPubKeyFromPrivateKeyString will test the method PubKeyFromPrivateKeyString()
func TestPubKeyFromPrivateKeyString(t *testing.T) {
t.Parallel()
var tests = []struct {
inputKey string
expectedPubKey string
compressed bool
expectedError bool
}{
{"54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd", "031b8c93100d35bd448f4646cc4678f278351b439b52b303ea31ec9edb5475e73f", true, false},
{"54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd", "041b8c93100d35bd448f4646cc4678f278351b439b52b303ea31ec9edb5475e73f36e7ef720509250313fcf1b4c5af0dc7c5efa126efe2c3b7008e6f1487c61f31", false, false},
{"0", "", true, true},
{"", "", true, true},
}
for _, test := range tests {
if pubKey, err := PubKeyFromPrivateKeyString(test.inputKey, test.compressed); err != nil && !test.expectedError {
t.Fatalf("%s Failed: [%s] inputted and error not expected but got: %s", t.Name(), test.inputKey, err.Error())
} else if err == nil && test.expectedError {
t.Fatalf("%s Failed: [%s] inputted and error was expected", t.Name(), test.inputKey)
} else if pubKey != test.expectedPubKey {
t.Fatalf("%s Failed: [%s] inputted and [%s] expected, but got: %s", t.Name(), test.inputKey, test.expectedPubKey, pubKey)
}
}
}
// ExamplePubKeyFromPrivateKeyString example using PubKeyFromPrivateKeyString()
func ExamplePubKeyFromPrivateKeyString() {
pubKey, err := PubKeyFromPrivateKeyString("54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd", true)
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
fmt.Printf("pubkey generated: %s", pubKey)
// Output:pubkey generated: 031b8c93100d35bd448f4646cc4678f278351b439b52b303ea31ec9edb5475e73f
}
// BenchmarkPubKeyFromPrivateKeyString benchmarks the method PubKeyFromPrivateKeyString()
func BenchmarkPubKeyFromPrivateKeyString(b *testing.B) {
key, _ := CreatePrivateKeyString()
for i := 0; i < b.N; i++ {
_, _ = PubKeyFromPrivateKeyString(key, true)
}
}
// TestPubKeyFromPrivateKey will test the method PubKeyFromPrivateKey()
func TestPubKeyFromPrivateKey(t *testing.T) {
t.Parallel()
priv, err := PrivateKeyFromString("54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd")
assert.NoError(t, err)
assert.NotNil(t, priv)
var tests = []struct {
inputKey *bec.PrivateKey
expectedPubKey string
expectedError bool
}{
{priv, "031b8c93100d35bd448f4646cc4678f278351b439b52b303ea31ec9edb5475e73f", false},
}
for _, test := range tests {
if pubKey := PubKeyFromPrivateKey(test.inputKey, true); pubKey != test.expectedPubKey {
t.Fatalf("%s Failed: [%v] inputted and [%s] expected, but got: %s", t.Name(), test.inputKey, test.expectedPubKey, pubKey)
}
}
}
// TestPubKeyFromPrivateKeyPanic tests for nil case in PubKeyFromPrivateKey()
func TestPubKeyFromPrivateKeyPanic(t *testing.T) {
t.Parallel()
assert.Panics(t, func() {
pubKey := PubKeyFromPrivateKey(nil, true)
assert.NotEqual(t, 0, len(pubKey))
})
}
// ExamplePubKeyFromPrivateKey example using PubKeyFromPrivateKey()
func ExamplePubKeyFromPrivateKey() {
privateKey, err := PrivateKeyFromString("54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd")
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
pubKey := PubKeyFromPrivateKey(privateKey, true)
fmt.Printf("pubkey generated: %s", pubKey)
// Output:pubkey generated: 031b8c93100d35bd448f4646cc4678f278351b439b52b303ea31ec9edb5475e73f
}
// BenchmarkPubKeyFromPrivateKey benchmarks the method PubKeyFromPrivateKey()
func BenchmarkPubKeyFromPrivateKey(b *testing.B) {
key, _ := CreatePrivateKey()
for i := 0; i < b.N; i++ {
_ = PubKeyFromPrivateKey(key, true)
}
}
// TestPubKeyFromString will test the method PubKeyFromString()
func TestPubKeyFromString(t *testing.T) {
t.Parallel()
var tests = []struct {
inputKey string
expectedPubKey string
expectedNil bool
expectedError bool
}{
{"", "", true, true},
{"0", "", true, true},
{"00000", "", true, true},
{"031b8c93100d35bd448f4646cc4678f278351b439b52b303ea31ec9edb5475e73f", "031b8c93100d35bd448f4646cc4678f278351b439b52b303ea31ec9edb5475e73f", false, false},
}
for _, test := range tests {
if pubKey, err := PubKeyFromString(test.inputKey); err != nil && !test.expectedError {
t.Fatalf("%s Failed: [%s] inputted and error not expected but got: %s", t.Name(), test.inputKey, err.Error())
} else if err == nil && test.expectedError {
t.Fatalf("%s Failed: [%s] inputted and error was expected", t.Name(), test.inputKey)
} else if pubKey != nil && test.expectedNil {
t.Fatalf("%s Failed: [%s] inputted and nil was expected", t.Name(), test.inputKey)
} else if pubKey == nil && !test.expectedNil {
t.Fatalf("%s Failed: [%s] inputted and nil was NOT expected", t.Name(), test.inputKey)
} else if pubKey != nil && hex.EncodeToString(pubKey.SerialiseCompressed()) != test.expectedPubKey {
t.Fatalf("%s Failed: [%s] inputted and [%s] expected, but got: %s", t.Name(), test.inputKey, test.expectedPubKey, hex.EncodeToString(pubKey.SerialiseCompressed()))
}
}
}
// ExamplePubKeyFromString example using PubKeyFromString()
func ExamplePubKeyFromString() {
pubKey, err := PubKeyFromString("031b8c93100d35bd448f4646cc4678f278351b439b52b303ea31ec9edb5475e73f")
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
fmt.Printf("pubkey from string: %s", hex.EncodeToString(pubKey.SerialiseCompressed()))
// Output:pubkey from string: 031b8c93100d35bd448f4646cc4678f278351b439b52b303ea31ec9edb5475e73f
}
// BenchmarkPubKeyFromString benchmarks the method PubKeyFromString()
func BenchmarkPubKeyFromString(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = PubKeyFromString("031b8c93100d35bd448f4646cc4678f278351b439b52b303ea31ec9edb5475e73f")
}
}