@verax-ai/proxy 0.8.1 → 0.9.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.
package/dist/anchors.d.ts CHANGED
@@ -1,20 +1,43 @@
1
1
  export declare const ANCHORS_NAME = "checkpoint-anchors.jsonl";
2
- /** `register`: the SCITT digest route; the log's leaf entry is SHA-256 of the 32 checkpoint-hash octets. */
3
- export type AnchorRoute = "register";
4
- export type AnchorRow = {
2
+ /**
3
+ * `register`: the digest route; the log's leaf entry is SHA-256 of the 32
4
+ * checkpoint-hash octets. `entries`: SCRAPI; the witness registers a Signed
5
+ * Statement for the checkpoint and the row keeps it beside the receipt.
6
+ */
7
+ export type AnchorRoute = "register" | "entries";
8
+ /** A receipt from the digest route, over an RFC 9162 log. */
9
+ export type DigestAnchorRow = {
5
10
  v: 1;
6
11
  checkpointHash: string;
7
12
  service: string;
8
- route: AnchorRoute;
13
+ route: "register";
9
14
  entryHash: string;
10
15
  leafIndex: number;
11
16
  treeSize: number;
12
17
  receiptB64: string;
13
18
  atMs: number;
14
19
  };
20
+ /**
21
+ * A Signed Statement registered over SCRAPI and the receipt for it. The
22
+ * receipt commits to the SHA-256 of `statementB64`'s octets, and `entryId` is
23
+ * the service's name for the entry (a CCF transaction id).
24
+ */
25
+ export type StatementAnchorRow = {
26
+ v: 2;
27
+ checkpointHash: string;
28
+ service: string;
29
+ route: "entries";
30
+ entryId: string;
31
+ statementB64: string;
32
+ receiptB64: string;
33
+ atMs: number;
34
+ };
35
+ export type AnchorRow = DigestAnchorRow | StatementAnchorRow;
15
36
  export declare function anchorsPath(dir: string): string;
16
- /** The leaf entry the service's log holds for this checkpoint under `route`. */
37
+ /** The leaf entry the digest route's log holds for this checkpoint. */
17
38
  export declare function anchorEntryHash(checkpointHash: string, route: AnchorRoute): string;
39
+ /** A CCF transaction id, `<view>.<seqno>`, as SCRAPI names an entry. */
40
+ export declare function isEntryId(value: unknown): value is string;
18
41
  /** Rows in file order. A line that is not a row is named, not skipped silently. */
19
42
  export declare function readAnchors(dir: string): {
20
43
  present: boolean;
package/dist/anchors.js CHANGED
@@ -19,10 +19,11 @@ import { appendFileSync, existsSync, lstatSync, readFileSync } from "node:fs";
19
19
  import { join } from "node:path";
20
20
  export const ANCHORS_NAME = "checkpoint-anchors.jsonl";
21
21
  const HASH = /^[0-9a-f]{64}$/;
22
+ const ENTRY_ID = /^[0-9]{1,20}\.[0-9]{1,20}$/;
22
23
  export function anchorsPath(dir) {
23
24
  return join(dir, ANCHORS_NAME);
24
25
  }
25
- /** The leaf entry the service's log holds for this checkpoint under `route`. */
26
+ /** The leaf entry the digest route's log holds for this checkpoint. */
26
27
  export function anchorEntryHash(checkpointHash, route) {
27
28
  if (!HASH.test(checkpointHash))
28
29
  throw new Error("anchor-checkpoint-hash");
@@ -30,25 +31,37 @@ export function anchorEntryHash(checkpointHash, route) {
30
31
  return createHash("sha256").update(Buffer.from(checkpointHash, "hex")).digest("hex");
31
32
  throw new Error(`anchor-route-${String(route)}`);
32
33
  }
34
+ /** A CCF transaction id, `<view>.<seqno>`, as SCRAPI names an entry. */
35
+ export function isEntryId(value) {
36
+ return typeof value === "string" && ENTRY_ID.test(value);
37
+ }
33
38
  function asRow(value) {
34
39
  if (!value || typeof value !== "object" || Array.isArray(value))
35
40
  return null;
36
41
  const r = value;
37
- if (r.v !== 1 || r.route !== "register")
38
- return null;
39
42
  if (typeof r.checkpointHash !== "string" || !HASH.test(r.checkpointHash))
40
43
  return null;
41
- if (typeof r.entryHash !== "string" || !HASH.test(r.entryHash))
42
- return null;
43
44
  if (typeof r.service !== "string" || r.service === "")
44
45
  return null;
45
46
  if (typeof r.receiptB64 !== "string" || r.receiptB64 === "")
46
47
  return null;
47
- if (!Number.isSafeInteger(r.leafIndex) || !Number.isSafeInteger(r.treeSize))
48
- return null;
49
48
  if (typeof r.atMs !== "number")
50
49
  return null;
51
- return r;
50
+ if (r.v === 1 && r.route === "register") {
51
+ if (typeof r.entryHash !== "string" || !HASH.test(r.entryHash))
52
+ return null;
53
+ if (!Number.isSafeInteger(r.leafIndex) || !Number.isSafeInteger(r.treeSize))
54
+ return null;
55
+ return r;
56
+ }
57
+ if (r.v === 2 && r.route === "entries") {
58
+ if (!isEntryId(r.entryId))
59
+ return null;
60
+ if (typeof r.statementB64 !== "string" || r.statementB64 === "")
61
+ return null;
62
+ return r;
63
+ }
64
+ return null;
52
65
  }
53
66
  /** Rows in file order. A line that is not a row is named, not skipped silently. */
54
67
  export function readAnchors(dir) {
package/dist/index.d.ts CHANGED
@@ -22,7 +22,8 @@ export type { ApprovalSignature, ControlSignature, OperatorRemovalInput, RevokeI
22
22
  export type { EffectSigner, ExplainChain, ExplainPair, ExplainFinding, ExplainOpts, ExplainResult, ExplainTrustRoot, ExplainWarning, ExtractWindow, Ledger, LedgerEffect, MediaContent, Policy, PolicyDecision, Principal, ProxyDeps, RecordSigner, ResultContent, TextContent, ToolCall, ToolResult, WitnessClass, } from "./types.ts";
23
23
  export { verifyLedger } from "./verify-ledger.ts";
24
24
  export type { VerifyResult, VerifyOptions, VerifyTrust, VerifyControl, VerifyInputs, VerifyAnchors, } from "./verify-ledger.ts";
25
- export { anchorEntryHash, appendAnchor, readAnchors } from "./anchors.ts";
26
- export type { AnchorRoute, AnchorRow } from "./anchors.ts";
27
- export { verifyReceipt } from "./transparency-receipt.ts";
28
- export type { ReceiptCheck, ReceiptStage } from "./transparency-receipt.ts";
25
+ export { anchorEntryHash, appendAnchor, isEntryId, readAnchors } from "./anchors.ts";
26
+ export type { AnchorRoute, AnchorRow, DigestAnchorRow, StatementAnchorRow } from "./anchors.ts";
27
+ export { checkpointStatement, readIssuerChain } from "./signed-statement.ts";
28
+ export { decodeCborItem, readCcfReceipt, verifyCcfReceipt, verifyReceipt } from "./transparency-receipt.ts";
29
+ export type { CcfReceiptCheck, CcfReceiptRead, ReceiptCheck, ReceiptStage } from "./transparency-receipt.ts";
package/dist/index.js CHANGED
@@ -21,5 +21,6 @@ export { signEffectAttestation } from "./ledger.js";
21
21
  export { spokenReason } from "./spoken-reason.js";
22
22
  export { OPERATOR_REMOVED_DIR, operatorRemovalMark } from "./operator-removal-sync.js";
23
23
  export { verifyLedger } from "./verify-ledger.js";
24
- export { anchorEntryHash, appendAnchor, readAnchors } from "./anchors.js";
25
- export { verifyReceipt } from "./transparency-receipt.js";
24
+ export { anchorEntryHash, appendAnchor, isEntryId, readAnchors } from "./anchors.js";
25
+ export { checkpointStatement, readIssuerChain } from "./signed-statement.js";
26
+ export { decodeCborItem, readCcfReceipt, verifyCcfReceipt, verifyReceipt } from "./transparency-receipt.js";
@@ -0,0 +1,26 @@
1
+ /** The DER certificates of a PEM chain, leaf first. A chain is a leaf and at least one CA. */
2
+ export declare function readIssuerChain(pem: string): Uint8Array[];
3
+ export declare function didX509Value(value: string): string;
4
+ /** did:x509:0:sha256:<root fingerprint>::subject:CN:<leaf CN>. */
5
+ export declare function didX509Issuer(chain: readonly Uint8Array[]): string;
6
+ /** The ledger a witness key signs for, as the statement's subject. */
7
+ export declare function ledgerSubject(publicKeyPem: string): string;
8
+ /** The Signed Statement for one checkpoint hash, signed by the witness key the chain's leaf holds. */
9
+ export declare function checkpointStatement(checkpointHash: string, chain: readonly Uint8Array[], keys: {
10
+ privateKeyPem: string;
11
+ publicKeyPem: string;
12
+ }): Uint8Array;
13
+ export type StatementRead = {
14
+ ok: true;
15
+ issuer: string;
16
+ subject: string;
17
+ } | {
18
+ ok: false;
19
+ reason: string;
20
+ };
21
+ /**
22
+ * Whether `bytes` is this ledger's Signed Statement for `checkpointHash`,
23
+ * signed by `publicKeyPem` with a chain whose leaf holds that key. The
24
+ * did:x509 itself is the service's to resolve when it registers the statement.
25
+ */
26
+ export declare function readCheckpointStatement(bytes: Uint8Array, checkpointHash: string, publicKeyPem: string): StatementRead;
@@ -0,0 +1,146 @@
1
+ /**
2
+ * A Signed Statement (RFC 9943) about one checkpoint, for a SCITT
3
+ * Transparency Service that registers statements over SCRAPI.
4
+ *
5
+ * The statement is a COSE Hash Envelope (draft-ietf-cose-hash-envelope): the
6
+ * payload is the 32-octet checkpoint hash, 258 names SHA-256 and 259 the
7
+ * preimage's type, a COSE_Sign1. Only the hash leaves the machine, as with the
8
+ * digest route; what changes is that the witness signs it as an Issuer.
9
+ *
10
+ * The Issuer is named in the CWT claims (15) as a did:x509 over the X.509
11
+ * chain the statement carries in x5chain (33), leaf first. The leaf must hold
12
+ * the witness key. Verax does not mint certificates; the operator issues the
13
+ * leaf for the witness public key and hands Verax the chain as PEM. The
14
+ * subject (CWT sub) is the ledger, named by the SHA-256 of the witness key.
15
+ */
16
+ import { createHash, createPublicKey, sign, verify, X509Certificate } from "node:crypto";
17
+ import { cborMap, encodeCbor } from "@cedulon/cose";
18
+ import { decodeSign1, sign1Structure } from "./transparency-receipt.js";
19
+ const HDR_ALG = 1;
20
+ const HDR_CWT_CLAIMS = 15;
21
+ const HDR_X5CHAIN = 33;
22
+ const HDR_PAYLOAD_HASH_ALG = 258;
23
+ const HDR_PREIMAGE_CONTENT_TYPE = 259;
24
+ const CWT_ISS = 1;
25
+ const CWT_SUB = 2;
26
+ const ALG_EDDSA = -8;
27
+ const HASH_SHA256 = -16;
28
+ const PREIMAGE_TYPE = "application/cose";
29
+ const HASH = /^[0-9a-f]{64}$/;
30
+ /** The DER certificates of a PEM chain, leaf first. A chain is a leaf and at least one CA. */
31
+ export function readIssuerChain(pem) {
32
+ const blocks = pem.match(/-----BEGIN CERTIFICATE-----[\s\S]*?-----END CERTIFICATE-----/g) ?? [];
33
+ if (blocks.length < 2)
34
+ throw new Error("issuer-chain-too-short");
35
+ return blocks.map((block) => {
36
+ try {
37
+ return new Uint8Array(new X509Certificate(block).raw);
38
+ }
39
+ catch {
40
+ throw new Error("issuer-chain-certificate");
41
+ }
42
+ });
43
+ }
44
+ // did:x509 keeps ALPHA / DIGIT / "-" / "." / "_" and percent-encodes the rest.
45
+ export function didX509Value(value) {
46
+ return Array.from(Buffer.from(value, "utf8"))
47
+ .map((b) => {
48
+ const c = String.fromCharCode(b);
49
+ return /[A-Za-z0-9._-]/.test(c) ? c : `%${b.toString(16).toUpperCase().padStart(2, "0")}`;
50
+ })
51
+ .join("");
52
+ }
53
+ /** did:x509:0:sha256:<root fingerprint>::subject:CN:<leaf CN>. */
54
+ export function didX509Issuer(chain) {
55
+ if (chain.length < 2)
56
+ throw new Error("issuer-chain-too-short");
57
+ const leaf = new X509Certificate(chain[0]);
58
+ const cn = leaf.subject
59
+ .split("\n")
60
+ .map((line) => line.trim())
61
+ .find((line) => line.startsWith("CN="));
62
+ if (!cn || cn.length === 3)
63
+ throw new Error("issuer-chain-leaf-cn");
64
+ const fingerprint = createHash("sha256").update(chain[chain.length - 1]).digest("base64url");
65
+ return `did:x509:0:sha256:${fingerprint}::subject:CN:${didX509Value(cn.slice(3))}`;
66
+ }
67
+ function spki(publicKeyPem) {
68
+ return createPublicKey(publicKeyPem).export({ type: "spki", format: "der" });
69
+ }
70
+ /** The ledger a witness key signs for, as the statement's subject. */
71
+ export function ledgerSubject(publicKeyPem) {
72
+ return `verax-ledger:${createHash("sha256").update(spki(publicKeyPem)).digest("hex")}`;
73
+ }
74
+ /** The Signed Statement for one checkpoint hash, signed by the witness key the chain's leaf holds. */
75
+ export function checkpointStatement(checkpointHash, chain, keys) {
76
+ if (!HASH.test(checkpointHash))
77
+ throw new Error("statement-checkpoint-hash");
78
+ const leafKey = new X509Certificate(chain[0]).publicKey.export({ type: "spki", format: "der" });
79
+ if (!leafKey.equals(spki(keys.publicKeyPem)))
80
+ throw new Error("issuer-chain-leaf-key");
81
+ const protectedBytes = encodeCbor(cborMap([
82
+ [HDR_ALG, ALG_EDDSA],
83
+ [
84
+ HDR_CWT_CLAIMS,
85
+ cborMap([
86
+ [CWT_ISS, didX509Issuer(chain)],
87
+ [CWT_SUB, ledgerSubject(keys.publicKeyPem)],
88
+ ]),
89
+ ],
90
+ [HDR_X5CHAIN, chain.map((der) => new Uint8Array(der))],
91
+ [HDR_PAYLOAD_HASH_ALG, HASH_SHA256],
92
+ [HDR_PREIMAGE_CONTENT_TYPE, PREIMAGE_TYPE],
93
+ ]));
94
+ const payload = new Uint8Array(Buffer.from(checkpointHash, "hex"));
95
+ const signature = new Uint8Array(sign(null, sign1Structure(protectedBytes, payload), keys.privateKeyPem));
96
+ // Tagged COSE_Sign1 (tag 18, one octet 0xd2): scitt-ccf-ledger refuses an untagged one.
97
+ return new Uint8Array(Buffer.concat([Buffer.of(0xd2), encodeCbor([protectedBytes, cborMap([]), payload, signature])]));
98
+ }
99
+ /**
100
+ * Whether `bytes` is this ledger's Signed Statement for `checkpointHash`,
101
+ * signed by `publicKeyPem` with a chain whose leaf holds that key. The
102
+ * did:x509 itself is the service's to resolve when it registers the statement.
103
+ */
104
+ export function readCheckpointStatement(bytes, checkpointHash, publicKeyPem) {
105
+ let sign1;
106
+ try {
107
+ sign1 = decodeSign1(bytes);
108
+ }
109
+ catch {
110
+ return { ok: false, reason: "the statement is not a COSE_Sign1" };
111
+ }
112
+ const h = sign1.protectedHeader;
113
+ if (h.get(HDR_ALG) !== ALG_EDDSA)
114
+ return { ok: false, reason: "the statement is not EdDSA" };
115
+ if (h.get(HDR_PAYLOAD_HASH_ALG) !== HASH_SHA256)
116
+ return { ok: false, reason: "the statement is not a SHA-256 hash envelope" };
117
+ const cwt = h.get(HDR_CWT_CLAIMS);
118
+ const issuer = cwt instanceof Map ? cwt.get(CWT_ISS) : undefined;
119
+ const subject = cwt instanceof Map ? cwt.get(CWT_SUB) : undefined;
120
+ if (typeof issuer !== "string" || !issuer.startsWith("did:x509:") || typeof subject !== "string") {
121
+ return { ok: false, reason: "the statement names no did:x509 issuer and subject" };
122
+ }
123
+ if (!HASH.test(checkpointHash) || !(sign1.payload instanceof Uint8Array) || !Buffer.from(sign1.payload).equals(Buffer.from(checkpointHash, "hex"))) {
124
+ return { ok: false, reason: "the statement names another checkpoint" };
125
+ }
126
+ let signed = false;
127
+ try {
128
+ signed = verify(null, sign1Structure(sign1.protectedBytes, sign1.payload), publicKeyPem, sign1.signature);
129
+ }
130
+ catch {
131
+ signed = false;
132
+ }
133
+ if (!signed)
134
+ return { ok: false, reason: "the statement does not verify under the checkpoint key" };
135
+ const x5chain = h.get(HDR_X5CHAIN);
136
+ const leaf = Array.isArray(x5chain) ? x5chain[0] : x5chain;
137
+ try {
138
+ if (!(leaf instanceof Uint8Array) || !new X509Certificate(leaf).publicKey.export({ type: "spki", format: "der" }).equals(spki(publicKeyPem))) {
139
+ return { ok: false, reason: "the statement's x5chain leaf holds another key" };
140
+ }
141
+ }
142
+ catch {
143
+ return { ok: false, reason: "the statement's x5chain leaf is not a certificate" };
144
+ }
145
+ return { ok: true, issuer, subject };
146
+ }
@@ -26,6 +26,7 @@ export type ReceiptCheck = {
26
26
  root: string;
27
27
  treeSize: number;
28
28
  leafIndex: number;
29
+ issuedAtSec?: number;
29
30
  } | {
30
31
  ok: false;
31
32
  stage: ReceiptStage;
@@ -36,3 +37,40 @@ export type ReceiptCheck = {
36
37
  * holds. vds is read from the protected header only.
37
38
  */
38
39
  export declare function verifyReceipt(receipt: Uint8Array, leafEntry: Uint8Array, publicKeyPem: string): ReceiptCheck;
40
+ export type CcfReceiptCheck = {
41
+ ok: true;
42
+ root: string;
43
+ commitEvidence: string;
44
+ } | {
45
+ ok: false;
46
+ stage: ReceiptStage;
47
+ reason: string;
48
+ };
49
+ /**
50
+ * Checks one receipt from a CCF ledger (vds 2, CCF_LEDGER_SHA256) for one
51
+ * claims digest under the service key the reader holds. scitt-ccf-ledger
52
+ * commits to the SHA-256 of the Signed Statement it registered.
53
+ *
54
+ * The inclusion proof at 396/-1 is the CBOR map {1: leaf, 2: path}. The leaf
55
+ * is [internal transaction hash, commit evidence, claims digest] and hashes as
56
+ * SHA-256(hash || SHA-256(evidence) || digest); each path step is
57
+ * [left, node], hashed as SHA-256(node || acc) when left and SHA-256(acc ||
58
+ * node) otherwise. The signature is over the root that yields.
59
+ */
60
+ export declare function verifyCcfReceipt(receipt: Uint8Array, claimsDigest: Uint8Array, publicKeyPem: string): CcfReceiptCheck;
61
+ export type CcfReceiptRead = {
62
+ ok: true;
63
+ root: Buffer;
64
+ commitEvidence: string;
65
+ sign1: Sign1;
66
+ } | {
67
+ ok: false;
68
+ stage: ReceiptStage;
69
+ reason: string;
70
+ };
71
+ /**
72
+ * The root a CCF receipt yields for `claimsDigest`, with no key: a receipt
73
+ * that is not for this digest is refused here, and only the signature is left
74
+ * for `verifyCcfReceipt`.
75
+ */
76
+ export declare function readCcfReceipt(receipt: Uint8Array, claimsDigest: Uint8Array): CcfReceiptRead;
@@ -273,5 +273,107 @@ export function verifyReceipt(receipt, leafEntry, publicKeyPem) {
273
273
  }
274
274
  if (!verified)
275
275
  return { ok: false, stage: "receipt-sig", reason: "signature does not verify over the rebuilt root" };
276
- return { ok: true, root: root.toString("hex"), treeSize, leafIndex };
276
+ // Optional CWT Claims (COSE header 15 / RFC 9597) with iat (CWT claim 6).
277
+ let issuedAtSec;
278
+ const cwt = sign1.protectedHeader.get(15);
279
+ if (cwt instanceof Map) {
280
+ const iat = cwt.get(6);
281
+ if (typeof iat === "number" && Number.isSafeInteger(iat))
282
+ issuedAtSec = iat;
283
+ }
284
+ return issuedAtSec === undefined
285
+ ? { ok: true, root: root.toString("hex"), treeSize, leafIndex }
286
+ : { ok: true, root: root.toString("hex"), treeSize, leafIndex, issuedAtSec };
287
+ }
288
+ /**
289
+ * Checks one receipt from a CCF ledger (vds 2, CCF_LEDGER_SHA256) for one
290
+ * claims digest under the service key the reader holds. scitt-ccf-ledger
291
+ * commits to the SHA-256 of the Signed Statement it registered.
292
+ *
293
+ * The inclusion proof at 396/-1 is the CBOR map {1: leaf, 2: path}. The leaf
294
+ * is [internal transaction hash, commit evidence, claims digest] and hashes as
295
+ * SHA-256(hash || SHA-256(evidence) || digest); each path step is
296
+ * [left, node], hashed as SHA-256(node || acc) when left and SHA-256(acc ||
297
+ * node) otherwise. The signature is over the root that yields.
298
+ */
299
+ export function verifyCcfReceipt(receipt, claimsDigest, publicKeyPem) {
300
+ const read = readCcfReceipt(receipt, claimsDigest);
301
+ if (!read.ok)
302
+ return read;
303
+ let verified = false;
304
+ try {
305
+ verified = verifyCoseSignature(read.sign1.protectedHeader.get(1), publicKeyPem, sign1Structure(read.sign1.protectedBytes, read.root), read.sign1.signature);
306
+ }
307
+ catch {
308
+ verified = false;
309
+ }
310
+ if (!verified)
311
+ return { ok: false, stage: "receipt-sig", reason: "signature does not verify over the rebuilt root" };
312
+ return { ok: true, root: read.root.toString("hex"), commitEvidence: read.commitEvidence };
313
+ }
314
+ /**
315
+ * The root a CCF receipt yields for `claimsDigest`, with no key: a receipt
316
+ * that is not for this digest is refused here, and only the signature is left
317
+ * for `verifyCcfReceipt`.
318
+ */
319
+ export function readCcfReceipt(receipt, claimsDigest) {
320
+ if (claimsDigest.length !== 32)
321
+ return { ok: false, stage: "inclusion", reason: "claims digest is not 32 octets" };
322
+ let sign1;
323
+ try {
324
+ sign1 = decodeSign1(receipt);
325
+ }
326
+ catch (err) {
327
+ return { ok: false, stage: "receipt-decode", reason: err instanceof Error ? err.message : String(err) };
328
+ }
329
+ const crit = sign1.protectedHeader.get(2);
330
+ if (crit !== undefined && (!Array.isArray(crit) || crit.length === 0 || !crit.every((l) => UNDERSTOOD.has(l)))) {
331
+ return { ok: false, stage: "receipt-decode", reason: "a critical header this reader does not understand" };
332
+ }
333
+ if (sign1.unprotected.has(2))
334
+ return { ok: false, stage: "receipt-decode", reason: "crit in the unprotected header" };
335
+ const vds = sign1.protectedHeader.get(395);
336
+ if (vds !== 2)
337
+ return { ok: false, stage: "vds-gate", reason: `vds ${String(vds)} is not CCF_LEDGER_SHA256` };
338
+ const vdp = sign1.unprotected.get(396);
339
+ const proofs = vdp instanceof Map ? vdp.get(-1) : undefined;
340
+ if (!Array.isArray(proofs) || proofs.length !== 1 || !(proofs[0] instanceof Uint8Array)) {
341
+ return { ok: false, stage: "receipt-decode", reason: "no single inclusion proof at 396/-1" };
342
+ }
343
+ if (sign1.payload !== null)
344
+ return { ok: false, stage: "receipt-decode", reason: "receipt payload is not detached" };
345
+ let proof;
346
+ try {
347
+ proof = decodeCborItem(proofs[0]);
348
+ }
349
+ catch (err) {
350
+ return { ok: false, stage: "receipt-decode", reason: err instanceof Error ? err.message : String(err) };
351
+ }
352
+ const leaf = proof instanceof Map ? proof.get(1) : undefined;
353
+ const path = proof instanceof Map ? proof.get(2) : undefined;
354
+ if (!Array.isArray(leaf) || leaf.length !== 3 || !Array.isArray(path)) {
355
+ return { ok: false, stage: "receipt-decode", reason: "inclusion proof shape" };
356
+ }
357
+ const [internalHash, evidence, leafDigest] = leaf;
358
+ if (!(internalHash instanceof Uint8Array) ||
359
+ internalHash.length !== 32 ||
360
+ typeof evidence !== "string" ||
361
+ !(leafDigest instanceof Uint8Array) ||
362
+ leafDigest.length !== 32) {
363
+ return { ok: false, stage: "receipt-decode", reason: "inclusion proof leaf members" };
364
+ }
365
+ const steps = [];
366
+ for (const step of path) {
367
+ if (!Array.isArray(step) || step.length !== 2 || typeof step[0] !== "boolean" || !(step[1] instanceof Uint8Array) || step[1].length !== 32) {
368
+ return { ok: false, stage: "receipt-decode", reason: "inclusion proof path members" };
369
+ }
370
+ steps.push([step[0], step[1]]);
371
+ }
372
+ if (!Buffer.from(leafDigest).equals(Buffer.from(claimsDigest))) {
373
+ return { ok: false, stage: "inclusion", reason: "the receipt commits to another claims digest" };
374
+ }
375
+ let root = sha256(internalHash, sha256(Buffer.from(evidence, "utf8")), leafDigest);
376
+ for (const [left, node] of steps)
377
+ root = left ? sha256(node, root) : sha256(root, node);
378
+ return { ok: true, root, commitEvidence: evidence, sign1 };
277
379
  }
@@ -1,3 +1,4 @@
1
+ import { type SignedCheckpoint } from "@cedulon/checkpoint";
1
2
  import type { SignedDecisionRecord } from "@cedulon/core";
2
3
  export type VerifyTrust = {
3
4
  /** `pinned`: the caller supplied the key. `in-ledger`: read from the records. */
@@ -46,6 +47,17 @@ export type VerifyResult = {
46
47
  /** Transparency Service receipts for checkpoints, when the ledger holds any. */
47
48
  anchors: VerifyAnchors;
48
49
  problems: string[];
50
+ /**
51
+ * Decision records whose signatures verified under the key used for this run.
52
+ * Empty when none verified. Added for readers that must count only accepted
53
+ * rows; does not change ok, problems, or any existing field.
54
+ */
55
+ attestedDecisions: SignedDecisionRecord[];
56
+ /**
57
+ * Checkpoint rows whose signatures verified under the key used for this run.
58
+ * Empty when none verified. Same additive contract as attestedDecisions.
59
+ */
60
+ attestedCheckpoints: SignedCheckpoint[];
49
61
  };
50
62
  export type VerifyAnchors = {
51
63
  line: string;
@@ -104,7 +104,7 @@
104
104
  * the answer rather than a footnote, and `publicKeyPem` lets a reader pin a
105
105
  * copy they hold themselves.
106
106
  */
107
- import { createPublicKey } from "node:crypto";
107
+ import { createHash, createPublicKey } from "node:crypto";
108
108
  import { samePublicKey, thirdPartyReceiptCoversRow } from "./third-party.js";
109
109
  import { existsSync, lstatSync, readFileSync } from "node:fs";
110
110
  import { join } from "node:path";
@@ -115,7 +115,8 @@ import { verifyEffectExtract } from "@cedulon/effect-extract";
115
115
  import { anchorEntryHash, readAnchors } from "./anchors.js";
116
116
  import { checkpointsPath, readCheckpointFile } from "./checkpoints.js";
117
117
  import { sha256Canonical } from "./hash.js";
118
- import { verifyReceipt } from "./transparency-receipt.js";
118
+ import { readCheckpointStatement } from "./signed-statement.js";
119
+ import { verifyCcfReceipt, verifyReceipt } from "./transparency-receipt.js";
119
120
  import { ledgerPiecePathProblem, readLedgerManifest, requireLedgerPiecePath, indexPath, manifestPath, parseIndexText, } from "./ledger-manifest.js";
120
121
  /**
121
122
  * A symbolic link is a named problem and is not read. Missing files are not
@@ -711,6 +712,7 @@ function tailStatement(dir, records, checkpointPublicKeyPem, attested = records)
711
712
  checkpointTrust: checkpointTrustOf(pinned, null, 0),
712
713
  problems: [checkpointLink],
713
714
  verified: [],
715
+ attestedRows: [],
714
716
  };
715
717
  }
716
718
  const loaded = readCheckpointFile(dir);
@@ -721,12 +723,14 @@ function tailStatement(dir, records, checkpointPublicKeyPem, attested = records)
721
723
  const checkpointTrust = checkpointTrustOf(pinned, taken, rows.length);
722
724
  const problems = [...loaded.problems];
723
725
  const verified = [];
726
+ const attestedRows = [];
724
727
  noteEd25519(key, problems);
725
728
  for (let i = 0; i < rows.length; i += 1) {
726
729
  if (!checkpointRowVerifies(rows[i], key)) {
727
730
  problems.push(`checkpoint signature does not verify: checkpoint ${i}`);
728
731
  continue;
729
732
  }
733
+ attestedRows.push(rows[i]);
730
734
  try {
731
735
  const count = rows[i].claims.receiptCount;
732
736
  const endMs = rows[i].claims.endMs;
@@ -734,6 +738,7 @@ function tailStatement(dir, records, checkpointPublicKeyPem, attested = records)
734
738
  hash: checkpointHash(rows[i]),
735
739
  receiptCount: typeof count === "number" ? count : null,
736
740
  endMs: typeof endMs === "number" ? endMs : null,
741
+ keyPem: key ?? "",
737
742
  });
738
743
  }
739
744
  catch {
@@ -758,7 +763,7 @@ function tailStatement(dir, records, checkpointPublicKeyPem, attested = records)
758
763
  const covered = brk ? rows.slice(0, brk.index) : rows;
759
764
  const newest = covered.length > 0 ? covered[covered.length - 1] : null;
760
765
  if (!newest) {
761
- return { tail: { line: TAIL_NONE, checkpoint: null }, checkpointTrust, problems, verified };
766
+ return { tail: { line: TAIL_NONE, checkpoint: null }, checkpointTrust, problems, verified, attestedRows };
762
767
  }
763
768
  const claims = newest.claims;
764
769
  const receiptCount = typeof claims.receiptCount === "number" ? claims.receiptCount : null;
@@ -789,6 +794,7 @@ function tailStatement(dir, records, checkpointPublicKeyPem, attested = records)
789
794
  checkpointTrust,
790
795
  problems,
791
796
  verified,
797
+ attestedRows,
792
798
  };
793
799
  }
794
800
  /**
@@ -855,26 +861,45 @@ function anchorStatement(dir, checkpoints, keyPem, problems, required = null) {
855
861
  problems.push(`anchor names a checkpoint the ledger does not hold: ${short}`);
856
862
  continue;
857
863
  }
858
- let entry;
859
- try {
860
- entry = anchorEntryHash(row.checkpointHash, row.route);
861
- }
862
- catch {
863
- problems.push(`anchor route is not one this verifier reads: ${short}`);
864
- continue;
865
- }
866
- if (entry !== row.entryHash) {
867
- problems.push(`anchor entry hash is not the checkpoint's: ${short}`);
868
- continue;
869
- }
870
- const check = verifyReceipt(Buffer.from(row.receiptB64, "base64"), Buffer.from(entry, "hex"), keyPem);
871
- if (!check.ok) {
872
- problems.push(`anchor receipt does not verify: ${short} (${check.stage}: ${check.reason})`);
873
- continue;
864
+ if (row.route === "entries") {
865
+ // The receipt commits to the Signed Statement, so the statement is
866
+ // checked first: it must name this checkpoint and be signed by the key
867
+ // the checkpoint verified under.
868
+ const statement = Buffer.from(row.statementB64, "base64");
869
+ const read = readCheckpointStatement(statement, row.checkpointHash, checkpoint.keyPem);
870
+ if (!read.ok) {
871
+ problems.push(`anchor statement is not this checkpoint's: ${short} (${read.reason})`);
872
+ continue;
873
+ }
874
+ const digest = createHash("sha256").update(statement).digest();
875
+ const check = verifyCcfReceipt(Buffer.from(row.receiptB64, "base64"), digest, keyPem);
876
+ if (!check.ok) {
877
+ problems.push(`anchor receipt does not verify: ${short} (${check.stage}: ${check.reason})`);
878
+ continue;
879
+ }
874
880
  }
875
- if (check.treeSize !== row.treeSize || check.leafIndex !== row.leafIndex) {
876
- problems.push(`anchor row disagrees with its receipt: ${short}`);
877
- continue;
881
+ else {
882
+ let entry;
883
+ try {
884
+ entry = anchorEntryHash(row.checkpointHash, row.route);
885
+ }
886
+ catch {
887
+ problems.push(`anchor route is not one this verifier reads: ${short}`);
888
+ continue;
889
+ }
890
+ if (entry !== row.entryHash) {
891
+ problems.push(`anchor entry hash is not the checkpoint's: ${short}`);
892
+ continue;
893
+ }
894
+ const check = verifyReceipt(Buffer.from(row.receiptB64, "base64"), Buffer.from(entry, "hex"), keyPem);
895
+ if (!check.ok) {
896
+ problems.push(`anchor receipt does not verify: ${short} (${check.stage}: ${check.reason})`);
897
+ continue;
898
+ }
899
+ if (check.treeSize !== row.treeSize || check.leafIndex !== row.leafIndex) {
900
+ problems.push(`anchor row disagrees with its receipt: ${short}`);
901
+ continue;
902
+ }
878
903
  }
879
904
  verified += 1;
880
905
  anchoredHashes.add(row.checkpointHash);
@@ -1071,6 +1096,8 @@ function unreadableResult(dir, problem) {
1071
1096
  inputs: INPUTS_UNCHECKED,
1072
1097
  anchors: ANCHORS_NONE,
1073
1098
  problems: [problem],
1099
+ attestedDecisions: [],
1100
+ attestedCheckpoints: [],
1074
1101
  };
1075
1102
  }
1076
1103
  function isDecisionRow(row) {
@@ -1154,6 +1181,8 @@ async function verifyLedgerUnchecked(dir, opts = {}) {
1154
1181
  inputs: INPUTS_UNCHECKED,
1155
1182
  anchors: ANCHORS_NONE,
1156
1183
  problems,
1184
+ attestedDecisions: [],
1185
+ attestedCheckpoints: [],
1157
1186
  };
1158
1187
  }
1159
1188
  const pinned = opts.publicKeyPem?.trim();
@@ -1469,5 +1498,7 @@ async function verifyLedgerUnchecked(dir, opts = {}) {
1469
1498
  inputs,
1470
1499
  anchors,
1471
1500
  problems,
1501
+ attestedDecisions: attested,
1502
+ attestedCheckpoints: tailed.attestedRows,
1472
1503
  };
1473
1504
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@verax-ai/proxy",
3
- "version": "0.8.1",
3
+ "version": "0.9.0",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "engines": {
@@ -19,12 +19,12 @@
19
19
  "policy"
20
20
  ],
21
21
  "dependencies": {
22
- "@cedulon/audit": "0.13.1",
23
- "@cedulon/checkpoint": "0.13.1",
24
- "@cedulon/core": "0.13.1",
25
- "@cedulon/cose": "0.13.1",
26
- "@cedulon/effect-extract": "0.13.1",
27
- "@cedulon/x402-adapter": "0.13.1",
22
+ "@cedulon/audit": "0.16.0",
23
+ "@cedulon/checkpoint": "0.16.0",
24
+ "@cedulon/core": "0.16.0",
25
+ "@cedulon/cose": "0.16.0",
26
+ "@cedulon/effect-extract": "0.16.0",
27
+ "@cedulon/x402-adapter": "0.16.0",
28
28
  "@conarium-ai/mask": "0.3.5"
29
29
  },
30
30
  "description": "The decision proxy behind VERAX: policy, signed decision records, and the ledger they live in.",