forked from wanchain/go-wanchain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
key.go
executable file
·446 lines (400 loc) · 11.7 KB
/
key.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// Copyright 2014 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum 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 go-ethereum 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package keystore
import (
"bytes"
"crypto/ecdsa"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/btcsuite/btcd/btcec"
"github.com/pborman/uuid"
"github.com/wanchain/go-wanchain/accounts"
"github.com/wanchain/go-wanchain/common"
"github.com/wanchain/go-wanchain/common/hexutil"
"github.com/wanchain/go-wanchain/common/math"
"github.com/wanchain/go-wanchain/crypto"
//"math/big"
)
const (
version = 3
)
//r@zy: 修改为新的结构
type Key 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
// privkey in this struct is always in plaintext
PrivateKey *ecdsa.PrivateKey
PrivateKey2 *ecdsa.PrivateKey
WAddress common.WAddress
}
type keyStore interface {
// Loads and decrypts the key from disk.
GetKey(addr common.Address, filename string, auth string) (*Key, error)
// Loads the encrypt key from disk
GetKeyEncrypt(addr common.Address, filename string) (*Key, error)
// Writes and encrypts the key.
StoreKey(filename string, k *Key, auth string) error
// Joins filename with the key directory unless it is already absolute.
JoinPath(filename string) string
}
type plainKeyJSON struct {
Address string `json:"address"`
PrivateKey string `json:"privatekey"`
Id string `json:"id"`
Version int `json:"version"`
}
type encryptedKeyJSONV3 struct {
Address string `json:"address"`
Crypto cryptoJSON `json:"crypto"`
Crypto2 cryptoJSON `json:"crypto2"`
Id string `json:"id"`
Version int `json:"version"`
WAddress string `json:"waddress"`
}
type encryptedKeyJSONV1 struct {
Address string `json:"address"`
Crypto cryptoJSON `json:"crypto"`
Id string `json:"id"`
Version string `json:"version"`
}
type cryptoJSON struct {
Cipher string `json:"cipher"`
CipherText string `json:"ciphertext"`
CipherParams cipherparamsJSON `json:"cipherparams"`
KDF string `json:"kdf"`
KDFParams map[string]interface{} `json:"kdfparams"`
MAC string `json:"mac"`
}
type cipherparamsJSON struct {
IV string `json:"iv"`
}
type scryptParamsJSON struct {
N int `json:"n"`
R int `json:"r"`
P int `json:"p"`
DkLen int `json:"dklen"`
Salt string `json:"salt"`
}
func (k *Key) MarshalJSON() (j []byte, err error) {
jStruct := plainKeyJSON{
hex.EncodeToString(k.Address[:]),
hex.EncodeToString(crypto.FromECDSA(k.PrivateKey)),
k.Id.String(),
version,
}
j, err = json.Marshal(jStruct)
return j, err
}
func (k *Key) UnmarshalJSON(j []byte) (err error) {
keyJSON := new(plainKeyJSON)
err = json.Unmarshal(j, &keyJSON)
if err != nil {
return err
}
u := new(uuid.UUID)
*u = uuid.Parse(keyJSON.Id)
k.Id = *u
addr, err := hex.DecodeString(keyJSON.Address)
if err != nil {
return err
}
privkey, err := hex.DecodeString(keyJSON.PrivateKey)
if err != nil {
return err
}
k.Address = common.BytesToAddress(addr)
k.PrivateKey = crypto.ToECDSA(privkey)
return nil
}
func newKeyFromECDSA(privateKeyECDSA *ecdsa.PrivateKey, privateKeyECDSA2 *ecdsa.PrivateKey) *Key {
id := uuid.NewRandom()
key := &Key{
Id: id,
Address: crypto.PubkeyToAddress(privateKeyECDSA.PublicKey),
PrivateKey: privateKeyECDSA,
PrivateKey2: privateKeyECDSA2,
}
updateWaddress(key)
return key
}
// SerializeCompressed serializes a public key in a 33-byte compressed format. from btcec.
//func isOdd(a *big.Int) bool {
// return a.Bit(0) == 1
//}
func PubkeySerializeCompressed(p *ecdsa.PublicKey) []byte {
const pubkeyCompressed byte = 0x2
b := make([]byte, 0, 33)
format := pubkeyCompressed
if p.Y.Bit(0) == 1 {
format |= 0x1
}
b = append(b, format)
//b = append(b, p.X.Bytes()...)
b = append(b, math.PaddedBigBytes(p.X, 32)...)
return b
}
func (k *Key) GenerateWaddress() common.WAddress {
var tmpWaddress common.WAddress
copy(tmpWaddress[0:33], PubkeySerializeCompressed(&k.PrivateKey.PublicKey))
copy(tmpWaddress[33:66], PubkeySerializeCompressed(&k.PrivateKey2.PublicKey))
return tmpWaddress
}
func GenerateWaddressFromPubkey(Pub1, Pub2 *ecdsa.PublicKey) common.WAddress {
var tmpWaddress common.WAddress
copy(tmpWaddress[0:33], PubkeySerializeCompressed(Pub1))
copy(tmpWaddress[33:66], PubkeySerializeCompressed(Pub2))
return tmpWaddress
}
// lzh add
func updateWaddress(k *Key) {
k.WAddress = k.GenerateWaddress()
}
// lzh add
func checkWaddressValid(k *Key) bool {
return k.WAddress == k.GenerateWaddress()
}
// lzh add
func (k *Key) GetTwoPublicKeyRawStrs() ([]string, error) {
PK1, PK2, err := k.GetTwoPublicKey()
if err != nil {
return nil, err
}
ret := common.TwoPublicKeyToHexSlice(PK1, PK2)
return ret, nil
}
// lzh add
func (k *Key) GetTwoPublicKey() (*ecdsa.PublicKey, *ecdsa.PublicKey, error) {
return GeneratePublicKeyFromWadress(k.WAddress[:])
}
// lzh add
func GeneratePublicKeyFromWadress(waddr []byte) (*ecdsa.PublicKey, *ecdsa.PublicKey, error) {
pb := make([]byte, 33)
copy(pb[0:33], waddr[0:33])
curve := btcec.S256()
pk1, err := btcec.ParsePubKey(pb, curve)
if err != nil {
return nil, nil, err
}
copy(pb[0:33], waddr[33:66])
pk2, err2 := btcec.ParsePubKey(pb, curve)
if err2 != nil {
return nil, nil, err2
}
return (*ecdsa.PublicKey)(pk1), (*ecdsa.PublicKey)(pk2), nil
}
func WaddrFromUncompressed(waddr []byte, raw []byte) error {
pub := make([]byte, 65)
pub[0] = 0x04
copy(pub[1:], raw[0:64])
A := crypto.ToECDSAPub(pub)
copy(pub[1:], raw[64:])
B := crypto.ToECDSAPub(pub)
wd := GenerateWaddressFromPubkey(A, B)
copy(waddr, wd[:])
return nil
}
func ToWaddr(raw []byte) ([]byte, error) {
pub := make([]byte, 65)
pub[0] = 0x04
copy(pub[1:], raw[0:64])
A := crypto.ToECDSAPub(pub)
copy(pub[1:], raw[64:])
B := crypto.ToECDSAPub(pub)
wd := GenerateWaddressFromPubkey(A, B)
return wd[:], nil
}
func WaddrToUncompressed(waddr []byte) ([]byte, error) {
A, B, err := GeneratePublicKeyFromWadress(waddr)
if err != nil {
return nil, err
}
u := make([]byte, 128)
ax := math.PaddedBigBytes(A.X, 32)
ay := math.PaddedBigBytes(A.Y, 32)
bx := math.PaddedBigBytes(B.X, 32)
by := math.PaddedBigBytes(B.Y, 32)
copy(u[0:], ax[0:32])
copy(u[32:], ay[0:32])
copy(u[64:], bx[0:32])
copy(u[96:], by[0:32])
return u, nil
}
func WaddrToUncompressedFromString(waddr string) ([]byte, error) {
waddrBytes, err := hexutil.Decode(waddr)
if err != nil {
return nil, err
}
return WaddrToUncompressed(waddrBytes)
}
// lzh add
func initPublicKeyFromWaddress(pk1, pk2 *ecdsa.PublicKey, waddress *common.WAddress) error {
PK1, PK2, err := GeneratePublicKeyFromWadress(waddress[:])
if err != nil {
return err
}
pk1.Curve = crypto.S256()
pk2.Curve = crypto.S256()
pk1.X = PK1.X
pk1.Y = PK1.Y
pk2.X = PK2.X
pk2.Y = PK2.Y
return nil
}
//// lzh add (ecdsa public key AX --> AY)
//func GetEllipticYFromX(curve *elliptic.CurveParams, x *big.Int, positive bool) *big.Int {
// // y² = x³ - 3x + b
// y2 := new(big.Int).Mul(x, x)
// y2.Mul(y2, x)
//
// threeX := new(big.Int).Lsh(x, 1)
// threeX.Add(threeX, x)
//
// y2.Sub(y2, threeX)
// y2.Add(y2, curve.B)
//
// // ⌊√y2⌋ --> y
// y := new(big.Int).Sqrt(y2)
//
//
// if positive && y.Cmp(new(big.Int)) < 0 {
// y.Sub(new(big.Int), y)
// }
//
// return y
//}
//
//func TestGetEllipticYFromX() {
// xStr := "d7dffe5e06d2c7024d9bb93f675b8242e71901ee66a1bfe3fe5369324c0a75bf"
// yStr := "6f033dc4af65f5d0fe7072e98788fcfa670919b5bdc046f1ca91f28dff59db70"
//
// x, ok := new(big.Int).SetString(xStr, 16)
// if !ok {
// return
// }
//
// strstr := common.Bytes2Hex(x.Bytes())
//
// exY, ok := new(big.Int).SetString(yStr, 16)
// if !ok {
// return
// }
//
// strstr = common.Bytes2Hex(exY.Bytes())
//
// y := GetEllipticYFromX(crypto.S256().Params(), x, false)
// strstr = common.Bytes2Hex(y.Bytes())
//
// if y.Cmp(exY) == 0 {
// log.Info("TestGetEllipticYFromX suc!", strstr)
// } else {
// log.Info("TestGetEllipticYFromX fail!", strstr)
// }
//}
// NewKeyForDirectICAP generates a key whose address fits into < 155 bits so it can fit
// into the Direct ICAP spec. for simplicity and easier compatibility with other libs, we
// retry until the first byte is 0.
func NewKeyForDirectICAP(rand io.Reader) *Key {
randBytes := make([]byte, 64*2)
_, err := rand.Read(randBytes)
if err != nil {
panic("key generation: could not read from random source: " + err.Error())
}
reader := bytes.NewReader(randBytes)
privateKeyECDSA, err := ecdsa.GenerateKey(crypto.S256(), reader)
if err != nil {
panic("key generation: ecdsa.GenerateKey failed: " + err.Error())
}
var privateKeyECDSA2 *ecdsa.PrivateKey
privateKeyECDSA2, err = ecdsa.GenerateKey(crypto.S256(), reader)
if err != nil {
panic("key generation: ecdsa.GenerateKey failed: " + err.Error())
}
key := newKeyFromECDSA(privateKeyECDSA, privateKeyECDSA2)
if !strings.HasPrefix(key.Address.Hex(), "0x00") {
return NewKeyForDirectICAP(rand)
}
return key
}
func newKey(rand io.Reader) (*Key, error) {
privateKeyECDSA, err := ecdsa.GenerateKey(crypto.S256(), rand)
if err != nil {
return nil, err
}
var privateKeyECDSA2 *ecdsa.PrivateKey
privateKeyECDSA2, err = ecdsa.GenerateKey(crypto.S256(), rand)
if err != nil {
return nil, err
}
return newKeyFromECDSA(privateKeyECDSA, privateKeyECDSA2), nil
}
func storeNewKey(ks keyStore, rand io.Reader, auth string) (*Key, accounts.Account, error) {
key, err := newKey(rand)
if err != nil {
return nil, accounts.Account{}, err
}
a := accounts.Account{Address: key.Address, URL: accounts.URL{Scheme: KeyStoreScheme, Path: ks.JoinPath(keyFileName(key.Address))}}
if err := ks.StoreKey(a.URL.Path, key, auth); err != nil {
zeroKey(key.PrivateKey)
return nil, a, err
}
return key, a, err
}
func writeKeyFile(file string, content []byte) error {
// Create the keystore directory with appropriate permissions
// in case it is not present yet.
const dirPerm = 0700
if err := os.MkdirAll(filepath.Dir(file), dirPerm); err != nil {
return err
}
// Atomic write: create a temporary hidden file first
// then move it into place. TempFile assigns mode 0600.
f, err := ioutil.TempFile(filepath.Dir(file), "."+filepath.Base(file)+".tmp")
if err != nil {
return err
}
if _, err := f.Write(content); err != nil {
f.Close()
os.Remove(f.Name())
return err
}
f.Close()
return os.Rename(f.Name(), file)
}
// keyFileName implements the naming convention for keyfiles:
// UTC--<created_at UTC ISO8601>-<address hex>
func keyFileName(keyAddr common.Address) string {
ts := time.Now().UTC()
return fmt.Sprintf("UTC--%s--%s", toISO8601(ts), hex.EncodeToString(keyAddr[:]))
}
func toISO8601(t time.Time) string {
var tz string
name, offset := t.Zone()
if name == "UTC" {
tz = "Z"
} else {
tz = fmt.Sprintf("%03d00", offset/3600)
}
return fmt.Sprintf("%04d-%02d-%02dT%02d-%02d-%02d.%09d%s", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), tz)
}