2017-09-27 00:30:31 +02:00
|
|
|
// <scriptSig> {serialized scriptPubKey script}
|
2016-12-14 05:52:38 +01:00
|
|
|
|
2019-03-06 13:38:36 +01:00
|
|
|
import * as bscript from '../../script';
|
|
|
|
const typeforce = require('typeforce');
|
2017-09-27 00:30:31 +02:00
|
|
|
|
2019-03-06 13:38:36 +01:00
|
|
|
import * as p2ms from '../multisig';
|
|
|
|
import * as p2pk from '../pubkey';
|
|
|
|
import * as p2pkh from '../pubkeyhash';
|
2017-09-27 00:30:31 +02:00
|
|
|
|
2019-03-07 06:16:45 +01:00
|
|
|
export function check(chunks: Buffer[], allowIncomplete?: boolean): boolean {
|
2019-03-06 13:38:36 +01:00
|
|
|
typeforce(typeforce.Array, chunks);
|
|
|
|
if (chunks.length < 1) return false;
|
2017-09-27 00:30:31 +02:00
|
|
|
|
2019-03-06 13:38:36 +01:00
|
|
|
const witnessScript = chunks[chunks.length - 1];
|
|
|
|
if (!Buffer.isBuffer(witnessScript)) return false;
|
2017-09-27 00:30:31 +02:00
|
|
|
|
2019-03-06 13:38:36 +01:00
|
|
|
const witnessScriptChunks = bscript.decompile(witnessScript);
|
2017-09-27 00:30:31 +02:00
|
|
|
|
|
|
|
// is witnessScript a valid script?
|
2019-03-06 13:38:36 +01:00
|
|
|
if (!witnessScriptChunks || witnessScriptChunks.length === 0) return false;
|
2017-09-27 00:30:31 +02:00
|
|
|
|
2019-03-06 13:38:36 +01:00
|
|
|
const witnessRawScriptSig = bscript.compile(chunks.slice(0, -1));
|
2017-09-27 00:30:31 +02:00
|
|
|
|
|
|
|
// match types
|
2019-03-06 13:38:36 +01:00
|
|
|
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;
|
2017-09-27 00:30:31 +02:00
|
|
|
}
|
2019-03-21 16:15:37 +01:00
|
|
|
check.toJSON = (): string => {
|
2019-03-06 13:38:36 +01:00
|
|
|
return 'witnessScriptHash input';
|
|
|
|
};
|