285 lines
7.9 KiB
JavaScript
285 lines
7.9 KiB
JavaScript
var assert = require('assert')
|
|
var base58check = require('bs58check')
|
|
var bcrypto = require('./crypto')
|
|
var createHmac = require('create-hmac')
|
|
var typeForce = require('typeforce')
|
|
var networks = require('./networks')
|
|
|
|
var BigInteger = require('bigi')
|
|
var ECPair = require('./ecpair')
|
|
|
|
var ecurve = require('ecurve')
|
|
var curve = ecurve.getCurveByName('secp256k1')
|
|
|
|
function findBIP32NetworkByVersion (version) {
|
|
for (var name in networks) {
|
|
var network = networks[name]
|
|
|
|
if (version === network.bip32.private || version === network.bip32.public) {
|
|
return network
|
|
}
|
|
}
|
|
|
|
assert(false, 'Could not find network for ' + version.toString(16))
|
|
}
|
|
|
|
function HDNode (keyPair, chainCode) {
|
|
typeForce('ECPair', keyPair)
|
|
typeForce('Buffer', chainCode)
|
|
|
|
assert.equal(chainCode.length, 32, 'Expected chainCode length of 32, got ' + chainCode.length)
|
|
assert('bip32' in keyPair.network, 'Unknown BIP32 constants for network')
|
|
assert.equal(keyPair.compressed, true, 'BIP32 only allows compressed keyPairs')
|
|
|
|
this.keyPair = keyPair
|
|
this.chainCode = chainCode
|
|
this.depth = 0
|
|
this.index = 0
|
|
this.parentFingerprint = 0x00000000
|
|
}
|
|
|
|
HDNode.MASTER_SECRET = new Buffer('Bitcoin seed')
|
|
HDNode.HIGHEST_BIT = 0x80000000
|
|
HDNode.LENGTH = 78
|
|
|
|
HDNode.fromSeedBuffer = function (seed, network) {
|
|
typeForce('Buffer', seed)
|
|
|
|
assert(seed.length >= 16, 'Seed should be at least 128 bits')
|
|
assert(seed.length <= 64, 'Seed should be at most 512 bits')
|
|
|
|
var I = createHmac('sha512', HDNode.MASTER_SECRET).update(seed).digest()
|
|
var IL = I.slice(0, 32)
|
|
var IR = I.slice(32)
|
|
|
|
// In case IL is 0 or >= n, the master key is invalid
|
|
// This is handled by the ECPair constructor
|
|
var pIL = BigInteger.fromBuffer(IL)
|
|
var keyPair = new ECPair(pIL, null, {
|
|
network: network
|
|
})
|
|
|
|
return new HDNode(keyPair, IR)
|
|
}
|
|
|
|
HDNode.fromSeedHex = function (hex, network) {
|
|
return HDNode.fromSeedBuffer(new Buffer(hex, 'hex'), network)
|
|
}
|
|
|
|
HDNode.fromBase58 = function (string, network) {
|
|
var buffer = base58check.decode(string)
|
|
assert.strictEqual(buffer.length, HDNode.LENGTH, 'Invalid buffer length')
|
|
|
|
// 4 byte: version bytes
|
|
var version = buffer.readUInt32BE(0)
|
|
|
|
if (network) {
|
|
assert(version === network.bip32.private || version === network.bip32.public, "Network doesn't match")
|
|
|
|
// auto-detect
|
|
} else {
|
|
network = findBIP32NetworkByVersion(version)
|
|
}
|
|
|
|
// 1 byte: depth: 0x00 for master nodes, 0x01 for level-1 descendants, ...
|
|
var depth = buffer.readUInt8(4)
|
|
|
|
// 4 bytes: the fingerprint of the parent's key (0x00000000 if master key)
|
|
var parentFingerprint = buffer.readUInt32BE(5)
|
|
if (depth === 0) {
|
|
assert.strictEqual(parentFingerprint, 0x00000000, '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)
|
|
var index = buffer.readUInt32BE(9)
|
|
assert(depth > 0 || index === 0, 'Invalid index')
|
|
|
|
// 32 bytes: the chain code
|
|
var chainCode = buffer.slice(13, 45)
|
|
var data, keyPair
|
|
|
|
// 33 bytes: private key data (0x00 + k)
|
|
if (version === network.bip32.private) {
|
|
assert.strictEqual(buffer.readUInt8(45), 0x00, 'Invalid private key')
|
|
data = buffer.slice(46, 78)
|
|
var d = BigInteger.fromBuffer(data)
|
|
|
|
keyPair = new ECPair(d, null, {
|
|
network: network
|
|
})
|
|
|
|
// 33 bytes: public key data (0x02 + X or 0x03 + X)
|
|
} else {
|
|
data = buffer.slice(45, 78)
|
|
var Q = ecurve.Point.decodeFrom(curve, data)
|
|
assert.equal(Q.compressed, true, 'Invalid public key')
|
|
|
|
// Verify that the X coordinate in the public point corresponds to a point on the curve.
|
|
// If not, the extended public key is invalid.
|
|
curve.validate(Q)
|
|
|
|
keyPair = new ECPair(null, Q, {
|
|
network: network
|
|
})
|
|
}
|
|
|
|
var hd = new HDNode(keyPair, chainCode)
|
|
hd.depth = depth
|
|
hd.index = index
|
|
hd.parentFingerprint = parentFingerprint
|
|
|
|
return hd
|
|
}
|
|
|
|
HDNode.prototype.getIdentifier = function () {
|
|
return bcrypto.hash160(this.keyPair.getPublicKeyBuffer())
|
|
}
|
|
|
|
HDNode.prototype.getFingerprint = function () {
|
|
return this.getIdentifier().slice(0, 4)
|
|
}
|
|
|
|
HDNode.prototype.getAddress = function () {
|
|
return this.keyPair.getAddress()
|
|
}
|
|
|
|
HDNode.prototype.neutered = function () {
|
|
var neuteredKeyPair = new ECPair(null, this.keyPair.Q, {
|
|
network: this.keyPair.network
|
|
})
|
|
|
|
var neutered = new HDNode(neuteredKeyPair, this.chainCode)
|
|
neutered.depth = this.depth
|
|
neutered.index = this.index
|
|
neutered.parentFingerprint = this.parentFingerprint
|
|
|
|
return neutered
|
|
}
|
|
|
|
HDNode.prototype.toBase58 = function (__isPrivate) {
|
|
assert.strictEqual(__isPrivate, undefined, 'Unsupported argument in 2.0.0')
|
|
|
|
// Version
|
|
var network = this.keyPair.network
|
|
var version = this.keyPair.d ? network.bip32.private : network.bip32.public
|
|
var buffer = new Buffer(HDNode.LENGTH)
|
|
|
|
// 4 bytes: version bytes
|
|
buffer.writeUInt32BE(version, 0)
|
|
|
|
// Depth
|
|
// 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.keyPair.d) {
|
|
// 0x00 + k for private keys
|
|
buffer.writeUInt8(0, 45)
|
|
this.keyPair.d.toBuffer(32).copy(buffer, 46)
|
|
|
|
// 33 bytes: the public key
|
|
} else {
|
|
// X9.62 encoding for public keys
|
|
this.keyPair.getPublicKeyBuffer().copy(buffer, 45)
|
|
}
|
|
|
|
return base58check.encode(buffer)
|
|
}
|
|
|
|
// https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#child-key-derivation-ckd-functions
|
|
HDNode.prototype.derive = function (index) {
|
|
var isHardened = index >= HDNode.HIGHEST_BIT
|
|
var indexBuffer = new Buffer(4)
|
|
indexBuffer.writeUInt32BE(index, 0)
|
|
|
|
var data
|
|
|
|
// Hardened child
|
|
if (isHardened) {
|
|
assert(this.keyPair.d, 'Could not derive hardened child key')
|
|
|
|
// data = 0x00 || ser256(kpar) || ser32(index)
|
|
data = Buffer.concat([
|
|
this.keyPair.d.toBuffer(33),
|
|
indexBuffer
|
|
])
|
|
|
|
// Normal child
|
|
} else {
|
|
// data = serP(point(kpar)) || ser32(index)
|
|
// = serP(Kpar) || ser32(index)
|
|
data = Buffer.concat([
|
|
this.keyPair.getPublicKeyBuffer(),
|
|
indexBuffer
|
|
])
|
|
}
|
|
|
|
var I = createHmac('sha512', this.chainCode).update(data).digest()
|
|
var IL = I.slice(0, 32)
|
|
var IR = I.slice(32)
|
|
|
|
var pIL = BigInteger.fromBuffer(IL)
|
|
|
|
// In case parse256(IL) >= n, proceed with the next value for i
|
|
if (pIL.compareTo(curve.n) >= 0) {
|
|
return this.derive(index + 1)
|
|
}
|
|
|
|
// Private parent key -> private child key
|
|
var derivedKeyPair
|
|
if (this.keyPair.d) {
|
|
// ki = parse256(IL) + kpar (mod n)
|
|
var ki = pIL.add(this.keyPair.d).mod(curve.n)
|
|
|
|
// In case ki == 0, proceed with the next value for i
|
|
if (ki.signum() === 0) {
|
|
return this.derive(index + 1)
|
|
}
|
|
|
|
derivedKeyPair = new ECPair(ki, null, {
|
|
network: this.keyPair.network
|
|
})
|
|
|
|
// Public parent key -> public child key
|
|
} else {
|
|
// Ki = point(parse256(IL)) + Kpar
|
|
// = G*IL + Kpar
|
|
var Ki = curve.G.multiply(pIL).add(this.keyPair.Q)
|
|
|
|
// In case Ki is the point at infinity, proceed with the next value for i
|
|
if (curve.isInfinity(Ki)) {
|
|
return this.derive(index + 1)
|
|
}
|
|
|
|
derivedKeyPair = new ECPair(null, Ki, {
|
|
network: this.keyPair.network
|
|
})
|
|
}
|
|
|
|
var hd = new HDNode(derivedKeyPair, IR)
|
|
hd.depth = this.depth + 1
|
|
hd.index = index
|
|
hd.parentFingerprint = this.getFingerprint().readUInt32BE(0)
|
|
|
|
return hd
|
|
}
|
|
|
|
HDNode.prototype.deriveHardened = function (index) {
|
|
// Only derives hardened private keys by default
|
|
return this.derive(index + HDNode.HIGHEST_BIT)
|
|
}
|
|
|
|
HDNode.prototype.toString = HDNode.prototype.toBase58
|
|
|
|
module.exports = HDNode
|