@mcp-i/core 0.1.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.
Files changed (226) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +390 -0
  3. package/dist/auth/handshake.d.ts +104 -0
  4. package/dist/auth/handshake.d.ts.map +1 -0
  5. package/dist/auth/handshake.js +230 -0
  6. package/dist/auth/handshake.js.map +1 -0
  7. package/dist/auth/index.d.ts +3 -0
  8. package/dist/auth/index.d.ts.map +1 -0
  9. package/dist/auth/index.js +2 -0
  10. package/dist/auth/index.js.map +1 -0
  11. package/dist/auth/types.d.ts +31 -0
  12. package/dist/auth/types.d.ts.map +1 -0
  13. package/dist/auth/types.js +7 -0
  14. package/dist/auth/types.js.map +1 -0
  15. package/dist/delegation/audience-validator.d.ts +9 -0
  16. package/dist/delegation/audience-validator.d.ts.map +1 -0
  17. package/dist/delegation/audience-validator.js +17 -0
  18. package/dist/delegation/audience-validator.js.map +1 -0
  19. package/dist/delegation/bitstring.d.ts +37 -0
  20. package/dist/delegation/bitstring.d.ts.map +1 -0
  21. package/dist/delegation/bitstring.js +117 -0
  22. package/dist/delegation/bitstring.js.map +1 -0
  23. package/dist/delegation/cascading-revocation.d.ts +45 -0
  24. package/dist/delegation/cascading-revocation.d.ts.map +1 -0
  25. package/dist/delegation/cascading-revocation.js +148 -0
  26. package/dist/delegation/cascading-revocation.js.map +1 -0
  27. package/dist/delegation/delegation-graph.d.ts +49 -0
  28. package/dist/delegation/delegation-graph.d.ts.map +1 -0
  29. package/dist/delegation/delegation-graph.js +99 -0
  30. package/dist/delegation/delegation-graph.js.map +1 -0
  31. package/dist/delegation/did-key-resolver.d.ts +64 -0
  32. package/dist/delegation/did-key-resolver.d.ts.map +1 -0
  33. package/dist/delegation/did-key-resolver.js +154 -0
  34. package/dist/delegation/did-key-resolver.js.map +1 -0
  35. package/dist/delegation/did-web-resolver.d.ts +83 -0
  36. package/dist/delegation/did-web-resolver.d.ts.map +1 -0
  37. package/dist/delegation/did-web-resolver.js +218 -0
  38. package/dist/delegation/did-web-resolver.js.map +1 -0
  39. package/dist/delegation/index.d.ts +21 -0
  40. package/dist/delegation/index.d.ts.map +1 -0
  41. package/dist/delegation/index.js +21 -0
  42. package/dist/delegation/index.js.map +1 -0
  43. package/dist/delegation/outbound-headers.d.ts +81 -0
  44. package/dist/delegation/outbound-headers.d.ts.map +1 -0
  45. package/dist/delegation/outbound-headers.js +139 -0
  46. package/dist/delegation/outbound-headers.js.map +1 -0
  47. package/dist/delegation/outbound-proof.d.ts +43 -0
  48. package/dist/delegation/outbound-proof.d.ts.map +1 -0
  49. package/dist/delegation/outbound-proof.js +52 -0
  50. package/dist/delegation/outbound-proof.js.map +1 -0
  51. package/dist/delegation/statuslist-manager.d.ts +44 -0
  52. package/dist/delegation/statuslist-manager.d.ts.map +1 -0
  53. package/dist/delegation/statuslist-manager.js +126 -0
  54. package/dist/delegation/statuslist-manager.js.map +1 -0
  55. package/dist/delegation/storage/memory-graph-storage.d.ts +70 -0
  56. package/dist/delegation/storage/memory-graph-storage.d.ts.map +1 -0
  57. package/dist/delegation/storage/memory-graph-storage.js +145 -0
  58. package/dist/delegation/storage/memory-graph-storage.js.map +1 -0
  59. package/dist/delegation/storage/memory-statuslist-storage.d.ts +19 -0
  60. package/dist/delegation/storage/memory-statuslist-storage.d.ts.map +1 -0
  61. package/dist/delegation/storage/memory-statuslist-storage.js +33 -0
  62. package/dist/delegation/storage/memory-statuslist-storage.js.map +1 -0
  63. package/dist/delegation/utils.d.ts +49 -0
  64. package/dist/delegation/utils.d.ts.map +1 -0
  65. package/dist/delegation/utils.js +131 -0
  66. package/dist/delegation/utils.js.map +1 -0
  67. package/dist/delegation/vc-issuer.d.ts +56 -0
  68. package/dist/delegation/vc-issuer.d.ts.map +1 -0
  69. package/dist/delegation/vc-issuer.js +80 -0
  70. package/dist/delegation/vc-issuer.js.map +1 -0
  71. package/dist/delegation/vc-verifier.d.ts +112 -0
  72. package/dist/delegation/vc-verifier.d.ts.map +1 -0
  73. package/dist/delegation/vc-verifier.js +280 -0
  74. package/dist/delegation/vc-verifier.js.map +1 -0
  75. package/dist/index.d.ts +45 -0
  76. package/dist/index.d.ts.map +1 -0
  77. package/dist/index.js +53 -0
  78. package/dist/index.js.map +1 -0
  79. package/dist/logging/index.d.ts +2 -0
  80. package/dist/logging/index.d.ts.map +1 -0
  81. package/dist/logging/index.js +2 -0
  82. package/dist/logging/index.js.map +1 -0
  83. package/dist/logging/logger.d.ts +23 -0
  84. package/dist/logging/logger.d.ts.map +1 -0
  85. package/dist/logging/logger.js +82 -0
  86. package/dist/logging/logger.js.map +1 -0
  87. package/dist/middleware/index.d.ts +7 -0
  88. package/dist/middleware/index.d.ts.map +1 -0
  89. package/dist/middleware/index.js +7 -0
  90. package/dist/middleware/index.js.map +1 -0
  91. package/dist/middleware/with-mcpi.d.ts +152 -0
  92. package/dist/middleware/with-mcpi.d.ts.map +1 -0
  93. package/dist/middleware/with-mcpi.js +472 -0
  94. package/dist/middleware/with-mcpi.js.map +1 -0
  95. package/dist/proof/errors.d.ts +49 -0
  96. package/dist/proof/errors.d.ts.map +1 -0
  97. package/dist/proof/errors.js +61 -0
  98. package/dist/proof/errors.js.map +1 -0
  99. package/dist/proof/generator.d.ts +65 -0
  100. package/dist/proof/generator.d.ts.map +1 -0
  101. package/dist/proof/generator.js +163 -0
  102. package/dist/proof/generator.js.map +1 -0
  103. package/dist/proof/index.d.ts +4 -0
  104. package/dist/proof/index.d.ts.map +1 -0
  105. package/dist/proof/index.js +4 -0
  106. package/dist/proof/index.js.map +1 -0
  107. package/dist/proof/verifier.d.ts +108 -0
  108. package/dist/proof/verifier.d.ts.map +1 -0
  109. package/dist/proof/verifier.js +299 -0
  110. package/dist/proof/verifier.js.map +1 -0
  111. package/dist/providers/base.d.ts +64 -0
  112. package/dist/providers/base.d.ts.map +1 -0
  113. package/dist/providers/base.js +19 -0
  114. package/dist/providers/base.js.map +1 -0
  115. package/dist/providers/index.d.ts +3 -0
  116. package/dist/providers/index.d.ts.map +1 -0
  117. package/dist/providers/index.js +3 -0
  118. package/dist/providers/index.js.map +1 -0
  119. package/dist/providers/memory.d.ts +33 -0
  120. package/dist/providers/memory.d.ts.map +1 -0
  121. package/dist/providers/memory.js +102 -0
  122. package/dist/providers/memory.js.map +1 -0
  123. package/dist/session/index.d.ts +2 -0
  124. package/dist/session/index.d.ts.map +1 -0
  125. package/dist/session/index.js +2 -0
  126. package/dist/session/index.js.map +1 -0
  127. package/dist/session/manager.d.ts +77 -0
  128. package/dist/session/manager.d.ts.map +1 -0
  129. package/dist/session/manager.js +251 -0
  130. package/dist/session/manager.js.map +1 -0
  131. package/dist/types/protocol.d.ts +320 -0
  132. package/dist/types/protocol.d.ts.map +1 -0
  133. package/dist/types/protocol.js +229 -0
  134. package/dist/types/protocol.js.map +1 -0
  135. package/dist/utils/base58.d.ts +31 -0
  136. package/dist/utils/base58.d.ts.map +1 -0
  137. package/dist/utils/base58.js +104 -0
  138. package/dist/utils/base58.js.map +1 -0
  139. package/dist/utils/base64.d.ts +13 -0
  140. package/dist/utils/base64.d.ts.map +1 -0
  141. package/dist/utils/base64.js +99 -0
  142. package/dist/utils/base64.js.map +1 -0
  143. package/dist/utils/crypto-service.d.ts +37 -0
  144. package/dist/utils/crypto-service.d.ts.map +1 -0
  145. package/dist/utils/crypto-service.js +153 -0
  146. package/dist/utils/crypto-service.js.map +1 -0
  147. package/dist/utils/did-helpers.d.ts +156 -0
  148. package/dist/utils/did-helpers.d.ts.map +1 -0
  149. package/dist/utils/did-helpers.js +193 -0
  150. package/dist/utils/did-helpers.js.map +1 -0
  151. package/dist/utils/ed25519-constants.d.ts +18 -0
  152. package/dist/utils/ed25519-constants.d.ts.map +1 -0
  153. package/dist/utils/ed25519-constants.js +21 -0
  154. package/dist/utils/ed25519-constants.js.map +1 -0
  155. package/dist/utils/index.d.ts +5 -0
  156. package/dist/utils/index.d.ts.map +1 -0
  157. package/dist/utils/index.js +5 -0
  158. package/dist/utils/index.js.map +1 -0
  159. package/package.json +105 -0
  160. package/src/__tests__/integration/full-flow.test.ts +362 -0
  161. package/src/__tests__/providers/base.test.ts +173 -0
  162. package/src/__tests__/providers/memory.test.ts +332 -0
  163. package/src/__tests__/utils/mock-providers.ts +319 -0
  164. package/src/__tests__/utils/node-crypto-provider.ts +93 -0
  165. package/src/auth/handshake.ts +411 -0
  166. package/src/auth/index.ts +11 -0
  167. package/src/auth/types.ts +40 -0
  168. package/src/delegation/__tests__/audience-validator.test.ts +110 -0
  169. package/src/delegation/__tests__/bitstring.test.ts +346 -0
  170. package/src/delegation/__tests__/cascading-revocation.test.ts +624 -0
  171. package/src/delegation/__tests__/delegation-graph.test.ts +623 -0
  172. package/src/delegation/__tests__/did-key-resolver.test.ts +265 -0
  173. package/src/delegation/__tests__/did-web-resolver.test.ts +467 -0
  174. package/src/delegation/__tests__/outbound-headers.test.ts +230 -0
  175. package/src/delegation/__tests__/outbound-proof.test.ts +179 -0
  176. package/src/delegation/__tests__/statuslist-manager.test.ts +515 -0
  177. package/src/delegation/__tests__/utils.test.ts +185 -0
  178. package/src/delegation/__tests__/vc-issuer.test.ts +487 -0
  179. package/src/delegation/__tests__/vc-verifier.test.ts +1029 -0
  180. package/src/delegation/audience-validator.ts +24 -0
  181. package/src/delegation/bitstring.ts +160 -0
  182. package/src/delegation/cascading-revocation.ts +224 -0
  183. package/src/delegation/delegation-graph.ts +143 -0
  184. package/src/delegation/did-key-resolver.ts +181 -0
  185. package/src/delegation/did-web-resolver.ts +270 -0
  186. package/src/delegation/index.ts +33 -0
  187. package/src/delegation/outbound-headers.ts +193 -0
  188. package/src/delegation/outbound-proof.ts +90 -0
  189. package/src/delegation/statuslist-manager.ts +219 -0
  190. package/src/delegation/storage/__tests__/memory-graph-storage.test.ts +366 -0
  191. package/src/delegation/storage/__tests__/memory-statuslist-storage.test.ts +228 -0
  192. package/src/delegation/storage/memory-graph-storage.ts +178 -0
  193. package/src/delegation/storage/memory-statuslist-storage.ts +42 -0
  194. package/src/delegation/utils.ts +189 -0
  195. package/src/delegation/vc-issuer.ts +137 -0
  196. package/src/delegation/vc-verifier.ts +440 -0
  197. package/src/index.ts +264 -0
  198. package/src/logging/__tests__/logger.test.ts +366 -0
  199. package/src/logging/index.ts +6 -0
  200. package/src/logging/logger.ts +91 -0
  201. package/src/middleware/__tests__/with-mcpi.test.ts +504 -0
  202. package/src/middleware/index.ts +16 -0
  203. package/src/middleware/with-mcpi.ts +766 -0
  204. package/src/proof/__tests__/proof-generator.test.ts +483 -0
  205. package/src/proof/__tests__/verifier.test.ts +488 -0
  206. package/src/proof/errors.ts +75 -0
  207. package/src/proof/generator.ts +255 -0
  208. package/src/proof/index.ts +22 -0
  209. package/src/proof/verifier.ts +449 -0
  210. package/src/providers/base.ts +68 -0
  211. package/src/providers/index.ts +15 -0
  212. package/src/providers/memory.ts +130 -0
  213. package/src/session/__tests__/session-manager.test.ts +342 -0
  214. package/src/session/index.ts +7 -0
  215. package/src/session/manager.ts +332 -0
  216. package/src/types/protocol.ts +596 -0
  217. package/src/utils/__tests__/base58.test.ts +281 -0
  218. package/src/utils/__tests__/base64.test.ts +239 -0
  219. package/src/utils/__tests__/crypto-service.test.ts +530 -0
  220. package/src/utils/__tests__/did-helpers.test.ts +156 -0
  221. package/src/utils/base58.ts +115 -0
  222. package/src/utils/base64.ts +116 -0
  223. package/src/utils/crypto-service.ts +209 -0
  224. package/src/utils/did-helpers.ts +210 -0
  225. package/src/utils/ed25519-constants.ts +23 -0
  226. package/src/utils/index.ts +9 -0
