HDWallet: add to/fromHex tests

This commit is contained in:
Daniel Cousens 2014-06-01 16:39:07 +10:00
parent 260705a24a
commit 4cec42a8d8
2 changed files with 35 additions and 4 deletions

View file

@ -132,6 +132,10 @@ HDWallet.fromBuffer = function(buffer) {
return hd
}
HDWallet.fromHex = function(hex, priv) {
return HDWallet.fromBuffer(new Buffer(hex, 'hex'))
}
HDWallet.prototype.getIdentifier = function() {
return crypto.hash160(this.pub.toBuffer())
}
@ -182,6 +186,7 @@ HDWallet.prototype.toBuffer = function(priv) {
return buffer
}
HDWallet.prototype.toHex = function(priv) {
return this.toBuffer(priv).toString('hex')
}

View file

@ -118,10 +118,10 @@ describe('HDWallet', function() {
})
})
describe('fromBuffer', function() {
describe('fromBuffer/fromHex', function() {
fixtures.valid.forEach(function(f) {
it('imports ' + f.master.hex + ' (public) correctly', function() {
var hd = HDWallet.fromBuffer(new Buffer(f.master.hex, 'hex'))
var hd = HDWallet.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.fromBuffer(new Buffer(f.master.hexPriv, 'hex'))
var hd = HDWallet.fromHex(f.master.hexPriv)
assert.equal(hd.toBuffer(true).toString('hex'), f.master.hexPriv)
})
@ -138,12 +138,38 @@ describe('HDWallet', function() {
fixtures.invalid.fromBuffer.forEach(function(f) {
it('throws on ' + f.string, function() {
assert.throws(function() {
HDWallet.fromBuffer(new Buffer(f.hex, 'hex'))
HDWallet.fromHex(f.hex)
}, new RegExp(f.exception))
})
})
})
describe('toBuffer/toHex', function() {
fixtures.valid.forEach(function(f) {
it('exports ' + f.master.hex + ' (public) correctly', function() {
var hd = HDWallet.fromSeedHex(f.master.seed)
assert.equal(hd.toHex(), f.master.hex)
})
})
fixtures.valid.forEach(function(f) {
it('exports ' + f.master.hexPriv + ' (private) correctly', function() {
var hd = HDWallet.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)
assert.throws(function() {
hd.toHex(true)
}, /Missing private key/)
})
})
describe('getIdentifier', function() {
var f = fixtures.valid[0]