diff --git a/cmd/geth/accountcmd.go b/cmd/geth/accountcmd.go index 59fc2e4493..fe6ef34468 100644 --- a/cmd/geth/accountcmd.go +++ b/cmd/geth/accountcmd.go @@ -22,8 +22,6 @@ import ( "os" "strings" - "github.com/urfave/cli/v2" - "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts/keystore" "github.com/ethereum/go-ethereum/cmd/utils" diff --git a/cmd/geth/consolecmd.go b/cmd/geth/consolecmd.go index d6250b4d93..aa78242fd3 100644 --- a/cmd/geth/consolecmd.go +++ b/cmd/geth/consolecmd.go @@ -18,13 +18,14 @@ package main import ( "fmt" + "os" + "path/filepath" "slices" "strings" - "github.com/urfave/cli/v2" - "github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/console" + "github.com/ethereum/go-ethereum/node" "github.com/urfave/cli/v2" ) diff --git a/cmd/geth/main.go b/cmd/geth/main.go index cc39a01b43..01ae223742 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -30,7 +30,6 @@ import ( "time" "github.com/ethereum/go-ethereum/accounts" - "github.com/ethereum/go-ethereum/accounts/keystore" "github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/console/prompt" @@ -59,7 +58,7 @@ const ( var ( // flags that configure the node - nodeFlags = flags.Merge([]cli.Flag{ + nodeFlags = slices.Concat([]cli.Flag{ utils.BorLogsFlag, utils.IdentityFlag, utils.UnlockedAccountFlag, @@ -465,41 +464,6 @@ func startNode(ctx *cli.Context, stack *node.Node, isConsole bool) { } } -// unlockAccounts unlocks any account specifically requested. -func unlockAccounts(ctx *cli.Context, stack *node.Node) { - var unlocks []string - - inputs := strings.Split(ctx.String(utils.UnlockedAccountFlag.Name), ",") - for _, input := range inputs { - if trimmed := strings.TrimSpace(input); trimmed != "" { - unlocks = append(unlocks, trimmed) - } - } - // Short circuit if there is no account to unlock. - if len(unlocks) == 0 { - return - } - // If insecure account unlocking is not allowed if node's APIs are exposed to external. - // Print warning log to user and skip unlocking. - if !stack.Config().InsecureUnlockAllowed && stack.Config().ExtRPCEnabled() { - utils.Fatalf("Account unlock with HTTP access is forbidden!") - } - - backends := stack.AccountManager().Backends(keystore.KeyStoreType) - - if len(backends) == 0 { - log.Warn("Failed to unlock accounts, keystore is not available") - return - } - - ks := backends[0].(*keystore.KeyStore) - passwords := utils.MakePasswordList(ctx) - - for i, account := range unlocks { - unlockAccount(ks, account, i, passwords) - } -} - func getHeimdallArgs(ctx *cli.Context) []string { heimdallArgs := strings.Split(ctx.String(utils.RunHeimdallArgsFlag.Name), ",") return append([]string{"start"}, heimdallArgs...) diff --git a/cmd/geth/misccmd.go b/cmd/geth/misccmd.go index af9ac6f987..f1d13f3da2 100644 --- a/cmd/geth/misccmd.go +++ b/cmd/geth/misccmd.go @@ -22,8 +22,6 @@ import ( "runtime" "strings" - "github.com/urfave/cli/v2" - "github.com/ethereum/go-ethereum/internal/version" "github.com/urfave/cli/v2" ) diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 8bc4479138..78c1ec5665 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -1311,6 +1311,33 @@ func MakeDatabaseHandles(max int) int { return int(raised / 2) // Leave half for networking and other stuff } +// MakeAddress converts an account specified directly as a hex encoded string or +// a key index in the key store to an internal account representation. +func MakeAddress(ks *keystore.KeyStore, account string) (accounts.Account, error) { + // If the specified account is a valid address, return it + if common.IsHexAddress(account) { + return accounts.Account{Address: common.HexToAddress(account)}, nil + } + // Otherwise try to interpret the account as a keystore index + index, err := strconv.Atoi(account) + if err != nil || index < 0 { + return accounts.Account{}, fmt.Errorf("invalid account address or index %q", account) + } + + log.Warn("-------------------------------------------------------------------") + log.Warn("Referring to accounts by order in the keystore folder is dangerous!") + log.Warn("This functionality is deprecated and will be removed in the future!") + log.Warn("Please use explicit addresses! (can search via `geth account list`)") + log.Warn("-------------------------------------------------------------------") + + accs := ks.Accounts() + if len(accs) <= index { + return accounts.Account{}, fmt.Errorf("index %d higher than number of accounts %d", index, len(accs)) + } + + return accs[index], nil +} + // setEtherbase retrieves the etherbase from the directly specified command line flags. func setEtherbase(ctx *cli.Context, cfg *ethconfig.Config) { if ctx.IsSet(MinerEtherbaseFlag.Name) { diff --git a/cmd/utils/prompt.go b/cmd/utils/prompt.go index 1016ff2163..bc14d38fde 100644 --- a/cmd/utils/prompt.go +++ b/cmd/utils/prompt.go @@ -49,3 +49,20 @@ func GetPassPhrase(text string, confirmation bool) string { return password } + +// GetPassPhraseWithList retrieves the password associated with an account, either fetched +// from a list of preloaded passphrases, or requested interactively from the user. +func GetPassPhraseWithList(text string, confirmation bool, index int, passwords []string) string { + // If a list of passwords was supplied, retrieve from them + if len(passwords) > 0 { + if index < len(passwords) { + return passwords[index] + } + + return passwords[len(passwords)-1] + } + // Otherwise prompt the user for the password + password := GetPassPhrase(text, confirmation) + + return password +}