Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/clef: replace password arg with prompt #17856

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 35 additions & 10 deletions cmd/clef/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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: "<address> <password>",
ArgsUsage: "<address>",
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).
`,
}
)
Expand Down Expand Up @@ -201,13 +201,37 @@ func init() {
app.Commands = []cli.Command{initCommand, attestCommand, addCredentialCommand}

}

func main() {
if err := app.Run(os.Args); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}

// 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
Expand Down Expand Up @@ -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.")
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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/


**/
**/