Skip to content

Commit 94457cc

Browse files
authored
cmd, miner, signer: avoid panic if keystore is not available (#27039)
* cmd, miner, singer: avoid panic if keystore is not available * cmd/geth: print warning instead of panic
1 parent 7076ae0 commit 94457cc

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

cmd/geth/accountcmd.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,11 @@ func accountUpdate(ctx *cli.Context) error {
301301
utils.Fatalf("No accounts specified to update")
302302
}
303303
stack, _ := makeConfigNode(ctx)
304-
ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)
304+
backends := stack.AccountManager().Backends(keystore.KeyStoreType)
305+
if len(backends) == 0 {
306+
utils.Fatalf("Keystore is not available")
307+
}
308+
ks := backends[0].(*keystore.KeyStore)
305309

306310
for _, addr := range ctx.Args().Slice() {
307311
account, oldPassword := unlockAccount(ks, addr, 0, nil)
@@ -326,7 +330,11 @@ func importWallet(ctx *cli.Context) error {
326330
stack, _ := makeConfigNode(ctx)
327331
passphrase := utils.GetPassPhraseWithList("", false, 0, utils.MakePasswordList(ctx))
328332

329-
ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)
333+
backends := stack.AccountManager().Backends(keystore.KeyStoreType)
334+
if len(backends) == 0 {
335+
utils.Fatalf("Keystore is not available")
336+
}
337+
ks := backends[0].(*keystore.KeyStore)
330338
acct, err := ks.ImportPreSaleKey(keyJSON, passphrase)
331339
if err != nil {
332340
utils.Fatalf("%v", err)
@@ -347,7 +355,11 @@ func accountImport(ctx *cli.Context) error {
347355
stack, _ := makeConfigNode(ctx)
348356
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))
349357

350-
ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)
358+
backends := stack.AccountManager().Backends(keystore.KeyStoreType)
359+
if len(backends) == 0 {
360+
utils.Fatalf("Keystore is not available")
361+
}
362+
ks := backends[0].(*keystore.KeyStore)
351363
acct, err := ks.ImportECDSA(key, passphrase)
352364
if err != nil {
353365
utils.Fatalf("Could not create the account: %v", err)

cmd/geth/main.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,12 @@ func unlockAccounts(ctx *cli.Context, stack *node.Node) {
464464
if !stack.Config().InsecureUnlockAllowed && stack.Config().ExtRPCEnabled() {
465465
utils.Fatalf("Account unlock with HTTP access is forbidden!")
466466
}
467-
ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)
467+
backends := stack.AccountManager().Backends(keystore.KeyStoreType)
468+
if len(backends) == 0 {
469+
log.Warn("Failed to unlock accounts, keystore is not available")
470+
return
471+
}
472+
ks := backends[0].(*keystore.KeyStore)
468473
passwords := utils.MakePasswordList(ctx)
469474
for i, account := range unlocks {
470475
unlockAccount(ks, account, i, passwords)

miner/stress/beacon/main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,11 @@ func newNode(typ nodetype, genesis *core.Genesis, enodes []*enode.Node) *ethNode
127127

128128
// Inject the signer key and start sealing with it
129129
stack.AccountManager().AddBackend(keystore.NewPlaintextKeyStore("beacon-stress"))
130-
store := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)
130+
ks := stack.AccountManager().Backends(keystore.KeyStoreType)
131+
if len(ks) == 0 {
132+
panic("Keystore is not available")
133+
}
134+
store := ks[0].(*keystore.KeyStore)
131135
if _, err := store.NewAccount(""); err != nil {
132136
panic(err)
133137
}

signer/core/uiapi.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,11 @@ func (s *UIServerAPI) DeriveAccount(url string, path string, pin *bool) (account
111111

112112
// fetchKeystore retrieves the encrypted keystore from the account manager.
113113
func fetchKeystore(am *accounts.Manager) *keystore.KeyStore {
114-
return am.Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)
114+
ks := am.Backends(keystore.KeyStoreType)
115+
if len(ks) == 0 {
116+
return nil
117+
}
118+
return ks[0].(*keystore.KeyStore)
115119
}
116120

117121
// ImportRawKey stores the given hex encoded ECDSA key into the key directory,

0 commit comments

Comments
 (0)