@@ -0,0 +1,1029 @@
1
+ import { describe, it, expect, beforeEach, vi, Mock } from "vitest";
2
+ import {
3
+ DelegationCredentialVerifier,
4
+ createDelegationVerifier,
5
+ type DIDResolver,
6
+ type StatusListResolver,
7
+ type SignatureVerificationFunction,
8
+ type DelegationVCVerificationResult,
9
+ } from "../vc-verifier.js";
10
+ import type { DelegationCredential } from "../../types/protocol.js";
11
+
12
+ // Mock the protocol functions (preserving all other exports)
13
+ vi.mock("../../types/protocol.js", async (importOriginal) => {
14
+ const actual = await importOriginal<typeof import("../../types/protocol.js")>();
15
+ return {
16
+ ...actual,
17
+ isDelegationCredentialExpired: vi.fn(),
18
+ isDelegationCredentialNotYetValid: vi.fn(),
19
+ validateDelegationCredential: vi.fn(),
20
+ };
21
+ });
22
+
23
+ describe("DelegationCredentialVerifier", () => {
24
+ let mockDidResolver: {
25
+ resolve: ReturnType<typeof vi.fn>;
26
+ };
27
+ let mockStatusListResolver: {
28
+ checkStatus: ReturnType<typeof vi.fn>;
29
+ };
30
+ let mockSignatureVerifier: Mock<SignatureVerificationFunction>;
31
+
32
+ let verifier: DelegationCredentialVerifier;
33
+
34
+ // Helper function to setup default contracts mocks
35
+ const setupDefaultContractsMocks = async () => {
36
+ // Import the mocked functions
37
+ const protocol = await import("../../types/protocol.js");
38
+
39
+ // Access the mocked functions and set return values
40
+ const validateDelegationCredential = vi.mocked(
41
+ protocol.validateDelegationCredential
42
+ );
43
+ const isDelegationCredentialExpired = vi.mocked(
44
+ protocol.isDelegationCredentialExpired
45
+ );
46
+ const isDelegationCredentialNotYetValid = vi.mocked(
47
+ protocol.isDelegationCredentialNotYetValid
48
+ );
49
+
50
+ // Setup default return values
51
+ validateDelegationCredential.mockReturnValue({
52
+ success: true,
53
+ data: mockValidVC,
54
+ });
55
+ isDelegationCredentialExpired.mockReturnValue(false);
56
+ isDelegationCredentialNotYetValid.mockReturnValue(false);
57
+
58
+ return {
59
+ validateDelegationCredential,
60
+ isDelegationCredentialExpired,
61
+ isDelegationCredentialNotYetValid,
62
+ };
63
+ };
64
+
65
+ const mockValidVC: DelegationCredential = {
66
+ "@context": ["https://www.w3.org/2018/credentials/v1"],
67
+ id: "urn:delegation:123",
68
+ type: ["VerifiableCredential", "DelegationCredential"],
69
+ issuer: "did:web:example.com:issuer",
70
+ issuanceDate: "2024-01-01T00:00:00Z",
71
+ credentialSubject: {
72
+ id: "did:web:example.com:subject",
73
+ delegation: {
74
+ id: "urn:delegation:123",
75
+ issuerDid: "did:web:example.com:issuer",
76
+ subjectDid: "did:web:example.com:subject",
77
+ constraints: {
78
+ scopes: ["read:profile"],
79
+ },
80
+ status: "active",
81
+ },
82
+ },
83
+ proof: {
84
+ type: "Ed25519Signature2020",
85
+ created: "2024-01-01T00:00:00Z",
86
+ verificationMethod: "did:web:example.com:issuer#key-1",
87
+ proofPurpose: "assertionMethod",
88
+ proofValue: "mock-proof-value",
89
+ },
90
+ };
91
+
92
+ beforeEach(() => {
93
+ vi.clearAllMocks();
94
+
95
+ // Setup mocks as proper interface implementations with vitest mock types
96
+ mockDidResolver = {
97
+ resolve: vi.fn() as any,
98
+ };
99
+
100
+ mockStatusListResolver = {
101
+ checkStatus: vi.fn() as any,
102
+ };
103
+
104
+ mockSignatureVerifier = vi.fn();
105
+
106
+ verifier = new DelegationCredentialVerifier({
107
+ didResolver: mockDidResolver as DIDResolver,
108
+ statusListResolver: mockStatusListResolver as StatusListResolver,
109
+ signatureVerifier: mockSignatureVerifier,
110
+ cacheTtl: 1000, // Short TTL for testing
111
+ });
112
+ });
113
+
114
+ describe("constructor", () => {
115
+ it("should create verifier with default options", () => {
116
+ const defaultVerifier = new DelegationCredentialVerifier();
117
+ expect(defaultVerifier).toBeInstanceOf(DelegationCredentialVerifier);
118
+ });
119
+
120
+ it("should create verifier with custom options", () => {
121
+ const customVerifier = new DelegationCredentialVerifier({
122
+ cacheTtl: 5000,
123
+ });
124
+ expect(customVerifier).toBeInstanceOf(DelegationCredentialVerifier);
125
+ });
126
+ });
127
+
128
+ describe("createDelegationVerifier", () => {
129
+ it("should create a verifier instance", () => {
130
+ const verifier = createDelegationVerifier();
131
+ expect(verifier).toBeInstanceOf(DelegationCredentialVerifier);
132
+ });
133
+
134
+ it("should pass options to verifier", () => {
135
+ const options = { cacheTtl: 3000 };
136
+ const verifier = createDelegationVerifier(options);
137
+ expect(verifier).toBeInstanceOf(DelegationCredentialVerifier);
138
+ });
139
+ });
140
+
141
+ describe("verifyDelegationCredential - Basic Validation Stage", () => {
142
+ it("should reject expired credentials at basic validation stage", async () => {
143
+ const contractsMock = await setupDefaultContractsMocks();
144
+ contractsMock.isDelegationCredentialExpired.mockReturnValue(true);
145
+
146
+ const result = await verifier.verifyDelegationCredential(mockValidVC);
147
+
148
+ expect(result.valid).toBe(false);
149
+ expect(result.reason).toBe("Delegation credential expired");
150
+ expect(result.stage).toBe("basic");
151
+ expect(result.metrics?.totalMs).toBeGreaterThanOrEqual(0);
152
+ expect(result.checks?.basicValid).toBe(false);
153
+ });
154
+
155
+ it("should reject not-yet-valid credentials at basic validation stage", async () => {
156
+ const contractsMock = await setupDefaultContractsMocks();
157
+ contractsMock.isDelegationCredentialNotYetValid.mockReturnValue(true);
158
+
159
+ const result = await verifier.verifyDelegationCredential(mockValidVC);
160
+
161
+ expect(result.valid).toBe(false);
162
+ expect(result.reason).toBe("Delegation credential not yet valid");
163
+ expect(result.stage).toBe("basic");
164
+ });
165
+
166
+ it("should reject revoked credentials at basic validation stage", async () => {
167
+ await setupDefaultContractsMocks();
168
+
169
+ const revokedVC = {
170
+ ...mockValidVC,
171
+ credentialSubject: {
172
+ ...mockValidVC.credentialSubject,
173
+ delegation: {
174
+ ...mockValidVC.credentialSubject.delegation,
175
+ status: "revoked" as const,
176
+ },
177
+ },
178
+ };
179
+
180
+ const result = await verifier.verifyDelegationCredential(revokedVC);
181
+
182
+ expect(result.valid).toBe(false);
183
+ expect(result.reason).toBe("Delegation status is revoked");
184
+ expect(result.stage).toBe("basic");
185
+ });
186
+
187
+ it("should reject expired status credentials at basic validation stage", async () => {
188
+ await setupDefaultContractsMocks();
189
+ const expiredVC = {
190
+ ...mockValidVC,
191
+ credentialSubject: {
192
+ ...mockValidVC.credentialSubject,
193
+ delegation: {
194
+ ...mockValidVC.credentialSubject.delegation,
195
+ status: "expired" as const,
196
+ },
197
+ },
198
+ };
199
+
200
+ const result = await verifier.verifyDelegationCredential(expiredVC);
201
+
202
+ expect(result.valid).toBe(false);
203
+ expect(result.reason).toBe("Delegation status is expired");
204
+ expect(result.stage).toBe("basic");
205
+ });
206
+
207
+ it("should reject credentials without issuer DID", async () => {
208
+ await setupDefaultContractsMocks();
209
+ const invalidVC = {
210
+ ...mockValidVC,
211
+ credentialSubject: {
212
+ ...mockValidVC.credentialSubject,
213
+ delegation: {
214
+ ...mockValidVC.credentialSubject.delegation,
215
+ issuerDid: undefined,
216
+ },
217
+ },
218
+ } as any;
219
+
220
+ const result = await verifier.verifyDelegationCredential(invalidVC);
221
+
222
+ expect(result.valid).toBe(false);
223
+ expect(result.reason).toBe("Missing issuer or subject DID");
224
+ expect(result.stage).toBe("basic");
225
+ });
226
+
227
+ it("should reject credentials without subject DID", async () => {
228
+ await setupDefaultContractsMocks();
229
+ const invalidVC = {
230
+ ...mockValidVC,
231
+ credentialSubject: {
232
+ ...mockValidVC.credentialSubject,
233
+ delegation: {
234
+ ...mockValidVC.credentialSubject.delegation,
235
+ subjectDid: undefined,
236
+ },
237
+ },
238
+ } as any;
239
+
240
+ const result = await verifier.verifyDelegationCredential(invalidVC);
241
+
242
+ expect(result.valid).toBe(false);
243
+ expect(result.reason).toBe("Missing issuer or subject DID");
244
+ expect(result.stage).toBe("basic");
245
+ });
246
+
247
+ it("should reject credentials without proof", async () => {
248
+ await setupDefaultContractsMocks();
249
+ const invalidVC = { ...mockValidVC };
250
+ delete invalidVC.proof;
251
+
252
+ const result = await verifier.verifyDelegationCredential(invalidVC);
253
+
254
+ expect(result.valid).toBe(false);
255
+ expect(result.reason).toBe("Missing proof");
256
+ expect(result.stage).toBe("basic");
257
+ });
258
+
259
+ it("should reject invalid schema at basic validation stage", async () => {
260
+ const contractsMock = await setupDefaultContractsMocks();
261
+ // Create a mock ZodError-like object without importing zod
262
+ const mockZodError = {
263
+ issues: [{ code: "custom", path: [], message: "Invalid schema" }],
264
+ message: "Invalid schema",
265
+ };
266
+ contractsMock.validateDelegationCredential.mockReturnValue({
267
+ success: false,
268
+ error: mockZodError,
269
+ } as any);
270
+
271
+ const result = await verifier.verifyDelegationCredential(mockValidVC);
272
+
273
+ expect(result.valid).toBe(false);
274
+ expect(result.reason).toBe("Schema validation failed: Invalid schema");
275
+ expect(result.stage).toBe("basic");
276
+ });
277
+ });
278
+
279
+ describe("verifyDelegationCredential - Signature Verification", () => {
280
+ it("should skip signature verification when skipSignature is true", async () => {
281
+ await setupDefaultContractsMocks();
282
+ mockStatusListResolver.checkStatus.mockResolvedValue(false);
283
+ mockSignatureVerifier.mockResolvedValue({ valid: false }); // This should not be called
284
+
285
+ const result = await verifier.verifyDelegationCredential(mockValidVC, {
286
+ skipSignature: true,
287
+ });
288
+
289
+ expect(result.valid).toBe(true); // Only basic checks pass
290
+ expect(result.stage).toBe("complete");
291
+ expect(result.checks?.signatureValid).toBe(true); // Skipped = treated as valid
292
+ expect(mockSignatureVerifier).not.toHaveBeenCalled();
293
+ });
294
+
295
+ it("should fail signature verification when no resolver available", async () => {
296
+ await setupDefaultContractsMocks();
297
+ const verifierWithoutResolver = new DelegationCredentialVerifier({
298
+ statusListResolver: mockStatusListResolver,
299
+ // No signatureVerifier
300
+ });
301
+
302
+ mockStatusListResolver.checkStatus.mockResolvedValue(false);
303
+
304
+ const result =
305
+ await verifierWithoutResolver.verifyDelegationCredential(mockValidVC);
306
+
307
+ expect(result.valid).toBe(false);
308
+ expect(result.checks?.signatureValid).toBe(false);
309
+ expect(result.reason).toContain("No DID resolver or signature verifier configured");
310
+ });
311
+
312
+ it("should fail when DID resolution fails", async () => {
313
+ await setupDefaultContractsMocks();
314
+ mockDidResolver.resolve.mockResolvedValue(null);
315
+
316
+ const result = await verifier.verifyDelegationCredential(mockValidVC);
317
+
318
+ expect(result.valid).toBe(false);
319
+ expect(result.reason).toBe(
320
+ "Could not resolve issuer DID: did:web:example.com:issuer"
321
+ );
322
+ expect(result.stage).toBe("complete");
323
+ expect(result.checks?.signatureValid).toBe(false);
324
+ });
325
+
326
+ it("should fail when verification method not found", async () => {
327
+ await setupDefaultContractsMocks();
328
+ mockDidResolver.resolve.mockResolvedValue({
329
+ id: "did:web:example.com:issuer",
330
+ verificationMethod: [], // Empty array
331
+ });
332
+
333
+ const result = await verifier.verifyDelegationCredential(mockValidVC);
334
+
335
+ expect(result.valid).toBe(false);
336
+ expect(result.reason).toBe(
337
+ "Verification method did:web:example.com:issuer#key-1 not found"
338
+ );
339
+ expect(result.checks?.signatureValid).toBe(false);
340
+ });
341
+
342
+ it("should fail when verification method missing public key", async () => {
343
+ await setupDefaultContractsMocks();
344
+ mockDidResolver.resolve.mockResolvedValue({
345
+ id: "did:web:example.com:issuer",
346
+ verificationMethod: [
347
+ {
348
+ id: "did:web:example.com:issuer#key-1",
349
+ type: "Ed25519VerificationKey2020",
350
+ controller: "did:web:example.com:issuer",
351
+ // Missing publicKeyJwk
352
+ },
353
+ ],
354
+ });
355
+
356
+ const result = await verifier.verifyDelegationCredential(mockValidVC);
357
+
358
+ expect(result.valid).toBe(false);
359
+ expect(result.reason).toBe("Verification method missing publicKeyJwk");
360
+ expect(result.checks?.signatureValid).toBe(false);
361
+ });
362
+
363
+ it("should succeed when signature verification passes", async () => {
364
+ await setupDefaultContractsMocks();
365
+ mockDidResolver.resolve.mockResolvedValue({
366
+ id: "did:web:example.com:issuer",
367
+ verificationMethod: [
368
+ {
369
+ id: "did:web:example.com:issuer#key-1",
370
+ type: "Ed25519VerificationKey2020",
371
+ controller: "did:web:example.com:issuer",
372
+ publicKeyJwk: { kty: "OKP", crv: "Ed25519", x: "mock-key" },
373
+ },
374
+ ],
375
+ });
376
+
377
+ mockSignatureVerifier.mockResolvedValue({ valid: true });
378
+ mockStatusListResolver.checkStatus.mockResolvedValue(false);
379
+
380
+ const result = await verifier.verifyDelegationCredential(mockValidVC);
381
+
382
+ expect(result.valid).toBe(true);
383
+ expect(result.stage).toBe("complete");
384
+ expect(result.checks?.signatureValid).toBe(true);
385
+ expect(result.checks?.statusValid).toBe(true);
386
+ expect(result.checks?.basicValid).toBe(true);
387
+ });
388
+
389
+ it("should fail when signature verification fails", async () => {
390
+ await setupDefaultContractsMocks();
391
+ mockDidResolver.resolve.mockResolvedValue({
392
+ id: "did:web:example.com:issuer",
393
+ verificationMethod: [
394
+ {
395
+ id: "did:web:example.com:issuer#key-1",
396
+ type: "Ed25519VerificationKey2020",
397
+ controller: "did:web:example.com:issuer",
398
+ publicKeyJwk: { kty: "OKP", crv: "Ed25519", x: "mock-key" },
399
+ },
400
+ ],
401
+ });
402
+
403
+ mockSignatureVerifier.mockResolvedValue({
404
+ valid: false,
405
+ reason: "Invalid signature",
406
+ });
407
+ mockStatusListResolver.checkStatus.mockResolvedValue(false);
408
+
409
+ const result = await verifier.verifyDelegationCredential(mockValidVC);
410
+
411
+ expect(result.valid).toBe(false);
412
+ expect(result.reason).toBe("Invalid signature");
413
+ expect(result.checks?.signatureValid).toBe(false);
414
+ });
415
+ });
416
+
417
+ describe("verifyDelegationCredential - Status Checking", () => {
418
+ it("should skip status checking when skipStatus is true", async () => {
419
+ await setupDefaultContractsMocks();
420
+ const vcWithoutStatus = { ...mockValidVC };
421
+ delete vcWithoutStatus.credentialStatus;
422
+
423
+ mockDidResolver.resolve.mockResolvedValue({
424
+ id: "did:web:example.com:issuer",
425
+ verificationMethod: [
426
+ {
427
+ id: "did:web:example.com:issuer#key-1",
428
+ type: "Ed25519VerificationKey2020",
429
+ controller: "did:web:example.com:issuer",
430
+ publicKeyJwk: { kty: "OKP", crv: "Ed25519", x: "mock-key" },
431
+ },
432
+ ],
433
+ });
434
+ mockSignatureVerifier.mockResolvedValue({ valid: true });
435
+ mockStatusListResolver.checkStatus.mockResolvedValue(true); // Revoked - should not be called
436
+
437
+ const result = await verifier.verifyDelegationCredential(
438
+ vcWithoutStatus,
439
+ {
440
+ skipStatus: true,
441
+ }
442
+ );
443
+
444
+ expect(result.valid).toBe(true);
445
+ expect(result.checks?.statusValid).toBe(true); // Skipped = treated as valid
446
+ expect(mockStatusListResolver.checkStatus).not.toHaveBeenCalled();
447
+ });
448
+
449
+ it("should skip status checking when no status entry exists", async () => {
450
+ await setupDefaultContractsMocks();
451
+ const vcWithoutStatus = { ...mockValidVC };
452
+ delete vcWithoutStatus.credentialStatus;
453
+
454
+ mockDidResolver.resolve.mockResolvedValue({
455
+ id: "did:web:example.com:issuer",
456
+ verificationMethod: [
457
+ {
458
+ id: "did:web:example.com:issuer#key-1",
459
+ type: "Ed25519VerificationKey2020",
460
+ controller: "did:web:example.com:issuer",
461
+ publicKeyJwk: { kty: "OKP", crv: "Ed25519", x: "mock-key" },
462
+ },
463
+ ],
464
+ });
465
+ mockSignatureVerifier.mockResolvedValue({ valid: true });
466
+
467
+ const result = await verifier.verifyDelegationCredential(vcWithoutStatus);
468
+
469
+ expect(result.valid).toBe(true);
470
+ expect(result.checks?.statusValid).toBe(true);
471
+ expect(mockStatusListResolver.checkStatus).not.toHaveBeenCalled();
472
+ });
473
+
474
+ it("should skip status checking when no resolver available", async () => {
475
+ await setupDefaultContractsMocks();
476
+ const verifierWithoutResolver = new DelegationCredentialVerifier({
477
+ didResolver: mockDidResolver,
478
+ signatureVerifier: mockSignatureVerifier,
479
+ // No statusListResolver
480
+ });
481
+
482
+ const vcWithStatus = {
483
+ ...mockValidVC,
484
+ credentialStatus: {
485
+ id: "https://example.com/status#123",
486
+ type: "StatusList2021Entry" as const,
487
+ statusPurpose: "revocation" as const,
488
+ statusListIndex: "123",
489
+ statusListCredential: "https://example.com/status",
490
+ },
491
+ };
492
+
493
+ mockDidResolver.resolve.mockResolvedValue({
494
+ id: "did:web:example.com:issuer",
495
+ verificationMethod: [
496
+ {
497
+ id: "did:web:example.com:issuer#key-1",
498
+ type: "Ed25519VerificationKey2020",
499
+ controller: "did:web:example.com:issuer",
500
+ publicKeyJwk: { kty: "OKP", crv: "Ed25519", x: "mock-key" },
501
+ },
502
+ ],
503
+ });
504
+ mockSignatureVerifier.mockResolvedValue({ valid: true });
505
+
506
+ const result =
507
+ await verifierWithoutResolver.verifyDelegationCredential(vcWithStatus);
508
+
509
+ expect(result.valid).toBe(true);
510
+ expect(result.checks?.statusValid).toBe(true); // Trust but don't verify
511
+ });
512
+
513
+ it("should fail when credential is revoked", async () => {
514
+ await setupDefaultContractsMocks();
515
+ const vcWithStatus = {
516
+ ...mockValidVC,
517
+ credentialStatus: {
518
+ id: "https://example.com/status#123",
519
+ type: "StatusList2021Entry" as const,
520
+ statusPurpose: "revocation" as const,
521
+ statusListIndex: "123",
522
+ statusListCredential: "https://example.com/status",
523
+ },
524
+ };
525
+
526
+ mockDidResolver.resolve.mockResolvedValue({
527
+ id: "did:web:example.com:issuer",
528
+ verificationMethod: [
529
+ {
530
+ id: "did:web:example.com:issuer#key-1",
531
+ type: "Ed25519VerificationKey2020",
532
+ controller: "did:web:example.com:issuer",
533
+ publicKeyJwk: { kty: "OKP", crv: "Ed25519", x: "mock-key" },
534
+ },
535
+ ],
536
+ });
537
+ mockSignatureVerifier.mockResolvedValue({ valid: true });
538
+ mockStatusListResolver.checkStatus.mockResolvedValue(true); // Revoked
539
+
540
+ const result = await verifier.verifyDelegationCredential(vcWithStatus);
541
+
542
+ expect(result.valid).toBe(false);
543
+ expect(result.reason).toBe(
544
+ "Credential revoked via StatusList2021 (revocation)"
545
+ );
546
+ expect(result.checks?.statusValid).toBe(false);
547
+ });
548
+
549
+ it("should succeed when credential is not revoked", async () => {
550
+ await setupDefaultContractsMocks();
551
+ const vcWithStatus = {
552
+ ...mockValidVC,
553
+ credentialStatus: {
554
+ id: "https://example.com/status#123",
555
+ type: "StatusList2021Entry" as const,
556
+ statusPurpose: "revocation" as const,
557
+ statusListIndex: "123",
558
+ statusListCredential: "https://example.com/status",
559
+ },
560
+ };
561
+
562
+ mockDidResolver.resolve.mockResolvedValue({
563
+ id: "did:web:example.com:issuer",
564
+ verificationMethod: [
565
+ {
566
+ id: "did:web:example.com:issuer#key-1",
567
+ type: "Ed25519VerificationKey2020",
568
+ controller: "did:web:example.com:issuer",
569
+ publicKeyJwk: { kty: "OKP", crv: "Ed25519", x: "mock-key" },
570
+ },
571
+ ],
572
+ });
573
+ mockSignatureVerifier.mockResolvedValue({ valid: true });
574
+ mockStatusListResolver.checkStatus.mockResolvedValue(false); // Not revoked
575
+
576
+ const result = await verifier.verifyDelegationCredential(vcWithStatus);
577
+
578
+ expect(result.valid).toBe(true);
579
+ expect(result.checks?.statusValid).toBe(true);
580
+ });
581
+ });
582
+
583
+ describe("verifyDelegationCredential - Caching", () => {
584
+ it("should return cached result when available", async () => {
585
+ await setupDefaultContractsMocks();
586
+ // First call - should verify and cache
587
+ mockDidResolver.resolve.mockResolvedValue({
588
+ id: "did:web:example.com:issuer",
589
+ verificationMethod: [
590
+ {
591
+ id: "did:web:example.com:issuer#key-1",
592
+ type: "Ed25519VerificationKey2020",
593
+ controller: "did:web:example.com:issuer",
594
+ publicKeyJwk: { kty: "OKP", crv: "Ed25519", x: "mock-key" },
595
+ },
596
+ ],
597
+ });
598
+ mockSignatureVerifier.mockResolvedValue({ valid: true });
599
+ mockStatusListResolver.checkStatus.mockResolvedValue(false);
600
+
601
+ const result1 = await verifier.verifyDelegationCredential(mockValidVC);
602
+ expect(result1.valid).toBe(true);
603
+ expect(result1.cached).toBeUndefined();
604
+
605
+ // Second call - should return cached result
606
+ mockSignatureVerifier.mockClear();
607
+ mockStatusListResolver.checkStatus.mockClear();
608
+
609
+ const result2 = await verifier.verifyDelegationCredential(mockValidVC);
610
+ expect(result2.valid).toBe(true);
611
+ expect(result2.cached).toBe(true);
612
+
613
+ // Verifiers should not be called again
614
+ expect(mockSignatureVerifier).not.toHaveBeenCalled();
615
+ expect(mockStatusListResolver.checkStatus).not.toHaveBeenCalled();
616
+ });
617
+
618
+ it("should skip cache when skipCache is true", async () => {
619
+ await setupDefaultContractsMocks();
620
+ // First call
621
+ mockDidResolver.resolve.mockResolvedValue({
622
+ id: "did:web:example.com:issuer",
623
+ verificationMethod: [
624
+ {
625
+ id: "did:web:example.com:issuer#key-1",
626
+ type: "Ed25519VerificationKey2020",
627
+ controller: "did:web:example.com:issuer",
628
+ publicKeyJwk: { kty: "OKP", crv: "Ed25519", x: "mock-key" },
629
+ },
630
+ ],
631
+ });
632
+ mockSignatureVerifier.mockResolvedValue({ valid: true });
633
+ mockStatusListResolver.checkStatus.mockResolvedValue(false);
634
+
635
+ await verifier.verifyDelegationCredential(mockValidVC);
636
+
637
+ // Second call with skipCache
638
+ mockDidResolver.resolve.mockResolvedValue({
639
+ id: "did:web:example.com:issuer",
640
+ verificationMethod: [
641
+ {
642
+ id: "did:web:example.com:issuer#key-1",
643
+ type: "Ed25519VerificationKey2020",
644
+ controller: "did:web:example.com:issuer",
645
+ publicKeyJwk: { kty: "OKP", crv: "Ed25519", x: "mock-key" },
646
+ },
647
+ ],
648
+ });
649
+ mockSignatureVerifier.mockResolvedValue({ valid: false }); // Different result
650
+ mockStatusListResolver.checkStatus.mockResolvedValue(false);
651
+
652
+ const result = await verifier.verifyDelegationCredential(mockValidVC, {
653
+ skipCache: true,
654
+ });
655
+
656
+ expect(result.valid).toBe(false); // Should get new result, not cached
657
+ expect(result.cached).toBeUndefined();
658
+ });
659
+
660
+ it("should not cache failed verifications", async () => {
661
+ // First call - fails
662
+ const contractsMock = await setupDefaultContractsMocks();
663
+ contractsMock.isDelegationCredentialExpired.mockReturnValue(true);
664
+
665
+ const result1 = await verifier.verifyDelegationCredential(mockValidVC);
666
+ expect(result1.valid).toBe(false);
667
+
668
+ // Reset for second call
669
+ contractsMock.isDelegationCredentialExpired.mockReturnValue(false);
670
+ mockDidResolver.resolve.mockResolvedValue({
671
+ id: "did:web:example.com:issuer",
672
+ verificationMethod: [
673
+ {
674
+ id: "did:web:example.com:issuer#key-1",
675
+ type: "Ed25519VerificationKey2020",
676
+ controller: "did:web:example.com:issuer",
677
+ publicKeyJwk: { kty: "OKP", crv: "Ed25519", x: "mock-key" },
678
+ },
679
+ ],
680
+ });
681
+ mockSignatureVerifier.mockResolvedValue({ valid: true });
682
+ mockStatusListResolver.checkStatus.mockResolvedValue(false);
683
+
684
+ // Second call - should verify again (not cached)
685
+ const result2 = await verifier.verifyDelegationCredential(mockValidVC);
686
+ expect(result2.valid).toBe(true);
687
+ expect(result2.cached).toBeUndefined(); // Should not be cached
688
+ });
689
+ });
690
+
691
+ describe("issuer handling", () => {
692
+ it("should handle issuer as object with id property", async () => {
693
+ await setupDefaultContractsMocks();
694
+ const vcWithObjectIssuer = {
695
+ ...mockValidVC,
696
+ issuer: {
697
+ id: "did:web:example.com:issuer",
698
+ name: "Example Issuer"
699
+ },
700
+ };
701
+
702
+ mockDidResolver.resolve.mockResolvedValue({
703
+ id: "did:web:example.com:issuer",
704
+ verificationMethod: [
705
+ {
706
+ id: "did:web:example.com:issuer#key-1",
707
+ type: "Ed25519VerificationKey2020",
708
+ controller: "did:web:example.com:issuer",
709
+ publicKeyJwk: { kty: "OKP", crv: "Ed25519", x: "mock-key" },
710
+ },
711
+ ],
712
+ });
713
+ mockSignatureVerifier.mockResolvedValue({ valid: true });
714
+ mockStatusListResolver.checkStatus.mockResolvedValue(false);
715
+
716
+ const result = await verifier.verifyDelegationCredential(vcWithObjectIssuer);
717
+
718
+ expect(result.valid).toBe(true);
719
+ expect(mockDidResolver.resolve).toHaveBeenCalledWith("did:web:example.com:issuer");
720
+ });
721
+
722
+ it("should handle missing verificationMethod in proof", async () => {
723
+ await setupDefaultContractsMocks();
724
+ const vcWithoutVerificationMethod = {
725
+ ...mockValidVC,
726
+ proof: {
727
+ type: "Ed25519Signature2020",
728
+ created: "2024-01-01T00:00:00Z",
729
+ // Missing verificationMethod
730
+ proofPurpose: "assertionMethod",
731
+ proofValue: "mock-proof-value",
732
+ },
733
+ } as any;
734
+
735
+ mockDidResolver.resolve.mockResolvedValue({
736
+ id: "did:web:example.com:issuer",
737
+ verificationMethod: [],
738
+ });
739
+
740
+ const result = await verifier.verifyDelegationCredential(vcWithoutVerificationMethod);
741
+
742
+ expect(result.valid).toBe(false);
743
+ expect(result.reason).toBe("Proof missing verificationMethod");
744
+ });
745
+ });
746
+
747
+ describe("cache expiry", () => {
748
+ it("should expire cached entries after TTL", async () => {
749
+ await setupDefaultContractsMocks();
750
+ // Create verifier with very short TTL (10ms)
751
+ const shortTtlVerifier = new DelegationCredentialVerifier({
752
+ didResolver: mockDidResolver as DIDResolver,
753
+ statusListResolver: mockStatusListResolver as StatusListResolver,
754
+ signatureVerifier: mockSignatureVerifier,
755
+ cacheTtl: 10,
756
+ });
757
+
758
+ mockDidResolver.resolve.mockResolvedValue({
759
+ id: "did:web:example.com:issuer",
760
+ verificationMethod: [
761
+ {
762
+ id: "did:web:example.com:issuer#key-1",
763
+ type: "Ed25519VerificationKey2020",
764
+ controller: "did:web:example.com:issuer",
765
+ publicKeyJwk: { kty: "OKP", crv: "Ed25519", x: "mock-key" },
766
+ },
767
+ ],
768
+ });
769
+ mockSignatureVerifier.mockResolvedValue({ valid: true });
770
+ mockStatusListResolver.checkStatus.mockResolvedValue(false);
771
+
772
+ // First verification
773
+ const result1 = await shortTtlVerifier.verifyDelegationCredential(mockValidVC);
774
+ expect(result1.valid).toBe(true);
775
+ expect(result1.cached).toBeUndefined();
776
+
777
+ // Wait for cache to expire
778
+ await new Promise((resolve) => setTimeout(resolve, 15));
779
+
780
+ // Clear mock call history
781
+ mockSignatureVerifier.mockClear();
782
+ mockStatusListResolver.checkStatus.mockClear();
783
+
784
+ // Second verification - cache should have expired
785
+ const result2 = await shortTtlVerifier.verifyDelegationCredential(mockValidVC);
786
+ expect(result2.cached).toBeUndefined(); // Should not be from cache
787
+
788
+ // Verifiers should be called again since cache expired
789
+ expect(mockSignatureVerifier).toHaveBeenCalled();
790
+ });
791
+ });
792
+
793
+ describe("cache management", () => {
794
+ it("should clear all cache entries", async () => {
795
+ await setupDefaultContractsMocks();
796
+ // Add to cache
797
+ mockDidResolver.resolve.mockResolvedValue({
798
+ id: "did:web:example.com:issuer",
799
+ verificationMethod: [
800
+ {
801
+ id: "did:web:example.com:issuer#key-1",
802
+ type: "Ed25519VerificationKey2020",
803
+ controller: "did:web:example.com:issuer",
804
+ publicKeyJwk: { kty: "OKP", crv: "Ed25519", x: "mock-key" },
805
+ },
806
+ ],
807
+ });
808
+ mockSignatureVerifier.mockResolvedValue({ valid: true });
809
+ mockStatusListResolver.checkStatus.mockResolvedValue(false);
810
+
811
+ await verifier.verifyDelegationCredential(mockValidVC);
812
+
813
+ // Clear cache
814
+ verifier.clearCache();
815
+
816
+ // Next call should verify again
817
+ mockDidResolver.resolve.mockResolvedValue({
818
+ id: "did:web:example.com:issuer",
819
+ verificationMethod: [
820
+ {
821
+ id: "did:web:example.com:issuer#key-1",
822
+ type: "Ed25519VerificationKey2020",
823
+ controller: "did:web:example.com:issuer",
824
+ publicKeyJwk: { kty: "OKP", crv: "Ed25519", x: "mock-key" },
825
+ },
826
+ ],
827
+ });
828
+ mockSignatureVerifier.mockResolvedValue({ valid: false }); // Different result
829
+
830
+ const result = await verifier.verifyDelegationCredential(mockValidVC);
831
+ expect(result.valid).toBe(false);
832
+ expect(result.cached).toBeUndefined();
833
+ });
834
+
835
+ it("should clear specific cache entry", async () => {
836
+ await setupDefaultContractsMocks();
837
+ // Add to cache
838
+ mockDidResolver.resolve.mockResolvedValue({
839
+ id: "did:web:example.com:issuer",
840
+ verificationMethod: [
841
+ {
842
+ id: "did:web:example.com:issuer#key-1",
843
+ type: "Ed25519VerificationKey2020",
844
+ controller: "did:web:example.com:issuer",
845
+ publicKeyJwk: { kty: "OKP", crv: "Ed25519", x: "mock-key" },
846
+ },
847
+ ],
848
+ });
849
+ mockSignatureVerifier.mockResolvedValue({ valid: true });
850
+ mockStatusListResolver.checkStatus.mockResolvedValue(false);
851
+
852
+ await verifier.verifyDelegationCredential(mockValidVC);
853
+
854
+ // Clear specific entry
855
+ verifier.clearCacheEntry("urn:delegation:123");
856
+
857
+ // Next call should verify again
858
+ mockDidResolver.resolve.mockResolvedValue({
859
+ id: "did:web:example.com:issuer",
860
+ verificationMethod: [
861
+ {
862
+ id: "did:web:example.com:issuer#key-1",
863
+ type: "Ed25519VerificationKey2020",
864
+ controller: "did:web:example.com:issuer",
865
+ publicKeyJwk: { kty: "OKP", crv: "Ed25519", x: "mock-key" },
866
+ },
867
+ ],
868
+ });
869
+ mockSignatureVerifier.mockResolvedValue({ valid: false });
870
+
871
+ const result = await verifier.verifyDelegationCredential(mockValidVC);
872
+ expect(result.valid).toBe(false);
873
+ expect(result.cached).toBeUndefined();
874
+ });
875
+ });
876
+
877
+ describe("performance metrics", () => {
878
+ it("should include timing metrics in results", async () => {
879
+ await setupDefaultContractsMocks();
880
+ mockDidResolver.resolve.mockResolvedValue({
881
+ id: "did:web:example.com:issuer",
882
+ verificationMethod: [
883
+ {
884
+ id: "did:web:example.com:issuer#key-1",
885
+ type: "Ed25519VerificationKey2020",
886
+ controller: "did:web:example.com:issuer",
887
+ publicKeyJwk: { kty: "OKP", crv: "Ed25519", x: "mock-key" },
888
+ },
889
+ ],
890
+ });
891
+ mockSignatureVerifier.mockResolvedValue({ valid: true });
892
+ mockStatusListResolver.checkStatus.mockResolvedValue(false);
893
+
894
+ const result = await verifier.verifyDelegationCredential(mockValidVC);
895
+
896
+ expect(result.metrics).toBeDefined();
897
+ expect(result.metrics?.totalMs).toBeGreaterThanOrEqual(0);
898
+ expect(result.metrics?.basicCheckMs).toBeGreaterThanOrEqual(0);
899
+ expect(result.metrics?.signatureCheckMs).toBeGreaterThanOrEqual(0);
900
+ expect(result.metrics?.statusCheckMs).toBeGreaterThanOrEqual(0);
901
+ });
902
+
903
+ it("should report zero timing for skipped checks", async () => {
904
+ await setupDefaultContractsMocks();
905
+ const result = await verifier.verifyDelegationCredential(mockValidVC, {
906
+ skipSignature: true,
907
+ skipStatus: true,
908
+ });
909
+
910
+ expect(result.metrics?.signatureCheckMs).toBe(0);
911
+ expect(result.metrics?.statusCheckMs).toBe(0);
912
+ });
913
+ });
914
+
915
+ describe("error handling", () => {
916
+ it("should handle DID resolver errors gracefully", async () => {
917
+ await setupDefaultContractsMocks();
918
+ mockDidResolver.resolve.mockRejectedValue(new Error("Network timeout"));
919
+
920
+ const result = await verifier.verifyDelegationCredential(mockValidVC);
921
+
922
+ expect(result.valid).toBe(false);
923
+ expect(result.reason).toContain(
924
+ "Signature verification error: Network timeout"
925
+ );
926
+ });
927
+
928
+ it("should handle status list resolver errors gracefully", async () => {
929
+ await setupDefaultContractsMocks();
930
+ const vcWithStatus = {
931
+ ...mockValidVC,
932
+ credentialStatus: {
933
+ id: "https://example.com/status#123",
934
+ type: "StatusList2021Entry" as const,
935
+ statusPurpose: "revocation" as const,
936
+ statusListIndex: "123",
937
+ statusListCredential: "https://example.com/status",
938
+ },
939
+ };
940
+
941
+ mockDidResolver.resolve.mockResolvedValue({
942
+ id: "did:web:example.com:issuer",
943
+ verificationMethod: [
944
+ {
945
+ id: "did:web:example.com:issuer#key-1",
946
+ type: "Ed25519VerificationKey2020",
947
+ controller: "did:web:example.com:issuer",
948
+ publicKeyJwk: { kty: "OKP", crv: "Ed25519", x: "mock-key" },
949
+ },
950
+ ],
951
+ });
952
+ mockSignatureVerifier.mockResolvedValue({ valid: true });
953
+ mockStatusListResolver.checkStatus.mockRejectedValue(
954
+ new Error("Status list unavailable")
955
+ );
956
+
957
+ const result = await verifier.verifyDelegationCredential(vcWithStatus);
958
+
959
+ expect(result.valid).toBe(false);
960
+ expect(result.reason).toContain(
961
+ "Status check error: Status list unavailable"
962
+ );
963
+ });
964
+
965
+ it("should handle signature verifier errors gracefully", async () => {
966
+ await setupDefaultContractsMocks();
967
+ mockDidResolver.resolve.mockResolvedValue({
968
+ id: "did:web:example.com:issuer",
969
+ verificationMethod: [
970
+ {
971
+ id: "did:web:example.com:issuer#key-1",
972
+ type: "Ed25519VerificationKey2020",
973
+ controller: "did:web:example.com:issuer",
974
+ publicKeyJwk: { kty: "OKP", crv: "Ed25519", x: "mock-key" },
975
+ },
976
+ ],
977
+ });
978
+
979
+ mockSignatureVerifier.mockRejectedValue(
980
+ new Error("Signature algorithm unsupported")
981
+ );
982
+ mockStatusListResolver.checkStatus.mockResolvedValue(false);
983
+
984
+ const result = await verifier.verifyDelegationCredential(mockValidVC);
985
+
986
+ expect(result.valid).toBe(false);
987
+ expect(result.reason).toContain(
988
+ "Signature verification error: Signature algorithm unsupported"
989
+ );
990
+ });
991
+ });
992
+
993
+ describe("findVerificationMethod", () => {
994
+ // This is a private method, but we can test it indirectly through the main verification flow
995
+ it("should find verification method by ID", async () => {
996
+ await setupDefaultContractsMocks();
997
+ const didDoc = {
998
+ id: "did:web:example.com:issuer",
999
+ verificationMethod: [
1000
+ {
1001
+ id: "did:web:example.com:issuer#key-1",
1002
+ type: "Ed25519VerificationKey2020",
1003
+ controller: "did:web:example.com:issuer",
1004
+ publicKeyJwk: { kty: "OKP", crv: "Ed25519", x: "mock-key" },
1005
+ },
1006
+ {
1007
+ id: "did:web:example.com:issuer#key-2",
1008
+ type: "Ed25519VerificationKey2020",
1009
+ controller: "did:web:example.com:issuer",
1010
+ publicKeyJwk: { kty: "OKP", crv: "Ed25519", x: "other-key" },
1011
+ },
1012
+ ],
1013
+ };
1014
+
1015
+ mockDidResolver.resolve.mockResolvedValue(didDoc);
1016
+ mockSignatureVerifier.mockResolvedValue({ valid: true });
1017
+ mockStatusListResolver.checkStatus.mockResolvedValue(false);
1018
+
1019
+ const result = await verifier.verifyDelegationCredential(mockValidVC);
1020
+
1021
+ expect(result.valid).toBe(true);
1022
+ expect(mockSignatureVerifier).toHaveBeenCalledWith(mockValidVC, {
1023
+ kty: "OKP",
1024
+ crv: "Ed25519",
1025
+ x: "mock-key",
1026
+ });
1027
+ });
1028
+ });
1029
+ });