import { readFabric } from "/mirror_record.js"; const TRUST_SCHEMA = "elementfold.mirror.trust.v1"; const TRUST_ROOT_SHA256 = "d8cfc19f687d8de16a202fd15608ae81b04efaa91f8ca870009360a3e625f870"; async function trustRead() { const response = await fetch("/trust_root.json", { cache: "no-store" }); if (!response.ok) { throw new Error("the trust root cannot be read"); } const bytes = await response.arrayBuffer(); const digest = [...new Uint8Array(await crypto.subtle.digest("SHA-256", bytes))] .map(byte => byte.toString(16).padStart(2, "0")).join(""); if (digest !== TRUST_ROOT_SHA256) { throw new Error("the trust root is not anchored by this release"); } const trust = JSON.parse(new TextDecoder().decode(bytes)); if (trust.schema !== TRUST_SCHEMA || typeof trust.issuers !== "object" || !Array.isArray(trust.nodes)) { throw new Error("the trust root has the wrong shape"); } return trust; } function drawn(record) { const panel = document.querySelector(`[data-panel="${record.type}"]`); if (!panel) { return; } const line = document.createElement("p"); line.className = "record"; line.textContent = new TextDecoder("utf-8", { fatal: true }).decode( Uint8Array.from(atob(record.payload), character => character.charCodeAt(0)), ); panel.append(line); } async function draw() { const standing = document.querySelector("#standing"); document.querySelectorAll(".record").forEach(record => record.remove()); try { const trust = await trustRead(); const { records, refusals } = await readFabric(trust); records.forEach(drawn); standing.textContent = refusals.length ? `${refusals.length} record or path problem(s): ${refusals.join(" ยท ")}` : ""; } catch (failure) { standing.textContent = failure instanceof Error ? failure.message : String(failure); } } void draw();