cmd/geth: remove account indexing by number

This commit is contained in:
Martin Holst Swende 2024-11-06 15:26:39 +01:00
parent f7088ba00c
commit e1a7cc7aff
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
2 changed files with 7 additions and 32 deletions

View file

@ -24,6 +24,7 @@ import (
"github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/keystore" "github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
@ -294,10 +295,10 @@ func accountUpdate(ctx *cli.Context) error {
ks := backends[0].(*keystore.KeyStore) ks := backends[0].(*keystore.KeyStore)
for _, addr := range ctx.Args().Slice() { for _, addr := range ctx.Args().Slice() {
account, err := utils.MakeAddress(ks, addr) if !common.IsHexAddress(addr) {
if err != nil { return errors.New("address must be specified in hexadecimal form")
return fmt.Errorf("could not locate account: %w", err)
} }
account := accounts.Account{Address: common.HexToAddress(addr)}
newPassword := utils.GetPassPhrase("Please give a NEW password. Do not forget this password.", true) newPassword := utils.GetPassPhrase("Please give a NEW password. Do not forget this password.", true)
updateFn := func(attempt int) error { updateFn := func(attempt int) error {
prompt := fmt.Sprintf("Please provide the OLD password for account %s | Attempt %d/%d", addr, attempt+1, 3) prompt := fmt.Sprintf("Please provide the OLD password for account %s | Attempt %d/%d", addr, attempt+1, 3)
@ -305,10 +306,9 @@ func accountUpdate(ctx *cli.Context) error {
return ks.Update(account, password, newPassword) return ks.Update(account, password, newPassword)
} }
// let user attempt unlock thrice. // let user attempt unlock thrice.
for attempts := 0; attempts < 3; attempts++ { err := updateFn(0)
if err = updateFn(attempts); !errors.Is(err, keystore.ErrDecrypt) { for attempts := 1; attempts < 3 && errors.Is(err, keystore.ErrDecrypt); attempts++ {
break // nil or some other type of error err = updateFn(attempts)
}
} }
if err != nil { if err != nil {
return fmt.Errorf("could not update account: %w", err) return fmt.Errorf("could not update account: %w", err)

View file

@ -1260,31 +1260,6 @@ func MakeDatabaseHandles(max int) int {
return int(raised / 2) // Leave half for networking and other stuff 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. // setEtherbase retrieves the etherbase from the directly specified command line flags.
func setEtherbase(ctx *cli.Context, cfg *ethconfig.Config) { func setEtherbase(ctx *cli.Context, cfg *ethconfig.Config) {
if ctx.IsSet(MinerEtherbaseFlag.Name) { if ctx.IsSet(MinerEtherbaseFlag.Name) {