2020-10-15 22:31:52 +00:00
|
|
|
package accounts
|
2020-07-22 02:41:39 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"flag"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-07-27 14:03:30 +00:00
|
|
|
"strconv"
|
2020-07-22 02:41:39 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2020-10-15 22:31:52 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/accounts/wallet"
|
2020-07-22 02:41:39 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/flags"
|
2020-10-15 22:31:52 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/keymanager"
|
|
|
|
"github.com/prysmaticlabs/prysm/validator/keymanager/derived"
|
2020-07-22 02:41:39 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
2020-09-30 14:13:37 +00:00
|
|
|
type recoverCfgStruct struct {
|
|
|
|
walletDir string
|
|
|
|
passwordFilePath string
|
|
|
|
mnemonicFilePath string
|
|
|
|
numAccounts int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupRecoverCfg(t *testing.T) *recoverCfgStruct {
|
2020-07-22 02:41:39 +00:00
|
|
|
testDir := testutil.TempDir()
|
|
|
|
walletDir := filepath.Join(testDir, walletDirName)
|
|
|
|
passwordFilePath := filepath.Join(testDir, passwordFileName)
|
|
|
|
require.NoError(t, ioutil.WriteFile(passwordFilePath, []byte(password), os.ModePerm))
|
|
|
|
mnemonicFilePath := filepath.Join(testDir, mnemonicFileName)
|
|
|
|
require.NoError(t, ioutil.WriteFile(mnemonicFilePath, []byte(mnemonic), os.ModePerm))
|
|
|
|
|
2020-09-30 14:13:37 +00:00
|
|
|
t.Cleanup(func() {
|
|
|
|
assert.NoError(t, os.RemoveAll(walletDir))
|
|
|
|
assert.NoError(t, os.Remove(passwordFilePath))
|
|
|
|
assert.NoError(t, os.Remove(mnemonicFilePath))
|
|
|
|
})
|
|
|
|
|
|
|
|
return &recoverCfgStruct{
|
|
|
|
walletDir: walletDir,
|
|
|
|
passwordFilePath: passwordFilePath,
|
|
|
|
mnemonicFilePath: mnemonicFilePath,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createRecoverCliCtx(t *testing.T, cfg *recoverCfgStruct) *cli.Context {
|
2020-07-22 02:41:39 +00:00
|
|
|
app := cli.App{}
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
2020-09-30 14:13:37 +00:00
|
|
|
set.String(flags.WalletDirFlag.Name, cfg.walletDir, "")
|
|
|
|
set.String(flags.WalletPasswordFileFlag.Name, cfg.passwordFilePath, "")
|
2020-10-15 22:31:52 +00:00
|
|
|
set.String(flags.KeymanagerKindFlag.Name, keymanager.Derived.String(), "")
|
2020-09-30 14:13:37 +00:00
|
|
|
set.String(flags.MnemonicFileFlag.Name, cfg.mnemonicFilePath, "")
|
|
|
|
set.Int64(flags.NumAccountsFlag.Name, cfg.numAccounts, "")
|
|
|
|
assert.NoError(t, set.Set(flags.WalletDirFlag.Name, cfg.walletDir))
|
|
|
|
assert.NoError(t, set.Set(flags.WalletPasswordFileFlag.Name, cfg.passwordFilePath))
|
2020-10-15 22:31:52 +00:00
|
|
|
assert.NoError(t, set.Set(flags.KeymanagerKindFlag.Name, keymanager.Derived.String()))
|
2020-09-30 14:13:37 +00:00
|
|
|
assert.NoError(t, set.Set(flags.MnemonicFileFlag.Name, cfg.mnemonicFilePath))
|
|
|
|
assert.NoError(t, set.Set(flags.NumAccountsFlag.Name, strconv.Itoa(int(cfg.numAccounts))))
|
|
|
|
return cli.NewContext(&app, set, nil)
|
|
|
|
}
|
2020-07-22 02:41:39 +00:00
|
|
|
|
2020-09-30 14:13:37 +00:00
|
|
|
func TestRecoverDerivedWallet(t *testing.T) {
|
|
|
|
cfg := setupRecoverCfg(t)
|
|
|
|
cfg.numAccounts = 4
|
|
|
|
cliCtx := createRecoverCliCtx(t, cfg)
|
2020-08-31 19:46:45 +00:00
|
|
|
require.NoError(t, RecoverWalletCli(cliCtx))
|
2020-07-22 02:41:39 +00:00
|
|
|
|
|
|
|
ctx := context.Background()
|
2020-09-17 01:34:42 +00:00
|
|
|
w, err := wallet.OpenWallet(cliCtx.Context, &wallet.Config{
|
2020-09-30 14:13:37 +00:00
|
|
|
WalletDir: cfg.walletDir,
|
2020-09-22 14:49:07 +00:00
|
|
|
WalletPassword: password,
|
2020-08-31 19:46:45 +00:00
|
|
|
})
|
2020-07-22 02:41:39 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-09-17 01:34:42 +00:00
|
|
|
encoded, err := w.ReadKeymanagerConfigFromDisk(ctx)
|
2020-07-22 02:41:39 +00:00
|
|
|
assert.NoError(t, err)
|
2020-09-30 14:13:37 +00:00
|
|
|
walletCfg, err := derived.UnmarshalOptionsFile(encoded)
|
2020-07-22 02:41:39 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
// We assert the created configuration was as desired.
|
2020-08-31 19:46:45 +00:00
|
|
|
wantCfg := derived.DefaultKeymanagerOpts()
|
2020-09-30 14:13:37 +00:00
|
|
|
assert.DeepEqual(t, wantCfg, walletCfg)
|
2020-07-27 14:03:30 +00:00
|
|
|
|
2020-09-17 01:34:42 +00:00
|
|
|
keymanager, err := w.InitializeKeymanager(cliCtx.Context, true)
|
2020-07-27 14:03:30 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
km, ok := keymanager.(*derived.Keymanager)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not a derived keymanager")
|
|
|
|
}
|
|
|
|
names, err := km.ValidatingAccountNames(ctx)
|
|
|
|
assert.NoError(t, err)
|
2020-09-30 14:13:37 +00:00
|
|
|
require.Equal(t, len(names), int(cfg.numAccounts))
|
2020-07-22 02:41:39 +00:00
|
|
|
}
|
2020-09-26 10:13:56 +00:00
|
|
|
|
|
|
|
// TestRecoverDerivedWallet_OneAccount is a test for regression in cases where the number of accounts recovered is 1
|
|
|
|
func TestRecoverDerivedWallet_OneAccount(t *testing.T) {
|
2020-09-30 14:13:37 +00:00
|
|
|
cfg := setupRecoverCfg(t)
|
|
|
|
cfg.numAccounts = 1
|
|
|
|
cliCtx := createRecoverCliCtx(t, cfg)
|
2020-09-26 10:13:56 +00:00
|
|
|
require.NoError(t, RecoverWalletCli(cliCtx))
|
|
|
|
|
|
|
|
_, err := wallet.OpenWallet(cliCtx.Context, &wallet.Config{
|
2020-09-30 14:13:37 +00:00
|
|
|
WalletDir: cfg.walletDir,
|
2020-09-26 10:13:56 +00:00
|
|
|
WalletPassword: password,
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2020-09-30 14:13:37 +00:00
|
|
|
|
|
|
|
func TestRecoverDerivedWallet_AlreadyExists(t *testing.T) {
|
|
|
|
cfg := setupRecoverCfg(t)
|
|
|
|
cfg.numAccounts = 4
|
|
|
|
cliCtx := createRecoverCliCtx(t, cfg)
|
|
|
|
require.NoError(t, RecoverWalletCli(cliCtx))
|
|
|
|
|
|
|
|
// Trying to recover an HD wallet into a directory that already exists should give an error
|
|
|
|
require.ErrorContains(t, "a wallet already exists at this location", RecoverWalletCli(cliCtx))
|
|
|
|
}
|