Skip to content

Instantly share code, notes, and snippets.

@pcaversaccio
Last active December 23, 2022 05:45
Show Gist options
  • Save pcaversaccio/49c2ddf04dc438b04b5817b1b1ff9d84 to your computer and use it in GitHub Desktop.
Save pcaversaccio/49c2ddf04dc438b04b5817b1b1ff9d84 to your computer and use it in GitHub Desktop.
Solidity and 🐍Vyper hashing versions for building an address-based Merkle tree.
/**
* @dev Solidity version for standard pack mode.
*/
function hashAddr(address addr) external pure returns (bytes32) {
/**
* @dev The function `keccak256` hashes `0x000000000000000000000000+addr`.
* Remember that one EVM word is 32 bytes and Ethereum addresses are 20 bytes long.
*/
return keccak256(abi.encode(addr));
}
/**
* @dev Vyper version for standard pack mode.
*/
@external
@pure
def hash_addr(addr: address) -> bytes32:
/**
* @dev The function `keccak256` hashes `0x000000000000000000000000+addr`.
* Remember that one EVM word is 32 bytes and Ethereum addresses are 20 bytes long.
*/
return keccak256(_abi_encode(addr))
----------------
/**
* @dev Solidity version for non-standard pack mode.
*/
function hashAddr(address addr) external pure returns (bytes32) {
/**
* @dev The function `keccak256` hashes `addr`.
*/
return keccak256(abi.encodePacked(addr));
}
/**
* @dev Vyper version for non-standard pack mode.
*/
@external
@pure
def hash_addr(addr: address) -> bytes32:
/**
* @dev The function `convert(addr, bytes32)` leads to `0x000000000000000000000000+addr`.
* If you want to return `Bytes` instead, you could also use `_abi_encode`.
* @notice The function `slice` copies a list of bytes and returns a specified slice.
*/
return keccak256(slice(convert(addr, bytes32), 12, 20))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment