accounts, ethkey: use fixed keyfile format generate file name

This commit is contained in:
rjl493456442 2018-02-04 11:10:32 +08:00
parent 59a852e418
commit 87d4d33f49
4 changed files with 10 additions and 16 deletions

View file

@ -171,7 +171,7 @@ func storeNewKey(ks keyStore, rand io.Reader, auth string) (*Key, accounts.Accou
if err != nil { if err != nil {
return nil, accounts.Account{}, err 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 { if err := ks.StoreKey(a.URL.Path, key, auth); err != nil {
zeroKey(key.PrivateKey) zeroKey(key.PrivateKey)
return nil, a, err return nil, a, err
@ -201,9 +201,9 @@ func writeKeyFile(file string, content []byte) error {
return os.Rename(f.Name(), file) return os.Rename(f.Name(), file)
} }
// keyFileName implements the naming convention for keyfiles: // KeyFileName implements the naming convention for keyfiles:
// UTC--<created_at UTC ISO8601>-<address hex> // UTC--<created_at UTC ISO8601>-<address hex>
func keyFileName(keyAddr common.Address) string { func KeyFileName(keyAddr common.Address) string {
ts := time.Now().UTC() ts := time.Now().UTC()
return fmt.Sprintf("UTC--%s--%s", toISO8601(ts), hex.EncodeToString(keyAddr[:])) return fmt.Sprintf("UTC--%s--%s", toISO8601(ts), hex.EncodeToString(keyAddr[:]))
} }

View file

@ -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) { 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 { if err := ks.storage.StoreKey(a.URL.Path, key, passphrase); err != nil {
return accounts.Account{}, err return accounts.Account{}, err
} }

View file

@ -38,7 +38,7 @@ func importPreSaleKey(keyStore keyStore, keyJSON []byte, password string) (accou
return accounts.Account{}, nil, err return accounts.Account{}, nil, err
} }
key.Id = uuid.NewRandom() 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) err = keyStore.StoreKey(a.URL.Path, key, password)
return a, key, err return a, key, err
} }

View file

@ -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 { 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 privateKey *ecdsa.PrivateKey
var err error var err error
if file := ctx.String("privatekey"); file != "" { 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) 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. // Store the file to disk.
if err := os.MkdirAll(filepath.Dir(keyfilepath), 0700); err != nil { if err := os.MkdirAll(filepath.Dir(keyfilepath), 0700); err != nil {
utils.Fatalf("Could not create directory %s", filepath.Dir(keyfilepath)) utils.Fatalf("Could not create directory %s", filepath.Dir(keyfilepath))