2016-11-02 02:30:37 +01:00
|
|
|
// m [pubKeys ...] n OP_CHECKMULTISIG
|
|
|
|
|
2018-12-28 05:18:42 +01:00
|
|
|
import * as bscript from '../../script'
|
|
|
|
import * as types from '../../types'
|
2018-12-29 13:39:19 +01:00
|
|
|
import { OPS } from '../../script'
|
2018-06-25 08:25:12 +02:00
|
|
|
const OP_INT_BASE = OPS.OP_RESERVED // OP_1 - 1
|
2016-11-02 02:30:37 +01:00
|
|
|
|
2018-12-28 05:18:42 +01:00
|
|
|
export function check (script: Buffer | Array<number | Buffer>, allowIncomplete?: boolean): boolean {
|
2018-12-28 17:55:07 +01:00
|
|
|
const chunks = <Array<number | Buffer>>bscript.decompile(script)
|
2016-11-02 02:30:37 +01:00
|
|
|
|
|
|
|
if (chunks.length < 4) return false
|
|
|
|
if (chunks[chunks.length - 1] !== OPS.OP_CHECKMULTISIG) return false
|
|
|
|
if (!types.Number(chunks[0])) return false
|
|
|
|
if (!types.Number(chunks[chunks.length - 2])) return false
|
2018-12-28 05:18:42 +01:00
|
|
|
const m = <number>chunks[0] - OP_INT_BASE
|
|
|
|
const n = <number>chunks[chunks.length - 2] - OP_INT_BASE
|
2016-11-02 02:30:37 +01:00
|
|
|
|
|
|
|
if (m <= 0) return false
|
|
|
|
if (n > 16) return false
|
|
|
|
if (m > n) return false
|
|
|
|
if (n !== chunks.length - 3) return false
|
|
|
|
if (allowIncomplete) return true
|
|
|
|
|
2018-12-28 05:18:42 +01:00
|
|
|
const keys = <Array<Buffer>> chunks.slice(1, -2)
|
2016-11-02 02:30:37 +01:00
|
|
|
return keys.every(bscript.isCanonicalPubKey)
|
|
|
|
}
|
2016-11-02 04:33:46 +01:00
|
|
|
check.toJSON = function () { return 'multi-sig output' }
|