feat(v2.0.8): implement ipld dag-json/CIDv1, quant financial collector, MCP server (hs-mcp), /learn/ sub-page, /gallery/ sub-page, and global Lemurs footer
This commit is contained in:
1 parent
b767f8a4e9
commit
c17f74097b
55 files changed
+3974
-10
No files matched your search
@@ -0,0 +1,90 @@
|
||||
"""
|
||||
hyperstitionstud_io.ipld — Canonical IPLD dag-json (multicodec 0x0129) Encoder, CIDv1 Generator
|
||||
& Zero-Drift Pretty-Print / dag-json Atomic Writer.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
# CIDv1 (0x01) + dag-json multicodec (0x0129 -> varint 0xa9 0x02) + sha2-256 (0x12) + 32-byte length (0x20)
|
||||
_CIDV1_DAG_JSON_SHA256_PREFIX = bytes([0x01, 0xA9, 0x02, 0x12, 0x20])
|
||||
|
||||
|
||||
def encode_dag_json(obj: Any) -> bytes:
|
||||
"""Serialize a Python data structure to canonical IPLD dag-json UTF-8 bytes."""
|
||||
return json.dumps(
|
||||
obj,
|
||||
sort_keys=True,
|
||||
separators=(",", ":"),
|
||||
ensure_ascii=False,
|
||||
allow_nan=False,
|
||||
).encode("utf-8")
|
||||
|
||||
|
||||
def compute_dag_json_sha256(obj: Any) -> str:
|
||||
"""Return the lowercase hex SHA-256 digest of the canonical dag-json byte representation."""
|
||||
return hashlib.sha256(encode_dag_json(obj)).hexdigest()
|
||||
|
||||
|
||||
def compute_dag_json_cidv1(obj: Any) -> str:
|
||||
"""Compute the multibase Base32lower CIDv1 ('baguqeera...') for an object encoded as dag-json (0x0129)."""
|
||||
raw_bytes = encode_dag_json(obj)
|
||||
digest = hashlib.sha256(raw_bytes).digest()
|
||||
cid_bytes = _CIDV1_DAG_JSON_SHA256_PREFIX + digest
|
||||
b32 = base64.b32encode(cid_bytes).decode("ascii").lower().rstrip("=")
|
||||
return "b" + b32
|
||||
|
||||
|
||||
def make_ipld_link(cid_str: str) -> dict[str, str]:
|
||||
"""Return an IPLD dag-json Link object {'/': '<cid>'}."""
|
||||
return {"/": cid_str}
|
||||
|
||||
|
||||
def inspect_dag_json_payload(obj: Any) -> dict[str, Any]:
|
||||
raw = encode_dag_json(obj)
|
||||
sha = hashlib.sha256(raw).hexdigest()
|
||||
cid = compute_dag_json_cidv1(obj)
|
||||
return {
|
||||
"codec": "dag-json",
|
||||
"multicodec_hex": "0x0129",
|
||||
"canonical_bytes": len(raw),
|
||||
"sha256": sha,
|
||||
"cidv1": cid,
|
||||
"ipld_link": {"/": cid},
|
||||
}
|
||||
|
||||
|
||||
def write_pretty_and_dag_json(
|
||||
pretty_path: str | Path,
|
||||
obj: Any,
|
||||
dag_sidecar_path: str | Path | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""
|
||||
Atomically write both a human-readable pretty-printed JSON file (indent=2) and (optionally)
|
||||
its canonical IPLD dag-json sidecar from the same validated canonical representation,
|
||||
guaranteeing zero drift between the two files.
|
||||
"""
|
||||
p_pretty = Path(pretty_path).resolve()
|
||||
p_pretty.parent.mkdir(parents=True, exist_ok=True)
|
||||
dag_bytes = encode_dag_json(obj)
|
||||
canonical_obj = json.loads(dag_bytes.decode("utf-8"))
|
||||
pretty_bytes = (json.dumps(canonical_obj, indent=2, ensure_ascii=False) + "\n").encode("utf-8")
|
||||
|
||||
tmp_pretty = p_pretty.with_suffix(p_pretty.suffix + f".tmp.{os.getpid()}")
|
||||
tmp_pretty.write_bytes(pretty_bytes)
|
||||
os.replace(tmp_pretty, p_pretty)
|
||||
|
||||
if dag_sidecar_path is not None:
|
||||
p_dag = Path(dag_sidecar_path).resolve()
|
||||
p_dag.parent.mkdir(parents=True, exist_ok=True)
|
||||
tmp_dag = p_dag.with_suffix(p_dag.suffix + f".tmp.{os.getpid()}")
|
||||
tmp_dag.write_bytes(dag_bytes + b"\n")
|
||||
os.replace(tmp_dag, p_dag)
|
||||
|
||||
return inspect_dag_json_payload(canonical_obj)
|
||||
Reference in new issue
Block a user