bitcoinjs-lib/src/hdnode.js

323 lines
8.9 KiB
JavaScript
Raw Normal View History

2014-07-02 14:37:32 +10:00
var base58check = require('bs58check')
var bcrypto = require('./crypto')
2015-03-17 12:31:53 +11:00
var createHmac = require('create-hmac')
var typeforce = require('typeforce')
var types = require('./types')
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-06-07 16:24:27 +10:00
var ecurve = require('ecurve')
var curve = ecurve.getCurveByName('secp256k1')
2015-03-02 16:48:36 +11:00
function HDNode (keyPair, chainCode) {
typeforce(types.tuple('ECPair', types.Buffer256bit), arguments)
2015-07-28 17:30:14 +10:00
if (!keyPair.compressed) throw new TypeError('BIP32 only allows compressed keyPairs')
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
this.index = 0
this.parentFingerprint = 0x00000000
}
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) {
typeforce(types.tuple(types.Buffer, types.maybe(types.Network)), arguments)
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)
}
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)
var network
// list of networks?
if (Array.isArray(networks)) {
network = networks.filter(function (network) {
return version === network.bip32.private ||
version === network.bip32.public
}).pop()
if (!network) throw new Error('Unknown network version')
// otherwise, assume a network object (or default to bitcoin)
} else {
network = networks || NETWORKS.bitcoin
}
if (version !== network.bip32.private &&
version !== network.bip32.public) throw new Error('Invalid network version')
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-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-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-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 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')
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)
} else {
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
// 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
}
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
}
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())
}
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-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-03-31 11:47:47 +08:00
// Version
2015-03-02 16:48:36 +11:00
var network = this.keyPair.network
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-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)
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.
// 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
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-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) {
typeforce(types.UInt32, index)
2015-09-09 12:25:50 +10:00
var isHardened = index >= HDNode.HIGHEST_BIT
var data = new Buffer(37)
2014-05-10 09:41:18 +10:00
// Hardened child
if (isHardened) {
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)
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)
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-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
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)
}
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)
}
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
}
2015-02-23 10:36:57 +11:00
HDNode.prototype.deriveHardened = function (index) {
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)
}
// Private === not neutered
// Public === neutered
HDNode.prototype.isNeutered = function () {
return !(this.keyPair.d)
}
HDNode.prototype.derivePath = function (path) {
typeforce(types.Bip32Path, path)
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) === "'") {
index = parseInt(indexStr.slice(0, -1), 10)
return prevHd.deriveHardened(index)
} else {
index = parseInt(indexStr, 10)
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