Spaces:
Sleeping
Sleeping
File size: 4,195 Bytes
885b32c | 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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | """
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,
}
|