Created
August 26, 2023 06:32
-
-
Save thurendous/8abfa3d294642fe9f97b4a58a249fb87 to your computer and use it in GitHub Desktop.
This is a script to check if there are function selector's collision.
This file contains hidden or 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 web3 import Web3 | |
import os | |
import hashlib | |
# Function to extract signatures | |
def extract_function_signatures(src): | |
lines = src.split("\n") | |
function_lines = [ | |
line.strip() for line in lines if line.strip().startswith("function") | |
] | |
functions = [ | |
line.split("(")[0].replace("function", "").strip() for line in function_lines | |
] | |
return functions | |
# Compute function selectors | |
def compute_selectors(functions): | |
return {func: Web3.keccak(primitive=func.encode()).hex()[:10] for func in functions} | |
# Load contract sources | |
with open("src/Distributor.sol", "r") as file: | |
distributor_src = file.read() | |
with open("src/ProxyFactory.sol", "r") as file: | |
proxy_factory_src = file.read() | |
# Extract function signatures | |
distributor_functions = extract_function_signatures(distributor_src) | |
proxy_factory_functions = extract_function_signatures(proxy_factory_src) | |
# Compute the function selectors | |
distributor_selectors = compute_selectors(distributor_functions) | |
proxy_factory_selectors = compute_selectors(proxy_factory_functions) | |
# Check for collisions | |
collisions = [] | |
for func, selector in distributor_selectors.items(): | |
for pfunc, pselector in proxy_factory_selectors.items(): | |
if selector == pselector and func != pfunc: | |
collisions.append((func, pfunc, selector)) | |
print(collisions) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Credits to @johnlaw8854 on discord.