Skip to content

Commit bedf6f4

Browse files
holimanfjl
authored andcommitted
cmd/geth: make geth account new faster with many keys (#15529)
1 parent b4f2e4d commit bedf6f4

File tree

3 files changed

+46
-18
lines changed

3 files changed

+46
-18
lines changed

accounts/keystore/keystore_passphrase.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ package keystore
2828
import (
2929
"bytes"
3030
"crypto/aes"
31+
crand "crypto/rand"
3132
"crypto/sha256"
3233
"encoding/hex"
3334
"encoding/json"
@@ -90,6 +91,12 @@ func (ks keyStorePassphrase) GetKey(addr common.Address, filename, auth string)
9091
return key, nil
9192
}
9293

94+
// StoreKey generates a key, encrypts with 'auth' and stores in the given directory
95+
func StoreKey(dir, auth string, scryptN, scryptP int) (common.Address, error) {
96+
_, a, err := storeNewKey(&keyStorePassphrase{dir, scryptN, scryptP}, crand.Reader, auth)
97+
return a.Address, err
98+
}
99+
93100
func (ks keyStorePassphrase) StoreKey(filename string, key *Key, auth string) error {
94101
keyjson, err := EncryptKey(key, auth, ks.scryptN, ks.scryptP)
95102
if err != nil {

cmd/geth/accountcmd.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,15 +291,28 @@ func ambiguousAddrRecovery(ks *keystore.KeyStore, err *keystore.AmbiguousAddrErr
291291

292292
// accountCreate creates a new account into the keystore defined by the CLI flags.
293293
func accountCreate(ctx *cli.Context) error {
294-
stack, _ := makeConfigNode(ctx)
294+
cfg := gethConfig{Node: defaultNodeConfig()}
295+
// Load config file.
296+
if file := ctx.GlobalString(configFileFlag.Name); file != "" {
297+
if err := loadConfig(file, &cfg); err != nil {
298+
utils.Fatalf("%v", err)
299+
}
300+
}
301+
utils.SetNodeConfig(ctx, &cfg.Node)
302+
scryptN, scryptP, keydir, err := cfg.Node.AccountConfig()
303+
304+
if err != nil {
305+
utils.Fatalf("Failed to read configuration: %v", err)
306+
}
307+
295308
password := getPassPhrase("Your new account is locked with a password. Please give a password. Do not forget this password.", true, 0, utils.MakePasswordList(ctx))
296309

297-
ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)
298-
account, err := ks.NewAccount(password)
310+
address, err := keystore.StoreKey(keydir, password, scryptN, scryptP)
311+
299312
if err != nil {
300313
utils.Fatalf("Failed to create account: %v", err)
301314
}
302-
fmt.Printf("Address: {%x}\n", account.Address)
315+
fmt.Printf("Address: {%x}\n", address)
303316
return nil
304317
}
305318

node/config.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -360,35 +360,43 @@ func (c *Config) parsePersistentNodes(path string) []*discover.Node {
360360
return nodes
361361
}
362362

363-
func makeAccountManager(conf *Config) (*accounts.Manager, string, error) {
363+
// AccountConfig determines the settings for scrypt and keydirectory
364+
func (c *Config) AccountConfig() (int, int, string, error) {
364365
scryptN := keystore.StandardScryptN
365366
scryptP := keystore.StandardScryptP
366-
if conf.UseLightweightKDF {
367+
if c.UseLightweightKDF {
367368
scryptN = keystore.LightScryptN
368369
scryptP = keystore.LightScryptP
369370
}
370371

371372
var (
372-
keydir string
373-
ephemeral string
374-
err error
373+
keydir string
374+
err error
375375
)
376376
switch {
377-
case filepath.IsAbs(conf.KeyStoreDir):
378-
keydir = conf.KeyStoreDir
379-
case conf.DataDir != "":
380-
if conf.KeyStoreDir == "" {
381-
keydir = filepath.Join(conf.DataDir, datadirDefaultKeyStore)
377+
case filepath.IsAbs(c.KeyStoreDir):
378+
keydir = c.KeyStoreDir
379+
case c.DataDir != "":
380+
if c.KeyStoreDir == "" {
381+
keydir = filepath.Join(c.DataDir, datadirDefaultKeyStore)
382382
} else {
383-
keydir, err = filepath.Abs(conf.KeyStoreDir)
383+
keydir, err = filepath.Abs(c.KeyStoreDir)
384384
}
385-
case conf.KeyStoreDir != "":
386-
keydir, err = filepath.Abs(conf.KeyStoreDir)
387-
default:
385+
case c.KeyStoreDir != "":
386+
keydir, err = filepath.Abs(c.KeyStoreDir)
387+
}
388+
return scryptN, scryptP, keydir, err
389+
}
390+
391+
func makeAccountManager(conf *Config) (*accounts.Manager, string, error) {
392+
scryptN, scryptP, keydir, err := conf.AccountConfig()
393+
var ephemeral string
394+
if keydir == "" {
388395
// There is no datadir.
389396
keydir, err = ioutil.TempDir("", "go-ethereum-keystore")
390397
ephemeral = keydir
391398
}
399+
392400
if err != nil {
393401
return nil, "", err
394402
}

0 commit comments

Comments
 (0)