@forestrie/receipt-verify 3.1.0 → 3.2.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/checkpoint-chain-receipt-outcome.d.ts +56 -0
- package/dist/checkpoint-chain-receipt-outcome.d.ts.map +1 -0
- package/dist/checkpoint-chain-receipt-outcome.js +1 -0
- package/dist/checkpoint-link-record.d.ts +69 -0
- package/dist/checkpoint-link-record.d.ts.map +1 -0
- package/dist/checkpoint-link-record.js +1 -0
- package/dist/checkpoint-link-verifier.d.ts +80 -0
- package/dist/checkpoint-link-verifier.d.ts.map +1 -0
- package/dist/checkpoint-link-verifier.js +181 -0
- package/dist/inclusion-walk-end.d.ts +6 -0
- package/dist/inclusion-walk-end.d.ts.map +1 -0
- package/dist/inclusion-walk-end.js +35 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -0
- package/dist/known-accumulator.d.ts.map +1 -1
- package/dist/known-accumulator.js +25 -9
- package/dist/ks256-attestation-usage.d.ts +50 -0
- package/dist/ks256-attestation-usage.d.ts.map +1 -0
- package/dist/ks256-attestation-usage.js +51 -0
- package/dist/select-ks256-attestation.d.ts +57 -0
- package/dist/select-ks256-attestation.d.ts.map +1 -0
- package/dist/select-ks256-attestation.js +119 -0
- package/dist/verify-grant-receipt-offline.d.ts +11 -0
- package/dist/verify-grant-receipt-offline.d.ts.map +1 -1
- package/dist/verify-grant-receipt-offline.js +25 -23
- package/dist/verify-receipt-against-checkpoint-chain.d.ts +42 -0
- package/dist/verify-receipt-against-checkpoint-chain.d.ts.map +1 -0
- package/dist/verify-receipt-against-checkpoint-chain.js +205 -0
- package/package.json +3 -3
- package/src/checkpoint-chain-receipt-outcome.ts +67 -0
- package/src/checkpoint-link-record.ts +74 -0
- package/src/checkpoint-link-verifier.ts +241 -0
- package/src/inclusion-walk-end.ts +38 -0
- package/src/index.ts +30 -0
- package/src/known-accumulator.ts +30 -8
- package/src/ks256-attestation-usage.ts +103 -0
- package/src/select-ks256-attestation.ts +151 -0
- package/src/verify-grant-receipt-offline.ts +43 -27
- package/src/verify-receipt-against-checkpoint-chain.ts +256 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The typed result of {@link verifyReceiptAgainstCheckpointChain}: the
|
|
3
|
+
* verdict, where it was decided, and everything a reporter needs (folded
|
|
4
|
+
* links, per-link delegation records, attestation usage).
|
|
5
|
+
*/
|
|
6
|
+
import type { CheckpointChainLink } from "./checkpoint-chain.js";
|
|
7
|
+
import type {
|
|
8
|
+
CheckpointLinkFailure,
|
|
9
|
+
CheckpointLinkRecord,
|
|
10
|
+
} from "./checkpoint-link-record.js";
|
|
11
|
+
import type { Ks256AttestationUsage } from "./ks256-attestation-usage.js";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Where a failing run stopped:
|
|
15
|
+
* - `input`: the caller's inputs (no root keys, a forest genesis without a
|
|
16
|
+
* chain binding).
|
|
17
|
+
* - `chain`: the checkpoint chain did not fold or a link was refused.
|
|
18
|
+
* - `parse`: the receipt or idtimestamp did not parse.
|
|
19
|
+
* - `anchor`: the chain folded but the receipt's leaf-derived peak is in no
|
|
20
|
+
* link (`peak_not_in_checkpoint_chain`), or the leaf is past the chain
|
|
21
|
+
* (`receipt_newer_than_checkpoint_chain`; refresh, not tamper).
|
|
22
|
+
* - `delegation`: the receipt matched, but a link covering its leaf was
|
|
23
|
+
* signed under an expired certificate (`delegation_expired`).
|
|
24
|
+
*/
|
|
25
|
+
export type CheckpointChainReceiptStage =
|
|
26
|
+
| "input"
|
|
27
|
+
| "chain"
|
|
28
|
+
| "parse"
|
|
29
|
+
| "anchor"
|
|
30
|
+
| "delegation";
|
|
31
|
+
|
|
32
|
+
/** Fields every outcome carries, pass or fail. */
|
|
33
|
+
type OutcomeCommon = {
|
|
34
|
+
/** The authenticated links (the verified prefix on a chain failure). */
|
|
35
|
+
links: CheckpointChainLink[];
|
|
36
|
+
/** Per-link delegation facts, one per authenticated link. */
|
|
37
|
+
records: CheckpointLinkRecord[];
|
|
38
|
+
/** Every supplied attestation, as used or unused with a reason. */
|
|
39
|
+
attestations: Ks256AttestationUsage;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/** A receipt anchored in a retained checkpoint chain, or why not. */
|
|
43
|
+
export type CheckpointChainReceiptOutcome =
|
|
44
|
+
| (OutcomeCommon & {
|
|
45
|
+
ok: true;
|
|
46
|
+
/** Index of the newest link whose accumulator holds the peak. */
|
|
47
|
+
matchedLink: number;
|
|
48
|
+
/** Index of the matching peak within that link's accumulator. */
|
|
49
|
+
matchedPeak: number;
|
|
50
|
+
})
|
|
51
|
+
| (OutcomeCommon & {
|
|
52
|
+
ok: false;
|
|
53
|
+
stage: CheckpointChainReceiptStage;
|
|
54
|
+
/**
|
|
55
|
+
* Stable reason: a {@link CheckpointLinkFailure} reason, a
|
|
56
|
+
* `CheckpointChainResult` reason prefixed `checkpoint_chain_`, a
|
|
57
|
+
* receipt parse reason, or one of the anchor/delegation reasons
|
|
58
|
+
* above.
|
|
59
|
+
*/
|
|
60
|
+
reason: string;
|
|
61
|
+
/** Offending link index, for `chain` and `delegation`. */
|
|
62
|
+
at?: number;
|
|
63
|
+
/** Human detail from the chain fold, when it failed there. */
|
|
64
|
+
detail?: string;
|
|
65
|
+
/** The per-link verifier's refusal, when it caused a `chain` failure. */
|
|
66
|
+
linkFailure?: CheckpointLinkFailure;
|
|
67
|
+
});
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the per-link checkpoint verifier learned about each link of a
|
|
3
|
+
* retained `.sth` chain ({@link makeCheckpointLinkVerifier}). Consumers
|
|
4
|
+
* (mcp-verify, forestrie-cli) report these facts; the post-match expiry
|
|
5
|
+
* check ({@link checkCheckpointLinkExpiry}) reads them.
|
|
6
|
+
*/
|
|
7
|
+
import type { DelegationConstraints } from "./resolve-delegated-verify-key.js";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Which key verified a link's checkpoint signature, and what vouched for it:
|
|
11
|
+
*
|
|
12
|
+
* - `es256-root`: an ES256 root key verified the checkpoint directly (no
|
|
13
|
+
* certificate, or the root signed despite one).
|
|
14
|
+
* - `delegated`: the label-1000 certificate's delegated key verified it, and
|
|
15
|
+
* an ES256 root (or passkey root) vouched for the certificate.
|
|
16
|
+
* - `ks256-root`: the delegated key verified it, and a KS256 root vouched for
|
|
17
|
+
* the certificate: by ecrecover, or by an ERC-1271 attestation when
|
|
18
|
+
* {@link CheckpointLinkRecord.attestationNeeded}.
|
|
19
|
+
*/
|
|
20
|
+
export type CheckpointLinkVerifiedBy =
|
|
21
|
+
| "es256-root"
|
|
22
|
+
| "ks256-root"
|
|
23
|
+
| "delegated";
|
|
24
|
+
|
|
25
|
+
/** One authenticated link of a checkpoint chain. */
|
|
26
|
+
export type CheckpointLinkRecord = {
|
|
27
|
+
/** Position in the supplied checkpoint list (fold order). */
|
|
28
|
+
index: number;
|
|
29
|
+
/** The checkpoint's SIGNED tree size (protected label -65933). */
|
|
30
|
+
signedSize: bigint;
|
|
31
|
+
/**
|
|
32
|
+
* The delegation certificate's limits, `null` when the link carries no
|
|
33
|
+
* certificate or the certificate declares none of labels 3/4/8/9.
|
|
34
|
+
*/
|
|
35
|
+
constraints: DelegationConstraints | null;
|
|
36
|
+
verifiedBy: CheckpointLinkVerifiedBy;
|
|
37
|
+
/**
|
|
38
|
+
* Hex (no `0x`) of the digest of the attestation this link needed, set
|
|
39
|
+
* only when {@link attestationNeeded}.
|
|
40
|
+
*/
|
|
41
|
+
attestationUsed?: string;
|
|
42
|
+
/**
|
|
43
|
+
* Index, in the caller's attestation list, of the attestation made for
|
|
44
|
+
* this link's certificate (digest and signature hash match), whether or
|
|
45
|
+
* not the link needed it.
|
|
46
|
+
*/
|
|
47
|
+
attestationIndex?: number;
|
|
48
|
+
/**
|
|
49
|
+
* Indexes of attestations made for this link's certificate that were
|
|
50
|
+
* tried before {@link attestationIndex} and failed their own checks
|
|
51
|
+
* (address, chain id, magic).
|
|
52
|
+
*/
|
|
53
|
+
attestationsRejected?: number[];
|
|
54
|
+
/**
|
|
55
|
+
* The link verified only because of an attestation: re-resolving its
|
|
56
|
+
* certificate under the same roots without one fails. Pure and offline.
|
|
57
|
+
*/
|
|
58
|
+
attestationNeeded: boolean;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/** Why the per-link verifier refused a link. */
|
|
62
|
+
export type CheckpointLinkFailure = {
|
|
63
|
+
index: number;
|
|
64
|
+
/**
|
|
65
|
+
* `delegation_invalid`, a KS256 reason (`ks256_root_not_recovered`,
|
|
66
|
+
* `ks256_attestation_*`, `delegation_not_ks256`), `delegation_out_of_range`,
|
|
67
|
+
* `signature_invalid` or `proof_malformed`.
|
|
68
|
+
*/
|
|
69
|
+
reason: string;
|
|
70
|
+
/** As {@link CheckpointLinkRecord.attestationIndex}. */
|
|
71
|
+
attestationIndex?: number;
|
|
72
|
+
/** As {@link CheckpointLinkRecord.attestationsRejected}. */
|
|
73
|
+
attestationsRejected?: number[];
|
|
74
|
+
};
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-link trust for a retained checkpoint chain: the `verifySignature`
|
|
3
|
+
* callback {@link verifyCheckpointChain} takes, plus the delegation limits
|
|
4
|
+
* each link's label-1000 certificate imposes. Lifted from mcp-verify 0.6.1
|
|
5
|
+
* (`src/core/verify-shared.ts`) so mcp-verify and forestrie-cli share one
|
|
6
|
+
* copy; see mcp-verify `docs/trust-roots.md` ("Delegation limits under
|
|
7
|
+
* checkpoint-chain").
|
|
8
|
+
*
|
|
9
|
+
* The limits mirror univocity's on-chain check
|
|
10
|
+
* ([delegationVerifier.sol](https://github.com/forestrie/univocity/blob/main/src/checkpoints/lib/delegationVerifier.sol)):
|
|
11
|
+
* a checkpoint of claimed size `S` is anchored only when its delegation
|
|
12
|
+
* covers `mmrIndex = S > 0 ? S - 1 : 0`, i.e. `mmrStart <= mmrIndex <=
|
|
13
|
+
* mmrEnd`. Here `S` is the checkpoint's SIGNED tree size (label -65933), so
|
|
14
|
+
* a link the contract would refuse is refused offline too
|
|
15
|
+
* (`delegation_out_of_range`). Expiry needs a leaf the receipt binds and is
|
|
16
|
+
* judged after the receipt matched ({@link checkCheckpointLinkExpiry}).
|
|
17
|
+
*
|
|
18
|
+
* Pure over bytes; no network (canopy rule C1).
|
|
19
|
+
*/
|
|
20
|
+
import { verifyCoseSign1WithParsedKey } from "@forestrie/encoding";
|
|
21
|
+
import { checkpointConsistencyProof } from "./checkpoint-chain.js";
|
|
22
|
+
import type {
|
|
23
|
+
CheckpointLinkFailure,
|
|
24
|
+
CheckpointLinkRecord,
|
|
25
|
+
CheckpointLinkVerifiedBy,
|
|
26
|
+
} from "./checkpoint-link-record.js";
|
|
27
|
+
import type { Ks256Attestation } from "./ks256-attestation.js";
|
|
28
|
+
import {
|
|
29
|
+
idtimestampToUnixSeconds,
|
|
30
|
+
resolveDelegatedVerifyKey,
|
|
31
|
+
type DelegationConstraints,
|
|
32
|
+
} from "./resolve-delegated-verify-key.js";
|
|
33
|
+
import { isParsedKs256RootKey, type RootVerifyKey } from "./root-verify-key.js";
|
|
34
|
+
import { resolveWithKs256Attestations } from "./select-ks256-attestation.js";
|
|
35
|
+
|
|
36
|
+
/** The per-link verifier and what it has learned so far. */
|
|
37
|
+
export type CheckpointLinkVerifier = {
|
|
38
|
+
/** Pass as `verifyCheckpointChain({ verifySignature })`. */
|
|
39
|
+
verifySignature: (
|
|
40
|
+
checkpointBytes: Uint8Array,
|
|
41
|
+
detachedPayload: Uint8Array,
|
|
42
|
+
) => Promise<boolean>;
|
|
43
|
+
/** One record per link that verified, in fold order. */
|
|
44
|
+
records: CheckpointLinkRecord[];
|
|
45
|
+
/** Set when the verifier refused a link; the chain fold stops there. */
|
|
46
|
+
failure(): CheckpointLinkFailure | undefined;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Build the per-link signature verifier for {@link verifyCheckpointChain}.
|
|
51
|
+
*
|
|
52
|
+
* For each checkpoint, in fold order: resolve its label-1000 certificate
|
|
53
|
+
* under `rootKeys` (with the attestation made for that certificate, if any),
|
|
54
|
+
* verify the COSE signature over the FOLDED accumulator (the detached
|
|
55
|
+
* payload the fold computes, never one read from the checkpoint; ADR-0046),
|
|
56
|
+
* then enforce the certificate's size bound against the signed size. KS256
|
|
57
|
+
* roots vouch only for certificates; only ES256 roots may verify a
|
|
58
|
+
* checkpoint directly.
|
|
59
|
+
*
|
|
60
|
+
* The callback must be used for exactly one fold: link indices are the call
|
|
61
|
+
* count.
|
|
62
|
+
*
|
|
63
|
+
* @param opts.rootKeys - ES256 keys and KS256 addresses, from genesis or
|
|
64
|
+
* caller-known log keys.
|
|
65
|
+
* @param opts.attestations - ERC-1271 attestations for KS256 contract
|
|
66
|
+
* roots; each certificate takes the one with its digest and signature
|
|
67
|
+
* hash ({@link selectKs256Attestation}).
|
|
68
|
+
* @param opts.forestChainId - The forest genesis chain id every attestation
|
|
69
|
+
* must carry; without it a contract root fails
|
|
70
|
+
* `ks256_attestation_chain_unbound`.
|
|
71
|
+
*/
|
|
72
|
+
export function makeCheckpointLinkVerifier(opts: {
|
|
73
|
+
rootKeys: readonly RootVerifyKey[];
|
|
74
|
+
attestations?: readonly Ks256Attestation[] | undefined;
|
|
75
|
+
forestChainId?: bigint | undefined;
|
|
76
|
+
}): CheckpointLinkVerifier {
|
|
77
|
+
const rootKeys = [...opts.rootKeys];
|
|
78
|
+
const attestations = opts.attestations ?? [];
|
|
79
|
+
const es256Roots = rootKeys.filter(
|
|
80
|
+
(k): k is CryptoKey => !isParsedKs256RootKey(k),
|
|
81
|
+
);
|
|
82
|
+
const records: CheckpointLinkRecord[] = [];
|
|
83
|
+
let failure: CheckpointLinkFailure | undefined;
|
|
84
|
+
let calls = 0;
|
|
85
|
+
|
|
86
|
+
const verifySignature = async (
|
|
87
|
+
checkpointBytes: Uint8Array,
|
|
88
|
+
detachedPayload: Uint8Array,
|
|
89
|
+
): Promise<boolean> => {
|
|
90
|
+
const index = calls++;
|
|
91
|
+
const chosen = await resolveWithKs256Attestations(
|
|
92
|
+
checkpointBytes,
|
|
93
|
+
rootKeys,
|
|
94
|
+
attestations,
|
|
95
|
+
opts.forestChainId,
|
|
96
|
+
);
|
|
97
|
+
const resolution = chosen.resolution;
|
|
98
|
+
const fail = (reason: string): false => {
|
|
99
|
+
failure = { index, reason };
|
|
100
|
+
if (chosen.selected !== undefined) {
|
|
101
|
+
failure.attestationIndex = chosen.selected;
|
|
102
|
+
}
|
|
103
|
+
if (chosen.rejected.length > 0) {
|
|
104
|
+
failure.attestationsRejected = chosen.rejected;
|
|
105
|
+
}
|
|
106
|
+
return false;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
if (resolution.kind === "broken") {
|
|
110
|
+
return fail(resolution.reason ?? "delegation_invalid");
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
let signedSize: bigint;
|
|
114
|
+
try {
|
|
115
|
+
signedSize = checkpointConsistencyProof(checkpointBytes).signedTreeSize2;
|
|
116
|
+
} catch {
|
|
117
|
+
// The fold read the same label moments ago; fail closed.
|
|
118
|
+
return fail("proof_malformed");
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const candidates: { key: CryptoKey; delegated: boolean }[] = [
|
|
122
|
+
...(resolution.kind === "resolved"
|
|
123
|
+
? [{ key: resolution.delegatedKey, delegated: true }]
|
|
124
|
+
: []),
|
|
125
|
+
...es256Roots.map((key) => ({ key, delegated: false })),
|
|
126
|
+
];
|
|
127
|
+
let signer: { key: CryptoKey; delegated: boolean } | undefined;
|
|
128
|
+
for (const c of candidates) {
|
|
129
|
+
if (
|
|
130
|
+
await verifyCoseSign1WithParsedKey(checkpointBytes, c.key, {
|
|
131
|
+
logPrefix: "checkpoint-chain",
|
|
132
|
+
detachedPayload,
|
|
133
|
+
})
|
|
134
|
+
) {
|
|
135
|
+
signer = c;
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (signer === undefined) return fail("signature_invalid");
|
|
140
|
+
|
|
141
|
+
// A resolved certificate's limits bind the link even when a root key
|
|
142
|
+
// verified it directly: stricter than univocity, which would not accept
|
|
143
|
+
// a root signature alongside a certificate at all.
|
|
144
|
+
const constraints =
|
|
145
|
+
resolution.kind === "resolved" ? resolution.constraints : null;
|
|
146
|
+
if (constraints !== null) {
|
|
147
|
+
const bound = checkCheckpointLinkSizeBound(constraints, signedSize);
|
|
148
|
+
if (!bound.ok) return fail(bound.reason);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
let verifiedBy: CheckpointLinkVerifiedBy = "es256-root";
|
|
152
|
+
if (signer.delegated) {
|
|
153
|
+
const viaEs256 = await resolveDelegatedVerifyKey(
|
|
154
|
+
checkpointBytes,
|
|
155
|
+
es256Roots,
|
|
156
|
+
);
|
|
157
|
+
verifiedBy = viaEs256.kind === "resolved" ? "delegated" : "ks256-root";
|
|
158
|
+
}
|
|
159
|
+
// Only a selected attestation can carry a certificate (a diagnostic
|
|
160
|
+
// stand-in never matches), so "needed" implies "selected".
|
|
161
|
+
const attestationNeeded =
|
|
162
|
+
chosen.selected !== undefined &&
|
|
163
|
+
resolution.kind === "resolved" &&
|
|
164
|
+
(await resolveDelegatedVerifyKey(checkpointBytes, rootKeys)).kind ===
|
|
165
|
+
"broken";
|
|
166
|
+
|
|
167
|
+
const record: CheckpointLinkRecord = {
|
|
168
|
+
index,
|
|
169
|
+
signedSize,
|
|
170
|
+
constraints,
|
|
171
|
+
verifiedBy,
|
|
172
|
+
attestationNeeded,
|
|
173
|
+
};
|
|
174
|
+
if (chosen.rejected.length > 0) {
|
|
175
|
+
record.attestationsRejected = chosen.rejected;
|
|
176
|
+
}
|
|
177
|
+
if (chosen.selected !== undefined) {
|
|
178
|
+
record.attestationIndex = chosen.selected;
|
|
179
|
+
if (attestationNeeded) {
|
|
180
|
+
record.attestationUsed = bytesToHex(
|
|
181
|
+
attestations[chosen.selected]!.digest,
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
records.push(record);
|
|
186
|
+
return true;
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
return { verifySignature, records, failure: () => failure };
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* The size bound univocity enforces on a checkpoint, judged from signed
|
|
194
|
+
* data alone: with `mmrIndex = signedSize > 0 ? signedSize - 1 : 0`, reject
|
|
195
|
+
* `mmrIndex < mmrStart || mmrIndex > mmrEnd` (`delegationVerifier.sol`,
|
|
196
|
+
* `_Univocity.sol` passes `claimedSize > 0 ? claimedSize - 1 : 0`).
|
|
197
|
+
*
|
|
198
|
+
* @returns `delegation_out_of_range` on either side of the window.
|
|
199
|
+
*/
|
|
200
|
+
export function checkCheckpointLinkSizeBound(
|
|
201
|
+
constraints: DelegationConstraints,
|
|
202
|
+
signedSize: bigint,
|
|
203
|
+
): { ok: true } | { ok: false; reason: "delegation_out_of_range" } {
|
|
204
|
+
const mmrIndex = signedSize > 0n ? signedSize - 1n : 0n;
|
|
205
|
+
if (mmrIndex < constraints.mmrStart || mmrIndex > constraints.mmrEnd) {
|
|
206
|
+
return { ok: false, reason: "delegation_out_of_range" };
|
|
207
|
+
}
|
|
208
|
+
return { ok: true };
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Expiry, judged only AFTER a receipt matched the chain, when its leaf's
|
|
213
|
+
* idtimestamp and mmrIndex are bound by the leaf hash and inclusion path
|
|
214
|
+
* rather than being caller claims. A link whose signed size covers the leaf
|
|
215
|
+
* (`signedSize > leaf.mmrIndex`) was signed after the leaf was sequenced, so
|
|
216
|
+
* a leaf time past `expiresAt` proves signing past the key's lifetime. A
|
|
217
|
+
* link that does not cover the leaf says nothing about its time. Never
|
|
218
|
+
* wall-clock; no `issuedAt` bound (a certificate is legitimately signed a
|
|
219
|
+
* few seconds after the leaf it first covers).
|
|
220
|
+
*
|
|
221
|
+
* @param records - From {@link makeCheckpointLinkVerifier}.
|
|
222
|
+
* @param leaf.idtimestamp - The bound leaf's snowflake idtimestamp.
|
|
223
|
+
* @returns The first offending link as `at`.
|
|
224
|
+
*/
|
|
225
|
+
export function checkCheckpointLinkExpiry(
|
|
226
|
+
records: readonly CheckpointLinkRecord[],
|
|
227
|
+
leaf: { mmrIndex: bigint; idtimestamp: bigint },
|
|
228
|
+
): { ok: true } | { ok: false; reason: "delegation_expired"; at: number } {
|
|
229
|
+
const leafSeconds = idtimestampToUnixSeconds(leaf.idtimestamp);
|
|
230
|
+
for (const r of records) {
|
|
231
|
+
if (r.constraints === null) continue;
|
|
232
|
+
if (r.signedSize > leaf.mmrIndex && leafSeconds > r.constraints.expiresAt) {
|
|
233
|
+
return { ok: false, reason: "delegation_expired", at: r.index };
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return { ok: true };
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function bytesToHex(bytes: Uint8Array): string {
|
|
240
|
+
return Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
241
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where an MMR inclusion walk ends. A leaf hash
|
|
3
|
+
* (`SHA-256(idtimestamp ‖ inner)`) does not commit the leaf's position, and
|
|
4
|
+
* a path of length 0 commits nothing more, so matching a recomputed peak by
|
|
5
|
+
* VALUE alone leaves the receipt's claimed `mmrIndex` unbound. Requiring the
|
|
6
|
+
* matched accumulator peak to sit at the node index the walk reaches binds
|
|
7
|
+
* it: interior nodes commit their position (`H(pos ‖ left ‖ right)`), and
|
|
8
|
+
* an empty path must start at the peak itself.
|
|
9
|
+
*
|
|
10
|
+
* Mirrors the position arithmetic of `calculateRoot` in
|
|
11
|
+
* [@forestrie/merklelog](https://github.com/forestrie/canopy/blob/main/packages/merklelog/src/mmr/algorithms.ts)
|
|
12
|
+
* (go-merklelog `mmr/proof.go`), without hashing.
|
|
13
|
+
*/
|
|
14
|
+
import { indexHeight } from "@forestrie/merklelog";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The zero-based MMR index of the node an inclusion path of `pathLength`
|
|
18
|
+
* siblings reaches from `leafMmrIndex`: the peak the path claims to prove.
|
|
19
|
+
*/
|
|
20
|
+
export function inclusionWalkEndIndex(
|
|
21
|
+
leafMmrIndex: bigint,
|
|
22
|
+
pathLength: number,
|
|
23
|
+
): bigint {
|
|
24
|
+
let height = indexHeight(leafMmrIndex);
|
|
25
|
+
// 1-based position, as calculateRoot tracks it.
|
|
26
|
+
let pos = leafMmrIndex + 1n;
|
|
27
|
+
for (let i = 0; i < pathLength; i++) {
|
|
28
|
+
// `indexHeight(pos)` is the height of the node AFTER the current one:
|
|
29
|
+
// higher means the current node is a right child.
|
|
30
|
+
if (indexHeight(pos) > height) {
|
|
31
|
+
pos += 1n;
|
|
32
|
+
} else {
|
|
33
|
+
pos += 2n << BigInt(height);
|
|
34
|
+
}
|
|
35
|
+
height += 1;
|
|
36
|
+
}
|
|
37
|
+
return pos - 1n;
|
|
38
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -211,3 +211,33 @@ export {
|
|
|
211
211
|
verifyReceiptOfflineAgainstKnownAccumulator,
|
|
212
212
|
type KnownAccumulator,
|
|
213
213
|
} from "./known-accumulator.js";
|
|
214
|
+
/**
|
|
215
|
+
* Checkpoint-chain trust root with per-link delegation limits and several
|
|
216
|
+
* ERC-1271 attestations (owner ruling D8, receipt-verify 3.2.0): the one
|
|
217
|
+
* shared copy mcp-verify and forestrie-cli use. The high-level verifier
|
|
218
|
+
* folds, matches and judges expiry; the pieces serve callers that match
|
|
219
|
+
* peaks their own way.
|
|
220
|
+
*/
|
|
221
|
+
export { verifyReceiptAgainstCheckpointChain } from "./verify-receipt-against-checkpoint-chain.js";
|
|
222
|
+
export type { VerifyReceiptAgainstCheckpointChainInput } from "./verify-receipt-against-checkpoint-chain.js";
|
|
223
|
+
export type {
|
|
224
|
+
CheckpointChainReceiptOutcome,
|
|
225
|
+
CheckpointChainReceiptStage,
|
|
226
|
+
} from "./checkpoint-chain-receipt-outcome.js";
|
|
227
|
+
export {
|
|
228
|
+
checkCheckpointLinkExpiry,
|
|
229
|
+
checkCheckpointLinkSizeBound,
|
|
230
|
+
makeCheckpointLinkVerifier,
|
|
231
|
+
type CheckpointLinkVerifier,
|
|
232
|
+
} from "./checkpoint-link-verifier.js";
|
|
233
|
+
export type {
|
|
234
|
+
CheckpointLinkFailure,
|
|
235
|
+
CheckpointLinkRecord,
|
|
236
|
+
CheckpointLinkVerifiedBy,
|
|
237
|
+
} from "./checkpoint-link-record.js";
|
|
238
|
+
export { selectKs256Attestation } from "./select-ks256-attestation.js";
|
|
239
|
+
export {
|
|
240
|
+
summarizeKs256AttestationUsage,
|
|
241
|
+
type Ks256AttestationUnusedReason,
|
|
242
|
+
type Ks256AttestationUsage,
|
|
243
|
+
} from "./ks256-attestation-usage.js";
|
package/src/known-accumulator.ts
CHANGED
|
@@ -33,8 +33,13 @@ import {
|
|
|
33
33
|
decodeCborDeterministic,
|
|
34
34
|
encodeCborDeterministic,
|
|
35
35
|
} from "@forestrie/encoding";
|
|
36
|
-
import {
|
|
36
|
+
import {
|
|
37
|
+
calculateRoot,
|
|
38
|
+
peakMMRIndexes,
|
|
39
|
+
type Proof,
|
|
40
|
+
} from "@forestrie/merklelog";
|
|
37
41
|
import { normalizeHexAddress } from "@forestrie/chain-rpc";
|
|
42
|
+
import { inclusionWalkEndIndex } from "./inclusion-walk-end.js";
|
|
38
43
|
import { parseReceipt } from "./parse-receipt.js";
|
|
39
44
|
import { univocityLeafHash } from "./leaf-commitment.js";
|
|
40
45
|
import { SubtleHasher } from "./subtle-hasher.js";
|
|
@@ -243,23 +248,40 @@ export async function verifyReceiptOfflineAgainstKnownAccumulator(input: {
|
|
|
243
248
|
};
|
|
244
249
|
}
|
|
245
250
|
|
|
251
|
+
// The leaf (idtimestamp ‖ inner) and inclusion path ALWAYS decide the
|
|
252
|
+
// peak. An attached payload is only a claim of what that peak is: taken on
|
|
253
|
+
// its own, any receipt attaching a real anchored peak would "verify" for
|
|
254
|
+
// any payload and idtimestamp. Same stage and reason as the main offline
|
|
255
|
+
// path's inclusion check (verifySignatureAndInclusion).
|
|
246
256
|
let recomputedPeak: Uint8Array;
|
|
247
|
-
|
|
248
|
-
recomputedPeak = parsed.explicitPeak;
|
|
249
|
-
} else {
|
|
250
|
-
const hasher = new SubtleHasher();
|
|
257
|
+
try {
|
|
251
258
|
const leafHash = await univocityLeafHash(input.idtimestampBe8, input.inner);
|
|
252
259
|
recomputedPeak = await calculateRoot(
|
|
253
|
-
|
|
260
|
+
new SubtleHasher(),
|
|
254
261
|
leafHash,
|
|
255
262
|
parsed.proof,
|
|
256
263
|
leafIdx,
|
|
257
264
|
);
|
|
265
|
+
} catch {
|
|
266
|
+
return { ok: false, stage: "inclusion", reason: "inclusion_failed" };
|
|
267
|
+
}
|
|
268
|
+
if (
|
|
269
|
+
parsed.explicitPeak !== null &&
|
|
270
|
+
!bytesEqual(parsed.explicitPeak, recomputedPeak)
|
|
271
|
+
) {
|
|
272
|
+
return { ok: false, stage: "inclusion", reason: "inclusion_failed" };
|
|
258
273
|
}
|
|
259
274
|
|
|
260
275
|
// 2. Exact peak match — receipt state is a snapshot-covered accumulator.
|
|
261
|
-
|
|
262
|
-
|
|
276
|
+
// The peak must also sit at the node the inclusion walk reaches (contract
|
|
277
|
+
// order is ascending MMR index, as peakMMRIndexes lists them): a leaf hash
|
|
278
|
+
// does not commit its position, so a value match alone would leave the
|
|
279
|
+
// mmrIndex the freshness check above relies on a caller claim.
|
|
280
|
+
const peakIndex = inclusionWalkEndIndex(leafIdx, parsed.proof.path.length);
|
|
281
|
+
const p = peakMMRIndexes(input.size - 1n).indexOf(peakIndex);
|
|
282
|
+
if (p >= 0) {
|
|
283
|
+
const anchored = input.accumulator[p];
|
|
284
|
+
if (anchored !== undefined && bytesEqual(recomputedPeak, anchored)) {
|
|
263
285
|
return { ok: true, stage: "binding" };
|
|
264
286
|
}
|
|
265
287
|
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Accounting for a LIST of ERC-1271 attestations over a checkpoint chain
|
|
3
|
+
* (owner ruling D8): every supplied attestation is reported as used or as
|
|
4
|
+
* unused with a reason, never silently ignored.
|
|
5
|
+
*/
|
|
6
|
+
import type { Ks256Attestation } from "./ks256-attestation.js";
|
|
7
|
+
import type {
|
|
8
|
+
CheckpointLinkFailure,
|
|
9
|
+
CheckpointLinkRecord,
|
|
10
|
+
} from "./checkpoint-link-record.js";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Why a supplied attestation contributed nothing:
|
|
14
|
+
* - `no_matching_certificate`: no link's certificate has its digest and
|
|
15
|
+
* signature hash.
|
|
16
|
+
* - `not_needed`: it matched a certificate that verified without it
|
|
17
|
+
* (ecrecover, or an ES256 root also vouched).
|
|
18
|
+
* - `duplicate`: another supplied attestation for the same certificate
|
|
19
|
+
* (same digest and signature hash) was used, selected or rejected first,
|
|
20
|
+
* so this one was never tried.
|
|
21
|
+
*/
|
|
22
|
+
export type Ks256AttestationUnusedReason =
|
|
23
|
+
| "no_matching_certificate"
|
|
24
|
+
| "not_needed"
|
|
25
|
+
| "duplicate";
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Which supplied attestations carried which links. An attestation may be
|
|
29
|
+
* in both `used` and `rejected` (used on earlier links, refused on a later
|
|
30
|
+
* one); it is in `unused` only when it is in neither.
|
|
31
|
+
*/
|
|
32
|
+
export type Ks256AttestationUsage = {
|
|
33
|
+
used: { index: number; digestHex: string; links: number[] }[];
|
|
34
|
+
/** Tried for a link's certificate and failed its own checks there. */
|
|
35
|
+
rejected: { index: number; digestHex: string; links: number[] }[];
|
|
36
|
+
unused: {
|
|
37
|
+
index: number;
|
|
38
|
+
digestHex: string;
|
|
39
|
+
reason: Ks256AttestationUnusedReason;
|
|
40
|
+
}[];
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Classify each supplied attestation against the per-link records.
|
|
45
|
+
*
|
|
46
|
+
* @param attestations - The list the verifier was given, in order.
|
|
47
|
+
* @param records - Records of the links that verified.
|
|
48
|
+
* @param failure - The link the verifier refused, if any.
|
|
49
|
+
*/
|
|
50
|
+
export function summarizeKs256AttestationUsage(
|
|
51
|
+
attestations: readonly Ks256Attestation[],
|
|
52
|
+
records: readonly CheckpointLinkRecord[],
|
|
53
|
+
failure?: CheckpointLinkFailure,
|
|
54
|
+
): Ks256AttestationUsage {
|
|
55
|
+
const usage: Ks256AttestationUsage = { used: [], rejected: [], unused: [] };
|
|
56
|
+
const seen = [...records, ...(failure !== undefined ? [failure] : [])];
|
|
57
|
+
const tried = (i: number) =>
|
|
58
|
+
seen.some(
|
|
59
|
+
(r) =>
|
|
60
|
+
r.attestationIndex === i || (r.attestationsRejected ?? []).includes(i),
|
|
61
|
+
);
|
|
62
|
+
attestations.forEach((a, index) => {
|
|
63
|
+
const digestHex = bytesToHex(a.digest);
|
|
64
|
+
const used = records
|
|
65
|
+
.filter((r) => r.attestationIndex === index && r.attestationNeeded)
|
|
66
|
+
.map((r) => r.index);
|
|
67
|
+
const rejected = seen
|
|
68
|
+
.filter((r) => (r.attestationsRejected ?? []).includes(index))
|
|
69
|
+
.map((r) => r.index);
|
|
70
|
+
if (used.length > 0) usage.used.push({ index, digestHex, links: used });
|
|
71
|
+
if (rejected.length > 0) {
|
|
72
|
+
usage.rejected.push({ index, digestHex, links: rejected });
|
|
73
|
+
}
|
|
74
|
+
if (used.length > 0 || rejected.length > 0) return;
|
|
75
|
+
let reason: Ks256AttestationUnusedReason = "no_matching_certificate";
|
|
76
|
+
if (seen.some((r) => r.attestationIndex === index)) {
|
|
77
|
+
reason = "not_needed";
|
|
78
|
+
} else if (
|
|
79
|
+
attestations.some(
|
|
80
|
+
(b, j) =>
|
|
81
|
+
j !== index &&
|
|
82
|
+
tried(j) &&
|
|
83
|
+
bytesEqual(a.digest, b.digest) &&
|
|
84
|
+
bytesEqual(a.signatureHash, b.signatureHash),
|
|
85
|
+
)
|
|
86
|
+
) {
|
|
87
|
+
reason = "duplicate";
|
|
88
|
+
}
|
|
89
|
+
usage.unused.push({ index, digestHex, reason });
|
|
90
|
+
});
|
|
91
|
+
return usage;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function bytesEqual(a: Uint8Array, b: Uint8Array): boolean {
|
|
95
|
+
if (a.length !== b.length) return false;
|
|
96
|
+
let x = 0;
|
|
97
|
+
for (let i = 0; i < a.length; i++) x |= a[i]! ^ b[i]!;
|
|
98
|
+
return x === 0;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function bytesToHex(bytes: Uint8Array): string {
|
|
102
|
+
return Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
103
|
+
}
|