mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-09 15:37:56 -05:00
Improve error message when creating a wallet in place of one that already exists (#7268)
* Tell user if content already exists at wallet create destination * Added test * changed wording * capitalization * Merge branch 'master' into dv8-i7264 * Merge branch 'master' into dv8-i7264 * Address PR comments Co-authored-by: Radosław Kapka <rkapka@wp.pl> * Address PR comments Co-authored-by: Radosław Kapka <rkapka@wp.pl> * Address PR comments Co-authored-by: Radosław Kapka <rkapka@wp.pl> * Address PR comments Co-authored-by: Radosław Kapka <rkapka@wp.pl> * Address PR comments Co-authored-by: Radosław Kapka <rkapka@wp.pl> * Minor fix * Improve test by addressing PR feedback. Change variable name * address PR comments
This commit is contained in:
@@ -3,9 +3,11 @@ package v2
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/manifoldco/promptui"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/prysmaticlabs/prysm/shared/fileutil"
|
||||
"github.com/prysmaticlabs/prysm/shared/promptutil"
|
||||
"github.com/prysmaticlabs/prysm/validator/accounts/v2/prompt"
|
||||
"github.com/prysmaticlabs/prysm/validator/accounts/v2/wallet"
|
||||
@@ -36,6 +38,19 @@ func CreateAndSaveWalletCli(cliCtx *cli.Context) (*wallet.Wallet, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dir := createWalletConfig.WalletCfg.WalletDir
|
||||
kmKind := createWalletConfig.WalletCfg.KeymanagerKind
|
||||
accountsPath := filepath.Join(dir, kmKind.String())
|
||||
ok, err := fileutil.HasDir(accountsPath)
|
||||
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" +
|
||||
" alternative location for the new wallet or remove the current wallet")
|
||||
}
|
||||
return CreateWalletWithKeymanager(cliCtx.Context, createWalletConfig)
|
||||
}
|
||||
|
||||
|
||||
@@ -217,6 +217,37 @@ func TestCreateWallet_Derived(t *testing.T) {
|
||||
assert.DeepEqual(t, derived.DefaultKeymanagerOpts(), cfg)
|
||||
}
|
||||
|
||||
// TestCreateWallet_WalletAlreadyExists checks for expected error if trying to create a wallet when there is one already.
|
||||
func TestCreateWallet_WalletAlreadyExists(t *testing.T) {
|
||||
walletDir, passwordsDir, passwordFile := setupWalletAndPasswordsDir(t)
|
||||
cliCtx := setupWalletCtx(t, &testWalletConfig{
|
||||
walletDir: walletDir,
|
||||
passwordsDir: passwordsDir,
|
||||
walletPasswordFile: passwordFile,
|
||||
keymanagerKind: v2keymanager.Derived,
|
||||
})
|
||||
|
||||
// We attempt to create the wallet.
|
||||
_, err := CreateAndSaveWalletCli(cliCtx)
|
||||
require.NoError(t, err)
|
||||
|
||||
// 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)
|
||||
|
||||
cliCtx = setupWalletCtx(t, &testWalletConfig{
|
||||
walletDir: walletDir,
|
||||
passwordsDir: passwordsDir,
|
||||
walletPasswordFile: passwordFile,
|
||||
keymanagerKind: v2keymanager.Direct,
|
||||
})
|
||||
|
||||
// We attempt to create another wallet of different type at the same location. We don't expect an error.
|
||||
_, err = CreateAndSaveWalletCli(cliCtx)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
// TestCorrectPassphrase_Derived makes sure the wallet created uses the provided passphrase
|
||||
func TestCorrectPassphrase_Derived(t *testing.T) {
|
||||
walletDir, _, passwordFile := setupWalletAndPasswordsDir(t)
|
||||
|
||||
Reference in New Issue
Block a user