/
keychain.go
155 lines (122 loc) · 3.12 KB
/
keychain.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
package bip32
import (
"bytes"
"crypto/elliptic"
"crypto/hmac"
"crypto/sha512"
"encoding/binary"
"encoding/hex"
"math/big"
"github.com/libs4go/bcf4go/bip44"
"github.com/libs4go/errors"
)
// DrivedKey .
type DrivedKey struct {
Param KeyParam
PublicKey []byte // 33 bytes
PrivateKey []byte // 32 bytes
ChainCode []byte // 32 bytes
}
// PublicKeyF .
type PublicKeyF func(privateKey []byte) []byte
// KeyParam .
type KeyParam interface {
// PrivateToPublic(privateKey []byte) []byte
Curve() elliptic.Curve
}
func isOdd(a *big.Int) bool {
return a.Bit(0) == 1
}
func paddedAppend(size uint, dst, src []byte) []byte {
for i := 0; i < int(size)-len(src); i++ {
dst = append(dst, 0)
}
return append(dst, src...)
}
// CompressedPublicKeyBytes .
func compressedPublicKeyBytes(x, y *big.Int) []byte {
b := make([]byte, 0, 33)
format := byte(0x2)
if isOdd(y) {
format |= 0x1
}
b = append(b, format)
return paddedAppend(32, b, x.Bytes())
}
func compress(x, y *big.Int) []byte {
two := big.NewInt(2)
rem := two.Mod(y, two).Uint64()
rem += 2
b := make([]byte, 2)
binary.BigEndian.PutUint16(b, uint16(rem))
rest := x.Bytes()
return append(b[1:], rest...)
}
func privateToPublic(curve elliptic.Curve, key []byte) []byte {
return compressedPublicKeyBytes(curve.ScalarBaseMult(key))
}
// NewMasterKey create new master key from seed
func NewMasterKey(seed []byte, param KeyParam) (*DrivedKey, error) {
hmac := hmac.New(sha512.New, []byte("Bitcoin seed"))
_, err := hmac.Write(seed)
if err != nil {
return nil, errors.Wrap(err, "hmac error")
}
intermediary := hmac.Sum(nil)
keyBytes := intermediary[:32]
chainCode := intermediary[32:]
return &DrivedKey{
Param: param,
PublicKey: privateToPublic(param.Curve(), keyBytes),
// PublicKey: param.PrivateToPublic(keyBytes),
PrivateKey: keyBytes,
ChainCode: chainCode,
}, nil
}
func getPrivateKeyByte33(pk []byte) []byte {
buff := make([]byte, 33)
copy(buff[33-len(pk):], pk)
return buff
}
// ChildKey get child key
func (key *DrivedKey) ChildKey(index bip44.Number) (*DrivedKey, error) {
indexBytes := uint32Bytes(uint32(index))
var buff bytes.Buffer
if index.IsHardened() {
buff.Write(getPrivateKeyByte33(key.PrivateKey))
} else {
buff.Write(key.PublicKey)
}
buff.Write(indexBytes)
seed := buff.Bytes()
dig := hmac.New(sha512.New, key.ChainCode)
_, err := dig.Write(seed)
if err != nil {
return nil, err
}
intermediary := dig.Sum(nil)
keyBytes := intermediary[:32]
chainCode := intermediary[32:]
newkey := key.addPrivKeys(keyBytes, key.PrivateKey)
return &DrivedKey{
Param: key.Param,
PublicKey: privateToPublic(key.Param.Curve(), newkey),
// PublicKey: key.Param.PrivateToPublic(keyBytes),
PrivateKey: newkey,
ChainCode: chainCode,
}, nil
}
func (key *DrivedKey) addPrivKeys(k1, k2 []byte) []byte {
i1 := big.NewInt(0).SetBytes(k1)
i2 := big.NewInt(0).SetBytes(k2)
i1.Add(i1, i2)
i1.Mod(i1, key.Param.Curve().Params().N)
k := i1.Bytes()
zero, _ := hex.DecodeString("00")
return append(zero, k...)
}
func uint32Bytes(i uint32) []byte {
bytes := make([]byte, 4)
binary.BigEndian.PutUint32(bytes, i)
return bytes
}