@verax-ai/proxy 0.4.2 → 0.4.3
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 +24 -0
- package/dist/anchors.js +86 -0
- package/dist/index.d.ts +6 -2
- package/dist/index.js +3 -1
- package/dist/transparency-receipt.d.ts +38 -0
- package/dist/transparency-receipt.js +277 -0
- package/dist/verify-ledger.d.ts +21 -0
- package/dist/verify-ledger.js +248 -8
- package/package.json +1 -1
|
@@ -0,0 +1,24 @@
|
|
|
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 = {
|
|
5
|
+
v: 1;
|
|
6
|
+
checkpointHash: string;
|
|
7
|
+
service: string;
|
|
8
|
+
route: AnchorRoute;
|
|
9
|
+
entryHash: string;
|
|
10
|
+
leafIndex: number;
|
|
11
|
+
treeSize: number;
|
|
12
|
+
receiptB64: string;
|
|
13
|
+
atMs: number;
|
|
14
|
+
};
|
|
15
|
+
export declare function anchorsPath(dir: string): string;
|
|
16
|
+
/** The leaf entry the service's log holds for this checkpoint under `route`. */
|
|
17
|
+
export declare function anchorEntryHash(checkpointHash: string, route: AnchorRoute): string;
|
|
18
|
+
/** Rows in file order. A line that is not a row is named, not skipped silently. */
|
|
19
|
+
export declare function readAnchors(dir: string): {
|
|
20
|
+
present: boolean;
|
|
21
|
+
rows: AnchorRow[];
|
|
22
|
+
problems: string[];
|
|
23
|
+
};
|
|
24
|
+
export declare function appendAnchor(dir: string, row: AnchorRow): void;
|
package/dist/anchors.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checkpoints registered with a Transparency Service run by someone else.
|
|
3
|
+
*
|
|
4
|
+
* `verax anchor` sends a service the SHA-256 of a checkpoint's COSE octets
|
|
5
|
+
* (the checkpoint hash core-03 defines) and nothing else: the window, the
|
|
6
|
+
* counts and the head stay on this machine. The service appends that digest to
|
|
7
|
+
* its RFC 9162 log and returns a COSE Receipt, which is kept here, one row per
|
|
8
|
+
* checkpoint and service, so `verax verify` can check it later with no network.
|
|
9
|
+
*
|
|
10
|
+
* What a verified receipt adds is a second party: the service's log held this
|
|
11
|
+
* checkpoint at the tree size in the receipt, under the service's key. A
|
|
12
|
+
* receipt does not show the service agrees with anything the checkpoint
|
|
13
|
+
* counts, and a reader who has only these files cannot tell whether a newer
|
|
14
|
+
* checkpoint was registered and then removed from them; asking the service is
|
|
15
|
+
* the check for that.
|
|
16
|
+
*/
|
|
17
|
+
import { createHash } from "node:crypto";
|
|
18
|
+
import { appendFileSync, existsSync, lstatSync, readFileSync } from "node:fs";
|
|
19
|
+
import { join } from "node:path";
|
|
20
|
+
export const ANCHORS_NAME = "checkpoint-anchors.jsonl";
|
|
21
|
+
const HASH = /^[0-9a-f]{64}$/;
|
|
22
|
+
export function anchorsPath(dir) {
|
|
23
|
+
return join(dir, ANCHORS_NAME);
|
|
24
|
+
}
|
|
25
|
+
/** The leaf entry the service's log holds for this checkpoint under `route`. */
|
|
26
|
+
export function anchorEntryHash(checkpointHash, route) {
|
|
27
|
+
if (!HASH.test(checkpointHash))
|
|
28
|
+
throw new Error("anchor-checkpoint-hash");
|
|
29
|
+
if (route === "register")
|
|
30
|
+
return createHash("sha256").update(Buffer.from(checkpointHash, "hex")).digest("hex");
|
|
31
|
+
throw new Error(`anchor-route-${String(route)}`);
|
|
32
|
+
}
|
|
33
|
+
function asRow(value) {
|
|
34
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
35
|
+
return null;
|
|
36
|
+
const r = value;
|
|
37
|
+
if (r.v !== 1 || r.route !== "register")
|
|
38
|
+
return null;
|
|
39
|
+
if (typeof r.checkpointHash !== "string" || !HASH.test(r.checkpointHash))
|
|
40
|
+
return null;
|
|
41
|
+
if (typeof r.entryHash !== "string" || !HASH.test(r.entryHash))
|
|
42
|
+
return null;
|
|
43
|
+
if (typeof r.service !== "string" || r.service === "")
|
|
44
|
+
return null;
|
|
45
|
+
if (typeof r.receiptB64 !== "string" || r.receiptB64 === "")
|
|
46
|
+
return null;
|
|
47
|
+
if (!Number.isSafeInteger(r.leafIndex) || !Number.isSafeInteger(r.treeSize))
|
|
48
|
+
return null;
|
|
49
|
+
if (typeof r.atMs !== "number")
|
|
50
|
+
return null;
|
|
51
|
+
return r;
|
|
52
|
+
}
|
|
53
|
+
/** Rows in file order. A line that is not a row is named, not skipped silently. */
|
|
54
|
+
export function readAnchors(dir) {
|
|
55
|
+
const path = anchorsPath(dir);
|
|
56
|
+
if (!existsSync(path))
|
|
57
|
+
return { present: false, rows: [], problems: [] };
|
|
58
|
+
if (lstatSync(path).isSymbolicLink())
|
|
59
|
+
return { present: true, rows: [], problems: [`symbolic link: ${path}`] };
|
|
60
|
+
const rows = [];
|
|
61
|
+
const problems = [];
|
|
62
|
+
const lines = readFileSync(path, "utf8").split("\n");
|
|
63
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
64
|
+
const line = lines[i].trim();
|
|
65
|
+
if (line === "")
|
|
66
|
+
continue;
|
|
67
|
+
let parsed;
|
|
68
|
+
try {
|
|
69
|
+
parsed = JSON.parse(line);
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
problems.push(`anchor line ${i + 1} is not JSON`);
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
const row = asRow(parsed);
|
|
76
|
+
if (!row) {
|
|
77
|
+
problems.push(`anchor line ${i + 1} is not an anchor row`);
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
rows.push(row);
|
|
81
|
+
}
|
|
82
|
+
return { present: true, rows, problems };
|
|
83
|
+
}
|
|
84
|
+
export function appendAnchor(dir, row) {
|
|
85
|
+
appendFileSync(anchorsPath(dir), `${JSON.stringify(row)}\n`, { encoding: "utf8" });
|
|
86
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ export { approvePending, approvalsLogFor, createApprovalBudgetGuard, enqueueAppr
|
|
|
7
7
|
export { loadDecisionRefsFromDir, loadEffectsFromDir, parseCardCsv, parseChannelJsonl, reconcile } from "./reconcile.ts";
|
|
8
8
|
export { tenantKey } from "./tenant.ts";
|
|
9
9
|
export { diskProbe } from "./disk.ts";
|
|
10
|
-
export { checkpointsPath } from "./checkpoints.ts";
|
|
10
|
+
export { checkpointsPath, readCheckpointFile } from "./checkpoints.ts";
|
|
11
11
|
export { signEffectAttestation } from "./ledger.ts";
|
|
12
12
|
export { spokenReason } from "./spoken-reason.ts";
|
|
13
13
|
export type { ApprovalRow, ApproveResult } from "./approvals.ts";
|
|
@@ -16,4 +16,8 @@ export type { FileLedgerOpts, LedgerCounts } from "./ledger.ts";
|
|
|
16
16
|
export type { ApprovalSignature } from "./types.ts";
|
|
17
17
|
export type { EffectSigner, ExplainChain, ExplainPair, ExplainFinding, ExplainOpts, ExplainResult, ExplainTrustRoot, ExplainWarning, ExtractWindow, Ledger, LedgerEffect, Policy, PolicyDecision, Principal, ProxyDeps, RecordSigner, ToolCall, ToolResult, WitnessClass, } from "./types.ts";
|
|
18
18
|
export { verifyLedger } from "./verify-ledger.ts";
|
|
19
|
-
export type { VerifyResult, VerifyOptions, VerifyTrust, VerifyControl } from "./verify-ledger.ts";
|
|
19
|
+
export type { VerifyResult, VerifyOptions, VerifyTrust, VerifyControl, VerifyInputs, VerifyAnchors, } from "./verify-ledger.ts";
|
|
20
|
+
export { anchorEntryHash, appendAnchor, readAnchors } from "./anchors.ts";
|
|
21
|
+
export type { AnchorRoute, AnchorRow } from "./anchors.ts";
|
|
22
|
+
export { verifyReceipt } from "./transparency-receipt.ts";
|
|
23
|
+
export type { ReceiptCheck, ReceiptStage } from "./transparency-receipt.ts";
|
package/dist/index.js
CHANGED
|
@@ -13,7 +13,9 @@ export { tenantKey } from "./tenant.js";
|
|
|
13
13
|
// through this package must be able to inject a full disk into the very
|
|
14
14
|
// instance the proxy uses. Deep imports are not in the exports map.
|
|
15
15
|
export { diskProbe } from "./disk.js";
|
|
16
|
-
export { checkpointsPath } from "./checkpoints.js";
|
|
16
|
+
export { checkpointsPath, readCheckpointFile } from "./checkpoints.js";
|
|
17
17
|
export { signEffectAttestation } from "./ledger.js";
|
|
18
18
|
export { spokenReason } from "./spoken-reason.js";
|
|
19
19
|
export { verifyLedger } from "./verify-ledger.js";
|
|
20
|
+
export { anchorEntryHash, appendAnchor, readAnchors } from "./anchors.js";
|
|
21
|
+
export { verifyReceipt } from "./transparency-receipt.js";
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export declare class CborTag {
|
|
2
|
+
readonly tag: number;
|
|
3
|
+
readonly value: unknown;
|
|
4
|
+
constructor(tag: number, value: unknown);
|
|
5
|
+
}
|
|
6
|
+
/** RFC 8949 definite-length items; floats and indefinite lengths are refused. */
|
|
7
|
+
export declare function decodeCborItem(bytes: Uint8Array): unknown;
|
|
8
|
+
/** RFC 9052 4.4: ["Signature1", body_protected, external_aad = h'', payload], CBOR-encoded. */
|
|
9
|
+
export declare function sign1Structure(protectedBytes: Uint8Array, payload: Uint8Array): Buffer;
|
|
10
|
+
export type Sign1 = {
|
|
11
|
+
protectedBytes: Uint8Array;
|
|
12
|
+
protectedHeader: Map<unknown, unknown>;
|
|
13
|
+
unprotected: Map<unknown, unknown>;
|
|
14
|
+
payload: Uint8Array | null;
|
|
15
|
+
signature: Uint8Array;
|
|
16
|
+
};
|
|
17
|
+
/** A COSE_Sign1, tagged 18 or untagged. */
|
|
18
|
+
export declare function decodeSign1(bytes: Uint8Array): Sign1;
|
|
19
|
+
/** COSE algorithms a receipt may use: EdDSA/Ed25519 (-8, -19), ES256 (-7), ES384 (-35). */
|
|
20
|
+
export declare function verifyCoseSignature(alg: unknown, publicKeyPem: string, data: Buffer, signature: Uint8Array): boolean;
|
|
21
|
+
/** RFC 9162 2.1.3.2: the root an inclusion proof yields for a leaf entry. */
|
|
22
|
+
export declare function rootFromInclusion(leafEntry: Uint8Array, leafIndex: number, treeSize: number, path: readonly Uint8Array[]): Buffer;
|
|
23
|
+
export type ReceiptStage = "receipt-decode" | "vds-gate" | "inclusion" | "receipt-sig";
|
|
24
|
+
export type ReceiptCheck = {
|
|
25
|
+
ok: true;
|
|
26
|
+
root: string;
|
|
27
|
+
treeSize: number;
|
|
28
|
+
leafIndex: number;
|
|
29
|
+
} | {
|
|
30
|
+
ok: false;
|
|
31
|
+
stage: ReceiptStage;
|
|
32
|
+
reason: string;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Checks one receipt for one leaf entry under the service key the reader
|
|
36
|
+
* holds. vds is read from the protected header only.
|
|
37
|
+
*/
|
|
38
|
+
export declare function verifyReceipt(receipt: Uint8Array, leafEntry: Uint8Array, publicKeyPem: string): ReceiptCheck;
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* COSE Receipts from a SCITT Transparency Service (RFC 9943) over an RFC 9162
|
|
3
|
+
* SHA-256 Merkle tree, read without the service.
|
|
4
|
+
*
|
|
5
|
+
* A receipt is a COSE_Sign1, normally tagged 18, whose protected header names
|
|
6
|
+
* the verifiable data structure at label 395 (1 is RFC9162_SHA256) and whose
|
|
7
|
+
* unprotected header carries, at label 396 key -1, one inclusion proof: the
|
|
8
|
+
* CBOR array [tree_size, leaf_index, audit_path]. The payload is detached: it
|
|
9
|
+
* is the tree root, which the verifier rebuilds from the leaf entry and the
|
|
10
|
+
* path and then checks the signature over. A receipt carries no key id, so the
|
|
11
|
+
* service key is always the reader's to supply.
|
|
12
|
+
*
|
|
13
|
+
* This is a second CBOR reader beside @cedulon/cose on purpose. That one reads
|
|
14
|
+
* Cedulon's own objects and refuses tags, floats and non-empty unprotected
|
|
15
|
+
* headers, which is right for them; a receipt written by someone else's
|
|
16
|
+
* service uses all three.
|
|
17
|
+
*/
|
|
18
|
+
import { createHash, createPublicKey, verify as verifySignature } from "node:crypto";
|
|
19
|
+
export class CborTag {
|
|
20
|
+
tag;
|
|
21
|
+
value;
|
|
22
|
+
constructor(tag, value) {
|
|
23
|
+
this.tag = tag;
|
|
24
|
+
this.value = value;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
const MAX_BYTES = 65_536;
|
|
28
|
+
const MAX_DEPTH = 16;
|
|
29
|
+
const MAX_ITEMS = 4_096;
|
|
30
|
+
/** RFC 8949 definite-length items; floats and indefinite lengths are refused. */
|
|
31
|
+
export function decodeCborItem(bytes) {
|
|
32
|
+
if (bytes.length > MAX_BYTES)
|
|
33
|
+
throw new Error("cbor-too-large");
|
|
34
|
+
let at = 0;
|
|
35
|
+
let items = 0;
|
|
36
|
+
const need = (n) => {
|
|
37
|
+
if (at + n > bytes.length)
|
|
38
|
+
throw new Error("cbor-eof");
|
|
39
|
+
};
|
|
40
|
+
const length = (ai) => {
|
|
41
|
+
if (ai < 24)
|
|
42
|
+
return ai;
|
|
43
|
+
const size = ai === 24 ? 1 : ai === 25 ? 2 : ai === 26 ? 4 : ai === 27 ? 8 : 0;
|
|
44
|
+
if (size === 0)
|
|
45
|
+
throw new Error("cbor-indefinite-or-reserved");
|
|
46
|
+
need(size);
|
|
47
|
+
let n = 0n;
|
|
48
|
+
for (let i = 0; i < size; i += 1)
|
|
49
|
+
n = (n << 8n) | BigInt(bytes[at + i]);
|
|
50
|
+
at += size;
|
|
51
|
+
if (n > BigInt(Number.MAX_SAFE_INTEGER))
|
|
52
|
+
throw new Error("cbor-int-too-large");
|
|
53
|
+
return Number(n);
|
|
54
|
+
};
|
|
55
|
+
const item = (depth) => {
|
|
56
|
+
if (depth > MAX_DEPTH)
|
|
57
|
+
throw new Error("cbor-too-deep");
|
|
58
|
+
items += 1;
|
|
59
|
+
if (items > MAX_ITEMS)
|
|
60
|
+
throw new Error("cbor-too-many-items");
|
|
61
|
+
need(1);
|
|
62
|
+
const ib = bytes[at];
|
|
63
|
+
at += 1;
|
|
64
|
+
const major = ib >> 5;
|
|
65
|
+
const ai = ib & 31;
|
|
66
|
+
if (major === 7) {
|
|
67
|
+
if (ai === 20)
|
|
68
|
+
return false;
|
|
69
|
+
if (ai === 21)
|
|
70
|
+
return true;
|
|
71
|
+
if (ai === 22)
|
|
72
|
+
return null;
|
|
73
|
+
throw new Error("cbor-simple-or-float");
|
|
74
|
+
}
|
|
75
|
+
const n = length(ai);
|
|
76
|
+
switch (major) {
|
|
77
|
+
case 0:
|
|
78
|
+
return n;
|
|
79
|
+
case 1:
|
|
80
|
+
return -1 - n;
|
|
81
|
+
case 2:
|
|
82
|
+
need(n);
|
|
83
|
+
at += n;
|
|
84
|
+
return bytes.slice(at - n, at);
|
|
85
|
+
case 3:
|
|
86
|
+
need(n);
|
|
87
|
+
at += n;
|
|
88
|
+
return new TextDecoder("utf-8", { fatal: true }).decode(bytes.slice(at - n, at));
|
|
89
|
+
case 4: {
|
|
90
|
+
if (n > MAX_ITEMS)
|
|
91
|
+
throw new Error("cbor-too-many-items");
|
|
92
|
+
const out = [];
|
|
93
|
+
for (let i = 0; i < n; i += 1)
|
|
94
|
+
out.push(item(depth + 1));
|
|
95
|
+
return out;
|
|
96
|
+
}
|
|
97
|
+
case 5: {
|
|
98
|
+
if (n > MAX_ITEMS)
|
|
99
|
+
throw new Error("cbor-too-many-items");
|
|
100
|
+
const out = new Map();
|
|
101
|
+
for (let i = 0; i < n; i += 1) {
|
|
102
|
+
const key = item(depth + 1);
|
|
103
|
+
if (key instanceof Uint8Array || key instanceof Map || Array.isArray(key))
|
|
104
|
+
throw new Error("cbor-key-type");
|
|
105
|
+
if (out.has(key))
|
|
106
|
+
throw new Error("cbor-duplicate-key");
|
|
107
|
+
out.set(key, item(depth + 1));
|
|
108
|
+
}
|
|
109
|
+
return out;
|
|
110
|
+
}
|
|
111
|
+
case 6:
|
|
112
|
+
return new CborTag(n, item(depth + 1));
|
|
113
|
+
default:
|
|
114
|
+
throw new Error("cbor-major");
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
const value = item(0);
|
|
118
|
+
if (at !== bytes.length)
|
|
119
|
+
throw new Error("cbor-trailing");
|
|
120
|
+
return value;
|
|
121
|
+
}
|
|
122
|
+
function head(major, n) {
|
|
123
|
+
if (n < 24)
|
|
124
|
+
return Buffer.of((major << 5) | n);
|
|
125
|
+
if (n < 0x100)
|
|
126
|
+
return Buffer.of((major << 5) | 24, n);
|
|
127
|
+
if (n < 0x10000)
|
|
128
|
+
return Buffer.of((major << 5) | 25, n >> 8, n & 0xff);
|
|
129
|
+
const b = Buffer.alloc(5);
|
|
130
|
+
b[0] = (major << 5) | 26;
|
|
131
|
+
b.writeUInt32BE(n, 1);
|
|
132
|
+
return b;
|
|
133
|
+
}
|
|
134
|
+
/** RFC 9052 4.4: ["Signature1", body_protected, external_aad = h'', payload], CBOR-encoded. */
|
|
135
|
+
export function sign1Structure(protectedBytes, payload) {
|
|
136
|
+
const text = Buffer.from("Signature1", "utf8");
|
|
137
|
+
return Buffer.concat([
|
|
138
|
+
head(4, 4),
|
|
139
|
+
head(3, text.length),
|
|
140
|
+
text,
|
|
141
|
+
head(2, protectedBytes.length),
|
|
142
|
+
protectedBytes,
|
|
143
|
+
head(2, 0),
|
|
144
|
+
head(2, payload.length),
|
|
145
|
+
payload,
|
|
146
|
+
]);
|
|
147
|
+
}
|
|
148
|
+
/** A COSE_Sign1, tagged 18 or untagged. */
|
|
149
|
+
export function decodeSign1(bytes) {
|
|
150
|
+
let value = decodeCborItem(bytes);
|
|
151
|
+
if (value instanceof CborTag) {
|
|
152
|
+
if (value.tag !== 18)
|
|
153
|
+
throw new Error(`cose-tag-${value.tag}`);
|
|
154
|
+
value = value.value;
|
|
155
|
+
}
|
|
156
|
+
if (!Array.isArray(value) || value.length !== 4)
|
|
157
|
+
throw new Error("cose-sign1-shape");
|
|
158
|
+
const [p, u, payload, signature] = value;
|
|
159
|
+
if (!(p instanceof Uint8Array) || !(u instanceof Map) || !(signature instanceof Uint8Array)) {
|
|
160
|
+
throw new Error("cose-sign1-members");
|
|
161
|
+
}
|
|
162
|
+
if (payload !== null && !(payload instanceof Uint8Array))
|
|
163
|
+
throw new Error("cose-sign1-payload");
|
|
164
|
+
const protectedHeader = p.length === 0 ? new Map() : decodeCborItem(p);
|
|
165
|
+
if (!(protectedHeader instanceof Map))
|
|
166
|
+
throw new Error("cose-protected-header");
|
|
167
|
+
return { protectedBytes: p, protectedHeader, unprotected: u, payload, signature };
|
|
168
|
+
}
|
|
169
|
+
/** COSE algorithms a receipt may use: EdDSA/Ed25519 (-8, -19), ES256 (-7), ES384 (-35). */
|
|
170
|
+
export function verifyCoseSignature(alg, publicKeyPem, data, signature) {
|
|
171
|
+
const key = createPublicKey(publicKeyPem);
|
|
172
|
+
if (alg === -8 || alg === -19) {
|
|
173
|
+
return key.asymmetricKeyType === "ed25519" && verifySignature(null, data, key, signature);
|
|
174
|
+
}
|
|
175
|
+
const curve = key.asymmetricKeyType === "ec" ? key.asymmetricKeyDetails?.namedCurve : undefined;
|
|
176
|
+
if (alg === -7) {
|
|
177
|
+
return curve === "prime256v1" && verifySignature("sha256", data, { key, dsaEncoding: "ieee-p1363" }, signature);
|
|
178
|
+
}
|
|
179
|
+
if (alg === -35) {
|
|
180
|
+
return curve === "secp384r1" && verifySignature("sha384", data, { key, dsaEncoding: "ieee-p1363" }, signature);
|
|
181
|
+
}
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
const sha256 = (...parts) => createHash("sha256").update(Buffer.concat(parts)).digest();
|
|
185
|
+
/** RFC 9162 2.1.3.2: the root an inclusion proof yields for a leaf entry. */
|
|
186
|
+
export function rootFromInclusion(leafEntry, leafIndex, treeSize, path) {
|
|
187
|
+
if (!Number.isSafeInteger(leafIndex) || !Number.isSafeInteger(treeSize) || leafIndex < 0 || leafIndex >= treeSize) {
|
|
188
|
+
throw new Error("inclusion-index");
|
|
189
|
+
}
|
|
190
|
+
let fn = leafIndex;
|
|
191
|
+
let sn = treeSize - 1;
|
|
192
|
+
// RFC 9162 2.1.1: a leaf hashes as SHA-256(0x00 || entry), an interior node as SHA-256(0x01 || left || right).
|
|
193
|
+
let r = sha256(Buffer.of(0x00), leafEntry);
|
|
194
|
+
for (const p of path) {
|
|
195
|
+
if (p.length !== 32)
|
|
196
|
+
throw new Error("inclusion-node-size");
|
|
197
|
+
if (sn === 0)
|
|
198
|
+
throw new Error("inclusion-path-too-long");
|
|
199
|
+
if (fn % 2 === 1 || fn === sn) {
|
|
200
|
+
r = sha256(Buffer.of(0x01), p, r);
|
|
201
|
+
while (fn % 2 === 0 && fn !== 0) {
|
|
202
|
+
fn = Math.floor(fn / 2);
|
|
203
|
+
sn = Math.floor(sn / 2);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
r = sha256(Buffer.of(0x01), r, p);
|
|
208
|
+
}
|
|
209
|
+
fn = Math.floor(fn / 2);
|
|
210
|
+
sn = Math.floor(sn / 2);
|
|
211
|
+
}
|
|
212
|
+
if (sn !== 0)
|
|
213
|
+
throw new Error("inclusion-path-too-short");
|
|
214
|
+
return r;
|
|
215
|
+
}
|
|
216
|
+
const UNDERSTOOD = new Set([1, 395]);
|
|
217
|
+
/**
|
|
218
|
+
* Checks one receipt for one leaf entry under the service key the reader
|
|
219
|
+
* holds. vds is read from the protected header only.
|
|
220
|
+
*/
|
|
221
|
+
export function verifyReceipt(receipt, leafEntry, publicKeyPem) {
|
|
222
|
+
let sign1;
|
|
223
|
+
try {
|
|
224
|
+
sign1 = decodeSign1(receipt);
|
|
225
|
+
}
|
|
226
|
+
catch (err) {
|
|
227
|
+
return { ok: false, stage: "receipt-decode", reason: err instanceof Error ? err.message : String(err) };
|
|
228
|
+
}
|
|
229
|
+
// RFC 9052 3.1: crit lists labels a reader must understand, is non-empty and
|
|
230
|
+
// sits in the protected header. This reader understands alg and vds only.
|
|
231
|
+
const crit = sign1.protectedHeader.get(2);
|
|
232
|
+
if (crit !== undefined && (!Array.isArray(crit) || crit.length === 0 || !crit.every((l) => UNDERSTOOD.has(l)))) {
|
|
233
|
+
return { ok: false, stage: "receipt-decode", reason: "a critical header this reader does not understand" };
|
|
234
|
+
}
|
|
235
|
+
if (sign1.unprotected.has(2))
|
|
236
|
+
return { ok: false, stage: "receipt-decode", reason: "crit in the unprotected header" };
|
|
237
|
+
const vds = sign1.protectedHeader.get(395);
|
|
238
|
+
if (vds !== 1)
|
|
239
|
+
return { ok: false, stage: "vds-gate", reason: `vds ${String(vds)} is not RFC9162_SHA256` };
|
|
240
|
+
const vdp = sign1.unprotected.get(396);
|
|
241
|
+
const proofs = vdp instanceof Map ? vdp.get(-1) : undefined;
|
|
242
|
+
if (!Array.isArray(proofs) || proofs.length !== 1 || !(proofs[0] instanceof Uint8Array)) {
|
|
243
|
+
return { ok: false, stage: "receipt-decode", reason: "no single inclusion proof at 396/-1" };
|
|
244
|
+
}
|
|
245
|
+
if (sign1.payload !== null)
|
|
246
|
+
return { ok: false, stage: "receipt-decode", reason: "receipt payload is not detached" };
|
|
247
|
+
let proof;
|
|
248
|
+
try {
|
|
249
|
+
proof = decodeCborItem(proofs[0]);
|
|
250
|
+
}
|
|
251
|
+
catch (err) {
|
|
252
|
+
return { ok: false, stage: "receipt-decode", reason: err instanceof Error ? err.message : String(err) };
|
|
253
|
+
}
|
|
254
|
+
if (!Array.isArray(proof) || proof.length !== 3)
|
|
255
|
+
return { ok: false, stage: "receipt-decode", reason: "inclusion proof shape" };
|
|
256
|
+
const [treeSize, leafIndex, path] = proof;
|
|
257
|
+
if (typeof treeSize !== "number" || typeof leafIndex !== "number" || !Array.isArray(path) || !path.every((p) => p instanceof Uint8Array)) {
|
|
258
|
+
return { ok: false, stage: "receipt-decode", reason: "inclusion proof members" };
|
|
259
|
+
}
|
|
260
|
+
let root;
|
|
261
|
+
try {
|
|
262
|
+
root = rootFromInclusion(leafEntry, leafIndex, treeSize, path);
|
|
263
|
+
}
|
|
264
|
+
catch (err) {
|
|
265
|
+
return { ok: false, stage: "inclusion", reason: err instanceof Error ? err.message : String(err) };
|
|
266
|
+
}
|
|
267
|
+
let verified = false;
|
|
268
|
+
try {
|
|
269
|
+
verified = verifyCoseSignature(sign1.protectedHeader.get(1), publicKeyPem, sign1Structure(sign1.protectedBytes, root), sign1.signature);
|
|
270
|
+
}
|
|
271
|
+
catch {
|
|
272
|
+
verified = false;
|
|
273
|
+
}
|
|
274
|
+
if (!verified)
|
|
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 };
|
|
277
|
+
}
|
package/dist/verify-ledger.d.ts
CHANGED
|
@@ -34,8 +34,27 @@ export type VerifyResult = {
|
|
|
34
34
|
tail: VerifyTail;
|
|
35
35
|
/** Halt windows. A ledger with none says so; that sentence is not a violation. */
|
|
36
36
|
control: VerifyControl;
|
|
37
|
+
/** Inputs rows checked against each record's signed `inputsHash`. */
|
|
38
|
+
inputs: VerifyInputs;
|
|
39
|
+
/** Transparency Service receipts for checkpoints, when the ledger holds any. */
|
|
40
|
+
anchors: VerifyAnchors;
|
|
37
41
|
problems: string[];
|
|
38
42
|
};
|
|
43
|
+
export type VerifyAnchors = {
|
|
44
|
+
line: string;
|
|
45
|
+
/** Receipts in `checkpoint-anchors.jsonl`. */
|
|
46
|
+
receipts: number;
|
|
47
|
+
/** Receipts that verified under the reader's key. Zero when no key was given. */
|
|
48
|
+
verified: number;
|
|
49
|
+
checked: boolean;
|
|
50
|
+
};
|
|
51
|
+
export type VerifyInputs = {
|
|
52
|
+
line: string;
|
|
53
|
+
/** Records whose inputs row was found and hashes to their `inputsHash`. */
|
|
54
|
+
matched: number;
|
|
55
|
+
missing: number;
|
|
56
|
+
mismatched: number;
|
|
57
|
+
};
|
|
39
58
|
export type VerifyIndex = {
|
|
40
59
|
present: boolean;
|
|
41
60
|
/** Index refs that are not decisions in the ledger. */
|
|
@@ -67,5 +86,7 @@ export type VerifyOptions = {
|
|
|
67
86
|
witnessPublicKeyPem?: string;
|
|
68
87
|
/** Verify every checkpoint row against this key instead of one key taken from the file. */
|
|
69
88
|
checkpointPublicKeyPem?: string;
|
|
89
|
+
/** The Transparency Service key that receipts in `checkpoint-anchors.jsonl` are checked under. */
|
|
90
|
+
anchorPublicKeyPem?: string;
|
|
70
91
|
};
|
|
71
92
|
export declare function verifyLedger(dir: string, opts?: VerifyOptions): Promise<VerifyResult>;
|
package/dist/verify-ledger.js
CHANGED
|
@@ -51,6 +51,31 @@
|
|
|
51
51
|
* that verified. A key taken from the file shows the checkpoints agree
|
|
52
52
|
* with each other, not that the key was ever trusted.
|
|
53
53
|
*
|
|
54
|
+
* Inputs are a further statement. The writer appends a call's inputs row
|
|
55
|
+
* before the record whose `inputsHash` commits to it, so a crash leaves at
|
|
56
|
+
* most an inputs row with no record, never the reverse. Each record that
|
|
57
|
+
* carries an `inputsHash` must find an inputs row under its ref whose
|
|
58
|
+
* canonical SHA-256 is that hash. A missing row is `inputs row missing`, a
|
|
59
|
+
* row under that ref with another hash is `inputs row does not match`, and
|
|
60
|
+
* either makes `ok` false: the approver block of an approval lives in that
|
|
61
|
+
* row, and an unbound row could be rewritten from a signed approval into an
|
|
62
|
+
* unsigned one. A row with no record is the crash case and is not named.
|
|
63
|
+
*
|
|
64
|
+
* Checkpoint counts are checked too. For every checkpoint that verified, its
|
|
65
|
+
* head must be a record the ledger holds, the verified records in its window
|
|
66
|
+
* up to that head must be its `receiptCount` ending at the head, and their
|
|
67
|
+
* allow, deny and defer counts must be its totals; a record appended later
|
|
68
|
+
* with a time in the window is not counted. Null totals (redacted) are not
|
|
69
|
+
* compared; the count still is.
|
|
70
|
+
*
|
|
71
|
+
* Anchors are the last statement. `checkpoint-anchors.jsonl` holds COSE
|
|
72
|
+
* Receipts from a Transparency Service for checkpoints `verax anchor`
|
|
73
|
+
* registered. A receipt names no key, so it is checked only under a key the
|
|
74
|
+
* reader supplies (`anchorPublicKeyPem`): each row must name a checkpoint that
|
|
75
|
+
* verified, its leaf entry must be the one the route defines, and the receipt
|
|
76
|
+
* must verify over the root its inclusion proof rebuilds. Without that key the
|
|
77
|
+
* receipts are counted and said to be unchecked; that is not a failure.
|
|
78
|
+
*
|
|
54
79
|
* Control records are a further statement. An allow whose subject is
|
|
55
80
|
* `verax.halt` opens a halt window; an allow whose subject is `verax.resume`
|
|
56
81
|
* closes it. An allow whose subject does not start with `verax.` while a
|
|
@@ -70,12 +95,14 @@
|
|
|
70
95
|
import { createPublicKey } from "node:crypto";
|
|
71
96
|
import { existsSync, lstatSync, readFileSync } from "node:fs";
|
|
72
97
|
import { join } from "node:path";
|
|
73
|
-
import { findCheckpointChainBreak, verifyCheckpoint, verifyCheckpointUnderPin, } from "@cedulon/checkpoint";
|
|
98
|
+
import { checkpointHash, findCheckpointChainBreak, totalsFromDecisionRecords, verifyCheckpoint, verifyCheckpointUnderPin, } from "@cedulon/checkpoint";
|
|
74
99
|
import { canonical, decisionRecordHash, findDecisionRecordChainBreak, verifyDecisionRecord } from "@cedulon/core";
|
|
75
100
|
import { coseFromHex, decodeCoseSign1, verifyCoseSign1 } from "@cedulon/cose";
|
|
76
101
|
import { verifyEffectExtract } from "@cedulon/effect-extract";
|
|
102
|
+
import { anchorEntryHash, readAnchors } from "./anchors.js";
|
|
77
103
|
import { checkpointsPath, readCheckpointFile } from "./checkpoints.js";
|
|
78
104
|
import { sha256Canonical } from "./hash.js";
|
|
105
|
+
import { verifyReceipt } from "./transparency-receipt.js";
|
|
79
106
|
import { ledgerPiecePathProblem, readLedgerManifest, requireLedgerPiecePath, indexPath, manifestPath, parseIndexText, } from "./ledger-manifest.js";
|
|
80
107
|
/**
|
|
81
108
|
* A symbolic link is a named problem and is not read. Missing files are not
|
|
@@ -120,13 +147,17 @@ function takePieceFile(dir, rel, problems, read) {
|
|
|
120
147
|
}
|
|
121
148
|
return path;
|
|
122
149
|
}
|
|
123
|
-
/**
|
|
150
|
+
/**
|
|
151
|
+
* Decision, effect and inputs files this directory holds, oldest piece first.
|
|
152
|
+
* An inputs file the disk lacks is not named here: a record that needed a row
|
|
153
|
+
* from it is named when inputs are checked.
|
|
154
|
+
*/
|
|
124
155
|
function ledgerFiles(dir, problems) {
|
|
125
156
|
const manifestFile = manifestPath(dir);
|
|
126
157
|
const manifestLink = symbolicLinkProblem(manifestFile);
|
|
127
158
|
if (manifestLink !== null) {
|
|
128
159
|
problems.push(manifestLink);
|
|
129
|
-
return { decisions: [], effects: [] };
|
|
160
|
+
return { decisions: [], effects: [], inputs: [] };
|
|
130
161
|
}
|
|
131
162
|
if (existsSync(manifestFile)) {
|
|
132
163
|
try {
|
|
@@ -137,7 +168,7 @@ function ledgerFiles(dir, problems) {
|
|
|
137
168
|
}
|
|
138
169
|
catch (err) {
|
|
139
170
|
problems.push(err instanceof Error ? err.message : "ledger-manifest-unreadable");
|
|
140
|
-
return { decisions: [], effects: [] };
|
|
171
|
+
return { decisions: [], effects: [], inputs: [] };
|
|
141
172
|
}
|
|
142
173
|
}
|
|
143
174
|
return legacyFiles(dir);
|
|
@@ -145,14 +176,17 @@ function ledgerFiles(dir, problems) {
|
|
|
145
176
|
function legacyFiles(dir) {
|
|
146
177
|
const decisionsPath = join(dir, "decisions.jsonl");
|
|
147
178
|
const effectsPath = join(dir, "effects.jsonl");
|
|
179
|
+
const inputsPath = join(dir, "inputs.jsonl");
|
|
148
180
|
return {
|
|
149
181
|
decisions: existsSync(decisionsPath) ? [decisionsPath] : [],
|
|
150
182
|
effects: existsSync(effectsPath) ? [effectsPath] : [],
|
|
183
|
+
inputs: existsSync(inputsPath) ? [inputsPath] : [],
|
|
151
184
|
};
|
|
152
185
|
}
|
|
153
186
|
function pieceFiles(dir, pieces, problems) {
|
|
154
187
|
const decisions = [];
|
|
155
188
|
const effects = [];
|
|
189
|
+
const inputs = [];
|
|
156
190
|
for (const p of pieces) {
|
|
157
191
|
const decision = takePieceFile(dir, p.decisions, problems, true);
|
|
158
192
|
if (decision)
|
|
@@ -160,9 +194,15 @@ function pieceFiles(dir, pieces, problems) {
|
|
|
160
194
|
const effect = takePieceFile(dir, p.effects, problems, true);
|
|
161
195
|
if (effect)
|
|
162
196
|
effects.push(effect);
|
|
197
|
+
const before = problems.length;
|
|
163
198
|
takePieceFile(dir, p.inputs, problems, false);
|
|
199
|
+
if (problems.length === before) {
|
|
200
|
+
const path = requireLedgerPiecePath(dir, p.inputs);
|
|
201
|
+
if (existsSync(path))
|
|
202
|
+
inputs.push(path);
|
|
203
|
+
}
|
|
164
204
|
}
|
|
165
|
-
return { decisions, effects };
|
|
205
|
+
return { decisions, effects, inputs };
|
|
166
206
|
}
|
|
167
207
|
/** Parses JSONL, reporting the line a bad row sits on rather than throwing. */
|
|
168
208
|
function readJsonl(path, problems) {
|
|
@@ -520,7 +560,7 @@ function noteEffectCompleteness(effectRefs, bound, problems) {
|
|
|
520
560
|
}
|
|
521
561
|
return EFFECT_COMPLETENESS_CHECKED;
|
|
522
562
|
}
|
|
523
|
-
function tailStatement(dir, records, checkpointPublicKeyPem) {
|
|
563
|
+
function tailStatement(dir, records, checkpointPublicKeyPem, attested = records) {
|
|
524
564
|
const checkpointFile = checkpointsPath(dir);
|
|
525
565
|
const checkpointLink = symbolicLinkProblem(checkpointFile);
|
|
526
566
|
if (checkpointLink !== null) {
|
|
@@ -529,6 +569,7 @@ function tailStatement(dir, records, checkpointPublicKeyPem) {
|
|
|
529
569
|
tail: { line: TAIL_NONE, checkpoint: null },
|
|
530
570
|
checkpointTrust: checkpointTrustOf(pinned, null, 0),
|
|
531
571
|
problems: [checkpointLink],
|
|
572
|
+
verified: [],
|
|
532
573
|
};
|
|
533
574
|
}
|
|
534
575
|
const loaded = readCheckpointFile(dir);
|
|
@@ -538,10 +579,22 @@ function tailStatement(dir, records, checkpointPublicKeyPem) {
|
|
|
538
579
|
const key = pinned !== "" ? pinned : taken;
|
|
539
580
|
const checkpointTrust = checkpointTrustOf(pinned, taken, rows.length);
|
|
540
581
|
const problems = [...loaded.problems];
|
|
582
|
+
const verified = [];
|
|
541
583
|
noteEd25519(key, problems);
|
|
542
584
|
for (let i = 0; i < rows.length; i += 1) {
|
|
543
585
|
if (!checkpointRowVerifies(rows[i], key)) {
|
|
544
586
|
problems.push(`checkpoint signature does not verify: checkpoint ${i}`);
|
|
587
|
+
continue;
|
|
588
|
+
}
|
|
589
|
+
try {
|
|
590
|
+
const count = rows[i].claims.receiptCount;
|
|
591
|
+
verified.push({ hash: checkpointHash(rows[i]), receiptCount: typeof count === "number" ? count : null });
|
|
592
|
+
}
|
|
593
|
+
catch {
|
|
594
|
+
// A row that verified but cannot be hashed cannot be anchored either.
|
|
595
|
+
}
|
|
596
|
+
for (const problem of checkpointCountProblems(rows[i], records, attested)) {
|
|
597
|
+
problems.push(`${problem}: checkpoint ${i}`);
|
|
545
598
|
}
|
|
546
599
|
}
|
|
547
600
|
let brk = null;
|
|
@@ -559,7 +612,7 @@ function tailStatement(dir, records, checkpointPublicKeyPem) {
|
|
|
559
612
|
const covered = brk ? rows.slice(0, brk.index) : rows;
|
|
560
613
|
const newest = covered.length > 0 ? covered[covered.length - 1] : null;
|
|
561
614
|
if (!newest) {
|
|
562
|
-
return { tail: { line: TAIL_NONE, checkpoint: null }, checkpointTrust, problems };
|
|
615
|
+
return { tail: { line: TAIL_NONE, checkpoint: null }, checkpointTrust, problems, verified };
|
|
563
616
|
}
|
|
564
617
|
const claims = newest.claims;
|
|
565
618
|
const receiptCount = typeof claims.receiptCount === "number" ? claims.receiptCount : null;
|
|
@@ -589,8 +642,185 @@ function tailStatement(dir, records, checkpointPublicKeyPem) {
|
|
|
589
642
|
tail: { line, checkpoint: { receiptCount, chainHeadHash, ledgerHoldsRecord } },
|
|
590
643
|
checkpointTrust,
|
|
591
644
|
problems,
|
|
645
|
+
verified,
|
|
646
|
+
};
|
|
647
|
+
}
|
|
648
|
+
const ANCHORS_NONE = {
|
|
649
|
+
line: "anchors: none (no checkpoint is registered with a transparency service)",
|
|
650
|
+
receipts: 0,
|
|
651
|
+
verified: 0,
|
|
652
|
+
checked: false,
|
|
653
|
+
};
|
|
654
|
+
function anchorStatement(dir, checkpoints, keyPem, problems) {
|
|
655
|
+
const read = readAnchors(dir);
|
|
656
|
+
if (!read.present)
|
|
657
|
+
return ANCHORS_NONE;
|
|
658
|
+
problems.push(...read.problems);
|
|
659
|
+
const receipts = read.rows.length;
|
|
660
|
+
const services = [...new Set(read.rows.map((r) => r.service))].join(", ");
|
|
661
|
+
if (keyPem === "") {
|
|
662
|
+
return {
|
|
663
|
+
line: `anchors: ${receipts} receipt(s) from ${services || "no service"}, not checked: pin the service key with --anchor-key`,
|
|
664
|
+
receipts,
|
|
665
|
+
verified: 0,
|
|
666
|
+
checked: false,
|
|
667
|
+
};
|
|
668
|
+
}
|
|
669
|
+
const byHash = new Map(checkpoints.map((c) => [c.hash, c]));
|
|
670
|
+
let verified = 0;
|
|
671
|
+
let newestCount = null;
|
|
672
|
+
for (const row of read.rows) {
|
|
673
|
+
const short = row.checkpointHash.slice(0, 12);
|
|
674
|
+
const checkpoint = byHash.get(row.checkpointHash);
|
|
675
|
+
if (!checkpoint) {
|
|
676
|
+
problems.push(`anchor names a checkpoint the ledger does not hold: ${short}`);
|
|
677
|
+
continue;
|
|
678
|
+
}
|
|
679
|
+
let entry;
|
|
680
|
+
try {
|
|
681
|
+
entry = anchorEntryHash(row.checkpointHash, row.route);
|
|
682
|
+
}
|
|
683
|
+
catch {
|
|
684
|
+
problems.push(`anchor route is not one this verifier reads: ${short}`);
|
|
685
|
+
continue;
|
|
686
|
+
}
|
|
687
|
+
if (entry !== row.entryHash) {
|
|
688
|
+
problems.push(`anchor entry hash is not the checkpoint's: ${short}`);
|
|
689
|
+
continue;
|
|
690
|
+
}
|
|
691
|
+
const check = verifyReceipt(Buffer.from(row.receiptB64, "base64"), Buffer.from(entry, "hex"), keyPem);
|
|
692
|
+
if (!check.ok) {
|
|
693
|
+
problems.push(`anchor receipt does not verify: ${short} (${check.stage}: ${check.reason})`);
|
|
694
|
+
continue;
|
|
695
|
+
}
|
|
696
|
+
if (check.treeSize !== row.treeSize || check.leafIndex !== row.leafIndex) {
|
|
697
|
+
problems.push(`anchor row disagrees with its receipt: ${short}`);
|
|
698
|
+
continue;
|
|
699
|
+
}
|
|
700
|
+
verified += 1;
|
|
701
|
+
if (checkpoint.receiptCount !== null && (newestCount === null || checkpoint.receiptCount > newestCount)) {
|
|
702
|
+
newestCount = checkpoint.receiptCount;
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
const covers = newestCount === null ? "" : `; the newest anchored checkpoint covers ${newestCount} record(s)`;
|
|
706
|
+
return {
|
|
707
|
+
line: verified === receipts
|
|
708
|
+
? `anchors: ${receipts} receipt(s) from ${services} verify under the key you supplied${covers}`
|
|
709
|
+
: `anchors: ${verified} of ${receipts} receipt(s) verify under the key you supplied`,
|
|
710
|
+
receipts,
|
|
711
|
+
verified,
|
|
712
|
+
checked: true,
|
|
592
713
|
};
|
|
593
714
|
}
|
|
715
|
+
const INPUTS_UNCHECKED = { line: "inputs: not checked", matched: 0, missing: 0, mismatched: 0 };
|
|
716
|
+
/**
|
|
717
|
+
* Pairs each record that carries an `inputsHash` with an inputs row under its
|
|
718
|
+
* ref that hashes to it. Rows are used once. A ref can hold more rows than
|
|
719
|
+
* records (a crash after the row, a retry), so a record is matched against
|
|
720
|
+
* any unused row under its ref, not the k-th.
|
|
721
|
+
*/
|
|
722
|
+
function inputsStatement(paths, records, problems) {
|
|
723
|
+
const rowsByRef = new Map();
|
|
724
|
+
for (const path of paths) {
|
|
725
|
+
for (const row of readJsonl(path, problems)) {
|
|
726
|
+
if (!row || typeof row !== "object" || Array.isArray(row))
|
|
727
|
+
continue;
|
|
728
|
+
const { ref, inputs } = row;
|
|
729
|
+
if (typeof ref !== "string" || inputs === undefined)
|
|
730
|
+
continue;
|
|
731
|
+
let hash;
|
|
732
|
+
try {
|
|
733
|
+
hash = sha256Canonical(inputs);
|
|
734
|
+
}
|
|
735
|
+
catch {
|
|
736
|
+
continue;
|
|
737
|
+
}
|
|
738
|
+
const list = rowsByRef.get(ref) ?? [];
|
|
739
|
+
list.push(hash);
|
|
740
|
+
rowsByRef.set(ref, list);
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
let matched = 0;
|
|
744
|
+
let missing = 0;
|
|
745
|
+
let mismatched = 0;
|
|
746
|
+
for (let i = 0; i < records.length; i += 1) {
|
|
747
|
+
const claims = records[i].claims;
|
|
748
|
+
const expected = claims?.inputsHash;
|
|
749
|
+
if (typeof expected !== "string")
|
|
750
|
+
continue;
|
|
751
|
+
const ref = typeof claims.ref === "string" ? claims.ref : null;
|
|
752
|
+
const rows = ref === null ? undefined : rowsByRef.get(ref);
|
|
753
|
+
if (!rows || rows.length === 0) {
|
|
754
|
+
missing += 1;
|
|
755
|
+
problems.push(`inputs row missing: record ${i} (ref ${ref ?? "?"})`);
|
|
756
|
+
continue;
|
|
757
|
+
}
|
|
758
|
+
const at = rows.indexOf(expected);
|
|
759
|
+
if (at < 0) {
|
|
760
|
+
mismatched += 1;
|
|
761
|
+
problems.push(`inputs row does not match its record's inputsHash: record ${i} (ref ${ref})`);
|
|
762
|
+
continue;
|
|
763
|
+
}
|
|
764
|
+
rows.splice(at, 1);
|
|
765
|
+
matched += 1;
|
|
766
|
+
}
|
|
767
|
+
const line = missing === 0 && mismatched === 0
|
|
768
|
+
? `inputs: ${matched} record(s), each with its inputs row`
|
|
769
|
+
: `inputs: ${matched} match, ${missing} missing, ${mismatched} do not match their record`;
|
|
770
|
+
return { line, matched, missing, mismatched };
|
|
771
|
+
}
|
|
772
|
+
/**
|
|
773
|
+
* What a checkpoint counted is fixed by its head. The witness counts the
|
|
774
|
+
* records whose time falls in `[startMs, endMs)` and names the last of them
|
|
775
|
+
* as the head, so the attested records in that window up to and including
|
|
776
|
+
* the head must be `receiptCount` records ending at the head, and their
|
|
777
|
+
* allow, deny and defer counts must be the signed totals (decision profile
|
|
778
|
+
* 4.4). A record appended later with a time inside the window was never seen
|
|
779
|
+
* by the witness and is not counted. A head the ledger does not hold is named
|
|
780
|
+
* for every checkpoint, not only the newest. A null head means the witness
|
|
781
|
+
* counted nothing. Null totals were redacted; the count is still checked.
|
|
782
|
+
*/
|
|
783
|
+
function checkpointCountProblems(row, records, attested) {
|
|
784
|
+
const claims = row.claims;
|
|
785
|
+
const { startMs, endMs, receiptCount } = claims;
|
|
786
|
+
if (typeof startMs !== "number" || typeof endMs !== "number")
|
|
787
|
+
return ["checkpoint has no window"];
|
|
788
|
+
const inWindow = (r) => r.claims.timestampMs >= startMs && r.claims.timestampMs < endMs;
|
|
789
|
+
let counted = [];
|
|
790
|
+
let headRecord = null;
|
|
791
|
+
if (typeof claims.chainHeadHash === "string") {
|
|
792
|
+
const at = records.findIndex((r) => {
|
|
793
|
+
try {
|
|
794
|
+
return decisionRecordHash(r) === claims.chainHeadHash;
|
|
795
|
+
}
|
|
796
|
+
catch {
|
|
797
|
+
return false;
|
|
798
|
+
}
|
|
799
|
+
});
|
|
800
|
+
if (at < 0)
|
|
801
|
+
return ["checkpoint names a head the ledger does not hold"];
|
|
802
|
+
headRecord = records[at];
|
|
803
|
+
const upTo = new Set(records.slice(0, at + 1));
|
|
804
|
+
counted = attested.filter((r) => upTo.has(r) && inWindow(r));
|
|
805
|
+
}
|
|
806
|
+
const problems = [];
|
|
807
|
+
const endsAtHead = headRecord === null ? counted.length === 0 : counted[counted.length - 1] === headRecord;
|
|
808
|
+
if (typeof receiptCount !== "number" || receiptCount !== counted.length || !endsAtHead) {
|
|
809
|
+
problems.push(`checkpoint covers ${String(receiptCount)} record(s) in its window; the ledger holds ${counted.length} there up to its head`);
|
|
810
|
+
}
|
|
811
|
+
if (claims.totals !== null && claims.totals !== undefined) {
|
|
812
|
+
let same = false;
|
|
813
|
+
try {
|
|
814
|
+
same = canonical(totalsFromDecisionRecords(counted)) === canonical(claims.totals);
|
|
815
|
+
}
|
|
816
|
+
catch {
|
|
817
|
+
// A totals value that does not canonicalise is not the map the profile defines.
|
|
818
|
+
}
|
|
819
|
+
if (!same)
|
|
820
|
+
problems.push("checkpoint totals do not match the records in its window");
|
|
821
|
+
}
|
|
822
|
+
return problems;
|
|
823
|
+
}
|
|
594
824
|
function controlStatement(records) {
|
|
595
825
|
let open = false;
|
|
596
826
|
let windows = 0;
|
|
@@ -656,6 +886,8 @@ function unreadableResult(dir, problem) {
|
|
|
656
886
|
effectCompleteness: EFFECT_COMPLETENESS_UNCHECKED,
|
|
657
887
|
tail: { line: TAIL_NONE, checkpoint: null },
|
|
658
888
|
control: CONTROL_UNCHECKED,
|
|
889
|
+
inputs: INPUTS_UNCHECKED,
|
|
890
|
+
anchors: ANCHORS_NONE,
|
|
659
891
|
problems: [problem],
|
|
660
892
|
};
|
|
661
893
|
}
|
|
@@ -717,6 +949,8 @@ async function verifyLedgerUnchecked(dir, opts = {}) {
|
|
|
717
949
|
effectCompleteness,
|
|
718
950
|
tail: emptyTail.tail,
|
|
719
951
|
control: emptyControl.control,
|
|
952
|
+
inputs: INPUTS_UNCHECKED,
|
|
953
|
+
anchors: ANCHORS_NONE,
|
|
720
954
|
problems,
|
|
721
955
|
};
|
|
722
956
|
}
|
|
@@ -737,6 +971,7 @@ async function verifyLedgerUnchecked(dir, opts = {}) {
|
|
|
737
971
|
};
|
|
738
972
|
let signaturesValid = 0;
|
|
739
973
|
let signaturesInvalid = 0;
|
|
974
|
+
const attested = [];
|
|
740
975
|
const recordRefused = ed25519Refusal(anahtar);
|
|
741
976
|
if (recordRefused)
|
|
742
977
|
problems.push(recordRefused);
|
|
@@ -751,6 +986,7 @@ async function verifyLedgerUnchecked(dir, opts = {}) {
|
|
|
751
986
|
}
|
|
752
987
|
if (gecerli) {
|
|
753
988
|
signaturesValid += 1;
|
|
989
|
+
attested.push(rec);
|
|
754
990
|
}
|
|
755
991
|
else {
|
|
756
992
|
signaturesInvalid += 1;
|
|
@@ -907,10 +1143,12 @@ async function verifyLedgerUnchecked(dir, opts = {}) {
|
|
|
907
1143
|
}
|
|
908
1144
|
const indexed = indexStatement(dir, refler);
|
|
909
1145
|
const effectCompleteness = noteEffectCompleteness(indexed.effectRefs, boundPrimaryRefs, problems);
|
|
910
|
-
const
|
|
1146
|
+
const inputs = inputsStatement(files.inputs, records, problems);
|
|
1147
|
+
const tailed = tailStatement(dir, records, opts.checkpointPublicKeyPem ?? "", attested);
|
|
911
1148
|
problems.push(...indexed.problems, ...tailed.problems);
|
|
912
1149
|
const stated = controlStatement(records);
|
|
913
1150
|
problems.push(...stated.violations);
|
|
1151
|
+
const anchors = anchorStatement(dir, tailed.verified, opts.anchorPublicKeyPem?.trim() ?? "", problems);
|
|
914
1152
|
const ok = problems.length === 0 && signaturesInvalid === 0 && chainBreakAt === null && effectsOrphaned === 0;
|
|
915
1153
|
return {
|
|
916
1154
|
ok,
|
|
@@ -930,6 +1168,8 @@ async function verifyLedgerUnchecked(dir, opts = {}) {
|
|
|
930
1168
|
effectCompleteness,
|
|
931
1169
|
tail: tailed.tail,
|
|
932
1170
|
control: stated.control,
|
|
1171
|
+
inputs,
|
|
1172
|
+
anchors,
|
|
933
1173
|
problems,
|
|
934
1174
|
};
|
|
935
1175
|
}
|