Create a new wallet on accounts import only when necessary (#8801)

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
This commit is contained in:
Radosław Kapka
2021-04-24 10:26:41 +02:00
committed by GitHub
parent 3d96e3c142
commit f1d7c0f2c9
2 changed files with 36 additions and 2 deletions

View File

@@ -83,6 +83,38 @@ type ImportAccountsConfig struct {
// values necessary to run the function.
func ImportAccountsCli(cliCtx *cli.Context) error {
w, err := wallet.OpenWalletOrElseCli(cliCtx, func(cliCtx *cli.Context) (*wallet.Wallet, error) {
walletDir, err := prompt.InputDirectory(cliCtx, prompt.WalletDirPromptText, flags.WalletDirFlag)
if err != nil {
return nil, err
}
exists, err := wallet.Exists(walletDir)
if err != nil {
return nil, errors.Wrap(err, wallet.CheckExistsErrMsg)
}
if exists {
isValid, err := wallet.IsValid(walletDir)
if err != nil {
return nil, errors.Wrap(err, wallet.CheckValidityErrMsg)
}
if !isValid {
return nil, errors.New(wallet.InvalidWalletErrMsg)
}
walletPassword, err := wallet.InputPassword(
cliCtx,
flags.WalletPasswordFileFlag,
wallet.PasswordPromptText,
false, /* Do not confirm password */
wallet.ValidateExistingPass,
)
if err != nil {
return nil, err
}
return wallet.OpenWallet(cliCtx.Context, &wallet.Config{
WalletDir: walletDir,
WalletPassword: walletPassword,
})
}
cfg, err := extractWalletCreationConfigFromCli(cliCtx, keymanager.Imported)
if err != nil {
return nil, err

View File

@@ -177,7 +177,7 @@ func OpenWalletOrElseCli(cliCtx *cli.Context, otherwise func(cliCtx *cli.Context
if err != nil {
return nil, err
}
walletPassword, err := inputPassword(
walletPassword, err := InputPassword(
cliCtx,
flags.WalletPasswordFileFlag,
PasswordPromptText,
@@ -414,7 +414,9 @@ func readKeymanagerKindFromWalletPath(walletPath string) (keymanager.Kind, error
return 0, errors.New("no keymanager folder (imported, remote, derived) found in wallet path")
}
func inputPassword(
// InputPassword prompts for a password and optionally for password confirmation.
// The password is validated according to custom rules.
func InputPassword(
cliCtx *cli.Context,
passwordFileFlag *cli.StringFlag,
promptText string,