@kya-os/mcp-i-core 1.3.7-canary.0 → 1.3.7-canary.clientinfo.20251126041014

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 (236) hide show
  1. package/.turbo/turbo-build.log +4 -0
  2. package/.turbo/turbo-test$colon$coverage.log +4239 -0
  3. package/.turbo/turbo-test.log +2973 -0
  4. package/COMPLIANCE_IMPROVEMENT_REPORT.md +483 -0
  5. package/Composer 3.md +615 -0
  6. package/GPT-5.md +1169 -0
  7. package/OPUS-plan.md +352 -0
  8. package/PHASE_3_AND_4.1_SUMMARY.md +585 -0
  9. package/PHASE_3_SUMMARY.md +317 -0
  10. package/PHASE_4.1.3_SUMMARY.md +428 -0
  11. package/PHASE_4.1_COMPLETE.md +525 -0
  12. package/PHASE_4_USER_DID_IDENTITY_LINKING_PLAN.md +1240 -0
  13. package/SCHEMA_COMPLIANCE_REPORT.md +275 -0
  14. package/TEST_PLAN.md +571 -0
  15. package/coverage/coverage-final.json +57 -0
  16. package/dist/__tests__/utils/mock-providers.d.ts +1 -2
  17. package/dist/__tests__/utils/mock-providers.d.ts.map +1 -1
  18. package/dist/__tests__/utils/mock-providers.js.map +1 -1
  19. package/dist/cache/oauth-config-cache.d.ts +69 -0
  20. package/dist/cache/oauth-config-cache.d.ts.map +1 -0
  21. package/dist/cache/oauth-config-cache.js +76 -0
  22. package/dist/cache/oauth-config-cache.js.map +1 -0
  23. package/dist/identity/idp-token-resolver.d.ts +53 -0
  24. package/dist/identity/idp-token-resolver.d.ts.map +1 -0
  25. package/dist/identity/idp-token-resolver.js +108 -0
  26. package/dist/identity/idp-token-resolver.js.map +1 -0
  27. package/dist/identity/idp-token-storage.interface.d.ts +42 -0
  28. package/dist/identity/idp-token-storage.interface.d.ts.map +1 -0
  29. package/dist/identity/idp-token-storage.interface.js +12 -0
  30. package/dist/identity/idp-token-storage.interface.js.map +1 -0
  31. package/dist/identity/user-did-manager.d.ts +39 -1
  32. package/dist/identity/user-did-manager.d.ts.map +1 -1
  33. package/dist/identity/user-did-manager.js +69 -3
  34. package/dist/identity/user-did-manager.js.map +1 -1
  35. package/dist/index.d.ts +24 -0
  36. package/dist/index.d.ts.map +1 -1
  37. package/dist/index.js +43 -1
  38. package/dist/index.js.map +1 -1
  39. package/dist/runtime/audit-logger.d.ts +37 -0
  40. package/dist/runtime/audit-logger.d.ts.map +1 -0
  41. package/dist/runtime/audit-logger.js +9 -0
  42. package/dist/runtime/audit-logger.js.map +1 -0
  43. package/dist/runtime/base.d.ts +19 -2
  44. package/dist/runtime/base.d.ts.map +1 -1
  45. package/dist/runtime/base.js +227 -11
  46. package/dist/runtime/base.js.map +1 -1
  47. package/dist/services/access-control.service.d.ts.map +1 -1
  48. package/dist/services/access-control.service.js +199 -15
  49. package/dist/services/access-control.service.js.map +1 -1
  50. package/dist/services/authorization/authorization-registry.d.ts +29 -0
  51. package/dist/services/authorization/authorization-registry.d.ts.map +1 -0
  52. package/dist/services/authorization/authorization-registry.js +57 -0
  53. package/dist/services/authorization/authorization-registry.js.map +1 -0
  54. package/dist/services/authorization/types.d.ts +53 -0
  55. package/dist/services/authorization/types.d.ts.map +1 -0
  56. package/dist/services/authorization/types.js +10 -0
  57. package/dist/services/authorization/types.js.map +1 -0
  58. package/dist/services/batch-delegation.service.d.ts +53 -0
  59. package/dist/services/batch-delegation.service.d.ts.map +1 -0
  60. package/dist/services/batch-delegation.service.js +95 -0
  61. package/dist/services/batch-delegation.service.js.map +1 -0
  62. package/dist/services/index.d.ts +2 -0
  63. package/dist/services/index.d.ts.map +1 -1
  64. package/dist/services/index.js +4 -1
  65. package/dist/services/index.js.map +1 -1
  66. package/dist/services/oauth-config.service.d.ts +53 -0
  67. package/dist/services/oauth-config.service.d.ts.map +1 -0
  68. package/dist/services/oauth-config.service.js +141 -0
  69. package/dist/services/oauth-config.service.js.map +1 -0
  70. package/dist/services/oauth-provider-registry.d.ts +88 -0
  71. package/dist/services/oauth-provider-registry.d.ts.map +1 -0
  72. package/dist/services/oauth-provider-registry.js +128 -0
  73. package/dist/services/oauth-provider-registry.js.map +1 -0
  74. package/dist/services/oauth-service.d.ts +77 -0
  75. package/dist/services/oauth-service.d.ts.map +1 -0
  76. package/dist/services/oauth-service.js +373 -0
  77. package/dist/services/oauth-service.js.map +1 -0
  78. package/dist/services/oauth-token-retrieval.service.d.ts +49 -0
  79. package/dist/services/oauth-token-retrieval.service.d.ts.map +1 -0
  80. package/dist/services/oauth-token-retrieval.service.js +150 -0
  81. package/dist/services/oauth-token-retrieval.service.js.map +1 -0
  82. package/dist/services/provider-resolver.d.ts +48 -0
  83. package/dist/services/provider-resolver.d.ts.map +1 -0
  84. package/dist/services/provider-resolver.js +121 -0
  85. package/dist/services/provider-resolver.js.map +1 -0
  86. package/dist/services/provider-validator.d.ts +55 -0
  87. package/dist/services/provider-validator.d.ts.map +1 -0
  88. package/dist/services/provider-validator.js +135 -0
  89. package/dist/services/provider-validator.js.map +1 -0
  90. package/dist/services/session-registration.service.d.ts +80 -0
  91. package/dist/services/session-registration.service.d.ts.map +1 -0
  92. package/dist/services/session-registration.service.js +228 -0
  93. package/dist/services/session-registration.service.js.map +1 -0
  94. package/dist/services/tool-context-builder.d.ts +57 -0
  95. package/dist/services/tool-context-builder.d.ts.map +1 -0
  96. package/dist/services/tool-context-builder.js +125 -0
  97. package/dist/services/tool-context-builder.js.map +1 -0
  98. package/dist/services/tool-protection.service.d.ts +27 -0
  99. package/dist/services/tool-protection.service.d.ts.map +1 -1
  100. package/dist/services/tool-protection.service.js +194 -4
  101. package/dist/services/tool-protection.service.js.map +1 -1
  102. package/dist/types/oauth-required-error.d.ts +40 -0
  103. package/dist/types/oauth-required-error.d.ts.map +1 -0
  104. package/dist/types/oauth-required-error.js +40 -0
  105. package/dist/types/oauth-required-error.js.map +1 -0
  106. package/dist/utils/did-helpers.d.ts +33 -0
  107. package/dist/utils/did-helpers.d.ts.map +1 -1
  108. package/dist/utils/did-helpers.js +40 -0
  109. package/dist/utils/did-helpers.js.map +1 -1
  110. package/dist/utils/index.d.ts +1 -0
  111. package/dist/utils/index.d.ts.map +1 -1
  112. package/dist/utils/index.js +1 -0
  113. package/dist/utils/index.js.map +1 -1
  114. package/docs/API_REFERENCE.md +1362 -0
  115. package/docs/COMPLIANCE_MATRIX.md +691 -0
  116. package/docs/STATUSLIST2021_GUIDE.md +696 -0
  117. package/docs/W3C_VC_DELEGATION_GUIDE.md +710 -0
  118. package/package.json +23 -54
  119. package/scripts/audit-compliance.ts +724 -0
  120. package/src/__tests__/cache/tool-protection-cache.test.ts +640 -0
  121. package/src/__tests__/config/provider-runtime-config.test.ts +309 -0
  122. package/src/__tests__/delegation-e2e.test.ts +690 -0
  123. package/src/__tests__/identity/user-did-manager.test.ts +213 -0
  124. package/src/__tests__/index.test.ts +56 -0
  125. package/src/__tests__/integration/full-flow.test.ts +776 -0
  126. package/src/__tests__/integration.test.ts +281 -0
  127. package/src/__tests__/providers/base.test.ts +173 -0
  128. package/src/__tests__/providers/memory.test.ts +319 -0
  129. package/src/__tests__/regression/phase2-regression.test.ts +429 -0
  130. package/src/__tests__/runtime/audit-logger.test.ts +154 -0
  131. package/src/__tests__/runtime/base-extensions.test.ts +593 -0
  132. package/src/__tests__/runtime/base.test.ts +869 -0
  133. package/src/__tests__/runtime/delegation-flow.test.ts +164 -0
  134. package/src/__tests__/runtime/proof-client-did.test.ts +375 -0
  135. package/src/__tests__/runtime/route-interception.test.ts +686 -0
  136. package/src/__tests__/runtime/tool-protection-enforcement.test.ts +908 -0
  137. package/src/__tests__/services/agentshield-integration.test.ts +784 -0
  138. package/src/__tests__/services/cache-busting.test.ts +125 -0
  139. package/src/__tests__/services/oauth-service-pkce.test.ts +556 -0
  140. package/src/__tests__/services/provider-resolver-edge-cases.test.ts +591 -0
  141. package/src/__tests__/services/tool-protection-oauth-provider.test.ts +480 -0
  142. package/src/__tests__/services/tool-protection.service.test.ts +1366 -0
  143. package/src/__tests__/utils/mock-providers.ts +340 -0
  144. package/src/cache/oauth-config-cache.d.ts +69 -0
  145. package/src/cache/oauth-config-cache.d.ts.map +1 -0
  146. package/src/cache/oauth-config-cache.js.map +1 -0
  147. package/src/cache/oauth-config-cache.ts +123 -0
  148. package/src/cache/tool-protection-cache.ts +171 -0
  149. package/src/compliance/EXAMPLE.md +412 -0
  150. package/src/compliance/__tests__/schema-verifier.test.ts +797 -0
  151. package/src/compliance/index.ts +8 -0
  152. package/src/compliance/schema-registry.ts +460 -0
  153. package/src/compliance/schema-verifier.ts +708 -0
  154. package/src/config/__tests__/remote-config.spec.ts +268 -0
  155. package/src/config/remote-config.ts +174 -0
  156. package/src/config.ts +309 -0
  157. package/src/delegation/__tests__/audience-validator.test.ts +112 -0
  158. package/src/delegation/__tests__/bitstring.test.ts +346 -0
  159. package/src/delegation/__tests__/cascading-revocation.test.ts +628 -0
  160. package/src/delegation/__tests__/delegation-graph.test.ts +584 -0
  161. package/src/delegation/__tests__/utils.test.ts +152 -0
  162. package/src/delegation/__tests__/vc-issuer.test.ts +442 -0
  163. package/src/delegation/__tests__/vc-verifier.test.ts +922 -0
  164. package/src/delegation/audience-validator.ts +52 -0
  165. package/src/delegation/bitstring.ts +278 -0
  166. package/src/delegation/cascading-revocation.ts +370 -0
  167. package/src/delegation/delegation-graph.ts +299 -0
  168. package/src/delegation/index.ts +14 -0
  169. package/src/delegation/statuslist-manager.ts +353 -0
  170. package/src/delegation/storage/__tests__/memory-graph-storage.test.ts +366 -0
  171. package/src/delegation/storage/__tests__/memory-statuslist-storage.test.ts +228 -0
  172. package/src/delegation/storage/index.ts +9 -0
  173. package/src/delegation/storage/memory-graph-storage.ts +178 -0
  174. package/src/delegation/storage/memory-statuslist-storage.ts +77 -0
  175. package/src/delegation/utils.ts +42 -0
  176. package/src/delegation/vc-issuer.ts +232 -0
  177. package/src/delegation/vc-verifier.ts +568 -0
  178. package/src/identity/idp-token-resolver.ts +147 -0
  179. package/src/identity/idp-token-storage.interface.ts +59 -0
  180. package/src/identity/user-did-manager.ts +370 -0
  181. package/src/index.ts +271 -0
  182. package/src/providers/base.d.ts +91 -0
  183. package/src/providers/base.d.ts.map +1 -0
  184. package/src/providers/base.js.map +1 -0
  185. package/src/providers/base.ts +96 -0
  186. package/src/providers/memory.ts +142 -0
  187. package/src/runtime/audit-logger.ts +39 -0
  188. package/src/runtime/base.ts +1329 -0
  189. package/src/services/__tests__/access-control.integration.test.ts +443 -0
  190. package/src/services/__tests__/access-control.proof-response-validation.test.ts +578 -0
  191. package/src/services/__tests__/access-control.service.test.ts +970 -0
  192. package/src/services/__tests__/batch-delegation.service.test.ts +351 -0
  193. package/src/services/__tests__/crypto.service.test.ts +531 -0
  194. package/src/services/__tests__/oauth-provider-registry.test.ts +142 -0
  195. package/src/services/__tests__/proof-verifier.integration.test.ts +485 -0
  196. package/src/services/__tests__/proof-verifier.test.ts +489 -0
  197. package/src/services/__tests__/provider-resolution.integration.test.ts +202 -0
  198. package/src/services/__tests__/provider-resolver.test.ts +213 -0
  199. package/src/services/__tests__/storage.service.test.ts +358 -0
  200. package/src/services/access-control.service.ts +990 -0
  201. package/src/services/authorization/authorization-registry.ts +66 -0
  202. package/src/services/authorization/types.ts +71 -0
  203. package/src/services/batch-delegation.service.ts +137 -0
  204. package/src/services/crypto.service.ts +302 -0
  205. package/src/services/errors.ts +76 -0
  206. package/src/services/index.ts +18 -0
  207. package/src/services/oauth-config.service.d.ts +53 -0
  208. package/src/services/oauth-config.service.d.ts.map +1 -0
  209. package/src/services/oauth-config.service.js.map +1 -0
  210. package/src/services/oauth-config.service.ts +192 -0
  211. package/src/services/oauth-provider-registry.d.ts +57 -0
  212. package/src/services/oauth-provider-registry.d.ts.map +1 -0
  213. package/src/services/oauth-provider-registry.js.map +1 -0
  214. package/src/services/oauth-provider-registry.ts +141 -0
  215. package/src/services/oauth-service.ts +544 -0
  216. package/src/services/oauth-token-retrieval.service.ts +245 -0
  217. package/src/services/proof-verifier.ts +478 -0
  218. package/src/services/provider-resolver.d.ts +48 -0
  219. package/src/services/provider-resolver.d.ts.map +1 -0
  220. package/src/services/provider-resolver.js.map +1 -0
  221. package/src/services/provider-resolver.ts +146 -0
  222. package/src/services/provider-validator.ts +170 -0
  223. package/src/services/session-registration.service.ts +317 -0
  224. package/src/services/storage.service.ts +566 -0
  225. package/src/services/tool-context-builder.ts +172 -0
  226. package/src/services/tool-protection.service.ts +982 -0
  227. package/src/types/oauth-required-error.ts +63 -0
  228. package/src/types/tool-protection.ts +155 -0
  229. package/src/utils/__tests__/did-helpers.test.ts +101 -0
  230. package/src/utils/base64.ts +148 -0
  231. package/src/utils/cors.ts +83 -0
  232. package/src/utils/did-helpers.ts +150 -0
  233. package/src/utils/index.ts +8 -0
  234. package/src/utils/storage-keys.ts +278 -0
  235. package/tsconfig.json +21 -0
  236. package/vitest.config.ts +56 -0
