2020-07-22 02:41:39 +00:00
|
|
|
package v2
|
|
|
|
|
|
|
|
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-09-17 01:34:42 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/accounts/v2/wallet"
|
2020-07-22 02:41:39 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/flags"
|
|
|
|
v2keymanager "github.com/prysmaticlabs/prysm/validator/keymanager/v2"
|
|
|
|
"github.com/prysmaticlabs/prysm/validator/keymanager/v2/derived"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRecoverDerivedWallet(t *testing.T) {
|
|
|
|
testDir := testutil.TempDir()
|
|
|
|
walletDir := filepath.Join(testDir, walletDirName)
|
|
|
|
passwordsDir := filepath.Join(testDir, passwordDirName)
|
|
|
|
exportDir := filepath.Join(testDir, exportDirName)
|
|
|
|
defer func() {
|
|
|
|
assert.NoError(t, os.RemoveAll(walletDir))
|
|
|
|
assert.NoError(t, os.RemoveAll(passwordsDir))
|
|
|
|
assert.NoError(t, os.RemoveAll(exportDir))
|
|
|
|
}()
|
|
|
|
|
|
|
|
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-07-27 14:03:30 +00:00
|
|
|
numAccounts := int64(4)
|
2020-07-22 02:41:39 +00:00
|
|
|
app := cli.App{}
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.String(flags.WalletDirFlag.Name, walletDir, "")
|
2020-08-07 00:02:30 +00:00
|
|
|
set.String(flags.DeprecatedPasswordsDirFlag.Name, passwordsDir, "")
|
2020-07-29 01:20:13 +00:00
|
|
|
set.String(flags.WalletPasswordFileFlag.Name, passwordFilePath, "")
|
2020-07-22 04:49:04 +00:00
|
|
|
set.String(flags.KeymanagerKindFlag.Name, v2keymanager.Derived.String(), "")
|
2020-07-22 02:41:39 +00:00
|
|
|
set.String(flags.MnemonicFileFlag.Name, mnemonicFilePath, "")
|
2020-07-27 14:03:30 +00:00
|
|
|
set.Int64(flags.NumAccountsFlag.Name, numAccounts, "")
|
2020-07-22 02:41:39 +00:00
|
|
|
assert.NoError(t, set.Set(flags.WalletDirFlag.Name, walletDir))
|
2020-08-07 00:02:30 +00:00
|
|
|
assert.NoError(t, set.Set(flags.DeprecatedPasswordsDirFlag.Name, passwordsDir))
|
2020-07-29 01:20:13 +00:00
|
|
|
assert.NoError(t, set.Set(flags.WalletPasswordFileFlag.Name, passwordFilePath))
|
2020-07-22 04:49:04 +00:00
|
|
|
assert.NoError(t, set.Set(flags.KeymanagerKindFlag.Name, v2keymanager.Derived.String()))
|
2020-07-22 02:41:39 +00:00
|
|
|
assert.NoError(t, set.Set(flags.MnemonicFileFlag.Name, mnemonicFilePath))
|
2020-07-27 14:03:30 +00:00
|
|
|
assert.NoError(t, set.Set(flags.NumAccountsFlag.Name, strconv.Itoa(int(numAccounts))))
|
2020-07-22 02:41:39 +00:00
|
|
|
cliCtx := cli.NewContext(&app, set, nil)
|
|
|
|
|
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-08-31 19:46:45 +00:00
|
|
|
WalletDir: walletDir,
|
|
|
|
})
|
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-08-31 19:46:45 +00:00
|
|
|
cfg, 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-07-22 02:41:39 +00:00
|
|
|
assert.DeepEqual(t, wantCfg, cfg)
|
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)
|
|
|
|
require.Equal(t, len(names), int(numAccounts))
|
|
|
|
|
2020-07-22 02:41:39 +00:00
|
|
|
}
|