@sanning/proof 0.10.0 → 0.11.1

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.
package/dist/chain.js CHANGED
@@ -54,6 +54,7 @@
54
54
  // against `anchor`); when it lands, the namespace table below stops mattering
55
55
  // and can go.
56
56
  import { hexToBytes, sha256Hex } from "./crypto.js";
57
+ import { jcsParse } from "./verifier.js";
57
58
  import { resolveTraceEvents } from "./evidence.js";
58
59
  /**
59
60
  * The one sentence a surface should print beside these findings, so the reader
@@ -109,7 +110,25 @@ function decodeRecord(hex) {
109
110
  return null;
110
111
  try {
111
112
  const bytes = hexToBytes(hex);
112
- const record = JSON.parse(new TextDecoder().decode(bytes));
113
+ // PINNED STRICT via `jcsParse`, and the twin at
114
+ // `src/sanning_proof/chain.py` is `jcs_loads` (proof#149). This was the
115
+ // LAST lenient decode pair in either kernel, and it was lenient on BOTH
116
+ // sides in different directions, so it agreed nowhere while looking like
117
+ // it agreed everywhere:
118
+ //
119
+ // * A bare `new TextDecoder()` here eats a leading byte-order mark and
120
+ // substitutes U+FFFD for every invalid sequence, so `BOM + JCS(r)`, a
121
+ // raw `0x80`, a CESU-8 surrogate pair and a truncated UTF-8 sequence
122
+ // each joined a chain HERE and joined none there.
123
+ // * Python's `json.loads` accepted `NaN`, `Infinity` and `-Infinity`,
124
+ // which RFC 8785 cannot emit and this kernel's `JSON.parse` rejects,
125
+ // so a record carrying one joined a chain THERE and none here.
126
+ //
127
+ // Five split readings on one pair, in two directions, and no test in
128
+ // either kernel could see them: each kernel agreed with itself. That is
129
+ // why agreement between two lenient readers is not evidence of anything,
130
+ // and why the comparison has to run on the same bytes.
131
+ const record = jcsParse(bytes);
113
132
  if (record === null || typeof record !== "object")
114
133
  return null;
115
134
  return { record, bytes };
package/dist/cli.js CHANGED
@@ -11,9 +11,29 @@
11
11
  // 0 verified
12
12
  // 1 a real verification failure (bad signature / tamper / broken inclusion)
13
13
  // 2 malformed bundle (unparseable, unknown spec major, body_hash mismatch)
14
- // 3 gateway-unavailable when an on-chain re-fetch was requested
14
+ // 3 UNDETERMINED: a gateway was unreachable when an on-chain re-fetch was
15
+ // requested, or the PACK is `incomplete` (see below)
16
+ //
17
+ // ── The headline is the PACK verdict, not the bundle status (proof#110) ──
18
+ //
19
+ // This command used to print `result.status` as its headline and map it to an
20
+ // exit code directly. That is the verdict on the signed BUNDLE, and it is not
21
+ // the verdict the rest of the product prints. `gradePack` in `./grading.ts` is
22
+ // the kernel's one rule for what a verification SURFACE may claim about a pack,
23
+ // the browser verify page calls it, and this command referenced it zero times.
24
+ // So the same file read `Incomplete` on the page and `VERIFIED`, exit 0, at the
25
+ // terminal: two answers to one question, from one kernel, which is the exact
26
+ // defect `grading.ts` was brought into the kernel to end.
27
+ //
28
+ // The two enums stay distinct, deliberately (`grading.ts` says so in its own
29
+ // comments, and `evidence-bundle.md` §3 does not carry `incomplete`): the
30
+ // BUNDLE status is still reported, unchanged, on its own line. What changed is
31
+ // which of the two is the headline and which one the exit code follows.
32
+ //
15
33
  // `payloadHashOk === null` (a withheld record) is semantics-undetermined and
16
- // surfaces as a per-event note — it does NOT fail the run.
34
+ // surfaces as a per-event note, and it does NOT fail the run. It does hold the
35
+ // PACK at `incomplete`, because a record nobody hashed is a record nobody
36
+ // checked, and exit 3 is how this command says "undetermined".
17
37
  import { readFile, readdir, stat, writeFile } from "node:fs/promises";
18
38
  import { realpathSync } from "node:fs";
19
39
  import { join } from "node:path";
@@ -21,12 +41,16 @@ import { fileURLToPath } from "node:url";
21
41
  import { isAgentProofSpec, verifyAgentProofBundle } from "./agent-proof.js";
22
42
  import { CHAIN_READING_SCOPE, readChains, renderChainFinding } from "./chain.js";
23
43
  import { composeExport } from "./compose.js";
24
- import { utf8 } from "./crypto.js";
44
+ import { isHexField, utf8 } from "./crypto.js";
25
45
  import { verifyEvidenceBundle } from "./evidence.js";
46
+ import { checkDeclaration, renderDeclarationSummary, renderNote } from "./declaration.js";
47
+ import { completenessNotes, gradePack, renderCause } from "./grading.js";
26
48
  const EXIT_VERIFIED = 0;
27
49
  const EXIT_FAILED = 1;
28
50
  const EXIT_MALFORMED = 2;
29
- const EXIT_GATEWAY_UNAVAILABLE = 3;
51
+ // One code, two undetermined outcomes: a gateway we could not reach, and a pack
52
+ // whose records nobody hashed. Neither is an accusation, and neither is a pass.
53
+ const EXIT_UNDETERMINED = 3;
30
54
  const G = "\x1b[32m";
31
55
  const R = "\x1b[31m";
32
56
  const Y = "\x1b[33m";
@@ -153,8 +177,22 @@ export async function runCli(argv, io) {
153
177
  // evidence about the records in front of us, not a verdict: they are printed
154
178
  // and they never move `exit`, because a chain is not a completeness proof and
155
179
  // an exit code is exactly the kind of thing people read as one.
156
- const chains = await readChains(parsed);
157
- const exit = reportEvidence(result, bundlePath, gateways, io, chains);
180
+ //
181
+ // A READING THAT COULD NOT BE PRODUCED SAYS SO. This was an unguarded call:
182
+ // a throw out of `readChains` would have escaped the command entirely and
183
+ // exited 1, which is the code for "failed" and would have named the
184
+ // customer's evidence tampered over a fault in this reader. The Python twin
185
+ // had the opposite half of the same defect and swallowed the throw into "no
186
+ // event chain was found to follow in this bundle" (the gate on proof#154).
187
+ // Neither kernel is entitled to either answer, so both now print the third.
188
+ let chains;
189
+ try {
190
+ chains = await readChains(parsed);
191
+ }
192
+ catch (e) {
193
+ chains = { unreadable: errMsg(e) };
194
+ }
195
+ const exit = reportEvidence(result, bundlePath, gateways, io, chains, parsed, logs);
158
196
  // Coverage is reported only once the bundle parsed far enough to have events
159
197
  // to compare against; over a MALFORMED bundle there is nothing to say.
160
198
  if (logs && result.status !== "malformed")
@@ -552,12 +590,28 @@ function sample(names, max = 3) {
552
590
  return names.length > max ? `${shown}, +${names.length - max} more` : shown;
553
591
  }
554
592
  // The --logs MAP lane decides each disclosed value's encoding: a clean,
555
- // even-length, lowercase-hex string is hex bytes; anything else is utf8 text.
556
- // (The verifier itself only distinguishes "string ⇒ hex" from "Uint8Array ⇒
557
- // raw bytes" — the human-facing encoding choice is made here. The DIRECTORY
558
- // lane never reaches this: its bytes are already bytes.)
593
+ // even-length hex string is hex bytes; anything else is utf8 text. (The
594
+ // verifier itself only distinguishes "string ⇒ hex" from "Uint8Array ⇒ raw
595
+ // bytes", so the human-facing encoding choice is made here. The DIRECTORY lane
596
+ // never reaches this: its bytes are already bytes.)
597
+ //
598
+ // EITHER CASE, through the one charset rule (proof#146). This test was
599
+ // `/^[0-9a-f]+$/`, lower case only, and it was the last copy of the hex rule
600
+ // in either kernel that answered differently from the others: the Python CLI
601
+ // passes every string to the kernel, and the kernel decodes it with the
602
+ // case-open `hexToBytes` on BOTH sides. So a disclosure map written in
603
+ // upper-case hex was read as 4 bytes of evidence there and as 8 bytes of TEXT
604
+ // here, and this command told the auditor their genuine, correctly disclosed
605
+ // evidence had FAILED: `proof verify --logs map.json` exited 3 in Python and
606
+ // 1 here, on one pack and one map. Telling an auditor their evidence is forged
607
+ // is the worst direction to be wrong in, which is the argument the Python
608
+ // twin's own docstring already makes about this exact lane.
609
+ //
610
+ // Pinned by `cross-kernel/run_cli.sh`'s `logs/hex-map-upper`, beside the
611
+ // lowercase lane that existed before and only ever exercised the one spelling
612
+ // the two commands happened to agree on.
559
613
  function decodeLogValue(v) {
560
- if (v.length > 0 && v.length % 2 === 0 && /^[0-9a-f]+$/.test(v))
614
+ if (v.length > 0 && v.length % 2 === 0 && isHexField(v, v.length))
561
615
  return v; // hex passthrough
562
616
  return utf8(v); // raw utf8 bytes
563
617
  }
@@ -591,9 +645,47 @@ function printUsage(io) {
591
645
  io.out(" and takes precedence. An event with no disclosed");
592
646
  io.out(" bytes is undetermined (~), never a failure.");
593
647
  io.out("");
594
- io.out("Exit codes: 0 verified · 1 failed · 2 malformed · 3 gateway-unavailable");
648
+ io.out("The headline is the PACK verdict, the same one the verify page prints:");
649
+ io.out("a record nobody hashed leaves the pack INCOMPLETE even when the signed");
650
+ io.out("bundle itself is verified. Both verdicts are printed when they differ.");
651
+ io.out("");
652
+ io.out("Exit codes: 0 verified · 1 failed · 2 malformed · 3 undetermined");
653
+ io.out(" (an unreachable gateway, or an incomplete pack)");
654
+ }
655
+ /**
656
+ * The disclosure account this command can honestly make, from what the VERIFIER
657
+ * determined rather than from what the loader happened to read.
658
+ *
659
+ * `accountDisclosure` is offered by `grading.ts` as a convenience and is
660
+ * explicitly "NOT THE ONLY WAY TO BUILD AN ACCOUNT". It reads the side map
661
+ * only, and this command has a second disclosure lane the side map cannot see:
662
+ * `events[].content`, carried INSIDE the bundle and bound by the verifier
663
+ * without any `--logs` at all. Accounting off the side map would file every
664
+ * in-body disclosure as `missing` and print a shortfall the pack does not have,
665
+ * which is the shape of manufactured accusation `grading.ts` keeps warning
666
+ * about.
667
+ *
668
+ * So `present` is "the verifier reached a determination for this record's
669
+ * content" and `missing` is "it could not", which is the same tri-state that
670
+ * decides the verdict, so the causes printed can never contradict the headline.
671
+ * `extra` still comes from the side input, because bytes that bound to nothing
672
+ * are a fact about the pack that only the loader knows.
673
+ */
674
+ function cliDisclosureAccount(result, logs) {
675
+ const expected = result.events.map((e) => e.eventId);
676
+ const known = new Set(expected);
677
+ return {
678
+ expected,
679
+ present: result.events.filter((e) => e.contentOk !== null).map((e) => e.eventId),
680
+ missing: result.events.filter((e) => e.contentOk === null).map((e) => e.eventId),
681
+ // The loader hard-stops (exit 2) on a file it selected and could not read,
682
+ // and NAMES everything it skipped as a note, so nothing reaches here as an
683
+ // unusable disclosure bound to a named record.
684
+ unreadable: [],
685
+ extra: (logs?.suppliedIds ?? []).filter((id) => !known.has(id)),
686
+ };
595
687
  }
