diff options
author | Gustav Simonsson <gustav.simonsson@gmail.com> | 2015-04-03 03:14:25 +0800 |
---|---|---|
committer | Gustav Simonsson <gustav.simonsson@gmail.com> | 2015-05-12 23:19:39 +0800 |
commit | da9fe951da4005761a014316c46771d628dc058e (patch) | |
tree | d5344172ed915b4890d66c3cf3b4181e30589471 /crypto | |
parent | 6b23094cff77d7e485e0a2ae5698884f63c87ce7 (diff) | |
download | go-tangerine-da9fe951da4005761a014316c46771d628dc058e.tar.gz go-tangerine-da9fe951da4005761a014316c46771d628dc058e.tar.zst go-tangerine-da9fe951da4005761a014316c46771d628dc058e.zip |
Use common.Address type for accounts.Address
Diffstat (limited to 'crypto')
-rw-r--r-- | crypto/crypto.go | 6 | ||||
-rw-r--r-- | crypto/key.go | 9 | ||||
-rw-r--r-- | crypto/key_store_passphrase.go | 14 | ||||
-rw-r--r-- | crypto/key_store_plain.go | 29 |
4 files changed, 30 insertions, 28 deletions
diff --git a/crypto/crypto.go b/crypto/crypto.go index 6fc5bfd36..2b1628124 100644 --- a/crypto/crypto.go +++ b/crypto/crypto.go @@ -231,13 +231,13 @@ func decryptPreSaleKey(fileContent []byte, password string) (key *Key, err error ecKey := ToECDSA(ethPriv) key = &Key{ Id: nil, - Address: PubkeyToAddress(ecKey.PublicKey), + Address: common.BytesToAddress(PubkeyToAddress(ecKey.PublicKey)), PrivateKey: ecKey, } - derivedAddr := common.Bytes2Hex(key.Address) + derivedAddr := hex.EncodeToString(key.Address.Bytes()) // needed because .Hex() gives leading "0x" expectedAddr := preSaleKeyStruct.EthAddr if derivedAddr != expectedAddr { - err = errors.New("decrypted addr not equal to expected addr") + err = errors.New(fmt.Sprintf("decrypted addr not equal to expected addr ", derivedAddr, expectedAddr)) } return key, err } diff --git a/crypto/key.go b/crypto/key.go index 654d9e83d..5e1f3637e 100644 --- a/crypto/key.go +++ b/crypto/key.go @@ -30,12 +30,13 @@ import ( "io" "code.google.com/p/go-uuid/uuid" + "github.com/ethereum/go-ethereum/common" ) type Key struct { Id uuid.UUID // Version 4 "random" for unique id not derived from key data // to simplify lookups we also store the address - Address []byte + Address common.Address // we only store privkey as pubkey/address can be derived from it // privkey in this struct is always in plaintext PrivateKey *ecdsa.PrivateKey @@ -63,7 +64,7 @@ type encryptedKeyJSON struct { func (k *Key) MarshalJSON() (j []byte, err error) { jStruct := plainKeyJSON{ k.Id, - k.Address, + k.Address.Bytes(), FromECDSA(k.PrivateKey), } j, err = json.Marshal(jStruct) @@ -80,7 +81,7 @@ func (k *Key) UnmarshalJSON(j []byte) (err error) { u := new(uuid.UUID) *u = keyJSON.Id k.Id = *u - k.Address = keyJSON.Address + k.Address = common.BytesToAddress(keyJSON.Address) k.PrivateKey = ToECDSA(keyJSON.PrivateKey) return err @@ -90,7 +91,7 @@ func NewKeyFromECDSA(privateKeyECDSA *ecdsa.PrivateKey) *Key { id := uuid.NewRandom() key := &Key{ Id: id, - Address: PubkeyToAddress(privateKeyECDSA.PublicKey), + Address: common.BytesToAddress(PubkeyToAddress(privateKeyECDSA.PublicKey)), PrivateKey: privateKeyECDSA, } return key diff --git a/crypto/key_store_passphrase.go b/crypto/key_store_passphrase.go index 4cfb1851b..739483d9f 100644 --- a/crypto/key_store_passphrase.go +++ b/crypto/key_store_passphrase.go @@ -68,7 +68,6 @@ import ( "bytes" "crypto/aes" "crypto/cipher" - "encoding/hex" "encoding/json" "errors" "io" @@ -76,6 +75,7 @@ import ( "path/filepath" "code.google.com/p/go-uuid/uuid" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto/randentropy" "golang.org/x/crypto/scrypt" ) @@ -100,7 +100,7 @@ func (ks keyStorePassphrase) GenerateNewKey(rand io.Reader, auth string) (key *K return GenerateNewKeyDefault(ks, rand, auth) } -func (ks keyStorePassphrase) GetKey(keyAddr []byte, auth string) (key *Key, err error) { +func (ks keyStorePassphrase) GetKey(keyAddr common.Address, auth string) (key *Key, err error) { keyBytes, keyId, err := DecryptKey(ks, keyAddr, auth) if err != nil { return nil, err @@ -113,7 +113,7 @@ func (ks keyStorePassphrase) GetKey(keyAddr []byte, auth string) (key *Key, err return key, err } -func (ks keyStorePassphrase) GetKeyAddresses() (addresses [][]byte, err error) { +func (ks keyStorePassphrase) GetKeyAddresses() (addresses []common.Address, err error) { return GetKeyAddresses(ks.keysDirPath) } @@ -150,7 +150,7 @@ func (ks keyStorePassphrase) StoreKey(key *Key, auth string) (err error) { } keyStruct := encryptedKeyJSON{ key.Id, - key.Address, + key.Address.Bytes(), cipherStruct, } keyJSON, err := json.Marshal(keyStruct) @@ -161,18 +161,18 @@ func (ks keyStorePassphrase) StoreKey(key *Key, auth string) (err error) { return WriteKeyFile(key.Address, ks.keysDirPath, keyJSON) } -func (ks keyStorePassphrase) DeleteKey(keyAddr []byte, auth string) (err error) { +func (ks keyStorePassphrase) DeleteKey(keyAddr common.Address, auth string) (err error) { // only delete if correct passphrase is given _, _, err = DecryptKey(ks, keyAddr, auth) if err != nil { return err } - keyDirPath := filepath.Join(ks.keysDirPath, hex.EncodeToString(keyAddr)) + keyDirPath := filepath.Join(ks.keysDirPath, keyAddr.Hex()) return os.RemoveAll(keyDirPath) } -func DecryptKey(ks keyStorePassphrase, keyAddr []byte, auth string) (keyBytes []byte, keyId []byte, err error) { +func DecryptKey(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 diff --git a/crypto/key_store_plain.go b/crypto/key_store_plain.go index 581968d7c..2ade11985 100644 --- a/crypto/key_store_plain.go +++ b/crypto/key_store_plain.go @@ -27,6 +27,7 @@ import ( "encoding/hex" "encoding/json" "fmt" + "github.com/ethereum/go-ethereum/common" "io" "io/ioutil" "os" @@ -37,10 +38,10 @@ import ( type KeyStore2 interface { // create new key using io.Reader entropy source and optionally using auth string GenerateNewKey(io.Reader, string) (*Key, error) - GetKey([]byte, string) (*Key, error) // key from addr and auth string - GetKeyAddresses() ([][]byte, error) // get all addresses - StoreKey(*Key, string) error // store key optionally using auth string - DeleteKey([]byte, string) error // delete key by addr and auth string + GetKey(common.Address, string) (*Key, error) // 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 } type keyStorePlain struct { @@ -66,7 +67,7 @@ func GenerateNewKeyDefault(ks KeyStore2, rand io.Reader, auth string) (key *Key, return key, err } -func (ks keyStorePlain) GetKey(keyAddr []byte, auth string) (key *Key, err error) { +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 @@ -77,7 +78,7 @@ func (ks keyStorePlain) GetKey(keyAddr []byte, auth string) (key *Key, err error return key, err } -func (ks keyStorePlain) GetKeyAddresses() (addresses [][]byte, err error) { +func (ks keyStorePlain) GetKeyAddresses() (addresses []common.Address, err error) { return GetKeyAddresses(ks.keysDirPath) } @@ -90,19 +91,19 @@ func (ks keyStorePlain) StoreKey(key *Key, auth string) (err error) { return err } -func (ks keyStorePlain) DeleteKey(keyAddr []byte, auth string) (err error) { - keyDirPath := filepath.Join(ks.keysDirPath, hex.EncodeToString(keyAddr)) +func (ks keyStorePlain) DeleteKey(keyAddr common.Address, auth string) (err error) { + keyDirPath := filepath.Join(ks.keysDirPath, keyAddr.Hex()) err = os.RemoveAll(keyDirPath) return err } -func GetKeyFile(keysDirPath string, keyAddr []byte) (fileContent []byte, err error) { - fileName := hex.EncodeToString(keyAddr) +func GetKeyFile(keysDirPath string, keyAddr common.Address) (fileContent []byte, err error) { + fileName := keyAddr.Hex() return ioutil.ReadFile(filepath.Join(keysDirPath, fileName, fileName)) } -func WriteKeyFile(addr []byte, keysDirPath string, content []byte) (err error) { - addrHex := hex.EncodeToString(addr) +func WriteKeyFile(addr common.Address, keysDirPath string, content []byte) (err error) { + addrHex := addr.Hex() keyDirPath := filepath.Join(keysDirPath, addrHex) keyFilePath := filepath.Join(keyDirPath, addrHex) err = os.MkdirAll(keyDirPath, 0700) // read, write and dir search for user @@ -112,7 +113,7 @@ func WriteKeyFile(addr []byte, keysDirPath string, content []byte) (err error) { return ioutil.WriteFile(keyFilePath, content, 0600) // read, write for user } -func GetKeyAddresses(keysDirPath string) (addresses [][]byte, err error) { +func GetKeyAddresses(keysDirPath string) (addresses []common.Address, err error) { fileInfos, err := ioutil.ReadDir(keysDirPath) if err != nil { return nil, err @@ -122,7 +123,7 @@ func GetKeyAddresses(keysDirPath string) (addresses [][]byte, err error) { if err != nil { continue } - addresses = append(addresses, address) + addresses = append(addresses, common.BytesToAddress(address)) } return addresses, err } |