From 8eaf44881a45a09441082d8d9a43247ead60c9db Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Tue, 17 Jun 2014 20:18:39 +1000 Subject: [PATCH] Transaction: improve hash length checking + tests --- src/transaction.js | 4 ++-- test/fixtures/transaction.json | 12 ++++++++++++ test/transaction.js | 11 +++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/transaction.js b/src/transaction.js index 0ed10c2..0926b84 100644 --- a/src/transaction.js +++ b/src/transaction.js @@ -39,7 +39,6 @@ Transaction.prototype.addInput = function(tx, index, sequence) { if (typeof tx === 'string') { hash = new Buffer(tx, 'hex') - assert.equal(hash.length, 32, 'Expected Transaction, txId or txHash, got ' + tx) // TxId hex is big-endian, we need little-endian Array.prototype.reverse.call(hash) @@ -48,10 +47,11 @@ Transaction.prototype.addInput = function(tx, index, sequence) { hash = tx.getHash() } else { - assert(Buffer.isBuffer(tx), 'Expected Transaction, txId or txHash, got ' + tx) hash = tx } + assert(Buffer.isBuffer(hash), 'Expected Transaction, txId or txHash, got ' + tx) + assert.equal(hash.length, 32, 'Expected hash length of 32, got ' + hash.length) assert.equal(typeof index, 'number', 'Expected number index, got ' + index) return (this.ins.push({ diff --git a/test/fixtures/transaction.json b/test/fixtures/transaction.json index eab60ad..c473323 100644 --- a/test/fixtures/transaction.json +++ b/test/fixtures/transaction.json @@ -166,6 +166,18 @@ } ], "invalid": { + "addInput": [ + { + "exception": "Expected hash length of 32, got 30", + "hash": "0aed1366a73b6057ee7800d737bff1bdf8c448e98d86bc0998f2b009816d", + "index": 0 + }, + { + "exception": "Expected hash length of 32, got 34", + "hash": "0aed1366a73b6057ee7800d737bff1bdf8c448e98d86bc0998f2b009816da9b0ffff", + "index": 0 + } + ], "fromBuffer": [ { "exception": "Transaction has unexpected data", diff --git a/test/transaction.js b/test/transaction.js index 0288ed7..a8aa1d7 100644 --- a/test/transaction.js +++ b/test/transaction.js @@ -113,6 +113,17 @@ describe('Transaction', function() { }) }) }) + + fixtures.invalid.addInput.forEach(function(f) { + it('throws on ' + f.exception, function() { + var tx = new Transaction() + var hash = new Buffer(f.hash, 'hex') + + assert.throws(function() { + tx.addInput(hash, f.index) + }, new RegExp(f.exception)) + }) + }) }) describe('addOutput', function() {