本文整理匯總了Python中web3.Web3.keccak方法的典型用法代碼示例。如果您正苦於以下問題:Python Web3.keccak方法的具體用法?Python Web3.keccak怎麽用?Python Web3.keccak使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類web3.Web3
的用法示例。
在下文中一共展示了Web3.keccak方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: get_address_from_public_key
# 需要導入模塊: from web3 import Web3 [as 別名]
# 或者: from web3.Web3 import keccak [as 別名]
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
示例2: keccak
# 需要導入模塊: from web3 import Web3 [as 別名]
# 或者: from web3.Web3 import keccak [as 別名]
def keccak():
return Web3.keccak
示例3: generate_tx_nonce
# 需要導入模塊: from web3 import Web3 [as 別名]
# 或者: from web3.Web3 import keccak [as 別名]
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()
示例4: test_keccak_text
# 需要導入模塊: from web3 import Web3 [as 別名]
# 或者: from web3.Web3 import keccak [as 別名]
def test_keccak_text(message, digest):
assert Web3.keccak(text=message) == digest
示例5: test_keccak_hexstr
# 需要導入模塊: from web3 import Web3 [as 別名]
# 或者: from web3.Web3 import keccak [as 別名]
def test_keccak_hexstr(hexstr, digest):
assert Web3.keccak(hexstr=hexstr) == digest
示例6: test_keccak_primitive_invalid
# 需要導入模塊: from web3 import Web3 [as 別名]
# 或者: from web3.Web3 import keccak [as 別名]
def test_keccak_primitive_invalid(primitive, exception):
with pytest.raises(exception):
Web3.keccak(primitive)
示例7: test_keccak_primitive
# 需要導入模塊: from web3 import Web3 [as 別名]
# 或者: from web3.Web3 import keccak [as 別名]
def test_keccak_primitive(primitive, digest):
assert Web3.keccak(primitive) == digest
示例8: test_keccak_raise_if_primitive_and
# 需要導入模塊: from web3 import Web3 [as 別名]
# 或者: from web3.Web3 import keccak [as 別名]
def test_keccak_raise_if_primitive_and(kwargs):
# must not set more than one input
with pytest.raises(TypeError):
Web3.keccak('', **kwargs)
示例9: test_keccak_raise_if_no_args
# 需要導入模塊: from web3 import Web3 [as 別名]
# 或者: from web3.Web3 import keccak [as 別名]
def test_keccak_raise_if_no_args():
with pytest.raises(TypeError):
Web3.keccak()
示例10: test_deprecated_bound_method
# 需要導入模塊: from web3 import Web3 [as 別名]
# 或者: from web3.Web3 import keccak [as 別名]
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
示例11: eth_sign_hash_message
# 需要導入模塊: from web3 import Web3 [as 別名]
# 或者: from web3.Web3 import keccak [as 別名]
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
)
示例12: test_updateReward
# 需要導入模塊: from web3 import Web3 [as 別名]
# 或者: from web3.Web3 import keccak [as 別名]
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
示例13: _get_hash
# 需要導入模塊: from web3 import Web3 [as 別名]
# 或者: from web3.Web3 import keccak [as 別名]
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))