@forestrie/receipt-verify 0.9.0 → 0.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -24,8 +24,8 @@ export { verifyReceiptOffline } from "./verify-grant-receipt-offline.js";
24
24
  */
25
25
  export { verifyGrantReceiptOfflineWithKeys, verifyReceiptOfflineWithKeys, } from "./verify-grant-receipt-offline.js";
26
26
  export type { VerifyGrantReceiptOfflineWithKeysInput, VerifyReceiptOfflineWithKeysInput, } from "./verify-grant-receipt-offline.js";
27
- export { resolveDelegatedVerifyKey } from "./resolve-delegated-verify-key.js";
28
- export type { DelegatedResolution } from "./resolve-delegated-verify-key.js";
27
+ export { checkDelegationConstraints, idtimestampToUnixSeconds, resolveDelegatedVerifyKey, } from "./resolve-delegated-verify-key.js";
28
+ export type { DelegatedResolution, DelegationConstraints, } from "./resolve-delegated-verify-key.js";
29
29
  /** Import a raw 64-byte x||y P-256 public key as an ES256 verify key. */
30
30
  export { importEs256PublicKeyFromGrantDataXy64 } from "./decode-trust-root-cbor.js";
31
31
  export { decodeForestrieGrantCose } from "./decode-forestrie-grant-cose.js";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,mBAAmB,EACnB,kBAAkB,GACnB,MAAM,4BAA4B,CAAC;AACpC,oFAAoF;AACpF,YAAY,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AACjD,YAAY,EAAE,8BAA8B,EAAE,MAAM,mCAAmC,CAAC;AACxF,YAAY,EAAE,yBAAyB,EAAE,MAAM,mCAAmC,CAAC;AACnF,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,YAAY,EACV,wBAAwB,EACxB,uBAAuB,EACvB,eAAe,EACf,gBAAgB,GACjB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EACL,wBAAwB,EACxB,mBAAmB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AAC;AAC/B;;;;;GAKG;AACH,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AACzD;;;;;;GAMG;AACH,OAAO,EACL,qBAAqB,EACrB,sBAAsB,EACtB,sBAAsB,EACtB,2CAA2C,EAC3C,KAAK,gBAAgB,GACtB,MAAM,wBAAwB,CAAC"}
package/dist/index.js CHANGED
@@ -16,7 +16,7 @@ export { verifyReceiptOffline } from "./verify-grant-receipt-offline.js";
16
16
  * See the trust-ladder notes on verifyReceiptOfflineWithKeys.
17
17
  */
18
18
  export { verifyGrantReceiptOfflineWithKeys, verifyReceiptOfflineWithKeys, } from "./verify-grant-receipt-offline.js";
19
- export { resolveDelegatedVerifyKey } from "./resolve-delegated-verify-key.js";
19
+ export { checkDelegationConstraints, idtimestampToUnixSeconds, resolveDelegatedVerifyKey, } from "./resolve-delegated-verify-key.js";
20
20
  /** Import a raw 64-byte x||y P-256 public key as an ES256 verify key. */
21
21
  export { importEs256PublicKeyFromGrantDataXy64 } from "./decode-trust-root-cbor.js";
22
22
  export { decodeForestrieGrantCose } from "./decode-forestrie-grant-cose.js";
@@ -1 +1 @@
1
- {"version":3,"file":"known-accumulator.d.ts","sourceRoot":"","sources":["../src/known-accumulator.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAWH,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AAoCtE,MAAM,MAAM,gBAAgB,GAAG;IAC7B,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,6CAA6C;IAC7C,SAAS,EAAE,UAAU,CAAC;IACtB,gEAAgE;IAChE,KAAK,EAAE,UAAU,CAAC;IAClB,+CAA+C;IAC/C,IAAI,EAAE,MAAM,CAAC;IACb,kEAAkE;IAClE,WAAW,EAAE,UAAU,EAAE,CAAC;IAC1B,WAAW,EAAE,MAAM,CAAC;IACpB,qEAAqE;IACrE,SAAS,EAAE,U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AAqE;IACrE,SAAS,EAAE,UAAU,CAAC;CACvB,CAAC;AAEF,yEAAyE;AACzE,wBAAgB,sBAAsB,CAAC,QAAQ,EAAE,gBAAgB,GAAG,UAAU,CAa7E;AAgBD,+DAA+D;AAC/D,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,UAAU,GAAG,gBAAgB,CAgC1E;AAMD;;;;;;GAMG;AACH,wBAAgB,qBAAqB,CACnC,QAAQ,EAAE,gBAAgB,EAC1B,IAAI,EAAE;IAAE,SAAS,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,GACnE,IAAI,CAiBN;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAsB,2CAA2C,CAAC,KAAK,EAAE;IACvE,WAAW,EAAE,UAAU,CAAC;IACxB,cAAc,EAAE,UAAU,CAAC;IAC3B,uEAAuE;IACvE,KAAK,EAAE,UAAU,CAAC;IAClB,2DAA2D;IAC3D,WAAW,EAAE,UAAU,EAAE,CAAC;IAC1B,yCAAyC;IACzC,IAAI,EAAE,MAAM,CAAC;CACd,GAAG,OAAO,CAAC,mBAAmB,CAAC,CAiD/B"}
@@ -20,14 +20,34 @@
20
20
  * (approach A, open) will derive per-log owner keys from genesis + public
21
21
  * tiles, closing that gap without key distribution.
22
22
  *
23
- * Note (parity with the server port source): this establishes that the root
24
- * authorized the delegated key, but does NOT yet enforce the certificate's MMR
25
- * window or expiry-at-issuance against the leaf — that hardening is shared with
26
- * the server path and tracked separately (FOR-323).
23
+ * Constraint enforcement (FOR-420): the certificate authorizes the delegated
24
+ * key only over an MMR coverage window `[mmrStart, mmrEnd]` (payload labels 3/4)
25
+ * and until `expiresAt` (label 9, Unix seconds). {@link checkDelegationConstraints}
26
+ * enforces the soundly-offline-decidable slice against the verified leaf: the
27
+ * `mmrEnd` over-horizon bound (the leaf's index lower-bounds the checkpoint
28
+ * `treeSize-1` the on-chain `delegationVerifier.sol` binds) and expiry-at-issuance
29
+ * against the leaf's snowflake idtimestamp (never wall-clock — receipts must
30
+ * verify forever). The `mmrStart` lower bound, the exact `size-1` bound, and the
31
+ * `issuedAt` lower bound are NOT enforced (see that function). This replaces the
32
+ * earlier "does not yet enforce the window" gap.
27
33
  */
