Last active
May 20, 2024 07:44
-
-
Save Aviksaikat/cc69acb525695e44db340d64e9889f5e to your computer and use it in GitHub Desktop.
Correct way to generate keccak256 hash of multiple data types using python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from brownie import web3 | |
# wrong | |
print(web3.solidityKeccak(["uint256", "address", "uint256"],[2, "0x0000000000000000000000000000000000001337", 8948253]).hex()) | |
# >> 0x7c251610f9474d6a5f00096d05e0ae40e433cae67c90e400cf88aad9f24398ef | |
# right | |
from eth_abi import encode | |
encoded_data = encode( | |
["uint256", "address", "uint256"], | |
[2, "0x0000000000000000000000000000000000001337", 8948253], | |
) | |
print(web3.keccak(encoded_data).hex()) | |
# >> 0x028182e30a562fc0fd9cf6fcd44c261f8b2f895d1745ea236e50ff8ee7c9e2d1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment