cli: move password getting to a separate function

Also do not ignore errors from `terminal.ReadPassword`.
This commit is contained in:
Evgenii Stratonikov 2020-02-20 13:34:27 +03:00
parent 5a727cabf8
commit a71c2c4bfd

View file

@ -69,14 +69,13 @@ func dumpWallet(ctx *cli.Context) error {
return cli.NewExitError(err, 1) return cli.NewExitError(err, 1)
} }
if ctx.Bool("decrypt") { if ctx.Bool("decrypt") {
fmt.Print("Wallet password: ") pass, err := readPassword("Enter wallet password > ")
pass, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil { if err != nil {
return cli.NewExitError(err, 1) return cli.NewExitError(err, 1)
} }
for i := range wall.Accounts { for i := range wall.Accounts {
// Just testing the decryption here. // Just testing the decryption here.
err := wall.Accounts[i].Decrypt(string(pass)) err := wall.Accounts[i].Decrypt(pass)
if err != nil { if err != nil {
return cli.NewExitError(err, 1) return cli.NewExitError(err, 1)
} }
@ -111,33 +110,36 @@ func createWallet(ctx *cli.Context) error {
} }
func createAccount(ctx *cli.Context, wall *wallet.Wallet) error { func createAccount(ctx *cli.Context, wall *wallet.Wallet) error {
var (
rawName,
rawPhrase,
rawPhraseCheck []byte
)
buf := bufio.NewReader(os.Stdin) buf := bufio.NewReader(os.Stdin)
fmt.Print("Enter the name of the account > ") fmt.Print("Enter the name of the account > ")
rawName, _ = buf.ReadBytes('\n') rawName, _ := buf.ReadBytes('\n')
fmt.Print("Enter passphrase > ") phrase, err := readPassword("Enter passphrase > ")
rawPhrase, _ = terminal.ReadPassword(int(syscall.Stdin)) if err != nil {
fmt.Print("\nConfirm passphrase > ") return cli.NewExitError(err, 1)
rawPhraseCheck, _ = terminal.ReadPassword(int(syscall.Stdin)) }
phraseCheck, err := readPassword("Confirm passphrase > ")
// Clean data if err != nil {
var ( return cli.NewExitError(err, 1)
name = strings.TrimRight(string(rawName), "\n") }
phrase = strings.TrimRight(string(rawPhrase), "\n")
phraseCheck = strings.TrimRight(string(rawPhraseCheck), "\n")
)
if phrase != phraseCheck { if phrase != phraseCheck {
return errPhraseMismatch return errPhraseMismatch
} }
name := strings.TrimRight(string(rawName), "\n")
return wall.CreateAccount(name, phrase) return wall.CreateAccount(name, phrase)
} }
func readPassword(prompt string) (string, error) {
fmt.Print(prompt)
rawPass, err := terminal.ReadPassword(syscall.Stdin)
fmt.Println()
if err != nil {
return "", err
}
return strings.TrimRight(string(rawPass), "\n"), nil
}
func fmtPrintWallet(wall *wallet.Wallet) { func fmtPrintWallet(wall *wallet.Wallet) {
b, _ := wall.JSON() b, _ := wall.JSON()
fmt.Println("") fmt.Println("")