diff --git a/src/script.js b/src/script.js index 4bd2fdf..0277061 100644 --- a/src/script.js +++ b/src/script.js @@ -98,11 +98,11 @@ function decompile (buffer) { var d = pushdata.decode(buffer, i) // did reading a pushDataInt fail? empty script - if (d === null) return [] + if (d === null) return null i += d.size // attempt to read too much data? empty script - if (i + d.number > buffer.length) return [] + if (i + d.number > buffer.length) return null var data = buffer.slice(i, i + d.number) i += d.number diff --git a/src/templates/index.js b/src/templates/index.js index 4916c78..af09112 100644 --- a/src/templates/index.js +++ b/src/templates/index.js @@ -28,6 +28,8 @@ function classifyOutput (script) { // XXX: optimization, below functions .decompile before use var chunks = decompile(script) + if (!chunks) throw new TypeError('Invalid script') + if (multisig.output.check(chunks)) return types.MULTISIG if (pubKey.output.check(chunks)) return types.P2PK if (witnessCommitment.output.check(chunks)) return types.WITNESS_COMMITMENT @@ -39,6 +41,7 @@ function classifyOutput (script) { function classifyInput (script, allowIncomplete) { // XXX: optimization, below functions .decompile before use var chunks = decompile(script) + if (!chunks) throw new TypeError('Invalid script') if (pubKeyHash.input.check(chunks)) return types.P2PKH if (scriptHash.input.check(chunks, allowIncomplete)) return types.P2SH @@ -51,6 +54,7 @@ function classifyInput (script, allowIncomplete) { function classifyWitness (script, allowIncomplete) { // XXX: optimization, below functions .decompile before use var chunks = decompile(script) + if (!chunks) throw new TypeError('Invalid script') if (witnessPubKeyHash.input.check(chunks)) return types.P2WPKH if (witnessScriptHash.input.check(chunks, allowIncomplete)) return types.P2WSH diff --git a/src/templates/scripthash/input.js b/src/templates/scripthash/input.js index cc5bb74..2ce55e3 100644 --- a/src/templates/scripthash/input.js +++ b/src/templates/scripthash/input.js @@ -21,7 +21,7 @@ function check (script, allowIncomplete) { var redeemScriptChunks = bscript.decompile(lastChunk) // is redeemScript a valid script? - if (redeemScriptChunks.length === 0) return false + if (!redeemScriptChunks) return false // is redeemScriptSig push only? if (!bscript.isPushOnly(scriptSigChunks)) return false diff --git a/src/transaction_builder.js b/src/transaction_builder.js index acfc232..3a1310b 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -71,7 +71,7 @@ function expandInput (scriptSig, witnessStack) { var witnessProgram var chunks - var scriptSigChunks = bscript.decompile(scriptSig) + var scriptSigChunks = bscript.decompile(scriptSig) || [] var sigType = btemplates.classifyInput(scriptSigChunks, true) if (sigType === scriptTypes.P2SH) { p2sh = true @@ -209,7 +209,7 @@ function fixMultisigOrder (input, transaction, vin) { function expandOutput (script, scriptType, ourPubKey) { typeforce(types.Buffer, script) - var scriptChunks = bscript.decompile(script) + var scriptChunks = bscript.decompile(script) || [] if (!scriptType) { scriptType = btemplates.classifyOutput(script) } @@ -257,8 +257,9 @@ function checkP2SHInput (input, redeemScriptHash) { if (input.prevOutType) { if (input.prevOutType !== scriptTypes.P2SH) throw new Error('PrevOutScript must be P2SH') - var prevOutScriptScriptHash = bscript.decompile(input.prevOutScript)[1] - if (!prevOutScriptScriptHash.equals(redeemScriptHash)) throw new Error('Inconsistent hash160(redeemScript)') + var chunks = bscript.decompile(input.prevOutScript) + if (!chunks) throw new Error('Invalid prevOutScript') + if (!chunks[1].equals(redeemScriptHash)) throw new Error('Inconsistent hash160(redeemScript)') } } @@ -266,8 +267,9 @@ function checkP2WSHInput (input, witnessScriptHash) { if (input.prevOutType) { if (input.prevOutType !== scriptTypes.P2WSH) throw new Error('PrevOutScript must be P2WSH') - var scriptHash = bscript.decompile(input.prevOutScript)[1] - if (!scriptHash.equals(witnessScriptHash)) throw new Error('Inconsistent sha256(witnessScript)') + var chunks = bscript.decompile(input.prevOutScript) + if (!chunks) throw new Error('Invalid witnessScript') + if (!chunks[1].equals(witnessScriptHash)) throw new Error('Inconsistent sha256(witnessScript)') } } diff --git a/test/script.js b/test/script.js index 07f907f..5a51dbd 100644 --- a/test/script.js +++ b/test/script.js @@ -124,10 +124,10 @@ describe('script', function () { }) fixtures.invalid.decompile.forEach(function (f) { - it('decompiles ' + f.script + ' to [] because of "' + f.description + '"', function () { + it('fails to decompile ' + f.script + ', because "' + f.description + '"', function () { var chunks = bscript.decompile(Buffer.from(f.script, 'hex')) - assert.strictEqual(chunks.length, 0) + assert.strictEqual(chunks, null) }) }) })