34
+ export declare function idtimestampToUnixSeconds(idtimestamp: bigint): number;
35
+ /**
36
+ * Coverage + validity window a root-signed delegation cert imposes on its
37
+ * delegated key. `mmrStart`/`mmrEnd` are inclusive MMR-index bounds;
38
+ * `issuedAt`/`expiresAt` are Unix seconds.
39
+ */
40
+ export type DelegationConstraints = {
41
+ mmrStart: bigint;
42
+ mmrEnd: bigint;
43
+ issuedAt: number;
44
+ expiresAt: number;
45
+ };
28
46
  /**
29
47
  * root-only: no delegation cert present — verify against the root keys as-is.
30
48
  * resolved: cert verified under a root key; use `delegatedKey` first.
49
+ * `constraints` carries the cert's coverage/validity window when the payload
50
+ * declares it (null for legacy certs that predate labels 3/4/8/9).
31
51
  * broken: a cert is present but did not verify under the root, or its delegated
32
52
  * key could not be parsed — the delegation chain is invalid.
33
53
  */
@@ -36,9 +56,44 @@ export type DelegatedResolution = {
36
56
  } | {
37
57
  kind: "resolved";
38
58
  delegatedKey: CryptoKey;
59
+ constraints: DelegationConstraints | null;
39
60
  } | {
40
61
  kind: "broken";
41
62
  };
63
+ /**
64
+ * Enforce a resolved cert's constraints against the verified leaf (FOR-420).
65
+ *
66
+ * Coverage: the delegation authorizes checkpoint positions `treeSize-1 ∈
67
+ * [mmrStart, mmrEnd]` (inclusive, `delegationVerifier.sol`). Offline we hold the
68
+ * verified leaf's `mmrIndex`, and `leafMmrIndex ≤ treeSize-1` (the leaf is
69
+ * included in the checkpoint). So `leafMmrIndex > mmrEnd` SOUNDLY implies
70
+ * `treeSize-1 > mmrEnd` — a key signing beyond its authorized horizon — and is
71
+ * rejected. The `mmrStart` lower bound is deliberately NOT enforced here: an
72
+ * early leaf can legitimately appear in a checkpoint whose `size-1 ≥ mmrStart`,
73
+ * so `leafMmrIndex < mmrStart` does not imply a violation and enforcing it would
74
+ * false-reject valid receipts under a narrow cert. The lower bound and the exact
75
+ * `size-1` upper bound need the checkpoint accumulator (absent in the single-peak
76
+ * offline path) — deferred; in practice lane certs are wide (`mmrStart=0`).
77
+ *
78
+ * Expiry-at-issuance: the leaf's time (from its snowflake idtimestamp) must not
79
+ * be AFTER the cert's `expiresAt` — a key signing past its authorized lifetime.
80
+ * Compared against the checkpoint/leaf time, NOT wall-clock, so a valid receipt
81
+ * verifies forever (the pinned FOR-297 / ADR-0050 requirement).
82
+ *
83
+ * The `issuedAt` lower bound is deliberately NOT enforced. `issuedAt` is when the
84
+ * cert was SIGNED; the leaf's idtimestamp is when it was SEQUENCED. On live lanes
85
+ * the delegation cert is signed a few seconds AFTER the leaf is sequenced (the
86
+ * checkpoint that includes the leaf is sealed reactively), so `issuedAt > leafTime`
87
+ * is normal and correct — the cert is never *used* to sign before it exists.
88
+ * Enforcing `issuedAt ≤ leafTime` false-rejects legitimate receipts (verified on
89
+ * lane A: a fresh bootstrap receipt has `issuedAt − leafTime ≈ 3s`).
90
+ */
91
+ export declare function checkDelegationConstraints(constraints: DelegationConstraints, leafMmrIndex: bigint, leafIdtimestamp: bigint): {
92
+ ok: true;
93
+ } | {
94
+ ok: false;
95
+ reason: string;
96
+ };
42
97
  /**
43
98
  * Resolve the delegated verify key from a receipt's delegation certificate.
44
99
  *
@@ -1 +1 @@
1
- {"version":3,"file":"resolve-delegated-verify-key.d.ts","sourceRoot":"","sources":["../src/resolve-delegated-verify-key.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAuBH;;;;;GAKG;AACH,MAAM,MAAM,mBAAmB,GAC3B;IAAE,IAAI,EAAE,WAAW,CAAA;CAAE,GACrB;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,YAAY,EAAE,SAAS,CAAA;CAAE,GAC7C;IAAE,IAAI,EAAE,QAAQ,CAAA;CAAE,CAAC;AAqEvB;;;;;GAKG;AACH,wBAAsB,yBAAyB,CAC7C,WAAW,EAAE,UAAU,EACvB,QAAQ,EAAE,SAAS,EAAE,GACpB,OAAO,CAAC,mBAAmB,CAAC,CA4B9B"}
1
+ {"version":3,"file":"resolve-delegated-verify-key.d.ts","sourceRoot":"","sources":["../src/resolve-delegated-verify-key.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AAqCH,wBAAgB,wBAAwB,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CAKpE;AAED;;;;GAIG;AACH,MAAM,MAAM,qBAAqB,GAAG;IAClC,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,MAAM,mBAAmB,GAC3B;IAAE,IAAI,EAAE,WAAW,CAAA;CAAE,GACrB;IACE,IAAI,EAAE,UAAU,CAAC;IACjB,YAAY,EAAE,SAAS,CAAC;IACxB,WAAW,EAAE,qBAAqB,GAAG,IAAI,CAAC;CAC3C,GACD;IAAE,IAAI,EAAE,QAAQ,CAAA;CAAE,CAAC;AAsGvB;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,0BAA0B,CACxC,WAAW,EAAE,qBAAqB,EAClC,YAAY,EAAE,MAAM,EACpB,eAAe,EAAE,MAAM,GACtB;IAAE,EAAE,EAAE,IAAI,CAAA;CAAE,GAAG;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAS9C;AAED;;;;;GAKG;AACH,wBAAsB,yBAAyB,CAC7C,WAAW,EAAE,UAAU,EACvB,QAAQ,EAAE,SAAS,EAAE,GACpB,OAAO,CAAC,mBAAmB,CAAC,CAgC9B"}
@@ -20,10 +20,16 @@
20
20
  * (approach A, open) will derive per-log owner keys from genesis + public
21
21
  * tiles, closing that gap without key distribution.
22
22
  *
23
- * Note (parity with the server port source): this establishes that the root
24
- * authorized the delegated key, but does NOT yet enforce the certificate's MMR
25
- * window or expiry-at-issuance against the leaf — that hardening is shared with
26
- * the server path and tracked separately (FOR-323).
23
+ * Constraint enforcement (FOR-420): the certificate authorizes the delegated
24
+ * key only over an MMR coverage window `[mmrStart, mmrEnd]` (payload labels 3/4)
25
+ * and until `expiresAt` (label 9, Unix seconds). {@link checkDelegationConstraints}
26
+ * enforces the soundly-offline-decidable slice against the verified leaf: the
27
+ * `mmrEnd` over-horizon bound (the leaf's index lower-bounds the checkpoint
28
+ * `treeSize-1` the on-chain `delegationVerifier.sol` binds) and expiry-at-issuance
29
+ * against the leaf's snowflake idtimestamp (never wall-clock — receipts must
30
+ * verify forever). The `mmrStart` lower bound, the exact `size-1` bound, and the
31
+ * `issuedAt` lower bound are NOT enforced (see that function). This replaces the
32
+ * earlier "does not yet enforce the window" gap.
27
33
  */
