getType static method

ScriptType? getType({
  1. required String hexData,
  2. bool hasSegwit = false,
})

Implementation

static ScriptType? getType(
    {required String hexData, bool hasSegwit = false}) {
  final Script s = fromRaw(hexData: hexData, hasSegwit: hasSegwit);
  if (s.script.isEmpty) return null;
  final first = s.script.elementAtOrNull(0);
  final sec = s.script.elementAtOrNull(1);
  final th = s.script.elementAtOrNull(2);
  final four = s.script.elementAtOrNull(3);
  final five = s.script.elementAtOrNull(4);
  if (first == "OP_0") {
    if (sec is String?) {
      if (sec?.length == 40) {
        return ScriptType.P2WPKH;
      } else if (sec?.length == 64) {
        return ScriptType.P2WSH;
      }
    }
  } else if (first == "OP_DUP") {
    if (sec == "OP_HASH160" &&
        four == "OP_EQUALVERIFY" &&
        five == "OP_CHECKSIG") {
      return ScriptType.P2PKH;
    }
  } else if (first == "OP_HASH160" && th == "OP_EQUAL") {
    return ScriptType.P2SH;
  } else if (sec == "OP_CHECKSIG" && first is String) {
    if (first.length == 66) {
      return ScriptType.P2PK;
    }
  }
  return null;
}