def abi_bytes_to_hex(abi_type, data): base, sub, arrlist = process_type(abi_type) if base == 'bytes' and not arrlist: bytes_data = hexstr_if_str(to_bytes, data) if not sub: return abi_type, to_hex(bytes_data) else: num_bytes = int(sub) if len(bytes_data) <= num_bytes: padded = bytes_data.ljust(num_bytes, b'\0') return abi_type, to_hex(padded) else: raise ValueError( "This value was expected to be at most %d bytes, but instead was %d: %r" % ((num_bytes, len(bytes_data), data)))
def encode_abi(web3, abi, arguments, data=None): argument_types = get_abi_input_types(abi) if not check_if_arguments_can_be_encoded(abi, arguments, {}): raise TypeError( "One or more arguments could not be encoded to the necessary " "ABI type. Expected types are: {0}".format( ', '.join(argument_types), )) try: normalizers = [ abi_ens_resolver(web3), abi_address_to_hex, abi_bytes_to_bytes, abi_string_to_text, ] normalized_arguments = map_abi_data( normalizers, argument_types, arguments, ) encoded_arguments = eth_abi_encode_abi( argument_types, normalized_arguments, ) except EncodingError as e: raise TypeError( "One or more arguments could not be encoded to the necessary " "ABI type: {0}".format(str(e))) if data: return to_hex(HexBytes(data) + encoded_arguments) else: return encode_hex(encoded_arguments)
def test_conversion_round_trip(value): intermediate_value = to_hex(value) result_value = to_int(hexstr=intermediate_value) error_msg = "Expected: {0!r}, Result: {1!r}, Intermediate: {2!r}".format( value, result_value, intermediate_value, ) assert result_value == value, error_msg
def detail_decode_data(data_list): detail_after = [] for i in data_list: if isinstance(i, list) and (detail_decode_data(i) not in detail_after): detail_after.append(detail_decode_data(i)) else: if i not in detail_after: str_i = to_hex(i) if str_i[:2] == '0x': detail_after.append(str_i[2:]) else: detail_after.append(str_i) return detail_after
def test_bytes_that_start_with_0x(): sneaky_bytes = b'0x\xde\xad' assert to_hex(sneaky_bytes) == '0x3078dead'
def test_to_hex(value, expected): assert to_hex(value) == expected
def sign(self, account, data=None, hexstr=None, text=None): message_hex = to_hex(data, hexstr=hexstr, text=text) return self.web3.manager.request_blocking( "platon_sign", [account, message_hex], )
def encode_abi(web3, abi, arguments, vmtype, data=None, setabi=None): arguments = list(arguments) if vmtype == 1: inputlength = len(abi['inputs']) if inputlength == len(arguments): if arguments: arrinputs = abi['inputs'] paramabi = encodeparameters(arrinputs, arguments, setabi) else: paramabi = [] else: raise Exception( 'The number of arguments is not matching the methods required number.' 'You need to pass {} arguments.'.format(inputlength)) magicnum = ['00', '61', '73', '6d'] paramabi.insert(0, fnv1_64(bytes(abi['name'], 'utf8'))) if abi['type'] == 'constructor': if data: data1 = bytes.fromhex(str(data, encoding='utf8')) deploydata = rlp.encode([data1, rlp.encode(paramabi)]) encodata = ''.join(magicnum) + deploydata.hex() return '0x' + encodata else: return '0x' + rlp.encode(paramabi).hex() else: encodata = rlp.encode(paramabi).hex() return '0x' + encodata else: argument_types = get_abi_input_types(abi) for j in range(len(argument_types)): if argument_types[j]: if argument_types[j] == 'address': hrpgot, data1 = bech32.decode(arguments[j][:3], arguments[j]) addr = to_checksum_address(bytes(data1)) arguments[j] = addr # .split(",") elif argument_types[j] == 'address[]': for i in range(len(arguments[j])): hrpgot, data1 = bech32.decode(arguments[j][i][:3], arguments[j][i]) addr = to_checksum_address(bytes(data1)) arguments[j][i] = addr if not check_if_arguments_can_be_encoded(abi, arguments, {}): raise TypeError( "One or more arguments could not be encoded to the necessary " "ABI type. Expected types are: {0}".format( ', '.join(argument_types), )) try: normalizers = [ abi_ens_resolver(web3), abi_address_to_hex, abi_bytes_to_bytes, abi_string_to_text, ] normalized_arguments = map_abi_data( normalizers, argument_types, arguments, ) encoded_arguments = eth_abi_encode_abi( argument_types, normalized_arguments, ) except EncodingError as e: raise TypeError( "One or more arguments could not be encoded to the necessary " "ABI type: {0}".format(str(e))) if data: return to_hex(HexBytes(data) + encoded_arguments) else: return encode_hex(encoded_arguments)
def wasmevent_decode( types, data, ): if isinstance(data, HexBytes) or isinstance(data, bytes): bufs = rlp_decode(hexstr2bytes(to_hex(data))) else: bufs = data data1 = [] # for i in range(len(bufs)): # buf = bufs[i] # type = types[i] if (not isinstance(bufs, tuple)) and (not isinstance(types, tuple)): buf = bufs type = types if type == 'string': tem = [] if isinstance(buf, list): for i in buf: tem.append(bytes.decode(HexBytes(i))) data1 = ''.join(tem) elif isinstance(buf, str): data1 = bytes.decode(HexBytes(buf)) elif isinstance(buf, tuple): data1 = [0 for x in range(len(buf))] for j in range(len(buf)): data1[j] = wasmevent_decode(types, buf[j]) elif type.startswith('uint') and not type.endswith(']'): digit = 0 if len(buf) <= 1: data1 = int(buf[0], 16) else: for i in range(len(buf)): digit += int(buf[i], 16) * (256**(len(buf) - 1 - i)) data1 = digit elif type == 'bool': if not buf: data1 = False elif int(buf[0], 16): data1 = True else: data1 = False elif type == 'int8': temp = np.uint8(decodeuint(buf)) data1 = np.int8(temp) elif type == 'int16': temp = np.uint16(decodeuint(buf)) data1 = np.int16(temp) elif type == 'int32': temp = np.uint32(decodeuint(buf)) data1 = np.int32(temp) elif type == 'int64': temp = np.uint64(decodeuint(buf)) data1 = np.int64(temp) elif type == 'float': data1 = struct.unpack('>f', HexBytes(buf)) elif type == 'double': data1 = struct.unpack('>d', HexBytes(buf)) elif type.endswith(']'): lasti = type.rindex('[') vectype = type[0:lasti] data1 = [0 for x in range(len(buf))] if vectype == 'uint8': for i in range(len(buf)): data1[i] = hex(int(buf[i], 16)) else: for i in range(len(buf)): data1[i] = wasmevent_decode(types, buf[i]) elif type.startswith('FixedHash'): data1 = '0x' + tostring_hex(buf) if type.endswith('<20>'): temp = [] try: temp = tobech32address('lax', data1) except: try: temp = tobech32address('lat', data1) except: raise ( 'wasmdecode error ! can not match FixedHash<20> type !' ) finally: data1 = temp else: if len(bufs) != len(types): if len(bufs[0]) == len(types): data1 = [0 for x in range(len(types))] for i in range(len(bufs[0])): buf = bufs[0][i] type = types[i] data1[i] = wasmevent_decode(type, buf) else: data1 = [0 for x in range(len(bufs))] for i in range(len(bufs)): buf = bufs[i] type = types[i] data1[i] = wasmevent_decode(type, buf) return data1
def wasmdecode_abi(types, data, setabi=None): if isinstance(data, HexBytes) or isinstance(data, bytes): buf = rlp_decode(hexstr2bytes(to_hex(data))) else: buf = data type = types['type'] name = types['name'] # data1 = buf # if isinstance(data,HexBytes): # decode_data = hexstr2bytes(to_hex(data)) if type == 'string': tem = [] if isinstance(buf, list): if not isinstance(buf[0], list): for i in buf: tem.append(bytes.decode(HexBytes(i))) data1 = ''.join(tem) else: data1 = [0 for x in range(len(buf))] for j in range(len(buf)): data1[j] = wasmdecode_abi({ 'type': type, 'name': '' }, buf[j], setabi) elif isinstance(buf, str): data1 = bytes.decode(HexBytes(buf)) elif isinstance(buf, tuple): data1 = [0 for x in range(len(buf))] for j in range(len(buf)): data1[j] = wasmdecode_abi({ 'type': type, 'name': '' }, buf[j], setabi) elif type.startswith('uint') and not type.endswith(']'): digit = 0 if len(buf) <= 1: data1 = int(buf[0], 16) else: for i in range(len(buf)): digit += int(buf[i], 16) * (256**(len(buf) - 1 - i)) data1 = digit elif type == 'bool': if not buf: data1 = False elif int(buf[0], 16): data1 = True else: data1 = False elif type == 'int8': temp = np.uint8(decodeuint(buf)) data1 = np.int8(temp) elif type == 'int16': temp = np.uint16(decodeuint(buf)) data1 = np.int16(temp) elif type == 'int32': temp = np.uint32(decodeuint(buf)) data1 = np.int32(temp) elif type == 'int64': temp = np.uint64(decodeuint(buf)) data1 = np.int64(temp) elif type == 'float': data1 = struct.unpack('>f', HexBytes(buf)) elif type == 'double': data1 = struct.unpack('>d', HexBytes(buf)) elif type.endswith(']'): lasti = type.rindex('[') vectype = type[0:lasti] if isinstance(buf, tuple) and len(buf) <= 1: buf = buf[0] data1 = [0 for x in range(len(buf))] if vectype == 'uint8': for i in range(len(buf)): data1[i] = hex(int(buf[i], 16)) else: for i in range(len(buf)): data1[i] = wasmdecode_abi({ 'type': vectype, 'name': '' }, buf[i], setabi) elif type.startswith('list'): i1 = type.index('<') i2 = type.index('>') itype = type[i1 + 1:i2] if isinstance(buf, tuple) and len(buf) <= 1: buf = buf[0] data1 = [0 for x in range(len(buf))] for j in range(len(buf)): data1[j] = (wasmdecode_abi({ 'type': itype, 'name': '' }, buf[j], setabi)) elif type.startswith('map'): i1 = type.index('<') i2 = type.index(',') i3 = type.index('>') ktype = type[i1 + 1:i2] vtype = type[i2 + 1:i3] data1 = [0 for x in range(len(buf))] for j in range(len(buf)): if len(buf[j]) <= 1 and len(buf[j][0]) >= 2: kvalue = wasmdecode_abi({ 'type': ktype, 'name': '' }, buf[j][0][0], setabi) vvalue = wasmdecode_abi({ 'type': vtype, 'name': '' }, buf[j][0][1], setabi) else: kvalue = wasmdecode_abi({ 'type': ktype, 'name': '' }, buf[j][0], setabi) vvalue = wasmdecode_abi({ 'type': vtype, 'name': '' }, buf[j][1], setabi) data1[j] = [kvalue, vvalue] elif type.startswith('pair'): i1 = type.index('<') i2 = type.index(',') i3 = type.index('>') ktype = type[i1 + 1:i2] vtype = type[i2 + 1:i3] if isinstance(buf, tuple) and len(buf) <= 1: buf = buf[0] data1 = [0 for x in range(len(buf))] data1[0] = wasmdecode_abi({'type': ktype, 'name': ''}, buf[0], setabi) data1[1] = wasmdecode_abi({'type': vtype, 'name': ''}, buf[1], setabi) elif type.startswith('set'): i1 = type.index('<') i2 = type.index('>') stype = type[i1 + 1:i2] if isinstance(buf, tuple) and len(buf) <= 1: buf = buf[0] data1 = [0 for x in range(len(buf))] for j in range(len(buf)): data1[j] = wasmdecode_abi({ 'type': stype, 'name': '' }, buf[j], setabi) data1 = set(data1) elif type == 'struct': structtype = [ item for item in setabi if item['type'] == 'struct' and item['name'] == name ] if not structtype: raise Exception('can not find struct in {} .'.format(name)) else: data1 = [0 for x in range(len(structtype[0]['inputs']))] for i in range(len(structtype[0]['inputs'])): data1[i] = wasmdecode_abi(structtype[0]['inputs'][i], buf[i], setabi) elif type.startswith('FixedHash'): data1 = '0x' + tostring_hex(buf) if type.endswith('<20>'): temp = [] try: temp = tobech32address('lax', data1) except: try: temp = tobech32address('lat', data1) except: raise ( 'wasmdecode error ! can not match FixedHash<20> type !' ) finally: data1 = temp else: structtype = [item for item in setabi if item['name'] == type] if not structtype: raise Exception('can not find struct through {} .'.format(type)) else: data1 = [0 for x in range(len(structtype[0]['inputs']))] for i in range(len(structtype[0]['inputs'])): data1[i] = wasmdecode_abi(structtype[0]['inputs'][i], buf[i], setabi) return data1