mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 18:02:24 +00:00
cmd/geth, cmd/utils: refactor to reduce common code
This commit is contained in:
parent
8e1a7d9389
commit
9bb772b156
2 changed files with 30 additions and 25 deletions
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Reference in a new issue