script/tx: decompile returns maybe chunks

This commit is contained in:
Daniel Cousens 2018-04-14 01:42:48 +10:00
parent 58b465f745
commit 581f57ff6f
5 changed files with 17 additions and 11 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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)')
}
}

View file

@ -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)
})
})
})