|
3 | 3 | import defusedxml.ElementTree as ET |
4 | 4 | from pathlib import Path |
5 | 5 | import sys |
6 | | -from typing import Union |
7 | 6 | import hashlib |
8 | 7 | import logging |
9 | 8 | import json |
10 | 9 | import platform |
11 | | -from typing import Union, Optional |
| 10 | +from typing import Union, Optional, Dict, Any, List, Tuple |
12 | 11 |
|
13 | 12 | logger = logging.getLogger(__name__) |
14 | 13 | MERKLE_CHUNK_SIZE_BYTES = 1024 * 1024 # 1MB |
@@ -227,6 +226,76 @@ def generate_manifest(raw_path, processed_path): |
227 | 226 |
|
228 | 227 | logger.info("Manifest written to %s", manifest_path) |
229 | 228 |
|
| 229 | +def export_merkle_proof( |
| 230 | + proof: List[Tuple[str, bool]], |
| 231 | + chunk_index: int, |
| 232 | + chunk_size: int, |
| 233 | + output_path: Union[str, Path] |
| 234 | +) -> None: |
| 235 | + """ |
| 236 | + Export Merkle proof to a JSON file for portable verification. |
| 237 | + """ |
| 238 | + |
| 239 | + if chunk_size <= 0: |
| 240 | + raise ValueError("chunk_size must be a positive integer") |
| 241 | + |
| 242 | + if not isinstance(proof, list): |
| 243 | + raise ValueError("proof must be a list") |
| 244 | + |
| 245 | + if chunk_index < 0: |
| 246 | + raise ValueError("chunk_index must be non-negative") |
| 247 | + |
| 248 | + data = { |
| 249 | + "chunk_index": chunk_index, |
| 250 | + "chunk_size": chunk_size, |
| 251 | + "proof": proof, |
| 252 | + } |
| 253 | + |
| 254 | + output_path = Path(output_path) |
| 255 | + with output_path.open("w", encoding="utf-8") as f: |
| 256 | + json.dump(data, f, indent=2) |
| 257 | + |
| 258 | +def load_merkle_proof( |
| 259 | + proof_path: Union[str, Path] |
| 260 | +) -> Dict[str, Any]: |
| 261 | + """ |
| 262 | + Load Merkle proof from a JSON file. |
| 263 | + """ |
| 264 | + proof_path = Path(proof_path) |
| 265 | + |
| 266 | + with proof_path.open("r", encoding="utf-8") as f: |
| 267 | + return json.load(f) |
| 268 | + |
| 269 | + |
| 270 | +# Content before line 270 remains unchanged |
| 271 | +# Entire function definition from lines 270-314 should be deleted |
| 272 | +def verify_merkle_proof_from_file( |
| 273 | + proof_file_path: Union[str, Path], |
| 274 | + chunk_data: bytes, |
| 275 | + expected_root: str |
| 276 | +) -> bool: |
| 277 | + proof_file_path = Path(proof_file_path) |
| 278 | + |
| 279 | + if not proof_file_path.exists(): |
| 280 | + raise FileNotFoundError(f"Proof file not found: {proof_file_path}") |
| 281 | + |
| 282 | + with proof_file_path.open("r", encoding="utf-8") as f: |
| 283 | + data = json.load(f) |
| 284 | + |
| 285 | + if not isinstance(data, dict): |
| 286 | + raise ValueError("Malformed proof file: expected JSON object") |
| 287 | + |
| 288 | + required_keys = {"chunk_index", "chunk_size", "proof"} |
| 289 | + if not required_keys.issubset(data.keys()): |
| 290 | + raise ValueError("Malformed proof file: missing required keys") |
| 291 | + |
| 292 | + proof = data["proof"] |
| 293 | + |
| 294 | + if not isinstance(proof, list): |
| 295 | + raise ValueError("Malformed proof: proof must be a list") |
| 296 | + |
| 297 | + return verify_merkle_proof(chunk_data, proof, expected_root) |
| 298 | + |
230 | 299 | # helpers:Update compute_sha256() to support bytes input directly. |
231 | 300 | def compute_sha256( |
232 | 301 | *, |
|
0 commit comments