Skip to content
Snippets Groups Projects
Commit 3100e5d4 authored by Sean Young's avatar Sean Young
Browse files

key json should store keys bytes in hex


Signed-off-by: default avatarSean Young <sean.young@monax.io>
parent 8570cbcb
No related branches found
No related tags found
No related merge requests found
...@@ -39,14 +39,14 @@ const ( ...@@ -39,14 +39,14 @@ const (
type keyJSON struct { type keyJSON struct {
CurveType string CurveType string
Address string Address string
PublicKey []byte PublicKey string
AddressHash string AddressHash string
PrivateKey privateKeyJSON PrivateKey privateKeyJSON
} }
type privateKeyJSON struct { type privateKeyJSON struct {
Crypto string Crypto string
Plain []byte `json:",omitempty"` Plain string `json:",omitempty"`
Salt []byte `json:",omitempty"` Salt []byte `json:",omitempty"`
Nonce []byte `json:",omitempty"` Nonce []byte `json:",omitempty"`
CipherText []byte `json:",omitempty"` CipherText []byte `json:",omitempty"`
...@@ -55,10 +55,10 @@ type privateKeyJSON struct { ...@@ -55,10 +55,10 @@ type privateKeyJSON struct {
func (k *Key) MarshalJSON() (j []byte, err error) { func (k *Key) MarshalJSON() (j []byte, err error) {
jStruct := keyJSON{ jStruct := keyJSON{
CurveType: k.CurveType.String(), CurveType: k.CurveType.String(),
Address: hex.EncodeToString(k.Address[:]), Address: hex.EncodeUpperToString(k.Address[:]),
PublicKey: k.Pubkey(), PublicKey: hex.EncodeUpperToString(k.Pubkey()),
AddressHash: k.PublicKey.AddressHashType(), AddressHash: k.PublicKey.AddressHashType(),
PrivateKey: privateKeyJSON{Crypto: CryptoNone, Plain: k.PrivateKey.RawBytes()}, PrivateKey: privateKeyJSON{Crypto: CryptoNone, Plain: hex.EncodeUpperToString(k.PrivateKey.RawBytes())},
} }
j, err = json.Marshal(jStruct) j, err = json.Marshal(jStruct)
return j, err return j, err
...@@ -77,7 +77,11 @@ func (k *Key) UnmarshalJSON(j []byte) (err error) { ...@@ -77,7 +77,11 @@ func (k *Key) UnmarshalJSON(j []byte) (err error) {
if err != nil { if err != nil {
curveType = crypto.CurveTypeEd25519 curveType = crypto.CurveTypeEd25519
} }
k2, err := NewKeyFromPriv(curveType, keyJ.PrivateKey.Plain) privKey, err := hex.DecodeString(keyJ.PrivateKey.Plain)
if err != nil {
return err
}
k2, err := NewKeyFromPriv(curveType, privKey)
if err != nil { if err != nil {
return err return err
} }
...@@ -201,9 +205,13 @@ func DecryptKey(passphrase string, keyProtected *keyJSON) (*Key, error) { ...@@ -201,9 +205,13 @@ func DecryptKey(passphrase string, keyProtected *keyJSON) (*Key, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
pubKey, err := hex.DecodeString(keyProtected.PublicKey)
if err != nil {
return nil, err
}
plainText, err := gcm.Open(nil, nonce, cipherText, nil) plainText, err := gcm.Open(nil, nonce, cipherText, nil)
if err != nil { if err != nil {
pkey, _ := NewKeyFromPub(curveType, keyProtected.PublicKey) pkey, _ := NewKeyFromPub(curveType, pubKey)
return pkey, err return pkey, err
} }
address, err := crypto.AddressFromHexString(keyProtected.Address) address, err := crypto.AddressFromHexString(keyProtected.Address)
...@@ -289,8 +297,8 @@ func (ks KeyStore) StoreKeyEncrypted(passphrase string, key *Key) error { ...@@ -289,8 +297,8 @@ func (ks KeyStore) StoreKeyEncrypted(passphrase string, key *Key) error {
} }
keyStruct := keyJSON{ keyStruct := keyJSON{
CurveType: key.CurveType.String(), CurveType: key.CurveType.String(),
Address: strings.ToUpper(hex.EncodeToString(key.Address[:])), Address: hex.EncodeUpperToString(key.Address[:]),
PublicKey: key.Pubkey(), PublicKey: hex.EncodeUpperToString(key.Pubkey()),
AddressHash: key.PublicKey.AddressHashType(), AddressHash: key.PublicKey.AddressHashType(),
PrivateKey: cipherStruct, PrivateKey: cipherStruct,
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment