bitcoinjs-lib/src/hdnode.js

315 lines
8.8 KiB
JavaScript
Raw Normal View History

var assert = require('assert')
2014-07-02 14:37:32 +10:00
var base58check = require('bs58check')
var bcrypto = require('./crypto')
var crypto = require('crypto')
2014-12-23 15:08:20 +11:00
var typeForce = require('typeforce')
2014-06-07 16:24:27 +10:00
var networks = require('./networks')
2014-04-17 06:18:31 +10:00
2014-05-03 10:04:54 +08:00
var BigInteger = require('bigi')
2014-05-13 16:44:29 +10:00
var ECKey = require('./eckey')
var ECPubKey = require('./ecpubkey')
2014-06-07 16:24:27 +10:00
var ecurve = require('ecurve')
var curve = ecurve.getCurveByName('secp256k1')
2015-02-23 10:36:57 +11:00
function findBIP32NetworkByVersion (version) {
for (var name in networks) {
var network = networks[name]
2015-02-23 10:36:57 +11:00
if (version === network.bip32.private || version === network.bip32.public) {
2014-12-01 10:43:52 +11:00
return network
}
}
2014-12-01 10:43:52 +11:00
assert(false, 'Could not find network for ' + version.toString(16))
}
2015-02-23 10:36:57 +11:00
function HDNode (K, chainCode, network) {
network = network || networks.bitcoin
2014-12-23 15:08:20 +11:00
typeForce('Buffer', chainCode)
2014-07-29 01:30:51 +10:00
assert.equal(chainCode.length, 32, 'Expected chainCode length of 32, got ' + chainCode.length)
2014-05-31 20:16:42 +10:00
assert(network.bip32, 'Unknown BIP32 constants for network')
2014-05-31 20:16:42 +10:00
this.chainCode = chainCode
2014-03-31 11:47:47 +08:00
this.depth = 0
this.index = 0
this.parentFingerprint = 0x00000000
2014-05-31 20:16:42 +10:00
this.network = network
if (K instanceof BigInteger) {
this.privKey = new ECKey(K, true)
this.pubKey = this.privKey.pub
} else if (K instanceof ECKey) {
assert(K.pub.compressed, 'ECKey must be compressed')
this.privKey = K
} else if (K instanceof ECPubKey) {
assert(K.compressed, 'ECPubKey must be compressed')
this.pubKey = K
2014-05-31 20:16:42 +10:00
} else {
this.pubKey = new ECPubKey(K, true)
2014-05-31 20:16:42 +10:00
}
}
2014-06-03 17:08:42 +10:00
HDNode.MASTER_SECRET = new Buffer('Bitcoin seed')
HDNode.HIGHEST_BIT = 0x80000000
HDNode.LENGTH = 78
2015-02-23 10:36:57 +11:00
HDNode.fromSeedBuffer = function (seed, network) {
2014-12-23 15:08:20 +11:00
typeForce('Buffer', seed)
2014-07-11 19:15:56 +10:00
assert(seed.length >= 16, 'Seed should be at least 128 bits')
assert(seed.length <= 64, 'Seed should be at most 512 bits')
2014-07-11 16:33:39 +10:00
var I = crypto.createHmac('sha512', HDNode.MASTER_SECRET).update(seed).digest()
2014-05-31 20:16:42 +10:00
var IL = I.slice(0, 32)
var IR = I.slice(32)
// In case IL is 0 or >= n, the master key is invalid
2014-06-03 17:08:42 +10:00
// This is handled by `new ECKey` in the HDNode constructor
2014-05-31 20:16:42 +10:00
var pIL = BigInteger.fromBuffer(IL)
2014-06-03 17:08:42 +10:00
return new HDNode(pIL, IR, network)
2014-05-31 20:16:42 +10:00
}
2015-02-23 10:36:57 +11:00
HDNode.fromSeedHex = function (hex, network) {
2014-06-03 17:08:42 +10:00
return HDNode.fromSeedBuffer(new Buffer(hex, 'hex'), network)
}
2015-02-23 10:36:57 +11:00
HDNode.fromBase58 = function (string, network) {
return HDNode.fromBuffer(base58check.decode(string), network, true)
}
// FIXME: remove in 2.x.y
2015-02-23 10:36:57 +11:00
HDNode.fromBuffer = function (buffer, network, __ignoreDeprecation) {
if (!__ignoreDeprecation) {
console.warn('HDNode.fromBuffer() is deprecated for removal in 2.x.y, use fromBase58 instead')
}
2014-06-03 17:08:42 +10:00
assert.strictEqual(buffer.length, HDNode.LENGTH, 'Invalid buffer length')
2014-03-31 11:47:47 +08:00
2014-04-17 06:18:31 +10:00
// 4 byte: version bytes
2014-05-31 20:16:42 +10:00
var version = buffer.readUInt32BE(0)
if (network) {
2015-02-23 10:36:57 +11:00
assert(version === network.bip32.private || version === network.bip32.public, "Network doesn't match")
2014-12-01 10:43:52 +11:00
// auto-detect
} else {
2014-12-01 10:43:52 +11:00
network = findBIP32NetworkByVersion(version)
}
2014-03-31 11:47:47 +08:00
// 1 byte: depth: 0x00 for master nodes, 0x01 for level-1 descendants, ...
2014-05-31 20:16:42 +10:00
var depth = buffer.readUInt8(4)
2014-03-31 11:47:47 +08:00
// 4 bytes: the fingerprint of the parent's key (0x00000000 if master key)
2014-05-31 20:16:42 +10:00
var parentFingerprint = buffer.readUInt32BE(5)
if (depth === 0) {
assert.strictEqual(parentFingerprint, 0x00000000, 'Invalid parent fingerprint')
2014-04-17 06:18:31 +10:00
}
2014-03-31 11:47:47 +08:00
// 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)
2014-05-31 20:16:42 +10:00
var index = buffer.readUInt32BE(9)
assert(depth > 0 || index === 0, 'Invalid index')
2014-03-31 11:47:47 +08:00
// 32 bytes: the chain code
2014-05-31 20:16:42 +10:00
var chainCode = buffer.slice(13, 45)
var data, hd
2014-06-04 14:47:39 +10:00
// 33 bytes: private key data (0x00 + k)
2014-12-01 10:43:52 +11:00
if (version === network.bip32.private) {
assert.strictEqual(buffer.readUInt8(45), 0x00, 'Invalid private key')
data = buffer.slice(46, 78)
2014-06-07 16:24:27 +10:00
var d = BigInteger.fromBuffer(data)
hd = new HDNode(d, chainCode, network)
2014-05-31 20:16:42 +10:00
2014-06-04 14:47:39 +10:00
// 33 bytes: public key data (0x02 + X or 0x03 + X)
} else {
data = buffer.slice(45, 78)
2014-06-07 16:24:27 +10:00
var Q = ecurve.Point.decodeFrom(curve, data)
assert.equal(Q.compressed, true, 'Invalid public key')
2014-05-31 20:16:42 +10:00
// Verify that the X coordinate in the public point corresponds to a point on the curve.
// If not, the extended public key is invalid.
2014-06-07 16:24:27 +10:00
curve.validate(Q)
2014-05-31 20:28:16 +10:00
hd = new HDNode(Q, chainCode, network)
2014-03-31 11:47:47 +08:00
}
2014-05-31 20:16:42 +10:00
hd.depth = depth
hd.index = index
hd.parentFingerprint = parentFingerprint
2014-03-31 11:47:47 +08:00
return hd
}
// FIXME: remove in 2.x.y
2015-02-23 10:36:57 +11:00
HDNode.fromHex = function (hex, network) {
return HDNode.fromBuffer(new Buffer(hex, 'hex'), network)
2014-06-01 16:39:07 +10:00
}
2015-02-23 10:36:57 +11:00
HDNode.prototype.getIdentifier = function () {
return bcrypto.hash160(this.pubKey.toBuffer())
}
2015-02-23 10:36:57 +11:00
HDNode.prototype.getFingerprint = function () {
2014-04-17 06:18:31 +10:00
return this.getIdentifier().slice(0, 4)
}
2015-02-23 10:36:57 +11:00
HDNode.prototype.getAddress = function () {
return this.pubKey.getAddress(this.network)
}
2015-02-23 10:36:57 +11:00
HDNode.prototype.neutered = function () {
2014-07-29 01:34:46 +10:00
var neutered = new HDNode(this.pubKey.Q, this.chainCode, this.network)
neutered.depth = this.depth
neutered.index = this.index
neutered.parentFingerprint = this.parentFingerprint
return neutered
}
2015-02-23 10:36:57 +11:00
HDNode.prototype.toBase58 = function (isPrivate) {
return base58check.encode(this.toBuffer(isPrivate, true))
2014-06-03 16:19:44 +10:00
}
// FIXME: remove in 2.x.y
2015-02-23 10:36:57 +11:00
HDNode.prototype.toBuffer = function (isPrivate, __ignoreDeprecation) {
2014-10-15 19:08:39 +11:00
if (isPrivate === undefined) {
isPrivate = !!this.privKey
// FIXME: remove in 2.x.y
} else {
console.warn('isPrivate flag is deprecated, please use the .neutered() method instead')
}
if (!__ignoreDeprecation) {
console.warn('HDNode.toBuffer() is deprecated for removal in 2.x.y, use toBase58 instead')
}
2014-03-31 11:47:47 +08:00
// Version
var version = isPrivate ? this.network.bip32.private : this.network.bip32.public
2014-06-03 17:08:42 +10:00
var buffer = new Buffer(HDNode.LENGTH)
2014-03-31 11:47:47 +08:00
2014-04-17 06:18:31 +10:00
// 4 bytes: version bytes
buffer.writeUInt32BE(version, 0)
2014-03-31 11:47:47 +08:00
// Depth
// 1 byte: depth: 0x00 for master nodes, 0x01 for level-1 descendants, ....
2014-04-17 06:18:31 +10:00
buffer.writeUInt8(this.depth, 4)
2014-03-31 11:47:47 +08:00
// 4 bytes: the fingerprint of the parent's key (0x00000000 if master key)
buffer.writeUInt32BE(this.parentFingerprint, 5)
2014-03-31 11:47:47 +08:00
// 4 bytes: child number. This is the number i in xi = xpar/i, with xi the key being serialized.
2014-04-17 06:18:31 +10:00
// This is encoded in Big endian. (0x00000000 if master key)
buffer.writeUInt32BE(this.index, 9)
2014-03-31 11:47:47 +08:00
// 32 bytes: the chain code
2014-05-31 20:16:42 +10:00
this.chainCode.copy(buffer, 13)
2014-03-31 11:47:47 +08:00
// 33 bytes: the public key or private key data
if (isPrivate) {
// FIXME: remove in 2.x.y
assert(this.privKey, 'Missing private key')
2014-04-17 06:18:31 +10:00
// 0x00 + k for private keys
buffer.writeUInt8(0, 45)
this.privKey.d.toBuffer(32).copy(buffer, 46)
2014-03-31 11:47:47 +08:00
} else {
2014-04-17 06:18:31 +10:00
// X9.62 encoding for public keys
this.pubKey.toBuffer().copy(buffer, 45)
2014-03-31 11:47:47 +08:00
}
return buffer
}
2014-06-01 16:39:07 +10:00
// FIXME: remove in 2.x.y
2015-02-23 10:36:57 +11:00
HDNode.prototype.toHex = function (isPrivate) {
return this.toBuffer(isPrivate).toString('hex')
}
2014-05-10 09:41:18 +10:00
// https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#child-key-derivation-ckd-functions
2015-02-23 10:36:57 +11:00
HDNode.prototype.derive = function (index) {
2014-06-03 17:08:42 +10:00
var isHardened = index >= HDNode.HIGHEST_BIT
2014-05-10 09:41:18 +10:00
var indexBuffer = new Buffer(4)
indexBuffer.writeUInt32BE(index, 0)
2014-05-04 16:37:18 +10:00
2014-05-10 09:41:18 +10:00
var data
2014-05-10 09:41:18 +10:00
// Hardened child
if (isHardened) {
assert(this.privKey, 'Could not derive hardened child key')
2014-05-10 09:41:18 +10:00
// data = 0x00 || ser256(kpar) || ser32(index)
data = Buffer.concat([
this.privKey.d.toBuffer(33),
2014-05-10 09:41:18 +10:00
indexBuffer
])
2014-03-31 11:47:47 +08:00
2014-05-10 09:41:18 +10:00
// Normal child
2014-03-31 11:47:47 +08:00
} else {
2014-05-10 09:41:18 +10:00
// data = serP(point(kpar)) || ser32(index)
// = serP(Kpar) || ser32(index)
data = Buffer.concat([
this.pubKey.toBuffer(),
2014-05-10 09:41:18 +10:00
indexBuffer
])
2014-03-31 11:47:47 +08:00
}
var I = crypto.createHmac('sha512', this.chainCode).update(data).digest()
2014-05-10 09:41:18 +10:00
var IL = I.slice(0, 32)
var IR = I.slice(32)
2014-05-10 09:41:18 +10:00
var pIL = BigInteger.fromBuffer(IL)
2014-04-17 06:18:31 +10:00
2014-05-31 20:16:42 +10:00
// In case parse256(IL) >= n, proceed with the next value for i
2014-06-16 01:36:05 +10:00
if (pIL.compareTo(curve.n) >= 0) {
2014-05-31 20:16:42 +10:00
return this.derive(index + 1)
}
2014-05-10 09:41:18 +10:00
// Private parent key -> private child key
var hd
if (this.privKey) {
2014-05-10 09:41:18 +10:00
// ki = parse256(IL) + kpar (mod n)
2014-06-16 01:36:05 +10:00
var ki = pIL.add(this.privKey.d).mod(curve.n)
2014-05-10 09:41:18 +10:00
2014-05-31 20:16:42 +10:00
// In case ki == 0, proceed with the next value for i
if (ki.signum() === 0) {
2014-05-10 09:41:18 +10:00
return this.derive(index + 1)
}
2014-06-03 17:08:42 +10:00
hd = new HDNode(ki, IR, this.network)
2014-05-10 09:41:18 +10:00
// Public parent key -> public child key
2014-03-31 11:47:47 +08:00
} else {
2014-05-10 09:41:18 +10:00
// Ki = point(parse256(IL)) + Kpar
// = G*IL + Kpar
2014-06-16 01:36:05 +10:00
var Ki = curve.G.multiply(pIL).add(this.pubKey.Q)
2014-05-10 09:41:18 +10:00
2014-05-31 20:16:42 +10:00
// In case Ki is the point at infinity, proceed with the next value for i
2014-06-07 16:24:27 +10:00
if (curve.isInfinity(Ki)) {
2014-05-10 09:41:18 +10:00
return this.derive(index + 1)
}
2014-06-03 17:08:42 +10:00
hd = new HDNode(Ki, IR, this.network)
2014-03-31 11:47:47 +08:00
}
hd.depth = this.depth + 1
2014-05-10 09:41:18 +10:00
hd.index = index
2014-05-31 20:16:42 +10:00
hd.parentFingerprint = this.getFingerprint().readUInt32BE(0)
2014-05-10 09:41:18 +10:00
2014-03-31 11:47:47 +08:00
return hd
}
2015-02-23 10:36:57 +11:00
HDNode.prototype.deriveHardened = function (index) {
2014-05-10 09:41:18 +10:00
// Only derives hardened private keys by default
2014-06-03 17:08:42 +10:00
return this.derive(index + HDNode.HIGHEST_BIT)
}
2014-06-03 17:08:42 +10:00
HDNode.prototype.toString = HDNode.prototype.toBase58
2014-03-08 13:02:40 +08:00
2014-06-03 17:08:42 +10:00
module.exports = HDNode