28
34
  import { coseUnprotectedToMap, decodeCborDeterministic, decodeCoseSign1, verifyCoseSign1WithParsedKey, } from "@forestrie/encoding";
29
35
  /** Unprotected header label carrying the delegation certificate. */
@@ -35,8 +41,26 @@ const COSE_KEY_X = -2;
35
41
  const COSE_KEY_Y = -3;
36
42
  const COSE_KTY_EC2 = 2;
37
43
  const COSE_CRV_P256 = 1;
38
- /** Delegation-cert payload label for the delegated COSE_Key. */
44
+ /** Delegation-cert payload labels (must match @forestrie/delegation-cose). */
45
+ const PAYLOAD_MMR_START = 3;
46
+ const PAYLOAD_MMR_END = 4;
39
47
  const PAYLOAD_DELEGATED_KEY = 5;
48
+ const PAYLOAD_ISSUED_AT = 8;
49
+ const PAYLOAD_EXPIRES_AT = 9;
50
+ /**
51
+ * Snowflake idtimestamp → Unix seconds (DataTrails scheme; arbor
52
+ * `snowflakeid`). `TimeShift = 24`, per-epoch span `2^40 - 1` ms, current
53
+ * `CommitmentEpoch = 1` (next epoch ~2038). `unixMs = epoch*(2^40-1) +
54
+ * (id >> 24)`.
55
+ */
56
+ const COMMITMENT_EPOCH = 1n;
57
+ const SNOWFLAKE_TIME_SHIFT = 24n;
58
+ const SNOWFLAKE_EPOCH_SPAN_MS = (1n << 40n) - 1n;
59
+ export function idtimestampToUnixSeconds(idtimestamp) {
60
+ const ms = COMMITMENT_EPOCH * SNOWFLAKE_EPOCH_SPAN_MS +
61
+ (idtimestamp >> SNOWFLAKE_TIME_SHIFT);
62
+ return Number(ms / 1000n);
63
+ }
40
64
  function extractDelegationCertBytes(unprotected) {
41
65
  const umap = coseUnprotectedToMap(unprotected);
42
66
  const certRaw = umap.get(DELEGATION_CERT_LABEL);
@@ -84,19 +108,91 @@ async function importDelegatedKey(keyMap) {
84
108
  return null;
85
109
  }
86
110
  }
