Skip to content

Commit

Permalink
Cleanup client/input.go per buckys request
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanfrey authored and rigelrozanski committed Mar 1, 2018
1 parent 05f5141 commit 03dc660
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 24 deletions.
31 changes: 15 additions & 16 deletions client/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@ import (
// 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
// BufferStdin is used to allow reading prompts for stdin
// multiple times, when we read from non-tty
func BufferStdin() *bufio.Reader {
return bufio.NewReader(os.Stdin)
}

// 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) {
func GetPassword(prompt string, buf *bufio.Reader) (pass string, err error) {
if inputIsTty() {
pass, err = speakeasy.Ask(prompt)
} else {
pass, err = stdinPassword()
pass, err = readLineFromBuf(buf)
}
if err != nil {
return "", err
Expand All @@ -37,11 +39,11 @@ func GetPassword(prompt string) (pass string, err error) {

// GetSeed will request a seed phrase from stdin and trims off
// leading/trailing spaces
func GetSeed(prompt string) (seed string, err error) {
func GetSeed(prompt string, buf *bufio.Reader) (seed string, err error) {
if inputIsTty() {
fmt.Println(prompt)
}
seed, err = stdinPassword()
seed, err = readLineFromBuf(buf)
seed = strings.TrimSpace(seed)
return
}
Expand All @@ -50,18 +52,18 @@ func GetSeed(prompt string) (seed string, err error) {
// 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) {
func GetCheckPassword(prompt, prompt2 string, buf *bufio.Reader) (string, error) {
// simple read on no-tty
if !inputIsTty() {
return GetPassword(prompt)
return GetPassword(prompt, buf)
}

// TODO: own function???
pass, err := GetPassword(prompt)
pass, err := GetPassword(prompt, buf)
if err != nil {
return "", err
}
pass2, err := GetPassword(prompt2)
pass2, err := GetPassword(prompt2, buf)
if err != nil {
return "", err
}
Expand All @@ -78,13 +80,10 @@ func inputIsTty() bool {
return isatty.IsTerminal(os.Stdin.Fd()) || isatty.IsCygwinTerminal(os.Stdin.Fd())
}

// stdinPassword reads one line from stdin.
// readLineFromBuf reads one line from stdin.
// Subsequent calls reuse the same buffer, so we don't lose
// any input when reading a password twice (to verify)
func stdinPassword() (string, error) {
if buf == nil {
buf = bufio.NewReader(os.Stdin)
}
func readLineFromBuf(buf *bufio.Reader) (string, error) {
pass, err := buf.ReadString('\n')
if err != nil {
return "", err
Expand Down
8 changes: 6 additions & 2 deletions client/keys/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func runAddCmd(cmd *cobra.Command, args []string) error {
var err error
var name, pass string

buf := client.BufferStdin()
if viper.GetBool(flagDryRun) {
// we throw this away, so don't enforce args,
// we want to get a new random seed phrase quickly
Expand All @@ -69,14 +70,17 @@ func runAddCmd(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
pass, err = client.GetCheckPassword("Enter a passphrase for your key:", "Repeat the passphrase:")
pass, err = client.GetCheckPassword(
"Enter a passphrase for your key:",
"Repeat the passphrase:", buf)
if err != nil {
return err
}
}

if viper.GetBool(flagRecover) {
seed, err := client.GetSeed("Enter your recovery seed phrase:")
seed, err := client.GetSeed(
"Enter your recovery seed phrase:", buf)
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion client/keys/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ func runDeleteCmd(cmd *cobra.Command, args []string) error {
}
name := args[0]

oldpass, err := client.GetPassword("DANGER - enter password to permanently delete key:")
buf := client.BufferStdin()
oldpass, err := client.GetPassword(
"DANGER - enter password to permanently delete key:", buf)
if err != nil {
return err
}
Expand Down
8 changes: 6 additions & 2 deletions client/keys/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,15 @@ func runUpdateCmd(cmd *cobra.Command, args []string) error {
}
name := args[0]

oldpass, err := client.GetPassword("Enter the current passphrase:")
buf := client.BufferStdin()
oldpass, err := client.GetPassword(
"Enter the current passphrase:", buf)
if err != nil {
return err
}
newpass, err := client.GetCheckPassword("Enter the new passphrase:", "Repeat the new passphrase:")
newpass, err := client.GetCheckPassword(
"Enter the new passphrase:",
"Repeat the new passphrase:", buf)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions x/bank/commands/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
rpcclient "github.com/tendermint/tendermint/rpc/client"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/examples/basecoin/app"
"github.com/cosmos/cosmos-sdk/examples/basecoin/types"
"github.com/cosmos/cosmos-sdk/examples/basecoin/app" // XXX: not good
"github.com/cosmos/cosmos-sdk/examples/basecoin/types" // XXX: not good
)

// GetAccountCmd returns a query account that will display the
Expand Down
3 changes: 2 additions & 1 deletion x/bank/commands/sendtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ func buildTx() ([]byte, error) {

// sign and build
bz := msg.GetSignBytes()
buf := client.BufferStdin()
prompt := fmt.Sprintf("Password to sign with '%s':", name)
passphrase, err := client.GetPassword(prompt)
passphrase, err := client.GetPassword(prompt, buf)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 03dc660

Please sign in to comment.