Skip to content

Commit 1157856

Browse files
authored
Merge pull request bitcoinjs#911 from bitcoinjs/segwittests
Add tests for scripts.witness*.input and fixes
2 parents ace2b55 + 8c217a7 commit 1157856

File tree

3 files changed

+130
-11
lines changed

3 files changed

+130
-11
lines changed
Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,63 @@
1-
// {signature} {pubKey}
1+
// <scriptSig> {serialized scriptPubKey script}
22

3-
var p2sh = require('../scripthash/input')
3+
var bscript = require('../../script')
4+
var types = require('../../types')
5+
var typeforce = require('typeforce')
6+
7+
var p2ms = require('../multisig/')
8+
var p2pk = require('../pubkey/')
9+
var p2pkh = require('../pubkeyhash/')
10+
11+
function check (chunks, allowIncomplete) {
12+
typeforce(types.Array, chunks)
13+
if (chunks.length < 1) return false
14+
15+
var witnessScript = chunks[chunks.length - 1]
16+
if (!Buffer.isBuffer(witnessScript)) return false
17+
18+
var witnessScriptChunks = bscript.decompile(witnessScript)
19+
20+
// is witnessScript a valid script?
21+
if (witnessScriptChunks.length === 0) return false
22+
23+
var witnessRawScriptSig = bscript.compile(chunks.slice(0, -1))
24+
25+
// match types
26+
if (p2pkh.input.check(witnessRawScriptSig) &&
27+
p2pkh.output.check(witnessScriptChunks)) return true
28+
29+
if (p2ms.input.check(witnessRawScriptSig, allowIncomplete) &&
30+
p2ms.output.check(witnessScriptChunks)) return true
31+
32+
if (p2pk.input.check(witnessRawScriptSig) &&
33+
p2pk.output.check(witnessScriptChunks)) return true
34+
35+
return false
36+
}
37+
check.toJSON = function () { return 'witnessScriptHash input' }
38+
39+
function encodeStack (witnessData, witnessScript) {
40+
typeforce({
41+
witnessData: [types.Buffer],
42+
witnessScript: types.Buffer
43+
}, {
44+
witnessData: witnessData,
45+
witnessScript: witnessScript
46+
})
47+
48+
return [].concat(witnessData, witnessScript)
49+
}
50+
51+
function decodeStack (chunks) {
52+
typeforce(check, chunks)
53+
return {
54+
witnessData: chunks.slice(0, -1),
55+
witnessScript: chunks[chunks.length - 1]
56+
}
57+
}
458

559
module.exports = {
6-
check: p2sh.check,
7-
decodeStack: p2sh.decodeStack,
8-
encodeStack: p2sh.encodeStack
60+
check: check,
61+
decodeStack: decodeStack,
62+
encodeStack: encodeStack
963
}

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)