HDWallet: rename to HDNode
This commit is contained in:
parent
c340f5cf83
commit
64e307b13f
6 changed files with 70 additions and 70 deletions
|
@ -28,7 +28,7 @@ function findBIP32ParamsByVersion(version) {
|
|||
assert(false, 'Could not find version ' + version.toString(16))
|
||||
}
|
||||
|
||||
function HDWallet(K, chainCode, network) {
|
||||
function HDNode(K, chainCode, network) {
|
||||
network = network || networks.bitcoin
|
||||
|
||||
assert(Buffer.isBuffer(chainCode), 'Expected Buffer, got ' + chainCode)
|
||||
|
@ -47,27 +47,27 @@ function HDWallet(K, chainCode, network) {
|
|||
}
|
||||
}
|
||||
|
||||
HDWallet.MASTER_SECRET = new Buffer('Bitcoin seed')
|
||||
HDWallet.HIGHEST_BIT = 0x80000000
|
||||
HDWallet.LENGTH = 78
|
||||
HDNode.MASTER_SECRET = new Buffer('Bitcoin seed')
|
||||
HDNode.HIGHEST_BIT = 0x80000000
|
||||
HDNode.LENGTH = 78
|
||||
|
||||
HDWallet.fromSeedBuffer = function(seed, network) {
|
||||
var I = crypto.HmacSHA512(seed, HDWallet.MASTER_SECRET)
|
||||
HDNode.fromSeedBuffer = function(seed, network) {
|
||||
var I = crypto.HmacSHA512(seed, HDNode.MASTER_SECRET)
|
||||
var IL = I.slice(0, 32)
|
||||
var IR = I.slice(32)
|
||||
|
||||
// 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)
|
||||
|
||||
return new HDWallet(pIL, IR, network)
|
||||
return new HDNode(pIL, IR, network)
|
||||
}
|
||||
|
||||
HDWallet.fromSeedHex = function(hex, network) {
|
||||
return HDWallet.fromSeedBuffer(new Buffer(hex, 'hex'), network)
|
||||
HDNode.fromSeedHex = function(hex, network) {
|
||||
return HDNode.fromSeedBuffer(new Buffer(hex, 'hex'), network)
|
||||
}
|
||||
|
||||
HDWallet.fromBase58 = function(string) {
|
||||
HDNode.fromBase58 = function(string) {
|
||||
var buffer = base58.decode(string)
|
||||
|
||||
var payload = buffer.slice(0, -4)
|
||||
|
@ -76,11 +76,11 @@ HDWallet.fromBase58 = function(string) {
|
|||
var newChecksum = crypto.hash256(payload).slice(0, 4)
|
||||
assert.deepEqual(newChecksum, checksum, 'Invalid checksum')
|
||||
|
||||
return HDWallet.fromBuffer(payload)
|
||||
return HDNode.fromBuffer(payload)
|
||||
}
|
||||
|
||||
HDWallet.fromBuffer = function(buffer) {
|
||||
assert.strictEqual(buffer.length, HDWallet.LENGTH, 'Invalid buffer length')
|
||||
HDNode.fromBuffer = function(buffer) {
|
||||
assert.strictEqual(buffer.length, HDNode.LENGTH, 'Invalid buffer length')
|
||||
|
||||
// 4 byte: version bytes
|
||||
var version = buffer.readUInt32BE(0)
|
||||
|
@ -113,7 +113,7 @@ HDWallet.fromBuffer = function(buffer) {
|
|||
data = data.slice(1)
|
||||
|
||||
var D = BigInteger.fromBuffer(data)
|
||||
hd = new HDWallet(D, chainCode, params.network)
|
||||
hd = new HDNode(D, chainCode, params.network)
|
||||
} else {
|
||||
|
||||
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.
|
||||
decode.Q.validate()
|
||||
|
||||
hd = new HDWallet(decode.Q, chainCode, params.network)
|
||||
hd = new HDNode(decode.Q, chainCode, params.network)
|
||||
}
|
||||
|
||||
hd.depth = depth
|
||||
|
@ -132,23 +132,23 @@ HDWallet.fromBuffer = function(buffer) {
|
|||
return hd
|
||||
}
|
||||
|
||||
HDWallet.fromHex = function(hex, isPrivate) {
|
||||
return HDWallet.fromBuffer(new Buffer(hex, 'hex'))
|
||||
HDNode.fromHex = function(hex, isPrivate) {
|
||||
return HDNode.fromBuffer(new Buffer(hex, 'hex'))
|
||||
}
|
||||
|
||||
HDWallet.prototype.getIdentifier = function() {
|
||||
HDNode.prototype.getIdentifier = function() {
|
||||
return crypto.hash160(this.pub.toBuffer())
|
||||
}
|
||||
|
||||
HDWallet.prototype.getFingerprint = function() {
|
||||
HDNode.prototype.getFingerprint = function() {
|
||||
return this.getIdentifier().slice(0, 4)
|
||||
}
|
||||
|
||||
HDWallet.prototype.getAddress = function() {
|
||||
HDNode.prototype.getAddress = function() {
|
||||
return this.pub.getAddress(this.network.pubKeyHash)
|
||||
}
|
||||
|
||||
HDWallet.prototype.toBase58 = function(isPrivate) {
|
||||
HDNode.prototype.toBase58 = function(isPrivate) {
|
||||
var buffer = this.toBuffer(isPrivate)
|
||||
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
|
||||
|
||||
// Version
|
||||
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
|
||||
buffer.writeUInt32BE(version, 0)
|
||||
|
@ -199,13 +199,13 @@ HDWallet.prototype.toBuffer = function(isPrivate) {
|
|||
return buffer
|
||||
}
|
||||
|
||||
HDWallet.prototype.toHex = function(isPrivate) {
|
||||
HDNode.prototype.toHex = function(isPrivate) {
|
||||
return this.toBuffer(isPrivate).toString('hex')
|
||||
}
|
||||
|
||||
// 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
|
||||
HDNode.prototype.derive = function(index) {
|
||||
var isHardened = index >= HDNode.HIGHEST_BIT
|
||||
var indexBuffer = new Buffer(4)
|
||||
indexBuffer.writeUInt32BE(index, 0)
|
||||
|
||||
|
@ -252,7 +252,7 @@ HDWallet.prototype.derive = function(index) {
|
|||
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
|
||||
} else {
|
||||
|
@ -265,7 +265,7 @@ HDWallet.prototype.derive = function(index) {
|
|||
return this.derive(index + 1)
|
||||
}
|
||||
|
||||
hd = new HDWallet(Ki, IR, this.network)
|
||||
hd = new HDNode(Ki, IR, this.network)
|
||||
}
|
||||
|
||||
hd.depth = this.depth + 1
|
||||
|
@ -275,11 +275,11 @@ HDWallet.prototype.derive = function(index) {
|
|||
return hd
|
||||
}
|
||||
|
||||
HDWallet.prototype.deriveHardened = function(index) {
|
||||
HDNode.prototype.deriveHardened = function(index) {
|
||||
// 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
|
|
@ -16,7 +16,7 @@ module.exports = {
|
|||
ECPubKey: require('./ecpubkey'),
|
||||
Message: require('./message'),
|
||||
opcodes: require('./opcodes'),
|
||||
HDWallet: require('./hdwallet'),
|
||||
HDNode: require('./hdnode'),
|
||||
Script: require('./script'),
|
||||
sec: require('./sec'),
|
||||
Transaction: T.Transaction,
|
||||
|
|
|
@ -3,7 +3,7 @@ var networks = require('./networks')
|
|||
var rng = require('secure-random')
|
||||
|
||||
var Address = require('./address')
|
||||
var HDNode = require('./hdwallet')
|
||||
var HDNode = require('./hdnode')
|
||||
var Transaction = require('./transaction').Transaction
|
||||
|
||||
function Wallet(seed, network) {
|
||||
|
|
|
@ -4,11 +4,11 @@ var sec = require('../src/sec')
|
|||
var ecparams = sec("secp256k1")
|
||||
|
||||
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() {
|
||||
var D = BigInteger.ONE
|
||||
var Q = ecparams.getG().multiply(D)
|
||||
|
@ -16,41 +16,41 @@ describe('HDWallet', function() {
|
|||
chainCode.fill(1)
|
||||
|
||||
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))
|
||||
})
|
||||
|
||||
it('only uses compressed points', function() {
|
||||
var hd = new HDWallet(Q, chainCode)
|
||||
var hdP = new HDWallet(D, chainCode)
|
||||
var hd = new HDNode(Q, chainCode)
|
||||
var hdP = new HDNode(D, chainCode)
|
||||
|
||||
assert.strictEqual(hd.pub.compressed, true)
|
||||
assert.strictEqual(hdP.pub.compressed, true)
|
||||
})
|
||||
|
||||
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.index, 0)
|
||||
})
|
||||
|
||||
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)
|
||||
})
|
||||
|
||||
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)
|
||||
})
|
||||
|
||||
it('throws an exception when an unknown network is given', function() {
|
||||
assert.throws(function() {
|
||||
new HDWallet(D, chainCode, {})
|
||||
new HDNode(D, chainCode, {})
|
||||
}, /Unknown BIP32 constants for network/)
|
||||
})
|
||||
})
|
||||
|
@ -58,7 +58,7 @@ describe('HDWallet', function() {
|
|||
describe('fromSeed*', function() {
|
||||
fixtures.valid.forEach(function(f) {
|
||||
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.chainCode.toString('hex'), f.master.chainCode)
|
||||
|
@ -69,7 +69,7 @@ describe('HDWallet', function() {
|
|||
describe('toBase58', function() {
|
||||
fixtures.valid.forEach(function(f) {
|
||||
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)
|
||||
})
|
||||
|
@ -77,14 +77,14 @@ describe('HDWallet', function() {
|
|||
|
||||
fixtures.valid.forEach(function(f) {
|
||||
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)
|
||||
})
|
||||
})
|
||||
|
||||
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() {
|
||||
hd.toBase58(true)
|
||||
|
@ -95,7 +95,7 @@ describe('HDWallet', function() {
|
|||
describe('fromBase58', function() {
|
||||
fixtures.valid.forEach(function(f) {
|
||||
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)
|
||||
})
|
||||
|
@ -103,7 +103,7 @@ describe('HDWallet', function() {
|
|||
|
||||
fixtures.valid.forEach(function(f) {
|
||||
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)
|
||||
})
|
||||
|
@ -112,7 +112,7 @@ describe('HDWallet', function() {
|
|||
fixtures.invalid.fromBase58.forEach(function(f) {
|
||||
it('throws on ' + f.string, function() {
|
||||
assert.throws(function() {
|
||||
HDWallet.fromBase58(f.string)
|
||||
HDNode.fromBase58(f.string)
|
||||
}, new RegExp(f.exception))
|
||||
})
|
||||
})
|
||||
|
@ -121,7 +121,7 @@ describe('HDWallet', function() {
|
|||
describe('fromBuffer/fromHex', function() {
|
||||
fixtures.valid.forEach(function(f) {
|
||||
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)
|
||||
})
|
||||
|
@ -129,7 +129,7 @@ describe('HDWallet', function() {
|
|||
|
||||
fixtures.valid.forEach(function(f) {
|
||||
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)
|
||||
})
|
||||
|
@ -138,7 +138,7 @@ describe('HDWallet', function() {
|
|||
fixtures.invalid.fromBuffer.forEach(function(f) {
|
||||
it('throws on ' + f.string, function() {
|
||||
assert.throws(function() {
|
||||
HDWallet.fromHex(f.hex)
|
||||
HDNode.fromHex(f.hex)
|
||||
}, new RegExp(f.exception))
|
||||
})
|
||||
})
|
||||
|
@ -147,7 +147,7 @@ describe('HDWallet', function() {
|
|||
describe('toBuffer/toHex', function() {
|
||||
fixtures.valid.forEach(function(f) {
|
||||
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)
|
||||
})
|
||||
|
@ -155,14 +155,14 @@ describe('HDWallet', function() {
|
|||
|
||||
fixtures.valid.forEach(function(f) {
|
||||
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)
|
||||
})
|
||||
})
|
||||
|
||||
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() {
|
||||
hd.toHex(true)
|
||||
|
@ -174,7 +174,7 @@ describe('HDWallet', function() {
|
|||
var f = fixtures.valid[0]
|
||||
|
||||
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)
|
||||
})
|
||||
|
@ -184,7 +184,7 @@ describe('HDWallet', function() {
|
|||
var f = fixtures.valid[0]
|
||||
|
||||
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)
|
||||
})
|
||||
|
@ -194,13 +194,13 @@ describe('HDWallet', function() {
|
|||
var f = fixtures.valid[0]
|
||||
|
||||
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)
|
||||
})
|
||||
|
||||
it('supports alternative networks', function() {
|
||||
var hd = HDWallet.fromBase58(f.master.base58)
|
||||
var hd = HDNode.fromBase58(f.master.base58)
|
||||
hd.network = networks.testnet
|
||||
|
||||
assert.equal(hd.getAddress().version, networks.testnet.pubKeyHash)
|
||||
|
@ -215,14 +215,14 @@ describe('HDWallet', function() {
|
|||
assert.equal(hd.depth, depth || 0)
|
||||
|
||||
if (v.hardened) {
|
||||
assert.equal(hd.index, v.m + HDWallet.HIGHEST_BIT)
|
||||
assert.equal(hd.index, v.m + HDNode.HIGHEST_BIT)
|
||||
} else {
|
||||
assert.equal(hd.index, v.m)
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
it(c.description + ' from ' + f.master.fingerprint, function() {
|
||||
|
@ -242,8 +242,8 @@ describe('HDWallet', function() {
|
|||
var f = fixtures.valid[1]
|
||||
var c = f.children[0]
|
||||
|
||||
var parent = HDWallet.fromBase58(f.master.base58)
|
||||
var child = parent.derive(c.m)
|
||||
var parentNode = HDNode.fromBase58(f.master.base58)
|
||||
var child = parentNode.derive(c.m)
|
||||
|
||||
assert.equal(child.toBase58(), c.base58)
|
||||
})
|
||||
|
@ -252,10 +252,10 @@ describe('HDWallet', function() {
|
|||
var f = fixtures.valid[1]
|
||||
var c = f.children[0]
|
||||
|
||||
var parent = HDWallet.fromBase58(f.master.base58)
|
||||
var parentNode = HDNode.fromBase58(f.master.base58)
|
||||
|
||||
assert.throws(function() {
|
||||
parent.deriveHardened(c.m)
|
||||
parentNode.deriveHardened(c.m)
|
||||
}, /Could not derive hardened child key/)
|
||||
})
|
||||
})
|
|
@ -4,7 +4,7 @@ var networks = require('../src/networks')
|
|||
var sinon = require('sinon')
|
||||
|
||||
var Address = require('../src/address')
|
||||
var HDWallet = require('../src/hdwallet')
|
||||
var HDNode = require('../src/hdnode')
|
||||
var Script = require('../src/script')
|
||||
var Transaction = require('../src/transaction').Transaction
|
||||
var Wallet = require('../src/wallet')
|
||||
|
@ -31,7 +31,7 @@ describe('Wallet', function() {
|
|||
|
||||
it("generates m/0' as the main account", function() {
|
||||
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)
|
||||
})
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue