From b09b1f3fa5851f90c7757aac5695450d289cd140 Mon Sep 17 00:00:00 2001 From: Raul Jordan Date: Tue, 22 Sep 2020 14:02:13 -0500 Subject: [PATCH] Disallow Creation of Multiple Wallets at Wallet Path (#7290) * disallow multiple wallet creation * imports * Merge branch 'master' into disallow-multiple-wallets * resolve conflicting test * Merge branch 'disallow-multiple-wallets' of github.com:prysmaticlabs/prysm into disallow-multiple-wallets * test passes * Merge refs/heads/master into disallow-multiple-wallets * Merge refs/heads/master into disallow-multiple-wallets * Merge refs/heads/master into disallow-multiple-wallets * Merge refs/heads/master into disallow-multiple-wallets * Merge refs/heads/master into disallow-multiple-wallets * Merge refs/heads/master into disallow-multiple-wallets * radek feedback * Merge branch 'master' into disallow-multiple-wallets --- validator/accounts/v2/wallet/wallet.go | 4 ++-- validator/accounts/v2/wallet_create.go | 10 +++------- validator/accounts/v2/wallet_create_test.go | 7 +++---- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/validator/accounts/v2/wallet/wallet.go b/validator/accounts/v2/wallet/wallet.go index 1d38955cf9..596b027e79 100644 --- a/validator/accounts/v2/wallet/wallet.go +++ b/validator/accounts/v2/wallet/wallet.go @@ -103,11 +103,11 @@ func NewWallet(cfg *Config) *Wallet { // Exists check if a wallet at the specified directory // exists and has valid information in it. func Exists(walletDir string) error { - ok, err := fileutil.HasDir(walletDir) + dirExists, err := fileutil.HasDir(walletDir) if err != nil { return errors.Wrap(err, "could not parse wallet directory") } - if ok { + if dirExists { isEmptyWallet, err := isEmptyWallet(walletDir) if err != nil { return errors.Wrap(err, "could not check if wallet has files") diff --git a/validator/accounts/v2/wallet_create.go b/validator/accounts/v2/wallet_create.go index 29202e51c8..3c336f658c 100644 --- a/validator/accounts/v2/wallet_create.go +++ b/validator/accounts/v2/wallet_create.go @@ -3,7 +3,6 @@ package v2 import ( "context" "fmt" - "path/filepath" "github.com/manifoldco/promptui" "github.com/pkg/errors" @@ -40,15 +39,12 @@ func CreateAndSaveWalletCli(cliCtx *cli.Context) (*wallet.Wallet, error) { } dir := createWalletConfig.WalletCfg.WalletDir - kmKind := createWalletConfig.WalletCfg.KeymanagerKind - accountsPath := filepath.Join(dir, kmKind.String()) - ok, err := fileutil.HasDir(accountsPath) + dirExists, err := fileutil.HasDir(dir) if err != nil { return nil, err } - // This wallet type already exists - if ok { - return nil, errors.New("a wallet of this type already exists at this location. Please input an" + + if dirExists { + return nil, errors.New("a wallet already exists at this location. Please input an" + " alternative location for the new wallet or remove the current wallet") } w, err := CreateWalletWithKeymanager(cliCtx.Context, createWalletConfig) diff --git a/validator/accounts/v2/wallet_create_test.go b/validator/accounts/v2/wallet_create_test.go index 84ecd9cb32..3ccc1c03aa 100644 --- a/validator/accounts/v2/wallet_create_test.go +++ b/validator/accounts/v2/wallet_create_test.go @@ -233,8 +233,7 @@ func TestCreateWallet_WalletAlreadyExists(t *testing.T) { // We attempt to create another wallet of the same type at the same location. We expect an error. _, err = CreateAndSaveWalletCli(cliCtx) - require.ErrorContains(t, "a wallet of this type already exists at this location. Please input an"+ - " alternative location for the new wallet or remove the current wallet", err) + require.ErrorContains(t, "already exists", err) cliCtx = setupWalletCtx(t, &testWalletConfig{ walletDir: walletDir, @@ -243,9 +242,9 @@ func TestCreateWallet_WalletAlreadyExists(t *testing.T) { keymanagerKind: v2keymanager.Direct, }) - // We attempt to create another wallet of different type at the same location. We don't expect an error. + // We attempt to create another wallet of different type at the same location. We expect an error. _, err = CreateAndSaveWalletCli(cliCtx) - require.NoError(t, err) + require.ErrorContains(t, "already exists", err) } // TestCorrectPassphrase_Derived makes sure the wallet created uses the provided passphrase