2014-02-28 16:05:43 +08:00
|
|
|
var assert = require('assert')
|
2014-07-02 14:37:32 +10:00
|
|
|
var base58check = require('bs58check')
|
2014-12-08 11:24:16 +11:00
|
|
|
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-01-16 14:03:09 +07:00
|
|
|
|
2014-06-07 16:24:27 +10:00
|
|
|
var ecurve = require('ecurve')
|
|
|
|
var curve = ecurve.getCurveByName('secp256k1')
|
2014-04-17 06:45:57 +10:00
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
function findBIP32NetworkByVersion (version) {
|
2014-05-31 20:22:32 +10:00
|
|
|
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-05-31 20:22:32 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-01 10:43:52 +11:00
|
|
|
assert(false, 'Could not find network for ' + version.toString(16))
|
2014-05-31 20:22:32 +10:00
|
|
|
}
|
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
function HDNode (K, chainCode, network) {
|
2014-05-30 18:41:03 +10:00
|
|
|
network = network || networks.bitcoin
|
2014-02-28 17:26:16 +08:00
|
|
|
|
2014-12-23 15:08:20 +11:00
|
|
|
typeForce('Buffer', chainCode)
|
2014-09-15 14:21:01 +10:00
|
|
|
|
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-12 09:56:00 +10:00
|
|
|
|
2014-05-31 20:16:42 +10:00
|
|
|
this.chainCode = chainCode
|
2014-03-31 11:47:47 +08:00
|
|
|
this.depth = 0
|
2014-05-12 09:56:00 +10:00
|
|
|
this.index = 0
|
2014-12-05 15:07:30 +11:00
|
|
|
this.parentFingerprint = 0x00000000
|
2014-05-31 20:16:42 +10:00
|
|
|
this.network = network
|
|
|
|
|
|
|
|
if (K instanceof BigInteger) {
|
2014-06-03 17:30:05 +10:00
|
|
|
this.privKey = new ECKey(K, true)
|
|
|
|
this.pubKey = this.privKey.pub
|
2015-02-24 12:23:31 +11:00
|
|
|
} else if (K instanceof ECKey) {
|
|
|
|
assert(K.pub.compressed, 'ECKey must be compressed')
|
|
|
|
this.privKey = K
|
2015-02-20 17:16:47 -05:00
|
|
|
} else if (K instanceof ECPubKey) {
|
2015-02-24 12:23:31 +11:00
|
|
|
assert(K.compressed, 'ECPubKey must be compressed')
|
2015-02-20 17:16:47 -05:00
|
|
|
this.pubKey = K
|
2014-05-31 20:16:42 +10:00
|
|
|
} else {
|
2014-06-03 17:30:05 +10:00
|
|
|
this.pubKey = new ECPubKey(K, true)
|
2014-05-31 20:16:42 +10:00
|
|
|
}
|
2014-01-16 14:03:09 +07:00
|
|
|
}
|
|
|
|
|
2014-06-03 17:08:42 +10:00
|
|
|
HDNode.MASTER_SECRET = new Buffer('Bitcoin seed')
|
|
|
|
HDNode.HIGHEST_BIT = 0x80000000
|
|
|
|
HDNode.LENGTH = 78
|
2014-01-16 14:03:09 +07:00
|
|
|
|
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-09-15 14:21:01 +10:00
|
|
|
|
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
|
|
|
|
2014-12-08 11:24:16 +11: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)
|
2014-01-16 14:03:09 +07:00
|
|
|
}
|
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
HDNode.fromBase58 = function (string, network) {
|
2014-09-15 15:00:13 +10:00
|
|
|
var buffer = base58check.decode(string)
|
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)
|
2014-11-29 12:04:02 +11:00
|
|
|
|
|
|
|
if (network) {
|
2015-02-23 10:36:57 +11:00
|
|
|
assert(version === network.bip32.private || version === network.bip32.public, "Network doesn't match")
|
2014-11-29 12:04:02 +11:00
|
|
|
|
2014-12-01 10:43:52 +11:00
|
|
|
// auto-detect
|
2014-11-29 12:04:02 +11:00
|
|
|
} else {
|
2014-12-01 10:43:52 +11:00
|
|
|
network = findBIP32NetworkByVersion(version)
|
2014-11-29 12:04:02 +11:00
|
|
|
}
|
2014-01-16 14:03:09 +07:00
|
|
|
|
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-01-16 14:03:09 +07:00
|
|
|
|
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-01-16 14:03:09 +07: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-01-16 14:03:09 +07:00
|
|
|
|
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)
|
2014-09-15 14:39:15 +10:00
|
|
|
var data, hd
|
2014-06-04 14:47:39 +10:00
|
|
|
|
2015-03-02 13:31:03 +11:00
|
|
|
// 33 bytes: private key data (0x00 + k)
|
2014-12-01 10:43:52 +11:00
|
|
|
if (version === network.bip32.private) {
|
2014-06-04 14:36:06 +10:00
|
|
|
assert.strictEqual(buffer.readUInt8(45), 0x00, 'Invalid private key')
|
2014-09-15 14:39:15 +10:00
|
|
|
data = buffer.slice(46, 78)
|
2014-06-07 16:24:27 +10:00
|
|
|
var d = BigInteger.fromBuffer(data)
|
2014-11-29 12:04:02 +11:00
|
|
|
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)
|
2014-06-04 14:36:06 +10:00
|
|
|
} else {
|
2014-09-15 14:39:15 +10:00
|
|
|
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
|
|
|
|
2014-06-04 14:36:19 +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
|
|
|
|
2014-11-29 12:04:02 +11:00
|
|
|
hd = new HDNode(Q, chainCode, network)
|
2014-03-31 11:47:47 +08:00
|
|
|
}
|
2014-01-16 14:03:09 +07: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
|
2014-01-16 14:03:09 +07:00
|
|
|
}
|
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
HDNode.prototype.getIdentifier = function () {
|
2014-12-08 11:24:16 +11:00
|
|
|
return bcrypto.hash160(this.pubKey.toBuffer())
|
2014-01-16 14:03:09 +07:00
|
|
|
}
|
|
|
|
|
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)
|
2014-01-16 14:03:09 +07:00
|
|
|
}
|
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
HDNode.prototype.getAddress = function () {
|
2014-06-03 21:43:10 +10:00
|
|
|
return this.pubKey.getAddress(this.network)
|
2014-01-16 14:03:09 +07:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-09-15 15:00:13 +10:00
|
|
|
HDNode.prototype.toBase58 = function (__isPrivate) {
|
|
|
|
assert.strictEqual(__isPrivate, undefined, 'Unsupported argument in 2.0.0')
|
2014-09-15 16:04:12 +10:00
|
|
|
|
2014-03-31 11:47:47 +08:00
|
|
|
// Version
|
2014-09-15 15:00:13 +10:00
|
|
|
var version = this.privKey ? 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-01-16 14:03:09 +07:00
|
|
|
|
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)
|
2014-12-05 15:07:30 +11:00
|
|
|
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
|
|
|
|
2014-09-15 15:00:13 +10:00
|
|
|
// 33 bytes: the private key, or
|
|
|
|
if (this.privKey) {
|
2014-04-17 06:18:31 +10:00
|
|
|
// 0x00 + k for private keys
|
|
|
|
buffer.writeUInt8(0, 45)
|
2014-06-07 18:24:16 +10:00
|
|
|
this.privKey.d.toBuffer(32).copy(buffer, 46)
|
2014-09-15 15:00:13 +10:00
|
|
|
|
|
|
|
// 33 bytes: the public key
|
2014-03-31 11:47:47 +08:00
|
|
|
} else {
|
2014-04-17 06:18:31 +10:00
|
|
|
// X9.62 encoding for public keys
|
2014-06-03 17:30:05 +10:00
|
|
|
this.pubKey.toBuffer().copy(buffer, 45)
|
2014-03-31 11:47:47 +08:00
|
|
|
}
|
|
|
|
|
2014-09-15 15:00:13 +10:00
|
|
|
return base58check.encode(buffer)
|
2014-01-16 14:03:09 +07:00
|
|
|
}
|
|
|
|
|
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-01-16 14:03:09 +07:00
|
|
|
|
2014-05-10 09:41:18 +10:00
|
|
|
// Hardened child
|
|
|
|
if (isHardened) {
|
2014-06-03 17:30:05 +10:00
|
|
|
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([
|
2014-06-07 18:24:16 +10:00
|
|
|
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([
|
2014-06-03 17:30:05 +10:00
|
|
|
this.pubKey.toBuffer(),
|
2014-05-10 09:41:18 +10:00
|
|
|
indexBuffer
|
|
|
|
])
|
2014-03-31 11:47:47 +08:00
|
|
|
}
|
|
|
|
|
2014-12-08 11:24:16 +11: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-01-16 14:03:09 +07:00
|
|
|
|
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
|
2014-06-03 21:01:16 +10:00
|
|
|
var hd
|
2014-06-03 17:30:05 +10:00
|
|
|
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-04-17 06:45:57 +10:00
|
|
|
|
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-04-17 06:45:57 +10:00
|
|
|
|
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
|
2014-01-16 14:03:09 +07:00
|
|
|
}
|
|
|
|
|
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-01-16 14:03:09 +07:00
|
|
|
}
|
|
|
|
|
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
|