bitcoinjs-lib/src/hdwallet.js

251 lines
6.6 KiB
JavaScript
Raw Normal View History

var assert = require('assert')
var base58 = require('./base58')
var convert = require('./convert')
2014-04-17 06:18:31 +10:00
var Address = require('./address')
2014-05-03 10:04:54 +08:00
var BigInteger = require('bigi')
var CJS = require('crypto-js')
2014-04-08 22:00:28 +10:00
var crypto = require('./crypto')
var ECKey = require('./eckey').ECKey
var ECPubKey = require('./eckey').ECPubKey
var networks = require('./networks')
2014-04-22 02:19:30 +10:00
var sec = require('./sec')
var ecparams = sec("secp256k1")
2014-05-10 09:41:00 +10:00
function HmacSHA512(data, secret) {
assert(Buffer.isBuffer(data))
assert(Buffer.isBuffer(secret))
var dataWords = convert.bytesToWordArray(data)
var secretWords = convert.bytesToWordArray(secret)
var hash = CJS.HmacSHA512(dataWords, secretWords)
2014-04-17 06:18:31 +10:00
return new Buffer(convert.wordArrayToBytes(hash))
}
2014-04-17 19:49:03 +10:00
function HDWallet(seed, networkString) {
if (seed == undefined) return; // FIXME: Boo, should be stricter
2014-03-31 11:47:47 +08:00
2014-05-10 09:41:00 +10:00
var I = HmacSHA512(seed, new Buffer('Bitcoin seed'))
2014-03-31 11:47:47 +08:00
this.chaincode = I.slice(32)
2014-04-17 19:49:03 +10:00
this.network = networkString || 'bitcoin'
if(!networks.hasOwnProperty(this.network)) {
2014-03-31 11:47:47 +08:00
throw new Error("Unknown network: " + this.network)
}
2014-04-17 19:08:16 +10:00
this.priv = ECKey.fromBuffer(I.slice(0, 32), true)
this.pub = this.priv.pub
2014-03-31 11:47:47 +08:00
this.index = 0
this.depth = 0
}
HDWallet.HIGHEST_BIT = 0x80000000
HDWallet.LENGTH = 78
HDWallet.fromSeedHex = function(hex, networkString) {
return new HDWallet(new Buffer(hex, 'hex'), networkString)
}
HDWallet.fromBase58 = function(string) {
var buffer = base58.decode(string)
var payload = buffer.slice(0, -4)
var checksum = buffer.slice(-4)
var newChecksum = crypto.hash256(payload).slice(0, 4)
assert.deepEqual(newChecksum, checksum)
assert.equal(payload.length, HDWallet.LENGTH)
2014-04-17 06:18:31 +10:00
return HDWallet.fromBuffer(payload)
}
HDWallet.fromHex = function(input) {
2014-04-17 06:18:31 +10:00
return HDWallet.fromBuffer(new Buffer(input, 'hex'))
}
2014-04-17 06:18:31 +10:00
HDWallet.fromBuffer = function(input) {
assert.strictEqual(input.length, HDWallet.LENGTH, 'Invalid buffer length')
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
for(var name in networks) {
var network = networks[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-03-31 11:47:47 +08: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-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-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.strictEqual(hd.parentFingerprint, 0x00000000, '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-04-17 06:18:31 +10:00
hd.index = input.readUInt32BE(9)
assert(hd.depth > 0 || hd.index === 0, 'Invalid index')
2014-03-31 11:47:47 +08:00
// 32 bytes: the chain code
hd.chaincode = input.slice(13, 45)
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 19:08:16 +10:00
hd.priv = ECKey.fromBuffer(input.slice(46, 78), true)
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-03-31 11:47:47 +08:00
return hd
}
HDWallet.prototype.getIdentifier = function() {
2014-04-17 19:08:16 +10:00
return crypto.hash160(this.pub.toBuffer())
}
HDWallet.prototype.getFingerprint = function() {
2014-04-17 06:18:31 +10:00
return this.getIdentifier().slice(0, 4)
}
2014-03-22 18:17:54 +11:00
HDWallet.prototype.getAddress = function() {
2014-04-17 19:08:16 +10:00
return this.pub.getAddress(this.getKeyVersion())
}
2014-04-17 06:18:31 +10:00
HDWallet.prototype.toBuffer = function(priv) {
2014-03-31 11:47:47 +08:00
// Version
var version = networks[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-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, 'Missing private key')
2014-04-17 06:18:31 +10:00
// 0x00 + k for private keys
buffer.writeUInt8(0, 45)
2014-04-17 19:08:16 +10:00
this.priv.toBuffer().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
}
HDWallet.prototype.toHex = function(priv) {
2014-04-17 06:18:31 +10:00
return this.toBuffer(priv).toString('hex')
}
HDWallet.prototype.toBase58 = function(priv) {
2014-05-04 11:58:01 +10:00
var buffer = this.toBuffer(priv)
var checksum = crypto.hash256(buffer).slice(0, 4)
return base58.encode(Buffer.concat([
buffer,
checksum
]))
}
HDWallet.prototype.derive = function(i) {
2014-05-05 07:44:51 +10:00
var iBuffer = new Buffer(4)
iBuffer.writeUInt32BE(i, 0)
2014-05-04 16:37:18 +10:00
var cPar = this.chaincode
var usePriv = i >= HDWallet.HIGHEST_BIT
2014-03-31 11:47:47 +08:00
if (usePriv) {
assert(this.priv, 'Missing private key')
2014-03-31 11:47:47 +08:00
// If 1, private derivation is used:
// let I = HMAC-SHA512(Key = cpar, Data = 0x00 || kpar || i) [Note:]
2014-04-17 19:08:16 +10:00
var kPar = this.priv.toBuffer().slice(0, 32)
2014-05-05 07:44:51 +10:00
iBuffer = Buffer.concat([new Buffer([0]), kPar, iBuffer], 37)
2014-03-31 11:47:47 +08:00
} else {
// If 0, public derivation is used:
// let I = HMAC-SHA512(Key = cpar, Data = χ(kpar*G) || i)
2014-04-17 19:08:16 +10:00
var KPar = this.pub.toBuffer()
2014-05-05 07:44:51 +10:00
iBuffer = Buffer.concat([KPar, iBuffer])
2014-03-31 11:47:47 +08:00
}
2014-05-10 09:41:00 +10:00
var I = HmacSHA512(iBuffer, cPar)
2014-04-17 06:18:31 +10:00
2014-03-31 11:47:47 +08:00
// Split I = IL || IR into two 32-byte sequences, IL and IR.
var ILb = I.slice(0, 32)
, IRb = I.slice(32)
2014-03-31 11:47:47 +08:00
var hd = new HDWallet()
hd.network = this.network
2014-04-22 02:19:30 +10:00
var IL = BigInteger.fromBuffer(ILb)
2014-04-17 06:18:31 +10:00
2014-03-31 11:47:47 +08:00
if (this.priv) {
// ki = IL + kpar (mod n).
2014-04-17 19:08:16 +10:00
var ki = IL.add(this.priv.D).mod(ecparams.getN())
hd.priv = new ECKey(ki, true)
2014-04-17 19:08:16 +10:00
hd.pub = hd.priv.pub
2014-03-31 11:47:47 +08:00
} else {
// Ki = (IL + kpar)*G = IL*G + Kpar
2014-05-04 11:40:48 +10:00
var Ki = ecparams.getG().multiply(IL).add(this.pub.Q)
hd.pub = new ECPubKey(Ki, true)
2014-03-31 11:47:47 +08:00
}
// ci = IR.
hd.chaincode = IRb
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
}
HDWallet.prototype.derivePrivate = function(index) {
2014-03-31 11:47:47 +08:00
return this.derive(index + HDWallet.HIGHEST_BIT)
}
HDWallet.prototype.getKeyVersion = function() {
return networks[this.network].pubKeyHash
}
HDWallet.prototype.toString = HDWallet.prototype.toBase58
2014-03-08 13:02:40 +08:00
module.exports = HDWallet