From bec7f690ce2eca6a91b6afc0b75c6518a9b72480 Mon Sep 17 00:00:00 2001 From: Thomas Kerin Date: Wed, 7 Dec 2016 13:04:31 +0100 Subject: [PATCH] Rename __hasWitnesses -> hasWitnesses, and add tests --- src/transaction.js | 8 ++++---- test/transaction.js | 8 ++++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/transaction.js b/src/transaction.js index 008e7dc..f2377a6 100644 --- a/src/transaction.js +++ b/src/transaction.js @@ -125,7 +125,7 @@ Transaction.fromBuffer = function (buffer, __noStrict) { } // was this pointless? - if (!tx.__hasWitnesses()) throw new Error('Transaction has superfluous witness data') + if (!tx.hasWitnesses()) throw new Error('Transaction has superfluous witness data') } tx.locktime = readUInt32() @@ -184,7 +184,7 @@ Transaction.prototype.addOutput = function (scriptPubKey, value) { }) - 1) } -Transaction.prototype.__hasWitnesses = function () { +Transaction.prototype.hasWitnesses = function () { return this.ins.some(function (x) { return x.witness.length !== 0 }) @@ -195,7 +195,7 @@ Transaction.prototype.byteLength = function () { } Transaction.prototype.__byteLength = function (__allowWitness) { - var hasWitnesses = __allowWitness && this.__hasWitnesses() + var hasWitnesses = __allowWitness && this.hasWitnesses() return ( (hasWitnesses ? 10 : 8) + @@ -417,7 +417,7 @@ Transaction.prototype.__toBuffer = function (buffer, initialOffset, __allowWitne writeInt32(this.version) - var hasWitnesses = __allowWitness && this.__hasWitnesses() + var hasWitnesses = __allowWitness && this.hasWitnesses() if (hasWitnesses) { writeUInt8(Transaction.ADVANCED_TRANSACTION_MARKER) diff --git a/test/transaction.js b/test/transaction.js index 1963d90..1881bd5 100644 --- a/test/transaction.js +++ b/test/transaction.js @@ -119,6 +119,14 @@ describe('Transaction', function () { }) }) + describe('hasWitnesses', function () { + fixtures.valid.forEach(function (f) { + it('detects if the transaction has witnesses: ' + (f.whex ? 'true' : 'false'), function () { + assert.strictEqual(Transaction.fromHex(f.whex ? f.whex : f.hex).hasWitnesses(), !!f.whex) + }) + }) + }) + describe('addInput', function () { var prevTxHash beforeEach(function () {