2014-07-02 14:37:32 +10:00
|
|
|
var base58check = require('bs58check')
|
2014-12-08 11:24:16 +11:00
|
|
|
var bcrypto = require('./crypto')
|
2015-03-17 12:31:53 +11:00
|
|
|
var createHmac = require('create-hmac')
|
2015-08-18 14:21:38 +10:00
|
|
|
var typeforce = require('typeforce')
|
|
|
|
var types = require('./types')
|
2015-07-28 17:47:18 +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')
|
2015-03-02 16:48:36 +11:00
|
|
|
var ECPair = require('./ecpair')
|
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-03-02 16:48:36 +11:00
|
|
|
function HDNode (keyPair, chainCode) {
|
2015-08-18 14:21:38 +10:00
|
|
|
typeforce(types.tuple('ECPair', types.Buffer256bit), arguments)
|
2014-09-15 14:21:01 +10:00
|
|
|
|
2015-07-28 17:30:14 +10:00
|
|
|
if (!keyPair.compressed) throw new TypeError('BIP32 only allows compressed keyPairs')
|
2014-05-12 09:56:00 +10:00
|
|
|
|
2015-03-02 16:48:36 +11:00
|
|
|
this.keyPair = keyPair
|
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-01-16 14:03:09 +07:00
|
|
|
}
|
|
|
|
|
2015-09-09 12:25:50 +10:00
|
|
|
HDNode.HIGHEST_BIT = 0x80000000
|
|
|
|
HDNode.LENGTH = 78
|
|
|
|
HDNode.MASTER_SECRET = new Buffer('Bitcoin seed')
|
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
HDNode.fromSeedBuffer = function (seed, network) {
|
2015-08-18 14:21:38 +10:00
|
|
|
typeforce(types.tuple(types.Buffer, types.maybe(types.Network)), arguments)
|
2014-09-15 14:21:01 +10:00
|
|
|
|
2015-07-28 17:30:14 +10:00
|
|
|
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')
|
2014-07-11 16:33:39 +10:00
|
|
|
|
2015-09-09 12:25:50 +10:00
|
|
|
var I = 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
|
2015-03-02 16:48:36 +11:00
|
|
|
// This is handled by the ECPair constructor
|
2014-05-31 20:16:42 +10:00
|
|
|
var pIL = BigInteger.fromBuffer(IL)
|
2015-03-02 16:48:36 +11:00
|
|
|
var keyPair = new ECPair(pIL, null, {
|
|
|
|
network: network
|
|
|
|
})
|
2014-05-31 20:16:42 +10:00
|
|
|
|
2015-03-02 16:48:36 +11:00
|
|
|
return new HDNode(keyPair, IR)
|
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-07-28 17:47:18 +10:00
|
|
|
HDNode.fromBase58 = function (string, networks) {
|
2014-09-15 15:00:13 +10:00
|
|
|
var buffer = base58check.decode(string)
|
2015-08-25 20:49:08 +10:00
|
|
|
if (buffer.length !== 78) throw new Error('Invalid buffer length')
|
2014-03-31 11:47:47 +08:00
|
|
|
|
2015-08-25 20:49:08 +10:00
|
|
|
// 4 bytes: version bytes
|
2014-05-31 20:16:42 +10:00
|
|
|
var version = buffer.readUInt32BE(0)
|
2015-07-28 17:47:18 +10:00
|
|
|
var network
|
2014-11-29 12:04:02 +11:00
|
|
|
|
2015-07-28 17:47:18 +10:00
|
|
|
// list of networks?
|
|
|
|
if (Array.isArray(networks)) {
|
|
|
|
network = networks.filter(function (network) {
|
|
|
|
return version === network.bip32.private ||
|
|
|
|
version === network.bip32.public
|
2016-02-25 13:41:35 +11:00
|
|
|
}).pop()
|
|
|
|
|
2016-02-25 13:48:29 +11:00
|
|
|
if (!network) throw new Error('Unknown network version')
|
2014-11-29 12:04:02 +11:00
|
|
|
|
2015-07-28 17:47:18 +10:00
|
|
|
// otherwise, assume a network object (or default to bitcoin)
|
2014-11-29 12:04:02 +11:00
|
|
|
} else {
|
2015-07-28 17:47:18 +10:00
|
|
|
network = networks || NETWORKS.bitcoin
|
2014-11-29 12:04:02 +11:00
|
|
|
}
|
2014-01-16 14:03:09 +07:00
|
|
|
|
2015-07-28 17:47:18 +10:00
|
|
|
if (version !== network.bip32.private &&
|
2016-02-25 13:41:35 +11:00
|
|
|
version !== network.bip32.public) throw new Error('Invalid network version')
|
2015-07-28 17:47:18 +10:00
|
|
|
|
2014-03-31 11:47:47 +08:00
|
|
|
// 1 byte: depth: 0x00 for master nodes, 0x01 for level-1 descendants, ...
|
2015-08-25 20:49:08 +10:00
|
|
|
var depth = buffer[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) {
|
2015-07-28 17:30:14 +10:00
|
|
|
if (parentFingerprint !== 0x00000000) throw new Error('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)
|
2015-07-28 17:30:14 +10:00
|
|
|
if (depth === 0 && index !== 0) throw new Error('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)
|
2015-08-20 14:23:27 +10:00
|
|
|
var keyPair
|
2014-06-04 14:47:39 +10:00
|
|
|
|
2015-03-19 13:35:25 +11:00
|
|
|
// 33 bytes: private key data (0x00 + k)
|
2014-12-01 10:43:52 +11:00
|
|
|
if (version === network.bip32.private) {
|
2015-07-28 17:30:14 +10:00
|
|
|
if (buffer.readUInt8(45) !== 0x00) throw new Error('Invalid private key')
|
|
|
|
|
2015-08-20 14:23:27 +10:00
|
|
|
var d = BigInteger.fromBuffer(buffer.slice(46, 78))
|
2015-03-02 16:48:36 +11:00
|
|
|
|
|
|
|
keyPair = new ECPair(d, null, {
|
|
|
|
network: 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 {
|
2015-08-20 14:23:27 +10:00
|
|
|
var Q = ecurve.Point.decodeFrom(curve, buffer.slice(45, 78))
|
2015-07-28 17:30:14 +10:00
|
|
|
if (!Q.compressed) throw new Error('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
|
|
|
|
2015-03-02 16:48:36 +11:00
|
|
|
keyPair = new ECPair(null, Q, {
|
|
|
|
network: network
|
|
|
|
})
|
2014-03-31 11:47:47 +08:00
|
|
|
}
|
2014-01-16 14:03:09 +07:00
|
|
|
|
2015-03-02 16:48:36 +11:00
|
|
|
var hd = new HDNode(keyPair, chainCode)
|
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-09-12 15:10:44 +10:00
|
|
|
HDNode.prototype.getAddress = function () {
|
|
|
|
return this.keyPair.getAddress()
|
|
|
|
}
|
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
HDNode.prototype.getIdentifier = function () {
|
2015-03-02 16:48:36 +11:00
|
|
|
return bcrypto.hash160(this.keyPair.getPublicKeyBuffer())
|
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-09-21 17:37:21 +10:00
|
|
|
HDNode.prototype.getNetwork = function () {
|
|
|
|
return this.keyPair.getNetwork()
|
|
|
|
}
|
|
|
|
|
2015-09-21 16:44:53 +10:00
|
|
|
HDNode.prototype.getPublicKeyBuffer = function () {
|
|
|
|
return this.keyPair.getPublicKeyBuffer()
|
|
|
|
}
|
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
HDNode.prototype.neutered = function () {
|
2015-03-02 16:48:36 +11:00
|
|
|
var neuteredKeyPair = new ECPair(null, this.keyPair.Q, {
|
|
|
|
network: this.keyPair.network
|
|
|
|
})
|
|
|
|
|
|
|
|
var neutered = new HDNode(neuteredKeyPair, this.chainCode)
|
2014-07-29 01:34:46 +10:00
|
|
|
neutered.depth = this.depth
|
|
|
|
neutered.index = this.index
|
|
|
|
neutered.parentFingerprint = this.parentFingerprint
|
|
|
|
|
|
|
|
return neutered
|
|
|
|
}
|
|
|
|
|
2015-09-21 16:34:25 +10:00
|
|
|
HDNode.prototype.sign = function (hash) {
|
|
|
|
return this.keyPair.sign(hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
HDNode.prototype.verify = function (hash, signature) {
|
|
|
|
return this.keyPair.verify(hash, signature)
|
|
|
|
}
|
|
|
|
|
2014-09-15 15:00:13 +10:00
|
|
|
HDNode.prototype.toBase58 = function (__isPrivate) {
|
2015-07-28 17:30:14 +10:00
|
|
|
if (__isPrivate !== undefined) throw new TypeError('Unsupported argument in 2.0.0')
|
2014-09-15 16:04:12 +10:00
|
|
|
|
2014-03-31 11:47:47 +08:00
|
|
|
// Version
|
2015-03-02 16:48:36 +11:00
|
|
|
var network = this.keyPair.network
|
2016-02-05 04:43:56 +01:00
|
|
|
var version = (!this.isNeutered()) ? network.bip32.private : network.bip32.public
|
2015-08-25 20:49:08 +10:00
|
|
|
var buffer = new Buffer(78)
|
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
|
|
|
// 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.
|
2015-08-22 12:53:47 +10:00
|
|
|
// This is encoded in big endian. (0x00000000 if master key)
|
2014-04-17 06:18:31 +10:00
|
|
|
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
|
|
|
|
2015-03-02 16:48:36 +11:00
|
|
|
// 33 bytes: the public key or private key data
|
2016-02-05 04:43:56 +01:00
|
|
|
if (!this.isNeutered()) {
|
2014-04-17 06:18:31 +10:00
|
|
|
// 0x00 + k for private keys
|
|
|
|
buffer.writeUInt8(0, 45)
|
2015-03-02 16:48:36 +11:00
|
|
|
this.keyPair.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
|
2015-03-02 16:48:36 +11:00
|
|
|
this.keyPair.getPublicKeyBuffer().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) {
|
2016-02-06 17:12:13 +01:00
|
|
|
typeforce(types.UInt32, index)
|
|
|
|
|
2015-09-09 12:25:50 +10:00
|
|
|
var isHardened = index >= HDNode.HIGHEST_BIT
|
2015-08-20 14:23:27 +10:00
|
|
|
var data = new Buffer(37)
|
2014-01-16 14:03:09 +07:00
|
|
|
|
2014-05-10 09:41:18 +10:00
|
|
|
// Hardened child
|
|
|
|
if (isHardened) {
|
2016-02-05 04:43:56 +01:00
|
|
|
if (this.isNeutered()) throw new TypeError('Could not derive hardened child key')
|
2014-05-10 09:41:18 +10:00
|
|
|
|
|
|
|
// data = 0x00 || ser256(kpar) || ser32(index)
|
2015-08-20 14:23:27 +10:00
|
|
|
data[0] = 0x00
|
|
|
|
this.keyPair.d.toBuffer(32).copy(data, 1)
|
|
|
|
data.writeUInt32BE(index, 33)
|
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)
|
2015-08-20 14:23:27 +10:00
|
|
|
this.keyPair.getPublicKeyBuffer().copy(data, 0)
|
|
|
|
data.writeUInt32BE(index, 33)
|
2014-03-31 11:47:47 +08:00
|
|
|
}
|
|
|
|
|
2015-03-17 12:31:53 +11:00
|
|
|
var I = 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
|
2015-03-02 16:48:36 +11:00
|
|
|
var derivedKeyPair
|
2016-02-05 04:43:56 +01:00
|
|
|
if (!this.isNeutered()) {
|
2014-05-10 09:41:18 +10:00
|
|
|
// ki = parse256(IL) + kpar (mod n)
|
2015-03-02 16:48:36 +11:00
|
|
|
var ki = pIL.add(this.keyPair.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
|
|
|
|
2015-03-02 16:48:36 +11:00
|
|
|
derivedKeyPair = new ECPair(ki, null, {
|
|
|
|
network: this.keyPair.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
|
2015-03-02 16:48:36 +11:00
|
|
|
var Ki = curve.G.multiply(pIL).add(this.keyPair.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
|
|
|
|
2015-03-02 16:48:36 +11:00
|
|
|
derivedKeyPair = new ECPair(null, Ki, {
|
|
|
|
network: this.keyPair.network
|
|
|
|
})
|
2014-03-31 11:47:47 +08:00
|
|
|
}
|
|
|
|
|
2015-03-02 16:48:36 +11:00
|
|
|
var hd = new HDNode(derivedKeyPair, IR)
|
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) {
|
2016-02-06 17:12:13 +01:00
|
|
|
typeforce(types.UInt31, index)
|
|
|
|
|
2014-05-10 09:41:18 +10:00
|
|
|
// Only derives hardened private keys by default
|
2015-09-09 12:25:50 +10:00
|
|
|
return this.derive(index + HDNode.HIGHEST_BIT)
|
2014-01-16 14:03:09 +07:00
|
|
|
}
|
|
|
|
|
2016-02-05 04:43:56 +01:00
|
|
|
// Private === not neutered
|
|
|
|
// Public === neutered
|
|
|
|
HDNode.prototype.isNeutered = function () {
|
|
|
|
return !(this.keyPair.d)
|
|
|
|
}
|
|
|
|
|
2016-02-06 02:01:20 +01:00
|
|
|
HDNode.prototype.derivePath = function (path) {
|
2016-02-14 16:12:16 +01:00
|
|
|
typeforce(types.Bip32Path, path)
|
2016-02-06 02:01:20 +01:00
|
|
|
|
|
|
|
var splitPath = path.split('/')
|
|
|
|
if (splitPath[0] === 'm') {
|
|
|
|
if (this.parentFingerprint) {
|
|
|
|
throw new Error('Not a master node')
|
|
|
|
}
|
|
|
|
|
|
|
|
splitPath = splitPath.slice(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return splitPath.reduce(function (prevHd, indexStr) {
|
|
|
|
var index
|
|
|
|
if (indexStr.slice(-1) === "'") {
|
2016-02-13 15:51:42 +01:00
|
|
|
index = parseInt(indexStr.slice(0, -1), 10)
|
2016-02-06 02:01:20 +01:00
|
|
|
return prevHd.deriveHardened(index)
|
|
|
|
} else {
|
2016-02-13 15:51:42 +01:00
|
|
|
index = parseInt(indexStr, 10)
|
2016-02-06 02:01:20 +01:00
|
|
|
return prevHd.derive(index)
|
|
|
|
}
|
|
|
|
}, this)
|
|
|
|
}
|
|
|
|
|
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
|