-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaccount_test.go
395 lines (358 loc) · 15 KB
/
account_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
// Copyright © 2020 Weald Technology Trading
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package distributed_test
import (
"context"
"encoding/hex"
"fmt"
"math/rand"
"os"
"path/filepath"
"sync"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
e2types "github.com/wealdtech/go-eth2-types/v2"
distributed "github.com/wealdtech/go-eth2-wallet-distributed"
keystorev4 "github.com/wealdtech/go-eth2-wallet-encryptor-keystorev4"
filesystem "github.com/wealdtech/go-eth2-wallet-store-filesystem"
scratch "github.com/wealdtech/go-eth2-wallet-store-scratch"
e2wtypes "github.com/wealdtech/go-eth2-wallet-types/v2"
)
// _byteArray is a helper to turn a string in to a byte array
func _byteArray(input string) []byte {
x, _ := hex.DecodeString(input)
return x
}
func TestImportAccount(t *testing.T) {
tests := []struct {
name string
accountName string
key []byte
pubKey []byte
signingThreshold uint32
verificationVector [][]byte
participants map[uint64]string
passphrase []byte
err string
}{
{
name: "Empty",
accountName: "",
err: "account name missing",
},
{
name: "Invalid",
accountName: "_bad",
err: `invalid account name "_bad"`,
},
{
name: "KeyMissing",
accountName: "test",
signingThreshold: 2,
verificationVector: [][]byte{
_byteArray("b5f7f572e3f50a970af6c13f02e2c20900cda0dffdcf8b2e2a06c78ba2bae667bfa7aab01b36fba268da4aa2aba5c68f"),
_byteArray("a88427e16f45b632f83247220bd885241cff6fd035803e976fe96c6352933d01a6205d6f3e87a96789cddcca64bbcf25"),
},
participants: map[uint64]string{1: "foo", 2: "bar", 3: "baz"},
passphrase: []byte("test passphrase"),
err: "private key missing",
},
{
name: "VerificationVectorMissing",
accountName: "test",
key: _byteArray("220091d10843519cd1c452a4ec721d378d7d4c5ece81c4b5556092d410e5e0e1"),
signingThreshold: 2,
participants: map[uint64]string{1: "foo", 2: "bar", 3: "baz"},
passphrase: []byte("test passphrase"),
err: "verification vector missing",
},
{
name: "ParticipantsMissing",
accountName: "test",
key: _byteArray("220091d10843519cd1c452a4ec721d378d7d4c5ece81c4b5556092d410e5e0e1"),
signingThreshold: 2,
verificationVector: [][]byte{
_byteArray("b5f7f572e3f50a970af6c13f02e2c20900cda0dffdcf8b2e2a06c78ba2bae667bfa7aab01b36fba268da4aa2aba5c68f"),
_byteArray("a88427e16f45b632f83247220bd885241cff6fd035803e976fe96c6352933d01a6205d6f3e87a96789cddcca64bbcf25"),
},
passphrase: []byte("test passphrase"),
err: "participants missing",
},
{
name: "SigninghTresholdMissing",
accountName: "test",
key: _byteArray("220091d10843519cd1c452a4ec721d378d7d4c5ece81c4b5556092d410e5e0e1"),
verificationVector: [][]byte{
_byteArray("b5f7f572e3f50a970af6c13f02e2c20900cda0dffdcf8b2e2a06c78ba2bae667bfa7aab01b36fba268da4aa2aba5c68f"),
_byteArray("a88427e16f45b632f83247220bd885241cff6fd035803e976fe96c6352933d01a6205d6f3e87a96789cddcca64bbcf25"),
},
participants: map[uint64]string{1: "foo", 2: "bar", 3: "baz"},
passphrase: []byte("test passphrase"),
err: "invalid signing threshold:participant ratio",
},
{
name: "SigningThresholdTooLow",
accountName: "test",
key: _byteArray("220091d10843519cd1c452a4ec721d378d7d4c5ece81c4b5556092d410e5e0e1"),
signingThreshold: 1,
verificationVector: [][]byte{
_byteArray("b5f7f572e3f50a970af6c13f02e2c20900cda0dffdcf8b2e2a06c78ba2bae667bfa7aab01b36fba268da4aa2aba5c68f"),
_byteArray("a88427e16f45b632f83247220bd885241cff6fd035803e976fe96c6352933d01a6205d6f3e87a96789cddcca64bbcf25"),
},
participants: map[uint64]string{1: "foo", 2: "bar", 3: "baz"},
passphrase: []byte("test passphrase"),
err: "invalid signing threshold:participant ratio",
},
{
name: "ImbalancedParticipants",
accountName: "test",
key: _byteArray("220091d10843519cd1c452a4ec721d378d7d4c5ece81c4b5556092d410e5e0e1"),
signingThreshold: 3,
verificationVector: [][]byte{
_byteArray("b5f7f572e3f50a970af6c13f02e2c20900cda0dffdcf8b2e2a06c78ba2bae667bfa7aab01b36fba268da4aa2aba5c68f"),
_byteArray("a88427e16f45b632f83247220bd885241cff6fd035803e976fe96c6352933d01a6205d6f3e87a96789cddcca64bbcf25"),
},
participants: map[uint64]string{1: "foo", 2: "bar", 3: "baz", 4: "qux", 5: "quux"},
passphrase: []byte("test passphrase"),
err: "verification vector invalid",
},
{
name: "Good",
accountName: "test",
key: _byteArray("220091d10843519cd1c452a4ec721d378d7d4c5ece81c4b5556092d410e5e0e1"),
pubKey: _byteArray("940e2565d2e3079dc1642d042ae000ee2182b37d94f40b68d8376ac1757ff9b87cdd31893986b0049dd62e369bc63d4e"),
signingThreshold: 2,
verificationVector: [][]byte{
_byteArray("b5f7f572e3f50a970af6c13f02e2c20900cda0dffdcf8b2e2a06c78ba2bae667bfa7aab01b36fba268da4aa2aba5c68f"),
_byteArray("a88427e16f45b632f83247220bd885241cff6fd035803e976fe96c6352933d01a6205d6f3e87a96789cddcca64bbcf25"),
},
participants: map[uint64]string{1: "foo", 2: "bar", 3: "baz"},
passphrase: []byte("test passphrase"),
},
{
name: "Duplicate",
accountName: "test",
key: _byteArray("220091d10843519cd1c452a4ec721d378d7d4c5ece81c4b5556092d410e5e0e2"),
signingThreshold: 2,
verificationVector: [][]byte{
_byteArray("b5f7f572e3f50a970af6c13f02e2c20900cda0dffdcf8b2e2a06c78ba2bae667bfa7aab01b36fba268da4aa2aba5c68f"),
_byteArray("a88427e16f45b632f83247220bd885241cff6fd035803e976fe96c6352933d01a6205d6f3e87a96789cddcca64bbcf25"),
},
participants: map[uint64]string{1: "foo", 2: "bar", 3: "baz"},
passphrase: []byte("test passphrase"),
err: `account with name "test" already exists`,
},
}
store := scratch.New()
encryptor := keystorev4.New()
wallet, err := distributed.CreateWallet(context.Background(), "test wallet", store, encryptor)
require.NoError(t, err)
// Try to import without unlocking the wallet; should fail
_, err = wallet.(e2wtypes.WalletDistributedAccountImporter).ImportDistributedAccount(context.Background(), "Locked", _byteArray("220091d10843519cd1c452a4ec721d378d7d4c5ece81c4b5556092d410e5e0e1"), 2, [][]byte{
_byteArray("b5f7f572e3f50a970af6c13f02e2c20900cda0dffdcf8b2e2a06c78ba2bae667bfa7aab01b36fba268da4aa2aba5c68f"),
_byteArray("a88427e16f45b632f83247220bd885241cff6fd035803e976fe96c6352933d01a6205d6f3e87a96789cddcca64bbcf25"),
},
map[uint64]string{1: "foo", 2: "bar", 3: "baz"},
[]byte("test passphrase"))
require.EqualError(t, err, "wallet must be unlocked to create accounts")
err = wallet.(e2wtypes.WalletLocker).Unlock(context.Background(), nil)
require.NoError(t, err)
defer func() {
if err := wallet.(e2wtypes.WalletLocker).Lock(context.Background()); err != nil {
panic(err)
}
}()
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
account, err := wallet.(e2wtypes.WalletDistributedAccountImporter).ImportDistributedAccount(context.Background(), test.accountName, test.key, test.signingThreshold, test.verificationVector, test.participants, test.passphrase)
if test.err != "" {
require.Error(t, err)
assert.EqualError(t, err, test.err)
} else {
require.NoError(t, err)
assert.Equal(t, test.accountName, account.Name())
assert.Equal(t, test.pubKey, account.PublicKey().Marshal())
pathProvider, isPathProvider := account.(e2wtypes.AccountPathProvider)
require.True(t, isPathProvider)
assert.NotNil(t, pathProvider.Path())
// Should not be able to obtain private key from a locked account
_, err = account.(e2wtypes.AccountPrivateKeyProvider).PrivateKey(context.Background())
assert.NotNil(t, err)
locker, isLocker := account.(e2wtypes.AccountLocker)
require.True(t, isLocker)
err = locker.Unlock(context.Background(), test.passphrase)
require.NoError(t, err)
_, err := account.(e2wtypes.AccountPrivateKeyProvider).PrivateKey(context.Background())
assert.Nil(t, err)
}
})
}
}
func TestRebuildIndex(t *testing.T) {
accountName := "test"
key := _byteArray("220091d10843519cd1c452a4ec721d378d7d4c5ece81c4b5556092d410e5e0e1")
signingThreshold := uint32(2)
verificationVector := [][]byte{
_byteArray("b5f7f572e3f50a970af6c13f02e2c20900cda0dffdcf8b2e2a06c78ba2bae667bfa7aab01b36fba268da4aa2aba5c68f"),
_byteArray("a88427e16f45b632f83247220bd885241cff6fd035803e976fe96c6352933d01a6205d6f3e87a96789cddcca64bbcf25"),
}
participants := map[uint64]string{1: "foo", 2: "bar", 3: "baz"}
passphrase := []byte("test passphrase")
// #nosec G404
path := filepath.Join(os.TempDir(), fmt.Sprintf("TestRebuildIndex-%d", rand.Int31()))
defer os.RemoveAll(path)
store := filesystem.New(filesystem.WithLocation(path))
encryptor := keystorev4.New()
wallet, err := distributed.CreateWallet(context.Background(), "test wallet", store, encryptor)
require.NoError(t, err)
err = wallet.(e2wtypes.WalletLocker).Unlock(context.Background(), nil)
require.NoError(t, err)
defer func() {
if err := wallet.(e2wtypes.WalletLocker).Lock(context.Background()); err != nil {
panic(err)
}
}()
_, err = wallet.(e2wtypes.WalletDistributedAccountImporter).ImportDistributedAccount(context.Background(), accountName, key, signingThreshold, verificationVector, participants, passphrase)
require.NoError(t, err)
// Ensure the wallet can see this account.
foundAccount := false
for account := range wallet.Accounts(context.Background()) {
if account.Name() == "test" {
foundAccount = true
break
}
}
require.True(t, foundAccount)
// Find and confirm the wallet index path.
indexPath := filepath.Join(
wallet.(e2wtypes.StoreProvider).Store().(e2wtypes.StoreLocationProvider).Location(),
wallet.ID().String(),
"index")
index, err := os.Open(indexPath)
require.NoError(t, err)
require.NoError(t, index.Close())
// Delete the index.
require.NoError(t, os.Remove(indexPath))
// Re-fetch the wallet and account.
wallet, err = distributed.OpenWallet(context.Background(), "test wallet", store, encryptor)
require.NoError(t, err)
// Re-fetch the account.
foundAccount = false
for account := range wallet.Accounts(context.Background()) {
if account.Name() == "test" {
foundAccount = true
break
}
}
require.True(t, foundAccount)
// Confirm the index has been rebuilt.
index, err = os.Open(indexPath)
require.NoError(t, err)
require.NoError(t, index.Close())
}
func TestAccountLocking(t *testing.T) {
accountName := "test"
key := _byteArray("220091d10843519cd1c452a4ec721d378d7d4c5ece81c4b5556092d410e5e0e1")
signingThreshold := uint32(2)
verificationVector := [][]byte{
_byteArray("b5f7f572e3f50a970af6c13f02e2c20900cda0dffdcf8b2e2a06c78ba2bae667bfa7aab01b36fba268da4aa2aba5c68f"),
_byteArray("a88427e16f45b632f83247220bd885241cff6fd035803e976fe96c6352933d01a6205d6f3e87a96789cddcca64bbcf25"),
}
participants := map[uint64]string{1: "foo", 2: "bar", 3: "baz"}
// #nosec G404
path := filepath.Join(os.TempDir(), fmt.Sprintf("TestRebuildIndex-%d", rand.Int31()))
defer os.RemoveAll(path)
store := filesystem.New(filesystem.WithLocation(path))
encryptor := keystorev4.New()
wallet, err := distributed.CreateWallet(context.Background(), "test wallet", store, encryptor)
require.NoError(t, err)
require.NoError(t, wallet.(e2wtypes.WalletLocker).Unlock(context.Background(), nil))
account, err := wallet.(e2wtypes.WalletDistributedAccountImporter).ImportDistributedAccount(context.Background(), accountName, key, signingThreshold, verificationVector, participants, []byte("account passphrase"))
require.NoError(t, err)
locker, isLocker := account.(e2wtypes.AccountLocker)
require.True(t, isLocker)
// Ensure the wallet is not unlocked to begin with.
unlocked, err := locker.IsUnlocked(context.Background())
require.NoError(t, err)
require.False(t, unlocked)
// Ensure the wallet is not unlocked when an incorrect passphrase is supplied.
require.EqualError(t, locker.Unlock(context.Background(), []byte("bad passphrase")), "incorrect passphrase")
unlocked, err = locker.IsUnlocked(context.Background())
require.NoError(t, err)
require.False(t, unlocked)
// Ensure the wallet is unlocked when the correct passphrase is supplied.
require.NoError(t, locker.Unlock(context.Background(), []byte("account passphrase")))
unlocked, err = locker.IsUnlocked(context.Background())
require.NoError(t, err)
require.True(t, unlocked)
// ensure the wallet remains unlocked when already unlocked.
require.NoError(t, locker.Unlock(context.Background(), []byte("account passphrase")))
unlocked, err = locker.IsUnlocked(context.Background())
require.NoError(t, err)
require.True(t, unlocked)
}
func TestConcurrentImport(t *testing.T) {
store := scratch.New()
encryptor := keystorev4.New()
wallet, err := distributed.CreateWallet(context.Background(), "test wallet", store, encryptor)
require.NoError(t, err)
locker, isLocker := wallet.(e2wtypes.WalletLocker)
require.True(t, isLocker)
require.NoError(t, locker.Unlock(context.Background(), nil))
// Create a number of runners that will try to create accounts simultaneously.
var runWG sync.WaitGroup
var setupWG sync.WaitGroup
starter := make(chan any)
numAccounts := 64
for i := 0; i < numAccounts; i++ {
setupWG.Add(1)
runWG.Add(1)
go func() {
id := rand.Uint32()
name := fmt.Sprintf("Test account %d", id)
privateKey, err := e2types.GenerateBLSPrivateKey()
require.NoError(t, err)
key := privateKey.Marshal()
verificationVector := [][]byte{
privateKey.PublicKey().Marshal(),
_byteArray("b5f7f572e3f50a970af6c13f02e2c20900cda0dffdcf8b2e2a06c78ba2bae667bfa7aab01b36fba268da4aa2aba5c68f"),
}
participants := map[uint64]string{
1: "host1:12345",
2: "host2:12345",
3: "host3:12345",
}
setupWG.Done()
<-starter
account, err := wallet.(e2wtypes.WalletDistributedAccountImporter).ImportDistributedAccount(context.Background(), name, key, 2, verificationVector, participants, []byte("test"))
require.NoError(t, err)
require.NotNil(t, account)
runWG.Done()
}()
}
// Wait for setup to complete.
setupWG.Wait()
// Start the jobs by closing the channel.
close(starter)
// Wait for run to complete
runWG.Wait()
// Confirm that all accounts have been created.
wallet, err = distributed.OpenWallet(context.Background(), "test wallet", store, encryptor)
require.NoError(t, err)
accounts := 0
for range wallet.Accounts(context.Background()) {
accounts++
}
require.Equal(t, numAccounts, accounts)
}