idea19-missionledger-verifi.../missionledger/governance.py

39 lines
1.1 KiB
Python

from __future__ import annotations
import hashlib
import json
from datetime import datetime
from typing import List
class GovernanceLedger:
"""A simple tamper-evident governance ledger with per-entry hashes.
Logs are append-only and anchored via a rolling hash to the previous entry,
producing a chain of custody suitable for auditing even on intermittent
connectivity.
"""
def __init__(self, device_id: str = "unknown"):
self.device_id = device_id
self._entries: List[dict] = []
self._head_hash = ""
def log(self, entry: dict) -> None:
timestamp = datetime.utcnow().isoformat() + "Z"
payload = {
"device_id": self.device_id,
"timestamp": timestamp,
"entry": entry,
"parent": self._head_hash,
}
self._head_hash = hashlib.sha256(json.dumps(payload, sort_keys=True).encode()).hexdigest()
self._entries.append(payload)
@property
def entries(self) -> List[dict]:
return self._entries[:]
def latest_hash(self) -> str:
return self._head_hash