@orangecheck/agent-core 1.0.1 → 1.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/README.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # @orangecheck/agent-core
2
2
 
3
+ > **Full reference:** [docs.ochk.io/sdk/agent-core](https://docs.ochk.io/sdk/agent-core) — auto-generated from the TypeScript source on every release.
4
+ > Hand-written prose below is the high-level overview; the docs site is the source of truth for every export, type, and signature.
5
+
6
+
3
7
  Canonical messages, envelope formats (delegation / action / revocation), scope grammar, and verification for [OC Agent](https://github.com/orangecheck/oc-agent-protocol) — the OrangeCheck authority primitive.
4
8
 
5
9
  - **Pure TypeScript.** No Node built-ins outside `@noble/hashes`. Runs in Node, browsers, Deno, Cloudflare Workers.
package/dist/index.d.mts CHANGED
@@ -1,5 +1,5 @@
1
- import { AgentErrorCode, ActionEnvelope, DelegationEnvelope, SubdelegationEnvelope, RevocationEnvelope, ChainLink, VerifyActionResult, VerifyDelegationResult, VerifyRevocationResult, VerifySubdelegationResult, ScopesEncryptedEnvelope } from './types.mjs';
2
- export { ActionCanonicalInput, ActionContent, ActionOts, ActorRef, DelegationBond, DelegationCanonicalInput, DelegationRevocationRef, ENVELOPE_VERSION, EnvelopeKind, RevocationCanonicalInput, RevocationHolder, Signature, SubdelegationCanonicalInput, VerifyActionOkExtra, VerifyErr, VerifyOk } from './types.mjs';
1
+ import { AgentErrorCode, ActionEnvelope, DelegationEnvelope, SubdelegationEnvelope, RevocationEnvelope, ChainLink, VerifyActionResult, VerifyDelegationResult, VerifyRevocationResult, VerifySubdelegationResult, ActorRef, DelegationBond, DelegationRevocationRef, ScopesEncryptedEnvelope } from './types.mjs';
2
+ export { ActionCanonicalInput, ActionContent, ActionOts, DelegationCanonicalInput, ENVELOPE_VERSION, EnvelopeKind, RevocationCanonicalInput, RevocationHolder, Signature, SubdelegationCanonicalInput, VerifyActionOkExtra, VerifyErr, VerifyOk } from './types.mjs';
3
3
  export { actionCanonicalBytes, actionCanonicalMessage, canonicalActionBytes, canonicalDelegationBytes, canonicalRevocationBytes, canonicalizeAction, canonicalizeDelegation, canonicalizeRevocation, canonicalizeScopes, computeActionId, computeDelegationId, computeRevocationId, computeSubdelegationId, delegationCanonicalBytes, delegationCanonicalMessage, parseAndCanonicalizeScopes, revocationCanonicalBytes, revocationCanonicalMessage, sha256Hex, subdelegationCanonicalBytes, subdelegationCanonicalMessage } from './canonical.mjs';
4
4
  import { ValidationOptions } from './scope.mjs';
5
5
  export { REGISTERED_SCOPES, Scope, ScopeConstraint, ScopeOp, ScopeParseError, canonicalizeScope, canonicalizeScopeString, isSubScope, parseScope, validateScope } from './scope.mjs';
@@ -58,6 +58,81 @@ interface VerifySubdelegationInput extends VerifyBase {
58
58
  }
59
59
  declare function verifySubdelegation(input: VerifySubdelegationInput): Promise<VerifySubdelegationResult>;
60
60
 
61
+ interface FederationGuardian {
62
+ address: string;
63
+ alg: 'bip322';
64
+ name?: string;
65
+ }
66
+ interface FederationDescriptor {
67
+ v: 1;
68
+ kind: 'agent-federation';
69
+ threshold: string;
70
+ guardians: FederationGuardian[];
71
+ }
72
+ interface FederationPrincipal {
73
+ alg: 'federation';
74
+ descriptor_id: string;
75
+ descriptor: FederationDescriptor;
76
+ }
77
+ interface FederationSignature {
78
+ alg: 'federation-bip322';
79
+ threshold: string;
80
+ signatures: Array<{
81
+ guardian_address: string;
82
+ value: string;
83
+ }>;
84
+ }
85
+ interface FederationDelegationEnvelope {
86
+ v: 1;
87
+ kind: 'agent-delegation';
88
+ id: string;
89
+ principal: FederationPrincipal;
90
+ agent: ActorRef;
91
+ scopes: string[];
92
+ bond: DelegationBond | null;
93
+ issued_at: string;
94
+ expires_at: string;
95
+ nonce: string;
96
+ revocation: DelegationRevocationRef;
97
+ sig: FederationSignature;
98
+ }
99
+ interface FederationRevocationEnvelope {
100
+ v: 1;
101
+ kind: 'agent-revocation';
102
+ id: string;
103
+ delegation_id: string;
104
+ signer: FederationPrincipal;
105
+ reason: string;
106
+ signed_at: string;
107
+ ots?: unknown | null;
108
+ sig: FederationSignature;
109
+ }
110
+ type FederationVerifyResult = {
111
+ ok: true;
112
+ id: string;
113
+ canonicalMessage: string;
114
+ } | {
115
+ ok: false;
116
+ code: AgentErrorCode;
117
+ message: string;
118
+ };
119
+ interface VerifyFederationBase {
120
+ verifyBip322?: (msg: string, signatureB64: string, address: string) => Promise<boolean>;
121
+ skipSignatureVerification?: boolean;
122
+ }
123
+ declare function federationDescriptorCanonicalMessage(descriptor: FederationDescriptor): string;
124
+ declare function computeFederationDescriptorId(descriptor: FederationDescriptor): string;
125
+ interface VerifyFederationDelegationInput extends VerifyFederationBase {
126
+ envelope: FederationDelegationEnvelope;
127
+ now?: Date;
128
+ skipTemporalCheck?: boolean;
129
+ }
130
+ declare function verifyFederationDelegation(input: VerifyFederationDelegationInput): Promise<FederationVerifyResult>;
131
+ interface VerifyFederationRevocationInput extends VerifyFederationBase {
132
+ envelope: FederationRevocationEnvelope;
133
+ }
134
+ declare function verifyFederationRevocation(input: VerifyFederationRevocationInput): Promise<FederationVerifyResult>;
135
+
61
136
  declare function encodeScopesPayload(scopes: string[]): Uint8Array;
62
137
  declare function decodeScopesPayload(bytes: Uint8Array): string[];
63
138
  interface SealScopesInput {
@@ -90,4 +165,14 @@ declare function hasPrivateScopes<T extends {
90
165
  scopes_encrypted: ScopesEncryptedEnvelope;
91
166
  };
92
167
 
93
- export { ActionEnvelope, AgentError, AgentErrorCode, ChainLink, DEFAULT_MAX_CHAIN_DEPTH, DelegationEnvelope, RevocationEnvelope, ScopesEncryptedEnvelope, type SealScopesInput, SubdelegationEnvelope, type UnsealScopesInput, type UnsealedScopes, ValidationOptions, type VerifyActionInput, VerifyActionResult, type VerifyBase, type VerifyDelegationInput, VerifyDelegationResult, type VerifyRevocationInput, VerifyRevocationResult, type VerifySubdelegationInput, VerifySubdelegationResult, decodeScopesPayload, encodeScopesPayload, hasPrivateScopes, sealScopes, unsealScopes, verifyAction, verifyDelegation, verifyRevocation, verifySubdelegation };
168
+ interface ScopeBearingDelegation {
169
+ scopes?: string[];
170
+ scopes_encrypted?: ScopesEncryptedEnvelope;
171
+ }
172
+ declare class ScopeNotGrantedError extends Error {
173
+ readonly reason: 'not_subscope' | 'scopes_encrypted' | 'no_scopes';
174
+ constructor(reason: ScopeNotGrantedError['reason'], message: string);
175
+ }
176
+ declare function assertScopeGranted(delegation: ScopeBearingDelegation, scopeExercised: string, fnName: string): void;
177
+
178
+ export { ActionEnvelope, ActorRef, AgentError, AgentErrorCode, ChainLink, DEFAULT_MAX_CHAIN_DEPTH, DelegationBond, DelegationEnvelope, DelegationRevocationRef, type FederationDelegationEnvelope, type FederationDescriptor, type FederationGuardian, type FederationPrincipal, type FederationRevocationEnvelope, type FederationSignature, type FederationVerifyResult, RevocationEnvelope, type ScopeBearingDelegation, ScopeNotGrantedError, ScopesEncryptedEnvelope, type SealScopesInput, SubdelegationEnvelope, type UnsealScopesInput, type UnsealedScopes, ValidationOptions, type VerifyActionInput, VerifyActionResult, type VerifyBase, type VerifyDelegationInput, VerifyDelegationResult, type VerifyFederationBase, type VerifyFederationDelegationInput, type VerifyFederationRevocationInput, type VerifyRevocationInput, VerifyRevocationResult, type VerifySubdelegationInput, VerifySubdelegationResult, assertScopeGranted, computeFederationDescriptorId, decodeScopesPayload, encodeScopesPayload, federationDescriptorCanonicalMessage, hasPrivateScopes, sealScopes, unsealScopes, verifyAction, verifyDelegation, verifyFederationDelegation, verifyFederationRevocation, verifyRevocation, verifySubdelegation };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { AgentErrorCode, ActionEnvelope, DelegationEnvelope, SubdelegationEnvelope, RevocationEnvelope, ChainLink, VerifyActionResult, VerifyDelegationResult, VerifyRevocationResult, VerifySubdelegationResult, ScopesEncryptedEnvelope } from './types.js';
2
- export { ActionCanonicalInput, ActionContent, ActionOts, ActorRef, DelegationBond, DelegationCanonicalInput, DelegationRevocationRef, ENVELOPE_VERSION, EnvelopeKind, RevocationCanonicalInput, RevocationHolder, Signature, SubdelegationCanonicalInput, VerifyActionOkExtra, VerifyErr, VerifyOk } from './types.js';
1
+ import { AgentErrorCode, ActionEnvelope, DelegationEnvelope, SubdelegationEnvelope, RevocationEnvelope, ChainLink, VerifyActionResult, VerifyDelegationResult, VerifyRevocationResult, VerifySubdelegationResult, ActorRef, DelegationBond, DelegationRevocationRef, ScopesEncryptedEnvelope } from './types.js';
2
+ export { ActionCanonicalInput, ActionContent, ActionOts, DelegationCanonicalInput, ENVELOPE_VERSION, EnvelopeKind, RevocationCanonicalInput, RevocationHolder, Signature, SubdelegationCanonicalInput, VerifyActionOkExtra, VerifyErr, VerifyOk } from './types.js';
3
3
  export { actionCanonicalBytes, actionCanonicalMessage, canonicalActionBytes, canonicalDelegationBytes, canonicalRevocationBytes, canonicalizeAction, canonicalizeDelegation, canonicalizeRevocation, canonicalizeScopes, computeActionId, computeDelegationId, computeRevocationId, computeSubdelegationId, delegationCanonicalBytes, delegationCanonicalMessage, parseAndCanonicalizeScopes, revocationCanonicalBytes, revocationCanonicalMessage, sha256Hex, subdelegationCanonicalBytes, subdelegationCanonicalMessage } from './canonical.js';
4
4
  import { ValidationOptions } from './scope.js';
5
5
  export { REGISTERED_SCOPES, Scope, ScopeConstraint, ScopeOp, ScopeParseError, canonicalizeScope, canonicalizeScopeString, isSubScope, parseScope, validateScope } from './scope.js';
@@ -58,6 +58,81 @@ interface VerifySubdelegationInput extends VerifyBase {
58
58
  }
59
59
  declare function verifySubdelegation(input: VerifySubdelegationInput): Promise<VerifySubdelegationResult>;
60
60
 
61
+ interface FederationGuardian {
62
+ address: string;
63
+ alg: 'bip322';
64
+ name?: string;
65
+ }
66
+ interface FederationDescriptor {
67
+ v: 1;
68
+ kind: 'agent-federation';
69
+ threshold: string;
70
+ guardians: FederationGuardian[];
71
+ }
72
+ interface FederationPrincipal {
73
+ alg: 'federation';
74
+ descriptor_id: string;
75
+ descriptor: FederationDescriptor;
76
+ }
77
+ interface FederationSignature {
78
+ alg: 'federation-bip322';
79
+ threshold: string;
80
+ signatures: Array<{
81
+ guardian_address: string;
82
+ value: string;
83
+ }>;
84
+ }
85
+ interface FederationDelegationEnvelope {
86
+ v: 1;
87
+ kind: 'agent-delegation';
88
+ id: string;
89
+ principal: FederationPrincipal;
90
+ agent: ActorRef;
91
+ scopes: string[];
92
+ bond: DelegationBond | null;
93
+ issued_at: string;
94
+ expires_at: string;
95
+ nonce: string;
96
+ revocation: DelegationRevocationRef;
97
+ sig: FederationSignature;
98
+ }
99
+ interface FederationRevocationEnvelope {
100
+ v: 1;
101
+ kind: 'agent-revocation';
102
+ id: string;
103
+ delegation_id: string;
104
+ signer: FederationPrincipal;
105
+ reason: string;
106
+ signed_at: string;
107
+ ots?: unknown | null;
108
+ sig: FederationSignature;
109
+ }
110
+ type FederationVerifyResult = {
111
+ ok: true;
112
+ id: string;
113
+ canonicalMessage: string;
114
+ } | {
115
+ ok: false;
116
+ code: AgentErrorCode;
117
+ message: string;
118
+ };
119
+ interface VerifyFederationBase {
120
+ verifyBip322?: (msg: string, signatureB64: string, address: string) => Promise<boolean>;
121
+ skipSignatureVerification?: boolean;
122
+ }
123
+ declare function federationDescriptorCanonicalMessage(descriptor: FederationDescriptor): string;
124
+ declare function computeFederationDescriptorId(descriptor: FederationDescriptor): string;
125
+ interface VerifyFederationDelegationInput extends VerifyFederationBase {
126
+ envelope: FederationDelegationEnvelope;
127
+ now?: Date;
128
+ skipTemporalCheck?: boolean;
129
+ }
130
+ declare function verifyFederationDelegation(input: VerifyFederationDelegationInput): Promise<FederationVerifyResult>;
131
+ interface VerifyFederationRevocationInput extends VerifyFederationBase {
132
+ envelope: FederationRevocationEnvelope;
133
+ }
134
+ declare function verifyFederationRevocation(input: VerifyFederationRevocationInput): Promise<FederationVerifyResult>;
135
+
61
136
  declare function encodeScopesPayload(scopes: string[]): Uint8Array;
62
137
  declare function decodeScopesPayload(bytes: Uint8Array): string[];
63
138
  interface SealScopesInput {
@@ -90,4 +165,14 @@ declare function hasPrivateScopes<T extends {
90
165
  scopes_encrypted: ScopesEncryptedEnvelope;
91
166
  };
92
167
 
93
- export { ActionEnvelope, AgentError, AgentErrorCode, ChainLink, DEFAULT_MAX_CHAIN_DEPTH, DelegationEnvelope, RevocationEnvelope, ScopesEncryptedEnvelope, type SealScopesInput, SubdelegationEnvelope, type UnsealScopesInput, type UnsealedScopes, ValidationOptions, type VerifyActionInput, VerifyActionResult, type VerifyBase, type VerifyDelegationInput, VerifyDelegationResult, type VerifyRevocationInput, VerifyRevocationResult, type VerifySubdelegationInput, VerifySubdelegationResult, decodeScopesPayload, encodeScopesPayload, hasPrivateScopes, sealScopes, unsealScopes, verifyAction, verifyDelegation, verifyRevocation, verifySubdelegation };
168
+ interface ScopeBearingDelegation {
169
+ scopes?: string[];
170
+ scopes_encrypted?: ScopesEncryptedEnvelope;
171
+ }
172
+ declare class ScopeNotGrantedError extends Error {
173
+ readonly reason: 'not_subscope' | 'scopes_encrypted' | 'no_scopes';
174
+ constructor(reason: ScopeNotGrantedError['reason'], message: string);
175
+ }
176
+ declare function assertScopeGranted(delegation: ScopeBearingDelegation, scopeExercised: string, fnName: string): void;
177
+
178
+ export { ActionEnvelope, ActorRef, AgentError, AgentErrorCode, ChainLink, DEFAULT_MAX_CHAIN_DEPTH, DelegationBond, DelegationEnvelope, DelegationRevocationRef, type FederationDelegationEnvelope, type FederationDescriptor, type FederationGuardian, type FederationPrincipal, type FederationRevocationEnvelope, type FederationSignature, type FederationVerifyResult, RevocationEnvelope, type ScopeBearingDelegation, ScopeNotGrantedError, ScopesEncryptedEnvelope, type SealScopesInput, SubdelegationEnvelope, type UnsealScopesInput, type UnsealedScopes, ValidationOptions, type VerifyActionInput, VerifyActionResult, type VerifyBase, type VerifyDelegationInput, VerifyDelegationResult, type VerifyFederationBase, type VerifyFederationDelegationInput, type VerifyFederationRevocationInput, type VerifyRevocationInput, VerifyRevocationResult, type VerifySubdelegationInput, VerifySubdelegationResult, assertScopeGranted, computeFederationDescriptorId, decodeScopesPayload, encodeScopesPayload, federationDescriptorCanonicalMessage, hasPrivateScopes, sealScopes, unsealScopes, verifyAction, verifyDelegation, verifyFederationDelegation, verifyFederationRevocation, verifyRevocation, verifySubdelegation };
package/dist/index.js CHANGED
@@ -945,6 +945,193 @@ function isHex64(s) {
945
945
  function isIsoUtc(s) {
946
946
  return typeof s === "string" && /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?Z$/.test(s);
947
947
  }
948
+ function federationDescriptorCanonicalMessage(descriptor) {
949
+ const addresses = descriptor.guardians.map((g) => g.address).sort((a, b) => a < b ? -1 : a > b ? 1 : 0);
950
+ return [
951
+ "oc-agent:federation:v1",
952
+ `threshold: ${descriptor.threshold}`,
953
+ ...addresses.map((a) => `guardian: ${a}`)
954
+ ].join("\n");
955
+ }
956
+ function computeFederationDescriptorId(descriptor) {
957
+ return canonical.hexEncode(sha256.sha256(new TextEncoder().encode(federationDescriptorCanonicalMessage(descriptor))));
958
+ }
959
+ function parseThreshold(t) {
960
+ if (typeof t !== "string") return null;
961
+ const m = /^(\d+)-of-(\d+)$/.exec(t);
962
+ if (!m) return null;
963
+ const mm = Number(m[1]);
964
+ const nn = Number(m[2]);
965
+ if (!Number.isInteger(mm) || !Number.isInteger(nn) || mm < 1 || mm > nn) return null;
966
+ return { m: mm, n: nn };
967
+ }
968
+ var HEX64 = /^[0-9a-f]{64}$/;
969
+ var ISO_UTC = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?Z$/;
970
+ async function checkFederationQuorum(principal, sig, reconstructedId, input) {
971
+ if (principal?.alg !== "federation") {
972
+ return fail("E_MALFORMED", 'principal.alg must be "federation"');
973
+ }
974
+ const descriptor = principal.descriptor;
975
+ if (!descriptor || descriptor.kind !== "agent-federation") {
976
+ return fail("E_MALFORMED", "principal.descriptor missing or wrong kind");
977
+ }
978
+ const parsed = parseThreshold(descriptor.threshold);
979
+ if (!parsed) return fail("E_MALFORMED", `malformed threshold "${descriptor.threshold}"`);
980
+ if (!Array.isArray(descriptor.guardians) || descriptor.guardians.length !== parsed.n) {
981
+ return fail("E_MALFORMED", "guardians length must equal N in M-of-N");
982
+ }
983
+ const computedDescId = computeFederationDescriptorId(descriptor);
984
+ if (principal.descriptor_id !== computedDescId) {
985
+ return fail(
986
+ "E_BAD_FEDERATION_DESCRIPTOR",
987
+ `declared descriptor_id (${principal.descriptor_id}) != canonical hash (${computedDescId})`
988
+ );
989
+ }
990
+ if (sig?.alg !== "federation-bip322") {
991
+ return fail("E_MALFORMED", 'sig.alg must be "federation-bip322"');
992
+ }
993
+ if (sig.threshold !== descriptor.threshold) {
994
+ return fail(
995
+ "E_THRESHOLD_MISMATCH",
996
+ `sig.threshold (${sig.threshold}) != descriptor.threshold (${descriptor.threshold})`
997
+ );
998
+ }
999
+ const sigs = sig.signatures;
1000
+ if (!Array.isArray(sigs)) return fail("E_MALFORMED", "sig.signatures must be an array");
1001
+ if (sigs.length < parsed.m) {
1002
+ return fail(
1003
+ "E_THRESHOLD_NOT_MET",
1004
+ `${sigs.length} signature(s) below threshold M=${parsed.m}`
1005
+ );
1006
+ }
1007
+ const guardianSet = new Set(descriptor.guardians.map((g) => g.address));
1008
+ const seen = /* @__PURE__ */ new Set();
1009
+ for (const s of sigs) {
1010
+ if (!guardianSet.has(s.guardian_address)) {
1011
+ return fail("E_UNKNOWN_GUARDIAN", `${s.guardian_address} is not a declared guardian`);
1012
+ }
1013
+ if (seen.has(s.guardian_address)) {
1014
+ return fail("E_DUPLICATE_GUARDIAN", `${s.guardian_address} signed more than once`);
1015
+ }
1016
+ seen.add(s.guardian_address);
1017
+ }
1018
+ if (!input.skipSignatureVerification) {
1019
+ if (!input.verifyBip322) return fail("E_BAD_SIG", "no BIP-322 verifier supplied");
1020
+ for (const s of sigs) {
1021
+ const ok = await input.verifyBip322(reconstructedId, s.value, s.guardian_address);
1022
+ if (!ok) {
1023
+ return fail("E_BAD_SIG", `guardian ${s.guardian_address} signature did not verify`);
1024
+ }
1025
+ }
1026
+ }
1027
+ return { ok: true };
1028
+ }
1029
+ async function verifyFederationDelegation(input) {
1030
+ const env = input.envelope;
1031
+ if (env?.kind !== "agent-delegation") return fail("E_MALFORMED", 'kind must be "agent-delegation"');
1032
+ if (!HEX64.test(env.id ?? "")) return fail("E_MALFORMED", "id must be 64 lowercase hex chars");
1033
+ if (!env.agent?.address || env.agent.alg !== "bip322") return fail("E_MALFORMED", "agent invalid");
1034
+ if (!Array.isArray(env.scopes) || env.scopes.length === 0) {
1035
+ return fail("E_MALFORMED", "scopes must be a non-empty array");
1036
+ }
1037
+ if (!ISO_UTC.test(env.issued_at) || !ISO_UTC.test(env.expires_at)) {
1038
+ return fail("E_MALFORMED", "issued_at / expires_at must be ISO 8601 UTC");
1039
+ }
1040
+ if (!/^[0-9a-f]{32}$/.test(env.nonce)) return fail("E_MALFORMED", "nonce must be 32 hex chars");
1041
+ let canonicalScopes;
1042
+ try {
1043
+ canonicalScopes = canonicalizeScopes(env.scopes);
1044
+ } catch (e) {
1045
+ return fail("E_BAD_SCOPE_GRAMMAR", e.message);
1046
+ }
1047
+ for (let i = 0; i < canonicalScopes.length; i++) {
1048
+ if (env.scopes[i] !== canonicalScopes[i]) {
1049
+ return fail("E_BAD_SCOPE_GRAMMAR", `scope index ${i} not in canonical order`);
1050
+ }
1051
+ }
1052
+ const canonInput = {
1053
+ principal: `federation:${env.principal?.descriptor_id ?? ""}`,
1054
+ agent: env.agent.address,
1055
+ scopes: canonicalScopes,
1056
+ bond_sats: env.bond?.sats ?? 0,
1057
+ bond_attestation: env.bond?.attestation_id ?? "none",
1058
+ issued_at: env.issued_at,
1059
+ expires_at: env.expires_at,
1060
+ nonce: env.nonce
1061
+ };
1062
+ const reconstructedId = computeDelegationId(canonInput);
1063
+ if (reconstructedId !== env.id) {
1064
+ return fail("E_BAD_ID", `reconstructed id (${reconstructedId}) != envelope.id (${env.id})`);
1065
+ }
1066
+ const quorum = await checkFederationQuorum(env.principal, env.sig, env.id, input);
1067
+ if (!quorum.ok) return quorum;
1068
+ if (!input.skipTemporalCheck) {
1069
+ const now = input.now ?? /* @__PURE__ */ new Date();
1070
+ const issued = new Date(env.issued_at);
1071
+ const expires = new Date(env.expires_at);
1072
+ if (expires <= issued) return fail("E_MALFORMED", "expires_at <= issued_at");
1073
+ if (now < issued) return fail("E_NOT_YET_VALID", `delegation not valid until ${env.issued_at}`);
1074
+ if (now >= expires) return fail("E_EXPIRED", `delegation expired at ${env.expires_at}`);
1075
+ }
1076
+ return { ok: true, id: env.id, canonicalMessage: delegationCanonicalMessage(canonInput) };
1077
+ }
1078
+ async function verifyFederationRevocation(input) {
1079
+ const env = input.envelope;
1080
+ if (env?.kind !== "agent-revocation") return fail("E_MALFORMED", 'kind must be "agent-revocation"');
1081
+ if (!HEX64.test(env.id ?? "")) return fail("E_MALFORMED", "id must be 64 lowercase hex chars");
1082
+ if (!HEX64.test(env.delegation_id ?? "")) return fail("E_MALFORMED", "delegation_id must be 64-hex");
1083
+ if (typeof env.reason !== "string" || env.reason.length > 128) {
1084
+ return fail("E_MALFORMED", "reason must be a string \u2264128 bytes");
1085
+ }
1086
+ if (!ISO_UTC.test(env.signed_at)) return fail("E_MALFORMED", "signed_at must be ISO 8601 UTC");
1087
+ const canonInput = {
1088
+ address: `federation:${env.signer?.descriptor_id ?? ""}`,
1089
+ delegation_id: env.delegation_id,
1090
+ reason: env.reason,
1091
+ signed_at: env.signed_at
1092
+ };
1093
+ const reconstructedId = computeRevocationId(canonInput);
1094
+ if (reconstructedId !== env.id) {
1095
+ return fail("E_BAD_ID", `reconstructed id (${reconstructedId}) != envelope.id (${env.id})`);
1096
+ }
1097
+ const quorum = await checkFederationQuorum(env.signer, env.sig, env.id, input);
1098
+ if (!quorum.ok) return quorum;
1099
+ return { ok: true, id: env.id, canonicalMessage: revocationCanonicalMessage(canonInput) };
1100
+ }
1101
+ function fail(code, message) {
1102
+ return { ok: false, code, message };
1103
+ }
1104
+
1105
+ // src/assert-scope.ts
1106
+ var ScopeNotGrantedError = class extends Error {
1107
+ constructor(reason, message) {
1108
+ super(message);
1109
+ this.name = "ScopeNotGrantedError";
1110
+ this.reason = reason;
1111
+ }
1112
+ };
1113
+ function assertScopeGranted(delegation, scopeExercised, fnName) {
1114
+ if (hasPrivateScopes(delegation)) {
1115
+ throw new ScopeNotGrantedError(
1116
+ "scopes_encrypted",
1117
+ `${fnName}: this delegation uses v1.2 private scopes \u2014 \`scopes_encrypted\` is set and \`scopes\` is absent, so the granted set cannot be read here. Decrypt it with decryptPrivateScopes() using a matching device key and pass the recovered scopes as \`delegation.scopes\`. Refusing to stamp \`${scopeExercised}\` against an unreadable grant.`
1118
+ );
1119
+ }
1120
+ const scopes = delegation.scopes;
1121
+ if (scopes === void 0 || scopes.length === 0) {
1122
+ throw new ScopeNotGrantedError(
1123
+ "no_scopes",
1124
+ `${fnName}: delegation grants no scopes, so \`${scopeExercised}\` cannot be exercised. An absent or empty \`scopes\` means nothing is granted, never everything.`
1125
+ );
1126
+ }
1127
+ const exercised = parseScope(scopeExercised);
1128
+ if (!scopes.map(parseScope).some((g) => isSubScope(exercised, g))) {
1129
+ throw new ScopeNotGrantedError(
1130
+ "not_subscope",
1131
+ `${fnName}: scope_exercised (${scopeExercised}) is not a sub-scope of any granted scope (${scopes.join(", ")})`
1132
+ );
1133
+ }
1134
+ }
948
1135
 
949
1136
  Object.defineProperty(exports, "canonicalize", {
950
1137
  enumerable: true,
@@ -958,9 +1145,11 @@ exports.AgentError = AgentError;
958
1145
  exports.DEFAULT_MAX_CHAIN_DEPTH = DEFAULT_MAX_CHAIN_DEPTH;
959
1146
  exports.ENVELOPE_VERSION = ENVELOPE_VERSION;
960
1147
  exports.REGISTERED_SCOPES = REGISTERED_SCOPES;
1148
+ exports.ScopeNotGrantedError = ScopeNotGrantedError;
961
1149
  exports.ScopeParseError = ScopeParseError;
962
1150
  exports.actionCanonicalBytes = actionCanonicalBytes;
963
1151
  exports.actionCanonicalMessage = actionCanonicalMessage;
1152
+ exports.assertScopeGranted = assertScopeGranted;
964
1153
  exports.canonicalActionBytes = canonicalActionBytes;
965
1154
  exports.canonicalDelegationBytes = canonicalDelegationBytes;
966
1155
  exports.canonicalRevocationBytes = canonicalRevocationBytes;
@@ -972,12 +1161,14 @@ exports.canonicalizeScopeString = canonicalizeScopeString;
972
1161
  exports.canonicalizeScopes = canonicalizeScopes;
973
1162
  exports.computeActionId = computeActionId;
974
1163
  exports.computeDelegationId = computeDelegationId;
1164
+ exports.computeFederationDescriptorId = computeFederationDescriptorId;
975
1165
  exports.computeRevocationId = computeRevocationId;
976
1166
  exports.computeSubdelegationId = computeSubdelegationId;
977
1167
  exports.decodeScopesPayload = decodeScopesPayload;
978
1168
  exports.delegationCanonicalBytes = delegationCanonicalBytes;
979
1169
  exports.delegationCanonicalMessage = delegationCanonicalMessage;
980
1170
  exports.encodeScopesPayload = encodeScopesPayload;
1171
+ exports.federationDescriptorCanonicalMessage = federationDescriptorCanonicalMessage;
981
1172
  exports.hasPrivateScopes = hasPrivateScopes;
982
1173
  exports.isSubScope = isSubScope;
983
1174
  exports.parseAndCanonicalizeScopes = parseAndCanonicalizeScopes;
@@ -992,6 +1183,8 @@ exports.unsealScopes = unsealScopes;
992
1183
  exports.validateScope = validateScope;
993
1184
  exports.verifyAction = verifyAction;
994
1185
  exports.verifyDelegation = verifyDelegation;
1186
+ exports.verifyFederationDelegation = verifyFederationDelegation;
1187
+ exports.verifyFederationRevocation = verifyFederationRevocation;
995
1188
  exports.verifyRevocation = verifyRevocation;
996
1189
  exports.verifySubdelegation = verifySubdelegation;
997
1190
  //# sourceMappingURL=index.js.map