gridguard-secure-attested-c.../src/gridguard_secure_attested_c.../governance.py

56 lines
2.1 KiB
Python

import hashlib
import time
from typing import Any, Dict, List
class GovernanceLedger:
"""Append-only governance log with simplistic cryptographic signatures."""
def __init__(self) -> None:
self._events: List[Dict[str, Any]] = []
def _sign(self, event: Dict[str, Any]) -> str:
payload = f"{event}{time.time()}".encode()
return hashlib.sha256(payload).hexdigest()
def append_event(self, event: Dict[str, Any]) -> Dict[str, Any]:
entry = dict(event)
entry["signature"] = self._sign(entry)
self._events.append(entry)
return entry
def get_events(self) -> List[Dict[str, Any]]:
return list(self._events)
class DeltaSync:
"""Tiny delta-sync helper that validates a proof and merges states."""
@staticmethod
def reconcile(local_state: Dict[str, Any], remote_state: Dict[str, Any], proof: Dict[str, Any]) -> Dict[str, Any]:
if not proof or not proof.get("valid", False):
# In real systems, you'd raise or handle conflicts; here we prefer local state
return dict(local_state)
merged = dict(local_state)
merged.update(remote_state)
return merged
def anchor_to_public(self, public_anchor_url: str, events: List[Dict[str, Any]]) -> str:
"""Create a simple anchor string for cross-organization auditability.
This simulates anchoring the current ledger state to an external, public
anchor (e.g., a blockchain or public logs). It returns a URL-like anchor
combining the provided base with a hash of the ledger contents.
"""
# Simple hash of all events to serve as a tamper-evident root
ledger_bytes = str(events).encode()
root_hash = hashlib.sha256(ledger_bytes).hexdigest()
return f"{public_anchor_url}#root={root_hash}"
# Lightweight verifier for external proofs (optional enhancement)
@staticmethod
def verify_proof(proof: Dict[str, Any]) -> bool:
# In MVP, a proof is valid if it contains a truthy 'valid' flag set to True
return bool(proof) and bool(proof.get("valid"))