-
Notifications
You must be signed in to change notification settings - Fork 44
/
account.go
331 lines (310 loc) · 8.44 KB
/
account.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* Copyright (C) 2018 The ontology Authors
* This file is part of The ontology library.
*
* The ontology 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 ontology 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 ontology. If not, see <http://www.gnu.org/licenses/>.
*/
package ontology_go_sdk
import (
"crypto/elliptic"
"encoding/hex"
"fmt"
"github.com/ontio/ontology-crypto/ec"
"github.com/ontio/ontology-crypto/keypair"
s "github.com/ontio/ontology-crypto/signature"
"github.com/ontio/ontology/account"
"github.com/ontio/ontology/core/types"
)
type Signer interface {
Sign(data []byte) ([]byte, error)
GetPublicKey() keypair.PublicKey
GetPrivateKey() keypair.PrivateKey
GetSigScheme() s.SignatureScheme
}
type Account account.Account
func NewAccountFromPrivateKey(privateKey []byte, signatureScheme s.SignatureScheme) (*Account, error) {
if privateKey == nil {
return nil, fmt.Errorf("privatekey should not be nil")
}
if len(privateKey) != 32 {
return nil, fmt.Errorf("the length of privatekey should be 32")
}
prikey := ec.ConstructPrivateKey(privateKey, elliptic.P256())
privaKey := ec.PrivateKey{
Algorithm: ec.ECDSA,
PrivateKey: prikey,
}
address := types.AddressFromPubKey(privaKey.Public())
return &Account{
PrivateKey: &privaKey,
PublicKey: privaKey.Public(),
Address: address,
SigScheme: signatureScheme,
}, nil
}
func NewAccount(sigscheme ...s.SignatureScheme) *Account {
var scheme s.SignatureScheme
if len(sigscheme) == 0 {
scheme = s.SHA256withECDSA
} else {
scheme = sigscheme[0]
}
var pkAlgorithm keypair.KeyType
var params interface{}
switch scheme {
case s.SHA224withECDSA, s.SHA3_224withECDSA:
pkAlgorithm = keypair.PK_ECDSA
params = keypair.P224
case s.SHA256withECDSA, s.SHA3_256withECDSA, s.RIPEMD160withECDSA:
pkAlgorithm = keypair.PK_ECDSA
params = keypair.P256
case s.SHA384withECDSA, s.SHA3_384withECDSA:
pkAlgorithm = keypair.PK_ECDSA
params = keypair.P384
case s.SHA512withECDSA, s.SHA3_512withECDSA:
pkAlgorithm = keypair.PK_ECDSA
params = keypair.P521
case s.SM3withSM2:
pkAlgorithm = keypair.PK_SM2
params = keypair.SM2P256V1
case s.SHA512withEDDSA:
pkAlgorithm = keypair.PK_EDDSA
params = keypair.ED25519
default:
return nil
}
pri, pub, _ := keypair.GenerateKeyPair(pkAlgorithm, params)
address := types.AddressFromPubKey(pub)
return &Account{
PrivateKey: pri,
PublicKey: pub,
Address: address,
SigScheme: scheme,
}
}
func (this *Account) Sign(data []byte) ([]byte, error) {
sig, err := s.Sign(this.SigScheme, this.PrivateKey, data, nil)
if err != nil {
return nil, err
}
sigData, err := s.Serialize(sig)
if err != nil {
return nil, fmt.Errorf("signature.Serialize error:%s", err)
}
return sigData, nil
}
func (this *Account) GetPrivateKey() keypair.PrivateKey {
return this.PrivateKey
}
func (this *Account) GetPublicKey() keypair.PublicKey {
return this.PublicKey
}
func (this *Account) GetSigScheme() s.SignatureScheme {
return this.SigScheme
}
/** AccountData - for wallet read and save, no crypto object included **/
type AccountData struct {
keypair.ProtectedKey
Label string `json:"label"`
PubKey string `json:"publicKey"`
SigSch string `json:"signatureScheme"`
IsDefault bool `json:"isDefault"`
Lock bool `json:"lock"`
scrypt *keypair.ScryptParam
}
func NewAccountData(keyType keypair.KeyType, curveCode byte, sigScheme s.SignatureScheme, passwd []byte, scrypts ...*keypair.ScryptParam) (*AccountData, error) {
if len(passwd) == 0 {
return nil, fmt.Errorf("password cannot empty")
}
if !CheckKeyTypeCurve(keyType, curveCode) {
return nil, fmt.Errorf("curve unmath key type")
}
if !CheckSigScheme(keyType, sigScheme) {
return nil, fmt.Errorf("sigScheme:%s does not match with KeyType:%s", sigScheme.Name(), GetKeyTypeString(keyType))
}
var scrypt *keypair.ScryptParam
if len(scrypts) > 0 {
scrypt = scrypts[0]
} else {
scrypt = keypair.GetScryptParameters()
}
prvkey, pubkey, err := keypair.GenerateKeyPair(keyType, curveCode)
if err != nil {
return nil, fmt.Errorf("generateKeyPair error:%s", err)
}
address := types.AddressFromPubKey(pubkey)
addressBase58 := address.ToBase58()
prvSecret, err := keypair.EncryptWithCustomScrypt(prvkey, addressBase58, passwd, scrypt)
if err != nil {
return nil, fmt.Errorf("encryptPrivateKey error:%s", err)
}
accData := &AccountData{}
accData.SetKeyPair(prvSecret)
accData.SigSch = sigScheme.Name()
accData.PubKey = hex.EncodeToString(keypair.SerializePublicKey(pubkey))
accData.scrypt = scrypt
return accData, nil
}
func (this *AccountData) GetAccount(passwd []byte) (*Account, error) {
privateKey, err := keypair.DecryptWithCustomScrypt(&this.ProtectedKey, passwd, this.scrypt)
if err != nil {
return nil, fmt.Errorf("decrypt privateKey error:%s", err)
}
publicKey := privateKey.Public()
addr := types.AddressFromPubKey(publicKey)
scheme, err := s.GetScheme(this.SigSch)
if err != nil {
return nil, fmt.Errorf("signature scheme error:%s", err)
}
return &Account{
PrivateKey: privateKey,
PublicKey: publicKey,
Address: addr,
SigScheme: scheme,
}, nil
}
func (this *AccountData) GetScrypt() *keypair.ScryptParam {
return this.scrypt
}
func (this *AccountData) SetScript(scrypt *keypair.ScryptParam) {
this.scrypt = scrypt
}
func (this *AccountData) Clone() *AccountData {
accData := &AccountData{
Label: this.Label,
PubKey: this.PubKey,
SigSch: this.SigSch,
IsDefault: this.IsDefault,
Lock: this.Lock,
scrypt: this.scrypt,
}
accData.SetKeyPair(this.GetKeyPair())
return accData
}
func (this *AccountData) SetKeyPair(keyinfo *keypair.ProtectedKey) {
this.Address = keyinfo.Address
this.EncAlg = keyinfo.EncAlg
this.Alg = keyinfo.Alg
this.Hash = keyinfo.Hash
this.Key = make([]byte, len(keyinfo.Key))
copy(this.Key, keyinfo.Key)
this.Param = keyinfo.Param
this.Salt = make([]byte, len(keyinfo.Salt))
copy(this.Salt, keyinfo.Salt)
}
func (this *AccountData) GetKeyPair() *keypair.ProtectedKey {
var keyinfo = new(keypair.ProtectedKey)
keyinfo.Address = this.Address
keyinfo.EncAlg = this.EncAlg
keyinfo.Alg = this.Alg
keyinfo.Hash = this.Hash
keyinfo.Key = make([]byte, len(this.Key))
copy(keyinfo.Key, this.Key)
keyinfo.Param = this.Param
keyinfo.Salt = make([]byte, len(this.Salt))
copy(keyinfo.Salt, this.Salt)
return keyinfo
}
func GetKeyTypeString(keyType keypair.KeyType) string {
switch keyType {
case keypair.PK_ECDSA:
return "ECDSA"
case keypair.PK_SM2:
return "SM2"
case keypair.PK_EDDSA:
return "Ed25519"
default:
return "unknown key type"
}
}
func CheckKeyTypeCurve(keyType keypair.KeyType, curveCode byte) bool {
switch keyType {
case keypair.PK_ECDSA:
switch curveCode {
case keypair.P224:
case keypair.P256:
case keypair.P384:
case keypair.P521:
default:
return false
}
case keypair.PK_SM2:
switch curveCode {
case keypair.SM2P256V1:
default:
return false
}
case keypair.PK_EDDSA:
switch curveCode {
case keypair.ED25519:
default:
return false
}
}
return true
}
func CheckSigScheme(keyType keypair.KeyType, sigScheme s.SignatureScheme) bool {
switch keyType {
case keypair.PK_ECDSA:
switch sigScheme {
case s.SHA224withECDSA:
case s.SHA256withECDSA:
case s.SHA384withECDSA:
case s.SHA512withECDSA:
case s.SHA3_224withECDSA:
case s.SHA3_256withECDSA:
case s.SHA3_384withECDSA:
case s.SHA3_512withECDSA:
case s.RIPEMD160withECDSA:
default:
return false
}
case keypair.PK_SM2:
switch sigScheme {
case s.SM3withSM2:
default:
return false
}
case keypair.PK_EDDSA:
switch sigScheme {
case s.SHA512withEDDSA:
default:
return false
}
default:
return false
}
return true
}
func GetCurveName(pubKey []byte) string {
if len(pubKey) < 2 {
return ""
}
switch keypair.KeyType(pubKey[0]) {
case keypair.PK_ECDSA, keypair.PK_SM2:
c, err := keypair.GetCurve(pubKey[1])
if err != nil {
return ""
}
return c.Params().Name
case keypair.PK_EDDSA:
if pubKey[1] == keypair.ED25519 {
return "ed25519"
} else {
return ""
}
default:
return ""
}
}