accounts, cmd, node: Added parameter to disable keystore file write

This commit is contained in:
Konrad Janica 2018-01-20 23:20:58 -08:00
parent 02aeb3d766
commit a5a6981db6
8 changed files with 64 additions and 6 deletions

View file

@ -53,6 +53,8 @@ type keyStore interface {
GetKey(addr common.Address, filename string, auth string) (*Key, error) GetKey(addr common.Address, filename string, auth string) (*Key, error)
// Writes and encrypts the key. // Writes and encrypts the key.
StoreKey(filename string, k *Key, auth string) error StoreKey(filename string, k *Key, auth string) error
// Creates and encrypts the key and returns in JSON
CreateKeyStore(k *Key, auth string) ([]byte, error)
// Joins filename with the key directory unless it is already absolute. // Joins filename with the key directory unless it is already absolute.
JoinPath(filename string) string JoinPath(filename string) string
} }
@ -179,6 +181,19 @@ func storeNewKey(ks keyStore, rand io.Reader, auth string) (*Key, accounts.Accou
return key, a, err return key, a, err
} }
func newKeyStore(ks keyStore, rand io.Reader, auth string) (*Key, []byte, error) {
key, err := newKey(rand)
if err != nil {
return nil, []byte{}, err
}
json, err := ks.CreateKeyStore(key, auth)
if err != nil {
zeroKey(key.PrivateKey)
return nil, []byte{}, err
}
return key, json, err
}
func writeKeyFile(file string, content []byte) error { func writeKeyFile(file string, content []byte) error {
// Create the keystore directory with appropriate permissions // Create the keystore directory with appropriate permissions
// in case it is not present yet. // in case it is not present yet.

View file

@ -98,13 +98,27 @@ func StoreKey(dir, auth string, scryptN, scryptP int) (common.Address, error) {
} }
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 := ks.CreateKeyStore(key, auth)
if err != nil { if err != nil {
return err return err
} }
return writeKeyFile(filename, keyjson) return writeKeyFile(filename, keyjson)
} }
// CreateKeyStore generates a key, encrypts with 'auth'
func CreateKeyStore(auth string, scryptN, scryptP int) ([]byte, error) {
_, json, err := newKeyStore(&keyStorePassphrase{scryptN: scryptN, scryptP: scryptP}, crand.Reader, auth)
return json, err
}
func (ks keyStorePassphrase) CreateKeyStore(key *Key, auth string) ([]byte, error) {
keyjson, err := EncryptKey(key, auth, ks.scryptN, ks.scryptP)
if err != nil {
return []byte{}, err
}
return keyjson, nil
}
func (ks keyStorePassphrase) JoinPath(filename string) string { func (ks keyStorePassphrase) JoinPath(filename string) string {
if filepath.IsAbs(filename) { if filepath.IsAbs(filename) {
return filename return filename

View file

@ -53,6 +53,14 @@ func (ks keyStorePlain) StoreKey(filename string, key *Key, auth string) error {
return writeKeyFile(filename, content) return writeKeyFile(filename, content)
} }
func (ks keyStorePlain) CreateKeyStore(key *Key, auth string) ([]byte, error) {
content, err := json.Marshal(key)
if err != nil {
return []byte{}, err
}
return content, nil
}
func (ks keyStorePlain) JoinPath(filename string) string { func (ks keyStorePlain) JoinPath(filename string) string {
if filepath.IsAbs(filename) { if filepath.IsAbs(filename) {
return filename return filename

View file

@ -110,6 +110,7 @@ Print a short summary of all accounts`,
utils.KeyStoreDirFlag, utils.KeyStoreDirFlag,
utils.PasswordFileFlag, utils.PasswordFileFlag,
utils.LightKDFFlag, utils.LightKDFFlag,
utils.WriteKeyStoreOnlyToStdout,
}, },
Description: ` Description: `
geth account new geth account new
@ -307,12 +308,20 @@ 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))
address, err := keystore.StoreKey(keydir, password, scryptN, scryptP) if ctx.GlobalBool(utils.WriteKeyStoreOnlyToStdout.Name) {
keystore, err := keystore.CreateKeyStore(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)
}
fmt.Printf("%s\n", keystore)
} else {
address, err := keystore.StoreKey(keydir, password, scryptN, scryptP)
if err != nil {
utils.Fatalf("Failed to create account: %v", err)
}
fmt.Printf("Address: {%x}\n", address)
} }
fmt.Printf("Address: {%x}\n", address)
return nil return nil
} }

View file

@ -60,6 +60,7 @@ var (
utils.BootnodesV5Flag, utils.BootnodesV5Flag,
utils.DataDirFlag, utils.DataDirFlag,
utils.KeyStoreDirFlag, utils.KeyStoreDirFlag,
utils.WriteKeyStoreOnlyToStdout,
utils.NoUSBFlag, utils.NoUSBFlag,
utils.DashboardEnabledFlag, utils.DashboardEnabledFlag,
utils.DashboardAddrFlag, utils.DashboardAddrFlag,

View file

@ -135,6 +135,7 @@ var AppHelpFlagGroups = []flagGroup{
Flags: []cli.Flag{ Flags: []cli.Flag{
utils.UnlockedAccountFlag, utils.UnlockedAccountFlag,
utils.PasswordFileFlag, utils.PasswordFileFlag,
utils.WriteKeyStoreOnlyToStdout,
}, },
}, },
{ {

View file

@ -122,6 +122,10 @@ var (
Name: "keystore", Name: "keystore",
Usage: "Directory for the keystore (default = inside the datadir)", Usage: "Directory for the keystore (default = inside the datadir)",
} }
WriteKeyStoreOnlyToStdout = cli.BoolFlag{
Name: "stdoutkeystore",
Usage: "Prevents writing keystore to file, instead outputs the keystore to standard output (stdout)",
}
NoUSBFlag = cli.BoolFlag{ NoUSBFlag = cli.BoolFlag{
Name: "nousb", Name: "nousb",
Usage: "Disables monitoring for and managing USB hardware wallets", Usage: "Disables monitoring for and managing USB hardware wallets",
@ -858,6 +862,9 @@ func SetNodeConfig(ctx *cli.Context, cfg *node.Config) {
if ctx.GlobalIsSet(KeyStoreDirFlag.Name) { if ctx.GlobalIsSet(KeyStoreDirFlag.Name) {
cfg.KeyStoreDir = ctx.GlobalString(KeyStoreDirFlag.Name) cfg.KeyStoreDir = ctx.GlobalString(KeyStoreDirFlag.Name)
} }
if ctx.GlobalIsSet(WriteKeyStoreOnlyToStdout.Name) {
cfg.WriteKeyStoreOnlyToStdout = ctx.GlobalBool(WriteKeyStoreOnlyToStdout.Name)
}
if ctx.GlobalIsSet(LightKDFFlag.Name) { if ctx.GlobalIsSet(LightKDFFlag.Name) {
cfg.UseLightweightKDF = ctx.GlobalBool(LightKDFFlag.Name) cfg.UseLightweightKDF = ctx.GlobalBool(LightKDFFlag.Name)
} }

View file

@ -78,6 +78,9 @@ type Config struct {
// is created by New and destroyed when the node is stopped. // is created by New and destroyed when the node is stopped.
KeyStoreDir string `toml:",omitempty"` KeyStoreDir string `toml:",omitempty"`
// Prevents writing keystore to file, instead outputs the keystore to standard output (stdout)
WriteKeyStoreOnlyToStdout bool `toml:",omitempty"`
// UseLightweightKDF lowers the memory and CPU requirements of the key store // UseLightweightKDF lowers the memory and CPU requirements of the key store
// scrypt KDF at the expense of security. // scrypt KDF at the expense of security.
UseLightweightKDF bool `toml:",omitempty"` UseLightweightKDF bool `toml:",omitempty"`