Skip to content

Instantly share code, notes, and snippets.

@sychou
Created July 23, 2024 16:12
Show Gist options
  • Save sychou/3b4a41436958802108db24b4013f9f2a to your computer and use it in GitHub Desktop.
Save sychou/3b4a41436958802108db24b4013f9f2a to your computer and use it in GitHub Desktop.
Keccak encode hex value
# Example of how to keccak encode a hex value. Used for converting Snapshot proposal ID to Votium Initiate Deposit _proposal parameter.
#
# Given the Snapshot proposal ID:
# https://snapshot.org/#/cvx.eth/proposal/0xd2f6785ba7e199e3a0169c9bfd561ae6d7c81baa54de4291eef0c355251eb94c
# 0xd2f6785ba7e199e3a0169c9bfd561ae6d7c81baa54de4291eef0c355251eb94c
#
# Should match the following Votium initiation proposal ID:
# https://etherscan.io/tx/0xcb95fedbcbd8889a741e0a12a61536e0bcc46b13b59c997abda8f1e214ede253
# 0xab60d2a73796b1c01607b025b4fa6b83332b0adee021020a42ce56c88d9eebe2
from web3 import Web3
# Define the hexadecimal value to hash (Snapshot proposal ID)
hex_value = 'd2f6785ba7e199e3a0169c9bfd561ae6d7c81baa54de4291eef0c355251eb94c'
# Convert the hexadecimal value to bytes
value_bytes = bytes.fromhex(hex_value)
# Apply Keccak-256 hash function using Web3.solidityKeccak()
hash_hex = Web3.solidityKeccak(['bytes32'], [value_bytes]).hex()
# Print the hash
print(f"0x{hex_value}\nencodes to\n{hash_hex}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment