-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallet_test.go
331 lines (273 loc) · 9.13 KB
/
wallet_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
package kms
import (
"context"
"encoding/hex"
"fmt"
"testing"
"github.com/decred/dcrd/dcrec/secp256k1/v4"
"github.com/hashicorp/vault/sdk/logical"
"github.com/perme-io/vault-plugin-secrets-kms/chains"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/sha3"
)
type testWalletEnv struct {
PrivateKey *secp256k1.PrivateKey
PublicKey *secp256k1.PublicKey
PrivateKeyString string
PublicKeyString string
PrivAddress string
PubAddress string
}
func TestSecp256k1(t *testing.T) {
walletEnv := testWalletEnv{}
if privateKey, err := secp256k1.GeneratePrivateKey(); err == nil {
walletEnv.PrivateKey = privateKey
walletEnv.PublicKey = privateKey.PubKey()
walletEnv.PrivAddress = privateKey.Key.String()
} else if err != nil {
t.Errorf("failed to generate privateKey with secp256k1: err=%v", err)
}
pubKeyHash := sha3.Sum256(walletEnv.PublicKey.SerializeCompressed())
walletEnv.PubAddress = hex.EncodeToString(pubKeyHash[len(pubKeyHash)-20:])
walletEnv.PrivateKeyString = hex.EncodeToString(walletEnv.PrivateKey.Serialize())
walletEnv.PublicKeyString = hex.EncodeToString(walletEnv.PublicKey.SerializeCompressed())
t.Logf("privateKey : %v", walletEnv.PrivateKey)
t.Logf("privateKey string : %v", walletEnv.PrivateKeyString)
t.Logf("PrivateKey Address : %v", walletEnv.PrivAddress)
t.Logf("publicKey : %v", walletEnv.PublicKey)
t.Logf("publicKey string : %v", walletEnv.PublicKeyString)
t.Logf("publicKey address : %v", walletEnv.PubAddress)
pkBytes := walletEnv.PrivateKey.Key.Bytes()
privKey := secp256k1.PrivKeyFromBytes(pkBytes[:])
privKeyString := hex.EncodeToString(privKey.Serialize())
require.Equalf(t, walletEnv.PrivateKeyString, privKeyString, "expected=%v, actual=%v", walletEnv.PrivateKeyString, privKeyString)
address := privKey.Key.String()
require.Equalf(t, walletEnv.PrivAddress, address, "expected=%v, actual=%v", walletEnv.PrivAddress, address)
pubKey, err := secp256k1.ParsePubKey(walletEnv.PublicKey.SerializeCompressed())
pubKeyString := hex.EncodeToString(pubKey.SerializeCompressed())
if err != nil {
t.Errorf("failed to parse publicKey: err=%v", err)
}
require.Equalf(t, walletEnv.PublicKeyString, pubKeyString, "expected=%v, actual=%v", walletEnv.PublicKeyString, pubKeyString)
}
func TestPubKeyAddress(t *testing.T) {
testCases := []struct {
chainName chains.ChainName
privKeyString string
expectedAddress string
}{
{
chains.ICON,
"1a5e70bfd427ec9ec3decf8d6e3461dfb4dbbde4351e071d9728d96e711e1b9c",
"hx5443d0db003fd7202046bbf31eaeade60af20c41",
},
{
chains.AERGO,
"83e992df7015dcc946ab9b404b65e2a786913761e9d09f45675e9dccd1a47a2e",
"AmN5kDEYAxUzFvjX6541AVrPkzeg2H4Qxc79ssBWUpqNLbur9M36",
},
}
for _, tc := range testCases {
testName := fmt.Sprintf("Test Public Key Address %s", tc.chainName)
t.Run(testName, func(t *testing.T) {
walletEnv := testWalletEnv{}
privKeyBytes, err := hex.DecodeString(tc.privKeyString)
if err != nil {
t.Errorf("error=%v", err)
}
privateKey := secp256k1.PrivKeyFromBytes(privKeyBytes)
walletEnv.PrivateKey = privateKey
walletEnv.PrivateKeyString = hex.EncodeToString(walletEnv.PrivateKey.Serialize())
walletEnv.PublicKey = privateKey.PubKey()
var chain chains.Chain
switch tc.chainName {
case chains.ICON:
chain = chains.IconChain{PrivateKey: privateKey}
case chains.AERGO:
chain = chains.AergoChain{PrivateKey: privateKey}
default:
assert.FailNowf(t, "unsupported chain", "chainName=%v", tc.chainName)
}
pubKeySerialized := chain.GetPublicKeySerialized()
walletEnv.PubAddress = chain.GetPublicKeyAddress(pubKeySerialized)
t.Logf("privateKey : %v", walletEnv.PrivateKey)
t.Logf("privateKey string : %v", walletEnv.PrivateKeyString)
t.Logf("publicKey serialized : %v", hex.EncodeToString(pubKeySerialized))
t.Logf("publicKey address : %v", walletEnv.PubAddress)
require.Equalf(t, tc.expectedAddress, walletEnv.PubAddress, "expected=%v, actual=%v", tc.expectedAddress, walletEnv.PubAddress)
})
}
}
func TestCreateWallet(t *testing.T) {
testCases := []struct {
chainName chains.ChainName
expectedErrMsg interface{}
}{
{chains.ICON, nil},
{chains.AERGO, nil},
{"solana", "unknown chain name"},
}
for _, tc := range testCases {
testName := fmt.Sprintf("Test Create Wallet %s", tc.chainName)
t.Run(testName, func(t *testing.T) {
wallet, err := createWallet(tc.chainName)
t.Logf("chainName=%v, error=%v", tc.chainName, err)
switch tc.chainName {
case chains.ICON, chains.AERGO:
require.Nilf(t, err, "createWallet err: expected nil, actual=%v", err)
require.NotNilf(t, wallet, "createWallet wallet: expected not nil, actual=%v", wallet)
default:
expectedErrMsg := tc.expectedErrMsg.(string)
require.ErrorContainsf(t, err, expectedErrMsg, "createWallet err: expected=%v, actual=%v", expectedErrMsg, err.Error())
}
})
}
}
const (
username = "tesuser@email.com"
token = "test-token"
)
// TestWallet mocks the creation, read, update, and delete
// of the backend wallet for kms.
func TestWallet(t *testing.T) {
b, reqStorage := getTestBackend(t)
t.Run("Test Wallet", func(t *testing.T) {
resp, err := testWalletCreate(t, b, reqStorage, map[string]interface{}{
"username": username,
"chainName": "icon",
})
assert.NoError(t, err)
var walletAddress string
if addr, ok := resp.Data["address"]; ok {
walletAddress = addr.(string)
}
t.Logf("wallet address : %v", walletAddress)
err = testWalletCreateWithEmptyUsername(t, b, reqStorage, map[string]interface{}{
"username": "",
})
assert.Error(t, err)
reqData := map[string]interface{}{
"username": username,
"address": walletAddress,
}
expectedData := map[string]interface{}{
"address": walletAddress,
}
err = testWalletRead(t, b, reqStorage, reqData, expectedData)
assert.NoError(t, err)
resp, err = testWalletUpdate(t, b, reqStorage, map[string]interface{}{
"username": username,
"chainName": "icon",
})
assert.NoError(t, err)
if addr, ok := resp.Data["address"]; ok {
walletAddress = addr.(string)
}
t.Logf("updated wallet address : %v", walletAddress)
reqData["address"] = walletAddress
expectedData["address"] = walletAddress
err = testWalletRead(t, b, reqStorage, reqData, expectedData)
assert.NoError(t, err)
err = testWalletDelete(t, b, reqStorage, map[string]interface{}{
"username": username,
"address": walletAddress,
"chainName": "icon",
})
assert.NoError(t, err)
})
}
func testWalletCreate(t *testing.T, b logical.Backend, s logical.Storage, d map[string]interface{}) (*logical.Response, error) {
resp, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.CreateOperation,
ClientToken: token,
Path: walletStoragePath,
Data: d,
Storage: s,
})
if err != nil {
return nil, err
}
if resp != nil && resp.IsError() {
return nil, resp.Error()
}
return resp, nil
}
func testWalletCreateWithEmptyUsername(t *testing.T, b logical.Backend, s logical.Storage, d map[string]interface{}) error {
resp, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.CreateOperation,
ClientToken: token,
Path: walletStoragePath,
Data: d,
Storage: s,
})
if err != nil {
return err
}
if resp != nil && resp.IsError() {
return resp.Error()
}
return nil
}
func testWalletUpdate(t *testing.T, b logical.Backend, s logical.Storage, d map[string]interface{}) (*logical.Response, error) {
resp, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.UpdateOperation,
ClientToken: token,
Path: walletStoragePath,
Data: d,
Storage: s,
})
if err != nil {
return nil, err
}
if resp != nil && resp.IsError() {
return nil, resp.Error()
}
return resp, nil
}
func testWalletRead(t *testing.T, b logical.Backend, s logical.Storage, d map[string]interface{}, expected map[string]interface{}) error {
resp, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ReadOperation,
ClientToken: token,
Path: walletStoragePath,
Data: d,
Storage: s,
})
if err != nil {
return err
}
if resp == nil && expected == nil {
return nil
}
if resp.IsError() {
return resp.Error()
}
if len(resp.Data) != 2 {
return fmt.Errorf("read data mismatch (expected %d values, got %d)", len(expected), len(resp.Data))
}
for k, expectedV := range expected {
actualV, ok := resp.Data[k]
if !ok {
return fmt.Errorf(`expected data["%s"] = %v but was not included in read output"`, k, expectedV)
} else if expectedV != actualV {
return fmt.Errorf(`expected data["%s"] = %v, instead got %v"`, k, expectedV, actualV)
}
}
return nil
}
func testWalletDelete(t *testing.T, b logical.Backend, s logical.Storage, d map[string]interface{}) error {
resp, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.DeleteOperation,
ClientToken: token,
Path: walletStoragePath,
Data: d,
Storage: s,
})
if err != nil {
return err
}
if resp != nil && resp.IsError() {
return resp.Error()
}
return nil
}