Skip to content

Commit f98e002

Browse files
author
Gustav Simonsson
committed
Address pull request comments; key header and hex encoding
* Remove key header from unencrypted key file format and replace it with a version field * Change encoding of bytes in key files from base64 to hex
1 parent 313eec3 commit f98e002

File tree

3 files changed

+67
-31
lines changed

3 files changed

+67
-31
lines changed

crypto/key.go

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,18 @@ package crypto
2626
import (
2727
"bytes"
2828
"crypto/ecdsa"
29+
"encoding/hex"
2930
"encoding/json"
3031
"io"
3132

3233
"code.google.com/p/go-uuid/uuid"
3334
"github.com/ethereum/go-ethereum/common"
3435
)
3536

37+
const (
38+
version = "1"
39+
)
40+
3641
type Key struct {
3742
Id uuid.UUID // Version 4 "random" for unique id not derived from key data
3843
// to simplify lookups we also store the address
@@ -43,29 +48,31 @@ type Key struct {
4348
}
4449

4550
type plainKeyJSON struct {
46-
Id []byte
47-
Address []byte
48-
PrivateKey []byte
51+
Version string
52+
Id string
53+
Address string
54+
PrivateKey string
4955
}
5056

5157
type encryptedKeyJSON struct {
52-
Id []byte
53-
Address []byte
58+
Version string
59+
Id string
60+
Address string
5461
Crypto cipherJSON
5562
}
5663

5764
type cipherJSON struct {
58-
MAC []byte
59-
Salt []byte
60-
IV []byte
65+
MAC string
66+
Salt string
67+
IV string
6168
KeyHeader keyHeaderJSON
62-
CipherText []byte
69+
CipherText string
6370
}
6471

6572
type keyHeaderJSON struct {
6673
Version string
6774
Kdf string
68-
KdfParams scryptParamsJSON // TODO: make more generic?
75+
KdfParams scryptParamsJSON
6976
}
7077

7178
type scryptParamsJSON struct {
@@ -78,9 +85,10 @@ type scryptParamsJSON struct {
7885

7986
func (k *Key) MarshalJSON() (j []byte, err error) {
8087
jStruct := plainKeyJSON{
81-
k.Id,
82-
k.Address.Bytes(),
83-
FromECDSA(k.PrivateKey),
88+
version,
89+
k.Id.String(),
90+
hex.EncodeToString(k.Address[:]),
91+
hex.EncodeToString(FromECDSA(k.PrivateKey)),
8492
}
8593
j, err = json.Marshal(jStruct)
8694
return j, err
@@ -94,12 +102,22 @@ func (k *Key) UnmarshalJSON(j []byte) (err error) {
94102
}
95103

96104
u := new(uuid.UUID)
97-
*u = keyJSON.Id
105+
*u = uuid.Parse(keyJSON.Id)
98106
k.Id = *u
99-
k.Address = common.BytesToAddress(keyJSON.Address)
100-
k.PrivateKey = ToECDSA(keyJSON.PrivateKey)
107+
addr, err := hex.DecodeString(keyJSON.Address)
108+
if err != nil {
109+
return err
110+
}
111+
112+
privkey, err := hex.DecodeString(keyJSON.PrivateKey)
113+
if err != nil {
114+
return err
115+
}
116+
117+
k.Address = common.BytesToAddress(addr)
118+
k.PrivateKey = ToECDSA(privkey)
101119

102-
return err
120+
return nil
103121
}
104122

105123
func NewKeyFromECDSA(privateKeyECDSA *ecdsa.PrivateKey) *Key {

crypto/key_store_passphrase.go

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ import (
6868
"bytes"
6969
"crypto/aes"
7070
"crypto/cipher"
71+
"encoding/hex"
7172
"encoding/json"
7273
"errors"
7374
"io"
@@ -164,15 +165,16 @@ func (ks keyStorePassphrase) StoreKey(key *Key, auth string) (err error) {
164165
mac := Sha3(keyHeaderJSONStr, derivedKey[16:32], cipherText)
165166

166167
cipherStruct := cipherJSON{
167-
mac,
168-
salt,
169-
iv,
168+
hex.EncodeToString(mac),
169+
hex.EncodeToString(salt),
170+
hex.EncodeToString(iv),
170171
keyHeaderJSON,
171-
cipherText,
172+
hex.EncodeToString(cipherText),
172173
}
173174
keyStruct := encryptedKeyJSON{
174-
key.Id,
175-
key.Address.Bytes(),
175+
version,
176+
key.Id.String(),
177+
hex.EncodeToString(key.Address[:]),
176178
cipherStruct,
177179
}
178180
keyJSON, err := json.Marshal(keyStruct)
@@ -190,7 +192,7 @@ func (ks keyStorePassphrase) DeleteKey(keyAddr common.Address, auth string) (err
190192
return err
191193
}
192194

193-
keyDirPath := filepath.Join(ks.keysDirPath, keyAddr.Hex())
195+
keyDirPath := filepath.Join(ks.keysDirPath, hex.EncodeToString(keyAddr[:]))
194196
return os.RemoveAll(keyDirPath)
195197
}
196198

@@ -203,12 +205,28 @@ func DecryptKey(ks keyStorePassphrase, keyAddr common.Address, auth string) (key
203205
keyProtected := new(encryptedKeyJSON)
204206
err = json.Unmarshal(fileContent, keyProtected)
205207

206-
keyId = keyProtected.Id
207-
mac := keyProtected.Crypto.MAC
208-
salt := keyProtected.Crypto.Salt
209-
iv := keyProtected.Crypto.IV
208+
keyId = uuid.Parse(keyProtected.Id)
209+
210+
mac, err := hex.DecodeString(keyProtected.Crypto.MAC)
211+
if err != nil {
212+
return nil, nil, err
213+
}
214+
215+
salt, err := hex.DecodeString(keyProtected.Crypto.Salt)
216+
if err != nil {
217+
return nil, nil, err
218+
}
219+
220+
iv, err := hex.DecodeString(keyProtected.Crypto.IV)
221+
if err != nil {
222+
return nil, nil, err
223+
}
224+
210225
keyHeader := keyProtected.Crypto.KeyHeader
211-
cipherText := keyProtected.Crypto.CipherText
226+
cipherText, err := hex.DecodeString(keyProtected.Crypto.CipherText)
227+
if err != nil {
228+
return nil, nil, err
229+
}
212230

213231
// used in MAC
214232
keyHeaderJSONStr, err := json.Marshal(keyHeader)

crypto/key_store_plain.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,12 @@ func (ks keyStorePlain) DeleteKey(keyAddr common.Address, auth string) (err erro
9898
}
9999

100100
func GetKeyFile(keysDirPath string, keyAddr common.Address) (fileContent []byte, err error) {
101-
fileName := keyAddr.Hex()
101+
fileName := hex.EncodeToString(keyAddr[:])
102102
return ioutil.ReadFile(filepath.Join(keysDirPath, fileName, fileName))
103103
}
104104

105105
func WriteKeyFile(addr common.Address, keysDirPath string, content []byte) (err error) {
106-
addrHex := addr.Hex()
106+
addrHex := hex.EncodeToString(addr[:])
107107
keyDirPath := filepath.Join(keysDirPath, addrHex)
108108
keyFilePath := filepath.Join(keyDirPath, addrHex)
109109
err = os.MkdirAll(keyDirPath, 0700) // read, write and dir search for user

0 commit comments

Comments
 (0)