This repository has been archived by the owner on May 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathhd_key_pair.go
206 lines (173 loc) · 3.81 KB
/
hd_key_pair.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package wallet
import (
"crypto/ed25519"
"encoding/hex"
"encoding/json"
"code.vegaprotocol.io/vegawallet/crypto"
)
type HDKeyPair struct {
index uint32
publicKey *key
privateKey *key
meta []Meta
tainted bool
algo crypto.SignatureAlgorithm
}
type key struct {
bytes []byte
encoded string
}
func NewHDKeyPair(
index uint32,
publicKey ed25519.PublicKey,
privateKey ed25519.PrivateKey,
) (*HDKeyPair, error) {
algo, err := crypto.NewSignatureAlgorithm(crypto.Ed25519, 1)
if err != nil {
return nil, err
}
return &HDKeyPair{
index: index,
publicKey: &key{
bytes: publicKey,
encoded: hex.EncodeToString(publicKey),
},
privateKey: &key{
bytes: privateKey,
encoded: hex.EncodeToString(privateKey),
},
algo: algo,
meta: nil,
tainted: false,
}, nil
}
func (k *HDKeyPair) Index() uint32 {
return k.index
}
func (k *HDKeyPair) PublicKey() string {
return k.publicKey.encoded
}
func (k *HDKeyPair) PrivateKey() string {
return k.privateKey.encoded
}
func (k *HDKeyPair) IsTainted() bool {
return k.tainted
}
func (k *HDKeyPair) Meta() []Meta {
return k.meta
}
func (k *HDKeyPair) AlgorithmVersion() uint32 {
return k.algo.Version()
}
func (k *HDKeyPair) AlgorithmName() string {
return k.algo.Name()
}
func (k *HDKeyPair) Taint() error {
if k.tainted {
return ErrPubKeyAlreadyTainted
}
k.tainted = true
return nil
}
func (k *HDKeyPair) Untaint() error {
if !k.tainted {
return ErrPubKeyNotTainted
}
k.tainted = false
return nil
}
func (k *HDKeyPair) SignAny(data []byte) ([]byte, error) {
if k.tainted {
return nil, ErrPubKeyIsTainted
}
return k.algo.Sign(k.privateKey.bytes, data)
}
func (k *HDKeyPair) VerifyAny(data, sig []byte) (bool, error) {
return k.algo.Verify(k.publicKey.bytes, data, sig)
}
func (k *HDKeyPair) Sign(data []byte) (*Signature, error) {
if k.tainted {
return nil, ErrPubKeyIsTainted
}
sig, err := k.algo.Sign(k.privateKey.bytes, data)
if err != nil {
return nil, err
}
return &Signature{
Value: hex.EncodeToString(sig),
Algo: k.algo.Name(),
Version: k.algo.Version(),
}, nil
}
func (k *HDKeyPair) DeepCopy() *HDKeyPair {
copiedK := *k
return &copiedK
}
// ToPublicKey ensures the sensitive information doesn't leak outside.
func (k *HDKeyPair) ToPublicKey() HDPublicKey {
return HDPublicKey{
Idx: k.Index(),
PublicKey: k.PublicKey(),
Algorithm: Algorithm{
Name: k.algo.Name(),
Version: k.algo.Version(),
},
Tainted: k.tainted,
MetaList: k.meta,
}
}
type jsonHDKeyPair struct {
Index uint32 `json:"index"`
PublicKey string `json:"public_key"`
PrivateKey string `json:"private_key"`
Meta []Meta `json:"meta"`
Tainted bool `json:"tainted"`
Algorithm Algorithm `json:"algorithm"`
}
func (k *HDKeyPair) MarshalJSON() ([]byte, error) {
jsonKp := jsonHDKeyPair{
Index: k.index,
PublicKey: k.publicKey.encoded,
PrivateKey: k.privateKey.encoded,
Meta: k.meta,
Tainted: k.tainted,
Algorithm: Algorithm{
Name: k.algo.Name(),
Version: k.algo.Version(),
},
}
return json.Marshal(jsonKp)
}
func (k *HDKeyPair) UnmarshalJSON(data []byte) error {
jsonKp := &jsonHDKeyPair{}
if err := json.Unmarshal(data, jsonKp); err != nil {
return err
}
algo, err := crypto.NewSignatureAlgorithm(jsonKp.Algorithm.Name, jsonKp.Algorithm.Version)
if err != nil {
return err
}
pubKeyBytes, err := hex.DecodeString(jsonKp.PublicKey)
if err != nil {
return err
}
privKeyBytes, err := hex.DecodeString(jsonKp.PrivateKey)
if err != nil {
return err
}
*k = HDKeyPair{
index: jsonKp.Index,
publicKey: &key{
bytes: pubKeyBytes,
encoded: jsonKp.PublicKey,
},
privateKey: &key{
bytes: privKeyBytes,
encoded: jsonKp.PrivateKey,
},
meta: jsonKp.Meta,
tainted: jsonKp.Tainted,
algo: algo,
}
return nil
}