@@ -0,0 +1,485 @@
1
+ /**
2
+ * Integration Tests for ProofVerifier with Real DID Resolution
3
+ *
4
+ * These tests verify end-to-end proof verification with actual DID resolution,
5
+ * testing both did:key (offline) and did:web (HTTP) resolution methods.
6
+ */
7
+
8
+ import { describe, it, expect, beforeEach, vi } from "vitest";
9
+ import { ProofVerifier } from "../proof-verifier.js";
10
+ import { CryptoService, type Ed25519JWK } from "../crypto.service.js";
11
+ import type {
12
+ CryptoProvider,
13
+ ClockProvider,
14
+ NonceCacheProvider,
15
+ FetchProvider,
16
+ } from "../../providers/base.js";
17
+ import type { DetachedProof } from "@kya-os/contracts/proof";
18
+ import {
19
+ PROOF_VERIFICATION_ERROR_CODES,
20
+ ProofVerificationError,
21
+ } from "../errors.js";
22
+
23
+ describe("ProofVerifier Integration - Real DID Resolution", () => {
24
+ let proofVerifier: ProofVerifier;
25
+ let mockCryptoProvider: CryptoProvider;
26
+ let mockClockProvider: ClockProvider;
27
+ let mockNonceCache: NonceCacheProvider;
28
+ let realFetchProvider: FetchProvider;
29
+
30
+ const validJwk: Ed25519JWK = {
31
+ kty: "OKP",
32
+ crv: "Ed25519",
33
+ x: "VCpo2LMLhn6iWku8MKvSLg2ZAoC-nlOyPVQaO3FxVeQ",
34
+ kid: "did:key:z6MkhaXgBZDvVCpo2LMLhn6iWku8MKvSLg2ZAoC-nlOyPVQaO3FxVeQ#key-1",
35
+ };
36
+
37
+ const createValidProof = (did: string, kid: string): DetachedProof => {
38
+ const header = { alg: "EdDSA", typ: "JWT" };
39
+ const payload = {
40
+ aud: "test-audience",
41
+ sub: did,
42
+ iss: did,
43
+ nonce: `nonce-${Date.now()}`,
44
+ ts: Math.floor(Date.now() / 1000),
45
+ sessionId: "session123",
46
+ requestHash: "sha256:" + "a".repeat(64),
47
+ responseHash: "sha256:" + "b".repeat(64),
48
+ };
49
+ // Use TextEncoder/Decoder for base64 encoding (works in both Node and browser)
50
+ const encoder = new TextEncoder();
51
+ const headerB64 = btoa(JSON.stringify(header))
52
+ .replace(/\+/g, "-")
53
+ .replace(/\//g, "_")
54
+ .replace(/=/g, "");
55
+ const payloadB64 = btoa(JSON.stringify(payload))
56
+ .replace(/\+/g, "-")
57
+ .replace(/\//g, "_")
58
+ .replace(/=/g, "");
59
+ const signatureB64 = btoa("signature")
60
+ .replace(/\+/g, "-")
61
+ .replace(/\//g, "_")
62
+ .replace(/=/g, "");
63
+ const jws = `${headerB64}.${payloadB64}.${signatureB64}`;
64
+
65
+ return {
66
+ jws,
67
+ meta: {
68
+ did,
69
+ kid,
70
+ ts: Math.floor(Date.now() / 1000),
71
+ nonce: `nonce-${Date.now()}`,
72
+ audience: "test-audience",
73
+ sessionId: "session123",
74
+ requestHash: "sha256:" + "a".repeat(64),
75
+ responseHash: "sha256:" + "b".repeat(64),
76
+ },
77
+ };
78
+ };
79
+
80
+ beforeEach(() => {
81
+ mockCryptoProvider = {
82
+ sign: vi.fn(),
83
+ verify: vi.fn().mockResolvedValue(true),
84
+ generateKeyPair: vi.fn(),
85
+ hash: vi.fn(),
86
+ randomBytes: vi.fn(),
87
+ };
88
+
89
+ mockClockProvider = {
90
+ now: vi.fn().mockReturnValue(Date.now()),
91
+ isWithinSkew: vi.fn().mockReturnValue(true),
92
+ hasExpired: vi.fn(),
93
+ calculateExpiry: vi.fn(
94
+ (ttlSeconds: number) => Date.now() + ttlSeconds * 1000
95
+ ),
96
+ format: vi.fn(),
97
+ };
98
+
99
+ mockNonceCache = {
100
+ has: vi.fn().mockResolvedValue(false),
101
+ add: vi.fn().mockResolvedValue(undefined),
102
+ cleanup: vi.fn().mockResolvedValue(undefined),
103
+ destroy: vi.fn().mockResolvedValue(undefined),
104
+ };
105
+
106
+ // Real fetch provider that actually resolves DIDs
107
+ realFetchProvider = {
108
+ resolveDID: async (did: string) => {
109
+ // did:key resolution (offline)
110
+ if (did.startsWith("did:key:")) {
111
+ const publicKeyMultibase = did.slice("did:key:".length);
112
+ return {
113
+ "@context": ["https://www.w3.org/ns/did/v1"],
114
+ id: did,
115
+ verificationMethod: [
116
+ {
117
+ id: `${did}#key-1`,
118
+ type: "Ed25519VerificationKey2020",
119
+ controller: did,
120
+ publicKeyMultibase,
121
+ publicKeyJwk: validJwk,
122
+ },
123
+ ],
124
+ authentication: [`${did}#key-1`],
125
+ assertionMethod: [`${did}#key-1`],
126
+ };
127
+ }
128
+
129
+ // did:web resolution (HTTP)
130
+ if (did.startsWith("did:web:")) {
131
+ const domain = did.slice("did:web:".length).replace(/:/g, "/");
132
+ const url = `https://${domain}/.well-known/did.json`;
133
+
134
+ try {
135
+ const response = await fetch(url);
136
+ if (!response.ok) {
137
+ throw new Error(
138
+ `HTTP ${response.status}: ${response.statusText}`
139
+ );
140
+ }
141
+ return await response.json();
142
+ } catch (error) {
143
+ throw new Error(
144
+ `Failed to resolve ${did}: ${error instanceof Error ? error.message : String(error)}`
145
+ );
146
+ }
147
+ }
148
+
149
+ throw new Error(`Unsupported DID method: ${did}`);
150
+ },
151
+ fetchStatusList: vi.fn(),
152
+ fetchDelegationChain: vi.fn(),
153
+ fetch: vi.fn(),
154
+ };
155
+
156
+ proofVerifier = new ProofVerifier({
157
+ cryptoProvider: mockCryptoProvider,
158
+ clockProvider: mockClockProvider,
159
+ nonceCacheProvider: mockNonceCache,
160
+ fetchProvider: realFetchProvider,
161
+ timestampSkewSeconds: 120,
162
+ nonceTtlSeconds: 300,
163
+ });
164
+ });
165
+
166
+ describe("did:key Resolution (Offline)", () => {
167
+ it("should resolve did:key and extract public key", async () => {
168
+ const did =
169
+ "did:key:z6MkhaXgBZDvVCpo2LMLhn6iWku8MKvSLg2ZAoC-nlOyPVQaO3FxVeQ";
170
+ const kid = "key-1"; // Just the fragment, not the full DID#key-1
171
+
172
+ const publicKey = await proofVerifier.fetchPublicKeyFromDID(did, kid);
173
+
174
+ expect(publicKey).toBeDefined();
175
+ expect(publicKey?.kty).toBe("OKP");
176
+ expect(publicKey?.crv).toBe("Ed25519");
177
+ expect(publicKey?.kid).toBe(`${did}#${kid}`);
178
+ });
179
+
180
+ it("should verify proof with did:key resolution", async () => {
181
+ const did =
182
+ "did:key:z6MkhaXgBZDvVCpo2LMLhn6iWku8MKvSLg2ZAoC-nlOyPVQaO3FxVeQ";
183
+ const kid = "key-1";
184
+ const proof = createValidProof(did, `${did}#${kid}`); // Full kid in proof meta
185
+
186
+ // Fetch public key from DID
187
+ const publicKey = await proofVerifier.fetchPublicKeyFromDID(did, kid);
188
+ expect(publicKey).toBeDefined();
189
+
190
+ // Verify proof
191
+ const result = await proofVerifier.verifyProof(proof, publicKey!);
192
+
193
+ expect(result.valid).toBe(true);
194
+ expect(mockNonceCache.has).toHaveBeenCalledWith(proof.meta.nonce, proof.meta.did);
195
+ expect(mockNonceCache.add).toHaveBeenCalledWith(
196
+ proof.meta.nonce,
197
+ expect.any(Number),
198
+ proof.meta.did
199
+ );
200
+ });
201
+
202
+ it("should handle missing verification method in did:key", async () => {
203
+ const did =
204
+ "did:key:z6MkhaXgBZDvVCpo2LMLhn6iWku8MKvSLg2ZAoC-nlOyPVQaO3FxVeQ";
205
+ const kid = "nonexistent"; // Just the fragment
206
+
207
+ await expect(
208
+ proofVerifier.fetchPublicKeyFromDID(did, kid)
209
+ ).rejects.toThrow(ProofVerificationError);
210
+
211
+ try {
212
+ await proofVerifier.fetchPublicKeyFromDID(did, kid);
213
+ expect.fail("Should have thrown ProofVerificationError");
214
+ } catch (error) {
215
+ expect(error).toBeInstanceOf(ProofVerificationError);
216
+ expect((error as ProofVerificationError).code).toBe(
217
+ PROOF_VERIFICATION_ERROR_CODES.VERIFICATION_METHOD_NOT_FOUND
218
+ );
219
+ }
220
+ });
221
+ });
222
+
223
+ describe("did:web Resolution (HTTP)", () => {
224
+ // Skip this test in CI environments - it depends on external network calls
225
+ // and can be flaky. Run locally for manual verification only.
226
+ it.skip("should resolve did:web from real endpoint", async () => {
227
+ // Use a known did:web endpoint (example.com has a well-known DID document)
228
+ // Note: This test may fail if the endpoint is unavailable
229
+ const did = "did:web:example.com";
230
+
231
+ try {
232
+ const publicKey = await proofVerifier.fetchPublicKeyFromDID(did);
233
+
234
+ // If resolution succeeds, verify structure
235
+ if (publicKey) {
236
+ expect(publicKey.kty).toBe("OKP");
237
+ expect(publicKey.crv).toBe("Ed25519");
238
+ }
239
+ } catch (error) {
240
+ // If resolution fails (network issue, endpoint doesn't exist), that's okay
241
+ // We're testing that the resolution mechanism works, not that every endpoint exists
242
+ expect(error).toBeInstanceOf(ProofVerificationError);
243
+ expect((error as ProofVerificationError).code).toBe(
244
+ PROOF_VERIFICATION_ERROR_CODES.DID_RESOLUTION_FAILED
245
+ );
246
+ }
247
+ }, 10000); // 10 second timeout for HTTP requests
248
+
249
+ it("should handle HTTP errors gracefully", async () => {
250
+ const did = "did:web:nonexistent-domain-that-does-not-exist-12345.com";
251
+
252
+ await expect(proofVerifier.fetchPublicKeyFromDID(did)).rejects.toThrow(
253
+ ProofVerificationError
254
+ );
255
+
256
+ try {
257
+ await proofVerifier.fetchPublicKeyFromDID(did);
258
+ } catch (error) {
259
+ expect(error).toBeInstanceOf(ProofVerificationError);
260
+ expect((error as ProofVerificationError).code).toBe(
261
+ PROOF_VERIFICATION_ERROR_CODES.DID_RESOLUTION_FAILED
262
+ );
263
+ }
264
+ }, 10000);
265
+
266
+ it("should handle DID document without verification methods", async () => {
267
+ // Mock a DID document without verification methods
268
+ const mockFetchProvider: FetchProvider = {
269
+ resolveDID: vi.fn().mockResolvedValue({
270
+ "@context": ["https://www.w3.org/ns/did/v1"],
271
+ id: "did:web:test.com",
272
+ // No verificationMethod
273
+ }),
274
+ fetchStatusList: vi.fn(),
275
+ fetchDelegationChain: vi.fn(),
276
+ fetch: vi.fn(),
277
+ };
278
+
279
+ const testVerifier = new ProofVerifier({
280
+ cryptoProvider: mockCryptoProvider,
281
+ clockProvider: mockClockProvider,
282
+ nonceCacheProvider: mockNonceCache,
283
+ fetchProvider: mockFetchProvider,
284
+ });
285
+
286
+ await expect(
287
+ testVerifier.fetchPublicKeyFromDID("did:web:test.com")
288
+ ).rejects.toThrow(ProofVerificationError);
289
+
290
+ try {
291
+ await testVerifier.fetchPublicKeyFromDID("did:web:test.com");
292
+ } catch (error) {
293
+ expect(error).toBeInstanceOf(ProofVerificationError);
294
+ expect((error as ProofVerificationError).code).toBe(
295
+ PROOF_VERIFICATION_ERROR_CODES.VERIFICATION_METHOD_NOT_FOUND
296
+ );
297
+ }
298
+ });
299
+
300
+ it("should handle verification method without publicKeyJwk", async () => {
301
+ const mockFetchProvider: FetchProvider = {
302
+ resolveDID: vi.fn().mockResolvedValue({
303
+ "@context": ["https://www.w3.org/ns/did/v1"],
304
+ id: "did:web:test.com",
305
+ verificationMethod: [
306
+ {
307
+ id: "did:web:test.com#key-1",
308
+ type: "Ed25519VerificationKey2020",
309
+ controller: "did:web:test.com",
310
+ // No publicKeyJwk
311
+ },
312
+ ],
313
+ }),
314
+ fetchStatusList: vi.fn(),
315
+ fetchDelegationChain: vi.fn(),
316
+ fetch: vi.fn(),
317
+ };
318
+
319
+ const testVerifier = new ProofVerifier({
320
+ cryptoProvider: mockCryptoProvider,
321
+ clockProvider: mockClockProvider,
322
+ nonceCacheProvider: mockNonceCache,
323
+ fetchProvider: mockFetchProvider,
324
+ });
325
+
326
+ await expect(
327
+ testVerifier.fetchPublicKeyFromDID("did:web:test.com", "key-1")
328
+ ).rejects.toThrow(ProofVerificationError);
329
+
330
+ try {
331
+ await testVerifier.fetchPublicKeyFromDID("did:web:test.com", "key-1");
332
+ } catch (error) {
333
+ expect(error).toBeInstanceOf(ProofVerificationError);
334
+ expect((error as ProofVerificationError).code).toBe(
335
+ PROOF_VERIFICATION_ERROR_CODES.PUBLIC_KEY_NOT_FOUND
336
+ );
337
+ }
338
+ });
339
+
340
+ it("should handle non-Ed25519 keys", async () => {
341
+ const mockFetchProvider: FetchProvider = {
342
+ resolveDID: vi.fn().mockResolvedValue({
343
+ "@context": ["https://www.w3.org/ns/did/v1"],
344
+ id: "did:web:test.com",
345
+ verificationMethod: [
346
+ {
347
+ id: "did:web:test.com#key-1",
348
+ type: "RsaVerificationKey2018",
349
+ controller: "did:web:test.com",
350
+ publicKeyJwk: {
351
+ kty: "RSA",
352
+ n: "test",
353
+ e: "AQAB",
354
+ },
355
+ },
356
+ ],
357
+ }),
358
+ fetchStatusList: vi.fn(),
359
+ fetchDelegationChain: vi.fn(),
360
+ fetch: vi.fn(),
361
+ };
362
+
363
+ const testVerifier = new ProofVerifier({
364
+ cryptoProvider: mockCryptoProvider,
365
+ clockProvider: mockClockProvider,
366
+ nonceCacheProvider: mockNonceCache,
367
+ fetchProvider: mockFetchProvider,
368
+ });
369
+
370
+ await expect(
371
+ testVerifier.fetchPublicKeyFromDID("did:web:test.com", "key-1")
372
+ ).rejects.toThrow(ProofVerificationError);
373
+
374
+ try {
375
+ await testVerifier.fetchPublicKeyFromDID("did:web:test.com", "key-1");
376
+ } catch (error) {
377
+ expect(error).toBeInstanceOf(ProofVerificationError);
378
+ expect((error as ProofVerificationError).code).toBe(
379
+ PROOF_VERIFICATION_ERROR_CODES.INVALID_JWK_FORMAT
380
+ );
381
+ }
382
+ });
383
+ });
384
+
385
+ describe("End-to-End Proof Verification with DID Resolution", () => {
386
+ it("should verify proof with did:key resolution end-to-end", async () => {
387
+ const did =
388
+ "did:key:z6MkhaXgBZDvVCpo2LMLhn6iWku8MKvSLg2ZAoC-nlOyPVQaO3FxVeQ";
389
+ const kid = "key-1";
390
+ const proof = createValidProof(did, `${did}#${kid}`); // Full kid in proof meta
391
+
392
+ // Fetch public key from DID
393
+ const publicKey = await proofVerifier.fetchPublicKeyFromDID(did, kid);
394
+ expect(publicKey).toBeDefined();
395
+
396
+ // Verify proof
397
+ const result = await proofVerifier.verifyProof(proof, publicKey!);
398
+
399
+ expect(result.valid).toBe(true);
400
+ expect(result.errorCode).toBeUndefined();
401
+ expect(mockCryptoProvider.verify).toHaveBeenCalled();
402
+ });
403
+
404
+ it("should return specific error code for nonce replay", async () => {
405
+ const did =
406
+ "did:key:z6MkhaXgBZDvVCpo2LMLhn6iWku8MKvSLg2ZAoC-nlOyPVQaO3FxVeQ";
407
+ const kid = "key-1";
408
+ const proof = createValidProof(did, `${did}#${kid}`);
409
+ const publicKey = await proofVerifier.fetchPublicKeyFromDID(did, kid);
410
+
411
+ // First verification should succeed
412
+ const result1 = await proofVerifier.verifyProof(proof, publicKey!);
413
+ expect(result1.valid).toBe(true);
414
+
415
+ // Second verification should fail with nonce replay error
416
+ mockNonceCache.has = vi.fn().mockResolvedValue(true);
417
+ const result2 = await proofVerifier.verifyProof(proof, publicKey!);
418
+
419
+ expect(result2.valid).toBe(false);
420
+ expect(result2.errorCode).toBe(
421
+ PROOF_VERIFICATION_ERROR_CODES.NONCE_REPLAY_DETECTED
422
+ );
423
+ expect(result2.details?.nonce).toBe(proof.meta.nonce);
424
+ });
425
+
426
+ it("should return specific error code for timestamp skew", async () => {
427
+ const did =
428
+ "did:key:z6MkhaXgBZDvVCpo2LMLhn6iWku8MKvSLg2ZAoC-nlOyPVQaO3FxVeQ";
429
+ const kid = "key-1";
430
+ const proof = createValidProof(did, `${did}#${kid}`);
431
+ const publicKey = await proofVerifier.fetchPublicKeyFromDID(did, kid);
432
+
433
+ // Mock clock to reject timestamp
434
+ mockClockProvider.isWithinSkew = vi.fn().mockReturnValue(false);
435
+
436
+ const result = await proofVerifier.verifyProof(proof, publicKey!);
437
+
438
+ expect(result.valid).toBe(false);
439
+ expect(result.errorCode).toBe(
440
+ PROOF_VERIFICATION_ERROR_CODES.TIMESTAMP_SKEW_EXCEEDED
441
+ );
442
+ expect(result.details?.timestamp).toBe(proof.meta.ts);
443
+ expect(result.details?.skewSeconds).toBe(120);
444
+ });
445
+
446
+ it("should return specific error code for invalid signature", async () => {
447
+ const did =
448
+ "did:key:z6MkhaXgBZDvVCpo2LMLhn6iWku8MKvSLg2ZAoC-nlOyPVQaO3FxVeQ";
449
+ const kid = "key-1";
450
+ const proof = createValidProof(did, `${did}#${kid}`);
451
+ const publicKey = await proofVerifier.fetchPublicKeyFromDID(did, kid);
452
+
453
+ // Mock signature verification to fail
454
+ mockCryptoProvider.verify = vi.fn().mockResolvedValue(false);
455
+
456
+ const result = await proofVerifier.verifyProof(proof, publicKey!);
457
+
458
+ expect(result.valid).toBe(false);
459
+ expect(result.errorCode).toBe(
460
+ PROOF_VERIFICATION_ERROR_CODES.INVALID_JWS_SIGNATURE
461
+ );
462
+ expect(result.details?.expectedKid).toBe(`${did}#${kid}`);
463
+ });
464
+ });
465
+
466
+ describe("Error Code Coverage", () => {
467
+ it("should return INVALID_PROOF_STRUCTURE for malformed proof", async () => {
468
+ const invalidProof = {
469
+ jws: "invalid",
470
+ meta: {
471
+ // Missing required fields
472
+ did: "did:test",
473
+ },
474
+ } as any;
475
+
476
+ const result = await proofVerifier.verifyProof(invalidProof, validJwk);
477
+
478
+ expect(result.valid).toBe(false);
479
+ expect(result.errorCode).toBe(
480
+ PROOF_VERIFICATION_ERROR_CODES.INVALID_PROOF_STRUCTURE
481
+ );
482
+ expect(result.details?.zodErrors).toBeDefined();
483
+ });
484
+ });
485
+ });