aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Holst Swende <martin@swende.se>2017-11-21 00:39:53 +0800
committerFelix Lange <fjl@users.noreply.github.com>2017-11-21 00:39:53 +0800
commitbedf6f40aff0dd14bfab533a37c329ddc9a4bdd5 (patch)
tree69450c1c69b418ad9040d58cf9d2e2584bd27e07
parentb4f2e4de8fcbaab22f1b8ac1faa0c99517bd9405 (diff)
downloaddexon-bedf6f40aff0dd14bfab533a37c329ddc9a4bdd5.tar
dexon-bedf6f40aff0dd14bfab533a37c329ddc9a4bdd5.tar.gz
dexon-bedf6f40aff0dd14bfab533a37c329ddc9a4bdd5.tar.bz2
dexon-bedf6f40aff0dd14bfab533a37c329ddc9a4bdd5.tar.lz
dexon-bedf6f40aff0dd14bfab533a37c329ddc9a4bdd5.tar.xz
dexon-bedf6f40aff0dd14bfab533a37c329ddc9a4bdd5.tar.zst
dexon-bedf6f40aff0dd14bfab533a37c329ddc9a4bdd5.zip
cmd/geth: make geth account new faster with many keys (#15529)
-rw-r--r--accounts/keystore/keystore_passphrase.go7
-rw-r--r--cmd/geth/accountcmd.go21
-rw-r--r--node/config.go36
3 files changed, 46 insertions, 18 deletions
diff --git a/accounts/keystore/keystore_passphrase.go b/accounts/keystore/keystore_passphrase.go
index 535608a60..eaec39f7d 100644
--- a/accounts/keystore/keystore_passphrase.go
+++ b/accounts/keystore/keystore_passphrase.go
@@ -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 {
diff --git a/cmd/geth/accountcmd.go b/cmd/geth/accountcmd.go
index 0f53c92b0..0db5c4ce0 100644
--- a/cmd/geth/accountcmd.go
+++ b/cmd/geth/accountcmd.go
@@ -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
}
diff --git a/node/config.go b/node/config.go
index be9e21b4f..1ee02d896 100644
--- a/node/config.go
+++ b/node/config.go
@@ -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
+ keydir 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
}