aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/key.go
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/key.go')
-rw-r--r--crypto/key.go75
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