From 39a681d2d3f11c5388fa85d4318ee792b28eaff8 Mon Sep 17 00:00:00 2001
From: Daniel Cousens <github@dcousens.com>
Date: Wed, 17 Jan 2018 14:34:06 +1100
Subject: [PATCH] tests: add failing test for #981

---
 src/transaction_builder.js             |  8 ++--
 test/fixtures/transaction_builder.json | 53 +++++++++++++++++++++++++-
 test/transaction_builder.js            | 10 +++--
 3 files changed, 63 insertions(+), 8 deletions(-)

diff --git a/src/transaction_builder.js b/src/transaction_builder.js
index 12ff3c3..f0f9efe 100644
--- a/src/transaction_builder.js
+++ b/src/transaction_builder.js
@@ -259,7 +259,7 @@ function checkP2SHInput (input, redeemScriptHash) {
     if (input.prevOutType !== scriptTypes.P2SH) throw new Error('PrevOutScript must be P2SH')
 
     var prevOutScriptScriptHash = bscript.decompile(input.prevOutScript)[1]
-    if (!prevOutScriptScriptHash.equals(redeemScriptHash)) throw new Error('Inconsistent hash160(RedeemScript)')
+    if (!prevOutScriptScriptHash.equals(redeemScriptHash)) throw new Error('Inconsistent hash160(redeemScript)')
   }
 }
 
@@ -268,7 +268,7 @@ function checkP2WSHInput (input, witnessScriptHash) {
     if (input.prevOutType !== scriptTypes.P2WSH) throw new Error('PrevOutScript must be P2WSH')
 
     var scriptHash = bscript.decompile(input.prevOutScript)[1]
-    if (!scriptHash.equals(witnessScriptHash)) throw new Error('Inconsistent sha25(WitnessScript)')
+    if (!scriptHash.equals(witnessScriptHash)) throw new Error('Inconsistent sha256(witnessScript)')
   }
 }
 
@@ -310,7 +310,7 @@ function prepareInput (input, kpPubKey, redeemScript, witnessValue, witnessScrip
     checkP2SHInput(input, redeemScriptHash)
 
     expanded = expandOutput(redeemScript, undefined, kpPubKey)
-    if (!expanded.pubKeys) throw new Error('RedeemScript not supported "' + bscript.toASM(redeemScript) + '"')
+    if (!expanded.pubKeys) throw new Error(expanded.scriptType + ' not supported as redeemScript (' + bscript.toASM(witnessScript) + ')')
 
     prevOutType = btemplates.types.P2SH
     prevOutScript = btemplates.scriptHash.output.encode(redeemScriptHash)
@@ -323,7 +323,7 @@ function prepareInput (input, kpPubKey, redeemScript, witnessValue, witnessScrip
     checkP2WSHInput(input, witnessScriptHash)
 
     expanded = expandOutput(witnessScript, undefined, kpPubKey)
-    if (!expanded.pubKeys) throw new Error('WitnessScript not supported "' + bscript.toASM(redeemScript) + '"')
+    if (!expanded.pubKeys) throw new Error(expanded.scriptType + ' not supported as witnessScript (' + bscript.toASM(witnessScript) + ')')
 
     prevOutType = btemplates.types.P2WSH
     prevOutScript = btemplates.witnessScriptHash.output.encode(witnessScriptHash)
diff --git a/test/fixtures/transaction_builder.json b/test/fixtures/transaction_builder.json
index b9d2969..93e135d 100644
--- a/test/fixtures/transaction_builder.json
+++ b/test/fixtures/transaction_builder.json
@@ -1040,8 +1040,8 @@
               {
                 "keyPair": "L2FroWqrUgsPpTMhpXcAFnVDLPTToDbveh3bhDaU4jhe7Cw6YujN",
                 "hashType": 1,
-                "witnessScript": "038de63cf582d058a399a176825c045672d5ff8ea25b64d28d4375dcdb14c02b2b OP_CHECKSIG",
                 "redeemScript": "OP_0 0f9ea7bae7166c980169059e39443ed13324495b0d6678ce716262e879591210",
+                "witnessScript": "038de63cf582d058a399a176825c045672d5ff8ea25b64d28d4375dcdb14c02b2b OP_CHECKSIG",
                 "value": 80000
               }
             ],
@@ -1922,6 +1922,57 @@
           }
         ]
       },
