From 4cec42a8d852b2f53e3312f42591606dea95441c Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Sun, 1 Jun 2014 16:39:07 +1000 Subject: [PATCH] HDWallet: add to/fromHex tests --- src/hdwallet.js | 5 +++++ test/hdwallet.js | 34 ++++++++++++++++++++++++++++++---- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/hdwallet.js b/src/hdwallet.js index 1283750..014d0a0 100644 --- a/src/hdwallet.js +++ b/src/hdwallet.js @@ -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') } diff --git a/test/hdwallet.js b/test/hdwallet.js index 5e22865..4ceebb1 100644 --- a/test/hdwallet.js +++ b/test/hdwallet.js @@ -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]