-
Notifications
You must be signed in to change notification settings - Fork 0
/
wif_test.go
143 lines (127 loc) · 2.81 KB
/
wif_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
package crypto
import (
"crypto/ecdsa"
"encoding/hex"
"testing"
"github.com/nspcc-dev/neofs-crypto/test"
"github.com/stretchr/testify/require"
)
type (
NPPrompt struct {
Key *ecdsa.PrivateKey
WIF string
}
WIFTestCase struct {
Error error
Name string
WIF string
Key *ecdsa.PrivateKey
}
)
func npPromptSKWIF(t *testing.T) *NPPrompt {
key, err := hex.DecodeString("c428b4a06f166fde9f8afcf918194acdde35aa2612ecf42fe0c94273425ded21")
require.NoError(t, err)
sk, err := UnmarshalPrivateKey(key)
require.NoError(t, err)
return &NPPrompt{
Key: sk,
WIF: "L3o221BojgcCPYgdbXsm6jn7ayTZ72xwREvBHXKknR8VJ3G4WmjB",
}
}
func TestWIF(t *testing.T) {
kw := npPromptSKWIF(t)
t.Run("encoding", func(t *testing.T) {
cases := []WIFTestCase{
{
Name: "success #1",
Key: test.DecodeKey(0),
WIF: "KyKwsDQhb6ncTw9wfoJqMXUABTsMLi36u7BZBBKo5uzmGFEHHDVu",
},
{
Name: "success #2",
Key: test.DecodeKey(1),
WIF: "Ky2aXYjbxUoMoLaPhyEw8U37BVD6N1rw7mfbbxDv3ULqM9dta7ZW",
},
{
Name: "success #3",
Key: test.DecodeKey(2),
WIF: "L5LDzGxboJv2CqWKp7v8UGddAMf834DZvMK7muRgNqXTSTwG8pRJ",
},
{
Name: "np-prompt generated SK/WIF",
Key: kw.Key,
WIF: kw.WIF,
},
{
Name: "empty key",
Error: ErrEmptyPrivateKey,
Key: nil,
},
}
for i := range cases {
current := cases[i]
t.Run(current.Name, func(t *testing.T) {
actual, err := WIFEncode(current.Key)
switch current.Error {
case nil:
require.NoError(t, err)
require.Equal(t, current.WIF, actual)
default:
require.ErrorIs(t, err, current.Error)
}
})
}
})
t.Run("decoding", func(t *testing.T) {
cases := []WIFTestCase{
{
Name: "success #1",
Key: test.DecodeKey(0),
WIF: "KyKwsDQhb6ncTw9wfoJqMXUABTsMLi36u7BZBBKo5uzmGFEHHDVu",
},
{
Name: "success #2",
Key: test.DecodeKey(1),
WIF: "Ky2aXYjbxUoMoLaPhyEw8U37BVD6N1rw7mfbbxDv3ULqM9dta7ZW",
},
{
Name: "success #3",
Key: test.DecodeKey(2),
WIF: "L5LDzGxboJv2CqWKp7v8UGddAMf834DZvMK7muRgNqXTSTwG8pRJ",
},
{
Name: "np-prompt generated SK/WIF",
Key: kw.Key,
WIF: kw.WIF,
},
{
Error: ErrBadChecksum,
Name: "bad checksum",
WIF: "KyKwsDQhb6ncTw9wfoJqMXUABTsMLi36u7BZBBKo5uzmGF7rYxf5",
},
{
Error: ErrBadWIF,
Name: "bad wif",
WIF: "bad_wif",
},
{
Error: ErrBadWIF,
Name: "bad wif length",
WIF: "L5LDzGxboJv2CqWKp7v8UGddAMf834DZvMK7muRgNqXTSTueCExxDEADBEAF",
},
}
for i := range cases {
current := cases[i]
t.Run(current.Name, func(t *testing.T) {
actual, err := WIFDecode(current.WIF)
switch current.Error {
case nil:
require.NoError(t, err)
require.Equal(t, current.Key, actual)
default:
require.ErrorIs(t, err, current.Error)
}
})
}
})
}