-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify_chain.py
More file actions
executable file
·59 lines (46 loc) · 1.85 KB
/
Copy pathverify_chain.py
File metadata and controls
executable file
·59 lines (46 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python3
import json
import hashlib
import sys
from pathlib import Path
HISTORY_PATH = Path("docs/proof/history.json")
def stable_hash(record):
data = {
"proof_id": record["proof_id"],
"timestamp": record["timestamp"],
"previous_hash": record["previous_hash"],
}
raw = json.dumps(data, ensure_ascii=False, sort_keys=True, separators=(",", ":"))
return hashlib.sha256(raw.encode("utf-8")).hexdigest()
def main():
if not HISTORY_PATH.exists():
print("[ERROR] history.json not found")
sys.exit(1)
history = json.loads(HISTORY_PATH.read_text(encoding="utf-8"))
if not history:
print("[ERROR] history is empty")
sys.exit(1)
expected_previous = "GENESIS"
for index, record in enumerate(history, start=1):
required = ["proof_id", "timestamp", "previous_hash", "current_hash"]
for key in required:
if key not in record:
print(f"[ERROR] missing key '{key}' at record #{index}")
sys.exit(1)
if record["previous_hash"] != expected_previous:
print(f"[ERROR] chain broken at record #{index}")
print(f" expected previous_hash: {expected_previous}")
print(f" actual previous_hash : {record['previous_hash']}")
sys.exit(1)
recalculated = stable_hash(record)
if record["current_hash"] != recalculated:
print(f"[ERROR] hash mismatch at record #{index}")
print(f" expected current_hash: {recalculated}")
print(f" actual current_hash : {record['current_hash']}")
sys.exit(1)
expected_previous = record["current_hash"]
print("[OK] Proof history chain is valid")
print(f"[OK] records verified: {len(history)}")
print(f"[OK] latest hash: {expected_previous}")
if __name__ == "__main__":
main()