@orangecheck/agent-core 0.1.0 → 0.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/src/verify.ts CHANGED
@@ -7,6 +7,7 @@ import {
7
7
  actionCanonicalBytes,
8
8
  actionCanonicalMessage,
9
9
  canonicalizeScopes,
10
+ computeSubdelegationId,
10
11
  delegationCanonicalBytes,
11
12
  delegationCanonicalMessage,
12
13
  hexEncode,
@@ -25,13 +26,19 @@ import {
25
26
  ENVELOPE_VERSION,
26
27
  type ActionEnvelope,
27
28
  type AgentErrorCode,
29
+ type ChainLink,
28
30
  type DelegationEnvelope,
29
31
  type RevocationEnvelope,
32
+ type SubdelegationEnvelope,
30
33
  type VerifyActionResult,
31
34
  type VerifyDelegationResult,
32
35
  type VerifyRevocationResult,
36
+ type VerifySubdelegationResult,
33
37
  } from './types.js';
34
38
 
39
+ /** Default maximum chain depth, per SUB-DELEGATION.md §2.1. */
40
+ export const DEFAULT_MAX_CHAIN_DEPTH = 5;
41
+
35
42
  // ─────────────────────────────────────────────────────────────────────────────
36
43
  // Shared options
37
44
  // ─────────────────────────────────────────────────────────────────────────────
@@ -146,11 +153,28 @@ export async function verifyDelegation(input: VerifyDelegationInput): Promise<Ve
146
153
 
147
154
  export interface VerifyActionInput extends VerifyBase {
148
155
  action: ActionEnvelope;
149
- /** The delegation cited by action.delegation_id. Required. */
156
+ /** The ROOT delegation rooting the authority chain. Always required. */
150
157
  delegation: DelegationEnvelope;
151
158
  /**
152
- * Known revocations targeting the delegation. The verifier scans for one whose
153
- * effective time precedes the action (SPEC §9.3).
159
+ * Optional v1.1 sub-delegation chain from S_1 (immediate child of `delegation`)
160
+ * to S_leaf (the envelope `action.delegation_id` cites). When provided, the
161
+ * verifier walks each link checking parent-id linkage, principal-equals-
162
+ * parent-agent, scope containment, and temporal containment. The action's
163
+ * delegation_id MUST equal the leaf's id; the action's signer MUST equal
164
+ * the leaf's agent. See SUB-DELEGATION.md §2.2.
165
+ */
166
+ subdelegationChain?: SubdelegationEnvelope[];
167
+ /**
168
+ * Maximum permitted chain depth (number of subdelegations).
169
+ * Default `DEFAULT_MAX_CHAIN_DEPTH` (5). Verifiers MAY lower; MUST NOT raise
170
+ * silently above their advertised cap. Chains exceeding this fail with
171
+ * E_SUBDELEGATION_DEPTH_EXCEEDED before any per-link work is performed.
172
+ */
173
+ maxChainDepth?: number;
174
+ /**
175
+ * Known revocations targeting any envelope in the chain (root + each
176
+ * subdelegation). The verifier checks every link per SUB-DELEGATION.md §2.2
177
+ * step 5 — a revocation against ANY link invalidates the action.
154
178
  */
155
179
  revocations?: RevocationEnvelope[];
156
180
  content?: Uint8Array;
@@ -163,7 +187,17 @@ export async function verifyAction(input: VerifyActionInput): Promise<VerifyActi
163
187
  const a = input.action;
164
188
  const d = input.delegation;
165
189
 
166
- // 1. First verify the delegation.
190
+ // 0. Chain-depth check (SUB-DELEGATION.md §2.1) — before any per-link work.
191
+ const chain: SubdelegationEnvelope[] = input.subdelegationChain ?? [];
192
+ const maxDepth = input.maxChainDepth ?? DEFAULT_MAX_CHAIN_DEPTH;
193
+ if (chain.length > maxDepth) {
194
+ return err(
195
+ 'E_SUBDELEGATION_DEPTH_EXCEEDED',
196
+ `chain depth ${chain.length} exceeds maximum ${maxDepth}`
197
+ );
198
+ }
199
+
200
+ // 1. First verify the root delegation.
167
201
  const dr = await verifyDelegation({
168
202
  envelope: d,
169
203
  verifyBip322: input.verifyBip322,
@@ -173,6 +207,16 @@ export async function verifyAction(input: VerifyActionInput): Promise<VerifyActi
173
207
  });
174
208
  if (!dr.ok) return dr;
175
209
 
210
+ // 1b. Walk the sub-delegation chain (SUB-DELEGATION.md §2.2 step 3).
211
+ let parent: ChainLink = d;
212
+ for (const sub of chain) {
213
+ const r = await verifyChainLink(sub, parent, input);
214
+ if (!r.ok) return r;
215
+ parent = sub;
216
+ }
217
+ /** The envelope `action.delegation_id` should cite (root if no chain, leaf otherwise). */
218
+ const leaf: ChainLink = chain.length > 0 ? chain[chain.length - 1]! : d;
219
+
176
220
  // 2. Core action checks.
177
221
  if (a.v !== ENVELOPE_VERSION) {
178
222
  return err('E_UNSUPPORTED_VERSION', `action version ${a.v} not supported`);
@@ -201,30 +245,31 @@ export async function verifyAction(input: VerifyActionInput): Promise<VerifyActi
201
245
  if (!ok) return err('E_BAD_ACTION_STAMP', 'action BIP-322 signature did not verify');
202
246
  }
203
247
 
204
- // 3. Authority chain.
205
- if (a.delegation_id !== d.id) {
206
- return err('E_DELEGATION_MISMATCH', `action.delegation_id (${a.delegation_id}) != delegation.id (${d.id})`);
248
+ // 3. Authority chain — leaf-binding (action cites the leaf of the chain,
249
+ // which is the root delegation when no subdelegation chain is present).
250
+ if (a.delegation_id !== leaf.id) {
251
+ return err('E_DELEGATION_MISMATCH', `action.delegation_id (${a.delegation_id}) != leaf.id (${leaf.id})`);
207
252
  }
208
- if (a.signer.address !== d.agent.address) {
209
- return err('E_AGENT_MISMATCH', `action signer (${a.signer.address}) != delegation.agent (${d.agent.address})`);
253
+ if (a.signer.address !== leaf.agent.address) {
254
+ return err('E_AGENT_MISMATCH', `action signer (${a.signer.address}) != leaf.agent (${leaf.agent.address})`);
210
255
  }
211
256
 
212
- // 4. Window.
213
- const issued = new Date(d.issued_at).getTime();
214
- const expires = new Date(d.expires_at).getTime();
257
+ // 4. Window — against the leaf.
258
+ const issued = new Date(leaf.issued_at).getTime();
259
+ const expires = new Date(leaf.expires_at).getTime();
215
260
  const signed = new Date(a.signed_at).getTime();
216
261
  if (Number.isNaN(issued) || Number.isNaN(expires) || Number.isNaN(signed)) {
217
262
  return err('E_MALFORMED', 'unparseable ISO 8601 timestamp');
218
263
  }
219
264
  if (signed < issued || signed >= expires) {
220
- return err('E_OUT_OF_WINDOW', `action.signed_at ${a.signed_at} is outside delegation window [${d.issued_at}, ${d.expires_at})`);
265
+ return err('E_OUT_OF_WINDOW', `action.signed_at ${a.signed_at} is outside leaf window [${leaf.issued_at}, ${leaf.expires_at})`);
221
266
  }
222
267
 
223
- // 5. Scope containment.
268
+ // 5. Scope containment — against the leaf's granted set.
224
269
  let exercised, accepted;
225
270
  try {
226
271
  exercised = canonicalizeScope(parseScope(a.scope_exercised));
227
- const granted = d.scopes.map((s) => parseScope(s));
272
+ const granted = leaf.scopes.map((s) => parseScope(s));
228
273
  const exercisedParsed = parseScope(a.scope_exercised);
229
274
  validateScope(exercisedParsed, { mode: input.scopeMode ?? 'strict' });
230
275
  accepted = granted.some((g) => isSubScope(exercisedParsed, g));
@@ -234,22 +279,29 @@ export async function verifyAction(input: VerifyActionInput): Promise<VerifyActi
234
279
  }
235
280
  if (!accepted) return err('E_SCOPE_DENIED', `scope_exercised (${exercised}) not a sub-scope of any granted scope`);
236
281
 
237
- // 6. Revocation check.
282
+ // 6. Revocation check — applies per-link to ALL envelopes in the chain
283
+ // (root + every subdelegation). Per SUB-DELEGATION.md §2.2 step 5, a
284
+ // revocation against ANY link invalidates the action.
238
285
  if (input.revocations && input.revocations.length > 0) {
239
- for (const rev of input.revocations) {
240
- if (rev.delegation_id !== d.id) continue;
241
- // Verify the revocation itself (signature + canonical) with the same BIP-322 verifier.
242
- const rr = await verifyRevocation({
243
- envelope: rev,
244
- delegation: d,
245
- verifyBip322: input.verifyBip322,
246
- skipSignatureVerification: input.skipSignatureVerification,
247
- });
248
- if (!rr.ok) continue; // malformed revocations don't affect the action
249
- const effective = effectiveRevocationTime(rev, input.resolveAnchorBlockHeight);
250
- const actionTime = actionEffectiveTime(a, input.resolveAnchorBlockHeight);
251
- if (compareTimes(effective, actionTime) <= 0) {
252
- return err('E_REVOKED', `delegation was revoked by ${rev.id} before action was signed`);
286
+ const allLinks: ChainLink[] = [d, ...chain];
287
+ for (const link of allLinks) {
288
+ for (const rev of input.revocations) {
289
+ if (rev.delegation_id !== link.id) continue;
290
+ // Verify the revocation itself (signature + canonical + signer
291
+ // authorization). verifyRevocation accepts ChainLink, so the
292
+ // call shape is identical for root vs sub.
293
+ const rr = await verifyRevocation({
294
+ envelope: rev,
295
+ delegation: link,
296
+ verifyBip322: input.verifyBip322,
297
+ skipSignatureVerification: input.skipSignatureVerification,
298
+ });
299
+ if (!rr.ok) continue; // malformed revocations don't affect the action
300
+ const effective = effectiveRevocationTime(rev, input.resolveAnchorBlockHeight);
301
+ const actionTime = actionEffectiveTime(a, input.resolveAnchorBlockHeight);
302
+ if (compareTimes(effective, actionTime) <= 0) {
303
+ return err('E_REVOKED', `chain link ${link.id} was revoked by ${rev.id} before action was signed`);
304
+ }
253
305
  }
254
306
  }
255
307
  }
@@ -291,6 +343,7 @@ export async function verifyAction(input: VerifyActionInput): Promise<VerifyActi
291
343
  canonicalMessage: reconstructedMessage,
292
344
  id: a.id,
293
345
  delegation: d,
346
+ chain,
294
347
  scopeExercised: exercised,
295
348
  anchor,
296
349
  };
@@ -302,8 +355,13 @@ export async function verifyAction(input: VerifyActionInput): Promise<VerifyActi
302
355
 
303
356
  export interface VerifyRevocationInput extends VerifyBase {
304
357
  envelope: RevocationEnvelope;
305
- /** The delegation targeted by the revocation. Required to check signer is authorized. */
306
- delegation: DelegationEnvelope;
358
+ /**
359
+ * The envelope targeted by the revocation. Required to check signer is
360
+ * authorized. May be a v1.0 root delegation OR a v1.1 sub-delegation —
361
+ * both have identical `principal`, `agent`, `id`, and `revocation.holders`
362
+ * field shapes per SUB-DELEGATION.md §3.
363
+ */
364
+ delegation: ChainLink;
307
365
  }
308
366
 
309
367
  export async function verifyRevocation(input: VerifyRevocationInput): Promise<VerifyRevocationResult> {
@@ -400,6 +458,179 @@ function checkRevocationShape(env: RevocationEnvelope): VerifyRevocationResult |
400
458
  return null;
401
459
  }
402
460
 
461
+ function checkSubdelegationShape(env: SubdelegationEnvelope): VerifySubdelegationResult | null {
462
+ if (env.kind !== 'agent-subdelegation') return err('E_MALFORMED', 'kind must be "agent-subdelegation"');
463
+ if (!isHex64(env.id)) return err('E_MALFORMED', 'id must be 64 lowercase hex chars');
464
+ if (!isHex64(env.parent_id)) return err('E_MALFORMED', 'parent_id must be 64-hex');
465
+ if (!env.principal?.address || env.principal.alg !== 'bip322') return err('E_MALFORMED', 'principal invalid');
466
+ if (!env.agent?.address || env.agent.alg !== 'bip322') return err('E_MALFORMED', 'agent invalid');
467
+ if (!Array.isArray(env.scopes) || env.scopes.length === 0) return err('E_MALFORMED', 'scopes must be non-empty array');
468
+ // Sub-delegations MUST NOT carry a bond field (SUB-DELEGATION.md §1.3).
469
+ if ('bond' in env && (env as { bond?: unknown }).bond !== undefined) {
470
+ return err('E_MALFORMED', 'sub-delegation envelopes MUST NOT carry a bond field');
471
+ }
472
+ if (!isIsoUtc(env.issued_at)) return err('E_MALFORMED', 'issued_at must be ISO 8601 UTC');
473
+ if (!isIsoUtc(env.expires_at)) return err('E_MALFORMED', 'expires_at must be ISO 8601 UTC');
474
+ if (!/^[0-9a-f]{32}$/.test(env.nonce)) return err('E_MALFORMED', 'nonce must be 32 lowercase hex chars');
475
+ if (env.sig?.alg !== 'bip322' || typeof env.sig.value !== 'string') return err('E_MALFORMED', 'sig invalid');
476
+ if (env.sig.pubkey !== env.principal.address) return err('E_MALFORMED', 'sig.pubkey must equal principal.address');
477
+ return null;
478
+ }
479
+
480
+ // ─────────────────────────────────────────────────────────────────────────────
481
+ // Sub-delegation chain link (SUB-DELEGATION.md §2.2 step 3)
482
+ // ─────────────────────────────────────────────────────────────────────────────
483
+
484
+ /**
485
+ * Verify a single sub-delegation envelope as a chain link from `parent`.
486
+ * Performs steps 3a–3g in order; returns the corresponding VerifyErr on
487
+ * failure or `{ ok: true, envelope: s }` on success.
488
+ *
489
+ * Skips the standalone temporal-validity check (SUB-DELEGATION.md §2.2 step
490
+ * 3d) — for action verification, the action-window check (step 4c) on the
491
+ * leaf is the binding temporal constraint. Callers that want a current-time
492
+ * "is this subdelegation active right now" check can use `verifySubdelegation`.
493
+ */
494
+ async function verifyChainLink(
495
+ s: SubdelegationEnvelope,
496
+ parent: ChainLink,
497
+ input: VerifyBase
498
+ ): Promise<VerifySubdelegationResult> {
499
+ if (s.v !== ENVELOPE_VERSION) {
500
+ return err('E_UNSUPPORTED_VERSION', `subdelegation version ${s.v} not supported`);
501
+ }
502
+ const shape = checkSubdelegationShape(s);
503
+ if (shape) return shape;
504
+
505
+ // Step 3a: canonical id.
506
+ let canonicalScopesList: string[];
507
+ try {
508
+ canonicalScopesList = canonicalizeScopes(s.scopes);
509
+ } catch (e) {
510
+ const msg = e instanceof ScopeParseError ? e.message : (e as Error).message;
511
+ return err('E_BAD_SCOPE_GRAMMAR', msg);
512
+ }
513
+ const canonInput = {
514
+ parent_id: s.parent_id,
515
+ principal: s.principal.address,
516
+ agent: s.agent.address,
517
+ scopes: canonicalScopesList,
518
+ issued_at: s.issued_at,
519
+ expires_at: s.expires_at,
520
+ nonce: s.nonce,
521
+ };
522
+ const reconstructedId = computeSubdelegationId(canonInput);
523
+ if (reconstructedId !== s.id) {
524
+ return err('E_BAD_ID', `reconstructed subdelegation id (${reconstructedId}) does not match envelope id (${s.id})`);
525
+ }
526
+
527
+ // Step 3b: scope grammar validation (registry-aware).
528
+ let parsedScopes;
529
+ try {
530
+ parsedScopes = s.scopes.map((str) => parseScope(str));
531
+ for (const p of parsedScopes) validateScope(p, { mode: input.scopeMode ?? 'strict' });
532
+ } catch (e) {
533
+ const msg = e instanceof ScopeParseError ? e.message : (e as Error).message;
534
+ return err('E_BAD_SCOPE_GRAMMAR', msg);
535
+ }
536
+
537
+ // Step 3c: BIP-322 signature.
538
+ if (!input.skipSignatureVerification) {
539
+ if (!input.verifyBip322) return err('E_BAD_SIG', 'no BIP-322 verifier supplied for subdelegation');
540
+ const ok = await input.verifyBip322(s.id, s.sig.value, s.principal.address);
541
+ if (!ok) return err('E_BAD_SIG', 'subdelegation BIP-322 signature did not verify');
542
+ }
543
+
544
+ // Step 3e: linkage.
545
+ if (s.parent_id !== parent.id) {
546
+ return err(
547
+ 'E_SUBDELEGATION_PRINCIPAL_MISMATCH',
548
+ `subdelegation.parent_id (${s.parent_id}) does not match parent envelope id (${parent.id})`
549
+ );
550
+ }
551
+ if (s.principal.address !== parent.agent.address) {
552
+ return err(
553
+ 'E_SUBDELEGATION_PRINCIPAL_MISMATCH',
554
+ `subdelegation.principal (${s.principal.address}) does not match parent.agent (${parent.agent.address})`
555
+ );
556
+ }
557
+
558
+ // Step 3f: temporal containment.
559
+ const sIssued = new Date(s.issued_at).getTime();
560
+ const sExpires = new Date(s.expires_at).getTime();
561
+ const pIssued = new Date(parent.issued_at).getTime();
562
+ const pExpires = new Date(parent.expires_at).getTime();
563
+ if (Number.isNaN(sIssued) || Number.isNaN(sExpires) || Number.isNaN(pIssued) || Number.isNaN(pExpires)) {
564
+ return err('E_MALFORMED', 'unparseable ISO 8601 timestamp in chain');
565
+ }
566
+ if (sExpires <= sIssued) {
567
+ return err('E_MALFORMED', 'subdelegation expires_at must be > issued_at');
568
+ }
569
+ if (sIssued < pIssued || sExpires > pExpires) {
570
+ return err(
571
+ 'E_SUBDELEGATION_EXPIRES_EXTENDED',
572
+ `subdelegation window [${s.issued_at}, ${s.expires_at}) is not contained in parent's [${parent.issued_at}, ${parent.expires_at})`
573
+ );
574
+ }
575
+
576
+ // Step 3g: scope containment (transitive narrowing).
577
+ const parentScopesParsed = parent.scopes.map((str) => parseScope(str));
578
+ for (let i = 0; i < parsedScopes.length; i++) {
579
+ const childScope = parsedScopes[i]!;
580
+ const containedBySomeParentScope = parentScopesParsed.some((p) => isSubScope(childScope, p));
581
+ if (!containedBySomeParentScope) {
582
+ return err(
583
+ 'E_SUBDELEGATION_SCOPE_ESCALATED',
584
+ `subdelegation scope ${canonicalizeScope(childScope)} is not a sub-scope of any granted scope on the parent`
585
+ );
586
+ }
587
+ }
588
+
589
+ return {
590
+ ok: true,
591
+ envelope: s,
592
+ canonicalMessage: '', // not populated for chain links; computeSubdelegationId is the binding form
593
+ id: s.id,
594
+ };
595
+ }
596
+
597
+ // ─────────────────────────────────────────────────────────────────────────────
598
+ // Standalone subdelegation verification (no action context)
599
+ // ─────────────────────────────────────────────────────────────────────────────
600
+
601
+ export interface VerifySubdelegationInput extends VerifyBase {
602
+ envelope: SubdelegationEnvelope;
603
+ /** The immediate parent envelope. Required for linkage / containment checks. */
604
+ parent: ChainLink;
605
+ /** Skip the "now ∈ [issued, expires)" check. Useful for inspection. */
606
+ skipTemporalCheck?: boolean;
607
+ /** Defaults to new Date(). */
608
+ now?: Date;
609
+ }
610
+
611
+ /**
612
+ * Verify a single sub-delegation envelope against its immediate parent.
613
+ * Includes the standalone temporal-validity check (`now ∈ [issued, expires)`)
614
+ * unless `skipTemporalCheck` is set. Useful for pre-flighting a chain link
615
+ * outside of action verification.
616
+ */
617
+ export async function verifySubdelegation(
618
+ input: VerifySubdelegationInput
619
+ ): Promise<VerifySubdelegationResult> {
620
+ const r = await verifyChainLink(input.envelope, input.parent, input);
621
+ if (!r.ok) return r;
622
+
623
+ if (!input.skipTemporalCheck) {
624
+ const now = (input.now ?? new Date()).getTime();
625
+ const issued = new Date(input.envelope.issued_at).getTime();
626
+ const expires = new Date(input.envelope.expires_at).getTime();
627
+ if (now < issued) return err('E_NOT_YET_VALID', `subdelegation issued_at ${input.envelope.issued_at} is in the future`);
628
+ if (now >= expires) return err('E_EXPIRED', `subdelegation expires_at ${input.envelope.expires_at} is past`);
629
+ }
630
+
631
+ return r;
632
+ }
633
+
403
634
  // ─────────────────────────────────────────────────────────────────────────────
404
635
  // Time comparison for revocation vs action (SPEC §9.3)
405
636
  // ─────────────────────────────────────────────────────────────────────────────