text: // OP_0 [signatures ...]
  
  var Buffer = require('safe-buffer').Buffer
  var bscript = require('../../script')
  var p2mso = require('./output')
  var typeforce = require('typeforce')
  var OPS = require('@psf/bitcoincash-ops')
  
  function partialSignature (value) {
    return value === OPS.OP_0 || bscript.isCanonicalSignature(value)
  }
  
  function check (script, allowIncomplete) {
    var chunks = bscript.decompile(script)
    if (chunks.length < 2) return false
    if (chunks[0] !== OPS.OP_0) return false
  
    if (allowIncomplete) {
      return chunks.slice(1).every(partialSignature)
    }
  
    return chunks.slice(1).every(bscript.isCanonicalSignature)
  }
  check.toJSON = function () { return 'multisig input' }
  
  var EMPTY_BUFFER = Buffer.allocUnsafe(0)
  
  function encodeStack (signatures, scriptPubKey) {
    typeforce([partialSignature], signatures)
  
    if (scriptPubKey) {
      var scriptData = p2mso.decode(scriptPubKey)
  
      if (signatures.length < scriptData.m) {
        throw new TypeError('Not enough signatures provided')
      }
  
      if (signatures.length > scriptData.pubKeys.length) {
        throw new TypeError('Too many signatures provided')
      }
    }
  
    return [].concat(EMPTY_BUFFER, signatures.map(function (sig) {
      if (sig === OPS.OP_0) {
        return EMPTY_BUFFER
      }
      return sig
    }))
  }
  
  function encode (signatures, scriptPubKey) {
    return bscript.compile(encodeStack(signatures, scriptPubKey))
  }
  
  function decodeStack (stack, allowIncomplete) {
    typeforce(typeforce.Array, stack)
    typeforce(check, stack, allowIncomplete)
    return stack.slice(1)
  }
  
  function decode (buffer, allowIncomplete) {
    var stack = bscript.decompile(buffer)
    return decodeStack(stack, allowIncomplete)
  }
  
  module.exports = {
    check: check,
    decode: decode,
    decodeStack: decodeStack,
    encode: encode,
    encodeStack: encodeStack
  }
  