Skip to content

Commit 97252b2

Browse files
authored
Merge pull request bitcoinjs#1039 from bitcoinjs/maybedec
decompile should return Maybe chunks
2 parents 58b465f + 4d7b516 commit 97252b2

File tree

6 files changed

+24
-12
lines changed

6 files changed

+24
-12
lines changed

src/script.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ function decompile (buffer) {
9898
var d = pushdata.decode(buffer, i)
9999

100100
// did reading a pushDataInt fail? empty script
101-
if (d === null) return []
101+
if (d === null) return null
102102
i += d.size
103103

104104
// attempt to read too much data? empty script
105-
if (i + d.number > buffer.length) return []
105+
if (i + d.number > buffer.length) return null
106106

107107
var data = buffer.slice(i, i + d.number)
108108
i += d.number

src/templates/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ function classifyOutput (script) {
2828

2929
// XXX: optimization, below functions .decompile before use
3030
var chunks = decompile(script)
31+
if (!chunks) throw new TypeError('Invalid script')
32+
3133
if (multisig.output.check(chunks)) return types.MULTISIG
3234
if (pubKey.output.check(chunks)) return types.P2PK
3335
if (witnessCommitment.output.check(chunks)) return types.WITNESS_COMMITMENT
@@ -39,6 +41,7 @@ function classifyOutput (script) {
3941
function classifyInput (script, allowIncomplete) {
4042
// XXX: optimization, below functions .decompile before use
4143
var chunks = decompile(script)
44+
if (!chunks) throw new TypeError('Invalid script')
4245

4346
if (pubKeyHash.input.check(chunks)) return types.P2PKH
4447
if (scriptHash.input.check(chunks, allowIncomplete)) return types.P2SH
@@ -51,6 +54,7 @@ function classifyInput (script, allowIncomplete) {
5154
function classifyWitness (script, allowIncomplete) {
5255
// XXX: optimization, below functions .decompile before use
5356
var chunks = decompile(script)
57+
if (!chunks) throw new TypeError('Invalid script')
5458

5559
if (witnessPubKeyHash.input.check(chunks)) return types.P2WPKH
5660
if (witnessScriptHash.input.check(chunks, allowIncomplete)) return types.P2WSH

src/templates/scripthash/input.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function check (script, allowIncomplete) {
2121
var redeemScriptChunks = bscript.decompile(lastChunk)
2222

2323
// is redeemScript a valid script?
24-
if (redeemScriptChunks.length === 0) return false
24+
if (!redeemScriptChunks) return false
2525

2626
// is redeemScriptSig push only?
2727
if (!bscript.isPushOnly(scriptSigChunks)) return false

src/transaction_builder.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function expandInput (scriptSig, witnessStack) {
7171
var witnessProgram
7272
var chunks
7373

74-
var scriptSigChunks = bscript.decompile(scriptSig)
74+
var scriptSigChunks = bscript.decompile(scriptSig) || []
7575
var sigType = btemplates.classifyInput(scriptSigChunks, true)
7676
if (sigType === scriptTypes.P2SH) {
7777
p2sh = true
@@ -209,7 +209,7 @@ function fixMultisigOrder (input, transaction, vin) {
209209
function expandOutput (script, scriptType, ourPubKey) {
210210
typeforce(types.Buffer, script)
211211

212-
var scriptChunks = bscript.decompile(script)
212+
var scriptChunks = bscript.decompile(script) || []
213213
if (!scriptType) {
214214
scriptType = btemplates.classifyOutput(script)
215215
}
@@ -257,17 +257,19 @@ function checkP2SHInput (input, redeemScriptHash) {
257257
if (input.prevOutType) {
258258
if (input.prevOutType !== scriptTypes.P2SH) throw new Error('PrevOutScript must be P2SH')
259259

260-
var prevOutScriptScriptHash = bscript.decompile(input.prevOutScript)[1]
261-
if (!prevOutScriptScriptHash.equals(redeemScriptHash)) throw new Error('Inconsistent hash160(redeemScript)')
260+
var chunks = bscript.decompile(input.prevOutScript)
261+
if (!chunks) throw new Error('Invalid prevOutScript')
262+
if (!chunks[1].equals(redeemScriptHash)) throw new Error('Inconsistent hash160(redeemScript)')
262263
}
263264
}
264265

265266
function checkP2WSHInput (input, witnessScriptHash) {
266267
if (input.prevOutType) {
267268
if (input.prevOutType !== scriptTypes.P2WSH) throw new Error('PrevOutScript must be P2WSH')
268269

269-
var scriptHash = bscript.decompile(input.prevOutScript)[1]
270-
if (!scriptHash.equals(witnessScriptHash)) throw new Error('Inconsistent sha256(witnessScript)')
270+
var chunks = bscript.decompile(input.prevOutScript)
271+
if (!chunks) throw new Error('Invalid witnessScript')
272+
if (!chunks[1].equals(witnessScriptHash)) throw new Error('Inconsistent sha256(witnessScript)')
271273
}
272274
}
273275

test/fixtures/transaction_builder.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1917,10 +1917,16 @@
19171917
}
19181918
]
19191919
},
1920+
{
1921+
"description": "Transaction w/ invalid scripts",
1922+
"exception": "Invalid script",
1923+
"incomplete": true,
1924+
"txHex": "010000000100000000171a0000e028f2000000000050178500000000000d0000000e000000000000002009f691b2263260e71f363d1db51ff3100d285956a40cc0e4f8c8c2c4a80559b1ffffffff0110270000000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000"
1925+
},
19201926
{
19211927
"description": "Complete transaction w/ non-standard inputs",
19221928
"exception": "nonstandard not supported",
1923-
"txHex": "010000000100000000171a0000e028f2000000000050178500000000000d0000000e000000000000002009f691b2263260e71f363d1db51ff3100d285956a40cc0e4f8c8c2c4a80559b1ffffffff0110270000000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000"
1929+
"txHex": "010000000100000000171a0000e028f2000000000050178500000000000d0000000e00000000000000201ff691b2263260e71f363d1db51ff3100d285956a40cc0e4f8c8c2c4a80559b1ffffffff0110270000000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000"
19241930
}
19251931
],
19261932
"sign": [

test/script.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,10 @@ describe('script', function () {
124124
})
125125

126126
fixtures.invalid.decompile.forEach(function (f) {
127-
it('decompiles ' + f.script + ' to [] because of "' + f.description + '"', function () {
127+
it('fails to decompile ' + f.script + ', because "' + f.description + '"', function () {
128128
var chunks = bscript.decompile(Buffer.from(f.script, 'hex'))
129129

130-
assert.strictEqual(chunks.length, 0)
130+
assert.strictEqual(chunks, null)
131131
})
132132
})
133133
})

0 commit comments

Comments
 (0)