From 87d4d33f499ad04d8efd490b5668920beb448b28 Mon Sep 17 00:00:00 2001 From: rjl493456442 Date: Sun, 4 Feb 2018 11:10:32 +0800 Subject: [PATCH] accounts, ethkey: use fixed keyfile format generate file name --- accounts/keystore/key.go | 6 +++--- accounts/keystore/keystore.go | 2 +- accounts/keystore/presale.go | 2 +- cmd/ethkey/generate.go | 16 +++++----------- 4 files changed, 10 insertions(+), 16 deletions(-) diff --git a/accounts/keystore/key.go b/accounts/keystore/key.go index 211fa863d7..af4de6c1cb 100644 --- a/accounts/keystore/key.go +++ b/accounts/keystore/key.go @@ -171,7 +171,7 @@ func storeNewKey(ks keyStore, rand io.Reader, auth string) (*Key, accounts.Accou if err != nil { return nil, accounts.Account{}, err } - a := accounts.Account{Address: key.Address, URL: accounts.URL{Scheme: KeyStoreScheme, Path: ks.JoinPath(keyFileName(key.Address))}} + a := accounts.Account{Address: key.Address, URL: accounts.URL{Scheme: KeyStoreScheme, Path: ks.JoinPath(KeyFileName(key.Address))}} if err := ks.StoreKey(a.URL.Path, key, auth); err != nil { zeroKey(key.PrivateKey) return nil, a, err @@ -201,9 +201,9 @@ func writeKeyFile(file string, content []byte) error { return os.Rename(f.Name(), file) } -// keyFileName implements the naming convention for keyfiles: +// KeyFileName implements the naming convention for keyfiles: // UTC---
-func keyFileName(keyAddr common.Address) string { +func KeyFileName(keyAddr common.Address) string { ts := time.Now().UTC() return fmt.Sprintf("UTC--%s--%s", toISO8601(ts), hex.EncodeToString(keyAddr[:])) } diff --git a/accounts/keystore/keystore.go b/accounts/keystore/keystore.go index 80ccd37419..753ddc6abf 100644 --- a/accounts/keystore/keystore.go +++ b/accounts/keystore/keystore.go @@ -454,7 +454,7 @@ func (ks *KeyStore) ImportECDSA(priv *ecdsa.PrivateKey, passphrase string) (acco } func (ks *KeyStore) importKey(key *Key, passphrase string) (accounts.Account, error) { - a := accounts.Account{Address: key.Address, URL: accounts.URL{Scheme: KeyStoreScheme, Path: ks.storage.JoinPath(keyFileName(key.Address))}} + a := accounts.Account{Address: key.Address, URL: accounts.URL{Scheme: KeyStoreScheme, Path: ks.storage.JoinPath(KeyFileName(key.Address))}} if err := ks.storage.StoreKey(a.URL.Path, key, passphrase); err != nil { return accounts.Account{}, err } diff --git a/accounts/keystore/presale.go b/accounts/keystore/presale.go index 1554294e14..9109547447 100644 --- a/accounts/keystore/presale.go +++ b/accounts/keystore/presale.go @@ -38,7 +38,7 @@ func importPreSaleKey(keyStore keyStore, keyJSON []byte, password string) (accou return accounts.Account{}, nil, err } key.Id = uuid.NewRandom() - a := accounts.Account{Address: key.Address, URL: accounts.URL{Scheme: KeyStoreScheme, Path: keyStore.JoinPath(keyFileName(key.Address))}} + a := accounts.Account{Address: key.Address, URL: accounts.URL{Scheme: KeyStoreScheme, Path: keyStore.JoinPath(KeyFileName(key.Address))}} err = keyStore.StoreKey(a.URL.Path, key, password) return a, key, err } diff --git a/cmd/ethkey/generate.go b/cmd/ethkey/generate.go index 6d57d17fb4..6f432d5371 100644 --- a/cmd/ethkey/generate.go +++ b/cmd/ethkey/generate.go @@ -54,17 +54,6 @@ If you want to encrypt an existing private key, it can be specified by setting }, }, Action: func(ctx *cli.Context) error { - // Check if keyfile path given and make sure it doesn't already exist. - keyfilepath := ctx.Args().First() - if keyfilepath == "" { - keyfilepath = defaultKeyfileName - } - if _, err := os.Stat(keyfilepath); err == nil { - utils.Fatalf("Keyfile already exists at %s.", keyfilepath) - } else if !os.IsNotExist(err) { - utils.Fatalf("Error checking if keyfile exists: %v", err) - } - var privateKey *ecdsa.PrivateKey var err error if file := ctx.String("privatekey"); file != "" { @@ -96,6 +85,11 @@ If you want to encrypt an existing private key, it can be specified by setting utils.Fatalf("Error encrypting key: %v", err) } + keyfilepath := ctx.Args().First() + if keyfilepath == "" { + // If the output keyfile name is not specified, generates the file name in a fixed format. + keyfilepath = keystore.KeyFileName(key.Address) + } // Store the file to disk. if err := os.MkdirAll(filepath.Dir(keyfilepath), 0700); err != nil { utils.Fatalf("Could not create directory %s", filepath.Dir(keyfilepath))