change passphrase confirmation logic (#178)

* Change API to OSTN for cookbook

* adding passphrase in cookbook

* change passphrase confirmation logic

* split getPassphrase into 2 seperate function

* remove trailing whitespace
pull/181/head
Soph 5 years ago committed by GitHub
parent f04d863eab
commit aea5efdb8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 36
      cmd/subcommands/keys.go

@ -40,8 +40,34 @@ var (
// getPassphrase fetches the correct passphrase depending on if a file is available to
// read from or if the user wants to enter in their own passphrase. Otherwise, just use
// the default passphrase.
// the default passphrase. No confirmation of passphrase
func getPassphrase() (string, error) {
if passphraseFilePath != "" {
if _, err := os.Stat(passphraseFilePath); os.IsNotExist(err) {
return "", errors.New(fmt.Sprintf("passphrase file not found at `%s`", passphraseFilePath))
}
dat, err := ioutil.ReadFile(passphraseFilePath)
if err != nil {
return "", err
}
pw := strings.TrimSuffix(string(dat), "\n")
return pw, nil
} else if userProvidesPassphrase {
fmt.Println("Enter passphrase:")
pass, err := terminal.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
return "", err
}
return string(pass), nil
} else {
return c.DefaultPassphrase, nil
}
}
// getPassphrase fetches the correct passphrase depending on if a file is available to
// read from or if the user wants to enter in their own passphrase. Otherwise, just use
// the default passphrase. Passphrase requires a confirmation
func getPassphraseWithConfirm() (string, error) {
if passphraseFilePath != "" {
if _, err := os.Stat(passphraseFilePath); os.IsNotExist(err) {
return "", errors.New(fmt.Sprintf("passphrase file not found at `%s`", passphraseFilePath))
@ -104,7 +130,7 @@ func keysSub() []*cobra.Command {
if store.DoesNamedAccountExist(args[0]) {
return fmt.Errorf("account %s already exists", args[0])
}
passphrase, err := getPassphrase()
passphrase, err := getPassphraseWithConfirm()
if err != nil {
return err
}
@ -168,7 +194,7 @@ func keysSub() []*cobra.Command {
if store.DoesNamedAccountExist(args[0]) {
return fmt.Errorf("account %s already exists", args[0])
}
passphrase, err := getPassphrase()
passphrase, err := getPassphraseWithConfirm()
if err != nil {
return err
}
@ -283,7 +309,7 @@ func keysSub() []*cobra.Command {
Use: "generate-bls-key",
Short: "Generate bls keys then encrypt and save the private key with a requested passphrase",
RunE: func(cmd *cobra.Command, args []string) error {
passphrase, err := getPassphrase()
passphrase, err := getPassphraseWithConfirm()
if err != nil {
return err
}
@ -315,7 +341,7 @@ func keysSub() []*cobra.Command {
Short: "Encrypt and save the bls private key with a requested passphrase",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
passphrase, err := getPassphrase()
passphrase, err := getPassphraseWithConfirm()
if err != nil {
return err
}

Loading…
Cancel
Save