mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-17 09:23:48 +00:00
accounts/keystore: Fix geth account new to operate directly on a keystore file
This commit is contained in:
parent
7b95cca56c
commit
8c752cccfd
3 changed files with 68 additions and 5 deletions
|
|
@ -28,6 +28,7 @@ package keystore
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/aes"
|
"crypto/aes"
|
||||||
|
crand "crypto/rand"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
@ -90,6 +91,23 @@ 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) {
|
||||||
|
key, err := newKey(crand.Reader)
|
||||||
|
if err != nil {
|
||||||
|
return err, common.Address{}
|
||||||
|
}
|
||||||
|
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 {
|
||||||
keyjson, err := EncryptKey(key, auth, ks.scryptN, ks.scryptP)
|
keyjson, err := EncryptKey(key, auth, ks.scryptN, ks.scryptP)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,7 @@ 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"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -291,15 +292,29 @@ 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 {
|
||||||
stack, _ := makeConfigNode(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))
|
|
||||||
|
|
||||||
ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)
|
cfg := gethConfig{}
|
||||||
account, err := ks.NewAccount(password)
|
// 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 := node.ResolveAccountConfig(&cfg.Node)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Fatalf("Failed to create account: %v", err)
|
utils.Fatalf("Failed to create account: %v", err)
|
||||||
}
|
}
|
||||||
fmt.Printf("Address: {%x}\n", account.Address)
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
utils.Fatalf("Failed to create account: %v", err)
|
||||||
|
}
|
||||||
|
fmt.Printf("Address: {%x}\n", address)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -359,6 +359,36 @@ func (c *Config) parsePersistentNodes(path string) []*discover.Node {
|
||||||
}
|
}
|
||||||
return nodes
|
return nodes
|
||||||
}
|
}
|
||||||
|
// ResolveAccountConfig determines the settings for scrypt and keydirectory
|
||||||
|
func ResolveAccountConfig(conf *Config) (int, int, string, error) {
|
||||||
|
scryptN := keystore.StandardScryptN
|
||||||
|
scryptP := keystore.StandardScryptP
|
||||||
|
if conf.UseLightweightKDF {
|
||||||
|
scryptN = keystore.LightScryptN
|
||||||
|
scryptP = keystore.LightScryptP
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
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)
|
||||||
|
} else {
|
||||||
|
keydir, err = filepath.Abs(conf.KeyStoreDir)
|
||||||
|
}
|
||||||
|
case conf.KeyStoreDir != "":
|
||||||
|
keydir, err = filepath.Abs(conf.KeyStoreDir)
|
||||||
|
default:
|
||||||
|
// There is no datadir.
|
||||||
|
keydir, err = ioutil.TempDir("", "go-ethereum-keystore")
|
||||||
|
}
|
||||||
|
return scryptN, scryptP, keydir, err
|
||||||
|
}
|
||||||
|
|
||||||
func makeAccountManager(conf *Config) (*accounts.Manager, string, error) {
|
func makeAccountManager(conf *Config) (*accounts.Manager, string, error) {
|
||||||
scryptN := keystore.StandardScryptN
|
scryptN := keystore.StandardScryptN
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue