Skip to content

Instantly share code, notes, and snippets.

@jmcph4
Last active February 26, 2025 04:35
Show Gist options
  • Save jmcph4/28d266f9db0e91a002d8e0d637e37938 to your computer and use it in GitHub Desktop.
Save jmcph4/28d266f9db0e91a002d8e0d637e37938 to your computer and use it in GitHub Desktop.
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