2020-06-27 15:48:36 +00:00
|
|
|
package v1_test
|
2020-03-13 12:26:10 +00:00
|
|
|
|
|
|
|
import (
|
2020-07-29 23:54:23 +00:00
|
|
|
"context"
|
2020-03-13 12:26:10 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2020-07-22 19:42:43 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2020-06-27 15:48:36 +00:00
|
|
|
keymanager "github.com/prysmaticlabs/prysm/validator/keymanager/v1"
|
2020-03-13 12:26:10 +00:00
|
|
|
keystorev4 "github.com/wealdtech/go-eth2-wallet-encryptor-keystorev4"
|
2020-07-29 23:54:23 +00:00
|
|
|
nd "github.com/wealdtech/go-eth2-wallet-nd/v2"
|
2020-03-13 12:26:10 +00:00
|
|
|
filesystem "github.com/wealdtech/go-eth2-wallet-store-filesystem"
|
2020-07-29 23:54:23 +00:00
|
|
|
e2wtypes "github.com/wealdtech/go-eth2-wallet-types/v2"
|
2020-03-13 12:26:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func SetupWallet(t *testing.T) string {
|
|
|
|
path, err := ioutil.TempDir("", "")
|
2020-07-22 19:42:43 +00:00
|
|
|
require.NoError(t, err)
|
2020-03-13 12:26:10 +00:00
|
|
|
store := filesystem.New(filesystem.WithLocation(path))
|
|
|
|
encryptor := keystorev4.New()
|
|
|
|
|
|
|
|
// Create wallets with keys.
|
2020-07-29 23:54:23 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
w1, err := nd.CreateWallet(ctx, "Wallet 1", store, encryptor)
|
|
|
|
unlocker, ok := w1.(e2wtypes.WalletLocker)
|
|
|
|
require.Equal(t, true, ok)
|
|
|
|
require.NoError(t, unlocker.Unlock(ctx, nil))
|
|
|
|
creator, ok := w1.(e2wtypes.WalletAccountCreator)
|
|
|
|
require.Equal(t, true, ok)
|
2020-07-22 19:42:43 +00:00
|
|
|
require.NoError(t, err, "Failed to create wallet")
|
|
|
|
require.NoError(t, err, "Failed to unlock wallet")
|
2020-07-29 23:54:23 +00:00
|
|
|
_, err = creator.CreateAccount(ctx, "Account 1", []byte("foo"))
|
2020-07-22 19:42:43 +00:00
|
|
|
require.NoError(t, err, "Failed to create account 1")
|
2020-07-29 23:54:23 +00:00
|
|
|
_, err = creator.CreateAccount(ctx, "Account 2", []byte("bar"))
|
2020-07-22 19:42:43 +00:00
|
|
|
require.NoError(t, err, "Failed to create account 2")
|
2020-03-13 12:26:10 +00:00
|
|
|
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
|
|
|
func wallet(t *testing.T, opts string) keymanager.KeyManager {
|
|
|
|
km, _, err := keymanager.NewWallet(opts)
|
2020-07-22 19:42:43 +00:00
|
|
|
require.NoError(t, err)
|
2020-03-13 12:26:10 +00:00
|
|
|
return km
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMultiplePassphrases(t *testing.T) {
|
|
|
|
path := SetupWallet(t)
|
2020-04-14 16:41:09 +00:00
|
|
|
defer func() {
|
|
|
|
if err := os.RemoveAll(path); err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
}
|
|
|
|
}()
|
2020-03-13 12:26:10 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
wallet keymanager.KeyManager
|
|
|
|
accounts int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Neither",
|
|
|
|
wallet: wallet(t, fmt.Sprintf(`{"location":%q,"accounts":["Wallet 1"],"passphrases":["neither"]}`, path)),
|
|
|
|
accounts: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Foo",
|
|
|
|
wallet: wallet(t, fmt.Sprintf(`{"location":%q,"accounts":["Wallet 1"],"passphrases":["foo"]}`, path)),
|
|
|
|
accounts: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Bar",
|
|
|
|
wallet: wallet(t, fmt.Sprintf(`{"location":%q,"accounts":["Wallet 1"],"passphrases":["bar"]}`, path)),
|
|
|
|
accounts: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Both",
|
|
|
|
wallet: wallet(t, fmt.Sprintf(`{"location":%q,"accounts":["Wallet 1"],"passphrases":["foo","bar"]}`, path)),
|
|
|
|
accounts: 2,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
keys, err := test.wallet.FetchValidatingKeys()
|
2020-07-22 19:42:43 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, test.accounts, len(keys), "Found %d keys", len(keys))
|
2020-03-13 12:26:10 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-05-18 22:25:36 +00:00
|
|
|
|
|
|
|
func TestEnvPassphrases(t *testing.T) {
|
|
|
|
path := SetupWallet(t)
|
|
|
|
defer func() {
|
|
|
|
if err := os.RemoveAll(path); err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-07-22 19:42:43 +00:00
|
|
|
err := os.Setenv("TESTENVPASSPHRASES_NEITHER", "neither")
|
|
|
|
require.NoError(t, err, "Error setting environment variable TESTENVPASSPHRASES_NEITHER")
|
2020-05-18 22:25:36 +00:00
|
|
|
defer func() {
|
2020-07-22 19:42:43 +00:00
|
|
|
err := os.Unsetenv("TESTENVPASSPHRASES_NEITHER")
|
|
|
|
require.NoError(t, err, "Error unsetting environment variable TESTENVPASSPHRASES_NEITHER")
|
2020-05-18 22:25:36 +00:00
|
|
|
}()
|
2020-07-22 19:42:43 +00:00
|
|
|
err = os.Setenv("TESTENVPASSPHRASES_FOO", "foo")
|
|
|
|
require.NoError(t, err, "Error setting environment variable TESTENVPASSPHRASES_FOO")
|
2020-05-18 22:25:36 +00:00
|
|
|
defer func() {
|
2020-07-22 19:42:43 +00:00
|
|
|
err := os.Unsetenv("TESTENVPASSPHRASES_FOO")
|
|
|
|
require.NoError(t, err, "Error unsetting environment variable TESTENVPASSPHRASES_FOO")
|
2020-05-18 22:25:36 +00:00
|
|
|
}()
|
2020-07-22 19:42:43 +00:00
|
|
|
err = os.Setenv("TESTENVPASSPHRASES_BAR", "bar")
|
|
|
|
require.NoError(t, err, "Error setting environment variable TESTENVPASSPHRASES_BAR")
|
2020-05-18 22:25:36 +00:00
|
|
|
defer func() {
|
2020-07-22 19:42:43 +00:00
|
|
|
err := os.Unsetenv("TESTENVPASSPHRASES_BAR")
|
|
|
|
require.NoError(t, err, "Error unsetting environment variable TESTENVPASSPHRASES_BAR")
|
2020-05-18 22:25:36 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
wallet keymanager.KeyManager
|
|
|
|
accounts int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Neither",
|
|
|
|
wallet: wallet(t, fmt.Sprintf(`{"location":%q,"accounts":["Wallet 1"],"passphrases":["$TESTENVPASSPHRASES_NEITHER"]}`, path)),
|
|
|
|
accounts: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Foo",
|
|
|
|
wallet: wallet(t, fmt.Sprintf(`{"location":%q,"accounts":["Wallet 1"],"passphrases":["$TESTENVPASSPHRASES_FOO"]}`, path)),
|
|
|
|
accounts: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Bar",
|
|
|
|
wallet: wallet(t, fmt.Sprintf(`{"location":%q,"accounts":["Wallet 1"],"passphrases":["$TESTENVPASSPHRASES_BAR"]}`, path)),
|
|
|
|
accounts: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Both",
|
|
|
|
wallet: wallet(t, fmt.Sprintf(`{"location":%q,"accounts":["Wallet 1"],"passphrases":["$TESTENVPASSPHRASES_FOO","$TESTENVPASSPHRASES_BAR"]}`, path)),
|
|
|
|
accounts: 2,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
keys, err := test.wallet.FetchValidatingKeys()
|
2020-07-22 19:42:43 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, test.accounts, len(keys), "Found %d keys", len(keys))
|
2020-05-18 22:25:36 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|