596
- function reportEvidence(result, path, gateways, io, chains) {
688
+ function reportEvidence(result, path, gateways, io, chains, parsed, logs) {
597
689
  if (result.status === "malformed") {
598
690
  io.err(paint("MALFORMED", R) + ` ${path}`);
599
691
  for (const e of result.errors)
@@ -669,16 +761,40 @@ function reportEvidence(result, path, gateways, io, chains) {
669
761
  if (result.export) {
670
762
  reportExport(result.export, io);
671
763
  }
764
+ // What the pack SAYS it holds, against what it does (evidence-bundle.md
765
+ // §5.4). A pack that deviates from its own declaration FAILS here and exits
766
+ // 1, which is the point: every other check in this command is a check on a
767
+ // record that is present, so a trimmed hand-over passes all of them.
768
+ //
769
+ // A pack with NO declaration exits exactly as it did before §5.4. The check
770
+ // answers `partial` and the line below says so in words, and pack grading
771
+ // keeps its own verdict: every pack built before §5.4 carries no
772
+ // declaration, and re-grading them all as undetermined would be a change to
773
+ // the pinned 0/1/2/3 contract rather than a new check. A pack that DOES
774
+ // carry a declaration this reader cannot read is a different case and exits
775
+ // 3: the holder chose that field, so UNKNOWN is the honest answer.
776
+ const declaration = checkDeclaration(parsed, logs?.content);
777
+ const grade = gradePack(result, cliDisclosureAccount(result, logs), completenessNotes(parsed), declaration);
672
778
  io.out("");
673
- printRollup(result.status, result.assertedStatus, gateways, result.onChainChecked, io);
779
+ printRollup(grade.verdict, result.status, result.assertedStatus, gateways, result.onChainChecked, io);
780
+ io.out(paint(` declaration: ${renderDeclarationSummary(declaration)}`, declaration.verdict === "failed" ? R : declaration.verdict === "verified" ? DIM : Y));
781
+ for (const n of declaration.notes)
782
+ io.out(paint(` note: ${renderNote(n)}`, Y));
674
783
  for (const e of result.errors)
675
784
  io.out(paint(` note: ${e}`, DIM));
676
- const disclosed = result.events.filter((e) => e.contentOk !== null);
677
- if (disclosed.length > 0) {
678
- const verified = disclosed.filter((e) => e.contentOk === true).length;
679
- io.out(paint(` logs: ${verified}/${disclosed.length} disclosed verified`, DIM));
785
+ // Every reason the pack is not verified, in the kernel's own words. An
786
+ // `incomplete` with no cause is a surface saying "unknown" and unable to say
787
+ // why, which `grading.ts` calls out by name as a defect of its own.
788
+ for (const c of grade.causes)
789
+ io.out(paint(` - ${renderCause(c, "the pack")}`, Y));
790
+ // The fraction describes the PACK, not the disclosure. It used to divide by
791
+ // the number of records that happened to disclose bytes, so a pack that
792
+ // disclosed two of three records printed `logs: 2/2`, a full score for a
793
+ // partial hand-over, and the one line a reader skims for exactly this.
794
+ if (result.events.length > 0) {
795
+ io.out(paint(` logs: ${grade.recordsVerified}/${result.events.length} record(s) content-verified`, DIM));
680
796
  }
681
- return mapStatusToExit(result.status, result.errors);
797
+ return mapGradeToExit(grade, result);
682
798
  }
683
799
  /**
684
800
  * The chain block: what following each record's pointer to the record before it
@@ -700,6 +816,13 @@ function reportEvidence(result, path, gateways, io, chains) {
700
816
  * decision this check is entitled to make.
701
817
  */
702
818
  function reportChains(reading, io) {
819
+ if ("unreadable" in reading) {
820
+ io.out("");
821
+ io.out(paint(` Event chains this kernel could not read the event chains in this bundle ` +
822
+ `(${reading.unreadable}), so the chain evidence is UNKNOWN here. That is not ` +
823
+ `the same as there being none to read.`, Y));
824
+ return;
825
+ }
703
826
  if (reading.chains.length === 0)
704
827
  return;
705
828
  io.out("");
@@ -744,34 +867,65 @@ function reportAgentProof(result, path, gateways, io) {
744
867
  const onChain = result.onChainOk === null ? "" : ` on-chain ${mark(result.onChainOk)}`;
745
868
  io.out(` leaf index ${result.leafIndex} / ${result.leafCount ?? "?"} inclusion ${mark(result.inclusionOk)}${onChain}`);
746
869
  io.out("");
747
- printRollup(result.status, null, gateways, result.onChainChecked, io);
870
+ // An agent inclusion proof carries no records and no disclosed bytes, so
871
+ // there is no pack to grade: the bundle status IS the headline here.
872
+ printRollup(result.status, result.status, null, gateways, result.onChainChecked, io);
748
873
  for (const e of result.errors)
749
874
  io.out(paint(` note: ${e}`, DIM));
750
875
  return mapStatusToExit(result.status, result.errors);
751
876
  }
752
- function printRollup(status, asserted, gateways, onChainChecked, io) {
877
+ function printRollup(headline, bundleStatus, asserted, gateways, onChainChecked, io) {
753
878
  const scope = onChainChecked
754
879
  ? `verified offline + on-chain against ${gateways.length} gateway(s)`
755
880
  : "verified offline (no gateway re-fetch requested)";
756
881
  let head;
757
- switch (status) {
882
+ switch (headline) {
758
883
  case "verified":
759
884
  head = paint("VERIFIED", G);
760
885
  break;
886
+ // Undetermined, in both of its words. Neither is an accusation, so neither
887
+ // is printed in the color this command uses for one.
761
888
  case "partial":
762
- head = paint("PARTIAL", Y);
889
+ case "pending":
890
+ case "incomplete":
891
+ head = paint(headline.toUpperCase(), Y);
763
892
  break;
764
893
  case "failed":
765
894
  head = paint("FAILED", R);
766
895
  break;
767
896
  default:
768
- head = paint(status.toUpperCase(), R);
897
+ head = paint(headline.toUpperCase(), R);
769
898
  }
770
899
  io.out(`${head} ${paint(scope, DIM)}`);
771
- if (asserted && asserted !== status) {
772
- io.out(paint(` (producer asserted "${asserted}"; recomputed verdict is "${status}" — recomputed wins)`, DIM));
900
+ // Both verdicts, whenever they differ, because they answer different
901
+ // questions: §3 grades the signed bundle, the pack verdict grades the bundle
902
+ // plus whatever bytes travelled beside it. A pack can be incomplete while the
903
+ // bundle it carries is impeccable, and a reader told only the headline would
904
+ // read a sound bundle as a broken one.
905
+ if (headline !== bundleStatus) {
906
+ io.out(paint(` (the signed bundle is "${bundleStatus}"; the pack around it is "${headline}")`, DIM));
907
+ }
908
+ if (asserted && asserted !== bundleStatus) {
909
+ io.out(paint(` (the asserting party said "${asserted}"; the recomputed verdict is "${bundleStatus}", ` +
910
+ `and the recomputed verdict wins)`, DIM));
773
911
  }
774
912
  }
913
+ /**
914
+ * The exit code follows the PACK verdict, then falls back to the bundle status.
915
+ *
916
+ * `incomplete` is 3 rather than 0: it is undetermined, which is what 3 already
917
+ * meant for an unreachable gateway, and it is emphatically not 1: the bytes
918
+ * being absent is never reported as tamper (`evidence-bundle.md` §5.1).
919
+ */
920
+ function mapGradeToExit(grade, result) {
921
+ if (grade.verdict === "malformed")
922
+ return EXIT_MALFORMED;
923
+ if (grade.verdict === "failed")
924
+ return EXIT_FAILED;
925
+ if (grade.verdict === "incomplete")
926
+ return EXIT_UNDETERMINED;
927
+ return mapStatusToExit(result.status, result.errors);
928
+ }
775
929
  function mapStatusToExit(status, errors) {
776
930
  if (status === "failed")
777
931
  return EXIT_FAILED;
@@ -782,7 +936,7 @@ function mapStatusToExit(status, errors) {
782
936
  // §5 step 3) — maps to exit 3 (undetermined, network-dependent).
783
937
  if (status === "partial" &&
784
938
  errors.some((e) => /unreachable|could not be re-fetched|unavailable offline/.test(e))) {
785
- return EXIT_GATEWAY_UNAVAILABLE;
939
+ return EXIT_UNDETERMINED;
786
940
  }
787
941
  // "verified" → 0. "partial" from a withheld record (semantics-undetermined,
788
942
  // not a failure) also exits 0 — the bundle is cryptographically sound.
package/dist/crypto.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export declare function bytesToHex(bytes: Uint8Array): string;
2
2
  export declare function hexToBytes(hex: string): Uint8Array;
3
+ export declare function isHexField(value: unknown, nibbles: number, lower?: boolean): value is string;
3
4
  export declare function utf8(s: string): Uint8Array;
4
5
  export declare function sha256Bytes(bytes: Uint8Array): Promise<Uint8Array>;
5
6
  export declare function sha256Hex(bytes: Uint8Array): Promise<string>;
package/dist/crypto.js CHANGED
@@ -20,6 +20,15 @@ export function bytesToHex(bytes) {
20
20
  return out;
21
21
  }
22
22
  export function hexToBytes(hex) {
23
+ // The type says `string` and a bundle is not typed: every caller here reads
24
+ // a field a party wrote, so `undefined` and `null` arrive at runtime. Until
25
+ // gate 4 on proof#140 this line threw the HOST's `Cannot read properties of
26
+ // undefined (reading 'length')` where the Python kernel raises
27
+ // `hexToBytes: not a string`, and a finding that wraps it (`inclusion verify
28
+ // error: ...`) then read two ways. This kernel's own words, in the other
29
+ // kernel's wording, so a finding may quote them.
30
+ if (typeof hex !== "string")
31
+ throw new Error("hexToBytes: not a string");
23
32
  if (hex.length % 2 !== 0)
24
33
  throw new Error("hexToBytes: odd-length string");
25
34
  // Full validation: parseInt() partially parses ("1g" -> 1), which would let
@@ -32,6 +41,26 @@ export function hexToBytes(hex) {
32
41
  }
33
42
  return out;
34
43
  }
44
+ // The one hex-field test, beside the one hex reader above. The Python twin is
45
+ // `is_hex_field` in `src/sanning_proof/hex.py`, and every site in either kernel
46
+ // that asks whether a producer's hex is the shape a spec fixes goes through one
47
+ // of the two.
48
+ //
49
+ // The test runs BEFORE any decoder sees the value, so the failure a reader is
50
+ // handed names the field's FORM rather than whatever the decoder happened to
51
+ // say about it: `hexToBytes: non-hex characters` on a respelled key sends an
52
+ // auditor looking for a broken tool, and `Ed25519 signature verification
53
+ // failed` sends them looking for a forgery. Neither is what happened.
54
+ //
55
+ // CASE IS A PARAMETER AND NOT THE DEFAULT. proof#135 (option B, Phil
56
+ // 2026-09-22) pinned the hex charset and left the case open, so `lower` is
57
+ // passed only where a value is compared against a RECOMPUTED lowercase digest
58
+ // and no other spelling of it could ever be that digest.
59
+ export function isHexField(value, nibbles, lower = false) {
60
+ if (typeof value !== "string" || value.length !== nibbles)
61
+ return false;
62
+ return (lower ? /^[0-9a-f]*$/ : /^[0-9a-fA-F]*$/).test(value);
63
+ }
35
64
  export function utf8(s) {
36
65
  return new TextEncoder().encode(s);
37
66
  }