diff options
author | zelig <viktor.tron@gmail.com> | 2015-07-03 05:58:00 +0800 |
---|---|---|
committer | zelig <viktor.tron@gmail.com> | 2015-07-03 10:19:32 +0800 |
commit | fc17a527bc2bd07fc30e16d161059a441042d5f1 (patch) | |
tree | 9a3c5b52c16043f61c1ddf73311ae50a2061a039 /crypto/key_store_passphrase.go | |
parent | a4df9d74eabb3bef8449744c4fe966572586dc39 (diff) | |
download | go-tangerine-fc17a527bc2bd07fc30e16d161059a441042d5f1.tar go-tangerine-fc17a527bc2bd07fc30e16d161059a441042d5f1.tar.gz go-tangerine-fc17a527bc2bd07fc30e16d161059a441042d5f1.tar.bz2 go-tangerine-fc17a527bc2bd07fc30e16d161059a441042d5f1.tar.lz go-tangerine-fc17a527bc2bd07fc30e16d161059a441042d5f1.tar.xz go-tangerine-fc17a527bc2bd07fc30e16d161059a441042d5f1.tar.zst go-tangerine-fc17a527bc2bd07fc30e16d161059a441042d5f1.zip |
fix account ordering
* chronological order of creation
* new naming scheme keystore/UTC--<created_at UTC ISO8601>-<address hex>
* KeyStore2 -> KeyStore
* backward compatibility
* refactor keyStore methods
Diffstat (limited to 'crypto/key_store_passphrase.go')
-rw-r--r-- | crypto/key_store_passphrase.go | 34 |
1 files changed, 15 insertions, 19 deletions
diff --git a/crypto/key_store_passphrase.go b/crypto/key_store_passphrase.go index 2000a2438..d26e3407f 100644 --- a/crypto/key_store_passphrase.go +++ b/crypto/key_store_passphrase.go @@ -41,8 +41,6 @@ import ( "errors" "fmt" "io" - "os" - "path/filepath" "reflect" "code.google.com/p/go-uuid/uuid" @@ -65,7 +63,7 @@ type keyStorePassphrase struct { keysDirPath string } -func NewKeyStorePassphrase(path string) KeyStore2 { +func NewKeyStorePassphrase(path string) KeyStore { return &keyStorePassphrase{path} } @@ -74,7 +72,7 @@ func (ks keyStorePassphrase) GenerateNewKey(rand io.Reader, auth string) (key *K } func (ks keyStorePassphrase) GetKey(keyAddr common.Address, auth string) (key *Key, err error) { - keyBytes, keyId, err := DecryptKeyFromFile(ks, keyAddr, auth) + keyBytes, keyId, err := decryptKeyFromFile(ks, keyAddr, auth) if err != nil { return nil, err } @@ -87,7 +85,7 @@ func (ks keyStorePassphrase) GetKey(keyAddr common.Address, auth string) (key *K } func (ks keyStorePassphrase) GetKeyAddresses() (addresses []common.Address, err error) { - return GetKeyAddresses(ks.keysDirPath) + return getKeyAddresses(ks.keysDirPath) } func (ks keyStorePassphrase) StoreKey(key *Key, auth string) (err error) { @@ -139,42 +137,40 @@ func (ks keyStorePassphrase) StoreKey(key *Key, auth string) (err error) { return err } - return WriteKeyFile(key.Address, ks.keysDirPath, keyJSON) + return writeKeyFile(key.Address, ks.keysDirPath, keyJSON) } func (ks keyStorePassphrase) DeleteKey(keyAddr common.Address, auth string) (err error) { // only delete if correct passphrase is given - _, _, err = DecryptKeyFromFile(ks, keyAddr, auth) + _, _, err = decryptKeyFromFile(ks, keyAddr, auth) if err != nil { return err } - keyDirPath := filepath.Join(ks.keysDirPath, hex.EncodeToString(keyAddr[:])) - return os.RemoveAll(keyDirPath) + return deleteKey(ks.keysDirPath, keyAddr) } -func DecryptKeyFromFile(ks keyStorePassphrase, keyAddr common.Address, auth string) (keyBytes []byte, keyId []byte, err error) { - fileContent, err := GetKeyFile(ks.keysDirPath, keyAddr) +func decryptKeyFromFile(ks keyStorePassphrase, keyAddr common.Address, auth string) (keyBytes []byte, keyId []byte, err error) { + m := make(map[string]interface{}) + err = getKey(ks.keysDirPath, keyAddr, &m) if err != nil { - return nil, nil, err + fmt.Printf("get key error: %v\n", err) + return } - m := make(map[string]interface{}) - err = json.Unmarshal(fileContent, &m) - v := reflect.ValueOf(m["version"]) if v.Kind() == reflect.String && v.String() == "1" { k := new(encryptedKeyJSONV1) - err := json.Unmarshal(fileContent, k) + getKey(ks.keysDirPath, keyAddr, &k) if err != nil { - return nil, nil, err + return } return decryptKeyV1(k, auth) } else { k := new(encryptedKeyJSONV3) - err := json.Unmarshal(fileContent, k) + getKey(ks.keysDirPath, keyAddr, &k) if err != nil { - return nil, nil, err + return } return decryptKeyV3(k, auth) } |