2014-02-28 16:05:43 +08:00
|
|
|
var assert = require('assert')
|
2014-04-08 22:13:03 +10:00
|
|
|
var base58 = require('./base58')
|
2014-04-17 06:18:31 +10:00
|
|
|
|
2014-05-03 10:04:54 +08:00
|
|
|
var BigInteger = require('bigi')
|
2014-04-08 22:00:28 +10:00
|
|
|
var crypto = require('./crypto')
|
2014-05-13 16:44:29 +10:00
|
|
|
var ECKey = require('./eckey')
|
|
|
|
var ECPubKey = require('./ecpubkey')
|
2014-05-05 13:23:22 +10:00
|
|
|
var networks = require('./networks')
|
2014-01-16 14:03:09 +07:00
|
|
|
|
2014-04-22 02:19:30 +10:00
|
|
|
var sec = require('./sec')
|
2014-04-17 06:45:57 +10:00
|
|
|
var ecparams = sec("secp256k1")
|
|
|
|
|
2014-05-30 18:41:03 +10:00
|
|
|
function HDWallet(seed, network) {
|
2014-04-17 06:09:45 +10:00
|
|
|
if (seed == undefined) return; // FIXME: Boo, should be stricter
|
2014-03-31 11:47:47 +08:00
|
|
|
|
2014-05-30 18:41:03 +10:00
|
|
|
network = network || networks.bitcoin
|
|
|
|
assert(network.bip32, 'Unknown BIP32 constants for network')
|
2014-02-28 17:26:16 +08:00
|
|
|
|
2014-05-10 09:55:55 +10:00
|
|
|
var I = crypto.HmacSHA512(seed, HDWallet.MASTER_SECRET)
|
2014-05-12 09:56:00 +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 (handled by ECKey.fromBuffer)
|
2014-05-16 13:46:06 +10:00
|
|
|
var pIL = BigInteger.fromBuffer(IL)
|
|
|
|
|
2014-05-30 18:41:03 +10:00
|
|
|
this.network = network
|
2014-05-16 13:46:06 +10:00
|
|
|
this.priv = new ECKey(pIL, true)
|
2014-04-17 19:08:16 +10:00
|
|
|
this.pub = this.priv.pub
|
2014-05-12 09:56:00 +10:00
|
|
|
|
|
|
|
this.chaincode = IR
|
2014-03-31 11:47:47 +08:00
|
|
|
this.depth = 0
|
2014-05-12 09:56:00 +10:00
|
|
|
this.index = 0
|
2014-01-16 14:03:09 +07:00
|
|
|
}
|
|
|
|
|
2014-05-12 09:56:00 +10:00
|
|
|
HDWallet.MASTER_SECRET = new Buffer('Bitcoin seed')
|
2014-01-16 14:03:09 +07:00
|
|
|
HDWallet.HIGHEST_BIT = 0x80000000
|
|
|
|
HDWallet.LENGTH = 78
|
|
|
|
|
2014-05-30 18:41:03 +10:00
|
|
|
HDWallet.fromSeedHex = function(hex, network) {
|
|
|
|
return new HDWallet(new Buffer(hex, 'hex'), network)
|
2014-01-16 14:03:09 +07:00
|
|
|
}
|
|
|
|
|
2014-04-04 05:10:12 +11:00
|
|
|
HDWallet.fromBase58 = function(string) {
|
|
|
|
var buffer = base58.decode(string)
|
2014-01-16 14:03:09 +07:00
|
|
|
|
2014-04-04 05:10:12 +11:00
|
|
|
var payload = buffer.slice(0, -4)
|
|
|
|
var checksum = buffer.slice(-4)
|
2014-04-08 22:13:03 +10:00
|
|
|
var newChecksum = crypto.hash256(payload).slice(0, 4)
|
2014-01-16 14:03:09 +07:00
|
|
|
|
2014-05-30 19:00:49 +10:00
|
|
|
assert.deepEqual(newChecksum, checksum, 'Invalid checksum')
|
|
|
|
assert.equal(payload.length, HDWallet.LENGTH, 'Invalid BIP32 string')
|
2014-01-16 14:03:09 +07:00
|
|
|
|
2014-04-17 06:18:31 +10:00
|
|
|
return HDWallet.fromBuffer(payload)
|
2014-01-16 14:03:09 +07:00
|
|
|
}
|
|
|
|
|
2014-04-17 06:18:31 +10:00
|
|
|
HDWallet.fromBuffer = function(input) {
|
2014-04-18 06:19:14 +10:00
|
|
|
assert.strictEqual(input.length, HDWallet.LENGTH, 'Invalid buffer length')
|
2014-04-04 05:10:12 +11:00
|
|
|
|
2014-03-31 11:47:47 +08:00
|
|
|
var hd = new HDWallet()
|
|
|
|
|
2014-04-17 06:18:31 +10:00
|
|
|
// 4 byte: version bytes
|
|
|
|
var version = input.readUInt32BE(0)
|
2014-03-31 11:47:47 +08:00
|
|
|
|
2014-04-17 06:18:31 +10:00
|
|
|
var type
|
2014-05-30 18:41:03 +10:00
|
|
|
for (var name in networks) {
|
2014-05-05 13:23:22 +10:00
|
|
|
var network = networks[name]
|
2014-04-01 15:52:40 +11:00
|
|
|
|
2014-05-30 18:41:03 +10:00
|
|
|
for (var t in network.bip32) {
|
2014-04-17 06:18:31 +10:00
|
|
|
if (version != network.bip32[t]) continue
|
2014-04-01 15:52:40 +11:00
|
|
|
|
|
|
|
type = t
|
2014-05-30 18:41:03 +10:00
|
|
|
hd.network = network
|
2014-01-16 14:03:09 +07:00
|
|
|
}
|
2014-03-31 11:47:47 +08:00
|
|
|
}
|
2014-01-16 14:03:09 +07:00
|
|
|
|
2014-03-31 11:47:47 +08:00
|
|
|
if (!hd.network) {
|
2014-04-17 06:27:51 +10:00
|
|
|
throw new Error('Could not find version ' + version.toString(16))
|
2014-03-31 11:47:47 +08: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-04-17 06:18:31 +10:00
|
|
|
hd.depth = input.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-04-17 06:18:31 +10:00
|
|
|
hd.parentFingerprint = input.readUInt32BE(5)
|
|
|
|
if (hd.depth === 0) {
|
2014-04-18 06:19:14 +10:00
|
|
|
assert.strictEqual(hd.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-04-17 06:18:31 +10:00
|
|
|
hd.index = input.readUInt32BE(9)
|
2014-04-18 06:19:14 +10:00
|
|
|
assert(hd.depth > 0 || hd.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
|
|
|
|
hd.chaincode = input.slice(13, 45)
|
2014-01-16 14:03:09 +07:00
|
|
|
|
2014-03-31 11:47:47 +08:00
|
|
|
// 33 bytes: the public key or private key data (0x02 + X or 0x03 + X for
|
|
|
|
// public keys, 0x00 + k for private keys)
|
|
|
|
if (type == 'priv') {
|
2014-05-16 13:46:06 +10:00
|
|
|
assert.equal(input.readUInt8(45), 0, 'Invalid private key')
|
|
|
|
var D = BigInteger.fromBuffer(input.slice(46, 78))
|
|
|
|
|
|
|
|
hd.priv = new ECKey(D, true)
|
2014-04-17 19:08:16 +10:00
|
|
|
hd.pub = hd.priv.pub
|
2014-03-31 11:47:47 +08:00
|
|
|
} else {
|
2014-04-17 19:08:16 +10:00
|
|
|
hd.pub = ECPubKey.fromBuffer(input.slice(45, 78), true)
|
2014-03-31 11:47:47 +08:00
|
|
|
}
|
2014-01-16 14:03:09 +07:00
|
|
|
|
2014-03-31 11:47:47 +08:00
|
|
|
return hd
|
2014-01-16 14:03:09 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
HDWallet.prototype.getIdentifier = function() {
|
2014-04-17 19:08:16 +10:00
|
|
|
return crypto.hash160(this.pub.toBuffer())
|
2014-01-16 14:03:09 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
HDWallet.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
|
|
|
}
|
|
|
|
|
2014-03-22 18:17:54 +11:00
|
|
|
HDWallet.prototype.getAddress = function() {
|
2014-05-31 18:53:39 +10:00
|
|
|
return this.pub.getAddress(this.network.pubKeyHash)
|
2014-01-16 14:03:09 +07:00
|
|
|
}
|
|
|
|
|
2014-04-17 06:18:31 +10:00
|
|
|
HDWallet.prototype.toBuffer = function(priv) {
|
2014-03-31 11:47:47 +08:00
|
|
|
// Version
|
2014-05-30 18:41:03 +10:00
|
|
|
var version = this.network.bip32[priv ? 'priv' : 'pub']
|
2014-04-17 06:18:31 +10:00
|
|
|
var buffer = new Buffer(HDWallet.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-04-17 06:18:31 +10:00
|
|
|
var fingerprint = this.depth ? this.parentFingerprint : 0x00000000
|
|
|
|
buffer.writeUInt32BE(fingerprint, 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-04-17 06:18:31 +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 (priv) {
|
2014-04-18 06:19:14 +10:00
|
|
|
assert(this.priv, 'Missing private key')
|
2014-04-17 06:18:31 +10:00
|
|
|
|
|
|
|
// 0x00 + k for private keys
|
|
|
|
buffer.writeUInt8(0, 45)
|
2014-05-16 13:46:06 +10:00
|
|
|
this.priv.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
|
2014-04-17 19:08:16 +10:00
|
|
|
this.pub.toBuffer().copy(buffer, 45)
|
2014-03-31 11:47:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return buffer
|
2014-01-16 14:03:09 +07:00
|
|
|
}
|
|
|
|
HDWallet.prototype.toHex = function(priv) {
|
2014-04-17 06:18:31 +10:00
|
|
|
return this.toBuffer(priv).toString('hex')
|
2014-01-16 14:03:09 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
HDWallet.prototype.toBase58 = function(priv) {
|
2014-05-04 11:58:01 +10:00
|
|
|
var buffer = this.toBuffer(priv)
|
2014-04-08 22:13:03 +10:00
|
|
|
var checksum = crypto.hash256(buffer).slice(0, 4)
|
2014-04-04 05:10:12 +11:00
|
|
|
|
|
|
|
return base58.encode(Buffer.concat([
|
|
|
|
buffer,
|
|
|
|
checksum
|
|
|
|
]))
|
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
|
|
|
|
HDWallet.prototype.derive = function(index) {
|
|
|
|
var isHardened = index >= HDWallet.HIGHEST_BIT
|
|
|
|
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) {
|
|
|
|
assert(this.priv, 'Could not derive hardened child key')
|
|
|
|
|
|
|
|
// data = 0x00 || ser256(kpar) || ser32(index)
|
|
|
|
data = Buffer.concat([
|
|
|
|
this.priv.D.toBuffer(33),
|
|
|
|
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.pub.toBuffer(),
|
|
|
|
indexBuffer
|
|
|
|
])
|
2014-03-31 11:47:47 +08:00
|
|
|
}
|
|
|
|
|
2014-05-10 09:55:55 +10:00
|
|
|
var I = crypto.HmacSHA512(data, this.chaincode)
|
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-03-31 11:47:47 +08:00
|
|
|
var hd = new HDWallet()
|
2014-05-10 09:41:18 +10:00
|
|
|
var pIL = BigInteger.fromBuffer(IL)
|
2014-04-17 06:18:31 +10:00
|
|
|
|
2014-05-10 09:41:18 +10:00
|
|
|
// Private parent key -> private child key
|
2014-03-31 11:47:47 +08:00
|
|
|
if (this.priv) {
|
2014-05-10 09:41:18 +10:00
|
|
|
// ki = parse256(IL) + kpar (mod n)
|
|
|
|
var ki = pIL.add(this.priv.D).mod(ecparams.getN())
|
|
|
|
|
|
|
|
// In case parse256(IL) >= n or ki == 0, one should proceed with the next value for i
|
|
|
|
if (pIL.compareTo(ecparams.getN()) >= 0 || ki.signum() === 0) {
|
|
|
|
return this.derive(index + 1)
|
|
|
|
}
|
2014-04-17 06:45:57 +10:00
|
|
|
|
|
|
|
hd.priv = new ECKey(ki, true)
|
2014-04-17 19:08:16 +10:00
|
|
|
hd.pub = hd.priv.pub
|
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
|
|
|
|
var Ki = ecparams.getG().multiply(pIL).add(this.pub.Q)
|
|
|
|
|
|
|
|
// In case parse256(IL) >= n or Ki is the point at infinity, one should proceed with the next value for i
|
|
|
|
if (pIL.compareTo(ecparams.getN()) >= 0 || Ki.isInfinity()) {
|
|
|
|
return this.derive(index + 1)
|
|
|
|
}
|
2014-04-17 06:45:57 +10:00
|
|
|
|
|
|
|
hd.pub = new ECPubKey(Ki, true)
|
2014-03-31 11:47:47 +08:00
|
|
|
}
|
|
|
|
|
2014-05-10 09:41:18 +10:00
|
|
|
hd.chaincode = IR
|
2014-03-31 11:47:47 +08:00
|
|
|
hd.depth = this.depth + 1
|
2014-05-10 09:41:18 +10:00
|
|
|
hd.network = this.network
|
|
|
|
hd.parentFingerprint = this.getFingerprint().readUInt32BE(0)
|
|
|
|
hd.index = index
|
|
|
|
|
2014-03-31 11:47:47 +08:00
|
|
|
return hd
|
2014-01-16 14:03:09 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
HDWallet.prototype.derivePrivate = function(index) {
|
2014-05-10 09:41:18 +10:00
|
|
|
// Only derives hardened private keys by default
|
2014-03-31 11:47:47 +08:00
|
|
|
return this.derive(index + HDWallet.HIGHEST_BIT)
|
2014-01-16 14:03:09 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
HDWallet.prototype.toString = HDWallet.prototype.toBase58
|
2014-03-08 13:02:40 +08:00
|
|
|
|
2014-04-04 05:10:12 +11:00
|
|
|
module.exports = HDWallet
|