Federation-Node / c_fed_id.py
Kode-Animator's picture
Deploy Federation Node: A2A identity and health server
885b32c
Raw
History Blame Contribute Delete
4.2 kB
"""
C-FED-ID Protocol Library (v0.1.0)
===================================
Glyph-Seal minting engine for SERAPHINA Federation nodes.
Standalone copy for Hugging Face deployment.
Canonical source: Infrastructure/tools/glyph-forge/c_fed_id.py
Contract: C-FED-GLYPH-001
Authority: Pantheon LadderWorks
"""
from __future__ import annotations
import base64
import hashlib
import os
import re
import time
from dataclasses import dataclass
from typing import Optional
# ─── Class Glyphs ───
CLASS_GLYPH: dict[str, str] = {
"NODE": "🜁",
"LAW": "πŸ›‘",
"LINK": "πŸŒ€",
"RITE": "πŸ”₯",
"ART": "πŸ•ΈοΈ",
"WIT": "πŸ“œ",
}
VALID_CLASSES = frozenset(CLASS_GLYPH.keys())
VALID_STATES = frozenset({
"VALID", "INVALID",
"ACTIVE", "DORMANT",
"OPEN", "SEALED",
"REFUSED", "READY",
"ATTESTED", "LISTENING",
"REVOKED",
})
# ─── Shard Encoding ───
def _b32_shard(raw: bytes, groups: tuple[int, ...] = (4, 4, 4)) -> str:
s = base64.b32encode(raw).decode("ascii").rstrip("=")
s = re.sub(r"[^A-Z2-7]", "", s)
out: list[str] = []
i = 0
for g in groups:
chunk = s[i:i + g]
if chunk:
out.append(chunk)
i += g
if i >= len(s):
break
return "-".join(out)
# ─── Anchor Generators ───
def _anchor_random(glyph: str, nbytes: int = 10) -> str:
shard = _b32_shard(os.urandom(nbytes))
return f"{glyph}-{shard}"
def _anchor_deterministic(glyph: str, material: str, nbytes: int = 6) -> str:
h = hashlib.blake2b(material.encode("utf-8"), digest_size=nbytes).digest()
shard = _b32_shard(h, groups=(4, 4))
return f"{glyph}-{shard}"
def _anchor_hybrid(glyph: str, nbytes: int = 5) -> str:
ts = time.strftime("%Y%m%d", time.localtime())
rand = _b32_shard(os.urandom(nbytes), groups=(4, 4))
return f"{glyph}-{ts}-{rand}"
def _anchor_from_key(glyph: str, public_key_bytes: bytes) -> str:
fingerprint = hashlib.blake2b(public_key_bytes, digest_size=10).digest()
shard = _b32_shard(fingerprint)
return f"{glyph}-{shard}"
# ─── Seal Model ───
@dataclass(frozen=True)
class GlyphSeal:
class_name: str
origin: str
breath_anchor: str
state: str
witness: Optional[str] = None
def __str__(self) -> str:
return f"⟦ {self.class_name} :: {self.origin} :: {self.breath_anchor} :: {self.state} ⟧"
def to_dict(self) -> dict:
return {
"class": self.class_name,
"origin": self.origin,
"breath_anchor": self.breath_anchor,
"state": self.state,
"seal": str(self),
}
# ─── Public API ───
def mint_seal(
class_name: str,
origin: str,
state: str = "VALID",
mode: str = "hybrid",
material: Optional[str] = None,
) -> GlyphSeal:
cn = class_name.upper()
st = state.upper()
if cn not in VALID_CLASSES:
raise ValueError(f"Invalid class '{cn}'")
if st not in VALID_STATES:
raise ValueError(f"Invalid state '{st}'")
glyph = CLASS_GLYPH[cn]
if mode == "deterministic":
if material is None:
raise ValueError("'material' required for deterministic mode")
anchor = _anchor_deterministic(glyph, material)
elif mode == "hybrid":
anchor = _anchor_hybrid(glyph)
elif mode == "random":
anchor = _anchor_random(glyph)
else:
raise ValueError(f"Invalid mode '{mode}'")
return GlyphSeal(class_name=cn, origin=origin.upper(), breath_anchor=anchor, state=st)
# ─── Validation ───
_SEAL_PATTERN = re.compile(
r"⟦\s*(?P<class>[A-Z]+)\s*::\s*(?P<origin>[A-Z0-9_\-]+)\s*::\s*"
r"(?P<anchor>.+?)\s*::\s*(?P<state>[A-Z]+)\s*⟧"
)
def verify_seal_syntax(seal_str: str) -> Optional[dict]:
m = _SEAL_PATTERN.search(seal_str)
if not m:
return None
return {
"class": m.group("class"),
"origin": m.group("origin"),
"breath_anchor": m.group("anchor").strip(),
"state": m.group("state"),
"valid_class": m.group("class") in VALID_CLASSES,
"valid_state": m.group("state") in VALID_STATES,
}