match static method

TaprootScriptInput? match(
  1. RawInput raw,
  2. List<Uint8List> witness
)
override

Checks if the raw input and witness data match the expected format for a TaprootScriptInput with the control block and script. If it matches this returns a TaprootScriptInput for the input or else it returns null. The script must be valid with minimal push data. The control block must be the correct size and contain the correct 0xc0 tapscript version but the internal key and parity bit is not validated.

Implementation

static TaprootScriptInput? match(RawInput raw, List<Uint8List> witness) {

  if (raw.scriptSig.isNotEmpty) return null;
  if (witness.length < 2) return null;

  final controlBlock = witness.last;
  final lengthAfterKey = controlBlock.length - 33;

  if (
    controlBlock.length < 33
    || lengthAfterKey % 32 != 0
    || lengthAfterKey / 32 > 128
    || controlBlock[0] & 0xfe != TapLeaf.tapscriptVersion
  ) {
    return null;
  }

  try {

    return TaprootScriptInput(
      prevOut: raw.prevOut,
      controlBlock: controlBlock,
      tapscript: Script.decompile(witness[witness.length-2]),
      stack: witness.sublist(0, witness.length-2),
      sequence: raw.sequence,
    );

  } on OutOfData {
    return null;
  } on PushDataNotMinimal {
    return null;
  }

}