From 19e4c679d304aec64e34328c40ab2be450e502a3 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Fri, 29 Apr 2022 18:22:33 +0300 Subject: [PATCH 1/2] cli: unify password-related prompts Close #2419. --- cli/wallet/validator.go | 2 +- cli/wallet/wallet.go | 26 +++++++++++++++++++------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/cli/wallet/validator.go b/cli/wallet/validator.go index 537d52211..3c0ed4d0e 100644 --- a/cli/wallet/validator.go +++ b/cli/wallet/validator.go @@ -204,7 +204,7 @@ func getDecryptedAccount(ctx *cli.Context, wall *wallet.Wallet, addr util.Uint16 return nil, fmt.Errorf("can't find account for the address: %s", address.Uint160ToString(addr)) } - if pass, err := input.ReadPassword("Password > "); err != nil { + if pass, err := input.ReadPassword(EnterPasswordPrompt); err != nil { fmt.Println("Error reading password", err) return nil, err } else if err := acc.Decrypt(pass, wall.Scrypt); err != nil { diff --git a/cli/wallet/wallet.go b/cli/wallet/wallet.go index 391326de1..8fabe443b 100644 --- a/cli/wallet/wallet.go +++ b/cli/wallet/wallet.go @@ -22,6 +22,18 @@ import ( "github.com/urfave/cli" ) +const ( + // EnterPasswordPrompt is a prompt used to ask the user for a password. + EnterPasswordPrompt = "Enter password > " + // EnterNewPasswordPrompt is a prompt used to ask the user for a password on + // account creation. + EnterNewPasswordPrompt = "Enter new password > " + // EnterOldPasswordPrompt is a prompt used to ask the user for an old password. + EnterOldPasswordPrompt = "Enter old password > " + // ConfirmPasswordPrompt is a prompt used to confirm the password. + ConfirmPasswordPrompt = "Confirm password > " +) + var ( errNoPath = errors.New("wallet path is mandatory and should be passed using (--wallet, -w) flags") errPhraseMismatch = errors.New("the entered pass-phrases do not match. Maybe you have misspelled them") @@ -315,7 +327,7 @@ func changePassword(ctx *cli.Context) error { } } - oldPass, err := input.ReadPassword("Enter password > ") + oldPass, err := input.ReadPassword(EnterOldPasswordPrompt) if err != nil { return cli.NewExitError(fmt.Errorf("Error reading old password: %w", err), 1) } @@ -368,7 +380,7 @@ func convertWallet(ctx *cli.Context) error { newWallet.Scrypt = wall.Scrypt for _, acc := range wall.Accounts { - pass, err := input.ReadPassword(fmt.Sprintf("Enter passphrase for account %s (label '%s') > ", acc.Address, acc.Label)) + pass, err := input.ReadPassword(fmt.Sprintf("Enter password for account %s (label '%s') > ", acc.Address, acc.Label)) if err != nil { return cli.NewExitError(fmt.Errorf("Error reading password: %w", err), 1) } @@ -438,7 +450,7 @@ loop: for _, wif := range wifs { if decrypt { - pass, err := input.ReadPassword("Enter password > ") + pass, err := input.ReadPassword(EnterPasswordPrompt) if err != nil { return cli.NewExitError(fmt.Errorf("Error reading password: %w", err), 1) } @@ -634,7 +646,7 @@ func dumpWallet(ctx *cli.Context) error { return cli.NewExitError(err, 1) } if ctx.Bool("decrypt") { - pass, err := input.ReadPassword("Enter wallet password > ") + pass, err := input.ReadPassword(EnterPasswordPrompt) if err != nil { return cli.NewExitError(fmt.Errorf("Error reading password: %w", err), 1) } @@ -734,11 +746,11 @@ func readAccountInfo() (string, string, error) { } func readNewPassword() (string, error) { - phrase, err := input.ReadPassword("Enter passphrase > ") + phrase, err := input.ReadPassword(EnterNewPasswordPrompt) if err != nil { return "", fmt.Errorf("Error reading password: %w", err) } - phraseCheck, err := input.ReadPassword("Confirm passphrase > ") + phraseCheck, err := input.ReadPassword(ConfirmPasswordPrompt) if err != nil { return "", fmt.Errorf("Error reading password: %w", err) } @@ -785,7 +797,7 @@ func newAccountFromWIF(w io.Writer, wif string, scrypt keys.ScryptParams) (*wall // note: NEP2 strings always have length of 58 even though // base58 strings can have different lengths even if slice lengths are equal if len(wif) == 58 { - pass, err := input.ReadPassword("Enter password > ") + pass, err := input.ReadPassword(EnterPasswordPrompt) if err != nil { return nil, fmt.Errorf("Error reading password: %w", err) } From 79f377d0f0c97d25fcc2974cee5c79aed153d56c Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Fri, 29 Apr 2022 18:34:42 +0300 Subject: [PATCH 2/2] cli: ensure wallet is non-empty while changing password Close #2429. --- cli/wallet/wallet.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cli/wallet/wallet.go b/cli/wallet/wallet.go index 8fabe443b..bbf044e6a 100644 --- a/cli/wallet/wallet.go +++ b/cli/wallet/wallet.go @@ -318,6 +318,9 @@ func changePassword(ctx *cli.Context) error { if err != nil { return cli.NewExitError(err, 1) } + if len(wall.Accounts) == 0 { + return cli.NewExitError("wallet has no accounts", 1) + } addrFlag := ctx.Generic("address").(*flags.Address) if addrFlag.IsSet { // Check for account presence first before asking for password.