Merge pull request #1039 from bitcoinjs/maybedec
decompile should return Maybe chunks
This commit is contained in:
commit
97252b2872
6 changed files with 24 additions and 12 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)')
|
||||
}
|
||||
}
|
||||
|
||||
|
|
8
test/fixtures/transaction_builder.json
vendored
8
test/fixtures/transaction_builder.json
vendored
|
@ -1917,10 +1917,16 @@
|
|||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "Transaction w/ invalid scripts",
|
||||
"exception": "Invalid script",
|
||||
"incomplete": true,
|
||||
"txHex": "010000000100000000171a0000e028f2000000000050178500000000000d0000000e000000000000002009f691b2263260e71f363d1db51ff3100d285956a40cc0e4f8c8c2c4a80559b1ffffffff0110270000000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000"
|
||||
},
|
||||
{
|
||||
"description": "Complete transaction w/ non-standard inputs",
|
||||
"exception": "nonstandard not supported",
|
||||
"txHex": "010000000100000000171a0000e028f2000000000050178500000000000d0000000e000000000000002009f691b2263260e71f363d1db51ff3100d285956a40cc0e4f8c8c2c4a80559b1ffffffff0110270000000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000"
|
||||
"txHex": "010000000100000000171a0000e028f2000000000050178500000000000d0000000e00000000000000201ff691b2263260e71f363d1db51ff3100d285956a40cc0e4f8c8c2c4a80559b1ffffffff0110270000000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000"
|
||||
}
|
||||
],
|
||||
"sign": [
|
||||
|
|
|
@ -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)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue