HDWallet: rename to HDNode

This commit is contained in:
Daniel Cousens 2014-06-03 17:08:42 +10:00
parent c340f5cf83
commit 64e307b13f
6 changed files with 70 additions and 70 deletions

View file

@ -28,7 +28,7 @@ function findBIP32ParamsByVersion(version) {
assert(false, 'Could not find version ' + version.toString(16)) assert(false, 'Could not find version ' + version.toString(16))
} }
function HDWallet(K, chainCode, network) { function HDNode(K, chainCode, network) {
network = network || networks.bitcoin network = network || networks.bitcoin
assert(Buffer.isBuffer(chainCode), 'Expected Buffer, got ' + chainCode) assert(Buffer.isBuffer(chainCode), 'Expected Buffer, got ' + chainCode)
@ -47,27 +47,27 @@ function HDWallet(K, chainCode, network) {
} }
} }
HDWallet.MASTER_SECRET = new Buffer('Bitcoin seed') HDNode.MASTER_SECRET = new Buffer('Bitcoin seed')
HDWallet.HIGHEST_BIT = 0x80000000 HDNode.HIGHEST_BIT = 0x80000000
HDWallet.LENGTH = 78 HDNode.LENGTH = 78
HDWallet.fromSeedBuffer = function(seed, network) { HDNode.fromSeedBuffer = function(seed, network) {
var I = crypto.HmacSHA512(seed, HDWallet.MASTER_SECRET) var I = crypto.HmacSHA512(seed, HDNode.MASTER_SECRET)
var IL = I.slice(0, 32) var IL = I.slice(0, 32)
var IR = I.slice(32) var IR = I.slice(32)
// In case IL is 0 or >= n, the master key is invalid // In case IL is 0 or >= n, the master key is invalid
// This is handled by `new ECKey` in the HDWallet constructor // This is handled by `new ECKey` in the HDNode constructor
var pIL = BigInteger.fromBuffer(IL) var pIL = BigInteger.fromBuffer(IL)
return new HDWallet(pIL, IR, network) return new HDNode(pIL, IR, network)
} }
HDWallet.fromSeedHex = function(hex, network) { HDNode.fromSeedHex = function(hex, network) {
return HDWallet.fromSeedBuffer(new Buffer(hex, 'hex'), network) return HDNode.fromSeedBuffer(new Buffer(hex, 'hex'), network)
} }
HDWallet.fromBase58 = function(string) { HDNode.fromBase58 = function(string) {
var buffer = base58.decode(string) var buffer = base58.decode(string)
var payload = buffer.slice(0, -4) var payload = buffer.slice(0, -4)
@ -76,11 +76,11 @@ HDWallet.fromBase58 = function(string) {
var newChecksum = crypto.hash256(payload).slice(0, 4) var newChecksum = crypto.hash256(payload).slice(0, 4)
assert.deepEqual(newChecksum, checksum, 'Invalid checksum') assert.deepEqual(newChecksum, checksum, 'Invalid checksum')
return HDWallet.fromBuffer(payload) return HDNode.fromBuffer(payload)
} }
HDWallet.fromBuffer = function(buffer) { HDNode.fromBuffer = function(buffer) {
assert.strictEqual(buffer.length, HDWallet.LENGTH, 'Invalid buffer length') assert.strictEqual(buffer.length, HDNode.LENGTH, 'Invalid buffer length')
// 4 byte: version bytes // 4 byte: version bytes
var version = buffer.readUInt32BE(0) var version = buffer.readUInt32BE(0)
@ -113,7 +113,7 @@ HDWallet.fromBuffer = function(buffer) {
data = data.slice(1) data = data.slice(1)
var D = BigInteger.fromBuffer(data) var D = BigInteger.fromBuffer(data)
hd = new HDWallet(D, chainCode, params.network) hd = new HDNode(D, chainCode, params.network)
} else { } else {
var decode = ECPointFp.decodeFrom(ecparams.getCurve(), data) var decode = ECPointFp.decodeFrom(ecparams.getCurve(), data)
@ -122,7 +122,7 @@ HDWallet.fromBuffer = function(buffer) {
// When importing a serialized extended public key, implementations must verify whether the X coordinate in the public key data corresponds to a point on the curve. If not, the extended public key is invalid. // When importing a serialized extended public key, implementations must verify whether the X coordinate in the public key data corresponds to a point on the curve. If not, the extended public key is invalid.
decode.Q.validate() decode.Q.validate()
hd = new HDWallet(decode.Q, chainCode, params.network) hd = new HDNode(decode.Q, chainCode, params.network)
} }
hd.depth = depth hd.depth = depth
@ -132,23 +132,23 @@ HDWallet.fromBuffer = function(buffer) {
return hd return hd
} }
HDWallet.fromHex = function(hex, isPrivate) { HDNode.fromHex = function(hex, isPrivate) {
return HDWallet.fromBuffer(new Buffer(hex, 'hex')) return HDNode.fromBuffer(new Buffer(hex, 'hex'))
} }
HDWallet.prototype.getIdentifier = function() { HDNode.prototype.getIdentifier = function() {
return crypto.hash160(this.pub.toBuffer()) return crypto.hash160(this.pub.toBuffer())
} }
HDWallet.prototype.getFingerprint = function() { HDNode.prototype.getFingerprint = function() {
return this.getIdentifier().slice(0, 4) return this.getIdentifier().slice(0, 4)
} }
HDWallet.prototype.getAddress = function() { HDNode.prototype.getAddress = function() {
return this.pub.getAddress(this.network.pubKeyHash) return this.pub.getAddress(this.network.pubKeyHash)
} }
HDWallet.prototype.toBase58 = function(isPrivate) { HDNode.prototype.toBase58 = function(isPrivate) {
var buffer = this.toBuffer(isPrivate) var buffer = this.toBuffer(isPrivate)
var checksum = crypto.hash256(buffer).slice(0, 4) var checksum = crypto.hash256(buffer).slice(0, 4)
@ -158,12 +158,12 @@ HDWallet.prototype.toBase58 = function(isPrivate) {
])) ]))
} }
HDWallet.prototype.toBuffer = function(isPrivate) { HDNode.prototype.toBuffer = function(isPrivate) {
if (isPrivate == undefined) isPrivate = !!this.priv if (isPrivate == undefined) isPrivate = !!this.priv
// Version // Version
var version = isPrivate ? this.network.bip32.private : this.network.bip32.public var version = isPrivate ? this.network.bip32.private : this.network.bip32.public
var buffer = new Buffer(HDWallet.LENGTH) var buffer = new Buffer(HDNode.LENGTH)
// 4 bytes: version bytes // 4 bytes: version bytes
buffer.writeUInt32BE(version, 0) buffer.writeUInt32BE(version, 0)
@ -199,13 +199,13 @@ HDWallet.prototype.toBuffer = function(isPrivate) {
return buffer return buffer
} }
HDWallet.prototype.toHex = function(isPrivate) { HDNode.prototype.toHex = function(isPrivate) {
return this.toBuffer(isPrivate).toString('hex') return this.toBuffer(isPrivate).toString('hex')
} }
// https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#child-key-derivation-ckd-functions // https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#child-key-derivation-ckd-functions
HDWallet.prototype.derive = function(index) { HDNode.prototype.derive = function(index) {
var isHardened = index >= HDWallet.HIGHEST_BIT var isHardened = index >= HDNode.HIGHEST_BIT
var indexBuffer = new Buffer(4) var indexBuffer = new Buffer(4)
indexBuffer.writeUInt32BE(index, 0) indexBuffer.writeUInt32BE(index, 0)
@ -252,7 +252,7 @@ HDWallet.prototype.derive = function(index) {
return this.derive(index + 1) return this.derive(index + 1)
} }
hd = new HDWallet(ki, IR, this.network) hd = new HDNode(ki, IR, this.network)
// Public parent key -> public child key // Public parent key -> public child key
} else { } else {
@ -265,7 +265,7 @@ HDWallet.prototype.derive = function(index) {
return this.derive(index + 1) return this.derive(index + 1)
} }
hd = new HDWallet(Ki, IR, this.network) hd = new HDNode(Ki, IR, this.network)
} }
hd.depth = this.depth + 1 hd.depth = this.depth + 1
@ -275,11 +275,11 @@ HDWallet.prototype.derive = function(index) {
return hd return hd
} }
HDWallet.prototype.deriveHardened = function(index) { HDNode.prototype.deriveHardened = function(index) {
// Only derives hardened private keys by default // Only derives hardened private keys by default
return this.derive(index + HDWallet.HIGHEST_BIT) return this.derive(index + HDNode.HIGHEST_BIT)
} }
HDWallet.prototype.toString = HDWallet.prototype.toBase58 HDNode.prototype.toString = HDNode.prototype.toBase58
module.exports = HDWallet module.exports = HDNode

