Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## [Unreleased]

- OTS anchoring: optional publishable manifest (format 2). `stamp()` / `build_manifest()`
accept `id_map_path`; the manifest then carries no paths and names each ledger only by a
salted id `sha256(salt || basename)`, with a per-ledger random salt kept in a private map
outside the output directory. Default output (format 1) is unchanged.
- New `locate_ledger()` finds the manifest row pinning a (possibly later, appended) ledger
file in either format, and checks a disclosed name + salt against a format-2 id.

## [0.3.0] — 2026-09-10

- Independent `mirror-stack` setup, record, verify, doctor, connect and serve commands.
Expand Down
104 changes: 96 additions & 8 deletions mirror_stack_mcp/ots_anchor.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,68 @@ def _head_seal(p) -> str | None:
return head


def build_manifest(ledger_paths: list[str], out_dir: str) -> tuple[str, str]:
"""Write a manifest pinning each ledger's sha256 + head seal. Returns (path, sha256)."""
REDACTED_SCHEME = "sha256(salt || utf8(ledger basename))"


def ledger_id(name: str, salt_hex: str) -> str:
"""Salted commitment to a ledger's file name, as written in a redacted manifest.

Disclosing (name, salt) for ONE ledger lets anyone recompute its id; the other
ledgers' names stay hidden because each ledger has its own random salt."""
return hashlib.sha256(bytes.fromhex(salt_hex) + name.encode("utf-8")).hexdigest()


def _load_salts(id_map_path: str, names: list[str]) -> dict:
"""Load the private name -> salt map, adding a fresh random salt for new ledgers.

Salts are stable across runs so the same ledger keeps the same id every day. The
map is the only link between ids and names: keep it private, never next to the
published manifests."""
p = Path(id_map_path)
data = json.loads(p.read_text(encoding="utf-8")) if p.exists() else {}
salts = dict(data.get("salts", {}))
added = [n for n in names if n not in salts]
for n in added:
salts[n] = os.urandom(16).hex()
if added or not p.exists():
p.parent.mkdir(parents=True, exist_ok=True)
tmp = p.with_name(p.name + f".tmp.{os.getpid()}")
fd = os.open(tmp, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
with os.fdopen(fd, "w", encoding="utf-8") as stream:
stream.write(json.dumps({"_type": "ots_ledger_id_map", "scheme": REDACTED_SCHEME,
"salts": salts}, ensure_ascii=False, indent=2))
stream.flush()
os.fsync(stream.fileno())
os.replace(tmp, p)
return salts


def build_manifest(ledger_paths: list[str], out_dir: str, *,
id_map_path: str | None = None) -> tuple[str, str]:
"""Write a manifest pinning each ledger's sha256 + head seal. Returns (path, sha256).

Default (format 1) records each ledger's basename and absolute path. With
``id_map_path`` the manifest is publishable as-is (format 2): no paths, and each
ledger is named only by a salted id (see ``ledger_id``); the private name -> salt
map is kept at ``id_map_path``, which must lie outside ``out_dir``."""
os.makedirs(out_dir, exist_ok=True)
rows = []
if id_map_path is not None:
out_real = os.path.realpath(out_dir)
if os.path.commonpath([out_real, os.path.realpath(id_map_path)]) == out_real:
raise ValueError("id_map_path must not be inside out_dir (it would be published)")
names = [os.path.basename(f) for f in ledger_paths]
if len(set(names)) != len(names):
raise ValueError("duplicate ledger basenames cannot be redacted unambiguously")
salts = _load_salts(id_map_path, names)
for f in ledger_paths:
rows.append({"ledger": os.path.basename(f), "path": str(f),
"bytes": Path(f).stat().st_size, "sha256": _sha256(f),
"head_seal": _head_seal(f)})
common = {"bytes": Path(f).stat().st_size, "sha256": _sha256(f),
"head_seal": _head_seal(f)}
if id_map_path is None:
rows.append({"ledger": os.path.basename(f), "path": str(f), **common})
else:
name = os.path.basename(f)
rows.append({"ledger_id": ledger_id(name, salts[name]), **common})
stamp = datetime.datetime.utcnow().strftime("%Y%m%d_%H%M%S")
# Different legitimate operations in the same second must not replace evidence.
man = os.path.join(out_dir, f"manifest_{stamp}_{uuid.uuid4().hex}.json")
Expand All @@ -64,6 +118,11 @@ def build_manifest(ledger_paths: list[str], out_dir: str) -> tuple[str, str]:
"purpose": "Bitcoin timestamp of mirror-stack ledger heads — "
"proves no-backdating, NOT content truth.",
"ledger_count": len(rows), "ledgers": rows}
if id_map_path is not None:
manifest["format"] = 2
manifest["ledger_naming"] = {
"scheme": REDACTED_SCHEME,
"salts": "private; disclosed per ledger by the operator on request"}
with open(man, "x", encoding="utf-8") as stream:
stream.write(json.dumps(manifest, ensure_ascii=False, indent=2))
stream.flush()
Expand All @@ -76,11 +135,13 @@ def _info(ots_path: str) -> str:
text=True, timeout=60).stdout


