#!/usr/bin/env python3 """Fetch five MIT-marked OpenZeppelin Contracts v5.7.0 reference files.""" from __future__ import annotations import hashlib import json from concurrent.futures import ThreadPoolExecutor, as_completed from pathlib import Path import requests REPO = "OpenZeppelin/openzeppelin-contracts" VERSION = "v5.7.0" RAW = f"https://raw.githubusercontent.com/{REPO}/{VERSION}/" FILES = [ ("contracts/token/ERC20/ERC20.sol", "ERC20: fungible-token accounting and transfer/allowance interface"), ("contracts/access/Ownable.sol", "Ownable: single-owner access-control pattern"), ("contracts/access/AccessControl.sol", "AccessControl: role-based authorization primitives"), ("contracts/utils/ReentrancyGuard.sol", "ReentrancyGuard: explicit nonReentrant execution guard"), ("contracts/utils/Pausable.sol", "Pausable: emergency pause-state primitives; authorization must be supplied by integrating code"), ] INSTRUCTION = "Gunakan berkas Solidity rujukan berikut untuk menjelaskan fungsi pola atau komponen yang disediakan. Bedakan kemampuan library dari jaminan keamanan seluruh sistem; jangan menganggap cuplikan ini sebagai audit atau aplikasi lengkap." SELECTION = "Five specific Solidity files with SPDX-License-Identifier: MIT from the tagged OpenZeppelin Contracts v5.7.0 release." def fetch(path: str) -> bytes: response = requests.get(RAW + path, timeout=45) response.raise_for_status() return response.content def sha256(data: bytes) -> str: return hashlib.sha256(data).hexdigest() def main() -> None: paths = [path for path, _ in FILES] + ["LICENSE"] contents = {} with ThreadPoolExecutor(max_workers=6) as pool: futures = {pool.submit(fetch, path): path for path in paths} for future in as_completed(futures): contents[futures[future]] = future.result() license_text = contents["LICENSE"].decode("utf-8", "replace") if "Permission is hereby granted, free of charge" not in license_text: raise RuntimeError("Pinned OpenZeppelin LICENSE did not match the MIT grant text") Path("OPENZEPPELIN-MIT.txt").write_bytes(contents["LICENSE"]) records = [] for relpath, summary in FILES: raw = contents[relpath] code = raw.decode("utf-8") if not code.startswith("// SPDX-License-Identifier: MIT"): raise RuntimeError(f"Missing MIT SPDX header: {relpath}") records.append({ "id": "openzeppelin-" + relpath.rsplit("/", 1)[-1].removesuffix(".sol").lower(), "task_type": "secure_code_reference", "domain": "smart_contract", "language": "Solidity", "instruction": INSTRUCTION, "input": code, "output": summary, "source_file": relpath, "source_version": VERSION, "source_url": f"https://github.com/{REPO}/blob/{VERSION}/{relpath}", "source_license": "MIT (SPDX identifier present in the source file; repository LICENSE included)", "source_sha256": sha256(raw), "selection_policy": SELECTION, "split": "reference", }) output = Path("staging/openzeppelin_solidity_references.jsonl") output.parent.mkdir(parents=True, exist_ok=True) with output.open("w", encoding="utf-8", newline="\n") as handle: for row in sorted(records, key=lambda item: item["id"]): handle.write(json.dumps(row, ensure_ascii=False, sort_keys=True) + "\n") print(json.dumps({ "source": REPO, "version": VERSION, "records": len(records), "output": str(output), "license": "OPENZEPPELIN-MIT.txt", }, ensure_ascii=False, indent=2)) if __name__ == "__main__": main()