Last active
February 26, 2025 04:35
-
-
Save jmcph4/28d266f9db0e91a002d8e0d637e37938 to your computer and use it in GitHub Desktop.
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
import re | |
import cbor2 | |
import argparse | |
from base58 import b58encode | |
def extract_ipfs_hash(bytecode: str) -> str: | |
""" | |
Extracts the IPFS hash from the CBOR-encoded metadata in Solidity bytecode. | |
""" | |
# Look for the CBOR metadata marker (0xa264...) at the end of the bytecode | |
match = re.search(r'a264.*$', bytecode) | |
if not match: | |
return None | |
cbor_data = bytes.fromhex(match.group(0)) | |
try: | |
decoded = cbor2.loads(cbor_data) | |
ipfs_multihash = decoded.get('ipfs') | |
if ipfs_multihash: | |
# Convert to Base58 (CIDv0 format) | |
return b58encode(ipfs_multihash).decode() | |
except Exception as e: | |
print(f"Error decoding CBOR: {e}") | |
return None | |
def main(): | |
parser = argparse.ArgumentParser(description="Extract IPFS hash from Solidity bytecode.") | |
parser.add_argument("bytecode", type=str, help="Hex string of the Solidity contract bytecode") | |
args = parser.parse_args() | |
ipfs_hash = extract_ipfs_hash(args.bytecode) | |
print("IPFS Hash:", ipfs_hash if ipfs_hash else "Not found") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment