cmd/geth: make geth account new faster with many keys (#15529)

This commit is contained in:
Martin Holst Swende 2017-11-20 17:39:53 +01:00 committed by Felix Lange
parent b4f2e4de8f
commit bedf6f40af
3 changed files with 46 additions and 18 deletions

@ -28,6 +28,7 @@ package keystore
import (
"bytes"
"crypto/aes"
crand "crypto/rand"
"crypto/sha256"
"encoding/hex"
"encoding/json"
@ -90,6 +91,12 @@ func (ks keyStorePassphrase) GetKey(addr common.Address, filename, auth string)
return key, nil
}
// StoreKey generates a key, encrypts with 'auth' and stores in the given directory
func StoreKey(dir, auth string, scryptN, scryptP int) (common.Address, error) {
_, a, err := storeNewKey(&keyStorePassphrase{dir, scryptN, scryptP}, crand.Reader, auth)
return a.Address, err
}
func (ks keyStorePassphrase) StoreKey(filename string, key *Key, auth string) error {
keyjson, err := EncryptKey(key, auth, ks.scryptN, ks.scryptP)
if err != nil {

@ -291,15 +291,28 @@ func ambiguousAddrRecovery(ks *keystore.KeyStore, err *keystore.AmbiguousAddrErr
// accountCreate creates a new account into the keystore defined by the CLI flags.
func accountCreate(ctx *cli.Context) error {
stack, _ := makeConfigNode(ctx)
cfg := gethConfig{Node: defaultNodeConfig()}
// Load config file.
if file := ctx.GlobalString(configFileFlag.Name); file != "" {
if err := loadConfig(file, &cfg); err != nil {
utils.Fatalf("%v", err)
}
}
utils.SetNodeConfig(ctx, &cfg.Node)
scryptN, scryptP, keydir, err := cfg.Node.AccountConfig()
if err != nil {
utils.Fatalf("Failed to read configuration: %v", err)
}
password := 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)
account, err := ks.NewAccount(password)
address, err := keystore.StoreKey(keydir, password, scryptN, scryptP)
if err != nil {
utils.Fatalf("Failed to create account: %v", err)
}
fmt.Printf("Address: {%x}\n", account.Address)
fmt.Printf("Address: {%x}\n", address)
return nil
}

@ -360,35 +360,43 @@ func (c *Config) parsePersistentNodes(path string) []*discover.Node {
return nodes
}
func makeAccountManager(conf *Config) (*accounts.Manager, string, error) {
// AccountConfig determines the settings for scrypt and keydirectory
func (c *Config) AccountConfig() (int, int, string, error) {
scryptN := keystore.StandardScryptN
scryptP := keystore.StandardScryptP
if conf.UseLightweightKDF {
if c.UseLightweightKDF {
scryptN = keystore.LightScryptN
scryptP = keystore.LightScryptP
}
var (
keydir string
ephemeral string
err error
)
switch {
case filepath.IsAbs(conf.KeyStoreDir):
keydir = conf.KeyStoreDir
case conf.DataDir != "":
if conf.KeyStoreDir == "" {
keydir = filepath.Join(conf.DataDir, datadirDefaultKeyStore)
case filepath.IsAbs(c.KeyStoreDir):
keydir = c.KeyStoreDir
case c.DataDir != "":
if c.KeyStoreDir == "" {
keydir = filepath.Join(c.DataDir, datadirDefaultKeyStore)
} else {
keydir, err = filepath.Abs(conf.KeyStoreDir)
keydir, err = filepath.Abs(c.KeyStoreDir)
}
case conf.KeyStoreDir != "":
keydir, err = filepath.Abs(conf.KeyStoreDir)
default:
case c.KeyStoreDir != "":
keydir, err = filepath.Abs(c.KeyStoreDir)
}
return scryptN, scryptP, keydir, err
}
func makeAccountManager(conf *Config) (*accounts.Manager, string, error) {
scryptN, scryptP, keydir, err := conf.AccountConfig()
var ephemeral string
if keydir == "" {
// There is no datadir.
keydir, err = ioutil.TempDir("", "go-ethereum-keystore")
ephemeral = keydir
}
if err != nil {
return nil, "", err
}