"""Reconstruct the ancillary proof inputs in a new, empty directory.""" import argparse import gzip import hashlib import json from pathlib import Path, PurePosixPath def check_digest(data, expected, label): if hashlib.sha256(data).hexdigest() != expected: raise SystemExit(f"SHA-256 mismatch: {label}") def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("directory", type=Path) args = parser.parse_args() root = args.directory.resolve() root.mkdir(parents=True, exist_ok=True) if any(root.iterdir()): parser.error("the output directory must be empty") source = Path(__file__).resolve().parent index = json.loads((source / "proof-index.json").read_bytes()) count = 0 for shard in index["shards"]: name = shard["file"] if Path(name).name != name or not name.startswith("proof-inputs-") or not name.endswith(".json"): raise SystemExit("Invalid shard name.") encoded = (source / name).read_bytes() check_digest(encoded, shard["sha256"], name) for item in json.loads(encoded): name = item["path"] relative = PurePosixPath(name) if relative.is_absolute() or ".." in relative.parts or "\\" in name or ":" in name: raise SystemExit(f"Invalid proof input path: {name}") target = root.joinpath(*relative.parts) if not target.resolve().is_relative_to(root) or target.exists(): raise SystemExit(f"Invalid or duplicate proof input path: {name}") target.parent.mkdir(parents=True, exist_ok=True) data = item["text"].encode("utf-8") check_digest(data, item["sha256"], name) target.write_bytes(gzip.compress(data, mtime=0) if item["gzip"] else data) count += 1 if count != index["entries"]: raise SystemExit("Incomplete proof input set.") print(f"Verified contents of {count} proof inputs.") print(index["command"]) if __name__ == "__main__": main()