From fc17a527bc2bd07fc30e16d161059a441042d5f1 Mon Sep 17 00:00:00 2001
From: zelig <viktor.tron@gmail.com>
Date: Thu, 2 Jul 2015 22:58:00 +0100
Subject: [PATCH] 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

---
 accounts/account_manager.go    |   6 +-
 accounts/accounts_test.go      |   5 +-
 cmd/geth/js_test.go            |   2 +-
 crypto/crypto.go               |   2 +-
 crypto/key_store_passphrase.go |  36 ++++-----
 crypto/key_store_plain.go      | 135 ++++++++++++++++++++-------------
 6 files changed, 105 insertions(+), 81 deletions(-)

diff --git a/accounts/account_manager.go b/accounts/account_manager.go
index 4519c84209..eb2672a7de 100644
--- a/accounts/account_manager.go
+++ b/accounts/account_manager.go
@@ -26,7 +26,7 @@ This abstracts part of a user's interaction with an account she controls.
 It's not an abstraction of core Ethereum accounts data type / logic -
 for that see the core processing code of blocks / txs.
 
-Currently this is pretty much a passthrough to the KeyStore2 interface,
+Currently this is pretty much a passthrough to the KeyStore interface,
 and accounts persistence is derived from stored keys' addresses
 
 */
@@ -54,7 +54,7 @@ type Account struct {
 }
 
 type Manager struct {
-	keyStore crypto.KeyStore2
+	keyStore crypto.KeyStore
 	unlocked map[common.Address]*unlocked
 	mutex    sync.RWMutex
 }
@@ -64,7 +64,7 @@ type unlocked struct {
 	abort chan struct{}
 }
 
-func NewManager(keyStore crypto.KeyStore2) *Manager {
+func NewManager(keyStore crypto.KeyStore) *Manager {
 	return &Manager{
 		keyStore: keyStore,
 		unlocked: make(map[common.Address]*unlocked),
diff --git a/accounts/accounts_test.go b/accounts/accounts_test.go
index 8bd70880c1..4b94b78fd4 100644
--- a/accounts/accounts_test.go
+++ b/accounts/accounts_test.go
@@ -98,10 +98,11 @@ func TestOverrideUnlock(t *testing.T) {
 	if err != ErrLocked {
 		t.Fatal("Signing should've failed with ErrLocked timeout expired, got ", err)
 	}
-
 }
 
-func tmpKeyStore(t *testing.T, new func(string) crypto.KeyStore2) (string, crypto.KeyStore2) {
+//
+
+func tmpKeyStore(t *testing.T, new func(string) crypto.KeyStore) (string, crypto.KeyStore) {
 	d, err := ioutil.TempDir("", "eth-keystore-test")
 	if err != nil {
 		t.Fatal(err)
diff --git a/cmd/geth/js_test.go b/cmd/geth/js_test.go
index 61e85d3995..480f77c917 100644
--- a/cmd/geth/js_test.go
+++ b/cmd/geth/js_test.go
@@ -9,7 +9,6 @@ import (
 	"runtime"
 	"strconv"
 	"testing"
-	"time"
 
 	"github.com/ethereum/go-ethereum/accounts"
 	"github.com/ethereum/go-ethereum/common"
@@ -128,6 +127,7 @@ func TestNodeInfo(t *testing.T) {
 	}
 	defer ethereum.Stop()
 	defer os.RemoveAll(tmp)
+
 	want := `{"DiscPort":0,"IP":"0.0.0.0","ListenAddr":"","Name":"test","NodeID":"4cb2fc32924e94277bf94b5e4c983beedb2eabd5a0bc941db32202735c6625d020ca14a5963d1738af43b6ac0a711d61b1a06de931a499fe2aa0b1a132a902b5","NodeUrl":"enode://4cb2fc32924e94277bf94b5e4c983beedb2eabd5a0bc941db32202735c6625d020ca14a5963d1738af43b6ac0a711d61b1a06de931a499fe2aa0b1a132a902b5@0.0.0.0:0","TCPPort":0,"Td":"131072"}`
 	checkEvalJSON(t, repl, `admin.nodeInfo`, want)
 }
diff --git a/crypto/crypto.go b/crypto/crypto.go
index 153bbbc5d4..deef67415c 100644
--- a/crypto/crypto.go
+++ b/crypto/crypto.go
@@ -209,7 +209,7 @@ func ImportBlockTestKey(privKeyBytes []byte) error {
 }
 
 // creates a Key and stores that in the given KeyStore by decrypting a presale key JSON
-func ImportPreSaleKey(keyStore KeyStore2, keyJSON []byte, password string) (*Key, error) {
+func ImportPreSaleKey(keyStore KeyStore, keyJSON []byte, password string) (*Key, error) {
 	key, err := decryptPreSaleKey(keyJSON, password)
 	if err != nil {
 		return nil, err
diff --git a/crypto/key_store_passphrase.go b/crypto/key_store_passphrase.go
index 2000a24386..d26e3407f2 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)
-	if err != nil {
-		return nil, nil, err
-	}
-
+func decryptKeyFromFile(ks keyStorePassphrase, keyAddr common.Address, auth string) (keyBytes []byte, keyId []byte, err error) {
 	m := make(map[string]interface{})
-	err = json.Unmarshal(fileContent, &m)
+	err = getKey(ks.keysDirPath, keyAddr, &m)
+	if err != nil {
+		fmt.Printf("get key error: %v\n", err)
+		return
+	}
 
 	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)
 	}
diff --git a/crypto/key_store_plain.go b/crypto/key_store_plain.go
index e3150e9a9f..d785fdf68c 100644
--- a/crypto/key_store_plain.go
+++ b/crypto/key_store_plain.go
@@ -31,18 +31,15 @@ import (
 	"io/ioutil"
 	"os"
 	"path/filepath"
-	"sort"
-	"syscall"
 	"time"
 
 	"github.com/ethereum/go-ethereum/common"
 )
 
-// TODO: rename to KeyStore when replacing existing KeyStore
-type KeyStore2 interface {
+type KeyStore interface {
 	// create new key using io.Reader entropy source and optionally using auth string
 	GenerateNewKey(io.Reader, string) (*Key, error)
-	GetKey(common.Address, string) (*Key, error) // key from addr and auth string
+	GetKey(common.Address, string) (*Key, error) // get key from addr and auth string
 	GetKeyAddresses() ([]common.Address, error)  // get all addresses
 	StoreKey(*Key, string) error                 // store key optionally using auth string
 	DeleteKey(common.Address, string) error      // delete key by addr and auth string
@@ -52,7 +49,7 @@ type keyStorePlain struct {
 	keysDirPath string
 }
 
-func NewKeyStorePlain(path string) KeyStore2 {
+func NewKeyStorePlain(path string) KeyStore {
 	return &keyStorePlain{path}
 }
 
@@ -60,7 +57,7 @@ func (ks keyStorePlain) GenerateNewKey(rand io.Reader, auth string) (key *Key, e
 	return GenerateNewKeyDefault(ks, rand, auth)
 }
 
-func GenerateNewKeyDefault(ks KeyStore2, rand io.Reader, auth string) (key *Key, err error) {
+func GenerateNewKeyDefault(ks KeyStore, rand io.Reader, auth string) (key *Key, err error) {
 	defer func() {
 		if r := recover(); r != nil {
 			err = fmt.Errorf("GenerateNewKey error: %v", r)
@@ -72,81 +69,111 @@ func GenerateNewKeyDefault(ks KeyStore2, rand io.Reader, auth string) (key *Key,
 }
 
 func (ks keyStorePlain) GetKey(keyAddr common.Address, auth string) (key *Key, err error) {
-	fileContent, err := GetKeyFile(ks.keysDirPath, keyAddr)
-	if err != nil {
-		return nil, err
-	}
-
 	key = new(Key)
-	err = json.Unmarshal(fileContent, key)
-	return key, err
+	err = getKey(ks.keysDirPath, keyAddr, key)
+	return
+}
+
+func getKey(keysDirPath string, keyAddr common.Address, content interface{}) (err error) {
+	fileContent, err := getKeyFile(keysDirPath, keyAddr)
+	if err != nil {
+		return
+	}
+	return json.Unmarshal(fileContent, content)
 }
 
 func (ks keyStorePlain) GetKeyAddresses() (addresses []common.Address, err error) {
-	return GetKeyAddresses(ks.keysDirPath)
+	return getKeyAddresses(ks.keysDirPath)
 }
 
 func (ks keyStorePlain) StoreKey(key *Key, auth string) (err error) {
 	keyJSON, err := json.Marshal(key)
 	if err != nil {
-		return err
+		return
 	}
-	err = WriteKeyFile(key.Address, ks.keysDirPath, keyJSON)
-	return err
+	err = writeKeyFile(key.Address, ks.keysDirPath, keyJSON)
+	return
 }
 
 func (ks keyStorePlain) DeleteKey(keyAddr common.Address, auth string) (err error) {
-	keyDirPath := filepath.Join(ks.keysDirPath, keyAddr.Hex())
-	err = os.RemoveAll(keyDirPath)
-	return err
+	return deleteKey(ks.keysDirPath, keyAddr)
 }
 
-func GetKeyFile(keysDirPath string, keyAddr common.Address) (fileContent []byte, err error) {
-	fileName := hex.EncodeToString(keyAddr[:])
-	return ioutil.ReadFile(filepath.Join(keysDirPath, fileName, fileName))
+func deleteKey(keysDirPath string, keyAddr common.Address) (err error) {
+	var keyFilePath string
+	keyFilePath, err = getKeyFilePath(keysDirPath, keyAddr)
+	if err == nil {
+		err = os.Remove(keyFilePath)
+	}
+	return
 }
 
-func WriteKeyFile(addr common.Address, keysDirPath string, content []byte) (err error) {
-	addrHex := hex.EncodeToString(addr[:])
-	keyDirPath := filepath.Join(keysDirPath, addrHex)
-	keyFilePath := filepath.Join(keyDirPath, addrHex)
-	err = os.MkdirAll(keyDirPath, 0700) // read, write and dir search for user
+func getKeyFilePath(keysDirPath string, keyAddr common.Address) (keyFilePath string, err error) {
+	addrHex := hex.EncodeToString(keyAddr[:])
+	matches, err := filepath.Glob(filepath.Join(keysDirPath, fmt.Sprintf("*--%s", addrHex)))
+	if len(matches) > 0 {
+		if err == nil {
+			keyFilePath = matches[len(matches)-1]
+		}
+		return
+	}
+	keyFilePath = filepath.Join(keysDirPath, addrHex, addrHex)
+	_, err = os.Stat(keyFilePath)
+	return
+}
+
+func getKeyFile(keysDirPath string, keyAddr common.Address) (fileContent []byte, err error) {
+	var keyFilePath string
+	keyFilePath, err = getKeyFilePath(keysDirPath, keyAddr)
+	if err == nil {
+		fileContent, err = ioutil.ReadFile(keyFilePath)
+	}
+	return
+}
+
+func writeKeyFile(addr common.Address, keysDirPath string, content []byte) (err error) {
+	filename := keyFileName(addr)
+	// read, write and dir search for user
+	err = os.MkdirAll(keysDirPath, 0700)
 	if err != nil {
 		return err
 	}
-	return ioutil.WriteFile(keyFilePath, content, 0600) // read, write for user
+	// read, write for user
+	return ioutil.WriteFile(filepath.Join(keysDirPath, filename), content, 0600)
 }
 
-func GetKeyAddresses(keysDirPath string) (addresses []common.Address, err error) {
+// keyFilePath implements the naming convention for keyfiles:
+// UTC--<created_at UTC ISO8601>-<address hex>
+func keyFileName(keyAddr common.Address) string {
+	ts := time.Now().UTC()
+	return fmt.Sprintf("UTC--%s--%s", toISO8601(ts), hex.EncodeToString(keyAddr[:]))
+}
+
+func toISO8601(t time.Time) string {
+	var tz string
+	name, offset := t.Zone()
+	if name == "UTC" {
+		tz = "Z"
+	} else {
+		tz = fmt.Sprintf("%03d00", offset/3600)
+	}
+	return fmt.Sprintf("%04d-%02d-%02dT%02d:%02d:%02d.%09d%s", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), tz)
+}
+
+func getKeyAddresses(keysDirPath string) (addresses []common.Address, err error) {
 	fileInfos, err := ioutil.ReadDir(keysDirPath)
 	if err != nil {
 		return nil, err
 	}
-	var kfis keyFileInfos
 	for _, fileInfo := range fileInfos {
-		stat := fileInfo.Sys().(*syscall.Stat_t)
-		ctime := time.Unix(int64(stat.Ctimespec.Sec), int64(stat.Ctimespec.Nsec))
-		kfis = append(kfis, keyFileInfo{fileInfo.Name(), ctime})
-	}
-	sort.Sort(kfis)
-	for _, kfi := range kfis {
-		address, err := hex.DecodeString(kfi.name)
-		if err != nil {
-			continue
+		filename := fileInfo.Name()
+		if len(filename) >= 40 {
+			addr := filename[len(filename)-40 : len(filename)]
+			address, err := hex.DecodeString(addr)
+			if err == nil {
+				addresses = append(addresses, common.BytesToAddress(address))
+			}
 		}
-		addresses = append(addresses, common.BytesToAddress(address))
 	}
 	return addresses, err
 }
-
-type keyFileInfo struct {
-	name  string
-	ctime time.Time
-}
-type keyFileInfos []keyFileInfo
-
-func (a keyFileInfos) Len() int      { return len(a) }
-func (a keyFileInfos) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
-func (a keyFileInfos) Less(i, j int) bool {
-	return a[i].ctime.Before(a[j].ctime)
-}