diff --git a/test/fixtures/templates.json b/test/fixtures/templates.json index 2623684..0cba6c6 100644 --- a/test/fixtures/templates.json +++ b/test/fixtures/templates.json @@ -86,16 +86,30 @@ { "type": "witnesspubkeyhash", "pubKey": "02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1", + "signature": "304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801", "output": "OP_0 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5", - "outputHex": "0014aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5" + "outputHex": "0014aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5", + "inputStack": [ + "304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801", + "02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1" + ] }, { "type": "witnessscripthash", - "witnessScriptPubKey": "OP_2 02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1 0395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a OP_2 OP_CHECKMULTISIG", - "witnessScriptSig": "OP_0 304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801 3045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d140501", + "witnessScript": "OP_2 02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1 0395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a OP_2 OP_CHECKMULTISIG", + "witnessData": [ + "", + "304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801", + "3045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d140501" + ], "output": "OP_0 32447752937d355ca2defddcd1f6b4fc53d182f8901cebbcff42f5e381bf0b80", "outputHex": "002032447752937d355ca2defddcd1f6b4fc53d182f8901cebbcff42f5e381bf0b80", - "witness": "OP_0 304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801 3045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d140501 522102359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1210395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a52ae" + "inputStack": [ + "", + "304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801", + "3045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d140501", + "522102359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1210395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a52ae" + ] }, { "type": "nulldata", diff --git a/test/templates.js b/test/templates.js index c3fc44e..104b326 100644 --- a/test/templates.js +++ b/test/templates.js @@ -8,6 +8,9 @@ var ops = require('bitcoin-ops') var fixtures = require('./fixtures/templates.json') +function fromHex (x) { return Buffer.from(x, 'hex') } +function toHex (x) { return x.toString('hex') } + describe('script-templates', function () { describe('classifyInput', function () { fixtures.valid.forEach(function (f) { @@ -296,8 +299,8 @@ describe('script-templates', function () { fixtures.valid.forEach(function (f) { if (f.type !== 'scripthash') return - var redeemScript = bscript.fromASM(f.redeemScript) var redeemScriptSig = bscript.fromASM(f.redeemScriptSig) + var redeemScript = bscript.fromASM(f.redeemScript) var input = btemplates.scriptHash.input.encode(redeemScriptSig, redeemScript) it('encodes to ' + f.output, function () { @@ -347,6 +350,31 @@ describe('script-templates', function () { }) }) + describe('witnessPubKeyHash.input', function () { + fixtures.valid.forEach(function (f) { + if (f.type !== 'pubkeyhash' && f.type !== 'witnesspubkeyhash') return + if (!f.inputStack) return + + var pubKey = Buffer.from(f.pubKey, 'hex') + var signature = Buffer.from(f.signature, 'hex') + + it('encodes to ' + f.input, function () { + var inputStack = btemplates.witnessPubKeyHash.input.encodeStack(signature, pubKey) + + assert.deepEqual(inputStack.map(toHex), f.inputStack) + }) + + it('decodes to original arguments', function () { + var fInputStack = f.inputStack.map(fromHex) + + assert.deepEqual(btemplates.witnessPubKeyHash.input.decodeStack(fInputStack), { + signature: signature, + pubKey: pubKey + }) + }) + }) + }) + describe('witnessPubKeyHash.output', function () { fixtures.valid.forEach(function (f) { if (f.type !== 'witnesspubkeyhash') return @@ -377,12 +405,35 @@ describe('script-templates', function () { }) }) + describe('witnessScriptHash.input', function () { + fixtures.valid.forEach(function (f) { + if (f.type !== 'witnessscripthash') return + if (!f.inputStack || !f.witnessData) return + + var witnessData = f.witnessData.map(fromHex) + var witnessScript = bscript.fromASM(f.witnessScript || f.redeemScript) + + it('encodes to ' + f.input, function () { + var inputStack = btemplates.witnessScriptHash.input.encodeStack(witnessData, witnessScript) + + assert.deepEqual(inputStack.map(toHex), f.inputStack) + }) + + it('decodes to original arguments', function () { + var result = btemplates.witnessScriptHash.input.decodeStack(f.inputStack.map(fromHex)) + + assert.deepEqual(result.witnessData.map(toHex), f.witnessData) + assert.strictEqual(bscript.toASM(result.witnessScript), f.witnessScript) + }) + }) + }) + describe('witnessScriptHash.output', function () { fixtures.valid.forEach(function (f) { if (f.type !== 'witnessscripthash') return if (!f.output) return - var witnessScriptPubKey = bscript.fromASM(f.witnessScriptPubKey) + var witnessScriptPubKey = bscript.fromASM(f.witnessScript) var scriptHash = bcrypto.hash256(witnessScriptPubKey) var output = btemplates.witnessScriptHash.output.encode(scriptHash)