forked from bitcoinjs/bip32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
288 lines (227 loc) · 7.9 KB
/
index.js
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
let Buffer = require('safe-buffer').Buffer
let bs58check = require('bs58check')
let crypto = require('./crypto')
let ecc = require('./ecc')
let typeforce = require('typeforce')
let wif = require('wif')
let NETWORK_TYPE = typeforce.compile({
wif: typeforce.UInt8,
bip32: {
public: typeforce.UInt32,
private: typeforce.UInt32
}
})
let BITCOIN = {
bip32: {
public: 0x0488b21e,
private: 0x0488ade4
},
wif: 0x80
}
function BIP32 (d, Q, chainCode, network) {
this.d = d || null
this.Q = Q || ecc.pointDerive(d, true)
if (this.Q === null) throw new TypeError('Bad public key')
this.chainCode = chainCode
this.depth = 0
this.index = 0
this.network = network
this.parentFingerprint = 0x00000000
}
function fromSeed (seed, network) {
typeforce(typeforce.Buffer, seed)
if (seed.length < 16) throw new TypeError('Seed should be at least 128 bits')
if (seed.length > 64) throw new TypeError('Seed should be at most 512 bits')
if (network) typeforce(NETWORK_TYPE, network)
network = network || BITCOIN
let I = crypto.hmacSHA512('Bitcoin seed', seed)
let IL = I.slice(0, 32)
let IR = I.slice(32)
// if IL is 0 or >= n, the master key is invalid
if (!ecc.isPrivateKey(IL)) throw new TypeError('Private key not in range [1, n)')
return new BIP32(IL, null, IR, network)
}
function fromBase58 (string, network) {
let buffer = bs58check.decode(string)
if (buffer.length !== 78) throw new TypeError('Invalid buffer length')
if (network) typeforce(NETWORK_TYPE, network)
network = network || BITCOIN
// 4 bytes: version bytes
let version = buffer.readUInt32BE(0)
if (version !== network.bip32.private &&
version !== network.bip32.public) throw new TypeError('Invalid network version')
// 1 byte: depth: 0x00 for master nodes, 0x01 for level-1 descendants, ...
let depth = buffer[4]
// 4 bytes: the fingerprint of the parent's key (0x00000000 if master key)
let parentFingerprint = buffer.readUInt32BE(5)
if (depth === 0) {
if (parentFingerprint !== 0x00000000) throw new TypeError('Invalid parent fingerprint')
}
// 4 bytes: child number. This is the number i in xi = xpar/i, with xi the key being serialized.
// This is encoded in MSB order. (0x00000000 if master key)
let index = buffer.readUInt32BE(9)
if (depth === 0 && index !== 0) throw new TypeError('Invalid index')
// 32 bytes: the chain code
let chainCode = buffer.slice(13, 45)
let hd
// 33 bytes: private key data (0x00 + k)
if (version === network.bip32.private) {
if (buffer.readUInt8(45) !== 0x00) throw new TypeError('Invalid private key')
let k = buffer.slice(46, 78)
// if IL is 0 or >= n, the master key is invalid
if (!ecc.isPrivateKey(k)) throw new TypeError('Private key not in range [1, n)')
hd = new BIP32(k, null, chainCode, network)
// 33 bytes: public key data (0x02 + X or 0x03 + X)
} else {
let X = buffer.slice(45, 78)
// verify the X coordinate is a point on the curve
if (!ecc.isPublicKey(X)) throw new TypeError('Point is not on the curve')
hd = new BIP32(null, X, chainCode, network)
}
hd.depth = depth
hd.index = index
hd.parentFingerprint = parentFingerprint
return hd
}
BIP32.prototype.getIdentifier = function () {
return crypto.hash160(this.Q)
}
BIP32.prototype.getFingerprint = function () {
return this.getIdentifier().slice(0, 4)
}
BIP32.prototype.getNetwork = function () {
return this.network
}
BIP32.prototype.getPublicKeyBuffer = function () {
return this.Q
}
BIP32.prototype.neutered = function () {
let neutered = new BIP32(null, this.Q, this.chainCode, this.network)
neutered.depth = this.depth
neutered.index = this.index
neutered.parentFingerprint = this.parentFingerprint
return neutered
}
BIP32.prototype.toBase58 = function () {
let network = this.network
let version = (!this.isNeutered()) ? network.bip32.private : network.bip32.public
let buffer = Buffer.allocUnsafe(78)
// 4 bytes: version bytes
buffer.writeUInt32BE(version, 0)
// 1 byte: depth: 0x00 for master nodes, 0x01 for level-1 descendants, ....
buffer.writeUInt8(this.depth, 4)
// 4 bytes: the fingerprint of the parent's key (0x00000000 if master key)
buffer.writeUInt32BE(this.parentFingerprint, 5)
// 4 bytes: child number. This is the number i in xi = xpar/i, with xi the key being serialized.
// This is encoded in big endian. (0x00000000 if master key)
buffer.writeUInt32BE(this.index, 9)
// 32 bytes: the chain code
this.chainCode.copy(buffer, 13)
// 33 bytes: the public key or private key data
if (!this.isNeutered()) {
// 0x00 + k for private keys
buffer.writeUInt8(0, 45)
this.d.copy(buffer, 46)
// 33 bytes: the public key
} else {
// X9.62 encoding for public keys
this.Q.copy(buffer, 45)
}
return bs58check.encode(buffer)
}
BIP32.prototype.toWIF = function () {
if (!this.d) throw new TypeError('Missing private key')
return wif.encode(this.network.wif, this.d, true)
}
let HIGHEST_BIT = 0x80000000
// https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#child-key-derivation-ckd-functions
BIP32.prototype.derive = function (index) {
typeforce(typeforce.UInt32, index)
let isHardened = index >= HIGHEST_BIT
let data = Buffer.allocUnsafe(37)
// Hardened child
if (isHardened) {
if (this.isNeutered()) throw new TypeError('Missing private key for hardened child key')
// data = 0x00 || ser256(kpar) || ser32(index)
data[0] = 0x00
this.d.copy(data, 1)
data.writeUInt32BE(index, 33)
// Normal child
} else {
// data = serP(point(kpar)) || ser32(index)
// = serP(Kpar) || ser32(index)
this.Q.copy(data, 0)
data.writeUInt32BE(index, 33)
}
let I = crypto.hmacSHA512(this.chainCode, data)
let IL = I.slice(0, 32)
let IR = I.slice(32)
// if parse256(IL) >= n, proceed with the next value for i
if (!ecc.isPrivateKey(IL)) return this.derive(index + 1)
// Private parent key -> private child key
let hd
if (!this.isNeutered()) {
// ki = parse256(IL) + kpar (mod n)
let ki = ecc.privateAdd(this.d, IL)
// In case ki == 0, proceed with the next value for i
if (ki == null) return this.derive(index + 1)
hd = new BIP32(ki, null, IR, this.network)
// Public parent key -> public child key
} else {
// Ki = point(parse256(IL)) + Kpar
// = G*IL + Kpar
let Ki = ecc.publicAdd(this.Q, IL, true)
// In case Ki is the point at infinity, proceed with the next value for i
if (Ki === null) return this.derive(index + 1)
hd = new BIP32(null, Ki, IR, this.network)
}
hd.depth = this.depth + 1
hd.index = index
hd.parentFingerprint = this.getFingerprint().readUInt32BE(0)
return hd
}
let UINT31_MAX = Math.pow(2, 31) - 1
function UInt31 (value) {
return typeforce.UInt32(value) && value <= UINT31_MAX
}
BIP32.prototype.deriveHardened = function (index) {
typeforce(UInt31, index)
// Only derives hardened private keys by default
return this.derive(index + HIGHEST_BIT)
}
// Private === not neutered
// Public === neutered
BIP32.prototype.isNeutered = function () {
return this.d === null
}
function isBIP32Path (value) {
return typeforce.String(value) && value.match(/^(m\/)?(\d+'?\/)*\d+'?$/)
}
BIP32.prototype.derivePath = function (path) {
typeforce(isBIP32Path, path)
let splitPath = path.split('/')
if (splitPath[0] === 'm') {
if (this.parentFingerprint) throw new TypeError('Expected master, got child')
splitPath = splitPath.slice(1)
}
return splitPath.reduce(function (prevHd, indexStr) {
let index
if (indexStr.slice(-1) === "'") {
index = parseInt(indexStr.slice(0, -1), 10)
return prevHd.deriveHardened(index)
} else {
index = parseInt(indexStr, 10)
return prevHd.derive(index)
}
}, this)
}
BIP32.prototype.sign = function (hash) {
return ecc.sign(hash, this.d)
}
BIP32.prototype.verify = function (hash, signature) {
return ecc.verify(hash, signature, this.Q)
}
module.exports = {
fromBase58,
fromSeed
}