Program.match constructor

Program.match(
  1. Script script
)

Takes a script and constructs a matching Program subclass if one exists, or a basic RawProgram if there is no match. The script should use minimal pushes. Program.decompile can be used directly on compiled scripts or Program.fromAsm can be used to match directly against ASM.

Implementation

factory Program.match(Script script) {

  try {
    return P2PKH.fromScript(script);
  } on NoProgramMatch catch(_) {}

  try {
    return P2SH.fromScript(script);
  } on NoProgramMatch catch(_) {}

  try {
    return P2TR.fromScript(script);
  } on NoProgramMatch catch(_) {}

  try {
    return P2WPKH.fromScript(script);
  } on NoProgramMatch catch(_) {}

  try {
    return P2WSH.fromScript(script);
  } on NoProgramMatch catch(_) {}

  // If no specific witness output matched, match with generic witness output
  try {
    return P2Witness.fromScript(script);
  } on NoProgramMatch catch(_) {}

  try {
    return MultisigProgram.fromScript(script);
  } on NoProgramMatch catch(_) {}

  // If nothing matched, return a raw program
  return RawProgram(script);

}