예제 #1
0
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)
예제 #2
0
def name_to_address_middleware(w3):
    normalizers = [
        abi_ens_resolver(w3),
    ]
    return construct_formatting_middleware(
        request_formatters=abi_request_formatters(normalizers, RPC_ABIS)
    )
 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()
예제 #4
0
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_ens_resolver(None)], 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)))
예제 #5
0
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()
예제 #6
0
    def _encode_abi(cls, 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(cls.web3),
                abi_address_to_hex,
                abi_bytes_to_hex,
                abi_string_to_hex,
                hexstrs_to_bytes,
            ]
            normalized_arguments = map_abi_data(
                normalizers,
                argument_types,
                arguments,
            )
            encoded_arguments = 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)
예제 #7
0
    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)
예제 #8
0
파일: main.py 프로젝트: syngraph/web3.py
    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 __resolve_ens_name_to_address(ens_name, web3):
    (abi_type, resolved) = abi_ens_resolver(w3=web3,
                                            abi_type="address",
                                            val=ens_name)
    return resolved
예제 #10
0
파일: names.py 프로젝트: drummonda/pluto
def name_to_address_middleware(w3):
    normalizers = [
        abi_ens_resolver(w3),
    ]
    return construct_formatting_middleware(
        request_formatters=abi_request_formatters(normalizers, RPC_ABIS))