2020-10-16 18:45:14 +00:00
|
|
|
package imported
|
2020-08-31 23:38:20 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-11-20 22:04:59 +00:00
|
|
|
"encoding/json"
|
2020-08-31 23:38:20 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-11-20 22:04:59 +00:00
|
|
|
"github.com/google/uuid"
|
2021-09-15 22:55:11 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/crypto/bls"
|
2020-08-31 23:38:20 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/event"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2020-10-15 22:31:52 +00:00
|
|
|
mock "github.com/prysmaticlabs/prysm/validator/accounts/testing"
|
2020-11-20 22:04:59 +00:00
|
|
|
keystorev4 "github.com/wealdtech/go-eth2-wallet-encryptor-keystorev4"
|
2020-08-31 23:38:20 +00:00
|
|
|
)
|
|
|
|
|
2020-11-20 22:04:59 +00:00
|
|
|
func TestImportedKeymanager_reloadAccountsFromKeystore_MismatchedNumKeys(t *testing.T) {
|
|
|
|
password := "Passw03rdz293**%#2"
|
|
|
|
wallet := &mock.Wallet{
|
|
|
|
Files: make(map[string]map[string][]byte),
|
|
|
|
AccountPasswords: make(map[string]string),
|
|
|
|
WalletPassword: password,
|
|
|
|
}
|
|
|
|
dr := &Keymanager{
|
|
|
|
wallet: wallet,
|
|
|
|
}
|
|
|
|
accountsStore := &accountStore{
|
|
|
|
PrivateKeys: [][]byte{[]byte("hello")},
|
|
|
|
PublicKeys: [][]byte{[]byte("hi"), []byte("world")},
|
|
|
|
}
|
|
|
|
encodedStore, err := json.MarshalIndent(accountsStore, "", "\t")
|
|
|
|
require.NoError(t, err)
|
|
|
|
encryptor := keystorev4.New()
|
|
|
|
cryptoFields, err := encryptor.Encrypt(encodedStore, dr.wallet.Password())
|
|
|
|
require.NoError(t, err)
|
|
|
|
id, err := uuid.NewRandom()
|
|
|
|
require.NoError(t, err)
|
2021-01-12 16:52:01 +00:00
|
|
|
keystore := &AccountsKeystoreRepresentation{
|
2020-11-20 22:04:59 +00:00
|
|
|
Crypto: cryptoFields,
|
|
|
|
ID: id.String(),
|
|
|
|
Version: encryptor.Version(),
|
|
|
|
Name: encryptor.Name(),
|
|
|
|
}
|
|
|
|
err = dr.reloadAccountsFromKeystore(keystore)
|
|
|
|
assert.ErrorContains(t, "do not match", err)
|
|
|
|
}
|
|
|
|
|
2020-10-16 18:45:14 +00:00
|
|
|
func TestImportedKeymanager_reloadAccountsFromKeystore(t *testing.T) {
|
2020-08-31 23:38:20 +00:00
|
|
|
password := "Passw03rdz293**%#2"
|
|
|
|
wallet := &mock.Wallet{
|
|
|
|
Files: make(map[string]map[string][]byte),
|
|
|
|
AccountPasswords: make(map[string]string),
|
|
|
|
WalletPassword: password,
|
|
|
|
}
|
|
|
|
dr := &Keymanager{
|
|
|
|
wallet: wallet,
|
|
|
|
accountsChangedFeed: new(event.Feed),
|
|
|
|
}
|
|
|
|
|
|
|
|
numAccounts := 20
|
|
|
|
privKeys := make([][]byte, numAccounts)
|
|
|
|
pubKeys := make([][]byte, numAccounts)
|
|
|
|
for i := 0; i < numAccounts; i++ {
|
2020-10-30 19:06:33 +00:00
|
|
|
privKey, err := bls.RandKey()
|
|
|
|
require.NoError(t, err)
|
2020-08-31 23:38:20 +00:00
|
|
|
privKeys[i] = privKey.Marshal()
|
|
|
|
pubKeys[i] = privKey.PublicKey().Marshal()
|
|
|
|
}
|
|
|
|
|
2021-01-12 16:52:01 +00:00
|
|
|
accountsStore, err := dr.CreateAccountsKeystore(context.Background(), privKeys, pubKeys)
|
2020-08-31 23:38:20 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, dr.reloadAccountsFromKeystore(accountsStore))
|
|
|
|
|
2020-09-01 18:13:44 +00:00
|
|
|
// Check that the public keys were added to the public keys cache.
|
|
|
|
for i, keyBytes := range pubKeys {
|
2020-10-10 02:07:28 +00:00
|
|
|
require.Equal(t, bytesutil.ToBytes48(keyBytes), orderedPublicKeys[i])
|
2020-09-01 18:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the secret keys were added to the secret keys cache.
|
2020-10-10 02:07:28 +00:00
|
|
|
lock.RLock()
|
|
|
|
defer lock.RUnlock()
|
2020-09-01 18:13:44 +00:00
|
|
|
for i, keyBytes := range privKeys {
|
2020-10-10 02:07:28 +00:00
|
|
|
privKey, ok := secretKeysCache[bytesutil.ToBytes48(pubKeys[i])]
|
2020-08-31 23:38:20 +00:00
|
|
|
require.Equal(t, true, ok)
|
2020-09-01 18:13:44 +00:00
|
|
|
require.Equal(t, bytesutil.ToBytes48(keyBytes), bytesutil.ToBytes48(privKey.Marshal()))
|
2020-08-31 23:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the key was added to the global accounts store.
|
|
|
|
require.Equal(t, numAccounts, len(dr.accountsStore.PublicKeys))
|
|
|
|
require.Equal(t, numAccounts, len(dr.accountsStore.PrivateKeys))
|
|
|
|
assert.DeepEqual(t, dr.accountsStore.PublicKeys[0], pubKeys[0])
|
|
|
|
}
|