|
| 1 | +import ast |
| 2 | +import asyncio |
| 3 | +import sys |
| 4 | +from enum import Enum |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +from starknet_py.net.full_node_client import FullNodeClient |
| 8 | + |
| 9 | +import garaga.hints.io as io |
| 10 | +from garaga.starknet.cli.smart_contract_project import SmartContractProject |
| 11 | + |
| 12 | + |
| 13 | +class Network(Enum): |
| 14 | + SEPOLIA = "sepolia" |
| 15 | + MAINNET = "mainnet" |
| 16 | + |
| 17 | + |
| 18 | +def get_class_hash_from_generator(): |
| 19 | + try: |
| 20 | + with open( |
| 21 | + "hydra/garaga/starknet/groth16_contract_generator/generator.py", "r" |
| 22 | + ) as f: |
| 23 | + tree = ast.parse(f.read()) |
| 24 | + for node in ast.walk(tree): |
| 25 | + if isinstance(node, ast.Assign) and len(node.targets) == 1: |
| 26 | + if getattr(node.targets[0], "id", None) == "ECIP_OPS_CLASS_HASH": |
| 27 | + return hex(node.value.value) |
| 28 | + raise ValueError("ECIP_OPS_CLASS_HASH not found in generator.py") |
| 29 | + except Exception as e: |
| 30 | + print(f"Error parsing generator.py: {str(e)}", file=sys.stderr) |
| 31 | + sys.exit(1) |
| 32 | + |
| 33 | + |
| 34 | +async def verify_network(network: Network, class_hash: str): |
| 35 | + class_hash = io.to_hex_str(class_hash) |
| 36 | + print(f"\nVerifying class hash {class_hash} on {network.value}...") |
| 37 | + client = FullNodeClient(f"https://free-rpc.nethermind.io/{network.value}-juno") |
| 38 | + try: |
| 39 | + result = await client.get_class_by_hash(class_hash) |
| 40 | + if not result: |
| 41 | + print(f"Error: Contract not declared on {network.value}", file=sys.stderr) |
| 42 | + sys.exit(1) |
| 43 | + print(f"✓ Contract verified on {network.value}") |
| 44 | + except Exception as e: |
| 45 | + print(f"Error checking {network.value}: {str(e)}", file=sys.stderr) |
| 46 | + sys.exit(1) |
| 47 | + |
| 48 | + |
| 49 | +async def verify_ecip_contract(): |
| 50 | + class_hash = get_class_hash_from_generator() |
| 51 | + print(f"Verifying ECIP contract using class hash: {class_hash}") |
| 52 | + |
| 53 | + await verify_network(Network.SEPOLIA, class_hash) |
| 54 | + await verify_network(Network.MAINNET, class_hash) |
| 55 | + |
| 56 | + print("\n✓ Contract verified on both networks") |
| 57 | + |
| 58 | + |
| 59 | +async def verify_contract_from_path(path: Path): |
| 60 | + contract = SmartContractProject(smart_contract_folder=path) |
| 61 | + class_hash = contract.get_sierra_class_hash() |
| 62 | + print(f"Verifying contract {path} with class hash {io.to_hex_str(class_hash)}") |
| 63 | + await verify_network(Network.SEPOLIA, class_hash) |
| 64 | + await verify_network(Network.MAINNET, class_hash) |
| 65 | + |
| 66 | + |
| 67 | +if __name__ == "__main__": |
| 68 | + asyncio.run(verify_ecip_contract()) |
| 69 | + asyncio.run(verify_contract_from_path(Path("src/contracts/drand_quicknet"))) |
| 70 | + asyncio.run(verify_contract_from_path(Path("src/contracts/risc0_verifier_bn254"))) |
0 commit comments