def create_code_params_script(param_list: []) -> bytearray: builder = ParamsBuilder() length = len(param_list) for j in range(length): i = length - 1 - j BuildParams.push_param(param_list[i], builder) return builder.to_bytearray()
def push_map(dict_param: dict, builder: ParamsBuilder): builder.emit(NEWMAP) builder.emit(TOALTSTACK) for key, value in dict_param.items(): builder.emit(DUPFROMALTSTACK) BuildParams.push_param(key, builder) BuildParams.push_param(value, builder) builder.emit(SETITEM) builder.emit(FROMALTSTACK)
def create_code_params_script_builder(param_list: list, builder: ParamsBuilder): length = len(param_list) for j in range(length): i = length - 1 - j BuildParams.push_param(param_list[i], builder) return builder.to_bytes()
def test_op_code_to_int(self): builder = ParamsBuilder() for num in range(100000): builder.emit_push_int(num) op_code = builder.to_bytes().hex() builder.clear_up() value = ContractDataParser.op_code_to_int(op_code) self.assertEqual(num, value)
def from_public_key(cls, public_key: bytes): builder = ParamsBuilder() builder.emit_push_bytearray(bytearray(public_key)) builder.emit(CHECKSIG) return cls.__from_byte_script(builder.to_bytes())
def push_param(param, builder: ParamsBuilder): if isinstance(param, bytearray) or isinstance(param, bytes): builder.emit_push_bytearray(param) elif isinstance(param, str): builder.emit_push_bytearray(bytes(param.encode())) elif isinstance(param, bool): builder.emit_push_bool(param) elif isinstance(param, int): builder.emit_push_int(param) elif isinstance(param, dict): BuildParams.push_map(param, builder) elif isinstance(param, list): BuildParams.create_code_params_script_builder(param, builder) builder.emit_push_int(len(param)) builder.emit(PACK) elif isinstance(param, Struct): BuildParams.push_struct(param, builder) elif isinstance(param, Address): builder.emit_push_bytearray(param.to_bytes()) elif isinstance(param, Account): builder.emit_push_bytearray(param.get_address().to_bytes()) else: raise SDKException(ErrorCode.other_error('parameter type is error'))
def get_map_bytes(param_dict: dict): builder = ParamsBuilder() builder.emit(BuildParams.Type.dict_type.value) builder.emit(utils.big_int_to_neo_bytearray(len(param_dict))) for key, value in param_dict.items(): builder.emit(BuildParams.Type.bytearray_type.value) builder.emit_push_bytearray(str(key).encode()) if isinstance(value, bytearray) or isinstance(value, bytes): builder.emit(BuildParams.Type.bytearray_type.value) builder.emit_push_bytearray(bytearray(value)) elif isinstance(value, str): builder.emit(BuildParams.Type.bytearray_type.value) builder.emit_push_bytearray(value.encode()) elif isinstance(value, bool): builder.emit(BuildParams.Type.bool_type.value) builder.emit_push_bool(value) elif isinstance(value, int): builder.emit(BuildParams.Type.int_type.value) builder.emit_push_int(int(value)) else: raise Exception("param error") return builder.to_bytes()
def push_struct(struct_param: Struct, builder: ParamsBuilder): builder.emit_push_int(0) builder.emit(NEWSTRUCT) builder.emit(TOALTSTACK) for item in struct_param.param_list: BuildParams.push_param(item, builder) builder.emit(DUPFROMALTSTACK) builder.emit(SWAP) builder.emit(APPEND) builder.emit(FROMALTSTACK)
def create_code_params_script_builder(param_list: list, builder: ParamsBuilder): length = len(param_list) for j in range(length): i = length - 1 - j if isinstance(param_list[i], bytearray) or isinstance( param_list[i], bytes): builder.emit_push_byte_array(param_list[i]) elif isinstance(param_list[i], str): builder.emit_push_byte_array(bytes(param_list[i].encode())) elif isinstance(param_list[i], int): builder.emit_push_integer(param_list[i]) elif isinstance(param_list[i], bool): builder.emit_push_bool(param_list[i]) elif isinstance(param_list[i], dict): builder.emit_push_byte_array( BuildParams.get_map_bytes(dict(param_list[i]))) elif isinstance(param_list[i], list): BuildParams.create_code_params_script_builder( param_list[i], builder) builder.emit_push_integer(len(param_list[i])) builder.emit(PACK) return builder.to_array()
def address_from_bytes_pubkey(public_key: bytes): builder = ParamsBuilder() builder.emit_push_byte_array(bytearray(public_key)) builder.emit(CHECKSIG) addr = Address(Address.to_script_hash(builder.to_array())) return addr
def program_from_pubkey(public_key): builder = ParamsBuilder() builder.emit_push_byte_array(public_key) builder.emit(CHECKSIG) return builder.to_bytes()
def program_from_multi_pubkey(m: int, pub_keys: []) -> bytes: n = len(pub_keys) if m <= 0: raise SDKException(ErrorCode.other_error(f'Param error: m == {m}')) if m > n: raise SDKException( ErrorCode.other_error(f'Param error: m == {m} n == {n}')) if n > define.MULTI_SIG_MAX_PUBKEY_SIZE: raise SDKException( ErrorCode.other_error( f'Param error: n == {n} > {define.MULTI_SIG_MAX_PUBKEY_SIZE}' )) builder = ParamsBuilder() builder.emit_push_integer(m) pub_keys = ProgramBuilder.sort_public_keys(pub_keys) for pk in pub_keys: builder.emit_push_byte_array(pk) builder.emit_push_integer(n) builder.emit(CHECKMULTISIG) return builder.to_bytes()
def build_native_invoke_code(contract_address: bytes, version: bytes, method: str, params): builder = ParamsBuilder() build_neo_vm_param(builder, params) builder.emit_push_byte_array(method.encode()) builder.emit_push_byte_array(contract_address) builder.emit_push_integer(int.from_bytes(version, 'little')) builder.emit(SYSCALL) builder.emit_push_byte_array(b'Ontology.Native.Invoke') return builder.to_bytes()
def program_from_multi_pubkey(m: int, pubkeys: []): n = len(pubkeys) if m <= 0 or m > n or n > define.MULTI_SIG_MAX_PUBKEY_SIZE: raise Exception("param error") builder = ParamsBuilder() builder.emit_push_integer(m) pubkeys = ProgramBuilder.sort_publickeys(pubkeys) for pubkey in pubkeys: builder.emit_push_byte_array(pubkey) builder.emit_push_integer(n) builder.emit(CHECKMULTISIG) return builder.to_array()
def build_native_invoke_code(contractAddres, cversion, method, params): builder = ParamsBuilder() build_neo_vm_param(builder, params) builder.emit_push_byte_array(method.encode()) builder.emit_push_byte_array(contractAddres) builder.emit_push_integer(int.from_bytes(cversion, 'little')) builder.emit(SYSCALL) builder.emit_push_byte_array("Ontology.Native.Invoke".encode()) return builder.to_array()