diff options
author | Jeffrey Wilcke <jeffrey@ethereum.org> | 2015-05-13 01:00:35 +0800 |
---|---|---|
committer | Jeffrey Wilcke <jeffrey@ethereum.org> | 2015-05-13 01:00:35 +0800 |
commit | d6357aa616715df1e98cfb90c3aa5372e15cc24b (patch) | |
tree | 3f1792e7d71a82de0ceb6047eaf87f2f72aa82ff /crypto/key.go | |
parent | 58d6ec689ff44232cd5d6a7cbbaad2d7a2cb44bd (diff) | |
parent | e389585f1f2e77fd7cd507499015bf3754581e4e (diff) | |
download | go-tangerine-d6357aa616715df1e98cfb90c3aa5372e15cc24b.tar go-tangerine-d6357aa616715df1e98cfb90c3aa5372e15cc24b.tar.gz go-tangerine-d6357aa616715df1e98cfb90c3aa5372e15cc24b.tar.bz2 go-tangerine-d6357aa616715df1e98cfb90c3aa5372e15cc24b.tar.lz go-tangerine-d6357aa616715df1e98cfb90c3aa5372e15cc24b.tar.xz go-tangerine-d6357aa616715df1e98cfb90c3aa5372e15cc24b.tar.zst go-tangerine-d6357aa616715df1e98cfb90c3aa5372e15cc24b.zip |
Merge pull request #631 from Gustav-Simonsson/improve_key_store_crypto
Improve key store crypto
Diffstat (limited to 'crypto/key.go')
-rw-r--r-- | crypto/key.go | 75 |
1 files changed, 55 insertions, 20 deletions
diff --git a/crypto/key.go b/crypto/key.go index 0b84bfec1..0c5ce4254 100644 --- a/crypto/key.go +++ b/crypto/key.go @@ -26,44 +26,69 @@ package crypto import ( "bytes" "crypto/ecdsa" + "encoding/hex" "encoding/json" "io" "code.google.com/p/go-uuid/uuid" + "github.com/ethereum/go-ethereum/common" +) + +const ( + version = "1" ) 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 } type plainKeyJSON struct { - Id []byte - Address []byte - PrivateKey []byte + Address string `json:"address"` + PrivateKey string `json:"privatekey"` + Id string `json:"id"` + Version string `json:"version"` } -type cipherJSON struct { - Salt []byte - IV []byte - CipherText []byte +type encryptedKeyJSON struct { + Address string `json:"address"` + Crypto cryptoJSON + Id string `json:"id"` + Version string `json:"version"` } -type encryptedKeyJSON struct { - Id []byte - Address []byte - Crypto cipherJSON +type cryptoJSON struct { + Cipher string `json:"cipher"` + CipherText string `json:"ciphertext"` + CipherParams cipherparamsJSON `json:"cipherparams"` + KDF string `json:"kdf"` + KDFParams scryptParamsJSON `json:"kdfparams"` + MAC string `json:"mac"` + Version string `json:"version"` +} + +type cipherparamsJSON struct { + IV string `json:"iv"` +} + +type scryptParamsJSON struct { + N int `json:"n"` + R int `json:"r"` + P int `json:"p"` + DkLen int `json:"dklen"` + Salt string `json:"salt"` } func (k *Key) MarshalJSON() (j []byte, err error) { jStruct := plainKeyJSON{ - k.Id, - k.Address, - FromECDSA(k.PrivateKey), + hex.EncodeToString(k.Address[:]), + hex.EncodeToString(FromECDSA(k.PrivateKey)), + k.Id.String(), + version, } j, err = json.Marshal(jStruct) return j, err @@ -77,19 +102,29 @@ func (k *Key) UnmarshalJSON(j []byte) (err error) { } u := new(uuid.UUID) - *u = keyJSON.Id + *u = uuid.Parse(keyJSON.Id) k.Id = *u - k.Address = keyJSON.Address - k.PrivateKey = ToECDSA(keyJSON.PrivateKey) + addr, err := hex.DecodeString(keyJSON.Address) + if err != nil { + return err + } + + privkey, err := hex.DecodeString(keyJSON.PrivateKey) + if err != nil { + return err + } + + k.Address = common.BytesToAddress(addr) + k.PrivateKey = ToECDSA(privkey) - return err + return nil } 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 |