From af3491822e3cf225f27aadef86133641d304289b Mon Sep 17 00:00:00 2001
From: Daniel Cousens <github@dcousens.com>
Date: Tue, 3 Mar 2015 09:53:57 +1100
Subject: [PATCH 1/4] scripts: multisigInput only uses scriptPubKey for
 validation, OP_0 validation not necessary

---
 src/scripts.js             |  9 ++-------
 test/fixtures/scripts.json | 20 ++++----------------
 test/scripts.js            |  3 +--
 3 files changed, 7 insertions(+), 25 deletions(-)

diff --git a/src/scripts.js b/src/scripts.js
index 3876184..047bc16 100644
--- a/src/scripts.js
+++ b/src/scripts.js
@@ -253,13 +253,8 @@ function multisigInput (signatures, scriptPubKey) {
     var m = mOp - (ops.OP_1 - 1)
     var n = nOp - (ops.OP_1 - 1)
 
-    var count = 0
-    signatures.forEach(function (signature) {
-      count += (signature !== ops.OP_0)
-    })
-
-    assert(count >= m, 'Not enough signatures provided')
-    assert(count <= n, 'Too many signatures provided')
+    assert(signatures.length >= m, 'Not enough signatures provided')
+    assert(signatures.length <= n, 'Too many signatures provided')
   }
 
   return Script.fromChunks([].concat(ops.OP_0, signatures))
diff --git a/test/fixtures/scripts.json b/test/fixtures/scripts.json
index e79a6e7..8bd22a9 100644
--- a/test/fixtures/scripts.json
+++ b/test/fixtures/scripts.json
@@ -173,31 +173,19 @@
       {
         "description": "Not enough signatures provided",
         "type": "multisig",
-        "pubKeys": [
-          "0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798",
-          "02b80011a883a0fd621ad46dfc405df1e74bf075cbaf700fd4aebef6e96f848340"
-        ],
-        "signatures": [
-          null,
-          null
-        ]
+        "scriptPubKey": "OP_2 0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 02b80011a883a0fd621ad46dfc405df1e74bf075cbaf700fd4aebef6e96f848340 OP_2 OP_CHECKMULTISIG",
+        "signatures": []
       },
       {
         "exception": "Not enough signatures provided",
-        "pubKeys": [
-          "02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1",
-          "0395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a"
-        ],
+        "scriptPubKey": "OP_2 02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1 0395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a OP_2 OP_CHECKMULTISIG",
         "signatures": [
           "304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801"
         ]
       },
       {
         "exception": "Too many signatures provided",
-        "pubKeys": [
-          "02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1",
-          "0395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a"
-        ],
+        "scriptPubKey": "OP_2 02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1 0395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a OP_2 OP_CHECKMULTISIG",
         "signatures": [
           "304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801",
           "3045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d140501",
diff --git a/test/scripts.js b/test/scripts.js
index ccfee0b..41069cd 100644
--- a/test/scripts.js
+++ b/test/scripts.js
@@ -192,8 +192,7 @@ describe('Scripts', function () {
     })
 
     fixtures.invalid.multisigInput.forEach(function (f) {
-      var pubKeys = f.pubKeys.map(ECPubKey.fromHex)
-      var scriptPubKey = scripts.multisigOutput(pubKeys.length, pubKeys)
+      var scriptPubKey = Script.fromASM(f.scriptPubKey)
 
       it('throws on ' + f.exception, function () {
         var signatures = f.signatures.map(function (signature) {

From c79fecffa3ded9ee26db8c777b72a58d09ed8a48 Mon Sep 17 00:00:00 2001
From: Daniel Cousens <github@dcousens.com>
Date: Tue, 3 Mar 2015 09:54:55 +1100
Subject: [PATCH 2/4] tests: add failing test for transaction builder multisig

---
 test/fixtures/transaction_builder.json | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/test/fixtures/transaction_builder.json b/test/fixtures/transaction_builder.json
index a1c5529..68c83c3 100644
--- a/test/fixtures/transaction_builder.json
+++ b/test/fixtures/transaction_builder.json
@@ -141,6 +141,31 @@
           }
         ]
       },
+      {
+        "description": "Transaction w/ scriptHash(multisig 2-of-3)",
+        "txHex": "0100000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000fd5e0100483045022100eec19e061cad41610f9b42d2b06638b6b0fec3da0de9c6858e7f8c06053979900220622936dd47e202b2ad17639cda680e52334d407149252959936bb1f38e4acc52014830450221009aac215157a74a18234fd06be27448dccee809986bbf93be457a9262f0c69a9402203ff41d7c757f0e8951e4471f205087ecff499f986400ab18210eaad9a628e33c014cc952410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b84104c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a4104f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e67253aeffffffff01e8030000000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000",
+        "inputs": [
+          {
+            "txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
+            "vout": 0,
+            "signs": [
+              {
+                "privKey": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn",
+                "redeemScript": "OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a 04f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e672 OP_3 OP_CHECKMULTISIG"
+              },
+              {
+                "privKey": "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgx3cTMqe"
+              }
+            ]
+          }
+        ],
+        "outputs": [
+          {
+            "script": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG",
+            "value": 1000
+          }
+        ]
+      },
       {
         "description": "Transaction w/ scriptHash(pubKey) -> pubKeyHash",
         "txHex": "0100000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff000000006c47304402201115644b134932c8a7a8e925769d130a801288d477130e2bf6fadda20b33754d02202ecefbf63844d7cb2d5868539c39f973fe019f72e5c31a707836c0d61ef317db012321033e29aea1168a835d5e386c292082db7b7807172a10ec634ad34226f36d79e70facffffffff0100f90295000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000",

From bcf8d0177dfb9b7c859b010aec695293372bf764 Mon Sep 17 00:00:00 2001
From: Daniel Cousens <github@dcousens.com>
Date: Tue, 3 Mar 2015 09:55:17 +1100
Subject: [PATCH 3/4] TxBuilder: fix OP_0 in buildComplete

---
 src/transaction_builder.js | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/src/transaction_builder.js b/src/transaction_builder.js
index 924f177..827f386 100644
--- a/src/transaction_builder.js
+++ b/src/transaction_builder.js
@@ -242,10 +242,15 @@ TransactionBuilder.prototype.__build = function (allowIncomplete) {
           })
 
           // fill in blanks with OP_0
-          for (var i = 0; i < msSignatures.length; ++i) {
-            if (msSignatures[i]) continue
+          if (allowIncomplete) {
+            for (var i = 0; i < msSignatures.length; ++i) {
+              if (msSignatures[i]) continue
 
-            msSignatures[i] = ops.OP_0
+              msSignatures[i] = ops.OP_0
+            }
+          } else {
+            // Array.prototype.filter returns non-sparse array
+            msSignatures = msSignatures.filter(function (x) { return x })
           }
 
           var redeemScript = allowIncomplete ? undefined : input.redeemScript

From cdcbb2ccb4828135e797f6d7caffdfc218819ac2 Mon Sep 17 00:00:00 2001
From: Daniel Cousens <github@dcousens.com>
Date: Tue, 3 Mar 2015 10:43:33 +1100
Subject: [PATCH 4/4] tests: fix inconsistent key compression

---
 test/fixtures/transaction_builder.json | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/test/fixtures/transaction_builder.json b/test/fixtures/transaction_builder.json
index 68c83c3..d3a6ef0 100644
--- a/test/fixtures/transaction_builder.json
+++ b/test/fixtures/transaction_builder.json
@@ -150,7 +150,7 @@
             "vout": 0,
             "signs": [
               {
-                "privKey": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn",
+                "privKey": "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx",
                 "redeemScript": "OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a 04f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e672 OP_3 OP_CHECKMULTISIG"
               },
               {