View file

@ -16,7 +16,7 @@ module.exports = {
ECPubKey: require('./ecpubkey'), ECPubKey: require('./ecpubkey'),
Message: require('./message'), Message: require('./message'),
opcodes: require('./opcodes'), opcodes: require('./opcodes'),
HDWallet: require('./hdwallet'), HDNode: require('./hdnode'),
Script: require('./script'), Script: require('./script'),
sec: require('./sec'), sec: require('./sec'),
Transaction: T.Transaction, Transaction: T.Transaction,

View file

@ -3,7 +3,7 @@ var networks = require('./networks')
var rng = require('secure-random') var rng = require('secure-random')
var Address = require('./address') var Address = require('./address')
var HDNode = require('./hdwallet') var HDNode = require('./hdnode')
var Transaction = require('./transaction').Transaction var Transaction = require('./transaction').Transaction
function Wallet(seed, network) { function Wallet(seed, network) {

View file

@ -4,11 +4,11 @@ var sec = require('../src/sec')
var ecparams = sec("secp256k1") var ecparams = sec("secp256k1")
var BigInteger = require('bigi') var BigInteger = require('bigi')
var HDWallet = require('../src/hdwallet') var HDNode = require('../src/hdnode')
var fixtures = require('./fixtures/hdwallet.json') var fixtures = require('./fixtures/hdnode.json')
describe('HDWallet', function() { describe('HDNode', function() {
describe('Constructor', function() { describe('Constructor', function() {
var D = BigInteger.ONE var D = BigInteger.ONE
var Q = ecparams.getG().multiply(D) var Q = ecparams.getG().multiply(D)
@ -16,41 +16,41 @@ describe('HDWallet', function() {
chainCode.fill(1) chainCode.fill(1)
it('calculates the publicKey from a BigInteger', function() { it('calculates the publicKey from a BigInteger', function() {
var hd = new HDWallet(D, chainCode) var hd = new HDNode(D, chainCode)
assert(hd.pub.Q.equals(Q)) assert(hd.pub.Q.equals(Q))
}) })
it('only uses compressed points', function() { it('only uses compressed points', function() {
var hd = new HDWallet(Q, chainCode) var hd = new HDNode(Q, chainCode)
var hdP = new HDWallet(D, chainCode) var hdP = new HDNode(D, chainCode)
assert.strictEqual(hd.pub.compressed, true) assert.strictEqual(hd.pub.compressed, true)
assert.strictEqual(hdP.pub.compressed, true) assert.strictEqual(hdP.pub.compressed, true)
}) })
it('has a default depth/index of 0', function() { it('has a default depth/index of 0', function() {
var hd = new HDWallet(Q, chainCode) var hd = new HDNode(Q, chainCode)
assert.strictEqual(hd.depth, 0) assert.strictEqual(hd.depth, 0)
assert.strictEqual(hd.index, 0) assert.strictEqual(hd.index, 0)
}) })
it('defaults to the bitcoin network', function() { it('defaults to the bitcoin network', function() {
var hd = new HDWallet(Q, chainCode) var hd = new HDNode(Q, chainCode)
assert.equal(hd.network, networks.bitcoin) assert.equal(hd.network, networks.bitcoin)
}) })
it('supports alternative networks', function() { it('supports alternative networks', function() {
var hd = new HDWallet(Q, chainCode, networks.testnet) var hd = new HDNode(Q, chainCode, networks.testnet)
assert.equal(hd.network, networks.testnet) assert.equal(hd.network, networks.testnet)
}) })
it('throws an exception when an unknown network is given', function() { it('throws an exception when an unknown network is given', function() {
assert.throws(function() { assert.throws(function() {
new HDWallet(D, chainCode, {}) new HDNode(D, chainCode, {})
}, /Unknown BIP32 constants for network/) }, /Unknown BIP32 constants for network/)
}) })
}) })
@ -58,7 +58,7 @@ describe('HDWallet', function() {
describe('fromSeed*', function() { describe('fromSeed*', function() {
fixtures.valid.forEach(function(f) { fixtures.valid.forEach(function(f) {
it('calculates privKey and chainCode for ' + f.master.fingerprint, function() { it('calculates privKey and chainCode for ' + f.master.fingerprint, function() {
var hd = HDWallet.fromSeedHex(f.master.seed) var hd = HDNode.fromSeedHex(f.master.seed)
assert.equal(hd.priv.toWIF(), f.master.wif) assert.equal(hd.priv.toWIF(), f.master.wif)
assert.equal(hd.chainCode.toString('hex'), f.master.chainCode) assert.equal(hd.chainCode.toString('hex'), f.master.chainCode)
@ -69,7 +69,7 @@ describe('HDWallet', function() {
describe('toBase58', function() { describe('toBase58', function() {
fixtures.valid.forEach(function(f) { fixtures.valid.forEach(function(f) {
it('exports ' + f.master.base58 + ' (public) correctly', function() { it('exports ' + f.master.base58 + ' (public) correctly', function() {
var hd = HDWallet.fromSeedHex(f.master.seed) var hd = HDNode.fromSeedHex(f.master.seed)
assert.equal(hd.toBase58(false), f.master.base58) assert.equal(hd.toBase58(false), f.master.base58)
}) })
@ -77,14 +77,14 @@ describe('HDWallet', function() {
fixtures.valid.forEach(function(f) { fixtures.valid.forEach(function(f) {
it('exports ' + f.master.base58Priv + ' (private) correctly', function() { it('exports ' + f.master.base58Priv + ' (private) correctly', function() {
var hd = HDWallet.fromSeedHex(f.master.seed) var hd = HDNode.fromSeedHex(f.master.seed)
assert.equal(hd.toBase58(true), f.master.base58Priv) assert.equal(hd.toBase58(true), f.master.base58Priv)
}) })
}) })
it('fails when there is no private key', function() { it('fails when there is no private key', function() {
var hd = HDWallet.fromBase58(fixtures.valid[0].master.base58) var hd = HDNode.fromBase58(fixtures.valid[0].master.base58)
assert.throws(function() { assert.throws(function() {
hd.toBase58(true) hd.toBase58(true)
@ -95,7 +95,7 @@ describe('HDWallet', function() {
describe('fromBase58', function() { describe('fromBase58', function() {
fixtures.valid.forEach(function(f) { fixtures.valid.forEach(function(f) {
it('imports ' + f.master.base58 + ' (public) correctly', function() { it('imports ' + f.master.base58 + ' (public) correctly', function() {
var hd = HDWallet.fromBase58(f.master.base58) var hd = HDNode.fromBase58(f.master.base58)
assert.equal(hd.toBase58(), f.master.base58) assert.equal(hd.toBase58(), f.master.base58)
}) })
@ -103,7 +103,7 @@ describe('HDWallet', function() {
fixtures.valid.forEach(function(f) { fixtures.valid.forEach(function(f) {
it('imports ' + f.master.base58Priv + ' (private) correctly', function() { it('imports ' + f.master.base58Priv + ' (private) correctly', function() {
var hd = HDWallet.fromBase58(f.master.base58Priv) var hd = HDNode.fromBase58(f.master.base58Priv)
assert.equal(hd.toBase58(), f.master.base58Priv) assert.equal(hd.toBase58(), f.master.base58Priv)
}) })
@ -112,7 +112,7 @@ describe('HDWallet', function() {
fixtures.invalid.fromBase58.forEach(function(f) { fixtures.invalid.fromBase58.forEach(function(f) {
it('throws on ' + f.string, function() { it('throws on ' + f.string, function() {
assert.throws(function() { assert.throws(function() {
HDWallet.fromBase58(f.string) HDNode.fromBase58(f.string)
}, new RegExp(f.exception)) }, new RegExp(f.exception))
}) })
}) })
@ -121,7 +121,7 @@ describe('HDWallet', function() {
describe('fromBuffer/fromHex', function() { describe('fromBuffer/fromHex', function() {
fixtures.valid.forEach(function(f) { fixtures.valid.forEach(function(f) {
it('imports ' + f.master.hex + ' (public) correctly', function() { it('imports ' + f.master.hex + ' (public) correctly', function() {
var hd = HDWallet.fromHex(f.master.hex) var hd = HDNode.fromHex(f.master.hex)
assert.equal(hd.toBuffer().toString('hex'), f.master.hex) assert.equal(hd.toBuffer().toString('hex'), f.master.hex)
}) })
@ -129,7 +129,7 @@ describe('HDWallet', function() {
fixtures.valid.forEach(function(f) { fixtures.valid.forEach(function(f) {
it('imports ' + f.master.hexPriv + ' (private) correctly', function() { it('imports ' + f.master.hexPriv + ' (private) correctly', function() {
var hd = HDWallet.fromHex(f.master.hexPriv) var hd = HDNode.fromHex(f.master.hexPriv)
assert.equal(hd.toBuffer().toString('hex'), f.master.hexPriv) assert.equal(hd.toBuffer().toString('hex'), f.master.hexPriv)
}) })
@ -138,7 +138,7 @@ describe('HDWallet', function() {
fixtures.invalid.fromBuffer.forEach(function(f) { fixtures.invalid.fromBuffer.forEach(function(f) {
it('throws on ' + f.string, function() { it('throws on ' + f.string, function() {
assert.throws(function() { assert.throws(function() {
HDWallet.fromHex(f.hex) HDNode.fromHex(f.hex)
}, new RegExp(f.exception)) }, new RegExp(f.exception))
}) })
}) })
@ -147,7 +147,7 @@ describe('HDWallet', function() {
describe('toBuffer/toHex', function() { describe('toBuffer/toHex', function() {
fixtures.valid.forEach(function(f) { fixtures.valid.forEach(function(f) {
it('exports ' + f.master.hex + ' (public) correctly', function() { it('exports ' + f.master.hex + ' (public) correctly', function() {
var hd = HDWallet.fromSeedHex(f.master.seed) var hd = HDNode.fromSeedHex(f.master.seed)
assert.equal(hd.toHex(false), f.master.hex) assert.equal(hd.toHex(false), f.master.hex)
}) })
@ -155,14 +155,14 @@ describe('HDWallet', function() {
fixtures.valid.forEach(function(f) { fixtures.valid.forEach(function(f) {
it('exports ' + f.master.hexPriv + ' (private) correctly', function() { it('exports ' + f.master.hexPriv + ' (private) correctly', function() {
var hd = HDWallet.fromSeedHex(f.master.seed) var hd = HDNode.fromSeedHex(f.master.seed)
assert.equal(hd.toHex(true), f.master.hexPriv) assert.equal(hd.toHex(true), f.master.hexPriv)
}) })
}) })
it('fails when there is no private key', function() { it('fails when there is no private key', function() {
var hd = HDWallet.fromHex(fixtures.valid[0].master.hex) var hd = HDNode.fromHex(fixtures.valid[0].master.hex)
assert.throws(function() { assert.throws(function() {
hd.toHex(true) hd.toHex(true)
@ -174,7 +174,7 @@ describe('HDWallet', function() {
var f = fixtures.valid[0] var f = fixtures.valid[0]
it('returns the identifier for ' + f.master.fingerprint, function() { it('returns the identifier for ' + f.master.fingerprint, function() {
var hd = HDWallet.fromBase58(f.master.base58) var hd = HDNode.fromBase58(f.master.base58)
assert.equal(hd.getIdentifier().toString('hex'), f.master.identifier) assert.equal(hd.getIdentifier().toString('hex'), f.master.identifier)
}) })
@ -184,7 +184,7 @@ describe('HDWallet', function() {
var f = fixtures.valid[0] var f = fixtures.valid[0]
it('returns the fingerprint for ' + f.master.fingerprint, function() { it('returns the fingerprint for ' + f.master.fingerprint, function() {
var hd = HDWallet.fromBase58(f.master.base58) var hd = HDNode.fromBase58(f.master.base58)
assert.equal(hd.getFingerprint().toString('hex'), f.master.fingerprint) assert.equal(hd.getFingerprint().toString('hex'), f.master.fingerprint)
}) })
@ -194,13 +194,13 @@ describe('HDWallet', function() {
var f = fixtures.valid[0] var f = fixtures.valid[0]
it('returns the Address (pubKeyHash) for ' + f.master.fingerprint, function() { it('returns the Address (pubKeyHash) for ' + f.master.fingerprint, function() {
var hd = HDWallet.fromBase58(f.master.base58) var hd = HDNode.fromBase58(f.master.base58)
assert.equal(hd.getAddress().toString(), f.master.address) assert.equal(hd.getAddress().toString(), f.master.address)
}) })
it('supports alternative networks', function() { it('supports alternative networks', function() {
var hd = HDWallet.fromBase58(f.master.base58) var hd = HDNode.fromBase58(f.master.base58)
hd.network = networks.testnet hd.network = networks.testnet
assert.equal(hd.getAddress().version, networks.testnet.pubKeyHash) assert.equal(hd.getAddress().version, networks.testnet.pubKeyHash)
@ -215,14 +215,14 @@ describe('HDWallet', function() {
assert.equal(hd.depth, depth || 0) assert.equal(hd.depth, depth || 0)
if (v.hardened) { if (v.hardened) {
assert.equal(hd.index, v.m + HDWallet.HIGHEST_BIT) assert.equal(hd.index, v.m + HDNode.HIGHEST_BIT)
} else { } else {
assert.equal(hd.index, v.m) assert.equal(hd.index, v.m)
} }
} }
fixtures.valid.forEach(function(f, j) { fixtures.valid.forEach(function(f, j) {
var hd = HDWallet.fromSeedHex(f.master.seed) var hd = HDNode.fromSeedHex(f.master.seed)
f.children.forEach(function(c, i) { f.children.forEach(function(c, i) {
it(c.description + ' from ' + f.master.fingerprint, function() { it(c.description + ' from ' + f.master.fingerprint, function() {
@ -242,8 +242,8 @@ describe('HDWallet', function() {
var f = fixtures.valid[1] var f = fixtures.valid[1]
var c = f.children[0] var c = f.children[0]
var parent = HDWallet.fromBase58(f.master.base58) var parentNode = HDNode.fromBase58(f.master.base58)
var child = parent.derive(c.m) var child = parentNode.derive(c.m)
assert.equal(child.toBase58(), c.base58) assert.equal(child.toBase58(), c.base58)
}) })
@ -252,10 +252,10 @@ describe('HDWallet', function() {
var f = fixtures.valid[1] var f = fixtures.valid[1]
var c = f.children[0] var c = f.children[0]
var parent = HDWallet.fromBase58(f.master.base58) var parentNode = HDNode.fromBase58(f.master.base58)
assert.throws(function() { assert.throws(function() {
parent.deriveHardened(c.m) parentNode.deriveHardened(c.m)
}, /Could not derive hardened child key/) }, /Could not derive hardened child key/)
}) })
}) })

View file

@ -4,7 +4,7 @@ var networks = require('../src/networks')
var sinon = require('sinon') var sinon = require('sinon')
var Address = require('../src/address') var Address = require('../src/address')
var HDWallet = require('../src/hdwallet') var HDNode = require('../src/hdnode')
var Script = require('../src/script') var Script = require('../src/script')
var Transaction = require('../src/transaction').Transaction var Transaction = require('../src/transaction').Transaction
var Wallet = require('../src/wallet') var Wallet = require('../src/wallet')
@ -31,7 +31,7 @@ describe('Wallet', function() {
it("generates m/0' as the main account", function() { it("generates m/0' as the main account", function() {
var mainAccount = wallet.getAccountZero() var mainAccount = wallet.getAccountZero()
assert.equal(mainAccount.index, 0 + HDWallet.HIGHEST_BIT) assert.equal(mainAccount.index, 0 + HDNode.HIGHEST_BIT)
assert.equal(mainAccount.depth, 1) assert.equal(mainAccount.depth, 1)
}) })