Skip to content

Commit

Permalink
Revert "cmd/geth: make account commands not require datadir lock (eth…
Browse files Browse the repository at this point in the history
…ereum#27084)"

This reverts commit 59a8a24.
  • Loading branch information
devopsbo3 authored Nov 10, 2023
1 parent a8ed755 commit 577ea5b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 53 deletions.
59 changes: 22 additions & 37 deletions cmd/geth/accountcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,34 +188,15 @@ 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 {
am := makeAccountManager(ctx)
stack, _ := makeConfigNode(ctx)
var index int
for _, wallet := range am.Wallets() {
for _, wallet := range stack.AccountManager().Wallets() {
for _, account := range wallet.Accounts() {
fmt.Printf("Account #%d: {%x} %s\n", index, account.Address, &account.URL)
index++
}
}

return nil
}

Expand Down Expand Up @@ -277,13 +258,17 @@ func ambiguousAddrRecovery(ks *keystore.KeyStore, err *keystore.AmbiguousAddrErr

// accountCreate creates a new account into the keystore defined by the CLI flags.
func accountCreate(ctx *cli.Context) error {
cfg := loadBaseConfig(ctx)
keydir, isEphemeral, err := cfg.Node.GetKeyStoreDir()
if err != nil {
utils.Fatalf("Failed to get the keystore directory: %v", err)
cfg := gethConfig{Node: defaultNodeConfig()}
// Load config file.
if file := ctx.String(configFileFlag.Name); file != "" {
if err := loadConfig(file, &cfg); err != nil {
utils.Fatalf("%v", err)
}
}
if isEphemeral {
utils.Fatalf("Can't use ephemeral directory as keystore path")
utils.SetNodeConfig(ctx, &cfg.Node)
keydir, err := cfg.Node.KeyDirConfig()
if err != nil {
utils.Fatalf("Failed to read configuration: %v", err)
}
scryptN := keystore.StandardScryptN
scryptP := keystore.StandardScryptP
Expand Down Expand Up @@ -315,8 +300,8 @@ func accountUpdate(ctx *cli.Context) error {
if ctx.Args().Len() == 0 {
utils.Fatalf("No accounts specified to update")
}
am := makeAccountManager(ctx)
backends := am.Backends(keystore.KeyStoreType)
stack, _ := makeConfigNode(ctx)
backends := stack.AccountManager().Backends(keystore.KeyStoreType)
if len(backends) == 0 {
utils.Fatalf("Keystore is not available")
}
Expand All @@ -342,14 +327,14 @@ func importWallet(ctx *cli.Context) error {
utils.Fatalf("Could not read wallet file: %v", err)
}

am := makeAccountManager(ctx)
backends := am.Backends(keystore.KeyStoreType)
stack, _ := makeConfigNode(ctx)
passphrase := utils.GetPassPhraseWithList("", false, 0, utils.MakePasswordList(ctx))

backends := stack.AccountManager().Backends(keystore.KeyStoreType)
if len(backends) == 0 {
utils.Fatalf("Keystore is not available")
}
ks := backends[0].(*keystore.KeyStore)
passphrase := utils.GetPassPhraseWithList("", false, 0, utils.MakePasswordList(ctx))

acct, err := ks.ImportPreSaleKey(keyJSON, passphrase)
if err != nil {
utils.Fatalf("%v", err)
Expand All @@ -367,14 +352,14 @@ func accountImport(ctx *cli.Context) error {
if err != nil {
utils.Fatalf("Failed to load the private key: %v", err)
}
am := makeAccountManager(ctx)
backends := am.Backends(keystore.KeyStoreType)
stack, _ := makeConfigNode(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 := stack.AccountManager().Backends(keystore.KeyStoreType)
if len(backends) == 0 {
utils.Fatalf("Keystore is not available")
}
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)
if err != nil {
utils.Fatalf("Could not create the account: %v", err)
Expand Down
19 changes: 7 additions & 12 deletions cmd/geth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (

"github.com/urfave/cli/v2"

"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/external"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/accounts/scwallet"
Expand Down Expand Up @@ -120,9 +119,8 @@ func defaultNodeConfig() node.Config {
return cfg
}

// loadBaseConfig loads the gethConfig based on the given command line
// parameters and config file.
func loadBaseConfig(ctx *cli.Context) gethConfig {
// makeConfigNode loads geth configuration and creates a blank node instance.
func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) {
// Load defaults.
cfg := gethConfig{
Eth: ethconfig.Defaults,
Expand All @@ -139,18 +137,12 @@ func loadBaseConfig(ctx *cli.Context) gethConfig {

// Apply flags.
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)
if err != nil {
utils.Fatalf("Failed to create the protocol stack: %v", err)
}
// Node doesn't by default populate account manager backends
if err := setAccountManagerBackends(stack.Config(), stack.AccountManager(), stack.KeyStoreDir()); err != nil {
if err := setAccountManagerBackends(stack); err != nil {
utils.Fatalf("Failed to set account manager backends: %v", err)
}

Expand Down Expand Up @@ -277,7 +269,10 @@ func deprecated(field string) bool {
}
}

func setAccountManagerBackends(conf *node.Config, am *accounts.Manager, keydir string) error {
func setAccountManagerBackends(stack *node.Node) error {
conf := stack.Config()
am := stack.AccountManager()
keydir := stack.KeyStoreDir()
scryptN := keystore.StandardScryptN
scryptP := keystore.StandardScryptP
if conf.UseLightweightKDF {
Expand Down
6 changes: 3 additions & 3 deletions node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,10 +448,10 @@ func (c *Config) KeyDirConfig() (string, error) {
return keydir, err
}

// GetKeyStoreDir retrieves the key directory and will create
// getKeyStoreDir retrieves the key directory and will create
// and ephemeral one if necessary.
func (c *Config) GetKeyStoreDir() (string, bool, error) {
keydir, err := c.KeyDirConfig()
func getKeyStoreDir(conf *Config) (string, bool, error) {
keydir, err := conf.KeyDirConfig()
if err != nil {
return "", false, err
}
Expand Down
2 changes: 1 addition & 1 deletion node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func New(conf *Config) (*Node, error) {
if err := node.openDataDir(); err != nil {
return nil, err
}
keyDir, isEphem, err := conf.GetKeyStoreDir()
keyDir, isEphem, err := getKeyStoreDir(conf)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 577ea5b

Please sign in to comment.