import uuid import hashlib import json import os class IdentityManager: def __init__(self): self.identity_file = "agent_identity.json" self.agent_id = None self.public_key = None self._load_or_create_identity() def _load_or_create_identity(self): if os.path.exists(self.identity_file): with open(self.identity_file, "r") as f: data = json.load(f) self.agent_id = data["agent_id"] self.public_key = data["public_key"] else: self.agent_id = str(uuid.uuid4()) self.public_key = hashlib.sha256(self.agent_id.encode()).hexdigest() with open(self.identity_file, "w") as f: json.dump({ "agent_id": self.agent_id, "public_key": self.public_key }, f, indent=2) def verify(self, other_id): return other_id == self.public_key