def test_hex_encode_abi_type(abi_type, value, expected): if isinstance(expected, type) and issubclass(expected, Exception): with pytest.raises(expected): hex_encode_abi_type(abi_type, value) return actual = hex_encode_abi_type(abi_type, value) assert actual == expected
def test_hex_encode_abi_type(abi_type, value, expected): if isinstance(expected, type) and issubclass(expected, Exception): with pytest.raises(expected): hex_encode_abi_type(abi_type, value) return actual = hex_encode_abi_type(abi_type, value) assert actual == expected
def pack_data(*args: Tuple[Any, str]) -> bytes: """Normalize data and pack them into a byte array""" values, abi_types = zip(*args) normalized_values = map_abi_data([abi_address_to_hex], abi_types, values) return decode_hex("".join( remove_0x_prefix(hex_encode_abi_type(abi_type, value)) for abi_type, value in zip(abi_types, normalized_values)))
def soliditySha256(self, abi_types, values): normalized_values = map_abi_data([abi_ens_resolver(Web3)], abi_types, values) #print(normalized_values) hex_string = add_0x_prefix(''.join( remove_0x_prefix(hex_encode_abi_type(abi_type, value)) for abi_type, value in zip(abi_types, normalized_values))) #print(hex_string) hash_object = hashlib.sha256(Web3.toBytes(hexstr=hex_string)) return hash_object.hexdigest()
def pack_data(abi_types, values) -> bytes: """Normalize data and pack them into a byte array""" if len(abi_types) != len(values): raise ValueError( "Length mismatch between provided abi types and values. Got " "{0} types and {1} values.".format(len(abi_types), len(values))) normalized_values = map_abi_data([abi_address_to_hex], abi_types, values) return decode_hex("".join( remove_0x_prefix(hex_encode_abi_type(abi_type, value)) for abi_type, value in zip(abi_types, normalized_values)))
def soliditySha3(self, abi_types, values): """ Executes sha3 (keccak256) exactly as Solidity does. Takes list of abi_types as inputs -- `[uint24, int8[], bool]` and list of corresponding values -- `[20, [-1, 5, 0], True]` """ if len(abi_types) != len(values): raise ValueError( "Length mismatch between provided abi types and values. Got " "{0} types and {1} values.".format(len(abi_types), len(values))) hex_string = add_0x_prefix(''.join( remove_0x_prefix(hex_encode_abi_type(abi_type, value)) for abi_type, value in zip(abi_types, values))) return self.sha3(hexstr=hex_string)
def pack_data(abi_types, values) -> bytes: """Normalize data and pack them into a byte array""" warnings.warn( 'eth_recover from raiden-libs is deprecated. ' 'Function is now moved in the raiden client', DeprecationWarning, ) if len(abi_types) != len(values): raise ValueError( "Length mismatch between provided abi types and values. Got " "{0} types and {1} values.".format(len(abi_types), len(values)), ) normalized_values = map_abi_data([abi_address_to_hex], abi_types, values) return decode_hex(''.join( remove_0x_prefix(hex_encode_abi_type(abi_type, value)) for abi_type, value in zip(abi_types, normalized_values)))
def get_request_bytes_representation(payee_id_addresses, amounts, payer, ipfs_hash=None): """ Return the bytes representation of the given Request data. The JS version uses lower-cased addresses but web3.py expects checksum addresses. To work around this the encoded result is converted to lowercase. address(creator) address(payer) uint8(number_of_payees) [ address(main_payee_address) int256(main_payee_expected_amount) address(second_payee_address) int256(second_payee_expected_amount) ... ] uint8(data_string_size) size(data) :return: """ ipfs_hash = ipfs_hash if ipfs_hash else '' payer = payer if payer else EMPTY_BYTES_20 parts = [(payee_id_addresses[0], 'address'), (payer, 'address'), (len(payee_id_addresses), 'uint8')] for i in range(0, len(payee_id_addresses)): parts.append((payee_id_addresses[i], 'address')) parts.append((amounts[i], 'int256')) parts.append((len(ipfs_hash), 'uint8')) parts.append((ipfs_hash, 'string')) values, abi_types = zip(*parts) # Taken from `Web3.soliditySha3` normalized_values = map_abi_data([abi_ens_resolver(w3)], abi_types, values) return add_0x_prefix(''.join( remove_0x_prefix(hex_encode_abi_type(abi_type, value)) for abi_type, value in zip(abi_types, normalized_values))).lower()
def soliditySha3(cls, abi_types, values): """ Executes sha3 (keccak256) exactly as Solidity does. Takes list of abi_types as inputs -- `[uint24, int8[], bool]` and list of corresponding values -- `[20, [-1, 5, 0], True]` """ if len(abi_types) != len(values): raise ValueError( "Length mismatch between provided abi types and values. Got " "{0} types and {1} values.".format(len(abi_types), len(values))) if isinstance(cls, type): w3 = None else: w3 = cls normalized_values = map_abi_data([abi_ens_resolver(w3)], abi_types, values) hex_string = add_0x_prefix(''.join( remove_0x_prefix(hex_encode_abi_type(abi_type, value)) for abi_type, value in zip(abi_types, normalized_values))) return cls.sha3(hexstr=hex_string)
def soliditySha3(cls, abi_types, values): """ Executes sha3 (keccak256) exactly as Solidity does. Takes list of abi_types as inputs -- `[uint24, int8[], bool]` and list of corresponding values -- `[20, [-1, 5, 0], True]` """ if len(abi_types) != len(values): raise ValueError( "Length mismatch between provided abi types and values. Got " "{0} types and {1} values.".format(len(abi_types), len(values)) ) if isinstance(cls, type): w3 = None else: w3 = cls normalized_values = map_abi_data([abi_ens_resolver(w3)], abi_types, values) hex_string = add_0x_prefix(''.join( remove_0x_prefix(hex_encode_abi_type(abi_type, value)) for abi_type, value in zip(abi_types, normalized_values) )) return cls.sha3(hexstr=hex_string)
def sha256_like_solidity(type_value_tuples): hex_string = add_0x_prefix(''.join( remove_0x_prefix(hex_encode_abi_type(abi_type, value)) for abi_type, value in type_value_tuples)) return add_0x_prefix(sha256(to_bytes(hexstr=hex_string)).hexdigest())