From a9f124423ffec1ebd3f5f1ab1bdbda4a70e2b2bd Mon Sep 17 00:00:00 2001
From: Thomas Kerin <me@thomaskerin.io>
Date: Tue, 8 Nov 2016 13:43:45 +0100
Subject: [PATCH] Test that parsing a transaction with superfluous witness data
 leads to an error

---
 src/transaction.js | 18 +++++-------------
 1 file changed, 5 insertions(+), 13 deletions(-)

diff --git a/src/transaction.js b/src/transaction.js
index b19fe8a..582204c 100644
--- a/src/transaction.js
+++ b/src/transaction.js
@@ -120,14 +120,12 @@ Transaction.fromBuffer = function (buffer, __noStrict) {
   }
 
   if (hasWitnesses) {
-    var isNull = true
     for (i = 0; i < vinLen; ++i) {
       tx.ins[i].witness = readVector()
-      isNull = isNull && tx.ins[i].witness.length === 0
-    }
-    if (isNull) {
-      throw new Error('Transaction has superfluous witness data')
     }
+
+    // was this pointless?
+    if (!tx._hasWitnesses()) throw new Error('Transaction has superfluous witness data')
   }
 
   tx.locktime = readUInt32()
@@ -188,7 +186,7 @@ Transaction.prototype.addOutput = function (scriptPubKey, value) {
 
 Transaction.prototype._hasWitnesses = function () {
   return this.ins.some(function (x) {
-    return x.witness !== EMPTY_WITNESS
+    return x.witness.length !== 0
   })
 }
 
@@ -412,7 +410,6 @@ Transaction.prototype.toBuffer = function (buffer, initialOffset) {
   writeInt32(this.version)
 
   var hasWitnesses = this._hasWitnesses()
-  var serializeWitnesses = hasWitnesses // TODO: remove this, temporary
 
   if (hasWitnesses) {
     writeUInt8(Transaction.ADVANCED_TRANSACTION_MARKER)
@@ -439,15 +436,10 @@ Transaction.prototype.toBuffer = function (buffer, initialOffset) {
     writeVarSlice(txOut.script)
   })
 
-  if (serializeWitnesses) {
-    var isNull = true
+  if (hasWitnesses) {
     this.ins.forEach(function (input) {
       writeVector(input.witness)
-      isNull = isNull && input.witness.length === 0
     })
-    if (isNull) {
-      throw new Error('Transaction has superfluous witness data')
-    }
   }
 
   writeUInt32(this.locktime)