2014-02-28 16:05:43 +08:00
|
|
|
var assert = require('assert')
|
2014-04-08 22:13:03 +10:00
|
|
|
var base58 = require('./base58')
|
|
|
|
var convert = require('./convert')
|
2014-04-17 06:18:31 +10:00
|
|
|
|
|
|
|
var Address = require('./address')
|
|
|
|
var BigInteger = require('./jsbn/jsbn')
|
2014-04-08 22:13:03 +10:00
|
|
|
var CJS = require('crypto-js')
|
2014-04-08 22:00:28 +10:00
|
|
|
var crypto = require('./crypto')
|
2014-04-08 22:13:03 +10:00
|
|
|
var ECKey = require('./eckey').ECKey
|
|
|
|
var ECPubKey = require('./eckey').ECPubKey
|
2014-02-28 16:05:43 +08:00
|
|
|
var Network = require('./network')
|
2014-01-16 14:03:09 +07:00
|
|
|
|
2014-04-08 22:13:03 +10:00
|
|
|
function HmacSHA512(buffer, secret) {
|
|
|
|
var words = convert.bytesToWordArray(buffer)
|
|
|
|
var hash = CJS.HmacSHA512(words, secret)
|
2014-04-04 05:10:12 +11:00
|
|
|
|
2014-04-17 06:18:31 +10:00
|
|
|
return new Buffer(convert.wordArrayToBytes(hash))
|
2014-04-04 05:10:12 +11:00
|
|
|
}
|
|
|
|
|
2014-04-17 06:09:45 +10:00
|
|
|
function HDWallet(seed, netstr) {
|
|
|
|
if (seed == undefined) return; // FIXME: Boo, should be stricter
|
2014-03-31 11:47:47 +08:00
|
|
|
|
2014-04-08 22:13:03 +10:00
|
|
|
var I = HmacSHA512(seed, 'Bitcoin seed')
|
2014-03-31 11:47:47 +08:00
|
|
|
this.chaincode = I.slice(32)
|
2014-04-17 06:09:45 +10:00
|
|
|
this.network = netstr || 'bitcoin'
|
|
|
|
|
2014-03-31 11:47:47 +08:00
|
|
|
if(!Network.hasOwnProperty(this.network)) {
|
|
|
|
throw new Error("Unknown network: " + this.network)
|
|
|
|
}
|
2014-02-28 17:26:16 +08:00
|
|
|
|
2014-04-17 06:18:31 +10:00
|
|
|
this.priv = new ECKey(I.slice(0, 32), true)
|
2014-03-31 11:47:47 +08:00
|
|
|
this.pub = this.priv.getPub()
|
|
|
|
this.index = 0
|
|
|
|
this.depth = 0
|
2014-01-16 14:03:09 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
HDWallet.HIGHEST_BIT = 0x80000000
|
|
|
|
HDWallet.LENGTH = 78
|
|
|
|
|
2014-03-11 22:41:20 +08:00
|
|
|
HDWallet.fromSeedHex = function(hex, network) {
|
2014-04-17 06:18:31 +10:00
|
|
|
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-04-04 05:10:12 +11:00
|
|
|
assert.deepEqual(newChecksum, checksum)
|
|
|
|
assert.equal(payload.length, HDWallet.LENGTH)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
HDWallet.fromHex = function(input) {
|
2014-04-17 06:18:31 +10:00
|
|
|
return HDWallet.fromBuffer(new Buffer(input, 'hex'))
|
2014-01-16 14:03:09 +07:00
|
|
|
}
|
|
|
|
|
2014-04-17 06:18:31 +10:00
|
|
|
HDWallet.fromBuffer = function(input) {
|
|
|
|
assert(input.length === HDWallet.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-03-31 11:47:47 +08:00
|
|
|
for(var name in Network) {
|
|
|
|
var network = Network[name]
|
2014-04-01 15:52:40 +11:00
|
|
|
|
2014-04-17 05:43:34 +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-03-31 11:47:47 +08:00
|
|
|
hd.network = name
|
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) {
|
|
|
|
assert(hd.parentFingerprint === 0x00000000)
|
|
|
|
}
|
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-03-31 11:47:47 +08:00
|
|
|
assert(hd.depth > 0 || hd.index === 0)
|
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-04-17 06:18:31 +10:00
|
|
|
hd.priv = new ECKey(input.slice(46, 78), true)
|
2014-03-31 11:47:47 +08:00
|
|
|
hd.pub = hd.priv.getPub()
|
|
|
|
} else {
|
|
|
|
hd.pub = new ECPubKey(input.slice(45, 78), true)
|
|
|
|
}
|
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-08 22:13:03 +10:00
|
|
|
return crypto.hash160(this.pub.toBytes())
|
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-04-08 22:13:03 +10:00
|
|
|
return new Address(crypto.hash160(this.pub.toBytes()), this.getKeyVersion())
|
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-04-17 05:43:34 +10:00
|
|
|
var version = Network[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) {
|
|
|
|
assert(this.priv, 'Cannot serialize to private without private key')
|
2014-04-17 06:18:31 +10:00
|
|
|
|
|
|
|
// 0x00 + k for private keys
|
|
|
|
buffer.writeUInt8(0, 45)
|
|
|
|
new Buffer(this.priv.toBytes()).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
|
|
|
|
new Buffer(this.pub.toBytes()).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-04-17 06:18:31 +10:00
|
|
|
var buffer = new 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
|
|
|
}
|
|
|
|
|
|
|
|
HDWallet.prototype.derive = function(i) {
|
2014-04-17 06:18:31 +10:00
|
|
|
var iBytes = convert.numToBytes(i, 4).reverse()
|
2014-01-16 14:03:09 +07:00
|
|
|
, cPar = this.chaincode
|
|
|
|
, usePriv = i >= HDWallet.HIGHEST_BIT
|
2014-04-08 22:13:03 +10:00
|
|
|
, SHA512 = CJS.algo.SHA512
|
2014-01-16 14:03:09 +07:00
|
|
|
|
2014-04-17 06:18:31 +10:00
|
|
|
var I
|
2014-03-31 11:47:47 +08:00
|
|
|
if (usePriv) {
|
|
|
|
assert(this.priv, 'Private derive on public key')
|
|
|
|
|
|
|
|
// If 1, private derivation is used:
|
|
|
|
// let I = HMAC-SHA512(Key = cpar, Data = 0x00 || kpar || i) [Note:]
|
|
|
|
var kPar = this.priv.toBytes().slice(0, 32)
|
2014-04-17 06:18:31 +10:00
|
|
|
|
|
|
|
// FIXME: Dislikes buffers
|
2014-03-31 11:47:47 +08:00
|
|
|
I = HmacFromBytesToBytes(SHA512, [0].concat(kPar, iBytes), cPar)
|
|
|
|
} else {
|
|
|
|
// If 0, public derivation is used:
|
|
|
|
// let I = HMAC-SHA512(Key = cpar, Data = χ(kpar*G) || i)
|
2014-04-17 06:18:31 +10:00
|
|
|
var KPar = this.pub.toBytes()
|
|
|
|
|
|
|
|
// FIXME: Dislikes buffers
|
2014-03-31 11:47:47 +08:00
|
|
|
I = HmacFromBytesToBytes(SHA512, KPar.concat(iBytes), cPar)
|
|
|
|
}
|
|
|
|
|
2014-04-17 06:18:31 +10:00
|
|
|
// FIXME: Boo, CSJ.algo.SHA512 uses byte arrays
|
|
|
|
I = new Buffer(I)
|
|
|
|
|
2014-03-31 11:47:47 +08:00
|
|
|
// Split I = IL || IR into two 32-byte sequences, IL and IR.
|
|
|
|
var IL = I.slice(0, 32)
|
2014-01-16 14:03:09 +07:00
|
|
|
, IR = I.slice(32)
|
|
|
|
|
2014-03-31 11:47:47 +08:00
|
|
|
var hd = new HDWallet()
|
|
|
|
hd.network = this.network
|
|
|
|
|
2014-04-17 06:18:31 +10:00
|
|
|
var ILbytes = Buffer.concat([IL, new Buffer([0x01])])
|
|
|
|
var ILpriv = new ECKey(ILbytes, true)
|
|
|
|
|
2014-03-31 11:47:47 +08:00
|
|
|
if (this.priv) {
|
|
|
|
// ki = IL + kpar (mod n).
|
2014-04-17 06:18:31 +10:00
|
|
|
hd.priv = this.priv.add(ILpriv)
|
2014-03-31 11:47:47 +08:00
|
|
|
hd.pub = hd.priv.getPub()
|
|
|
|
} else {
|
|
|
|
// Ki = (IL + kpar)*G = IL*G + Kpar
|
2014-04-17 06:18:31 +10:00
|
|
|
hd.pub = this.pub.add(ILpriv.getPub())
|
2014-03-31 11:47:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ci = IR.
|
|
|
|
hd.chaincode = IR
|
2014-04-17 06:18:31 +10:00
|
|
|
hd.parentFingerprint = this.getFingerprint().readUInt32BE(0)
|
2014-03-31 11:47:47 +08:00
|
|
|
hd.depth = this.depth + 1
|
|
|
|
hd.index = i
|
|
|
|
hd.pub.compressed = true
|
|
|
|
return hd
|
2014-01-16 14:03:09 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
HDWallet.prototype.derivePrivate = function(index) {
|
2014-03-31 11:47:47 +08:00
|
|
|
return this.derive(index + HDWallet.HIGHEST_BIT)
|
2014-01-16 14:03:09 +07:00
|
|
|
}
|
|
|
|
|
2014-02-28 14:08:34 +08:00
|
|
|
HDWallet.prototype.getKeyVersion = function() {
|
2014-04-17 05:43:34 +10:00
|
|
|
return Network[this.network].pubKeyHash
|
2014-02-28 14:08:34 +08:00
|
|
|
}
|
|
|
|
|
2014-01-16 14:03:09 +07:00
|
|
|
HDWallet.prototype.toString = HDWallet.prototype.toBase58
|
2014-03-08 13:02:40 +08:00
|
|
|
|
2014-03-25 20:57:19 +01:00
|
|
|
function HmacFromBytesToBytes(hasher, message, key) {
|
2014-04-08 22:13:03 +10:00
|
|
|
var hmac = CJS.algo.HMAC.create(hasher, convert.bytesToWordArray(key))
|
2014-03-25 20:57:19 +01:00
|
|
|
hmac.update(convert.bytesToWordArray(message))
|
|
|
|
return convert.wordArrayToBytes(hmac.finalize())
|
|
|
|
}
|
|
|
|
|
2014-04-04 05:10:12 +11:00
|
|
|
module.exports = HDWallet
|