accounts/keystore: address review-feedback

This commit is contained in:
Martin Holst Swende 2017-11-20 15:42:49 +01:00
parent 1adadea161
commit bb22dfe02f
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
3 changed files with 25 additions and 35 deletions

View file

@ -91,21 +91,10 @@ func (ks keyStorePassphrase) GetKey(addr common.Address, filename, auth string)
return key, nil return key, nil
} }
func StoreKey(dir, auth string, scryptN, scryptP int) (error, common.Address) { // StoreKey generates a key, encrypts with 'auth' and stores in the given directory
key, err := newKey(crand.Reader) func StoreKey(dir, auth string, scryptN, scryptP int) (common.Address, error) {
if err != nil { _, a, err := storeNewKey(&keyStorePassphrase{dir, scryptN, scryptP}, crand.Reader, auth)
return err, common.Address{} return a.Address, err
}
keyjson, err := EncryptKey(key, auth, scryptN, scryptP)
if err != nil {
return err, common.Address{}
}
fullpath := filepath.Join(dir, keyFileName(key.Address))
err = writeKeyFile(fullpath, keyjson)
if err != nil {
return err, common.Address{}
}
return nil, key.Address
} }
func (ks keyStorePassphrase) StoreKey(filename string, key *Key, auth string) error { func (ks keyStorePassphrase) StoreKey(filename string, key *Key, auth string) error {

View file

@ -26,7 +26,6 @@ import (
"github.com/ethereum/go-ethereum/console" "github.com/ethereum/go-ethereum/console"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/node"
"gopkg.in/urfave/cli.v1" "gopkg.in/urfave/cli.v1"
) )
@ -292,7 +291,6 @@ func ambiguousAddrRecovery(ks *keystore.KeyStore, err *keystore.AmbiguousAddrErr
// accountCreate creates a new account into the keystore defined by the CLI flags. // accountCreate creates a new account into the keystore defined by the CLI flags.
func accountCreate(ctx *cli.Context) error { func accountCreate(ctx *cli.Context) error {
cfg := gethConfig{} cfg := gethConfig{}
// Load config file. // Load config file.
if file := ctx.GlobalString(configFileFlag.Name); file != "" { if file := ctx.GlobalString(configFileFlag.Name); file != "" {
@ -301,7 +299,7 @@ func accountCreate(ctx *cli.Context) error {
} }
} }
utils.SetNodeConfig(ctx, &cfg.Node) utils.SetNodeConfig(ctx, &cfg.Node)
scryptN, scryptP, keydir, err := node.ResolveAccountConfig(&cfg.Node) scryptN, scryptP, keydir, err := cfg.Node.AccountConfig()
if err != nil { if err != nil {
utils.Fatalf("Failed to create account: %v", err) utils.Fatalf("Failed to create account: %v", err)
@ -309,7 +307,7 @@ func accountCreate(ctx *cli.Context) error {
password := getPassPhrase("Your new account is locked with a password. Please give a password. Do not forget this password.", true, 0, utils.MakePasswordList(ctx)) password := getPassPhrase("Your new account is locked with a password. Please give a password. Do not forget this password.", true, 0, utils.MakePasswordList(ctx))
err, address := keystore.StoreKey(keydir, password, scryptN, scryptP) address, err := keystore.StoreKey(keydir, password, scryptN, scryptP)
if err != nil { if err != nil {
utils.Fatalf("Failed to create account: %v", err) utils.Fatalf("Failed to create account: %v", err)

View file

@ -360,11 +360,11 @@ func (c *Config) parsePersistentNodes(path string) []*discover.Node {
return nodes return nodes
} }
// ResolveAccountConfig determines the settings for scrypt and keydirectory // AccountConfig determines the settings for scrypt and keydirectory
func ResolveAccountConfig(conf *Config) (int, int, string, error) { func (c *Config) AccountConfig() (int, int, string, error) {
scryptN := keystore.StandardScryptN scryptN := keystore.StandardScryptN
scryptP := keystore.StandardScryptP scryptP := keystore.StandardScryptP
if conf.UseLightweightKDF { if c.UseLightweightKDF {
scryptN = keystore.LightScryptN scryptN = keystore.LightScryptN
scryptP = keystore.LightScryptP scryptP = keystore.LightScryptP
} }
@ -374,25 +374,28 @@ func ResolveAccountConfig(conf *Config) (int, int, string, error) {
err error err error
) )
switch { switch {
case filepath.IsAbs(conf.KeyStoreDir): case filepath.IsAbs(c.KeyStoreDir):
keydir = conf.KeyStoreDir keydir = c.KeyStoreDir
case conf.DataDir != "": case c.DataDir != "":
if conf.KeyStoreDir == "" { if c.KeyStoreDir == "" {
keydir = filepath.Join(conf.DataDir, datadirDefaultKeyStore) keydir = filepath.Join(c.DataDir, datadirDefaultKeyStore)
} else { } else {
keydir, err = filepath.Abs(conf.KeyStoreDir) keydir, err = filepath.Abs(c.KeyStoreDir)
} }
case conf.KeyStoreDir != "": case c.KeyStoreDir != "":
keydir, err = filepath.Abs(conf.KeyStoreDir) keydir, err = filepath.Abs(c.KeyStoreDir)
default:
// There is no datadir.
keydir, err = ioutil.TempDir("", "go-ethereum-keystore")
} }
return scryptN, scryptP, keydir, err return scryptN, scryptP, keydir, err
} }
func makeAccountManager(conf *Config) (*accounts.Manager, string, error) { func makeAccountManager(conf *Config) (*accounts.Manager, string, error) {
scryptN, scryptP, keydir, err := ResolveAccountConfig(conf) 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 { if err != nil {
return nil, "", err return nil, "", err
@ -418,5 +421,5 @@ func makeAccountManager(conf *Config) (*accounts.Manager, string, error) {
backends = append(backends, trezorhub) backends = append(backends, trezorhub)
} }
} }
return accounts.NewManager(backends...), keydir, nil return accounts.NewManager(backends...), ephemeral, nil
} }