Prompt for password on sendtx

This commit is contained in:
Ethan Frey 2018-02-23 10:40:10 +01:00 committed by rigelrozanski
parent 67e896dcaf
commit 00304dd094
8 changed files with 101 additions and 82 deletions

87
client/input.go Normal file
View File

@ -0,0 +1,87 @@
package client
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/bgentry/speakeasy"
"github.com/pkg/errors"
isatty "github.com/tendermint/vendor-bak/github.com/mattn/go-isatty"
)
// MinPassLength is the minimum acceptable password length
const MinPassLength = 8
// if we read from non-tty, we just need to init the buffer reader once,
// in case we try to read multiple passwords (eg. update)
var buf *bufio.Reader
// GetPassword will prompt for a password one-time (to sign a tx)
// It enforces the password length
func GetPassword(prompt string) (pass string, err error) {
if inputIsTty() {
pass, err = speakeasy.Ask(prompt)
} else {
pass, err = stdinPassword()
}
if err != nil {
return "", err
}
if len(pass) < MinPassLength {
return "", errors.Errorf("Password must be at least %d characters", MinPassLength)
}
return pass, nil
}
// GetSeed will request a seed phrase from stdin and trims off
// leading/trailing spaces
func GetSeed(prompt string) (seed string, err error) {
if inputIsTty() {
fmt.Println(prompt)
}
seed, err = stdinPassword()
seed = strings.TrimSpace(seed)
return
}
// GetCheckPassword will prompt for a password twice to verify they
// match (for creating a new password).
// It enforces the password length. Only parses password once if
// input is piped in.
func GetCheckPassword(prompt, prompt2 string) (string, error) {
// simple read on no-tty
if !inputIsTty() {
return GetPassword(prompt)
}
// TODO: own function???
pass, err := GetPassword(prompt)
if err != nil {
return "", err
}
pass2, err := GetPassword(prompt2)
if err != nil {
return "", err
}
if pass != pass2 {
return "", errors.New("Passphrases don't match")
}
return pass, nil
}
func inputIsTty() bool {
return isatty.IsTerminal(os.Stdin.Fd()) || isatty.IsCygwinTerminal(os.Stdin.Fd())
}
func stdinPassword() (string, error) {
if buf == nil {
buf = bufio.NewReader(os.Stdin)
}
pass, err := buf.ReadString('\n')
if err != nil {
return "", err
}
return strings.TrimSpace(pass), nil
}

View File

@ -69,14 +69,14 @@ func runAddCmd(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
pass, err = getCheckPassword("Enter a passphrase for your key:", "Repeat the passphrase:")
pass, err = client.GetCheckPassword("Enter a passphrase for your key:", "Repeat the passphrase:")
if err != nil {
return err
}
}
if viper.GetBool(flagRecover) {
seed, err := getPassword("Enter your recovery seed phrase:")
seed, err := client.GetSeed("Enter your recovery seed phrase:")
if err != nil {
return err
}

View File

@ -17,6 +17,7 @@ package keys
import (
"fmt"
"github.com/cosmos/cosmos-sdk/client"
"github.com/pkg/errors"
"github.com/spf13/cobra"
@ -37,7 +38,7 @@ func runDeleteCmd(cmd *cobra.Command, args []string) error {
}
name := args[0]
oldpass, err := getPassword("DANGER - enter password to permanently delete key:")
oldpass, err := client.GetPassword("DANGER - enter password to permanently delete key:")
if err != nil {
return err
}

View File

@ -17,6 +17,7 @@ package keys
import (
"fmt"
"github.com/cosmos/cosmos-sdk/client"
"github.com/pkg/errors"
"github.com/spf13/cobra"
@ -37,11 +38,11 @@ func runUpdateCmd(cmd *cobra.Command, args []string) error {
}
name := args[0]
oldpass, err := getPassword("Enter the current passphrase:")
oldpass, err := client.GetPassword("Enter the current passphrase:")
if err != nil {
return err
}
newpass, err := getCheckPassword("Enter the new passphrase:", "Repeat the new passphrase:")
newpass, err := client.GetCheckPassword("Enter the new passphrase:", "Repeat the new passphrase:")
if err != nil {
return err
}

View File

@ -1,14 +1,8 @@
package keys
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/bgentry/speakeasy"
isatty "github.com/mattn/go-isatty"
"github.com/pkg/errors"
"github.com/spf13/viper"
keys "github.com/tendermint/go-crypto/keys"
@ -17,9 +11,6 @@ import (
"github.com/cosmos/cosmos-sdk/client"
)
// MinPassLength is the minimum acceptable password length
const MinPassLength = 8
var (
// keybase is used to make GetKeyBase a singleton
keybase keys.Keybase
@ -38,70 +29,6 @@ func GetKeyBase() (keys.Keybase, error) {
return keybase, nil
}
// if we read from non-tty, we just need to init the buffer reader once,
// in case we try to read multiple passwords (eg. update)
var buf *bufio.Reader
func inputIsTty() bool {
return isatty.IsTerminal(os.Stdin.Fd()) || isatty.IsCygwinTerminal(os.Stdin.Fd())
}
func stdinPassword() (string, error) {
if buf == nil {
buf = bufio.NewReader(os.Stdin)
}
pass, err := buf.ReadString('\n')
if err != nil {
return "", err
}
return strings.TrimSpace(pass), nil
}
func getPassword(prompt string) (pass string, err error) {
if inputIsTty() {
pass, err = speakeasy.Ask(prompt)
} else {
pass, err = stdinPassword()
}
if err != nil {
return "", err
}
if len(pass) < MinPassLength {
return "", errors.Errorf("Password must be at least %d characters", MinPassLength)
}
return pass, nil
}
func getSeed(prompt string) (seed string, err error) {
if inputIsTty() {
fmt.Println(prompt)
}
seed, err = stdinPassword()
seed = strings.TrimSpace(seed)
return
}
func getCheckPassword(prompt, prompt2 string) (string, error) {
// simple read on no-tty
if !inputIsTty() {
return getPassword(prompt)
}
// TODO: own function???
pass, err := getPassword(prompt)
if err != nil {
return "", err
}
pass2, err := getPassword(prompt2)
if err != nil {
return "", err
}
if pass != pass2 {
return "", errors.New("Passphrases don't match")
}
return pass, nil
}
func printInfo(info keys.Info) {
switch viper.Get(cli.OutputFlag) {
case "text":

View File

@ -70,8 +70,7 @@ func getAccount(cmd *cobra.Command, args []string) error {
return err
}
// TODO: better
// fmt.Printf("Account: %#v\n", acct)
// print out whole account or just coins?
output, err := json.MarshalIndent(acct, "", " ")
// output, err := json.MarshalIndent(acct.BaseAccount.Coins, "", " ")
if err != nil {

View File

@ -89,7 +89,11 @@ func buildTx() ([]byte, error) {
// sign and build
bz := msg.GetSignBytes()
passphrase := "1234567890" // XXX: adults only
prompt := fmt.Sprintf("Password to sign with '%s':", name)
passphrase, err := client.GetPassword(prompt)
if err != nil {
return nil, err
}
sig, pubkey, err := keybase.Sign(name, passphrase, bz)
if err != nil {
return nil, err

View File

@ -15,7 +15,7 @@ var _ sdk.Account = (*AppAccount)(nil)
// auth.AccountStore uses the flexible go-wire library.
type AppAccount struct {
auth.BaseAccount
Name string
Name string `json:"name"`
}
// nolint