2016-11-02 12:30:37 +11:00
|
|
|
// <scriptSig> {serialized scriptPubKey script}
|
|
|
|
|
2018-12-28 14:57:30 +09:00
|
|
|
import * as bscript from '../../script'
|
|
|
|
import * as p2ms from '../multisig'
|
|
|
|
import * as p2pk from '../pubkey'
|
|
|
|
import * as p2pkh from '../pubkeyhash'
|
|
|
|
import * as p2wpkho from '../witnesspubkeyhash/output'
|
|
|
|
import * as p2wsho from '../witnessscripthash/output'
|
2018-06-25 16:25:12 +10:00
|
|
|
|
2018-12-29 21:39:19 +09:00
|
|
|
|
2017-09-26 13:32:14 +10:00
|
|
|
|
2018-12-28 14:57:30 +09:00
|
|
|
export function check (script: Buffer | Array<number | Buffer>, allowIncomplete?: boolean): boolean {
|
2019-01-15 17:47:30 +09:00
|
|
|
const chunks = bscript.decompile(script)!
|
2017-01-03 20:09:58 +01:00
|
|
|
if (chunks.length < 1) return false
|
2016-11-02 12:30:37 +11:00
|
|
|
|
2018-06-25 16:37:45 +10:00
|
|
|
const lastChunk = chunks[chunks.length - 1]
|
2016-11-02 12:30:37 +11:00
|
|
|
if (!Buffer.isBuffer(lastChunk)) return false
|
|
|
|
|
2019-01-15 17:47:30 +09:00
|
|
|
const scriptSigChunks = bscript.decompile(bscript.compile(chunks.slice(0, -1)))!
|
|
|
|
const redeemScriptChunks = bscript.decompile(lastChunk)
|
2016-11-02 12:30:37 +11:00
|
|
|
|
2017-01-20 21:07:19 +01:00
|
|
|
// is redeemScript a valid script?
|
2018-04-14 01:42:48 +10:00
|
|
|
if (!redeemScriptChunks) return false
|
2016-11-02 12:30:37 +11:00
|
|
|
|
2016-12-14 15:52:15 +11:00
|
|
|
// is redeemScriptSig push only?
|
|
|
|
if (!bscript.isPushOnly(scriptSigChunks)) return false
|
|
|
|
|
2017-09-22 12:01:06 +10:00
|
|
|
// is witness?
|
2017-01-03 20:15:28 +01:00
|
|
|
if (chunks.length === 1) {
|
2017-09-26 13:32:14 +10:00
|
|
|
return p2wsho.check(redeemScriptChunks) ||
|
|
|
|
p2wpkho.check(redeemScriptChunks)
|
2017-01-03 20:09:58 +01:00
|
|
|
}
|
2017-09-22 12:01:06 +10:00
|
|
|
|
|
|
|
// match types
|
2017-09-26 13:32:14 +10:00
|
|
|
if (p2pkh.input.check(scriptSigChunks) &&
|
|
|
|
p2pkh.output.check(redeemScriptChunks)) return true
|
2017-09-22 12:01:06 +10:00
|
|
|
|
2017-09-26 13:32:14 +10:00
|
|
|
if (p2ms.input.check(scriptSigChunks, allowIncomplete) &&
|
|
|
|
p2ms.output.check(redeemScriptChunks)) return true
|
2017-09-22 12:01:06 +10:00
|
|
|
|
2017-09-26 13:32:14 +10:00
|
|
|
if (p2pk.input.check(scriptSigChunks) &&
|
|
|
|
p2pk.output.check(redeemScriptChunks)) return true
|
2017-09-22 12:01:06 +10:00
|
|
|
|
|
|
|
return false
|
2016-11-02 12:30:37 +11:00
|
|
|
}
|
2016-11-02 14:33:46 +11:00
|
|
|
check.toJSON = function () { return 'scriptHash input' }
|