+      {
+        "description": "Transaction w/ P2WSH(P2PK), signing with uncompressed public key",
+        "exception": "BIP143 rejects uncompressed public keys in P2WPKH or P2WSH",
+        "inputs": [
+          {
+            "txId": "2fddebc1a7e67e04fc6b77645ae9ae10eeaa35e168606587d79b031ebca33345",
+            "vout": 0,
+            "prevTxScript": "OP_0 5339df4de3854c4208376443ed075014ad996aa349ad6b5abf6c4d20f604d348",
+            "signs": [
+              {
+                "keyPair": "5JiHJJjdufSiMxbvnyNcKtQNLYH6SvUpQnRv9yZENFDWTQKQkzC",
+                "witnessScript": "04f56d09b32cefc818735150bf8560eefdaf30d2edb3fe557bf27682aedaed81bf9aaff7eeb496e088058ec548826c12b521dbb566a862d9b67677910c2b421e06 OP_CHECKSIG",
+                "value": 80000,
+                "throws": true
+              }
+            ]
+          }
+        ],
+        "outputs": [
+          {
+            "script": "OP_DUP OP_HASH160 851a33a5ef0d4279bd5854949174e2c65b1d4500 OP_EQUALVERIFY OP_CHECKSIG",
+            "value": 60000
+          }
+        ]
+      },
+      {
+        "description": "Transaction w/ P2SH(P2WSH(P2PK)), signing with uncompressed public key",
+        "exception": "BIP143 rejects uncompressed public keys in P2WPKH or P2WSH",
+        "inputs": [
+          {
+            "txId": "2fddebc1a7e67e04fc6b77645ae9ae10eeaa35e168606587d79b031ebca33345",
+            "vout": 0,
+            "prevTxScript": "OP_HASH160 5afe12b2827e3eac05fe3f17c59406ef262aa177 OP_EQUAL",
+            "signs": [
+              {
+                "keyPair": "5JiHJJjdufSiMxbvnyNcKtQNLYH6SvUpQnRv9yZENFDWTQKQkzC",
+                "redeemScript": "OP_0 5339df4de3854c4208376443ed075014ad996aa349ad6b5abf6c4d20f604d348",
+                "witnessScript": "04f56d09b32cefc818735150bf8560eefdaf30d2edb3fe557bf27682aedaed81bf9aaff7eeb496e088058ec548826c12b521dbb566a862d9b67677910c2b421e06 OP_CHECKSIG",
+                "value": 80000,
+                "throws": true
+              }
+            ]
+          }
+        ],
+        "outputs": [
+          {
+            "script": "OP_DUP OP_HASH160 851a33a5ef0d4279bd5854949174e2c65b1d4500 OP_EQUALVERIFY OP_CHECKSIG",
+            "value": 60000
+          }
+        ]
+      },
       {
         "exception": "RedeemScript not supported \"OP_RETURN 06deadbeef03f895a2ad89fb6d696497af486cb7c644a27aa568c7a18dd06113401115185474\"",
         "inputs": [
diff --git a/test/transaction_builder.js b/test/transaction_builder.js
index 6cbd43f..f2325a4 100644
--- a/test/transaction_builder.js
+++ b/test/transaction_builder.js
@@ -315,17 +315,21 @@ describe('TransactionBuilder', function () {
           input.signs.forEach(function (sign) {
             var keyPairNetwork = NETWORKS[sign.network || f.network]
             var keyPair2 = ECPair.fromWIF(sign.keyPair, keyPairNetwork)
-            var redeemScript
+            var redeemScript, witnessScript
 
             if (sign.redeemScript) {
               redeemScript = bscript.fromASM(sign.redeemScript)
             }
 
+            if (sign.witnessScript) {
+              witnessScript = bscript.fromASM(sign.witnessScript)
+            }
+
             if (!sign.throws) {
-              txb.sign(index, keyPair2, redeemScript, sign.hashType, sign.value)
+              txb.sign(index, keyPair2, redeemScript, sign.hashType, sign.value, witnessScript)
             } else {
               assert.throws(function () {
-                txb.sign(index, keyPair2, redeemScript, sign.hashType, sign.value)
+                txb.sign(index, keyPair2, redeemScript, sign.hashType, sign.value, witnessScript)
               }, new RegExp(f.exception))
             }
           })