87
- function extractDelegatedKeyFromPayload(payloadBytes) {
111
+ function decodeCertPayloadMap(payloadBytes) {
88
112
  if (!payloadBytes || payloadBytes.length === 0)
89
113
  return null;
90
- let payloadMap;
91
114
  try {
92
- payloadMap = decodeCborDeterministic(payloadBytes);
115
+ return decodeCborDeterministic(payloadBytes);
93
116
  }
94
117
  catch {
95
118
  return null;
96
119
  }
120
+ }
121
+ function extractDelegatedKeyFromPayloadMap(payloadMap) {
97
122
  const get = labelGetter(payloadMap);
98
123
  return get ? get(PAYLOAD_DELEGATED_KEY) : null;
99
124
  }
125
+ function toBigIntOrNull(v) {
126
+ if (typeof v === "bigint")
127
+ return v;
128
+ if (typeof v === "number" && Number.isInteger(v))
129
+ return BigInt(v);
130
+ return null;
131
+ }
132
+ function toNumberOrNull(v) {
133
+ if (typeof v === "bigint")
134
+ return Number(v);
135
+ if (typeof v === "number" && Number.isFinite(v))
136
+ return v;
137
+ return null;
138
+ }
139
+ /**
140
+ * Parse the cert's coverage/validity window from its payload map. Returns null
141
+ * when any of labels 3/4/8/9 is absent (legacy certs predating FOR-390 advance
142
+ * delegation) — enforcement is then skipped, preserving pre-constraint trust.
143
+ */
144
+ function parseDelegationConstraints(payloadMap) {
145
+ const get = labelGetter(payloadMap);
146
+ if (!get)
147
+ return null;
148
+ const mmrStart = toBigIntOrNull(get(PAYLOAD_MMR_START));
149
+ const mmrEnd = toBigIntOrNull(get(PAYLOAD_MMR_END));
150
+ const issuedAt = toNumberOrNull(get(PAYLOAD_ISSUED_AT));
151
+ const expiresAt = toNumberOrNull(get(PAYLOAD_EXPIRES_AT));
152
+ if (mmrStart === null || mmrEnd === null)
153
+ return null;
154
+ if (issuedAt === null || expiresAt === null)
155
+ return null;
156
+ return { mmrStart, mmrEnd, issuedAt, expiresAt };
157
+ }
158
+ /**
159
+ * Enforce a resolved cert's constraints against the verified leaf (FOR-420).
160
+ *
161
+ * Coverage: the delegation authorizes checkpoint positions `treeSize-1 ∈
162
+ * [mmrStart, mmrEnd]` (inclusive, `delegationVerifier.sol`). Offline we hold the
163
+ * verified leaf's `mmrIndex`, and `leafMmrIndex ≤ treeSize-1` (the leaf is
164
+ * included in the checkpoint). So `leafMmrIndex > mmrEnd` SOUNDLY implies
165
+ * `treeSize-1 > mmrEnd` — a key signing beyond its authorized horizon — and is
166
+ * rejected. The `mmrStart` lower bound is deliberately NOT enforced here: an
167
+ * early leaf can legitimately appear in a checkpoint whose `size-1 ≥ mmrStart`,
168
+ * so `leafMmrIndex < mmrStart` does not imply a violation and enforcing it would
169
+ * false-reject valid receipts under a narrow cert. The lower bound and the exact
170
+ * `size-1` upper bound need the checkpoint accumulator (absent in the single-peak
171
+ * offline path) — deferred; in practice lane certs are wide (`mmrStart=0`).
172
+ *
173
+ * Expiry-at-issuance: the leaf's time (from its snowflake idtimestamp) must not
174
+ * be AFTER the cert's `expiresAt` — a key signing past its authorized lifetime.
175
+ * Compared against the checkpoint/leaf time, NOT wall-clock, so a valid receipt
176
+ * verifies forever (the pinned FOR-297 / ADR-0050 requirement).
177
+ *
178
+ * The `issuedAt` lower bound is deliberately NOT enforced. `issuedAt` is when the
179
+ * cert was SIGNED; the leaf's idtimestamp is when it was SEQUENCED. On live lanes
180
+ * the delegation cert is signed a few seconds AFTER the leaf is sequenced (the
181
+ * checkpoint that includes the leaf is sealed reactively), so `issuedAt > leafTime`
182
+ * is normal and correct — the cert is never *used* to sign before it exists.
183
+ * Enforcing `issuedAt ≤ leafTime` false-rejects legitimate receipts (verified on
184
+ * lane A: a fresh bootstrap receipt has `issuedAt − leafTime ≈ 3s`).
185
+ */
186
+ export function checkDelegationConstraints(constraints, leafMmrIndex, leafIdtimestamp) {
187
+ if (leafMmrIndex > constraints.mmrEnd) {
188
+ return { ok: false, reason: "delegation_out_of_range" };
189
+ }
190
+ const t = idtimestampToUnixSeconds(leafIdtimestamp);
191
+ if (t > constraints.expiresAt) {
192
+ return { ok: false, reason: "delegation_expired" };
193
+ }
194
+ return { ok: true };
195
+ }
100
196
  /**
101
197
  * Resolve the delegated verify key from a receipt's delegation certificate.
102
198
  *
@@ -124,9 +220,13 @@ export async function resolveDelegatedVerifyKey(receiptCbor, rootKeys) {
124
220
  const certDecoded = decodeCoseSign1(certBytes);
125
221
  if (!certDecoded)
126
222
  return { kind: "broken" };
127
- const keyRaw = extractDelegatedKeyFromPayload(certDecoded.payloadBstr);
223
+ const payloadMap = decodeCertPayloadMap(certDecoded.payloadBstr);
224
+ const keyRaw = payloadMap
225
+ ? extractDelegatedKeyFromPayloadMap(payloadMap)
226
+ : null;
128
227
  const delegatedKey = keyRaw ? await importDelegatedKey(keyRaw) : null;
129
228
  if (!delegatedKey)
130
229
  return { kind: "broken" };
131
- return { kind: "resolved", delegatedKey };
230
+ const constraints = parseDelegationConstraints(payloadMap);
231
+ return { kind: "resolved", delegatedKey, constraints };
132
232
  }
@@ -1 +1 @@
1
- {"version":3,"file":"verify-grant-receipt-offline.d.ts","sourceRoot":"","sources":["../src/verify-grant-receipt-offline.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AAOjD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AAGtE,MAAM,MAAM,8BAA8B,GAAG;IAC3C,WAAW,EAAE,UAAU,CAAC;IACxB,WAAW,EAAE,UAAU,CAAC;IACxB,KAAK,EAAE,KAAK,CAAC;IACb,cAAc,EAAE,UAAU,CAAC;CAC5B,CAAC;AA4EF;;GAEG;AACH,wBAAsB,yBAAyB,CAC7C,KAAK,EAAE,8BAA8B,GACpC,OAAO,CAAC,mBAAmB,CAAC,CAc9B;AAED,MAAM,MAAM,sCAAsC,GAAG,IAAI,CACvD,8BAA8B,EAC9B,aAAa,CACd,GAAG;IACF,qFAAqF;IACrF,SAAS,EAAE,SAAS,EAAE,CAAC;CACxB,CAAC;AAEF;;;;GAIG;AACH,wBAAsB,iCAAiC,CACrD,KAAK,EAAE,sCAAsC,GAC5C,OAAO,CAAC,mBAAmB,CAAC,CAa9B;AAED,MAAM,MAAM,yBAAyB,GAAG;IACtC,WAAW,EAAE,UAAU,CAAC;IACxB,WAAW,EAAE,UAAU,CAAC;IACxB;;;;;OAKG;IACH,OAAO,EAAE,UAAU,CAAC;IACpB,cAAc,EAAE,UAAU,CAAC;CAC5B,CAAC;AAEF;;;;;;GAMG;AACH,wBAAsB,oBAAoB,CACxC,KAAK,EAAE,yBAAyB,GAC/B,OAAO,CAAC,mBAAmB,CAAC,CAa9B;AAED,MAAM,MAAM,iCAAiC,GAAG,IAAI,CAClD,yBAAyB,EACzB,aAAa,CACd,GAAG;IACF;;;;;OAKG;IACH,SAAS,EAAE,SAAS,EAAE,CAAC;CACxB,CAAC;AAEF;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAsB,4BAA4B,CAChD,KAAK,EAAE,iCAAiC,GACvC,OAAO,CAAC,mBAAmB,CAAC,CAa9B"}
1
+ {"version":3,"file":"verify-grant-receipt-offline.d.ts","sourceRoot":"","sources":["../src/verify-grant-receipt-offline.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AAUjD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AAGtE,MAAM,MAAM,8BAA8B,GAAG;IAC3C,WAAW,EAAE,UAAU,CAAC;IACxB,WAAW,EAAE,UAAU,CAAC;IACxB,KAAK,EAAE,KAAK,CAAC;IACb,cAAc,EAAE,UAAU,CAAC;CAC5B,CAAC;AA4EF;;GAEG;AACH,wBAAsB,yBAAyB,CAC7C,KAAK,EAAE,8BAA8B,GACpC,OAAO,CAAC,mBAAmB,CAAC,CAc9B;AAED,MAAM,MAAM,sCAAsC,GAAG,IAAI,CACvD,8BAA8B,EAC9B,aAAa,CACd,GAAG;IACF,qFAAqF;IACrF,SAAS,EAAE,SAAS,EAAE,CAAC;CACxB,CAAC;AAEF;;;;GAIG;AACH,wBAAsB,iCAAiC,CACrD,KAAK,EAAE,sCAAsC,GAC5C,OAAO,CAAC,mBAAmB,CAAC,CAa9B;AAED,MAAM,MAAM,yBAAyB,GAAG;IACtC,WAAW,EAAE,UAAU,CAAC;IACxB,WAAW,EAAE,UAAU,CAAC;IACxB;;;;;OAKG;IACH,OAAO,EAAE,UAAU,CAAC;IACpB,cAAc,EAAE,UAAU,CAAC;CAC5B,CAAC;AAEF;;;;;;GAMG;AACH,wBAAsB,oBAAoB,CACxC,KAAK,EAAE,yBAAyB,GAC/B,OAAO,CAAC,mBAAmB,CAAC,CAa9B;AAED,MAAM,MAAM,iCAAiC,GAAG,IAAI,CAClD,yBAAyB,EACzB,aAAa,CACd,GAAG;IACF;;;;;OAKG;IACH,SAAS,EAAE,SAAS,EAAE,CAAC;CACxB,CAAC;AAEF;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAsB,4BAA4B,CAChD,KAAK,EAAE,iCAAiC,GACvC,OAAO,CAAC,mBAAmB,CAAC,CAa9B"}
@@ -3,7 +3,7 @@ import { calculateRoot, verifyInclusion, } from "@forestrie/merklelog";
3
3
  import { grantCommitmentHashFromGrant } from "./grant-commitment.js";
4
4
  import { decodeTrustRootFromGenesis } from "./decode-trust-root-from-genesis.js";
5
5
  import { es256ReceiptVerifyKeys } from "./decode-trust-root-cbor.js";
6
- import { resolveDelegatedVerifyKey } from "./resolve-delegated-verify-key.js";
6
+ import { checkDelegationConstraints, resolveDelegatedVerifyKey, } from "./resolve-delegated-verify-key.js";
7
7
  import { univocityLeafHash } from "./leaf-commitment.js";
8
8
  import { parseReceipt } from "./parse-receipt.js";
9
9
  import { SubtleHasher } from "./subtle-hasher.js";
@@ -197,6 +197,17 @@ async function verifyReceiptOfflineWithLeafInnerKeys(input) {
197
197
  catch {
198
198
  return { ok: false, stage: "binding", reason: "idtimestamp_invalid" };
199
199
  }
200
+ // FOR-420: a resolved delegation cert authorizes its key only within an MMR
201
+ // coverage window and validity window. Enforce both before reporting the
202
+ // signature/inclusion as ok — a delegated key signing outside its authorized
203
+ // range, or after its expiry-at-issuance, fails at stage `signature`.
204
+ if (delegation.kind === "resolved" && delegation.constraints) {
205
+ const leafMmrIndex = parsed.proof.mmrIndex ?? parsed.proof.leafIndex ?? 0n;
206
+ const check = checkDelegationConstraints(delegation.constraints, leafMmrIndex, idtimestamp);
207
+ if (!check.ok) {
208
+ return { ok: false, stage: "signature", reason: check.reason };
209
+ }
210
+ }
200
211
  const leafHash = await univocityLeafHash(idtimestamp, input.inner);
201
212
  return verifySignatureAndInclusion({
202
213
  receiptCbor: input.receiptCbor,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@forestrie/receipt-verify",
3
- "version": "0.9.0",
3
+ "version": "0.10.1",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "description": "Offline SCITT grant receipt verification (ADR-0045)",
@@ -28,8 +28,8 @@
28
28
  ],
29
29
  "dependencies": {
30
30
  "@noble/hashes": "^1.7.1",
31
- "@forestrie/merklelog": "0.3.0",
32
31
  "@forestrie/chain-rpc": "0.1.1",
32
+ "@forestrie/merklelog": "0.3.0",
33
33
  "@forestrie/encoding": "0.5.0"
34
34
  },
35
35
  "devDependencies": {
package/src/index.ts CHANGED
@@ -47,8 +47,15 @@ export type {
47
47
  VerifyGrantReceiptOfflineWithKeysInput,
48
48
  VerifyReceiptOfflineWithKeysInput,
49
49
  } from "./verify-grant-receipt-offline.js";
50
- export { resolveDelegatedVerifyKey } from "./resolve-delegated-verify-key.js";
51
- export type { DelegatedResolution } from "./resolve-delegated-verify-key.js";
50
+ export {
51
+ checkDelegationConstraints,
52
+ idtimestampToUnixSeconds,
53
+ resolveDelegatedVerifyKey,
54
+ } from "./resolve-delegated-verify-key.js";
55
+ export type {
56
+ DelegatedResolution,
57
+ DelegationConstraints,
58
+ } from "./resolve-delegated-verify-key.js";
52
59
  /** Import a raw 64-byte x||y P-256 public key as an ES256 verify key. */
53
60
  export { importEs256PublicKeyFromGrantDataXy64 } from "./decode-trust-root-cbor.js";
54
61
  export { decodeForestrieGrantCose } from "./decode-forestrie-grant-cose.js";
@@ -50,7 +50,9 @@ import type { ReceiptVerifyResult } from "./receipt-verify-result.js";
50
50
  function toContractLogId(logId: string): string {
51
51
  const hex = logId.replace(/-/g, "").replace(/^0x/, "").toLowerCase();
52
52
  if (!/^[0-9a-f]{32}$/.test(hex) && !/^[0-9a-f]{64}$/.test(hex)) {
53
- throw new Error(`log id must be a UUID or 16/32-byte hex id, got '${logId}'`);
53
+ throw new Error(
54
+ `log id must be a UUID or 16/32-byte hex id, got '${logId}'`,
55
+ );
54
56
  }
55
57
  return "0x" + hex.padStart(64, "0");
56
58
  }
@@ -247,7 +249,12 @@ export async function verifyReceiptOfflineAgainstKnownAccumulator(input: {
247
249
  } else {
248
250
  const hasher = new SubtleHasher();
249
251
  const leafHash = await univocityLeafHash(input.idtimestampBe8, input.inner);
250
- recomputedPeak = await calculateRoot(hasher, leafHash, parsed.proof, leafIdx);
252
+ recomputedPeak = await calculateRoot(
253
+ hasher,
254
+ leafHash,
255
+ parsed.proof,
256
+ leafIdx,
257
+ );
251
258
  }
252
259
 
253
260
  // 2. Exact peak match — receipt state is a snapshot-covered accumulator.
@@ -20,10 +20,16 @@
20
20
  * (approach A, open) will derive per-log owner keys from genesis + public
21
21
  * tiles, closing that gap without key distribution.
22
22
  *
23
- * Note (parity with the server port source): this establishes that the root
24
- * authorized the delegated key, but does NOT yet enforce the certificate's MMR
25
- * window or expiry-at-issuance against the leaf — that hardening is shared with
26
- * the server path and tracked separately (FOR-323).
23
+ * Constraint enforcement (FOR-420): the certificate authorizes the delegated
24
+ * key only over an MMR coverage window `[mmrStart, mmrEnd]` (payload labels 3/4)
25
+ * and until `expiresAt` (label 9, Unix seconds). {@link checkDelegationConstraints}
26
+ * enforces the soundly-offline-decidable slice against the verified leaf: the
27
+ * `mmrEnd` over-horizon bound (the leaf's index lower-bounds the checkpoint
28
+ * `treeSize-1` the on-chain `delegationVerifier.sol` binds) and expiry-at-issuance
29
+ * against the leaf's snowflake idtimestamp (never wall-clock — receipts must
30
+ * verify forever). The `mmrStart` lower bound, the exact `size-1` bound, and the
31
+ * `issuedAt` lower bound are NOT enforced (see that function). This replaces the
32
+ * earlier "does not yet enforce the window" gap.
27
33
  */
28
34
 
29
35
  import {
@@ -44,18 +50,57 @@ const COSE_KEY_Y = -3;
44
50
  const COSE_KTY_EC2 = 2;
45
51
  const COSE_CRV_P256 = 1;
46
52
 
47
- /** Delegation-cert payload label for the delegated COSE_Key. */
53
+ /** Delegation-cert payload labels (must match @forestrie/delegation-cose). */
54
+ const PAYLOAD_MMR_START = 3;
55
+ const PAYLOAD_MMR_END = 4;
48
56
  const PAYLOAD_DELEGATED_KEY = 5;
57
+ const PAYLOAD_ISSUED_AT = 8;
58
+ const PAYLOAD_EXPIRES_AT = 9;
59
+
60
+ /**
61
+ * Snowflake idtimestamp → Unix seconds (DataTrails scheme; arbor
62
+ * `snowflakeid`). `TimeShift = 24`, per-epoch span `2^40 - 1` ms, current
63
+ * `CommitmentEpoch = 1` (next epoch ~2038). `unixMs = epoch*(2^40-1) +
64
+ * (id >> 24)`.
65
+ */
66
+ const COMMITMENT_EPOCH = 1n;
67
+ const SNOWFLAKE_TIME_SHIFT = 24n;
68
+ const SNOWFLAKE_EPOCH_SPAN_MS = (1n << 40n) - 1n;
69
+
70
+ export function idtimestampToUnixSeconds(idtimestamp: bigint): number {
71
+ const ms =
72
+ COMMITMENT_EPOCH * SNOWFLAKE_EPOCH_SPAN_MS +
73
+ (idtimestamp >> SNOWFLAKE_TIME_SHIFT);
74
+ return Number(ms / 1000n);
75
+ }
76
+
77
+ /**
78
+ * Coverage + validity window a root-signed delegation cert imposes on its
79
+ * delegated key. `mmrStart`/`mmrEnd` are inclusive MMR-index bounds;
80
+ * `issuedAt`/`expiresAt` are Unix seconds.
81
+ */
82
+ export type DelegationConstraints = {
83
+ mmrStart: bigint;
84
+ mmrEnd: bigint;
85
+ issuedAt: number;
86
+ expiresAt: number;
87
+ };
49
88
 
50
89
  /**
51
90
  * root-only: no delegation cert present — verify against the root keys as-is.
52
91
  * resolved: cert verified under a root key; use `delegatedKey` first.
92
+ * `constraints` carries the cert's coverage/validity window when the payload
93
+ * declares it (null for legacy certs that predate labels 3/4/8/9).
53
94
  * broken: a cert is present but did not verify under the root, or its delegated
54
95
  * key could not be parsed — the delegation chain is invalid.
55
96
  */
56
97
  export type DelegatedResolution =
57
98
  | { kind: "root-only" }
58
- | { kind: "resolved"; delegatedKey: CryptoKey }
99
+ | {
100
+ kind: "resolved";
101
+ delegatedKey: CryptoKey;
102
+ constraints: DelegationConstraints | null;
103
+ }
59
104
  | { kind: "broken" };
60
105
 
61
106
  function extractDelegationCertBytes(unprotected: unknown): Uint8Array | null {
@@ -111,20 +156,96 @@ async function importDelegatedKey(keyMap: unknown): Promise<CryptoKey | null> {
111
156
  }
112
157
  }
113
158
 
114
- function extractDelegatedKeyFromPayload(
115
- payloadBytes: Uint8Array,
116
- ): unknown | null {
159
+ function decodeCertPayloadMap(payloadBytes: Uint8Array): unknown | null {
117
160
  if (!payloadBytes || payloadBytes.length === 0) return null;
118
- let payloadMap: unknown;
119
161
  try {
120
- payloadMap = decodeCborDeterministic(payloadBytes);
162
+ return decodeCborDeterministic(payloadBytes);
121
163
  } catch {
122
164
  return null;
123
165
  }
166
+ }
167
+
168
+ function extractDelegatedKeyFromPayloadMap(
169
+ payloadMap: unknown,
170
+ ): unknown | null {
124
171
  const get = labelGetter(payloadMap);
125
172
  return get ? get(PAYLOAD_DELEGATED_KEY) : null;
126
173
  }
127
174
 
175
+ function toBigIntOrNull(v: unknown): bigint | null {
176
+ if (typeof v === "bigint") return v;
177
+ if (typeof v === "number" && Number.isInteger(v)) return BigInt(v);
178
+ return null;
179
+ }
180
+
181
+ function toNumberOrNull(v: unknown): number | null {
182
+ if (typeof v === "bigint") return Number(v);
183
+ if (typeof v === "number" && Number.isFinite(v)) return v;
184
+ return null;
185
+ }
186
+
187
+ /**
188
+ * Parse the cert's coverage/validity window from its payload map. Returns null
189
+ * when any of labels 3/4/8/9 is absent (legacy certs predating FOR-390 advance
190
+ * delegation) — enforcement is then skipped, preserving pre-constraint trust.
191
+ */
192
+ function parseDelegationConstraints(
193
+ payloadMap: unknown,
194
+ ): DelegationConstraints | null {
195
+ const get = labelGetter(payloadMap);
196
+ if (!get) return null;
197
+ const mmrStart = toBigIntOrNull(get(PAYLOAD_MMR_START));
198
+ const mmrEnd = toBigIntOrNull(get(PAYLOAD_MMR_END));
199
+ const issuedAt = toNumberOrNull(get(PAYLOAD_ISSUED_AT));
200
+ const expiresAt = toNumberOrNull(get(PAYLOAD_EXPIRES_AT));
201
+ if (mmrStart === null || mmrEnd === null) return null;
202
+ if (issuedAt === null || expiresAt === null) return null;
203
+ return { mmrStart, mmrEnd, issuedAt, expiresAt };
204
+ }
205
+
206
+ /**
207
+ * Enforce a resolved cert's constraints against the verified leaf (FOR-420).
208
+ *
209
+ * Coverage: the delegation authorizes checkpoint positions `treeSize-1 ∈
210
+ * [mmrStart, mmrEnd]` (inclusive, `delegationVerifier.sol`). Offline we hold the
211
+ * verified leaf's `mmrIndex`, and `leafMmrIndex ≤ treeSize-1` (the leaf is
212
+ * included in the checkpoint). So `leafMmrIndex > mmrEnd` SOUNDLY implies
213
+ * `treeSize-1 > mmrEnd` — a key signing beyond its authorized horizon — and is
214
+ * rejected. The `mmrStart` lower bound is deliberately NOT enforced here: an
215
+ * early leaf can legitimately appear in a checkpoint whose `size-1 ≥ mmrStart`,
216
+ * so `leafMmrIndex < mmrStart` does not imply a violation and enforcing it would
217
+ * false-reject valid receipts under a narrow cert. The lower bound and the exact
218
+ * `size-1` upper bound need the checkpoint accumulator (absent in the single-peak
219
+ * offline path) — deferred; in practice lane certs are wide (`mmrStart=0`).
220
+ *
221
+ * Expiry-at-issuance: the leaf's time (from its snowflake idtimestamp) must not
222
+ * be AFTER the cert's `expiresAt` — a key signing past its authorized lifetime.
223
+ * Compared against the checkpoint/leaf time, NOT wall-clock, so a valid receipt
224
+ * verifies forever (the pinned FOR-297 / ADR-0050 requirement).
225
+ *
226
+ * The `issuedAt` lower bound is deliberately NOT enforced. `issuedAt` is when the
227
+ * cert was SIGNED; the leaf's idtimestamp is when it was SEQUENCED. On live lanes
228
+ * the delegation cert is signed a few seconds AFTER the leaf is sequenced (the
229
+ * checkpoint that includes the leaf is sealed reactively), so `issuedAt > leafTime`
230
+ * is normal and correct — the cert is never *used* to sign before it exists.
231
+ * Enforcing `issuedAt ≤ leafTime` false-rejects legitimate receipts (verified on
232
+ * lane A: a fresh bootstrap receipt has `issuedAt − leafTime ≈ 3s`).
233
+ */
234
+ export function checkDelegationConstraints(
235
+ constraints: DelegationConstraints,
236
+ leafMmrIndex: bigint,
237
+ leafIdtimestamp: bigint,
238
+ ): { ok: true } | { ok: false; reason: string } {
239
+ if (leafMmrIndex > constraints.mmrEnd) {
240
+ return { ok: false, reason: "delegation_out_of_range" };
241
+ }
242
+ const t = idtimestampToUnixSeconds(leafIdtimestamp);
243
+ if (t > constraints.expiresAt) {
244
+ return { ok: false, reason: "delegation_expired" };
245
+ }
246
+ return { ok: true };
247
+ }
248
+
128
249
  /**
129
250
  * Resolve the delegated verify key from a receipt's delegation certificate.
130
251
  *
@@ -157,9 +278,13 @@ export async function resolveDelegatedVerifyKey(
157
278
  const certDecoded = decodeCoseSign1(certBytes);
158
279
  if (!certDecoded) return { kind: "broken" };
159
280
 
160
- const keyRaw = extractDelegatedKeyFromPayload(certDecoded.payloadBstr);
281
+ const payloadMap = decodeCertPayloadMap(certDecoded.payloadBstr);
282
+ const keyRaw = payloadMap
283
+ ? extractDelegatedKeyFromPayloadMap(payloadMap)
284
+ : null;
161
285
  const delegatedKey = keyRaw ? await importDelegatedKey(keyRaw) : null;
162
286
  if (!delegatedKey) return { kind: "broken" };
163
287
 
164
- return { kind: "resolved", delegatedKey };
288
+ const constraints = parseDelegationConstraints(payloadMap);
289
+ return { kind: "resolved", delegatedKey, constraints };
165
290
  }
@@ -8,7 +8,10 @@ import type { Grant } from "@forestrie/encoding";
8
8
  import { grantCommitmentHashFromGrant } from "./grant-commitment.js";
9
9
  import { decodeTrustRootFromGenesis } from "./decode-trust-root-from-genesis.js";
10
10
  import { es256ReceiptVerifyKeys } from "./decode-trust-root-cbor.js";
11
- import { resolveDelegatedVerifyKey } from "./resolve-delegated-verify-key.js";
11
+ import {
12
+ checkDelegationConstraints,
13
+ resolveDelegatedVerifyKey,
14
+ } from "./resolve-delegated-verify-key.js";
12
15
  import { univocityLeafHash } from "./leaf-commitment.js";
13
16
  import { parseReceipt } from "./parse-receipt.js";
14
17
  import type { ReceiptVerifyResult } from "./receipt-verify-result.js";
@@ -309,6 +312,22 @@ async function verifyReceiptOfflineWithLeafInnerKeys(input: {
309
312
  return { ok: false, stage: "binding", reason: "idtimestamp_invalid" };
310
313
  }
311
314
 
315
+ // FOR-420: a resolved delegation cert authorizes its key only within an MMR
316
+ // coverage window and validity window. Enforce both before reporting the
317
+ // signature/inclusion as ok — a delegated key signing outside its authorized
318
+ // range, or after its expiry-at-issuance, fails at stage `signature`.
319
+ if (delegation.kind === "resolved" && delegation.constraints) {
320
+ const leafMmrIndex = parsed.proof.mmrIndex ?? parsed.proof.leafIndex ?? 0n;
321
+ const check = checkDelegationConstraints(
322
+ delegation.constraints,
323
+ leafMmrIndex,
324
+ idtimestamp,
325
+ );
326
+ if (!check.ok) {
327
+ return { ok: false, stage: "signature", reason: check.reason };
328
+ }
329
+ }
330
+
312
331
  const leafHash = await univocityLeafHash(idtimestamp, input.inner);
313
332
 
314
333
  return verifySignatureAndInclusion({