From 9cba79edee6d859d8bdddb9e077ba821f435495d Mon Sep 17 00:00:00 2001 From: Shivam Sharma Date: Mon, 19 Dec 2022 13:57:18 +0530 Subject: [PATCH 1/5] init : remove exit on keystore err --- internal/cli/server/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/cli/server/config.go b/internal/cli/server/config.go index b8310aea6f..f1056ca769 100644 --- a/internal/cli/server/config.go +++ b/internal/cli/server/config.go @@ -722,7 +722,7 @@ func (c *Config) buildEth(stack *node.Node, accountManager *accounts.Manager) (* for i, account := range c.Accounts.Unlock { err = ks.Unlock(accounts.Account{Address: common.HexToAddress(account)}, passwords[i]) if err != nil { - return nil, fmt.Errorf("could not unlock an account %q", account) + log.Warn("Could not unlock account", "account", account, "err", err) } } } From 43cafc0415ad23d75a8d50667a14cc3155aef7f2 Mon Sep 17 00:00:00 2001 From: Shivam Sharma Date: Mon, 19 Dec 2022 18:06:16 +0530 Subject: [PATCH 2/5] add : multiple keystore tolerance --- internal/cli/server/config.go | 61 ++++++++++++++++++++++++++++++++--- 1 file changed, 57 insertions(+), 4 deletions(-) diff --git a/internal/cli/server/config.go b/internal/cli/server/config.go index f1056ca769..7dfb0df6c7 100644 --- a/internal/cli/server/config.go +++ b/internal/cli/server/config.go @@ -21,6 +21,7 @@ import ( "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/common/fdlimit" "github.com/ethereum/go-ethereum/eth/downloader" @@ -720,10 +721,7 @@ func (c *Config) buildEth(stack *node.Node, accountManager *accounts.Manager) (* } for i, account := range c.Accounts.Unlock { - err = ks.Unlock(accounts.Account{Address: common.HexToAddress(account)}, passwords[i]) - if err != nil { - log.Warn("Could not unlock account", "account", account, "err", err) - } + unlockAccount(ks, account, i, passwords) } } @@ -904,6 +902,61 @@ var ( gitDate = "" // Git commit date YYYYMMDD of the release (set via linker flags) ) +// tries unlocking the specified account a few times. +func unlockAccount(ks *keystore.KeyStore, address string, i int, passwords []string) (accounts.Account, string) { + account, err := utils.MakeAddress(ks, address) + if err != nil { + utils.Fatalf("Could not list accounts: %v", err) + } + for trials := 0; trials < 3; trials++ { + prompt := fmt.Sprintf("Unlocking account %s | Attempt %d/%d", address, trials+1, 3) + password := utils.GetPassPhraseWithList(prompt, false, i, passwords) + err = ks.Unlock(account, password) + if err == nil { + log.Info("Unlocked account", "address", account.Address.Hex()) + return account, password + } + if err, ok := err.(*keystore.AmbiguousAddrError); ok { + log.Info("Unlocked account", "address", account.Address.Hex()) + return ambiguousAddrRecovery(ks, err, password), password + } + if err != keystore.ErrDecrypt { + // No need to prompt again if the error is not decryption-related. + break + } + } + // All trials expended to unlock account, bail out + utils.Fatalf("Failed to unlock account %s (%v)", address, err) + + return accounts.Account{}, "" +} + +func ambiguousAddrRecovery(ks *keystore.KeyStore, err *keystore.AmbiguousAddrError, auth string) accounts.Account { + fmt.Printf("Multiple key files exist for address %x:\n", err.Addr) + for _, a := range err.Matches { + fmt.Println(" ", a.URL) + } + fmt.Println("Testing your password against all of them...") + var match *accounts.Account + for _, a := range err.Matches { + if err := ks.Unlock(a, auth); err == nil { + match = &a + break + } + } + if match == nil { + utils.Fatalf("None of the listed files could be unlocked.") + } + fmt.Printf("Your password unlocked %s\n", match.URL) + fmt.Println("In order to avoid this warning, you need to remove the following duplicate key files:") + for _, a := range err.Matches { + if a != *match { + fmt.Println(" ", a.URL) + } + } + return *match +} + func (c *Config) buildNode() (*node.Config, error) { ipcPath := "" if !c.JsonRPC.IPCDisable { From 828801f9d8d0c80ba1bc56ed223975406b9896c5 Mon Sep 17 00:00:00 2001 From: Shivam Sharma Date: Mon, 19 Dec 2022 18:36:10 +0530 Subject: [PATCH 3/5] lint : fix linters --- internal/cli/server/config.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/internal/cli/server/config.go b/internal/cli/server/config.go index 7dfb0df6c7..1d011cc665 100644 --- a/internal/cli/server/config.go +++ b/internal/cli/server/config.go @@ -905,21 +905,26 @@ var ( // tries unlocking the specified account a few times. func unlockAccount(ks *keystore.KeyStore, address string, i int, passwords []string) (accounts.Account, string) { account, err := utils.MakeAddress(ks, address) + if err != nil { utils.Fatalf("Could not list accounts: %v", err) } + for trials := 0; trials < 3; trials++ { prompt := fmt.Sprintf("Unlocking account %s | Attempt %d/%d", address, trials+1, 3) password := utils.GetPassPhraseWithList(prompt, false, i, passwords) err = ks.Unlock(account, password) + if err == nil { log.Info("Unlocked account", "address", account.Address.Hex()) return account, password } + if err, ok := err.(*keystore.AmbiguousAddrError); ok { log.Info("Unlocked account", "address", account.Address.Hex()) return ambiguousAddrRecovery(ks, err, password), password } + if err != keystore.ErrDecrypt { // No need to prompt again if the error is not decryption-related. break @@ -933,27 +938,36 @@ func unlockAccount(ks *keystore.KeyStore, address string, i int, passwords []str func ambiguousAddrRecovery(ks *keystore.KeyStore, err *keystore.AmbiguousAddrError, auth string) accounts.Account { fmt.Printf("Multiple key files exist for address %x:\n", err.Addr) + for _, a := range err.Matches { fmt.Println(" ", a.URL) } + fmt.Println("Testing your password against all of them...") + var match *accounts.Account + for _, a := range err.Matches { if err := ks.Unlock(a, auth); err == nil { + // nolint: gosec, exportloopref match = &a break } } + if match == nil { utils.Fatalf("None of the listed files could be unlocked.") } + fmt.Printf("Your password unlocked %s\n", match.URL) fmt.Println("In order to avoid this warning, you need to remove the following duplicate key files:") + for _, a := range err.Matches { if a != *match { fmt.Println(" ", a.URL) } } + return *match } From 513127cd8f881940b9be8acfaea8f1f573dc7873 Mon Sep 17 00:00:00 2001 From: Shivam Sharma Date: Tue, 20 Dec 2022 10:54:03 +0530 Subject: [PATCH 4/5] chg : use standard logging --- internal/cli/server/config.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/internal/cli/server/config.go b/internal/cli/server/config.go index 1d011cc665..83971cf217 100644 --- a/internal/cli/server/config.go +++ b/internal/cli/server/config.go @@ -937,13 +937,13 @@ func unlockAccount(ks *keystore.KeyStore, address string, i int, passwords []str } func ambiguousAddrRecovery(ks *keystore.KeyStore, err *keystore.AmbiguousAddrError, auth string) accounts.Account { - fmt.Printf("Multiple key files exist for address %x:\n", err.Addr) + log.Warn("Multiple key files exist for", "address", err.Addr) for _, a := range err.Matches { - fmt.Println(" ", a.URL) + log.Info("Multiple keys", "file", a.URL) } - fmt.Println("Testing your password against all of them...") + log.Info("Testing your password against all of them...") var match *accounts.Account @@ -959,8 +959,8 @@ func ambiguousAddrRecovery(ks *keystore.KeyStore, err *keystore.AmbiguousAddrErr utils.Fatalf("None of the listed files could be unlocked.") } - fmt.Printf("Your password unlocked %s\n", match.URL) - fmt.Println("In order to avoid this warning, you need to remove the following duplicate key files:") + log.Info("Your password unlocked", "key", match.URL) + log.Warn("In order to avoid this warning, you need to remove the following duplicate key files:") for _, a := range err.Matches { if a != *match { From 7b52c93c1bfcd4dc2f40f68bfd3e8a5c4c1d6347 Mon Sep 17 00:00:00 2001 From: Shivam Sharma Date: Tue, 20 Dec 2022 11:39:11 +0530 Subject: [PATCH 5/5] chg : logging strings --- internal/cli/server/config.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/cli/server/config.go b/internal/cli/server/config.go index 83971cf217..34c17b3f7d 100644 --- a/internal/cli/server/config.go +++ b/internal/cli/server/config.go @@ -940,7 +940,7 @@ func ambiguousAddrRecovery(ks *keystore.KeyStore, err *keystore.AmbiguousAddrErr log.Warn("Multiple key files exist for", "address", err.Addr) for _, a := range err.Matches { - log.Info("Multiple keys", "file", a.URL) + log.Info("Multiple keys", "file", a.URL.String()) } log.Info("Testing your password against all of them...") @@ -959,12 +959,12 @@ func ambiguousAddrRecovery(ks *keystore.KeyStore, err *keystore.AmbiguousAddrErr utils.Fatalf("None of the listed files could be unlocked.") } - log.Info("Your password unlocked", "key", match.URL) + log.Info("Your password unlocked", "key", match.URL.String()) log.Warn("In order to avoid this warning, you need to remove the following duplicate key files:") for _, a := range err.Matches { if a != *match { - fmt.Println(" ", a.URL) + log.Warn("Duplicate", "key", a.URL.String()) } }