Skip to content

Commit 04c4e50

Browse files
authored
ethapi: don't crash when keystore-specific methods are called but external signer used (#21279)
* console: prevent importRawKey from getting into CLI history * internal/ethapi: error on keystore-methods when no keystore is present
1 parent 7451fc6 commit 04c4e50

2 files changed

Lines changed: 26 additions & 7 deletions

File tree

‎console/console.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ import (
3939
)
4040

4141
var (
42-
passwordRegexp = regexp.MustCompile(`personal.[nus]`)
42+
// u: unlock, s: signXX, sendXX, n: newAccount, i: importXX
43+
passwordRegexp = regexp.MustCompile(`personal.[nusi]`)
4344
onlyWhitespace = regexp.MustCompile(`^\s*$`)
4445
exit = regexp.MustCompile(`^\s*exit\s*;*\s*$`)
4546
)

‎internal/ethapi/api.go‎

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,11 @@ func (s *PrivateAccountAPI) DeriveAccount(url string, path string, pin *bool) (a
277277

278278
// NewAccount will create a new account and returns the address for the new account.
279279
func (s *PrivateAccountAPI) NewAccount(password string) (common.Address, error) {
280-
acc, err := fetchKeystore(s.am).NewAccount(password)
280+
ks, err := fetchKeystore(s.am)
281+
if err != nil {
282+
return common.Address{}, err
283+
}
284+
acc, err := ks.NewAccount(password)
281285
if err == nil {
282286
log.Info("Your new key was generated", "address", acc.Address)
283287
log.Warn("Please backup your key file!", "path", acc.URL.Path)
@@ -288,8 +292,11 @@ func (s *PrivateAccountAPI) NewAccount(password string) (common.Address, error)
288292
}
289293

290294
// fetchKeystore retrieves the encrypted keystore from the account manager.
291-
func fetchKeystore(am *accounts.Manager) *keystore.KeyStore {
292-
return am.Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)
295+
func fetchKeystore(am *accounts.Manager) (*keystore.KeyStore, error) {
296+
if ks := am.Backends(keystore.KeyStoreType); len(ks) > 0 {
297+
return ks[0].(*keystore.KeyStore), nil
298+
}
299+
return nil, errors.New("local keystore not used")
293300
}
294301

295302
// ImportRawKey stores the given hex encoded ECDSA key into the key directory,
@@ -299,7 +306,11 @@ func (s *PrivateAccountAPI) ImportRawKey(privkey string, password string) (commo
299306
if err != nil {
300307
return common.Address{}, err
301308
}
302-
acc, err := fetchKeystore(s.am).ImportECDSA(key, password)
309+
ks, err := fetchKeystore(s.am)
310+
if err != nil {
311+
return common.Address{}, err
312+
}
313+
acc, err := ks.ImportECDSA(key, password)
303314
return acc.Address, err
304315
}
305316

@@ -323,7 +334,11 @@ func (s *PrivateAccountAPI) UnlockAccount(ctx context.Context, addr common.Addre
323334
} else {
324335
d = time.Duration(*duration) * time.Second
325336
}
326-
err := fetchKeystore(s.am).TimedUnlock(accounts.Account{Address: addr}, password, d)
337+
ks, err := fetchKeystore(s.am)
338+
if err != nil {
339+
return false, err
340+
}
341+
err = ks.TimedUnlock(accounts.Account{Address: addr}, password, d)
327342
if err != nil {
328343
log.Warn("Failed account unlock attempt", "address", addr, "err", err)
329344
}
@@ -332,7 +347,10 @@ func (s *PrivateAccountAPI) UnlockAccount(ctx context.Context, addr common.Addre
332347

333348
// LockAccount will lock the account associated with the given address when it's unlocked.
334349
func (s *PrivateAccountAPI) LockAccount(addr common.Address) bool {
335-
return fetchKeystore(s.am).Lock(addr) == nil
350+
if ks, err := fetchKeystore(s.am); err == nil {
351+
return ks.Lock(addr) == nil
352+
}
353+
return false
336354
}
337355

338356
// signTransaction sets defaults and signs the given transaction

0 commit comments

Comments
 (0)