forked from tyler-smith/go-bip32
-
Notifications
You must be signed in to change notification settings - Fork 7
/
bip32.go
179 lines (151 loc) · 4.37 KB
/
bip32.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
package bip32
import (
"bytes"
"crypto/hmac"
"crypto/rand"
"crypto/sha512"
"encoding/hex"
"errors"
)
const (
FirstHardenedChild = uint32(0x80000000)
PublicKeyCompressedLength = 33
)
var (
PrivateWalletVersion, _ = hex.DecodeString("0488ADE4")
PublicWalletVersion, _ = hex.DecodeString("0488B21E")
)
// Represents a bip32 extended key containing key data, chain code, parent information, and other meta data
type Key struct {
Version []byte // 4 bytes
Depth byte // 1 bytes
ChildNumber []byte // 4 bytes
FingerPrint []byte // 4 bytes
ChainCode []byte // 32 bytes
Key []byte // 33 bytes
IsPrivate bool // unserialized
}
// Creates a new master extended key from a seed
func NewMasterKey(seed []byte) (*Key, error) {
// Generate key and chaincode
hmac := hmac.New(sha512.New, []byte("Bitcoin seed"))
hmac.Write([]byte(seed))
intermediary := hmac.Sum(nil)
// Split it into our key and chain code
keyBytes := intermediary[:32]
chainCode := intermediary[32:]
// Validate key
err := validatePrivateKey(keyBytes)
if err != nil {
return nil, err
}
// Create the key struct
key := &Key{
Version: PrivateWalletVersion,
ChainCode: chainCode,
Key: keyBytes,
Depth: 0x0,
ChildNumber: []byte{0x00, 0x00, 0x00, 0x00},
FingerPrint: []byte{0x00, 0x00, 0x00, 0x00},
IsPrivate: true,
}
return key, nil
}
// Derives a child key from a given parent as outlined by bip32
func (key *Key) NewChildKey(childIdx uint32) (*Key, error) {
hardenedChild := childIdx >= FirstHardenedChild
childIndexBytes := uint32Bytes(childIdx)
// Fail early if trying to create hardned child from public key
if !key.IsPrivate && hardenedChild {
return nil, errors.New("Can't create hardened child for public key")
}
// Get intermediary to create key and chaincode from
// Hardened children are based on the private key
// NonHardened children are based on the public key
var data []byte
if hardenedChild {
data = append([]byte{0x0}, key.Key...)
} else {
data = publicKeyForPrivateKey(key.Key)
}
data = append(data, childIndexBytes...)
hmac := hmac.New(sha512.New, key.ChainCode)
hmac.Write(data)
intermediary := hmac.Sum(nil)
// Create child Key with data common to all both scenarios
childKey := &Key{
ChildNumber: childIndexBytes,
ChainCode: intermediary[32:],
Depth: key.Depth + 1,
IsPrivate: key.IsPrivate,
}
// Bip32 CKDpriv
if key.IsPrivate {
childKey.Version = PrivateWalletVersion
childKey.FingerPrint = hash160(publicKeyForPrivateKey(key.Key))[:4]
childKey.Key = addPrivateKeys(intermediary[:32], key.Key)
// Validate key
err := validatePrivateKey(childKey.Key)
if err != nil {
return nil, err
}
// Bip32 CKDpub
} else {
keyBytes := publicKeyForPrivateKey(intermediary[:32])
// Validate key
err := validateChildPublicKey(keyBytes)
if err != nil {
return nil, err
}
childKey.Version = PublicWalletVersion
childKey.FingerPrint = hash160(key.Key)[:4]
childKey.Key = addPublicKeys(keyBytes, key.Key)
}
return childKey, nil
}
// Create public version of key or return a copy; 'Neuter' function from the bip32 spec
func (key *Key) PublicKey() *Key {
keyBytes := key.Key
if key.IsPrivate {
keyBytes = publicKeyForPrivateKey(keyBytes)
}
return &Key{
Version: PublicWalletVersion,
Key: keyBytes,
Depth: key.Depth,
ChildNumber: key.ChildNumber,
FingerPrint: key.FingerPrint,
ChainCode: key.ChainCode,
IsPrivate: false,
}
}
// Serialized an Key to a 78 byte byte slice
func (key *Key) Serialize() []byte {
// Private keys should be prepended with a single null byte
keyBytes := key.Key
if key.IsPrivate {
keyBytes = append([]byte{0x0}, keyBytes...)
}
// Write fields to buffer in order
buffer := new(bytes.Buffer)
buffer.Write(key.Version)
buffer.WriteByte(key.Depth)
buffer.Write(key.FingerPrint)
buffer.Write(key.ChildNumber)
buffer.Write(key.ChainCode)
buffer.Write(keyBytes)
// Append the standard doublesha256 checksum
serializedKey := addChecksumToBytes(buffer.Bytes())
return serializedKey
}
// Encode the Key in the standard Bitcoin base58 encoding
func (key *Key) String() string {
return string(base58Encode(key.Serialize()))
}
// Cryptographically secure seed
func NewSeed() ([]byte, error) {
// Well that easy, just make go read 256 random bytes into a slice
s := make([]byte, 256)
_, err := rand.Read([]byte(s))
return s, err
}