cmd/geth: make account commands not require datadir lock (#27084)

Makes the `geth account ... ` commands usable even if a geth-process is already executing, since the account commands do not read the chaindata, it was not required for those to use the same locking mechanism. 

---
Signed-off-by: jsvisa <delweng@gmail.com>
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Sina Mahmoodi <itz.s1na@gmail.com>
This commit is contained in:
Delweng 2023-04-27 18:57:29 +08:00 committed by GitHub
parent 66c0c4e517
commit 8f373227ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 53 additions and 33 deletions

View File

@ -188,15 +188,34 @@ nodes.
} }
) )
// makeAccountManager creates an account manager with backends
func makeAccountManager(ctx *cli.Context) *accounts.Manager {
cfg := loadBaseConfig(ctx)
am := accounts.NewManager(&accounts.Config{InsecureUnlockAllowed: cfg.Node.InsecureUnlockAllowed})
keydir, isEphemeral, err := cfg.Node.GetKeyStoreDir()
if err != nil {
utils.Fatalf("Failed to get the keystore directory: %v", err)
}
if isEphemeral {
utils.Fatalf("Can't use ephemeral directory as keystore path")
}
if err := setAccountManagerBackends(&cfg.Node, am, keydir); err != nil {
utils.Fatalf("Failed to set account manager backends: %v", err)
}
return am
}
func accountList(ctx *cli.Context) error { func accountList(ctx *cli.Context) error {
stack, _ := makeConfigNode(ctx) am := makeAccountManager(ctx)
var index int var index int
for _, wallet := range stack.AccountManager().Wallets() { for _, wallet := range am.Wallets() {
for _, account := range wallet.Accounts() { for _, account := range wallet.Accounts() {
fmt.Printf("Account #%d: {%x} %s\n", index, account.Address, &account.URL) fmt.Printf("Account #%d: {%x} %s\n", index, account.Address, &account.URL)
index++ index++
} }
} }
return nil return nil
} }
@ -258,17 +277,13 @@ func ambiguousAddrRecovery(ks *keystore.KeyStore, err *keystore.AmbiguousAddrErr
// accountCreate creates a new account into the keystore defined by the CLI flags. // accountCreate creates a new account into the keystore defined by the CLI flags.
func accountCreate(ctx *cli.Context) error { func accountCreate(ctx *cli.Context) error {
cfg := gethConfig{Node: defaultNodeConfig()} cfg := loadBaseConfig(ctx)
// Load config file. keydir, isEphemeral, err := cfg.Node.GetKeyStoreDir()
if file := ctx.String(configFileFlag.Name); file != "" {
if err := loadConfig(file, &cfg); err != nil {
utils.Fatalf("%v", err)
}
}
utils.SetNodeConfig(ctx, &cfg.Node)
keydir, err := cfg.Node.KeyDirConfig()
if err != nil { if err != nil {
utils.Fatalf("Failed to read configuration: %v", err) utils.Fatalf("Failed to get the keystore directory: %v", err)
}
if isEphemeral {
utils.Fatalf("Can't use ephemeral directory as keystore path")
} }
scryptN := keystore.StandardScryptN scryptN := keystore.StandardScryptN
scryptP := keystore.StandardScryptP scryptP := keystore.StandardScryptP
@ -300,8 +315,8 @@ func accountUpdate(ctx *cli.Context) error {
if ctx.Args().Len() == 0 { if ctx.Args().Len() == 0 {
utils.Fatalf("No accounts specified to update") utils.Fatalf("No accounts specified to update")
} }
stack, _ := makeConfigNode(ctx) am := makeAccountManager(ctx)
backends := stack.AccountManager().Backends(keystore.KeyStoreType) backends := am.Backends(keystore.KeyStoreType)
if len(backends) == 0 { if len(backends) == 0 {
utils.Fatalf("Keystore is not available") utils.Fatalf("Keystore is not available")
} }
@ -327,14 +342,14 @@ func importWallet(ctx *cli.Context) error {
utils.Fatalf("Could not read wallet file: %v", err) utils.Fatalf("Could not read wallet file: %v", err)
} }
stack, _ := makeConfigNode(ctx) am := makeAccountManager(ctx)
passphrase := utils.GetPassPhraseWithList("", false, 0, utils.MakePasswordList(ctx)) backends := am.Backends(keystore.KeyStoreType)
backends := stack.AccountManager().Backends(keystore.KeyStoreType)
if len(backends) == 0 { if len(backends) == 0 {
utils.Fatalf("Keystore is not available") utils.Fatalf("Keystore is not available")
} }
ks := backends[0].(*keystore.KeyStore) ks := backends[0].(*keystore.KeyStore)
passphrase := utils.GetPassPhraseWithList("", false, 0, utils.MakePasswordList(ctx))
acct, err := ks.ImportPreSaleKey(keyJSON, passphrase) acct, err := ks.ImportPreSaleKey(keyJSON, passphrase)
if err != nil { if err != nil {
utils.Fatalf("%v", err) utils.Fatalf("%v", err)
@ -352,14 +367,14 @@ func accountImport(ctx *cli.Context) error {
if err != nil { if err != nil {
utils.Fatalf("Failed to load the private key: %v", err) utils.Fatalf("Failed to load the private key: %v", err)
} }
stack, _ := makeConfigNode(ctx) am := makeAccountManager(ctx)
passphrase := utils.GetPassPhraseWithList("Your new account is locked with a password. Please give a password. Do not forget this password.", true, 0, utils.MakePasswordList(ctx)) backends := am.Backends(keystore.KeyStoreType)
backends := stack.AccountManager().Backends(keystore.KeyStoreType)
if len(backends) == 0 { if len(backends) == 0 {
utils.Fatalf("Keystore is not available") utils.Fatalf("Keystore is not available")
} }
ks := backends[0].(*keystore.KeyStore) ks := backends[0].(*keystore.KeyStore)
passphrase := utils.GetPassPhraseWithList("Your new account is locked with a password. Please give a password. Do not forget this password.", true, 0, utils.MakePasswordList(ctx))
acct, err := ks.ImportECDSA(key, passphrase) acct, err := ks.ImportECDSA(key, passphrase)
if err != nil { if err != nil {
utils.Fatalf("Could not create the account: %v", err) utils.Fatalf("Could not create the account: %v", err)

View File

@ -26,6 +26,7 @@ import (
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/external" "github.com/ethereum/go-ethereum/accounts/external"
"github.com/ethereum/go-ethereum/accounts/keystore" "github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/accounts/scwallet" "github.com/ethereum/go-ethereum/accounts/scwallet"
@ -119,8 +120,9 @@ func defaultNodeConfig() node.Config {
return cfg return cfg
} }
// makeConfigNode loads geth configuration and creates a blank node instance. // loadBaseConfig loads the gethConfig based on the given command line
func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) { // parameters and config file.
func loadBaseConfig(ctx *cli.Context) gethConfig {
// Load defaults. // Load defaults.
cfg := gethConfig{ cfg := gethConfig{
Eth: ethconfig.Defaults, Eth: ethconfig.Defaults,
@ -137,12 +139,18 @@ func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) {
// Apply flags. // Apply flags.
utils.SetNodeConfig(ctx, &cfg.Node) utils.SetNodeConfig(ctx, &cfg.Node)
return cfg
}
// makeConfigNode loads geth configuration and creates a blank node instance.
func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) {
cfg := loadBaseConfig(ctx)
stack, err := node.New(&cfg.Node) stack, err := node.New(&cfg.Node)
if err != nil { if err != nil {
utils.Fatalf("Failed to create the protocol stack: %v", err) utils.Fatalf("Failed to create the protocol stack: %v", err)
} }
// Node doesn't by default populate account manager backends // Node doesn't by default populate account manager backends
if err := setAccountManagerBackends(stack); err != nil { if err := setAccountManagerBackends(stack.Config(), stack.AccountManager(), stack.KeyStoreDir()); err != nil {
utils.Fatalf("Failed to set account manager backends: %v", err) utils.Fatalf("Failed to set account manager backends: %v", err)
} }
@ -269,10 +277,7 @@ func deprecated(field string) bool {
} }
} }
func setAccountManagerBackends(stack *node.Node) error { func setAccountManagerBackends(conf *node.Config, am *accounts.Manager, keydir string) error {
conf := stack.Config()
am := stack.AccountManager()
keydir := stack.KeyStoreDir()
scryptN := keystore.StandardScryptN scryptN := keystore.StandardScryptN
scryptP := keystore.StandardScryptP scryptP := keystore.StandardScryptP
if conf.UseLightweightKDF { if conf.UseLightweightKDF {

View File

@ -448,10 +448,10 @@ func (c *Config) KeyDirConfig() (string, error) {
return keydir, err return keydir, err
} }
// getKeyStoreDir retrieves the key directory and will create // GetKeyStoreDir retrieves the key directory and will create
// and ephemeral one if necessary. // and ephemeral one if necessary.
func getKeyStoreDir(conf *Config) (string, bool, error) { func (c *Config) GetKeyStoreDir() (string, bool, error) {
keydir, err := conf.KeyDirConfig() keydir, err := c.KeyDirConfig()
if err != nil { if err != nil {
return "", false, err return "", false, err
} }

View File

@ -119,7 +119,7 @@ func New(conf *Config) (*Node, error) {
if err := node.openDataDir(); err != nil { if err := node.openDataDir(); err != nil {
return nil, err return nil, err
} }
keyDir, isEphem, err := getKeyStoreDir(conf) keyDir, isEphem, err := conf.GetKeyStoreDir()
if err != nil { if err != nil {
return nil, err return nil, err
} }