Skip to content

Commit 6013bf0

Browse files
committed
tests: add passing and failing tests for witness*.input.encode/decode
1 parent b6a6b0a commit 6013bf0

File tree

2 files changed

+71
-6
lines changed

2 files changed

+71
-6
lines changed

test/fixtures/templates.json

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,30 @@
8686
{
8787
"type": "witnesspubkeyhash",
8888
"pubKey": "02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1",
89+
"signature": "304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801",
8990
"output": "OP_0 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5",
90-
"outputHex": "0014aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5"
91+
"outputHex": "0014aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5",
92+
"inputStack": [
93+
"304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801",
94+
"02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1"
95+
]
9196
},
9297
{
9398
"type": "witnessscripthash",
94-
"witnessScriptPubKey": "OP_2 02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1 0395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a OP_2 OP_CHECKMULTISIG",
95-
"witnessScriptSig": "OP_0 304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801 3045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d140501",
99+
"witnessScript": "OP_2 02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1 0395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a OP_2 OP_CHECKMULTISIG",
100+
"witnessData": [
101+
"",
102+
"304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801",
103+
"3045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d140501"
104+
],
96105
"output": "OP_0 32447752937d355ca2defddcd1f6b4fc53d182f8901cebbcff42f5e381bf0b80",
97106
"outputHex": "002032447752937d355ca2defddcd1f6b4fc53d182f8901cebbcff42f5e381bf0b80",
98-
"witness": "OP_0 304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801 3045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d140501 522102359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1210395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a52ae"
107+
"inputStack": [
108+
"",
109+
"304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801",
110+
"3045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d140501",
111+
"522102359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1210395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a52ae"
112+
]
99113
},
100114
{
101115
"type": "nulldata",

test/templates.js

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ var ops = require('bitcoin-ops')
88

99
var fixtures = require('./fixtures/templates.json')
1010

11+
function fromHex (x) { return Buffer.from(x, 'hex') }
12+
function toHex (x) { return x.toString('hex') }
13+
1114
describe('script-templates', function () {
1215
describe('classifyInput', function () {
1316
fixtures.valid.forEach(function (f) {
@@ -296,8 +299,8 @@ describe('script-templates', function () {
296299
fixtures.valid.forEach(function (f) {
297300
if (f.type !== 'scripthash') return
298301

299-
var redeemScript = bscript.fromASM(f.redeemScript)
300302
var redeemScriptSig = bscript.fromASM(f.redeemScriptSig)
303+
var redeemScript = bscript.fromASM(f.redeemScript)
301304
var input = btemplates.scriptHash.input.encode(redeemScriptSig, redeemScript)
302305

303306
it('encodes to ' + f.output, function () {
@@ -347,6 +350,31 @@ describe('script-templates', function () {
347350
})
348351
})
349352

353+
describe('witnessPubKeyHash.input', function () {
354+
fixtures.valid.forEach(function (f) {
355+
if (f.type !== 'pubkeyhash' && f.type !== 'witnesspubkeyhash') return
356+
if (!f.inputStack) return
357+
358+
var pubKey = Buffer.from(f.pubKey, 'hex')
359+
var signature = Buffer.from(f.signature, 'hex')
360+
361+
it('encodes to ' + f.input, function () {
362+
var inputStack = btemplates.witnessPubKeyHash.input.encodeStack(signature, pubKey)
363+
364+
assert.deepEqual(inputStack.map(toHex), f.inputStack)
365+
})
366+
367+
it('decodes to original arguments', function () {
368+
var fInputStack = f.inputStack.map(fromHex)
369+
370+
assert.deepEqual(btemplates.witnessPubKeyHash.input.decodeStack(fInputStack), {
371+
signature: signature,
372+
pubKey: pubKey
373+
})
374+
})
375+
})
376+
})
377+
350378
describe('witnessPubKeyHash.output', function () {
351379
fixtures.valid.forEach(function (f) {
352380
if (f.type !== 'witnesspubkeyhash') return
@@ -377,12 +405,35 @@ describe('script-templates', function () {
377405
})
378406
})
379407

408+
describe('witnessScriptHash.input', function () {
409+
fixtures.valid.forEach(function (f) {
410+
if (f.type !== 'witnessscripthash') return
411+
if (!f.inputStack || !f.witnessData) return
412+
413+
var witnessData = f.witnessData.map(fromHex)
414+
var witnessScript = bscript.fromASM(f.witnessScript || f.redeemScript)
415+
416+
it('encodes to ' + f.input, function () {
417+
var inputStack = btemplates.witnessScriptHash.input.encodeStack(witnessData, witnessScript)
418+
419+
assert.deepEqual(inputStack.map(toHex), f.inputStack)
420+
})
421+
422+
it('decodes to original arguments', function () {
423+
var result = btemplates.witnessScriptHash.input.decodeStack(f.inputStack.map(fromHex))
424+
425+
assert.deepEqual(result.witnessData.map(toHex), f.witnessData)
426+
assert.strictEqual(bscript.toASM(result.witnessScript), f.witnessScript)
427+
})
428+
})
429+
})
430+
380431
describe('witnessScriptHash.output', function () {
381432
fixtures.valid.forEach(function (f) {
382433
if (f.type !== 'witnessscripthash') return
383434
if (!f.output) return
384435

385-
var witnessScriptPubKey = bscript.fromASM(f.witnessScriptPubKey)
436+
var witnessScriptPubKey = bscript.fromASM(f.witnessScript)
386437
var scriptHash = bcrypto.hash256(witnessScriptPubKey)
387438
var output = btemplates.witnessScriptHash.output.encode(scriptHash)
388439

0 commit comments

Comments
 (0)