match static method

P2SHMultisigInput? match(
  1. RawInput raw
)

Checks if the RawInput matches the expected format for a P2SHMultisigInput with any number of signatures. If it does it returns a P2SHMultisigInput for the input or else it returns null.

Implementation

static P2SHMultisigInput? match(RawInput raw) {

  final script = raw.script;
  if (script == null) return null;
  final ops = script.ops;
  if (ops.length < 2) return null;

  // Check that the first item is 0 which is necessary for CHECKMULTISIG
  if (ops[0].number != 0) return null;

  // Last push needs to be the redeemScript
  if (ops.last is! ScriptPushData) return null;

  // Check redeemScript is multisig
  late MultisigProgram multisig;
  try {
    multisig = MultisigProgram.decompile((ops.last as ScriptPushData).data);
  } on NoProgramMatch {
    return null;
  } on PushDataNotMinimal {
    return null;
  } on OutOfData {
    return null;
  }

  // Can only have upto threshold sigs plus OP_0 and redeemScript
  if (ops.length > 2 + multisig.threshold) return null;

  // Convert signature data into ECDSAInputSignatures
  final sigs
    = ops.getRange(1, ops.length-1)
    .map((op) => op.ecdsaSig).toList();

  // Fail if any signature is null
  if (sigs.any((sig) => sig == null)) return null;

  return P2SHMultisigInput(
    prevOut: raw.prevOut,
    program: multisig,
    // Cast necessary to ensure non-null, despite checking for null above
    sigs: sigs.whereType<ECDSAInputSignature>().toList(),
    sequence: raw.sequence,
  );

}