mercurymesh-privacy-preserv.../mercurymesh/provenance.py

31 lines
1.1 KiB
Python

from __future__ import annotations
import hashlib
from datetime import datetime
from mercurymesh.contracts import MarketSignal
def _hash_string(s: str) -> str:
return hashlib.sha256(s.encode("utf-8")).hexdigest()
def merkle_proof_for_signal(signal: MarketSignal) -> str:
"""Generate a lightweight, deterministic Merkle-like proof for a signal.
This is a simple stand-in for a real Merkle proof suitable for MVP testing.
It hashes the concatenation of venue_id, symbol, timestamp, and a sorted
representation of features.
"""
features_items = sorted(signal.features.items())
features_str = ",".join([f"{k}={v}" for k, v in features_items])
base = f"{signal.venue_id}|{signal.symbol}|{signal.timestamp.isoformat()}|{features_str}"
return _hash_string(base)
def verify_provenance(aggregated_proof: str, signals: List[MarketSignal]) -> bool:
"""Very lightweight verification: recompute proof from the first signal and compare."""
if not signals:
return False
expected = merkle_proof_for_signal(signals[0])
return expected == aggregated_proof