def stamp(ledger_paths: list[str], out_dir: str) -> dict:
"""Build a manifest of the ledger heads and submit it to the OTS Bitcoin calendars."""
def stamp(ledger_paths: list[str], out_dir: str, *, id_map_path: str | None = None) -> dict:
"""Build a manifest of the ledger heads and submit it to the OTS Bitcoin calendars.

Pass ``id_map_path`` to write a publishable (format 2, redacted-name) manifest."""
if not ledger_paths:
return {"ok": False, "state": "error", "error": "ledger_paths is empty"}
man, sha = build_manifest(ledger_paths, out_dir)
man, sha = build_manifest(ledger_paths, out_dir, id_map_path=id_map_path)
try:
r = subprocess.run([OTS, "stamp", man], capture_output=True, text=True, timeout=120)
except FileNotFoundError:
Expand Down Expand Up @@ -142,3 +203,30 @@ def verify(ots_path: str, explorer: str = DEFAULT_EXPLORER) -> dict:
"explorer": explorer,
"proves": "ledger heads existed before this Bitcoin block (no backdating)",
"does_not_prove": ["content truth (GIGO)", "an external judging witness exists"]}


def locate_ledger(manifest_path: str, ledger_path: str, *, name: str | None = None,
salt_hex: str | None = None) -> dict:
"""Find which manifest row pins this ledger (format 1 or 2).

Ledgers are append-only, so a later copy still matches if its first ``bytes``
bytes hash to the pinned sha256. With ``name`` + ``salt_hex`` (disclosed by the
operator) a format-2 row's ``ledger_id`` is also checked against that name."""
manifest = json.loads(Path(manifest_path).read_text(encoding="utf-8"))
data = Path(ledger_path).read_bytes()
want_id = ledger_id(name, salt_hex) if (name and salt_hex) else None
for row in manifest.get("ledgers", []):
n = row.get("bytes")
if not isinstance(n, int) or n > len(data):
continue
if hashlib.sha256(data[:n]).hexdigest() != row.get("sha256"):
continue
result = {"found": True, "format": manifest.get("format", 1),
"pinned_bytes": n, "exact": n == len(data),
"pinned_lines": data[:n].count(b"\n"),
"head_seal": row.get("head_seal"),
"ledger_id": row.get("ledger_id"), "ledger": row.get("ledger")}
if want_id is not None:
result["name_matches"] = row.get("ledger_id") == want_id or row.get("ledger") == name
return result
return {"found": False, "format": manifest.get("format", 1)}
68 changes: 68 additions & 0 deletions tests/test_ots_manifest_redaction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import json
from pathlib import Path

import pytest

from mirror_stack_mcp.ots_anchor import build_manifest, ledger_id, locate_ledger


def _ledger(tmp_path, name, lines):
p = tmp_path / "ledgers" / name
p.parent.mkdir(exist_ok=True)
p.write_text("".join(json.dumps({"seal": s}) + "\n" for s in lines), encoding="utf-8")
return p


def test_format1_unchanged(tmp_path):
led = _ledger(tmp_path, "secret_project.jsonl", ["a1"])
man, _ = build_manifest([str(led)], str(tmp_path / "out"))
d = json.loads(Path(man).read_text(encoding="utf-8"))
assert "format" not in d
assert d["ledgers"][0]["ledger"] == "secret_project.jsonl"
assert d["ledgers"][0]["path"] == str(led)


def test_format2_hides_names_and_paths(tmp_path):
led = _ledger(tmp_path, "secret_project.jsonl", ["a1", "a2"])
idmap = tmp_path / "private" / "ids.json"
man, _ = build_manifest([str(led)], str(tmp_path / "out"), id_map_path=str(idmap))
text = Path(man).read_text(encoding="utf-8")
assert "secret_project" not in text and str(tmp_path) not in text
d = json.loads(text)
assert d["format"] == 2 and "path" not in d["ledgers"][0] and "ledger" not in d["ledgers"][0]
salt = json.loads(idmap.read_text(encoding="utf-8"))["salts"]["secret_project.jsonl"]
assert d["ledgers"][0]["ledger_id"] == ledger_id("secret_project.jsonl", salt)


def test_ids_stable_across_runs_and_salts_per_ledger(tmp_path):
a = _ledger(tmp_path, "a.jsonl", ["x"])
b = _ledger(tmp_path, "b.jsonl", ["y"])
idmap = str(tmp_path / "private" / "ids.json")
m1, _ = build_manifest([str(a), str(b)], str(tmp_path / "out"), id_map_path=idmap)
m2, _ = build_manifest([str(a), str(b)], str(tmp_path / "out"), id_map_path=idmap)
ids = lambda m: [r["ledger_id"] for r in json.loads(Path(m).read_text())["ledgers"]]
assert ids(m1) == ids(m2)
salts = json.loads(Path(idmap).read_text())["salts"]
assert salts["a.jsonl"] != salts["b.jsonl"]


def test_id_map_must_stay_outside_published_dir(tmp_path):
led = _ledger(tmp_path, "a.jsonl", ["x"])
out = tmp_path / "out"
with pytest.raises(ValueError):
build_manifest([str(led)], str(out), id_map_path=str(out / "ids.json"))


def test_locate_ledger_after_append_both_formats(tmp_path):
led = _ledger(tmp_path, "a.jsonl", ["x1", "x2"])
idmap = str(tmp_path / "private" / "ids.json")
m1, _ = build_manifest([str(led)], str(tmp_path / "out"))
m2, _ = build_manifest([str(led)], str(tmp_path / "out"), id_map_path=idmap)
with open(led, "a", encoding="utf-8") as f:
f.write(json.dumps({"seal": "x3"}) + "\n")
salt = json.loads(Path(idmap).read_text())["salts"]["a.jsonl"]
r1 = locate_ledger(m1, str(led))
r2 = locate_ledger(m2, str(led), name="a.jsonl", salt_hex=salt)
assert r1["found"] and r1["pinned_lines"] == 2 and not r1["exact"]
assert r2["found"] and r2["format"] == 2 and r2["name_matches"] is True
assert locate_ledger(m2, str(led), name="b.jsonl", salt_hex=salt)["name_matches"] is False
Loading