From df6ea8aea2b2b1bbea036f44aa42de9dd7773680 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Fri, 25 Apr 2014 00:18:13 +1000 Subject: [PATCH] Removes toPaddedBuffer and extends toBuffer --- src/bigi.js | 20 +++++++++----------- src/eckey.js | 2 +- src/message.js | 4 ++-- test/bigi.js | 24 ++++++++++++------------ 4 files changed, 24 insertions(+), 26 deletions(-) diff --git a/src/bigi.js b/src/bigi.js index ee17b3a..118db69 100644 --- a/src/bigi.js +++ b/src/bigi.js @@ -21,20 +21,18 @@ BigInteger.fromBuffer = function(buffer) { } // Export operations -BigInteger.prototype.toBuffer = function() { - return new Buffer(this.toByteArrayUnsigned()) -} +BigInteger.prototype.toBuffer = function(s) { + if (s != undefined) assert(Number.isFinite(s)) -BigInteger.prototype.toHex = function() { - return this.toBuffer().toString('hex') -} + var buffer = new Buffer(this.toByteArrayUnsigned()) + var padded = new Buffer(s - buffer.length) + padded.fill(0) -BigInteger.prototype.toPaddedBuffer = function(s) { - var buffer = this.toBuffer() - var padded = new Buffer(s - buffer.length) - padded.fill(0) + return Buffer.concat([padded, buffer], s) +} - return Buffer.concat([padded, buffer], s) +BigInteger.prototype.toHex = function(s) { + return this.toBuffer(s).toString('hex') } module.exports = BigInteger diff --git a/src/eckey.js b/src/eckey.js index af3b7f1..bb2b3a0 100644 --- a/src/eckey.js +++ b/src/eckey.js @@ -65,7 +65,7 @@ ECKey.prototype.sign = function(hash) { // Export functions ECKey.prototype.toBuffer = function() { - return this.D.toPaddedBuffer(32) + return this.D.toBuffer(32) } ECKey.prototype.toHex = function() { return this.toBuffer().toString('hex') diff --git a/src/message.js b/src/message.js index 8bb9203..f389398 100644 --- a/src/message.js +++ b/src/message.js @@ -33,8 +33,8 @@ function sign(key, message) { i += 4 } - var rB = sig.r.toPaddedBuffer(32) - var sB = sig.s.toPaddedBuffer(32) + var rB = sig.r.toBuffer(32) + var sB = sig.s.toBuffer(32) return Buffer.concat([new Buffer([i]), rB, sB], 65) } diff --git a/test/bigi.js b/test/bigi.js index 8963920..4df4aad 100644 --- a/test/bigi.js +++ b/test/bigi.js @@ -7,8 +7,8 @@ describe('BigInteger', function() { describe('fromBuffer/fromHex', function() { it('should match the test vectors', function() { fixtures.valid.forEach(function(f) { - assert.deepEqual(BigInteger.fromHex(f.hex).toString(), f.dec) - assert.deepEqual(BigInteger.fromHex(f.hexPadded).toString(), f.dec) + assert.equal(BigInteger.fromHex(f.hex).toString(), f.dec) + assert.equal(BigInteger.fromHex(f.hexPadded).toString(), f.dec) }) }) @@ -24,21 +24,21 @@ describe('BigInteger', function() { describe('toBuffer/toHex', function() { it('should match the test vectors', function() { fixtures.valid.forEach(function(f) { - var actualHex = new BigInteger(f.dec).toHex() + var bi = new BigInteger(f.dec) - assert.equal(actualHex, f.hex) + assert.equal(bi.toHex(), f.hex) + assert.equal(bi.toHex(32), f.hexPadded) }) }) - }) - describe('toPaddedBuffer', function() { - it('should match the test vectors', function() { - fixtures.valid.forEach(function(f) { - var actualBuf = new BigInteger(f.dec).toPaddedBuffer(32) + it('throws on non-finite padding value', function() { + var bi = new BigInteger('1') - assert.equal(actualBuf.length, 32) - assert.equal(actualBuf.toString('hex'), f.hexPadded) - }) + assert.throws(function() { bi.toHex({}) }) + assert.throws(function() { bi.toHex([]) }) + assert.throws(function() { bi.toHex('') }) + assert.throws(function() { bi.toHex(0 / 0) }) + assert.throws(function() { bi.toHex(1 / 0) }) }) }) })