File size: 1,073 Bytes
df6cf36
 
 
 
d8d73d9
 
 
df6cf36
d8d73d9
 
 
 
 
 
 
 
 
 
 
df6cf36
d8d73d9
df6cf36
d8d73d9
 
 
df6cf36
 
 
d8d73d9
 
df6cf36
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
import json
import os

class VitalisLedger:
    def __init__(self, path="data/ledger.json"):
        self.path = path
        os.makedirs(os.path.dirname(self.path), exist_ok=True)

    def get_last_hash(self):
        try:
            if not os.path.exists(self.path):
                return "0" * 64
            with open(self.path, "r") as f:
                lines = [line.strip() for line in f if line.strip()]
                if not lines:
                    return "0" * 64
                return json.loads(lines[-1])["hash"]
        except (json.JSONDecodeError, KeyError, Exception):
            return "0" * 64

    def write_entry(self, action, payload):
        prev_hash = self.get_last_hash()
        new_hash = str(hash(json.dumps(payload) + prev_hash))
        entry = {"action": action, "payload": payload, "hash": new_hash}
        with open(self.path, "a") as f:
            f.write(json.dumps(entry) + "\n")

    def verify_ledger(self):
        # Placeholder for integrity check logic
        # Returns True if the chain is intact
        return True