2017-09-27 08:30:31 +10:00
|
|
|
// <scriptSig> {serialized scriptPubKey script}
|
2016-12-14 15:52:38 +11:00
|
|
|
|
2018-06-25 16:25:12 +10:00
|
|
|
const bscript = require('../../script')
|
|
|
|
const types = require('../../types')
|
|
|
|
const typeforce = require('typeforce')
|
2017-09-27 08:30:31 +10:00
|
|
|
|
2018-06-25 16:25:12 +10:00
|
|
|
const p2ms = require('../multisig/')
|
|
|
|
const p2pk = require('../pubkey/')
|
|
|
|
const p2pkh = require('../pubkeyhash/')
|
2017-09-27 08:30:31 +10:00
|
|
|
|
|
|
|
function check (chunks, allowIncomplete) {
|
|
|
|
typeforce(types.Array, chunks)
|
|
|
|
if (chunks.length < 1) return false
|
|
|
|
|
2018-06-25 16:37:45 +10:00
|
|
|
const witnessScript = chunks[chunks.length - 1]
|
2017-09-27 08:30:31 +10:00
|
|
|
if (!Buffer.isBuffer(witnessScript)) return false
|
|
|
|
|
2018-06-25 16:37:45 +10:00
|
|
|
const witnessScriptChunks = bscript.decompile(witnessScript)
|
2017-09-27 08:30:31 +10:00
|
|
|
|
|
|
|
// is witnessScript a valid script?
|
2018-06-05 20:17:06 +10:00
|
|
|
if (!witnessScriptChunks || witnessScriptChunks.length === 0) return false
|
2017-09-27 08:30:31 +10:00
|
|
|
|
2018-06-25 16:37:45 +10:00
|
|
|
const witnessRawScriptSig = bscript.compile(chunks.slice(0, -1))
|
2017-09-27 08:30:31 +10:00
|
|
|
|
|
|
|
// match types
|
|
|
|
if (p2pkh.input.check(witnessRawScriptSig) &&
|
|
|
|
p2pkh.output.check(witnessScriptChunks)) return true
|
|
|
|
|
|
|
|
if (p2ms.input.check(witnessRawScriptSig, allowIncomplete) &&
|
|
|
|
p2ms.output.check(witnessScriptChunks)) return true
|
|
|
|
|
|
|
|
if (p2pk.input.check(witnessRawScriptSig) &&
|
|
|
|
p2pk.output.check(witnessScriptChunks)) return true
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
check.toJSON = function () { return 'witnessScriptHash input' }
|
|
|
|
|
2018-07-03 23:00:00 +10:00
|
|
|
module.exports = { check }
|
2018-12-26 14:25:33 +09:00
|
|
|
export {}
|