Merge pull request #2455 from nspcc-dev/pass

cli: unify password-related prompts
This commit is contained in:
Roman Khimov 2022-04-29 18:46:29 +03:00 committed by GitHub
commit 19eb976166
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 8 deletions

View file

@ -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 {

View file

@ -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")
@ -306,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.
@ -315,7 +330,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 +383,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 +453,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 +649,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 +749,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 +800,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)
}