|
| 1 | +const DEFAULTS = Object.freeze({ |
| 2 | + version: "v1.7.0", |
| 3 | + capsuleUrl: "https://truthframer.github.io/truthframer-platform/capsule/TRUTHFRAMER_PUBLIC_VERIFICATION_CAPSULE.json", |
| 4 | + receiptUrl: "https://truthframer.github.io/truthframer-platform/receipt/TRUTHFRAMER_PUBLIC_CAPSULE_CONSUMPTION_RECEIPT.json" |
| 5 | +}); |
| 6 | + |
| 7 | +export function stableStringify(value) { |
| 8 | + if (value === null || typeof value !== "object") return JSON.stringify(value); |
| 9 | + if (Array.isArray(value)) return "[" + value.map(stableStringify).join(",") + "]"; |
| 10 | + const keys = Object.keys(value).sort(); |
| 11 | + return "{" + keys.map((key) => JSON.stringify(key) + ":" + stableStringify(value[key])).join(",") + "}"; |
| 12 | +} |
| 13 | + |
| 14 | +export async function sha256Hex(input) { |
| 15 | + const bytes = typeof input === "string" ? new TextEncoder().encode(input) : input; |
| 16 | + |
| 17 | + if (globalThis.crypto && globalThis.crypto.subtle) { |
| 18 | + const digest = await globalThis.crypto.subtle.digest("SHA-256", bytes); |
| 19 | + return Array.from(new Uint8Array(digest)).map((b) => b.toString(16).padStart(2, "0")).join(""); |
| 20 | + } |
| 21 | + |
| 22 | + const { createHash } = await import("node:crypto"); |
| 23 | + return createHash("sha256").update(Buffer.from(bytes)).digest("hex"); |
| 24 | +} |
| 25 | + |
| 26 | +function normalizeKey(key) { |
| 27 | + return String(key).replace(/[^a-zA-Z0-9]/g, "").toUpperCase(); |
| 28 | +} |
| 29 | + |
| 30 | +function isSha256(value) { |
| 31 | + return typeof value === "string" && /^[a-f0-9]{64}$/i.test(value); |
| 32 | +} |
| 33 | + |
| 34 | +function asString(value) { |
| 35 | + if (value === null || value === undefined) return ""; |
| 36 | + return String(value); |
| 37 | +} |
| 38 | + |
| 39 | +function asBool(value) { |
| 40 | + if (value === true) return true; |
| 41 | + if (value === false) return false; |
| 42 | + if (typeof value === "string") return value.toLowerCase() === "true"; |
| 43 | + return false; |
| 44 | +} |
| 45 | + |
| 46 | +function walk(value, path = [], out = []) { |
| 47 | + if (!value || typeof value !== "object") return out; |
| 48 | + |
| 49 | + if (Array.isArray(value)) { |
| 50 | + value.forEach((item, index) => walk(item, path.concat(String(index)), out)); |
| 51 | + return out; |
| 52 | + } |
| 53 | + |
| 54 | + for (const [key, child] of Object.entries(value)) { |
| 55 | + const itemPath = path.concat(key); |
| 56 | + out.push({ |
| 57 | + key, |
| 58 | + normalizedKey: normalizeKey(key), |
| 59 | + normalizedPath: itemPath.map(normalizeKey).join("."), |
| 60 | + value: child |
| 61 | + }); |
| 62 | + walk(child, itemPath, out); |
| 63 | + } |
| 64 | + |
| 65 | + return out; |
| 66 | +} |
| 67 | + |
| 68 | +function firstBoolBySemanticKey(root, names) { |
| 69 | + const wanted = new Set(names.map(normalizeKey)); |
| 70 | + const hit = walk(root).find((entry) => wanted.has(entry.normalizedKey)); |
| 71 | + return hit ? asBool(hit.value) : false; |
| 72 | +} |
| 73 | + |
| 74 | +function firstStringBySemanticKey(root, names) { |
| 75 | + const wanted = new Set(names.map(normalizeKey)); |
| 76 | + const hit = walk(root).find((entry) => wanted.has(entry.normalizedKey)); |
| 77 | + return hit ? asString(hit.value) : ""; |
| 78 | +} |
| 79 | + |
| 80 | +function shaCandidates(root, predicate) { |
| 81 | + return walk(root) |
| 82 | + .filter((entry) => isSha256(entry.value)) |
| 83 | + .filter(predicate) |
| 84 | + .map((entry) => ({ |
| 85 | + key: entry.key, |
| 86 | + path: entry.normalizedPath, |
| 87 | + value: String(entry.value).toLowerCase() |
| 88 | + })); |
| 89 | +} |
| 90 | + |
| 91 | +function preferredSha(root, predicate) { |
| 92 | + const hits = shaCandidates(root, predicate); |
| 93 | + if (!hits.length) return ""; |
| 94 | + const exact = hits.find((hit) => hit.path.split(".").length <= 3); |
| 95 | + return (exact || hits[0]).value; |
| 96 | +} |
| 97 | + |
| 98 | +function capsuleShaFromCapsule(capsuleJson) { |
| 99 | + return preferredSha(capsuleJson, (entry) => { |
| 100 | + const k = entry.normalizedKey; |
| 101 | + const p = entry.normalizedPath; |
| 102 | + return ( |
| 103 | + k.endsWith("CAPSULESHA256") && |
| 104 | + !p.includes("RAWBYTES") && |
| 105 | + !p.includes("CANONICALJSON") && |
| 106 | + !p.includes("WITHOUTSHAFIELDS") && |
| 107 | + !p.includes("RECEIPT") |
| 108 | + ); |
| 109 | + }); |
| 110 | +} |
| 111 | + |
| 112 | +function capsuleShaFromReceipt(receiptJson) { |
| 113 | + return preferredSha(receiptJson, (entry) => { |
| 114 | + const k = entry.normalizedKey; |
| 115 | + const p = entry.normalizedPath; |
| 116 | + return ( |
| 117 | + k.endsWith("CAPSULESHA256") && |
| 118 | + !p.includes("RAWBYTES") && |
| 119 | + !p.includes("CANONICALJSON") && |
| 120 | + !p.includes("WITHOUTSHAFIELDS") && |
| 121 | + !p.includes("RECEIPT") |
| 122 | + ); |
| 123 | + }); |
| 124 | +} |
| 125 | + |
| 126 | +function receiptShaFromReceipt(receiptJson) { |
| 127 | + return preferredSha(receiptJson, (entry) => { |
| 128 | + const k = entry.normalizedKey; |
| 129 | + const p = entry.normalizedPath; |
| 130 | + return ( |
| 131 | + k.endsWith("RECEIPTSHA256") || |
| 132 | + k.endsWith("CONSUMPTIONRECEIPTSHA256") || |
| 133 | + p.endsWith("RECEIPTSHA256") || |
| 134 | + p.endsWith("CONSUMPTIONRECEIPTSHA256") |
| 135 | + ); |
| 136 | + }); |
| 137 | +} |
| 138 | + |
| 139 | +async function fetchPublicJson(url) { |
| 140 | + const response = await fetch(url, { cache: "no-store" }); |
| 141 | + const text = await response.text(); |
| 142 | + |
| 143 | + if (!response.ok) { |
| 144 | + throw new Error(`PUBLIC_FETCH_FAILED url=${url} status=${response.status}`); |
| 145 | + } |
| 146 | + |
| 147 | + return { |
| 148 | + url, |
| 149 | + status: response.status, |
| 150 | + text, |
| 151 | + json: JSON.parse(text), |
| 152 | + raw_bytes_sha256: await sha256Hex(text), |
| 153 | + canonical_json_sha256: await sha256Hex(stableStringify(JSON.parse(text))) |
| 154 | + }; |
| 155 | +} |
| 156 | + |
| 157 | +export async function verifyTruthframerPublicIndependently(config = {}) { |
| 158 | + const capsuleUrl = config.capsuleUrl || DEFAULTS.capsuleUrl; |
| 159 | + const receiptUrl = config.receiptUrl || DEFAULTS.receiptUrl; |
| 160 | + |
| 161 | + const capsule = await fetchPublicJson(capsuleUrl); |
| 162 | + const receipt = await fetchPublicJson(receiptUrl); |
| 163 | + |
| 164 | + const capsuleDeclaredSha = capsuleShaFromCapsule(capsule.json); |
| 165 | + const receiptCapsuleSha = capsuleShaFromReceipt(receipt.json); |
| 166 | + const receiptDeclaredSha = receiptShaFromReceipt(receipt.json); |
| 167 | + const receiptObservedSha = receiptDeclaredSha || receipt.canonical_json_sha256; |
| 168 | + |
| 169 | + const claims = { |
| 170 | + capsule_sha256: capsuleDeclaredSha, |
| 171 | + receipt_sha256: receiptObservedSha, |
| 172 | + receipt_sha256_basis: receiptDeclaredSha ? "declared_inside_public_receipt_json" : "computed_public_receipt_canonical_json_sha256", |
| 173 | + receipt_capsule_sha256: receiptCapsuleSha, |
| 174 | + capsule_fetched_from_public_url: firstBoolBySemanticKey(receipt.json, ["CAPSULE_FETCHED_FROM_PUBLIC_URL"]), |
| 175 | + capsule_sha256_match: firstBoolBySemanticKey(receipt.json, ["CAPSULE_SHA256_MATCH"]), |
| 176 | + capsule_sha256_match_basis: firstStringBySemanticKey(receipt.json, ["CAPSULE_SHA256_MATCH_BASIS"]), |
| 177 | + capsule_artifacts_live: firstBoolBySemanticKey(receipt.json, ["CAPSULE_ARTIFACTS_LIVE"]), |
| 178 | + release_tag_bound: firstBoolBySemanticKey(receipt.json, ["RELEASE_TAG_BOUND"]), |
| 179 | + no_private_source_required: firstBoolBySemanticKey(receipt.json, ["NO_PRIVATE_SOURCE_REQUIRED"]) |
| 180 | + }; |
| 181 | + |
| 182 | + const assertions = { |
| 183 | + capsule_public_fetch_ok: capsule.status === 200, |
| 184 | + receipt_public_fetch_ok: receipt.status === 200, |
| 185 | + capsule_declares_sha256: isSha256(claims.capsule_sha256), |
| 186 | + receipt_has_public_sha256_basis: isSha256(claims.receipt_sha256), |
| 187 | + receipt_capsule_sha256_matches_capsule: claims.receipt_capsule_sha256 === claims.capsule_sha256, |
| 188 | + receipt_claims_capsule_public_fetch: claims.capsule_fetched_from_public_url === true, |
| 189 | + receipt_claims_capsule_sha256_match: claims.capsule_sha256_match === true, |
| 190 | + receipt_claims_capsule_artifacts_live: claims.capsule_artifacts_live === true, |
| 191 | + receipt_claims_release_tag_bound: claims.release_tag_bound === true, |
| 192 | + receipt_claims_no_private_source_required: claims.no_private_source_required === true, |
| 193 | + receipt_sha_basis_is_public_capsule_internal_digest: |
| 194 | + claims.capsule_sha256_match_basis === "expected_sha256_found_inside_public_capsule_json" |
| 195 | + }; |
| 196 | + |
| 197 | + const pass = Object.values(assertions).every(Boolean); |
| 198 | + |
| 199 | + const result = { |
| 200 | + protocol: "TRUTHFRAMER_PUBLIC_INDEPENDENT_VERIFIER", |
| 201 | + version: DEFAULTS.version, |
| 202 | + status: pass ? "PUBLIC_INDEPENDENT_VERIFICATION_PASS" : "PUBLIC_INDEPENDENT_VERIFICATION_FAIL", |
| 203 | + public_inputs: { |
| 204 | + public_verification_capsule_url: capsuleUrl, |
| 205 | + public_capsule_consumption_receipt_url: receiptUrl |
| 206 | + }, |
| 207 | + public_object_digests: { |
| 208 | + capsule_raw_bytes_sha256: capsule.raw_bytes_sha256, |
| 209 | + capsule_canonical_json_sha256: capsule.canonical_json_sha256, |
| 210 | + receipt_raw_bytes_sha256: receipt.raw_bytes_sha256, |
| 211 | + receipt_canonical_json_sha256: receipt.canonical_json_sha256 |
| 212 | + }, |
| 213 | + observed_public_claims: claims, |
| 214 | + assertions |
| 215 | + }; |
| 216 | + |
| 217 | + result.verifier_result_sha256 = await sha256Hex(stableStringify(result)); |
| 218 | + return result; |
| 219 | +} |
| 220 | + |
| 221 | +if (typeof window !== "undefined") { |
| 222 | + window.TRUTHFRAMER_PUBLIC_INDEPENDENT_VERIFIER = { |
| 223 | + verifyTruthframerPublicIndependently, |
| 224 | + stableStringify, |
| 225 | + sha256Hex |
| 226 | + }; |
| 227 | +} |
0 commit comments