cmd/clef: replace password arg with prompt (#17829)

This commit is contained in:
Johns Beharry 2018-10-07 10:36:11 +08:00
parent f95811e65b
commit 76f36a4c67
No known key found for this signature in database
GPG key ID: 0B605DDF87285EA7

View file

@ -37,6 +37,7 @@ import (
"github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/console"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/node"
@ -159,15 +160,14 @@ Clef that the file is 'safe' to execute.`,
Action: utils.MigrateFlags(addCredential), Action: utils.MigrateFlags(addCredential),
Name: "addpw", Name: "addpw",
Usage: "Store a credential for a keystore file", Usage: "Store a credential for a keystore file",
ArgsUsage: "<address> <password>", ArgsUsage: "<address>",
Flags: []cli.Flag{ Flags: []cli.Flag{
logLevelFlag, logLevelFlag,
configdirFlag, configdirFlag,
signerSecretFlag, signerSecretFlag,
}, },
Description: ` Description: `
The addpw command stores a password for a given address (keyfile). If you invoke it with only one parameter, it will The addpw command stores a password for a given address (keyfile).
remove any stored credential for that address (keyfile)
`, `,
} }
) )
@ -201,6 +201,7 @@ func init() {
app.Commands = []cli.Command{initCommand, attestCommand, addCredentialCommand} app.Commands = []cli.Command{initCommand, attestCommand, addCredentialCommand}
} }
func main() { func main() {
if err := app.Run(os.Args); err != nil { if err := app.Run(os.Args); err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
@ -208,6 +209,29 @@ func main() {
} }
} }
// promptPassword retrieves the password entered interactively
// from the user.
func promptPassword(prompt string, confirmation bool) string {
// Prompt the user for the password
if prompt != "" {
fmt.Println(prompt)
}
password, err := console.Stdin.PromptPassword("Password: ")
if err != nil {
utils.Fatalf("Failed to read password: %v", err)
}
if confirmation {
confirm, err := console.Stdin.PromptPassword("Repeat password: ")
if err != nil {
utils.Fatalf("Failed to read password confirmation: %v", err)
}
if password != confirm {
utils.Fatalf("Passwords do not match")
}
}
return password
}
func initializeSecrets(c *cli.Context) error { func initializeSecrets(c *cli.Context) error {
if err := initialize(c); err != nil { if err := initialize(c); err != nil {
return err return err
@ -247,6 +271,7 @@ NOTE: This file does not contain your accounts. Those need to be backed up separ
`) `)
return nil return nil
} }
func attestFile(ctx *cli.Context) error { func attestFile(ctx *cli.Context) error {
if len(ctx.Args()) < 1 { if len(ctx.Args()) < 1 {
utils.Fatalf("This command requires an argument.") utils.Fatalf("This command requires an argument.")
@ -273,8 +298,12 @@ func attestFile(ctx *cli.Context) error {
func addCredential(ctx *cli.Context) error { func addCredential(ctx *cli.Context) error {
if len(ctx.Args()) < 1 { if len(ctx.Args()) < 1 {
utils.Fatalf("This command requires at leaste one argument.") utils.Fatalf("This command requires at least one argument.")
} }
// Prompt user to enter a password
password := promptPassword("Enter a password to store with this address.", true)
if err := initialize(ctx); err != nil { if err := initialize(ctx); err != nil {
return err return err
} }
@ -290,11 +319,7 @@ func addCredential(ctx *cli.Context) error {
// Initialize the encrypted storages // Initialize the encrypted storages
pwStorage := storage.NewAESEncryptedStorage(filepath.Join(vaultLocation, "credentials.json"), pwkey) pwStorage := storage.NewAESEncryptedStorage(filepath.Join(vaultLocation, "credentials.json"), pwkey)
key := ctx.Args().First() key := ctx.Args().First()
value := "" pwStorage.Put(key, password)
if len(ctx.Args()) > 1 {
value = ctx.Args().Get(1)
}
pwStorage.Put(key, value)
log.Info("Credential store updated", "key", key) log.Info("Credential store updated", "key", key)
return nil return nil
} }
@ -643,4 +668,4 @@ curl -i -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0","me
curl -i -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"account_sign","params":["0x694267f14675d7e1b9494fd8d72fefe1755710fa","bazonk gaz baz"],"id":67}' http://localhost:8550/ curl -i -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"account_sign","params":["0x694267f14675d7e1b9494fd8d72fefe1755710fa","bazonk gaz baz"],"id":67}' http://localhost:8550/
**/ **/