@sanning/proof 0.12.0 → 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/README.md +25 -12
  2. package/dist/agent-proof.d.ts +2 -2
  3. package/dist/agent-proof.js +4 -4
  4. package/dist/case-file.d.ts +126 -0
  5. package/dist/case-file.js +353 -0
  6. package/dist/case.d.ts +217 -26
  7. package/dist/case.js +535 -124
  8. package/dist/cli.d.ts +64 -8
  9. package/dist/cli.js +799 -219
  10. package/dist/compose.js +6 -1
  11. package/dist/crypto.d.ts +1 -1
  12. package/dist/crypto.js +5 -1
  13. package/dist/declaration.d.ts +58 -52
  14. package/dist/declaration.js +256 -33
  15. package/dist/evidence.d.ts +38 -0
  16. package/dist/evidence.js +238 -22
  17. package/dist/exit-codes.d.ts +17 -0
  18. package/dist/exit-codes.js +50 -0
  19. package/dist/file-match.d.ts +105 -0
  20. package/dist/file-match.js +374 -0
  21. package/dist/grading.d.ts +28 -13
  22. package/dist/grading.js +90 -31
  23. package/dist/index.d.ts +14 -7
  24. package/dist/index.js +20 -6
  25. package/dist/message-parts.d.ts +18 -10
  26. package/dist/message-parts.js +25 -15
  27. package/dist/types.d.ts +1 -0
  28. package/dist/verifier.d.ts +63 -4
  29. package/dist/verifier.js +223 -15
  30. package/dist/witness-cache.d.ts +85 -0
  31. package/dist/witness-cache.js +264 -0
  32. package/dist/zip-reader.d.ts +50 -0
  33. package/dist/zip-reader.js +220 -0
  34. package/package.json +2 -2
  35. package/src/agent-proof.ts +4 -4
  36. package/src/case-file.ts +422 -0
  37. package/src/case.ts +671 -131
  38. package/src/cli.ts +932 -217
  39. package/src/compose.ts +8 -1
  40. package/src/crypto.ts +5 -1
  41. package/src/declaration.ts +248 -52
  42. package/src/evidence.ts +257 -23
  43. package/src/exit-codes.ts +54 -0
  44. package/src/file-match.ts +461 -0
  45. package/src/grading.ts +109 -52
  46. package/src/index.ts +101 -2
  47. package/src/message-parts.ts +25 -15
  48. package/src/types.ts +5 -0
  49. package/src/verifier.ts +225 -16
  50. package/src/witness-cache.ts +306 -0
  51. package/src/zip-reader.ts +229 -0
  52. package/vectors/README.md +66 -8
  53. package/vectors/case-file-index.json +668 -0
  54. package/vectors/case-split.json +218 -9
  55. package/vectors/file-match.json +1808 -0
  56. package/vectors/jcs-reader.json +82 -3
  57. package/vectors/pack-grading.json +68 -4
  58. package/vectors/stamp-on-chain.json +20 -5
  59. package/vectors/witness-cache.json +1304 -0
package/src/verifier.ts CHANGED
@@ -23,7 +23,7 @@ import type { ContentRole, Envelope, VerificationResult, VerifyOptions } from ".
23
23
  // profile-specific dialect (no underscore strip, that is mlflow-only).
24
24
  //
25
25
  // Exported for parity with the sibling kernels' registries — Python
26
- // `ACCEPTED_SPEC_VERSIONS`, Go `SupportedSpecMajor` — so the accept-set is
26
+ // `ACCEPTED_SPEC_MAJORS`, Go `SupportedSpecMajor` — so the accept-set is
27
27
  // assertable rather than inferable: test/accept-set.test.ts holds it to the
28
28
  // ratified §4/§5 set transcribed in cross-kernel/accept-set.json, and the
29
29
  // cross-kernel gate probes one signed envelope per identifier. This entry list
@@ -201,11 +201,13 @@ const JCS_BOM = "";
201
201
  * twin is `jcs_loads` in `src/sanning_proof/canonicalize.py`, and every site in
202
202
  * either kernel that reads bytes a producer signed goes through one of the two.
203
203
  *
204
- * ONE RULE, SPELLED OUT, BECAUSE TWO BUILT-INS ARE NOT ONE RULE. Four checks
204
+ * ONE RULE, SPELLED OUT, BECAUSE TWO BUILT-INS ARE NOT ONE RULE. Five checks
205
205
  * carry it, none of them left to a decoder's defaults: strict UTF-8, no leading
206
206
  * byte-order mark, no non-finite literal (this kernel's `JSON.parse` rejects
207
207
  * `NaN`, `Infinity` and `-Infinity` natively, and Python names them in a
208
- * `parse_constant` hook), and no unpaired surrogate in the parsed value.
208
+ * `parse_constant` hook), no object that repeats a member name
209
+ * (`DuplicateKeyError`, proof#144), and no unpaired surrogate in the parsed
210
+ * value.
209
211
  *
210
212
  * THE FOURTH CHECK IS NOT THE FIRST ONE TWICE. The strict decode refuses a
211
213
  * surrogate written as raw bytes; it cannot see one written as the ASCII
@@ -231,14 +233,143 @@ const JCS_BOM = "";
231
233
  * Throws for anything that is not conformant; every call site treats a throw as
232
234
  * "this reader cannot read these bytes" and names the check that failed.
233
235
  */
236
+ /**
237
+ * Bytes as strict UTF-8, a leading byte-order mark KEPT as a character.
238
+ * `utf8_strict` in the Python twin's `canonicalize.py` is the same rule, and
239
+ * `jcsParse` below performs the identical decode. Throws on bytes that are not
240
+ * UTF-8.
241
+ */
242
+ export function utf8Strict(bytes: Uint8Array): string {
243
+ return new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(bytes);
244
+ }
245
+
234
246
  export function jcsParse(bytes: Uint8Array): unknown {
235
247
  const text = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(bytes);
236
248
  if (text.startsWith(JCS_BOM)) throw new Error("jcsParse: leading byte-order mark");
237
- const value: unknown = JSON.parse(text);
249
+ const value: unknown = parseRefusingDuplicates(text);
238
250
  rejectUnpairedSurrogates(value);
239
251
  return value;
240
252
  }
241
253
 
254
+ /**
255
+ * A JSON object that names one member twice (I-JSON, RFC 7493 section 2.3).
256
+ * The Python twin is `DuplicateKey` in `canonicalize.py`, and the message is
257
+ * the same kernel wording in both: `duplicate key ` and the name as
258
+ * `renderKey` spells it.
259
+ *
260
+ * JCS (RFC 8785) is defined over I-JSON, and I-JSON forbids duplicate member
261
+ * names, so bytes that repeat a name are MALFORMED rather than another spelling
262
+ * of the last value. `JSON.parse` and Python's `json.loads` both keep the last
263
+ * value and say nothing, which let a bundle carrying `"spec_version":"junk"`
264
+ * before its real `spec_version` read `verified` in both kernels (proof#144).
265
+ */
266
+ export class DuplicateKeyError extends Error {
267
+ readonly key: string;
268
+ constructor(key: string) {
269
+ super(`duplicate key ${renderKey(key)}`);
270
+ this.key = key;
271
+ }
272
+ }
273
+
274
+ // The longest rendered name a message carries, in characters, before it is
275
+ // cut. The rendering is pure ASCII, so the cut lands on the same character in
276
+ // both kernels.
277
+ const RENDERED_KEY_MAX = 64;
278
+
279
+ /**
280
+ * A member name as a quoted, pure-ASCII string, identically in both kernels
281
+ * (`render_key` in the Python twin). Printable ASCII other than `"` and `\` is
282
+ * kept; `"` and `\` are backslash-escaped; every other UTF-16 code unit is
283
+ * `\uXXXX` in lower-case hex. A rendering longer than 64 characters is cut
284
+ * there and `...` follows the closing quote.
285
+ */
286
+ export function renderKey(key: string): string {
287
+ let body = "";
288
+ for (let i = 0; i < key.length; i++) {
289
+ const c = key.charCodeAt(i);
290
+ const ch = key[i]!;
291
+ if (ch === '"' || ch === "\\") body += "\\" + ch;
292
+ else if (c >= 0x20 && c <= 0x7e) body += ch;
293
+ else body += "\\u" + c.toString(16).padStart(4, "0");
294
+ }
295
+ if (body.length > RENDERED_KEY_MAX) return `"${body.slice(0, RENDERED_KEY_MAX)}"...`;
296
+ return `"${body}"`;
297
+ }
298
+
299
+ /**
300
+ * `JSON.parse(text)`, refusing an object that repeats a member name. The Python
301
+ * twin is `loads_refusing_duplicates` in `canonicalize.py`.
302
+ *
303
+ * `JSON.parse` cannot see a repeat, because it keeps the last value, so the
304
+ * text is scanned once more AFTER it parsed. The parse runs first, so text
305
+ * that is not JSON at all fails as JSON and never as a duplicate, as it does in
306
+ * the Python twin. Throws `DuplicateKeyError`.
307
+ */
308
+ export function parseRefusingDuplicates(text: string): unknown {
309
+ const value: unknown = JSON.parse(text);
310
+ const dup = firstDuplicateKey(text);
311
+ if (dup !== undefined) throw new DuplicateKeyError(dup);
312
+ return value;
313
+ }
314
+
315
+ /**
316
+ * The repeated member name to report for text `JSON.parse` has already
317
+ * accepted, or `undefined` when no object repeats one.
318
+ *
319
+ * WHICH ONE, because both kernels must name the same one. Objects are checked
320
+ * as they CLOSE, innermost first, which is the order Python's
321
+ * `object_pairs_hook` sees them in; inside one object the name reported is the
322
+ * first whose repeat comes earliest. Names compare after unescaping, so `"a"`
323
+ * and `"\u0061"` are one name, as they are to both parsers.
324
+ *
325
+ * ITERATIVE, with an explicit stack, for the reason `rejectUnpairedSurrogates`
326
+ * is: this walks bytes a producer wrote. It relies on the text being valid
327
+ * JSON, which is why it runs only after `JSON.parse` succeeded: outside a
328
+ * string, only `{`, `[`, `}`, `]`, `,` and `"` change what comes next.
329
+ */
330
+ export function firstDuplicateKey(text: string): string | undefined {
331
+ // `names` is null for an array. `expectName` is true where the next string
332
+ // is a member name rather than a value.
333
+ const stack: Array<{ names: Set<string> | null; expectName: boolean; dup?: string }> = [];
334
+ const n = text.length;
335
+ let i = 0;
336
+ while (i < n) {
337
+ const c = text.charCodeAt(i);
338
+ if (c === 0x22) {
339
+ let j = i + 1;
340
+ for (;;) {
341
+ const d = text.charCodeAt(j);
342
+ if (d === 0x5c) j += 2;
343
+ else if (d === 0x22) break;
344
+ else j++;
345
+ }
346
+ const top = stack[stack.length - 1];
347
+ if (top !== undefined && top.names !== null && top.expectName) {
348
+ const name = JSON.parse(text.slice(i, j + 1)) as string;
349
+ if (top.names.has(name)) {
350
+ if (top.dup === undefined) top.dup = name;
351
+ } else {
352
+ top.names.add(name);
353
+ }
354
+ top.expectName = false;
355
+ }
356
+ i = j + 1;
357
+ continue;
358
+ }
359
+ if (c === 0x7b) stack.push({ names: new Set(), expectName: true });
360
+ else if (c === 0x5b) stack.push({ names: null, expectName: false });
361
+ else if (c === 0x7d || c === 0x5d) {
362
+ const closed = stack.pop();
363
+ if (closed?.dup !== undefined) return closed.dup;
364
+ } else if (c === 0x2c) {
365
+ const top = stack[stack.length - 1];
366
+ if (top !== undefined && top.names !== null) top.expectName = true;
367
+ }
368
+ i++;
369
+ }
370
+ return undefined;
371
+ }
372
+
242
373
  /**
243
374
  * Refuse a parsed value carrying an unpaired surrogate, in a key or a value, at
244
375
  * any depth. The Python twin is `_reject_unpaired_surrogates`, and the two name
@@ -311,9 +442,17 @@ function hasLoneSurrogate(s: string): boolean {
311
442
  // `<major>.<minor>` where minor is `[0-9]+`. A non-numeric minor suffix is
312
443
  // malformed, not a tolerated future version — matching the Python kernel's
313
444
  // 0.1.1 semantics (the TS/Go fail-open lenience was closed deliberately).
314
- export function specVersionSupported(specVersion: string): boolean {
445
+ //
446
+ // `accepted` NARROWS the registry and never widens it, as the Python twin's
447
+ // `spec_version_supported(spec_version, accepted)` does (P-10, 0.13.0).
448
+ export function specVersionSupported(
449
+ specVersion: string,
450
+ accepted?: readonly string[],
451
+ ): boolean {
315
452
  if (typeof specVersion !== "string" || specVersion === "") return false;
316
- return ACCEPTED_SPEC_MAJORS.some((m) => {
453
+ const registry =
454
+ accepted === undefined ? ACCEPTED_SPEC_MAJORS : ACCEPTED_SPEC_MAJORS.filter((m) => accepted.includes(m));
455
+ return registry.some((m) => {
317
456
  if (specVersion === m) return true;
318
457
  if (!specVersion.startsWith(`${m}.`)) return false;
319
458
  const minor = specVersion.slice(m.length + 1);
@@ -392,9 +531,75 @@ function skeletonFieldErrors(env: Envelope): string[] {
392
531
  if (environment !== undefined && environment !== "dev" && environment !== "production") {
393
532
  out.push('malformed environment: not "dev" or "production" (envelope-spec §2)');
394
533
  }
534
+ out.push(...identityFieldErrors(own));
535
+ return out;
536
+ }
537
+
538
+ // The four fields envelope-spec §2 types and A23 pins (proof#97, proof#155).
539
+ // Every pattern is anchored at both ends with no `m` flag, so `$` never matches
540
+ // before a trailing newline, and uses ASCII classes only.
541
+ //
542
+ // `event_id`: "UUID v4, lowercase", the pattern both SDKs already refuse to
543
+ // sign anything else under (`UUID_V4_RE` in `@sanning/anchor`, `_UUID_V4_RE` in
544
+ // `sanning-anchor`); the Go agent's `uuid.NewString` emits the same.
545
+ const UUID_V4 = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
546
+ // `payload_hash`: sha256-hex, which invariant 2 defines as lowercase hex with no
547
+ // `0x`. Lower case is pinned here, unlike `public_key`, because every kernel
548
+ // compares the value against a digest it recomputes in lower case: an
549
+ // upper-case `payload_hash` already failed wherever there was material.
550
+ const SHA256_HEX = /^[0-9a-f]{64}$/;
551
+ // `payload_ref`: §2 says "string (URI)" and names no grammar, so this pins what
552
+ // both SDKs emit: an ASCII scheme, a colon, then RFC 3986 unreserved, reserved
553
+ // and percent-encoded characters, at most 2048 of them in all (`REF_RE` and
554
+ // `MAX_REF_LEN` in `@sanning/anchor`'s `ref.ts`).
555
+ const URI_REF = /^[A-Za-z][A-Za-z0-9+.-]*:[A-Za-z0-9\-._~:/?#[\]@!$&'()*+,;=%]*$/;
556
+ const MAX_URI_REF = 2048;
557
+ // `subject`: "string fields <=128 chars matching `^[A-Za-z0-9_.:-]+$`".
558
+ const SUBJECT_FIELD = /^[A-Za-z0-9_.:-]{1,128}$/;
559
+
560
+ // `event_id`, `payload_hash`, `payload_ref` and `subject` (A23). Each is checked
561
+ // only when present, and present includes a JSON null, as for `signed_at`.
562
+ // `payload_hash` is the exception: only a STRING is checked here, because a
563
+ // missing or non-string one is already `missing payload_hash` below, and naming
564
+ // it twice would state one fact in two sets of words. `subject` fixes only its
565
+ // STRING members: §2 types no other, so a member of another type is left as it
566
+ // was. Own members only, as everywhere in this file. The messages are
567
+ // byte-identical to the Python and Go kernels'.
568
+ function identityFieldErrors(own: (k: string) => unknown): string[] {
569
+ const out: string[] = [];
570
+ const eventId = own("event_id");
571
+ if (eventId !== undefined && !(typeof eventId === "string" && UUID_V4.test(eventId))) {
572
+ out.push("malformed event_id: not a lowercase UUID v4 (envelope-spec §2)");
573
+ }
574
+ const payloadHash = own("payload_hash");
575
+ if (typeof payloadHash === "string" && !SHA256_HEX.test(payloadHash)) {
576
+ out.push("malformed payload_hash: not 64 lowercase hex characters (envelope-spec §2)");
577
+ }
578
+ const payloadRef = own("payload_ref");
579
+ if (
580
+ payloadRef !== undefined &&
581
+ !(typeof payloadRef === "string" && payloadRef.length <= MAX_URI_REF && URI_REF.test(payloadRef))
582
+ ) {
583
+ out.push("malformed payload_ref: not a URI of at most 2048 RFC 3986 characters (envelope-spec §2)");
584
+ }
585
+ const subject = own("subject");
586
+ if (subject !== undefined && !isWellFormedSubject(subject)) {
587
+ out.push(
588
+ "malformed subject: not an object whose string fields are 1 to 128 characters of [A-Za-z0-9_.:-] (envelope-spec §2)",
589
+ );
590
+ }
395
591
  return out;
396
592
  }
397
593
 
594
+ function isWellFormedSubject(subject: unknown): boolean {
595
+ if (typeof subject !== "object" || subject === null || Array.isArray(subject)) return false;
596
+ for (const key of Object.keys(subject)) {
597
+ const v = (subject as Record<string, unknown>)[key];
598
+ if (typeof v === "string" && !SUBJECT_FIELD.test(v)) return false;
599
+ }
600
+ return true;
601
+ }
602
+
398
603
  // The content hash(es) an envelope commits to, by event type — the values a
399
604
  // reverse lookup can match the user's in-browser file hash against.
400
605
  // asset_registered -> payload.hash (the registered, known-good bytes)
@@ -456,22 +661,26 @@ export function contentHashes(env: Envelope): { role: ContentRole; hash: string
456
661
  // signature, so stripping it to fake "external" — or injecting one to fake
457
662
  // "inline" — breaks the signature).
458
663
  //
459
- // The second argument accepts the legacy `expectedContentHash` string form
460
- // or a VerifyOptions object ({ payloadBytes, expectedContentHash }).
664
+ // The second argument is a VerifyOptions object ({ payloadBytes,
665
+ // expectedContentHash, acceptedSpecMajors }). Until 0.13.0 it could also be a
666
+ // bare `expectedContentHash` string; that overload is gone (P-10), so the two
667
+ // kernels take one options shape: write `{ expectedContentHash: hash }`.
461
668
  export async function verifyEnvelope(
462
669
  env: Envelope,
463
- optionsOrContentHash?: string | VerifyOptions,
670
+ options?: VerifyOptions,
464
671
  ): Promise<VerificationResult> {
465
- const opts: VerifyOptions =
466
- typeof optionsOrContentHash === "string"
467
- ? { expectedContentHash: optionsOrContentHash }
468
- : (optionsOrContentHash ?? {});
672
+ if (typeof options === "string") {
673
+ throw new TypeError(
674
+ "verifyEnvelope: pass { expectedContentHash } rather than a bare string (0.13.0)",
675
+ );
676
+ }
677
+ const opts: VerifyOptions = options ?? {};
469
678
  // REFUSE an unknown option key rather than ignore it. `expected_content_hash`
470
679
  // — the Python kernel's spelling — previously fell through as `undefined`, so
471
680
  // the content bind silently never ran and the caller got a clean pass with no
472
681
  // errors at all. A check that quietly does not happen is worse than one that
473
682
  // fails: the caller believes they verified an artifact they never checked.
474
- const KNOWN_VERIFY_OPTIONS = new Set(["payloadBytes", "expectedContentHash"]);
683
+ const KNOWN_VERIFY_OPTIONS = new Set(["payloadBytes", "expectedContentHash", "acceptedSpecMajors"]);
475
684
  const unknownOptions = Object.keys(opts).filter((k) => !KNOWN_VERIFY_OPTIONS.has(k));
476
685
  if (unknownOptions.length > 0) {
477
686
  throw new TypeError(
@@ -551,7 +760,7 @@ async function verifyEnvelopeChecked(
551
760
  };
552
761
  }
553
762
 
554
- const specVersionOk = specVersionSupported(env.spec_version);
763
+ const specVersionOk = specVersionSupported(env.spec_version, opts.acceptedSpecMajors);
555
764
  if (!specVersionOk) errors.push(`unsupported spec_version: ${JSON.stringify(env.spec_version)}`);
556
765
 
557
766
  // Skeleton field format (envelope-spec §2, proof#95): a present `signed_at`
@@ -625,7 +834,7 @@ async function verifyEnvelopeChecked(
625
834
  let signatureOk = false;
626
835
  try {
627
836
  const { signature: _signature, co_signatures: _coSignatures, ...envelopeForSig } = env;
628
- signatureOk = await ed25519Verify(env.signature, utf8(jcs(envelopeForSig)), env.public_key);
837
+ signatureOk = await ed25519Verify(utf8(jcs(envelopeForSig)), env.signature, env.public_key);
629
838
  if (!signatureOk) errors.push("Ed25519 signature verification failed");
630
839
  } catch (e) {
631
840
  errors.push(`signature verification error: ${stringifyErr(e)}`);
@@ -0,0 +1,306 @@
1
+ // The witnessed-time cache: what a pack remembers about its witness, unverified
2
+ // (evidence-bundle.md 5.6.6, v1.25, A30, proof#24).
3
+ //
4
+ // The TypeScript half of `src/sanning_proof/witness_cache.py`. Both kernels are
5
+ // held to one table, `ts/vectors/witness-cache.json`, so neither can pass by
6
+ // agreeing with the other.
7
+ //
8
+ // WHAT THE CACHE IS. A file beside the pack's `bundle.json`,
9
+ // `witness-cache.json`, and never a member of the bundle. Its one list today,
10
+ // `witnessed`, gives for each stamp the block height and block time of the
11
+ // witness transaction, as the builder of the pack fetched them. Nobody signs
12
+ // it, and anyone holding the pack can edit it. EVERY MEMBER OF THE FILE IS
13
+ // UNVERIFIED BY CONSTRUCTION, and the name carries that warning to every field
14
+ // added to it later.
15
+ //
16
+ // Because it is a separate file, a reader before v1.25 never opens it: it lists
17
+ // it as a file outside every pack and reads the pack exactly as it would
18
+ // without it. A `cache` member written INSIDE `bundle.json` is an unknown
19
+ // wrapper field, counted in the signed scope by every reader, this one
20
+ // included.
21
+ //
22
+ // WHAT THIS MODULE DOES, AND WHAT IT MUST NEVER DO.
23
+ //
24
+ // - `readWitnessCache` reads the file's bytes against the signed bundle beside
25
+ // it. It is pure, and it is never an input to the bundle verdict, the pack
26
+ // grade or the case reading: neither `verifyEvidenceBundle` nor `readCase` is
27
+ // ever handed the file.
28
+ // - `checkWitnessCache` looks each cached time up on the public record, where a
29
+ // caller asked for a lookup. A lookup that answers with a different block is
30
+ // a finding, reported on the witness line as `differs`. A lookup that does
31
+ // not settle leaves the cached value labelled not checked.
32
+ // - Nothing here returns a time a reader may rely on. The only time this module
33
+ // ever reports as the public record's is one a gateway returned, and a
34
+ // gateway is named, never trusted (6).
35
+ //
36
+ // A file that is absent, that is not JSON under the reader's rule, whose
37
+ // `cache_version` is not `WITNESS_CACHE_VERSION`, whose `witnessed` is not a
38
+ // list, or whose entry is not well formed, reads as no cache for what it could
39
+ // not say. It is never a finding and never moves anything.
40
+
41
+ import { jsonDepthExceeds } from "./case-file.js";
42
+ import { jcs, jcsParse } from "./verifier.js";
43
+
44
+ /**
45
+ * The last second a block time can name and still be printed the same way by
46
+ * both kernels: 9999-12-31T23:59:59Z. A larger value is a cache entry this
47
+ * reader cannot print, and reads as none.
48
+ */
49
+ export const MAX_BLOCK_TIME = 253402300799;
50
+
51
+ /** The file's name, beside `bundle.json` in the pack's folder (5.6.1). */
52
+ export const WITNESS_CACHE_NAME = "witness-cache.json";
53
+
54
+ /**
55
+ * The dispatch key: a file whose `cache_version` is absent or any other value
56
+ * reads as no cache.
57
+ */
58
+ export const WITNESS_CACHE_VERSION = "sanning.witness-cache/v1";
59
+
60
+ /**
61
+ * How deep `[` and `{` may nest in the file before it is refused unread: the
62
+ * limit `bundle.json` and `contents.json` are read under.
63
+ */
64
+ export const MAX_WITNESS_CACHE_DEPTH = 512;
65
+
66
+ // The bodies that carry `stamps[]`, the only witnesses the cache speaks for.
67
+ const STAMP_BODIES = new Set(["sanning.stamp.trace/v1", "sanning.stamp.evidence/v2"]);
68
+ const STAMP_REF = /^[0-9a-f]{64}$/;
69
+ // The one witness kind a time is looked up for, and the only ids the lookup
70
+ // sends: the same rule the byte lookup applies (evidence-bundle 5.1.1, v1.6).
71
+ const ADAPTER_KIND = "arweave";
72
+ const ARWEAVE_ID = /^[A-Za-z0-9_-]{43}$/;
73
+ // A GraphQL answer to one query for one transaction is a few hundred bytes. A
74
+ // deeper answer is not one, and refusing it before any parser recurses keeps
75
+ // the two kernels on one reading of it.
76
+ const MAX_ANSWER_DEPTH = 16;
77
+
78
+ /**
79
+ * One `cache.witnessed[]` entry that reads, joined to the stamp it names.
80
+ * `witnessKind` and `witnessId` are the stamp's own `anchor_ref`, read from the
81
+ * signed body, never from the cache: the cache says WHEN, and the body alone
82
+ * says WHICH witness it is about.
83
+ */
84
+ export interface CachedWitnessTime {
85
+ stampRef: string;
86
+ blockHeight: number;
87
+ blockTime: number;
88
+ witnessKind: string;
89
+ witnessId: string;
90
+ }
91
+
92
+ /**
93
+ * One gateway's answer for one cached time. `matches`: the gateway named the
94
+ * cached block height and block time. `differs`: it named a block, and the
95
+ * block is not the cached one. `unreachable`: it named no block (a transport
96
+ * failure, a refusal, an answer that is not one, or a witness not yet in a
97
+ * block).
98
+ */
99
+ export interface WitnessTimeAnswer {
100
+ gateway: string;
101
+ outcome: "matches" | "differs" | "unreachable";
102
+ blockHeight: number | null;
103
+ blockTime: number | null;
104
+ }
105
+
106
+ /**
107
+ * One cached time, and what the public record said about it: `differs` when
108
+ * any gateway named a different block, otherwise `matches` when any gateway
109
+ * named the cached one, otherwise `not_checked`. The same worst-answer-wins
110
+ * order as the byte lookup.
111
+ */
112
+ export interface WitnessTimeCheck {
113
+ cached: CachedWitnessTime;
114
+ outcome: "matches" | "differs" | "not_checked";
115
+ answers: WitnessTimeAnswer[];
116
+ }
117
+
118
+ function isObject(v: unknown): v is Record<string, unknown> {
119
+ return v !== null && typeof v === "object" && !Array.isArray(v);
120
+ }
121
+
122
+ // A member the object itself holds, never one inherited from its prototype.
123
+ function own(o: Record<string, unknown>, key: string): unknown {
124
+ return Object.prototype.hasOwnProperty.call(o, key) ? o[key] : undefined;
125
+ }
126
+
127
+ function blockNumber(value: unknown, upper: number): number | null {
128
+ if (typeof value !== "number" || !Number.isSafeInteger(value)) return null;
129
+ if (value < 0 || value > upper) return null;
130
+ return value;
131
+ }
132
+
133
+ // `stamp_ref` to `[kind, id]` for every stamp in the signed body that names a
134
+ // witness. A `stamp_ref` spelled on two stamps names neither.
135
+ function stampWitnesses(bundle: Record<string, unknown>): Map<string, [string, string]> {
136
+ const out = new Map<string, [string, string]>();
137
+ if (typeof bundle.body_type !== "string" || !STAMP_BODIES.has(bundle.body_type)) return out;
138
+ const body = bundle.body;
139
+ const stamps = isObject(body) ? body.stamps : undefined;
140
+ if (!Array.isArray(stamps)) return out;
141
+ const seen = new Set<string>();
142
+ for (const stamp of stamps) {
143
+ if (!isObject(stamp)) continue;
144
+ const ref = stamp.stamp_ref;
145
+ if (typeof ref !== "string" || !STAMP_REF.test(ref)) continue;
146
+ if (seen.has(ref)) {
147
+ out.delete(ref);
148
+ continue;
149
+ }
150
+ seen.add(ref);
151
+ const anchor = stamp.anchor_ref;
152
+ if (!isObject(anchor)) continue;
153
+ if (typeof anchor.kind === "string" && typeof anchor.id === "string") {
154
+ out.set(ref, [anchor.kind, anchor.id]);
155
+ }
156
+ }
157
+ return out;
158
+ }
159
+
160
+ // The file's JSON, read under the rule `contents.json` is read under (5.6.2):
161
+ // strict UTF-8, no byte-order mark, no `NaN` or `Infinity`, no member named
162
+ // twice, and nothing nested past `MAX_WITNESS_CACHE_DEPTH`. `undefined` for
163
+ // anything else.
164
+ function readFile(raw: Uint8Array | null | undefined): unknown {
165
+ if (!(raw instanceof Uint8Array)) return undefined;
166
+ if (jsonDepthExceeds(raw, MAX_WITNESS_CACHE_DEPTH)) return undefined;
167
+ try {
168
+ return jcsParse(raw);
169
+ } catch {
170
+ return undefined;
171
+ }
172
+ }
173
+
174
+ /**
175
+ * The cached witness times the `witness-cache.json` bytes `raw` give for the
176
+ * signed `bundle` beside them, in the order the file lists them. `raw` is
177
+ * `null` where the pack has no such file. Pure, and never throws. The file
178
+ * reads only where it is a JSON object whose `cache_version` is
179
+ * `WITNESS_CACHE_VERSION` and whose `witnessed` is a list. An entry reads only
180
+ * where it is an object whose `stamp_ref` is 64 lowercase hex characters naming
181
+ * exactly one stamp of the signed body that has an `anchor_ref`, whose
182
+ * `block_height` is a non-negative safe integer, and whose `block_time` is a
183
+ * whole number of seconds since 1970-01-01T00:00:00Z from 0 to
184
+ * `MAX_BLOCK_TIME`. A `stamp_ref` two entries name reads for neither.
185
+ * Everything else reads as no cache for the stamp it would have spoken for.
186
+ */
187
+ export function readWitnessCache(bundle: unknown, raw: Uint8Array | null | undefined): CachedWitnessTime[] {
188
+ if (!isObject(bundle)) return [];
189
+ const cache = readFile(raw);
190
+ if (!isObject(cache)) return [];
191
+ if (own(cache, "cache_version") !== WITNESS_CACHE_VERSION) return [];
192
+ const entries = own(cache, "witnessed");
193
+ if (!Array.isArray(entries)) return [];
194
+ const witnesses = stampWitnesses(bundle);
195
+ const read: CachedWitnessTime[] = [];
196
+ const named = new Map<string, number>();
197
+ for (const entry of entries) {
198
+ if (!isObject(entry)) continue;
199
+ const ref = own(entry, "stamp_ref");
200
+ if (typeof ref !== "string" || !witnesses.has(ref)) continue;
201
+ named.set(ref, (named.get(ref) ?? 0) + 1);
202
+ const height = blockNumber(own(entry, "block_height"), Number.MAX_SAFE_INTEGER);
203
+ const time = blockNumber(own(entry, "block_time"), MAX_BLOCK_TIME);
204
+ if (height === null || time === null) continue;
205
+ const [kind, id] = witnesses.get(ref)!;
206
+ read.push({ stampRef: ref, blockHeight: height, blockTime: time, witnessKind: kind, witnessId: id });
207
+ }
208
+ return read.filter((c) => named.get(c.stampRef) === 1);
209
+ }
210
+
211
+ /** A block time as RFC 3339 UTC to the second, the way both kernels print it: `2026-09-08T12:10:07Z`. */
212
+ export function formatBlockTime(seconds: number): string {
213
+ return new Date(seconds * 1000).toISOString().replace(/\.\d{3}Z$/, "Z");
214
+ }
215
+
216
+ /** `block 1996701 at 2026-09-08T12:10:07Z`. */
217
+ export function renderWitnessTime(blockHeight: number, blockTime: number): string {
218
+ return `block ${blockHeight} at ${formatBlockTime(blockTime)}`;
219
+ }
220
+
221
+ /**
222
+ * The request body the lookup sends: one GraphQL query for one transaction's
223
+ * block, as JCS, so both kernels send the same bytes.
224
+ */
225
+ export function witnessTimeQuery(witnessId: string): string {
226
+ return jcs({ query: `query{transaction(id:"${witnessId}"){block{height timestamp}}}` });
227
+ }
228
+
229
+ function readAnswer(raw: Uint8Array): [number, number] | null {
230
+ if (jsonDepthExceeds(raw, MAX_ANSWER_DEPTH)) return null;
231
+ let node: unknown;
232
+ try {
233
+ node = jcsParse(raw);
234
+ } catch {
235
+ return null;
236
+ }
237
+ for (const key of ["data", "transaction", "block"]) {
238
+ if (!isObject(node)) return null;
239
+ node = node[key];
240
+ }
241
+ if (!isObject(node)) return null;
242
+ const height = blockNumber(node.height, Number.MAX_SAFE_INTEGER);
243
+ const time = blockNumber(node.timestamp, Number.MAX_SAFE_INTEGER);
244
+ if (height === null || time === null) return null;
245
+ return [height, time];
246
+ }
247
+
248
+ async function ask(
249
+ cached: CachedWitnessTime,
250
+ gateway: string,
251
+ fetchImpl: typeof fetch,
252
+ ): Promise<WitnessTimeAnswer> {
253
+ const unreachable: WitnessTimeAnswer = { gateway, outcome: "unreachable", blockHeight: null, blockTime: null };
254
+ const url = `${gateway.replace(/\/+$/, "")}/graphql`;
255
+ let raw: Uint8Array;
256
+ try {
257
+ const resp = await fetchImpl(url, {
258
+ method: "POST",
259
+ headers: { "content-type": "application/json" },
260
+ body: witnessTimeQuery(cached.witnessId),
261
+ });
262
+ if (!resp.ok) return unreachable;
263
+ raw = new Uint8Array(await resp.arrayBuffer());
264
+ } catch {
265
+ return unreachable;
266
+ }
267
+ const block = readAnswer(raw);
268
+ if (block === null) return unreachable;
269
+ const [height, time] = block;
270
+ const same = height === cached.blockHeight && time === cached.blockTime;
271
+ return { gateway, outcome: same ? "matches" : "differs", blockHeight: height, blockTime: time };
272
+ }
273
+
274
+ /**
275
+ * Look each cached time up on every gateway, in order. With no gateways or no
276
+ * `fetchImpl`, nothing is looked up and every time is `not_checked`. A witness
277
+ * that is not `arweave`, or whose id is not a 43-character base64url
278
+ * transaction id, is never looked up.
279
+ */
280
+ export async function checkWitnessCache(
281
+ cached: readonly CachedWitnessTime[],
282
+ gateways: readonly string[],
283
+ fetchImpl: typeof fetch | undefined,
284
+ ): Promise<WitnessTimeCheck[]> {
285
+ const out: WitnessTimeCheck[] = [];
286
+ for (const c of cached) {
287
+ if (
288
+ gateways.length === 0 ||
289
+ fetchImpl === undefined ||
290
+ c.witnessKind !== ADAPTER_KIND ||
291
+ !ARWEAVE_ID.test(c.witnessId)
292
+ ) {
293
+ out.push({ cached: c, outcome: "not_checked", answers: [] });
294
+ continue;
295
+ }
296
+ const answers: WitnessTimeAnswer[] = [];
297
+ for (const gw of gateways) answers.push(await ask(c, gw, fetchImpl));
298
+ const outcome = answers.some((a) => a.outcome === "differs")
299
+ ? "differs"
300
+ : answers.some((a) => a.outcome === "matches")
301
+ ? "matches"
302
+ : "not_checked";
303
+ out.push({ cached: c, outcome, answers });
304
+ }
305
+ return out;
306
+ }