From 76f36a4c67d6bbff7d552563e190a4021802ceaa Mon Sep 17 00:00:00 2001 From: Johns Beharry Date: Sun, 7 Oct 2018 10:36:11 +0800 Subject: [PATCH] cmd/clef: replace password arg with prompt (#17829) --- cmd/clef/main.go | 45 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/cmd/clef/main.go b/cmd/clef/main.go index c060285be604..fc9a5a835f5f 100644 --- a/cmd/clef/main.go +++ b/cmd/clef/main.go @@ -37,6 +37,7 @@ import ( "github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/console" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/node" @@ -159,15 +160,14 @@ Clef that the file is 'safe' to execute.`, Action: utils.MigrateFlags(addCredential), Name: "addpw", Usage: "Store a credential for a keystore file", - ArgsUsage: "
", + ArgsUsage: "
", Flags: []cli.Flag{ logLevelFlag, configdirFlag, signerSecretFlag, }, Description: ` -The addpw command stores a password for a given address (keyfile). If you invoke it with only one parameter, it will -remove any stored credential for that address (keyfile) +The addpw command stores a password for a given address (keyfile). `, } ) @@ -201,6 +201,7 @@ func init() { app.Commands = []cli.Command{initCommand, attestCommand, addCredentialCommand} } + func main() { if err := app.Run(os.Args); err != nil { 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 { if err := initialize(c); err != nil { return err @@ -247,6 +271,7 @@ NOTE: This file does not contain your accounts. Those need to be backed up separ `) return nil } + func attestFile(ctx *cli.Context) error { if len(ctx.Args()) < 1 { utils.Fatalf("This command requires an argument.") @@ -273,8 +298,12 @@ func attestFile(ctx *cli.Context) error { func addCredential(ctx *cli.Context) error { 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 { return err } @@ -290,11 +319,7 @@ func addCredential(ctx *cli.Context) error { // Initialize the encrypted storages pwStorage := storage.NewAESEncryptedStorage(filepath.Join(vaultLocation, "credentials.json"), pwkey) key := ctx.Args().First() - value := "" - if len(ctx.Args()) > 1 { - value = ctx.Args().Get(1) - } - pwStorage.Put(key, value) + pwStorage.Put(key, password) log.Info("Credential store updated", "key", key) 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/ -**/ +**/ \ No newline at end of file