|
|
|
|
@ -431,7 +431,23 @@ class OPPushDataGeneric:
|
|
|
|
|
or (isinstance(item, type) and issubclass(item, cls)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class OPGeneric: |
|
|
|
|
def __init__(self, matcher: Callable=None): |
|
|
|
|
if matcher is not None: |
|
|
|
|
self.matcher = matcher |
|
|
|
|
|
|
|
|
|
def match(self, op) -> bool: |
|
|
|
|
return self.matcher(op) |
|
|
|
|
|
|
|
|
|
@classmethod |
|
|
|
|
def is_instance(cls, item): |
|
|
|
|
# accept objects that are instances of this class |
|
|
|
|
# or other classes that are subclasses |
|
|
|
|
return isinstance(item, cls) \ |
|
|
|
|
or (isinstance(item, type) and issubclass(item, cls)) |
|
|
|
|
|
|
|
|
|
OPPushDataPubkey = OPPushDataGeneric(lambda x: x in (33, 65)) |
|
|
|
|
OP_ANYSEGWIT_VERSION = OPGeneric(lambda x: x in list(range(opcodes.OP_1, opcodes.OP_16 + 1))) |
|
|
|
|
|
|
|
|
|
SCRIPTPUBKEY_TEMPLATE_P2PKH = [opcodes.OP_DUP, opcodes.OP_HASH160, |
|
|
|
|
OPPushDataGeneric(lambda x: x == 20), |
|
|
|
|
@ -440,6 +456,7 @@ SCRIPTPUBKEY_TEMPLATE_P2SH = [opcodes.OP_HASH160, OPPushDataGeneric(lambda x: x
|
|
|
|
|
SCRIPTPUBKEY_TEMPLATE_WITNESS_V0 = [opcodes.OP_0, OPPushDataGeneric(lambda x: x in (20, 32))] |
|
|
|
|
SCRIPTPUBKEY_TEMPLATE_P2WPKH = [opcodes.OP_0, OPPushDataGeneric(lambda x: x == 20)] |
|
|
|
|
SCRIPTPUBKEY_TEMPLATE_P2WSH = [opcodes.OP_0, OPPushDataGeneric(lambda x: x == 32)] |
|
|
|
|
SCRIPTPUBKEY_TEMPLATE_ANYSEGWIT = [OP_ANYSEGWIT_VERSION, OPPushDataGeneric(lambda x: x in list(range(2, 40 + 1)))] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def match_script_against_template(script, template) -> bool: |
|
|
|
|
@ -459,6 +476,8 @@ def match_script_against_template(script, template) -> bool:
|
|
|
|
|
script_item = script[i] |
|
|
|
|
if OPPushDataGeneric.is_instance(template_item) and template_item.check_data_len(script_item[0]): |
|
|
|
|
continue |
|
|
|
|
if OPGeneric.is_instance(template_item) and template_item.match(script_item[0]): |
|
|
|
|
continue |
|
|
|
|
if template_item != script_item[0]: |
|
|
|
|
return False |
|
|
|
|
return True |
|
|
|
|
|