/
keyv4.go
140 lines (117 loc) · 3.49 KB
/
keyv4.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright 2020 The klaytn Authors
// This file is part of the klaytn library.
//
// The klaytn library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The klaytn library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the klaytn library. If not, see <http://www.gnu.org/licenses/>.
package keystore
import (
"crypto/ecdsa"
"encoding/hex"
"encoding/json"
"github.com/klaytn/klaytn/common"
"github.com/klaytn/klaytn/crypto"
"github.com/pborman/uuid"
)
type KeyV4 struct {
Id uuid.UUID // Version 4 "random" for unique id not derived from key data
// to simplify lookups we also store the address
Address common.Address
// We only store privkey as pubkey/address can be derived from it.
PrivateKeys [][]*ecdsa.PrivateKey
}
type plainKeyJSONV4 struct {
Address string `json:"address"`
PrivateKeys [][]string `json:"privatekeys"`
Id string `json:"id"`
Version int `json:"version"`
}
type encryptedKeyJSONV4 struct {
Address string `json:"address"`
Keyring [][]cryptoJSON `json:"keyring"`
Id string `json:"id"`
Version int `json:"version"`
}
type encryptedKeyJSONV4Single struct {
Address string `json:"address"`
Keyring []cryptoJSON `json:"keyring"`
Id string `json:"id"`
Version int `json:"version"`
}
func (k *KeyV4) MarshalJSON() (j []byte, err error) {
privateKeys := make([][]string, len(k.PrivateKeys))
for i, keys := range k.PrivateKeys {
privateKeys[i] = make([]string, len(keys))
for j, key := range keys {
privateKeys[i][j] = common.Bytes2Hex(crypto.FromECDSA(key))
}
}
jStruct := plainKeyJSONV4{
Address: hex.EncodeToString(k.Address.Bytes()),
PrivateKeys: privateKeys,
Id: k.Id.String(),
Version: 4,
}
j, err = json.Marshal(jStruct)
return j, err
}
func (k *KeyV4) UnmarshalJSON(j []byte) (err error) {
keyJSON := new(plainKeyJSONV4)
if err := json.Unmarshal(j, keyJSON); err != nil {
return err
}
k.Id = uuid.Parse(keyJSON.Id)
addr, err := hex.DecodeString(keyJSON.Address)
if err != nil {
return err
}
k.PrivateKeys = make([][]*ecdsa.PrivateKey, len(keyJSON.PrivateKeys))
for i, keys := range keyJSON.PrivateKeys {
k.PrivateKeys[i] = make([]*ecdsa.PrivateKey, len(keys))
for j, key := range keys {
k.PrivateKeys[i][j], err = crypto.HexToECDSA(key)
if err != nil {
return err
}
}
}
k.Address = common.BytesToAddress(addr)
return nil
}
func (k *KeyV4) GetId() uuid.UUID {
return k.Id
}
func (k *KeyV4) GetAddress() common.Address {
return k.Address
}
func (k *KeyV4) GetPrivateKey() *ecdsa.PrivateKey {
if len(k.PrivateKeys) == 0 || len(k.PrivateKeys[0]) == 0 {
return nil
}
return k.PrivateKeys[0][0]
}
func (k *KeyV4) GetPrivateKeys() [][]*ecdsa.PrivateKey {
return k.PrivateKeys
}
func (k *KeyV4) GetPrivateKeysWithRole(role int) []*ecdsa.PrivateKey {
if len(k.PrivateKeys) <= role {
return nil
}
return k.PrivateKeys[role]
}
func (k *KeyV4) ResetPrivateKey() {
for _, keys := range k.PrivateKeys {
for _, key := range keys {
zeroKey(key)
}
}
}