@kya-os/mcp-i-core 1.3.12 → 1.3.14

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.
Files changed (254) hide show
  1. package/dist/config/remote-config.js +9 -12
  2. package/dist/runtime/base.js +11 -0
  3. package/dist/services/access-control.service.js +5 -0
  4. package/dist/services/tool-protection.service.js +17 -8
  5. package/package.json +2 -2
  6. package/.turbo/turbo-build.log +0 -4
  7. package/.turbo/turbo-test$colon$coverage.log +0 -4586
  8. package/.turbo/turbo-test.log +0 -3169
  9. package/COMPLIANCE_IMPROVEMENT_REPORT.md +0 -483
  10. package/Composer 3.md +0 -615
  11. package/GPT-5.md +0 -1169
  12. package/OPUS-plan.md +0 -352
  13. package/PHASE_3_AND_4.1_SUMMARY.md +0 -585
  14. package/PHASE_3_SUMMARY.md +0 -317
  15. package/PHASE_4.1.3_SUMMARY.md +0 -428
  16. package/PHASE_4.1_COMPLETE.md +0 -525
  17. package/PHASE_4_USER_DID_IDENTITY_LINKING_PLAN.md +0 -1240
  18. package/SCHEMA_COMPLIANCE_REPORT.md +0 -275
  19. package/TEST_PLAN.md +0 -571
  20. package/coverage/coverage-final.json +0 -60
  21. package/dist/cache/oauth-config-cache.d.ts.map +0 -1
  22. package/dist/cache/oauth-config-cache.js.map +0 -1
  23. package/dist/cache/tool-protection-cache.d.ts.map +0 -1
  24. package/dist/cache/tool-protection-cache.js.map +0 -1
  25. package/dist/compliance/index.d.ts.map +0 -1
  26. package/dist/compliance/index.js.map +0 -1
  27. package/dist/compliance/schema-registry.d.ts.map +0 -1
  28. package/dist/compliance/schema-registry.js.map +0 -1
  29. package/dist/compliance/schema-verifier.d.ts.map +0 -1
  30. package/dist/compliance/schema-verifier.js.map +0 -1
  31. package/dist/config/remote-config.d.ts.map +0 -1
  32. package/dist/config/remote-config.js.map +0 -1
  33. package/dist/config.d.ts.map +0 -1
  34. package/dist/config.js.map +0 -1
  35. package/dist/delegation/audience-validator.d.ts.map +0 -1
  36. package/dist/delegation/audience-validator.js.map +0 -1
  37. package/dist/delegation/bitstring.d.ts.map +0 -1
  38. package/dist/delegation/bitstring.js.map +0 -1
  39. package/dist/delegation/cascading-revocation.d.ts.map +0 -1
  40. package/dist/delegation/cascading-revocation.js.map +0 -1
  41. package/dist/delegation/delegation-graph.d.ts.map +0 -1
  42. package/dist/delegation/delegation-graph.js.map +0 -1
  43. package/dist/delegation/did-key-resolver.d.ts.map +0 -1
  44. package/dist/delegation/did-key-resolver.js.map +0 -1
  45. package/dist/delegation/index.d.ts.map +0 -1
  46. package/dist/delegation/index.js.map +0 -1
  47. package/dist/delegation/statuslist-manager.d.ts.map +0 -1
  48. package/dist/delegation/statuslist-manager.js.map +0 -1
  49. package/dist/delegation/storage/index.d.ts.map +0 -1
  50. package/dist/delegation/storage/index.js.map +0 -1
  51. package/dist/delegation/storage/memory-graph-storage.d.ts.map +0 -1
  52. package/dist/delegation/storage/memory-graph-storage.js.map +0 -1
  53. package/dist/delegation/storage/memory-statuslist-storage.d.ts.map +0 -1
  54. package/dist/delegation/storage/memory-statuslist-storage.js.map +0 -1
  55. package/dist/delegation/utils.d.ts.map +0 -1
  56. package/dist/delegation/utils.js.map +0 -1
  57. package/dist/delegation/vc-issuer.d.ts.map +0 -1
  58. package/dist/delegation/vc-issuer.js.map +0 -1
  59. package/dist/delegation/vc-verifier.d.ts.map +0 -1
  60. package/dist/delegation/vc-verifier.js.map +0 -1
  61. package/dist/identity/idp-token-resolver.d.ts.map +0 -1
  62. package/dist/identity/idp-token-resolver.js.map +0 -1
  63. package/dist/identity/idp-token-storage.interface.d.ts.map +0 -1
  64. package/dist/identity/idp-token-storage.interface.js.map +0 -1
  65. package/dist/identity/user-did-manager.d.ts.map +0 -1
  66. package/dist/identity/user-did-manager.js.map +0 -1
  67. package/dist/index.d.ts.map +0 -1
  68. package/dist/index.js.map +0 -1
  69. package/dist/providers/base.d.ts.map +0 -1
  70. package/dist/providers/base.js.map +0 -1
  71. package/dist/providers/memory.d.ts.map +0 -1
  72. package/dist/providers/memory.js.map +0 -1
  73. package/dist/runtime/audit-logger.d.ts.map +0 -1
  74. package/dist/runtime/audit-logger.js.map +0 -1
  75. package/dist/runtime/base.d.ts.map +0 -1
  76. package/dist/runtime/base.js.map +0 -1
  77. package/dist/services/access-control.service.d.ts.map +0 -1
  78. package/dist/services/access-control.service.js.map +0 -1
  79. package/dist/services/authorization/authorization-registry.d.ts.map +0 -1
  80. package/dist/services/authorization/authorization-registry.js.map +0 -1
  81. package/dist/services/authorization/types.d.ts.map +0 -1
  82. package/dist/services/authorization/types.js.map +0 -1
  83. package/dist/services/batch-delegation.service.d.ts.map +0 -1
  84. package/dist/services/batch-delegation.service.js.map +0 -1
  85. package/dist/services/crypto.service.d.ts.map +0 -1
  86. package/dist/services/crypto.service.js.map +0 -1
  87. package/dist/services/errors.d.ts.map +0 -1
  88. package/dist/services/errors.js.map +0 -1
  89. package/dist/services/index.d.ts.map +0 -1
  90. package/dist/services/index.js.map +0 -1
  91. package/dist/services/oauth-config.service.d.ts.map +0 -1
  92. package/dist/services/oauth-config.service.js.map +0 -1
  93. package/dist/services/oauth-provider-registry.d.ts.map +0 -1
  94. package/dist/services/oauth-provider-registry.js.map +0 -1
  95. package/dist/services/oauth-service.d.ts.map +0 -1
  96. package/dist/services/oauth-service.js.map +0 -1
  97. package/dist/services/oauth-token-retrieval.service.d.ts.map +0 -1
  98. package/dist/services/oauth-token-retrieval.service.js.map +0 -1
  99. package/dist/services/proof-verifier.d.ts.map +0 -1
  100. package/dist/services/proof-verifier.js.map +0 -1
  101. package/dist/services/provider-resolver.d.ts.map +0 -1
  102. package/dist/services/provider-resolver.js.map +0 -1
  103. package/dist/services/provider-validator.d.ts.map +0 -1
  104. package/dist/services/provider-validator.js.map +0 -1
  105. package/dist/services/session-registration.service.d.ts.map +0 -1
  106. package/dist/services/session-registration.service.js.map +0 -1
  107. package/dist/services/storage.service.d.ts.map +0 -1
  108. package/dist/services/storage.service.js.map +0 -1
  109. package/dist/services/tool-context-builder.d.ts.map +0 -1
  110. package/dist/services/tool-context-builder.js.map +0 -1
  111. package/dist/services/tool-protection.service.d.ts.map +0 -1
  112. package/dist/services/tool-protection.service.js.map +0 -1
  113. package/dist/types/oauth-required-error.d.ts.map +0 -1
  114. package/dist/types/oauth-required-error.js.map +0 -1
  115. package/dist/types/tool-protection.d.ts.map +0 -1
  116. package/dist/types/tool-protection.js.map +0 -1
  117. package/dist/utils/base58.d.ts.map +0 -1
  118. package/dist/utils/base58.js.map +0 -1
  119. package/dist/utils/base64.d.ts.map +0 -1
  120. package/dist/utils/base64.js.map +0 -1
  121. package/dist/utils/cors.d.ts.map +0 -1
  122. package/dist/utils/cors.js.map +0 -1
  123. package/dist/utils/did-helpers.d.ts.map +0 -1
  124. package/dist/utils/did-helpers.js.map +0 -1
  125. package/dist/utils/index.d.ts.map +0 -1
  126. package/dist/utils/index.js.map +0 -1
  127. package/dist/utils/storage-keys.d.ts.map +0 -1
  128. package/dist/utils/storage-keys.js.map +0 -1
  129. package/docs/API_REFERENCE.md +0 -1362
  130. package/docs/COMPLIANCE_MATRIX.md +0 -691
  131. package/docs/STATUSLIST2021_GUIDE.md +0 -696
  132. package/docs/W3C_VC_DELEGATION_GUIDE.md +0 -710
  133. package/src/__tests__/cache/tool-protection-cache.test.ts +0 -640
  134. package/src/__tests__/config/provider-runtime-config.test.ts +0 -309
  135. package/src/__tests__/delegation-e2e.test.ts +0 -690
  136. package/src/__tests__/identity/user-did-manager.test.ts +0 -232
  137. package/src/__tests__/index.test.ts +0 -56
  138. package/src/__tests__/integration/full-flow.test.ts +0 -789
  139. package/src/__tests__/integration.test.ts +0 -281
  140. package/src/__tests__/providers/base.test.ts +0 -173
  141. package/src/__tests__/providers/memory.test.ts +0 -319
  142. package/src/__tests__/regression/phase2-regression.test.ts +0 -429
  143. package/src/__tests__/runtime/audit-logger.test.ts +0 -154
  144. package/src/__tests__/runtime/base-extensions.test.ts +0 -595
  145. package/src/__tests__/runtime/base.test.ts +0 -869
  146. package/src/__tests__/runtime/delegation-flow.test.ts +0 -164
  147. package/src/__tests__/runtime/proof-client-did.test.ts +0 -376
  148. package/src/__tests__/runtime/route-interception.test.ts +0 -686
  149. package/src/__tests__/runtime/tool-protection-enforcement.test.ts +0 -908
  150. package/src/__tests__/services/agentshield-integration.test.ts +0 -791
  151. package/src/__tests__/services/cache-busting.test.ts +0 -125
  152. package/src/__tests__/services/oauth-service-pkce.test.ts +0 -556
  153. package/src/__tests__/services/provider-resolver-edge-cases.test.ts +0 -591
  154. package/src/__tests__/services/tool-protection-merged-config.test.ts +0 -485
  155. package/src/__tests__/services/tool-protection-oauth-provider.test.ts +0 -480
  156. package/src/__tests__/services/tool-protection.service.test.ts +0 -1373
  157. package/src/__tests__/utils/mock-providers.ts +0 -340
  158. package/src/cache/oauth-config-cache.d.ts +0 -69
  159. package/src/cache/oauth-config-cache.d.ts.map +0 -1
  160. package/src/cache/oauth-config-cache.js.map +0 -1
  161. package/src/cache/oauth-config-cache.ts +0 -123
  162. package/src/cache/tool-protection-cache.ts +0 -171
  163. package/src/compliance/EXAMPLE.md +0 -412
  164. package/src/compliance/__tests__/schema-verifier.test.ts +0 -797
  165. package/src/compliance/index.ts +0 -8
  166. package/src/compliance/schema-registry.ts +0 -460
  167. package/src/compliance/schema-verifier.ts +0 -708
  168. package/src/config/__tests__/merged-config.spec.ts +0 -445
  169. package/src/config/__tests__/remote-config.spec.ts +0 -268
  170. package/src/config/remote-config.ts +0 -264
  171. package/src/config.ts +0 -312
  172. package/src/delegation/__tests__/audience-validator.test.ts +0 -112
  173. package/src/delegation/__tests__/bitstring.test.ts +0 -346
  174. package/src/delegation/__tests__/cascading-revocation.test.ts +0 -628
  175. package/src/delegation/__tests__/delegation-graph.test.ts +0 -584
  176. package/src/delegation/__tests__/did-key-resolver.test.ts +0 -265
  177. package/src/delegation/__tests__/utils.test.ts +0 -152
  178. package/src/delegation/__tests__/vc-issuer.test.ts +0 -442
  179. package/src/delegation/__tests__/vc-verifier.test.ts +0 -922
  180. package/src/delegation/audience-validator.ts +0 -52
  181. package/src/delegation/bitstring.ts +0 -278
  182. package/src/delegation/cascading-revocation.ts +0 -370
  183. package/src/delegation/delegation-graph.ts +0 -299
  184. package/src/delegation/did-key-resolver.ts +0 -179
  185. package/src/delegation/index.ts +0 -14
  186. package/src/delegation/statuslist-manager.ts +0 -353
  187. package/src/delegation/storage/__tests__/memory-graph-storage.test.ts +0 -366
  188. package/src/delegation/storage/__tests__/memory-statuslist-storage.test.ts +0 -228
  189. package/src/delegation/storage/index.ts +0 -9
  190. package/src/delegation/storage/memory-graph-storage.ts +0 -178
  191. package/src/delegation/storage/memory-statuslist-storage.ts +0 -77
  192. package/src/delegation/utils.ts +0 -221
  193. package/src/delegation/vc-issuer.ts +0 -232
  194. package/src/delegation/vc-verifier.ts +0 -568
  195. package/src/identity/idp-token-resolver.ts +0 -181
  196. package/src/identity/idp-token-storage.interface.ts +0 -94
  197. package/src/identity/user-did-manager.ts +0 -526
  198. package/src/index.ts +0 -310
  199. package/src/providers/base.d.ts +0 -91
  200. package/src/providers/base.d.ts.map +0 -1
  201. package/src/providers/base.js.map +0 -1
  202. package/src/providers/base.ts +0 -96
  203. package/src/providers/memory.ts +0 -142
  204. package/src/runtime/audit-logger.ts +0 -39
  205. package/src/runtime/base.ts +0 -1392
  206. package/src/services/__tests__/access-control.integration.test.ts +0 -443
  207. package/src/services/__tests__/access-control.proof-response-validation.test.ts +0 -578
  208. package/src/services/__tests__/access-control.service.test.ts +0 -970
  209. package/src/services/__tests__/batch-delegation.service.test.ts +0 -351
  210. package/src/services/__tests__/crypto.service.test.ts +0 -531
  211. package/src/services/__tests__/oauth-provider-registry.test.ts +0 -142
  212. package/src/services/__tests__/proof-verifier.integration.test.ts +0 -485
  213. package/src/services/__tests__/proof-verifier.test.ts +0 -489
  214. package/src/services/__tests__/provider-resolution.integration.test.ts +0 -202
  215. package/src/services/__tests__/provider-resolver.test.ts +0 -213
  216. package/src/services/__tests__/storage.service.test.ts +0 -358
  217. package/src/services/access-control.service.ts +0 -990
  218. package/src/services/authorization/authorization-registry.ts +0 -66
  219. package/src/services/authorization/types.ts +0 -71
  220. package/src/services/batch-delegation.service.ts +0 -137
  221. package/src/services/crypto.service.ts +0 -302
  222. package/src/services/errors.ts +0 -76
  223. package/src/services/index.ts +0 -18
  224. package/src/services/oauth-config.service.d.ts +0 -53
  225. package/src/services/oauth-config.service.d.ts.map +0 -1
  226. package/src/services/oauth-config.service.js.map +0 -1
  227. package/src/services/oauth-config.service.ts +0 -192
  228. package/src/services/oauth-provider-registry.d.ts +0 -57
  229. package/src/services/oauth-provider-registry.d.ts.map +0 -1
  230. package/src/services/oauth-provider-registry.js.map +0 -1
  231. package/src/services/oauth-provider-registry.ts +0 -141
  232. package/src/services/oauth-service.ts +0 -544
  233. package/src/services/oauth-token-retrieval.service.ts +0 -245
  234. package/src/services/proof-verifier.ts +0 -478
  235. package/src/services/provider-resolver.d.ts +0 -48
  236. package/src/services/provider-resolver.d.ts.map +0 -1
  237. package/src/services/provider-resolver.js.map +0 -1
  238. package/src/services/provider-resolver.ts +0 -146
  239. package/src/services/provider-validator.ts +0 -170
  240. package/src/services/session-registration.service.ts +0 -251
  241. package/src/services/storage.service.ts +0 -566
  242. package/src/services/tool-context-builder.ts +0 -237
  243. package/src/services/tool-protection.service.ts +0 -1070
  244. package/src/types/oauth-required-error.ts +0 -63
  245. package/src/types/tool-protection.ts +0 -155
  246. package/src/utils/__tests__/did-helpers.test.ts +0 -156
  247. package/src/utils/base58.ts +0 -109
  248. package/src/utils/base64.ts +0 -148
  249. package/src/utils/cors.ts +0 -83
  250. package/src/utils/did-helpers.ts +0 -210
  251. package/src/utils/index.ts +0 -8
  252. package/src/utils/storage-keys.ts +0 -278
  253. package/tsconfig.json +0 -21
  254. package/vitest.config.ts +0 -56
@@ -1,568 +0,0 @@
1
- /**
2
- * Delegation Credential Verifier (Platform-Agnostic)
3
- *
4
- * Progressive enhancement verification for W3C Delegation Credentials.
5
- * Follows the Edge-Delegation-Verification.md pattern:
6
- *
7
- * Stage 1: Fast basic checks (no network, early rejection)
8
- * Stage 2: Parallel advanced checks (signature, status)
9
- * Stage 3: Combined results
10
- *
11
- * Related Spec: MCP-I §4.3, W3C VC Data Model 1.1
12
- * Python Reference: Edge-Delegation-Verification.md
13
- */
14
-
15
- import type {
16
- DelegationCredential,
17
- CredentialStatus,
18
- } from '@kya-os/contracts';
19
- import {
20
- isDelegationCredentialExpired,
21
- isDelegationCredentialNotYetValid,
22
- validateDelegationCredential,
23
- } from '@kya-os/contracts';
24
-
25
- /**
26
- * Verification result for delegation credentials
27
- */
28
- export interface DelegationVCVerificationResult {
29
- /** Whether the delegation credential is valid */
30
- valid: boolean;
31
-
32
- /** Reason for invalid result (if valid=false) */
33
- reason?: string;
34
-
35
- /** Stage at which verification completed */
36
- stage: 'basic' | 'signature' | 'status' | 'complete';
37
-
38
- /** Whether result came from cache */
39
- cached?: boolean;
40
-
41
- /** Performance metrics */
42
- metrics?: {
43
- basicCheckMs?: number;
44
- signatureCheckMs?: number;
45
- statusCheckMs?: number;
46
- totalMs: number;
47
- };
48
-
49
- /** Details about what was checked */
50
- checks?: {
51
- basicValid?: boolean;
52
- signatureValid?: boolean;
53
- statusValid?: boolean;
54
- };
55
- }
56
-
57
- /**
58
- * Options for verification
59
- */
60
- export interface VerifyDelegationVCOptions {
61
- /** Skip cache and force fresh verification */
62
- skipCache?: boolean;
63
-
64
- /** Skip signature verification (faster, less secure) */
65
- skipSignature?: boolean;
66
-
67
- /** Skip status checking (faster, may miss revocations) */
68
- skipStatus?: boolean;
69
-
70
- /** DID resolver for fetching public keys */
71
- didResolver?: DIDResolver;
72
-
73
- /** Status list resolver for checking revocation */
74
- statusListResolver?: StatusListResolver;
75
- }
76
-
77
- /**
78
- * DID Resolver interface
79
- */
80
- export interface DIDResolver {
81
- /**
82
- * Resolve a DID to get the DID Document
83
- * @param did - The DID to resolve
84
- * @returns DID Document with verification methods
85
- */
86
- resolve(did: string): Promise<DIDDocument | null>;
87
- }
88
-
89
- /**
90
- * DID Document (simplified)
91
- */
92
- export interface DIDDocument {
93
- id: string;
94
- verificationMethod?: VerificationMethod[];
95
- authentication?: (string | VerificationMethod)[];
96
- assertionMethod?: (string | VerificationMethod)[];
97
- }
98
-
99
- /**
100
- * Verification Method
101
- */
102
- export interface VerificationMethod {
103
- id: string;
104
- type: string;
105
- controller: string;
106
- publicKeyJwk?: any; // JWK type
107
- publicKeyBase58?: string;
108
- publicKeyMultibase?: string;
109
- }
110
-
111
- /**
112
- * Status List Resolver interface
113
- */
114
- export interface StatusListResolver {
115
- /**
116
- * Check if a credential is revoked via StatusList2021
117
- * @param status - The credential status entry
118
- * @returns true if revoked, false otherwise
119
- */
120
- checkStatus(status: CredentialStatus): Promise<boolean>;
121
- }
122
-
123
- /**
124
- * Signature verification function interface
125
- *
126
- * Platform-specific implementations provide this function.
127
- */
128
- export interface SignatureVerificationFunction {
129
- /**
130
- * Verify an Ed25519 signature on a VC
131
- *
132
- * @param vc - The VC to verify
133
- * @param publicKeyJwk - The public key in JWK format
134
- * @returns Verification result
135
- */
136
- (vc: DelegationCredential, publicKeyJwk: any): Promise<{
137
- valid: boolean;
138
- reason?: string;
139
- }>;
140
- }
141
-
142
- /**
143
- * Delegation Credential Verifier (Platform-Agnostic)
144
- *
145
- * Implements progressive enhancement pattern from Edge-Delegation-Verification.md:
146
- * 1. Fast basic checks (no network) - early rejection
147
- * 2. Parallel advanced checks (signature + status)
148
- * 3. Combined results
149
- */
150
- export class DelegationCredentialVerifier {
151
- private didResolver?: DIDResolver;
152
- private statusListResolver?: StatusListResolver;
153
- private signatureVerifier?: SignatureVerificationFunction;
154
- private cache = new Map<
155
- string,
156
- { result: DelegationVCVerificationResult; expiresAt: number }
157
- >();
158
- private cacheTtl: number;
159
-
160
- constructor(options?: {
161
- didResolver?: DIDResolver;
162
- statusListResolver?: StatusListResolver;
163
- signatureVerifier?: SignatureVerificationFunction;
164
- cacheTtl?: number;
165
- }) {
166
- this.didResolver = options?.didResolver;
167
- this.statusListResolver = options?.statusListResolver;
168
- this.signatureVerifier = options?.signatureVerifier;
169
- this.cacheTtl = options?.cacheTtl || 60_000; // 1 minute default
170
- }
171
-
172
- /**
173
- * Verify a delegation credential with progressive enhancement
174
- *
175
- * Per Edge-Delegation-Verification.md:41-102
176
- *
177
- * @param vc - The delegation credential to verify
178
- * @param options - Verification options
179
- * @returns Verification result
180
- */
181
- async verifyDelegationCredential(
182
- vc: DelegationCredential,
183
- options: VerifyDelegationVCOptions = {}
184
- ): Promise<DelegationVCVerificationResult> {
185
- const startTime = Date.now();
186
-
187
- // Check cache first
188
- if (!options.skipCache) {
189
- const cached = this.getFromCache(vc.id || '');
190
- if (cached) {
191
- return { ...cached, cached: true };
192
- }
193
- }
194
-
195
- // ===================================================================
196
- // STAGE 1: Fast Basic Checks (no network calls)
197
- // Per Edge-Delegation-Verification.md:152-186
198
- // ===================================================================
199
- const basicCheckStart = Date.now();
200
- const basicValidation = this.validateBasicProperties(vc);
201
- const basicCheckMs = Date.now() - basicCheckStart;
202
-
203
- if (!basicValidation.valid) {
204
- const result: DelegationVCVerificationResult = {
205
- valid: false,
206
- reason: basicValidation.reason,
207
- stage: 'basic',
208
- metrics: {
209
- basicCheckMs,
210
- totalMs: Date.now() - startTime,
211
- },
212
- checks: {
213
- basicValid: false,
214
- },
215
- };
216
- return result;
217
- }
218
-
219
- // ===================================================================
220
- // STAGE 2: Parallel Advanced Checks
221
- // Per Edge-Delegation-Verification.md:281-301
222
- // ===================================================================
223
-
224
- // Start signature verification (if not skipped)
225
- const signaturePromise = !options.skipSignature
226
- ? this.verifySignature(vc, options.didResolver || this.didResolver)
227
- : Promise.resolve<{
228
- valid: boolean;
229
- reason?: string;
230
- durationMs?: number;
231
- }>({
232
- valid: true,
233
- durationMs: 0,
234
- });
235
-
236
- // Start status checking (if not skipped)
237
- const statusPromise =
238
- !options.skipStatus && vc.credentialStatus
239
- ? this.checkCredentialStatus(
240
- vc.credentialStatus,
241
- options.statusListResolver || this.statusListResolver
242
- )
243
- : Promise.resolve<{
244
- valid: boolean;
245
- reason?: string;
246
- durationMs?: number;
247
- }>({
248
- valid: true,
249
- durationMs: 0,
250
- });
251
-
252
- // Wait for both checks in parallel
253
- const [signatureResult, statusResult] = await Promise.all([
254
- signaturePromise,
255
- statusPromise,
256
- ]);
257
-
258
- const signatureCheckMs = signatureResult.durationMs || 0;
259
- const statusCheckMs = statusResult.durationMs || 0;
260
-
261
- // ===================================================================
262
- // STAGE 3: Combined Results
263
- // Per Edge-Delegation-Verification.md:82-94
264
- // ===================================================================
265
-
266
- const allValid =
267
- basicValidation.valid && signatureResult.valid && statusResult.valid;
268
-
269
- const result: DelegationVCVerificationResult = {
270
- valid: allValid,
271
- reason: !allValid
272
- ? signatureResult.reason || statusResult.reason || 'Unknown failure'
273
- : undefined,
274
- stage: 'complete',
275
- metrics: {
276
- basicCheckMs,
277
- signatureCheckMs,
278
- statusCheckMs,
279
- totalMs: Date.now() - startTime,
280
- },
281
- checks: {
282
- basicValid: basicValidation.valid,
283
- signatureValid: signatureResult.valid,
284
- statusValid: statusResult.valid,
285
- },
286
- };
287
-
288
- // Cache successful verifications
289
- if (result.valid && vc.id) {
290
- this.setInCache(vc.id, result);
291
- }
292
-
293
- return result;
294
- }
295
-
296
- /**
297
- * Stage 1: Validate basic properties (no network calls)
298
- *
299
- * Fast path for early rejection of invalid delegations.
300
- * Per Edge-Delegation-Verification.md:155-186
301
- *
302
- * @param vc - The delegation credential
303
- * @returns Validation result
304
- */
305
- private validateBasicProperties(
306
- vc: DelegationCredential
307
- ): { valid: boolean; reason?: string } {
308
- // 1. Validate schema
309
- const schemaValidation = validateDelegationCredential(vc);
310
- if (!schemaValidation.success) {
311
- return {
312
- valid: false,
313
- reason: `Schema validation failed: ${schemaValidation.error.message}`,
314
- };
315
- }
316
-
317
- // 2. Check expiration
318
- if (isDelegationCredentialExpired(vc)) {
319
- return { valid: false, reason: 'Delegation credential expired' };
320
- }
321
-
322
- // 3. Check not yet valid
323
- if (isDelegationCredentialNotYetValid(vc)) {
324
- return { valid: false, reason: 'Delegation credential not yet valid' };
325
- }
326
-
327
- // 4. Check delegation status
328
- const delegation = vc.credentialSubject.delegation;
329
- if (delegation.status === 'revoked') {
330
- return { valid: false, reason: 'Delegation status is revoked' };
331
- }
332
- if (delegation.status === 'expired') {
333
- return { valid: false, reason: 'Delegation status is expired' };
334
- }
335
-
336
- // 5. Check required fields
337
- if (!delegation.issuerDid || !delegation.subjectDid) {
338
- return { valid: false, reason: 'Missing issuer or subject DID' };
339
- }
340
-
341
- // 6. Check proof exists (we'll verify it later)
342
- if (!vc.proof) {
343
- return { valid: false, reason: 'Missing proof' };
344
- }
345
-
346
- return { valid: true };
347
- }
348
-
349
- /**
350
- * Stage 2a: Verify signature
351
- *
352
- * Per Edge-Delegation-Verification.md:191-234
353
- *
354
- * @param vc - The delegation credential
355
- * @param didResolver - Optional DID resolver
356
- * @returns Verification result
357
- */
358
- private async verifySignature(
359
- vc: DelegationCredential,
360
- didResolver?: DIDResolver
361
- ): Promise<{ valid: boolean; reason?: string; durationMs?: number }> {
362
- const startTime = Date.now();
363
-
364
- try {
365
- // Get issuer DID
366
- const issuerDid =
367
- typeof vc.issuer === 'string' ? vc.issuer : vc.issuer.id;
368
-
369
- // If no DID resolver or signature verifier, skip verification
370
- if (!didResolver || !this.signatureVerifier) {
371
- return {
372
- valid: true, // Trust but don't verify (no resolver/verifier available)
373
- reason:
374
- 'No DID resolver or signature verifier available, skipping signature verification',
375
- durationMs: Date.now() - startTime,
376
- };
377
- }
378
-
379
- // Resolve issuer DID to get public key
380
- const didDoc = await didResolver.resolve(issuerDid);
381
- if (!didDoc) {
382
- return {
383
- valid: false,
384
- reason: `Could not resolve issuer DID: ${issuerDid}`,
385
- durationMs: Date.now() - startTime,
386
- };
387
- }
388
-
389
- // Find verification method from proof
390
- if (!vc.proof) {
391
- return {
392
- valid: false,
393
- reason: 'Proof is missing',
394
- durationMs: Date.now() - startTime,
395
- };
396
- }
397
-
398
- const verificationMethodId = vc.proof.verificationMethod;
399
- if (!verificationMethodId) {
400
- return {
401
- valid: false,
402
- reason: 'Proof missing verificationMethod',
403
- durationMs: Date.now() - startTime,
404
- };
405
- }
406
-
407
- const verificationMethod = this.findVerificationMethod(
408
- didDoc,
409
- verificationMethodId
410
- );
411
- if (!verificationMethod) {
412
- return {
413
- valid: false,
414
- reason: `Verification method ${verificationMethodId} not found`,
415
- durationMs: Date.now() - startTime,
416
- };
417
- }
418
-
419
- // Extract public key
420
- const publicKeyJwk = verificationMethod.publicKeyJwk;
421
- if (!publicKeyJwk) {
422
- return {
423
- valid: false,
424
- reason: 'Verification method missing publicKeyJwk',
425
- durationMs: Date.now() - startTime,
426
- };
427
- }
428
-
429
- // Verify signature using platform-specific verifier
430
- const verificationResult = await this.signatureVerifier(vc, publicKeyJwk);
431
-
432
- return {
433
- valid: verificationResult.valid,
434
- reason: verificationResult.reason,
435
- durationMs: Date.now() - startTime,
436
- };
437
- } catch (error) {
438
- return {
439
- valid: false,
440
- reason: `Signature verification error: ${
441
- error instanceof Error ? error.message : 'Unknown error'
442
- }`,
443
- durationMs: Date.now() - startTime,
444
- };
445
- }
446
- }
447
-
448
- /**
449
- * Stage 2b: Check credential status via StatusList2021
450
- *
451
- * @param status - The credential status entry
452
- * @param statusListResolver - Optional status list resolver
453
- * @returns Status check result
454
- */
455
- private async checkCredentialStatus(
456
- status: CredentialStatus,
457
- statusListResolver?: StatusListResolver
458
- ): Promise<{ valid: boolean; reason?: string; durationMs?: number }> {
459
- const startTime = Date.now();
460
-
461
- try {
462
- // If no status list resolver, we can't check the status
463
- if (!statusListResolver) {
464
- return {
465
- valid: true, // Trust but don't verify (no resolver available)
466
- reason: 'No status list resolver available, skipping status check',
467
- durationMs: Date.now() - startTime,
468
- };
469
- }
470
-
471
- // Check if credential is revoked/suspended
472
- const isRevoked = await statusListResolver.checkStatus(status);
473
-
474
- if (isRevoked) {
475
- return {
476
- valid: false,
477
- reason: `Credential revoked via StatusList2021 (${status.statusPurpose})`,
478
- durationMs: Date.now() - startTime,
479
- };
480
- }
481
-
482
- return {
483
- valid: true,
484
- durationMs: Date.now() - startTime,
485
- };
486
- } catch (error) {
487
- return {
488
- valid: false,
489
- reason: `Status check error: ${
490
- error instanceof Error ? error.message : 'Unknown error'
491
- }`,
492
- durationMs: Date.now() - startTime,
493
- };
494
- }
495
- }
496
-
497
- /**
498
- * Find verification method in DID document
499
- *
500
- * @param didDoc - The DID document
501
- * @param verificationMethodId - The verification method ID
502
- * @returns Verification method or undefined
503
- */
504
- private findVerificationMethod(
505
- didDoc: DIDDocument,
506
- verificationMethodId: string
507
- ): VerificationMethod | undefined {
508
- return didDoc.verificationMethod?.find(
509
- (vm) => vm.id === verificationMethodId
510
- );
511
- }
512
-
513
- /**
514
- * Get from cache
515
- */
516
- private getFromCache(id: string): DelegationVCVerificationResult | null {
517
- const entry = this.cache.get(id);
518
- if (!entry) return null;
519
-
520
- if (Date.now() > entry.expiresAt) {
521
- this.cache.delete(id);
522
- return null;
523
- }
524
-
525
- return entry.result;
526
- }
527
-
528
- /**
529
- * Set in cache
530
- */
531
- private setInCache(id: string, result: DelegationVCVerificationResult): void {
532
- this.cache.set(id, {
533
- result,
534
- expiresAt: Date.now() + this.cacheTtl,
535
- });
536
- }
537
-
538
- /**
539
- * Clear cache
540
- */
541
- clearCache(): void {
542
- this.cache.clear();
543
- }
544
-
545
- /**
546
- * Clear cache entry for specific VC
547
- */
548
- clearCacheEntry(id: string): void {
549
- this.cache.delete(id);
550
- }
551
- }
552
-
553
- /**
554
- * Create a delegation credential verifier
555
- *
556
- * Convenience factory function.
557
- *
558
- * @param options - Verifier options
559
- * @returns DelegationCredentialVerifier instance
560
- */
561
- export function createDelegationVerifier(options?: {
562
- didResolver?: DIDResolver;
563
- statusListResolver?: StatusListResolver;
564
- signatureVerifier?: SignatureVerificationFunction;
565
- cacheTtl?: number;
566
- }): DelegationCredentialVerifier {
567
- return new DelegationCredentialVerifier(options);
568
- }