1515import datetime
1616import struct
1717from enum import IntEnum
18+ from typing import Callable
1819
1920from cryptography .exceptions import InvalidSignature
2021from cryptography .hazmat .primitives import hashes
4849from hathor .transaction .scripts .script_context import ScriptContext
4950
5051
52+ class OpcodeVersion (IntEnum ):
53+ V1 = 1
54+ V2 = 2
55+
56+
5157class Opcode (IntEnum ):
5258 OP_0 = 0x50
5359 OP_1 = 0x51
@@ -626,7 +632,7 @@ def op_integer(opcode: int, stack: Stack) -> None:
626632 raise ScriptError (e ) from e
627633
628634
629- def execute_op_code (opcode : Opcode , context : ScriptContext ) -> None :
635+ def execute_op_code (opcode : Opcode , context : ScriptContext , version : OpcodeVersion ) -> None :
630636 """
631637 Execute a function opcode.
632638
@@ -635,17 +641,27 @@ def execute_op_code(opcode: Opcode, context: ScriptContext) -> None:
635641 context: the script context to be manipulated.
636642 """
637643 context .logs .append (f'Executing function opcode { opcode .name } ({ hex (opcode .value )} )' )
638- match opcode :
639- case Opcode .OP_DUP : op_dup (context )
640- case Opcode .OP_EQUAL : op_equal (context )
641- case Opcode .OP_EQUALVERIFY : op_equalverify (context )
642- case Opcode .OP_CHECKSIG : op_checksig (context )
643- case Opcode .OP_HASH160 : op_hash160 (context )
644- case Opcode .OP_GREATERTHAN_TIMESTAMP : op_greaterthan_timestamp (context )
645- case Opcode .OP_CHECKMULTISIG : op_checkmultisig (context )
646- case Opcode .OP_DATA_STREQUAL : op_data_strequal (context )
647- case Opcode .OP_DATA_GREATERTHAN : op_data_greaterthan (context )
648- case Opcode .OP_DATA_MATCH_VALUE : op_data_match_value (context )
649- case Opcode .OP_CHECKDATASIG : op_checkdatasig (context )
650- case Opcode .OP_FIND_P2PKH : op_find_p2pkh (context )
651- case _: raise ScriptError (f'unknown opcode: { opcode } ' )
644+ opcode_fns : dict [Opcode , Callable [[ScriptContext ], None ]] = {
645+ Opcode .OP_DUP : op_dup ,
646+ Opcode .OP_EQUAL : op_equal ,
647+ Opcode .OP_EQUALVERIFY : op_equalverify ,
648+ Opcode .OP_CHECKSIG : op_checksig ,
649+ Opcode .OP_HASH160 : op_hash160 ,
650+ Opcode .OP_GREATERTHAN_TIMESTAMP : op_greaterthan_timestamp ,
651+ Opcode .OP_CHECKMULTISIG : op_checkmultisig ,
652+ }
653+
654+ if version == OpcodeVersion .V1 :
655+ opcode_fns .update ({
656+ Opcode .OP_DATA_STREQUAL : op_data_strequal ,
657+ Opcode .OP_DATA_GREATERTHAN : op_data_greaterthan ,
658+ Opcode .OP_DATA_MATCH_VALUE : op_data_match_value ,
659+ Opcode .OP_CHECKDATASIG : op_checkdatasig ,
660+ Opcode .OP_FIND_P2PKH : op_find_p2pkh ,
661+ })
662+
663+ opcode_fn = opcode_fns .get (opcode )
664+ if opcode_fn is None :
665+ raise ScriptError (f'unknown opcode: { opcode } ' )
666+
667+ opcode_fn (context )
0 commit comments