Python web3.Web3.keccak() Examples

The following are 13 code examples of web3.Web3.keccak(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module web3.Web3 , or try the search function .
Example #1
Source File: conftest.py    From vyper with Apache License 2.0 5 votes vote down vote up
def keccak():
    return Web3.keccak 
Example #2
Source File: ethereum.py    From agents-aea with Apache License 2.0 5 votes vote down vote up
def get_address_from_public_key(cls, public_key: str) -> str:
        """
        Get the address from the public key.

        :param public_key: the public key
        :return: str
        """
        keccak_hash = Web3.keccak(hexstr=public_key)
        raw_address = keccak_hash[-20:].hex().upper()
        address = Web3.toChecksumAddress(raw_address)
        return address 
Example #3
Source File: ethereum.py    From agents-aea with Apache License 2.0 5 votes vote down vote up
def generate_tx_nonce(self, seller: Address, client: Address) -> str:
        """
        Generate a unique hash to distinguish txs with the same terms.

        :param seller: the address of the seller.
        :param client: the address of the client.
        :return: return the hash in hex.
        """
        time_stamp = int(time.time())
        aggregate_hash = Web3.keccak(
            b"".join([seller.encode(), client.encode(), time_stamp.to_bytes(32, "big")])
        )
        return aggregate_hash.hex() 
Example #4
Source File: test_keccak.py    From web3.py with MIT License 5 votes vote down vote up
def test_keccak_text(message, digest):
    assert Web3.keccak(text=message) == digest 
Example #5
Source File: test_keccak.py    From web3.py with MIT License 5 votes vote down vote up
def test_keccak_hexstr(hexstr, digest):
    assert Web3.keccak(hexstr=hexstr) == digest 
Example #6
Source File: test_keccak.py    From web3.py with MIT License 5 votes vote down vote up
def test_keccak_primitive_invalid(primitive, exception):
    with pytest.raises(exception):
        Web3.keccak(primitive) 
Example #7
Source File: test_keccak.py    From web3.py with MIT License 5 votes vote down vote up
def test_keccak_primitive(primitive, digest):
    assert Web3.keccak(primitive) == digest 
Example #8
Source File: test_keccak.py    From web3.py with MIT License 5 votes vote down vote up
def test_keccak_raise_if_primitive_and(kwargs):
    # must not set more than one input
    with pytest.raises(TypeError):
        Web3.keccak('', **kwargs) 
Example #9
Source File: test_keccak.py    From web3.py with MIT License 5 votes vote down vote up
def test_keccak_raise_if_no_args():
    with pytest.raises(TypeError):
        Web3.keccak() 
Example #10
Source File: test_keccak.py    From web3.py with MIT License 5 votes vote down vote up
def test_deprecated_bound_method():
    w3 = Web3()
    h = HexBytes('0x0f355f04c0a06eebac1d219b34c598f85a1169badee164be8a30345944885fe8')
    with pytest.warns(DeprecationWarning, match='sha3 is deprecated in favor of keccak'):
        assert w3.sha3(text='cowmö') == h 
Example #11
Source File: proofs.py    From raiden-contracts with MIT License 5 votes vote down vote up
def eth_sign_hash_message(encoded_message: bytes) -> bytes:
    signature_prefix = "\x19Ethereum Signed Message:\n"
    return Web3.keccak(
        Web3.toBytes(text=signature_prefix)
        + Web3.toBytes(text=str(len(encoded_message)))
        + encoded_message
    ) 
Example #12
Source File: test_monitoring_service.py    From raiden-contracts with MIT License 5 votes vote down vote up
def test_updateReward(
    monitoring_service_internals: Contract,
    ms_address: HexAddress,
    token_network: Contract,
    monitor_data_internal: Dict,
) -> None:
    A, B = monitor_data_internal["participants"]
    reward_identifier = Web3.keccak(
        encode_single("uint256", monitor_data_internal["channel_identifier"])
        + Web3.toBytes(hexstr=token_network.address)
    )

    def update_with_nonce(nonce: int) -> None:
        call_and_transact(
            monitoring_service_internals.functions.updateRewardPublic(
                token_network.address,
                A,
                B,
                REWARD_AMOUNT,
                nonce,
                ms_address,
                monitor_data_internal["non_closing_signature"],
                monitor_data_internal["reward_proof_signature"],
            ),
            {"from": ms_address},
        )

    # normal first call succeeds
    update_with_nonce(2)
    assert monitoring_service_internals.functions.rewardNonce(reward_identifier).call() == 2

    # calling again with same nonce fails
    with pytest.raises(TransactionFailed, match="stale nonce"):
        update_with_nonce(2)

    # calling again with higher nonce succeeds
    update_with_nonce(3)
    assert monitoring_service_internals.functions.rewardNonce(reward_identifier).call() == 3 
Example #13
Source File: helpers.py    From agents-aea with Apache License 2.0 4 votes vote down vote up
def _get_hash(
    tx_sender_addr: Address,
    tx_counterparty_addr: Address,
    good_ids: List[int],
    sender_supplied_quantities: List[int],
    counterparty_supplied_quantities: List[int],
    tx_amount: int,
    tx_nonce: int,
) -> bytes:
    """
    Generate a hash from transaction information.

    :param tx_sender_addr: the sender address
    :param tx_counterparty_addr: the counterparty address
    :param good_ids: the list of good ids
    :param sender_supplied_quantities: the quantities supplied by the sender (must all be positive)
    :param counterparty_supplied_quantities: the quantities supplied by the counterparty (must all be positive)
    :param tx_amount: the amount of the transaction
    :param tx_nonce: the nonce of the transaction
    :return: the hash
    """
    aggregate_hash = Web3.keccak(
        b"".join(
            [
                good_ids[0].to_bytes(32, "big"),
                sender_supplied_quantities[0].to_bytes(32, "big"),
                counterparty_supplied_quantities[0].to_bytes(32, "big"),
            ]
        )
    )
    for idx, good_id in enumerate(good_ids):
        if not idx == 0:
            aggregate_hash = Web3.keccak(
                b"".join(
                    [
                        aggregate_hash,
                        good_id.to_bytes(32, "big"),
                        sender_supplied_quantities[idx].to_bytes(32, "big"),
                        counterparty_supplied_quantities[idx].to_bytes(32, "big"),
                    ]
                )
            )

    m_list = []  # type: List[bytes]
    m_list.append(tx_sender_addr.encode("utf-8"))
    m_list.append(tx_counterparty_addr.encode("utf-8"))
    m_list.append(aggregate_hash)
    m_list.append(tx_amount.to_bytes(32, "big"))
    m_list.append(tx_nonce.to_bytes(32, "big"))
    return Web3.keccak(b"".join(m_list))