From afa3a4ea5c5b30a3a0c73e732aef616d407bd277 Mon Sep 17 00:00:00 2001 From: JukLee0ira Date: Thu, 26 Jun 2025 09:14:41 +0800 Subject: [PATCH] cmd, internal/ethapi: avoid panic if keystore is not available #27039 (#1157) --- cmd/XDC/accountcmd.go | 18 +++++++++++++++--- cmd/XDC/main.go | 7 ++++++- internal/ethapi/api.go | 6 +++++- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/cmd/XDC/accountcmd.go b/cmd/XDC/accountcmd.go index 252a109b3d..29134623b0 100644 --- a/cmd/XDC/accountcmd.go +++ b/cmd/XDC/accountcmd.go @@ -337,7 +337,11 @@ func accountUpdate(ctx *cli.Context) error { utils.Fatalf("No accounts specified to update") } stack, _ := makeConfigNode(ctx) - ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore) + backends := stack.AccountManager().Backends(keystore.KeyStoreType) + if len(backends) == 0 { + utils.Fatalf("Keystore is not available") + } + ks := backends[0].(*keystore.KeyStore) for _, addr := range ctx.Args().Slice() { account, oldPassword := unlockAccount(ctx, ks, addr, 0, nil) @@ -362,7 +366,11 @@ func importWallet(ctx *cli.Context) error { stack, _ := makeConfigNode(ctx) passphrase := getPassPhrase("", false, 0, utils.MakePasswordList(ctx)) - ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore) + backends := stack.AccountManager().Backends(keystore.KeyStoreType) + if len(backends) == 0 { + utils.Fatalf("Keystore is not available") + } + ks := backends[0].(*keystore.KeyStore) acct, err := ks.ImportPreSaleKey(keyJson, passphrase) if err != nil { utils.Fatalf("%v", err) @@ -383,7 +391,11 @@ func accountImport(ctx *cli.Context) error { stack, _ := makeConfigNode(ctx) passphrase := getPassPhrase("Your new account is locked with a password. Please give a password. Do not forget this password.", true, 0, utils.MakePasswordList(ctx)) - ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore) + backends := stack.AccountManager().Backends(keystore.KeyStoreType) + if len(backends) == 0 { + utils.Fatalf("Keystore is not available") + } + ks := backends[0].(*keystore.KeyStore) acct, err := ks.ImportECDSA(key, passphrase) if err != nil { utils.Fatalf("Could not create the account: %v", err) diff --git a/cmd/XDC/main.go b/cmd/XDC/main.go index fda725bdb2..0fa41c9113 100644 --- a/cmd/XDC/main.go +++ b/cmd/XDC/main.go @@ -280,7 +280,12 @@ func startNode(ctx *cli.Context, stack *node.Node, backend ethapi.Backend, cfg X utils.StartNode(stack) // Unlock any account specifically requested - ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore) + 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) if ctx.IsSet(utils.UnlockedAccountFlag.Name) { cfg.Account.Unlocks = strings.Split(ctx.String(utils.UnlockedAccountFlag.Name), ",") diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index e3b9acc5ed..78b1353daf 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -392,7 +392,11 @@ func (s *PersonalAccountAPI) NewAccount(password string) (common.AddressEIP55, e // fetchKeystore retrives the encrypted keystore from the account manager. func fetchKeystore(am *accounts.Manager) *keystore.KeyStore { - return am.Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore) + ks := am.Backends(keystore.KeyStoreType) + if len(ks) == 0 { + return nil + } + return ks[0].(*keystore.KeyStore) } // ImportRawKey stores the given hex encoded ECDSA key into the key directory,