Skip to content

Commit 83c6681

Browse files
committed
templates: re-add decodeStack, add tests
1 parent 3b1a222 commit 83c6681

File tree

6 files changed

+56
-4
lines changed

6 files changed

+56
-4
lines changed

src/script.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,10 @@ function toASM (chunks) {
133133

134134
return chunks.map(function (chunk) {
135135
// data?
136-
if (Buffer.isBuffer(chunk)) return chunk.toString('hex')
136+
if (Buffer.isBuffer(chunk)) {
137+
if (chunk.length === 0) return 'OP_0'
138+
return chunk.toString('hex')
139+
}
137140

138141
// opcode!
139142
return REVERSE_OPS[chunk]

src/templates/multisig/input.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,16 @@ function decode (buffer, allowIncomplete) {
5353
return chunks.slice(1)
5454
}
5555

56+
function decodeStack (stack, allowIncomplete) {
57+
typeforce(bscript.isStack, stack)
58+
var buffer = bscript.compile(stack)
59+
return decode(buffer, allowIncomplete)
60+
}
61+
5662
module.exports = {
5763
check: check,
5864
decode: decode,
65+
decodeStack: decodeStack,
5966
encode: encode,
6067
encodeStack: encodeStack,
6168
encodeRaw: encodeRaw

src/templates/pubkey/input.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,15 @@ function decode (buffer) {
3131
return chunks[0]
3232
}
3333

34+
function decodeStack (stack) {
35+
typeforce(bscript.isStack, stack)
36+
return decode(stack)
37+
}
38+
3439
module.exports = {
3540
check: check,
3641
decode: decode,
42+
decodeStack: decodeStack,
3743
encode: encode,
3844
encodeStack: encodeStack,
3945
encodeRaw: encodeRaw

src/templates/pubkeyhash/input.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,15 @@ function decode (buffer) {
4040
}
4141
}
4242

43+
function decodeStack (stack) {
44+
typeforce(bscript.isStack, stack)
45+
return decode(stack)
46+
}
47+
4348
module.exports = {
4449
check: check,
4550
decode: decode,
51+
decodeStack: decodeStack,
4652
encode: encode,
4753
encodeStack: encodeStack,
4854
encodeRaw: encodeRaw

src/templates/scripthash/input.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,15 @@ function decode (buffer) {
5353
}
5454
}
5555

56+
function decodeStack (stack, allowIncomplete) {
57+
typeforce(bscript.isStack, stack)
58+
return decode(stack)
59+
}
60+
5661
module.exports = {
5762
check: check,
5863
decode: decode,
64+
decodeStack: decodeStack,
5965
encode: encode,
6066
encodeStack: encodeStack,
6167
encodeRaw: encodeRaw

test/templates.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,19 @@ describe('script-templates', function () {
146146

147147
var signature = Buffer.from(f.signature, 'hex')
148148
var input = bscript.pubKey.input.encode(signature)
149+
var inputStack = bscript.pubKey.input.encodeStack(signature)
149150

150151
it('encodes to ' + f.input, function () {
151152
assert.strictEqual(bscript.toASM(input), f.input)
153+
assert.strictEqual(bscript.toASM(inputStack), f.input)
152154
})
153155

154156
it('decodes to ' + f.signature, function () {
155-
assert.deepEqual(bscript.pubKey.input.decode(input), signature)
157+
var unpack = bscript.pubKey.input.decode(input)
158+
assert.deepEqual(unpack, signature)
159+
160+
unpack = bscript.pubKey.input.decodeStack(inputStack)
161+
assert.deepEqual(unpack, signature)
156162
})
157163
})
158164
})
@@ -181,13 +187,22 @@ describe('script-templates', function () {
181187
var pubKey = Buffer.from(f.pubKey, 'hex')
182188
var signature = Buffer.from(f.signature, 'hex')
183189
var input = bscript.pubKeyHash.input.encode(signature, pubKey)
190+
var inputStack = bscript.pubKeyHash.input.encodeStack(signature, pubKey)
184191

185192
it('encodes to ' + f.input, function () {
186193
assert.strictEqual(bscript.toASM(input), f.input)
194+
assert.strictEqual(bscript.toASM(inputStack), f.input)
187195
})
188196

189197
it('decodes to original arguments', function () {
190-
assert.deepEqual(bscript.pubKeyHash.input.decode(input), {
198+
var unpack = bscript.pubKeyHash.input.decode(input)
199+
assert.deepEqual(unpack, {
200+
signature: signature,
201+
pubKey: pubKey
202+
})
203+
204+
unpack = bscript.pubKeyHash.input.decodeStack(inputStack)
205+
assert.deepEqual(unpack, {
191206
signature: signature,
192207
pubKey: pubKey
193208
})
@@ -234,13 +249,16 @@ describe('script-templates', function () {
234249
})
235250

236251
var input = bscript.multisig.input.encode(signatures)
252+
var inputStack = bscript.multisig.input.encodeStack(signatures)
237253

238254
it('encodes to ' + f.input, function () {
239255
assert.strictEqual(bscript.toASM(input), f.input)
256+
assert.strictEqual(bscript.toASM(inputStack), f.input)
240257
})
241258

242259
it('decodes to ' + signatures.map(function (x) { return x === ops.OP_0 ? 'OP_0' : x.toString('hex') }), function () {
243260
assert.deepEqual(bscript.multisig.input.decode(input, allowIncomplete), signatures)
261+
assert.deepEqual(bscript.multisig.input.decodeStack(inputStack, allowIncomplete), signatures)
244262
})
245263
})
246264

@@ -302,8 +320,9 @@ describe('script-templates', function () {
302320
var redeemScript = bscript.fromASM(f.redeemScript)
303321
var redeemScriptSig = bscript.fromASM(f.redeemScriptSig)
304322
var input = bscript.scriptHash.input.encode(redeemScriptSig, redeemScript)
323+
var inputStack = bscript.scriptHash.input.encodeStack(redeemScriptSig, redeemScript)
305324

306-
it('encodes to ' + f.output, function () {
325+
it('encodes to ' + f.input, function () {
307326
if (f.input) {
308327
assert.strictEqual(bscript.toASM(input), f.input)
309328
} else {
@@ -316,6 +335,11 @@ describe('script-templates', function () {
316335
redeemScriptSig: redeemScriptSig,
317336
redeemScript: redeemScript
318337
})
338+
339+
assert.deepEqual(bscript.scriptHash.input.decodeStack(inputStack), {
340+
redeemScriptSig: redeemScriptSig,
341+
redeemScript: redeemScript
342+
})
319343
})
320344
})
321345
})

0 commit comments

Comments
 (0)