From 9bb772b156ad82fcb3a7c6dd25f367508fe314f8 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Wed, 6 Nov 2024 21:46:42 +0100 Subject: [PATCH] cmd/geth, cmd/utils: refactor to reduce common code --- cmd/geth/accountcmd.go | 25 ++++++++++++++++++++++--- cmd/utils/flags.go | 30 ++++++++---------------------- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/cmd/geth/accountcmd.go b/cmd/geth/accountcmd.go index 1e53140bcb..51721a54e5 100644 --- a/cmd/geth/accountcmd.go +++ b/cmd/geth/accountcmd.go @@ -20,6 +20,7 @@ import ( "errors" "fmt" "os" + "strings" "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts/keystore" @@ -220,6 +221,24 @@ func accountList(ctx *cli.Context) error { return nil } +// readPasswordFromFile reads the first line of the given file, trims line endings, +// and returns the password and whether the reading was successful. +func readPasswordFromFile(path string) (string, bool) { + if path == "" { + return "", false + } + text, err := os.ReadFile(path) + if err != nil { + utils.Fatalf("Failed to read password file: %v", err) + } + lines := strings.Split(string(text), "\n") + if len(lines) == 0 { + return "", false + } + // Sanitise DOS line endings. + return strings.TrimRight(lines[0], "\r"), true +} + // accountCreate creates a new account into the keystore defined by the CLI flags. func accountCreate(ctx *cli.Context) error { cfg := loadBaseConfig(ctx) @@ -237,7 +256,7 @@ func accountCreate(ctx *cli.Context) error { scryptP = keystore.LightScryptP } - password, ok := utils.ReadPasswordFromList(ctx) + password, ok := readPasswordFromFile(ctx.Path(utils.PasswordFileFlag.Name)) if !ok { password = utils.GetPassPhrase("Your new account is locked with a password. Please give a password. Do not forget this password.", true) } @@ -307,7 +326,7 @@ func importWallet(ctx *cli.Context) error { if len(backends) == 0 { utils.Fatalf("Keystore is not available") } - password, ok := utils.ReadPasswordFromList(ctx) + password, ok := readPasswordFromFile(ctx.Path(utils.PasswordFileFlag.Name)) if !ok { password = utils.GetPassPhrase("", false) } @@ -335,7 +354,7 @@ func accountImport(ctx *cli.Context) error { utils.Fatalf("Keystore is not available") } ks := backends[0].(*keystore.KeyStore) - password, ok := utils.ReadPasswordFromList(ctx) + password, ok := readPasswordFromFile(ctx.Path(utils.PasswordFileFlag.Name)) if !ok { password = utils.GetPassPhrase("Your new account is locked with a password. Please give a password. Do not forget this password.", true) } diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 5faedac0ae..98898f05be 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -1280,25 +1280,6 @@ func setEtherbase(ctx *cli.Context, cfg *ethconfig.Config) { cfg.Miner.PendingFeeRecipient = common.BytesToAddress(b) } -// MakePasswordList reads the first line of the given file, trims line endings, -// and returns the password and whether the reading was successful. -func ReadPasswordFromList(ctx *cli.Context) (string, bool) { - path := ctx.Path(PasswordFileFlag.Name) - if path == "" { - return "", false - } - text, err := os.ReadFile(path) - if err != nil { - Fatalf("Failed to read password file: %v", err) - } - lines := strings.Split(string(text), "\n") - if len(lines) == 0 { - return "", false - } - // Sanitise DOS line endings. - return strings.TrimRight(lines[0], "\r"), true -} - func SetP2PConfig(ctx *cli.Context, cfg *p2p.Config) { setNodeKey(ctx, cfg) setNAT(ctx, cfg) @@ -1773,10 +1754,15 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) { passphrase string err error ) - if pw, ok := ReadPasswordFromList(ctx); ok { - passphrase = pw + if path := ctx.Path(PasswordFileFlag.Name); path != "" { + if text, err := os.ReadFile(path); err != nil { + Fatalf("Failed to read password file: %v", err) + } else { + if lines := strings.Split(string(text), "\n"); len(lines) > 0 { + passphrase = strings.TrimRight(lines[0], "\r") // Sanitise DOS line endings. + } + } } - // Unlock the developer account by local keystore. var ks *keystore.KeyStore if keystores := stack.AccountManager().Backends(keystore.KeyStoreType); len(keystores) > 0 {