@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,63 +0,0 @@
1
- /**
2
- * OAuth Required Error
3
- *
4
- * Error thrown when a tool requires an IDP token (OAuth) but none is available.
5
- * This triggers the OAuth flow to obtain the necessary token.
6
- *
7
- * @package @kya-os/mcp-i-core
8
- */
9
-
10
- export interface OAuthRequiredErrorOptions {
11
- /** Name of the tool requiring OAuth */
12
- toolName: string;
13
-
14
- /** Required OAuth scopes */
15
- requiredScopes: string[];
16
-
17
- /** OAuth provider name (e.g., "github", "google") */
18
- provider: string;
19
-
20
- /** OAuth authorization URL */
21
- oauthUrl: string;
22
-
23
- /** Optional resume token for continuing after OAuth */
24
- resumeToken?: string;
25
-
26
- /** Optional user DID */
27
- userDid?: string;
28
-
29
- /** Optional session ID */
30
- sessionId?: string;
31
- }
32
-
33
- /**
34
- * Error thrown when a tool requires OAuth but IDP token is not available
35
- *
36
- * This error should trigger the OAuth flow to obtain the necessary IDP token.
37
- */
38
- export class OAuthRequiredError extends Error {
39
- public readonly toolName: string;
40
- public readonly requiredScopes: string[];
41
- public readonly provider: string;
42
- public readonly oauthUrl: string;
43
- public readonly resumeToken?: string;
44
- public readonly userDid?: string;
45
- public readonly sessionId?: string;
46
-
47
- constructor(options: OAuthRequiredErrorOptions) {
48
- const { toolName, requiredScopes, provider, oauthUrl } = options;
49
- super(
50
- `OAuth required for tool "${toolName}" with provider "${provider}". ` +
51
- `Required scopes: ${requiredScopes.join(", ")}`
52
- );
53
- this.name = "OAuthRequiredError";
54
- this.toolName = toolName;
55
- this.requiredScopes = requiredScopes;
56
- this.provider = provider;
57
- this.oauthUrl = oauthUrl;
58
- this.resumeToken = options.resumeToken;
59
- this.userDid = options.userDid;
60
- this.sessionId = options.sessionId;
61
- }
62
- }
63
-
@@ -1,155 +0,0 @@
1
- /**
2
- * Tool Protection Types
3
- *
4
- * Implementation-specific type definitions for tool protection services.
5
- * Core spec types are imported from @kya-os/contracts/tool-protection.
6
- *
7
- * @package @kya-os/mcp-i-core
8
- * @version 1.0.0
9
- */
10
-
11
- // Import spec types from contracts
12
- import type {
13
- ToolProtection,
14
- ToolProtectionMap,
15
- } from "@kya-os/contracts/tool-protection";
16
-
17
- // Re-export for backward compatibility
18
- export type { ToolProtection };
19
-
20
- /**
21
- * Complete tool protection configuration for a project
22
- * @deprecated Use ToolProtectionMap from @kya-os/contracts/tool-protection
23
- */
24
- export interface ToolProtectionConfig {
25
- /**
26
- * Map of tool names to their protection settings
27
- */
28
- toolProtections: ToolProtectionMap;
29
- }
30
-
31
- /**
32
- * Configuration for the ToolProtectionService
33
- */
34
- export interface ToolProtectionServiceConfig {
35
- /**
36
- * AgentShield API base URL
37
- * @example "https://kya.vouched.id"
38
- */
39
- apiUrl: string;
40
-
41
- /**
42
- * AgentShield API key for authentication
43
- * @example "sk_..."
44
- */
45
- apiKey: string;
46
-
47
- /**
48
- * Project ID (optional, used for backward compatibility)
49
- * The service now fetches config by agent DID instead
50
- * @deprecated Use agent DID from runtime identity
51
- * @example "mcp-i-ieu0ro"
52
- */
53
- projectId?: string;
54
-
55
- /**
56
- * Cache TTL in milliseconds
57
- * @default 300000 (5 minutes)
58
- */
59
- cacheTtl?: number;
60
-
61
- /**
62
- * Fallback configuration to use if API is unavailable
63
- * @optional
64
- */
65
- fallbackConfig?: ToolProtectionConfig;
66
-
67
- /**
68
- * Fail-safe behavior when API is unavailable and no fallback is provided
69
- * - 'deny-all': Block all tools by default (secure, recommended for production)
70
- * - 'allow-all': Allow all tools (insecure, not recommended for production)
71
- * @default 'deny-all'
72
- */
73
- failSafeBehavior?: 'deny-all' | 'allow-all';
74
-
75
- /**
76
- * Maximum age (in milliseconds) for stale cache to be used during outages
77
- * Stale cache is used when API fails and no fallback is provided
78
- * @default 86400000 (24 hours)
79
- */
80
- maxStaleCacheAge?: number;
81
-
82
- /**
83
- * Allow using stale cache during outages
84
- * @default true
85
- */
86
- allowStaleCache?: boolean;
87
-
88
- /**
89
- * Enable debug logging
90
- * @default false
91
- */
92
- debug?: boolean;
93
- }
94
-
95
- /**
96
- * API response from /api/v1/bouncer/projects/{projectId}/tool-protections
97
- */
98
- export interface ToolProtectionApiResponse {
99
- success: boolean;
100
- data: {
101
- toolProtections: Record<string, ToolProtection>;
102
- };
103
- metadata: {
104
- timestamp: string;
105
- cachedUntil: string;
106
- };
107
- }
108
-
109
- /**
110
- * Error thrown when a tool requires delegation but none is provided
111
- *
112
- * Note: Resume token generation is handled by MCPIRuntimeBase.generateResumeToken().
113
- * The runtime should generate the token and pass it to this constructor.
114
- */
115
- export class DelegationRequiredError extends Error {
116
- public resumeToken?: string;
117
- public interceptedCall?: InterceptedToolCall;
118
-
119
- constructor(
120
- public toolName: string,
121
- public requiredScopes: string[],
122
- public consentUrl?: string,
123
- interceptedCall?: InterceptedToolCall,
124
- resumeToken?: string
125
- ) {
126
- super(`Delegation required for tool "${toolName}"`);
127
- this.name = "DelegationRequiredError";
128
- this.interceptedCall = interceptedCall;
129
- this.resumeToken = resumeToken;
130
- }
131
- }
132
-
133
- /**
134
- * Context for an intercepted tool call that needs authorization
135
- */
136
- export interface InterceptedToolCall {
137
- toolName: string;
138
- args: any;
139
- sessionId: string;
140
- timestamp: number;
141
- expiresAt: number;
142
- }
143
-
144
- /**
145
- * Error thrown when the tool protection service fails
146
- */
147
- export class ToolProtectionServiceError extends Error {
148
- constructor(
149
- message: string,
150
- public cause?: Error
151
- ) {
152
- super(message);
153
- this.name = "ToolProtectionServiceError";
154
- }
155
- }
@@ -1,156 +0,0 @@
1
- /**
2
- * Tests for DID Helper Utilities
3
- *
4
- * @package @kya-os/mcp-i-core/utils/__tests__
5
- */
6
-
7
- import { describe, it, expect } from "vitest";
8
- import {
9
- isValidDid,
10
- getDidMethod,
11
- normalizeDid,
12
- compareDids,
13
- getServerDid,
14
- generateDidKeyFromBytes,
15
- generateDidKeyFromBase64,
16
- } from "../did-helpers";
17
-
18
- describe("DID Helpers", () => {
19
- describe("isValidDid", () => {
20
- it("should return true for valid DIDs", () => {
21
- expect(isValidDid("did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK")).toBe(true);
22
- expect(isValidDid("did:web:example.com")).toBe(true);
23
- expect(isValidDid("did:web:example.com:path")).toBe(true);
24
- });
25
-
26
- it("should return false for invalid DIDs", () => {
27
- expect(isValidDid("not-a-did")).toBe(false);
28
- expect(isValidDid("")).toBe(false);
29
- expect(isValidDid("did")).toBe(false);
30
- expect(isValidDid("key:z6Mk...")).toBe(false);
31
- });
32
- });
33
-
34
- describe("getDidMethod", () => {
35
- it("should extract DID method correctly", () => {
36
- expect(getDidMethod("did:key:z6Mk...")).toBe("key");
37
- expect(getDidMethod("did:web:example.com")).toBe("web");
38
- expect(getDidMethod("did:ion:...")).toBe("ion");
39
- });
40
-
41
- it("should return null for invalid DIDs", () => {
42
- expect(getDidMethod("not-a-did")).toBeNull();
43
- expect(getDidMethod("")).toBeNull();
44
- expect(getDidMethod("did")).toBeNull();
45
- });
46
- });
47
-
48
- describe("normalizeDid", () => {
49
- it("should trim whitespace", () => {
50
- expect(normalizeDid(" did:key:z6Mk... ")).toBe("did:key:z6Mk...");
51
- expect(normalizeDid("did:key:z6Mk...")).toBe("did:key:z6Mk...");
52
- });
53
- });
54
-
55
- describe("compareDids", () => {
56
- it("should compare DIDs correctly", () => {
57
- expect(compareDids("did:key:z6Mk...", "did:key:z6Mk...")).toBe(true);
58
- expect(compareDids("did:key:z6Mk...", "did:web:example.com")).toBe(false);
59
- });
60
-
61
- it("should normalize before comparing", () => {
62
- expect(compareDids(" did:key:z6Mk... ", "did:key:z6Mk...")).toBe(true);
63
- });
64
- });
65
-
66
- describe("getServerDid", () => {
67
- it("should return serverDid when present", () => {
68
- const config = {
69
- identity: {
70
- serverDid: "did:web:server.com",
71
- },
72
- };
73
- expect(getServerDid(config)).toBe("did:web:server.com");
74
- });
75
-
76
- it("should return agentDid when serverDid not present (backward compatibility)", () => {
77
- const config = {
78
- identity: {
79
- agentDid: "did:web:old-server.com",
80
- },
81
- };
82
- expect(getServerDid(config)).toBe("did:web:old-server.com");
83
- });
84
-
85
- it("should prefer serverDid over agentDid", () => {
86
- const config = {
87
- identity: {
88
- serverDid: "did:web:new-server.com",
89
- agentDid: "did:web:old-server.com",
90
- },
91
- };
92
- expect(getServerDid(config)).toBe("did:web:new-server.com");
93
- });
94
-
95
- it("should throw error when neither serverDid nor agentDid present", () => {
96
- const config = {
97
- identity: {},
98
- };
99
- expect(() => getServerDid(config)).toThrow("Server DID not configured");
100
- });
101
- });
102
-
103
- describe("generateDidKeyFromBytes", () => {
104
- it("should generate valid did:key from 32-byte Ed25519 public key", () => {
105
- // Use a known test key (32 bytes)
106
- const publicKeyBytes = new Uint8Array(32).fill(0xab);
107
- const did = generateDidKeyFromBytes(publicKeyBytes);
108
-
109
- expect(did).toMatch(/^did:key:z6Mk/);
110
- expect(isValidDid(did)).toBe(true);
111
- expect(getDidMethod(did)).toBe("key");
112
- });
113
-
114
- it("should generate consistent did:key for same input", () => {
115
- const publicKeyBytes = new Uint8Array(32).fill(0x42);
116
- const did1 = generateDidKeyFromBytes(publicKeyBytes);
117
- const did2 = generateDidKeyFromBytes(publicKeyBytes);
118
-
119
- expect(did1).toBe(did2);
120
- });
121
-
122
- it("should generate different did:key for different inputs", () => {
123
- const key1 = new Uint8Array(32).fill(0x11);
124
- const key2 = new Uint8Array(32).fill(0x22);
125
-
126
- const did1 = generateDidKeyFromBytes(key1);
127
- const did2 = generateDidKeyFromBytes(key2);
128
-
129
- expect(did1).not.toBe(did2);
130
- });
131
- });
132
-
133
- describe("generateDidKeyFromBase64", () => {
134
- it("should generate valid did:key from base64-encoded public key", () => {
135
- // Base64 encode a 32-byte key
136
- const publicKeyBytes = new Uint8Array(32).fill(0xcd);
137
- const publicKeyBase64 = btoa(String.fromCharCode(...publicKeyBytes));
138
-
139
- const did = generateDidKeyFromBase64(publicKeyBase64);
140
-
141
- expect(did).toMatch(/^did:key:z6Mk/);
142
- expect(isValidDid(did)).toBe(true);
143
- });
144
-
145
- it("should produce same result as generateDidKeyFromBytes", () => {
146
- const publicKeyBytes = new Uint8Array(32).fill(0xef);
147
- const publicKeyBase64 = btoa(String.fromCharCode(...publicKeyBytes));
148
-
149
- const didFromBytes = generateDidKeyFromBytes(publicKeyBytes);
150
- const didFromBase64 = generateDidKeyFromBase64(publicKeyBase64);
151
-
152
- expect(didFromBytes).toBe(didFromBase64);
153
- });
154
- });
155
- });
156
-
@@ -1,109 +0,0 @@
1
- /**
2
- * Base58 Utilities (Bitcoin alphabet)
3
- *
4
- * Encoding and decoding utilities for Base58 (Bitcoin alphabet).
5
- * Used for did:key multibase encoding (with 'z' prefix for base58btc).
6
- *
7
- * The Bitcoin alphabet excludes ambiguous characters (0, O, I, l).
8
- */
9
-
10
- const ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
11
- const ALPHABET_MAP = new Map<string, number>();
12
-
13
- // Build reverse lookup map
14
- for (let i = 0; i < ALPHABET.length; i++) {
15
- ALPHABET_MAP.set(ALPHABET[i], i);
16
- }
17
-
18
- /**
19
- * Encode bytes to Base58 (Bitcoin alphabet)
20
- *
21
- * @param bytes - Bytes to encode
22
- * @returns Base58-encoded string
23
- */
24
- export function base58Encode(bytes: Uint8Array): string {
25
- if (bytes.length === 0) return '';
26
-
27
- // Convert bytes to big integer
28
- let num = BigInt(0);
29
- for (let i = 0; i < bytes.length; i++) {
30
- num = num * BigInt(256) + BigInt(bytes[i]);
31
- }
32
-
33
- // Convert to base58
34
- let result = '';
35
- while (num > 0) {
36
- result = ALPHABET[Number(num % BigInt(58))] + result;
37
- num = num / BigInt(58);
38
- }
39
-
40
- // Add leading zeros (encoded as '1' in base58)
41
- for (let i = 0; i < bytes.length && bytes[i] === 0; i++) {
42
- result = '1' + result;
43
- }
44
-
45
- return result;
46
- }
47
-
48
- /**
49
- * Decode Base58 (Bitcoin alphabet) to bytes
50
- *
51
- * @param encoded - Base58-encoded string
52
- * @returns Decoded bytes
53
- * @throws Error if input contains invalid characters
54
- */
55
- export function base58Decode(encoded: string): Uint8Array {
56
- if (encoded.length === 0) return new Uint8Array(0);
57
-
58
- // Convert base58 to big integer
59
- let num = BigInt(0);
60
- for (const char of encoded) {
61
- const value = ALPHABET_MAP.get(char);
62
- if (value === undefined) {
63
- throw new Error(`Invalid base58 character: ${char}`);
64
- }
65
- num = num * BigInt(58) + BigInt(value);
66
- }
67
-
68
- // Convert big integer to bytes
69
- const bytes: number[] = [];
70
- while (num > 0) {
71
- bytes.unshift(Number(num % BigInt(256)));
72
- num = num / BigInt(256);
73
- }
74
-
75
- // Count leading zeros in input (encoded as '1')
76
- let leadingZeros = 0;
77
- for (const char of encoded) {
78
- if (char === '1') {
79
- leadingZeros++;
80
- } else {
81
- break;
82
- }
83
- }
84
-
85
- // Prepend leading zero bytes
86
- const result = new Uint8Array(leadingZeros + bytes.length);
87
- // Leading zeros are already 0 in Uint8Array
88
- result.set(bytes, leadingZeros);
89
-
90
- return result;
91
- }
92
-
93
- /**
94
- * Validate a Base58 string
95
- *
96
- * @param encoded - String to validate
97
- * @returns true if valid Base58, false otherwise
98
- */
99
- export function isValidBase58(encoded: string): boolean {
100
- if (encoded.length === 0) return true;
101
-
102
- for (const char of encoded) {
103
- if (!ALPHABET_MAP.has(char)) {
104
- return false;
105
- }
106
- }
107
-
108
- return true;
109
- }
@@ -1,148 +0,0 @@
1
- /**
2
- * Base64URL Encoding/Decoding Utilities
3
- *
4
- * Environment-aware base64url helpers that work in both Node.js and Cloudflare Workers.
5
- * Uses Buffer in Node.js, TextEncoder/Decoder fallback for Workers.
6
- */
7
-
8
- /**
9
- * Decode base64url string to string
10
- */
11
- export function base64urlDecodeToString(input: string): string {
12
- const padded = addPadding(input);
13
- const base64 = padded.replace(/-/g, "+").replace(/_/g, "/");
14
-
15
- // For platforms that don't have Buffer (e.g., Cloudflare Workers)
16
- if (typeof atob !== "undefined") {
17
- try {
18
- return atob(base64);
19
- } catch (error) {
20
- throw new Error(
21
- `Invalid base64url string: ${error instanceof Error ? error.message : String(error)}`
22
- );
23
- }
24
- }
25
-
26
- // For Node.js environments - Buffer.from doesn't throw for invalid base64
27
- // We need to validate by checking if the input contains only valid base64 characters
28
- // Base64 characters: A-Z, a-z, 0-9, +, /, = (padding)
29
- const base64Regex = /^[A-Za-z0-9+/]*={0,2}$/;
30
- if (!base64Regex.test(base64)) {
31
- throw new Error("Invalid base64url string: contains invalid characters");
32
- }
33
-
34
- try {
35
- const decoded = Buffer.from(base64, "base64").toString("utf-8");
36
- return decoded;
37
- } catch (error) {
38
- throw new Error(
39
- `Invalid base64url string: ${error instanceof Error ? error.message : String(error)}`
40
- );
41
- }
42
- }
43
-
44
- /**
45
- * Decode base64url string to Uint8Array
46
- */
47
- export function base64urlDecodeToBytes(input: string): Uint8Array {
48
- const padded = addPadding(input);
49
- const base64 = padded.replace(/-/g, "+").replace(/_/g, "/");
50
-
51
- // For platforms that don't have Buffer (e.g., Cloudflare Workers)
52
- if (typeof atob !== "undefined") {
53
- try {
54
- const binaryString = atob(base64);
55
- const bytes = new Uint8Array(binaryString.length);
56
- for (let i = 0; i < binaryString.length; i++) {
57
- bytes[i] = binaryString.charCodeAt(i);
58
- }
59
- return bytes;
60
- } catch (error) {
61
- throw new Error(
62
- `Invalid base64url string: ${error instanceof Error ? error.message : String(error)}`
63
- );
64
- }
65
- }
66
-
67
- // For Node.js environments - Buffer.from doesn't throw, so we can't validate here
68
- // The caller should validate the result if needed
69
- return new Uint8Array(Buffer.from(base64, "base64"));
70
- }
71
-
72
- /**
73
- * Encode string to base64url
74
- */
75
- export function base64urlEncodeFromString(input: string): string {
76
- // For platforms that don't have Buffer
77
- if (typeof btoa !== "undefined") {
78
- const base64 = btoa(input);
79
- return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
80
- }
81
-
82
- // For Node.js environments
83
- const base64 = Buffer.from(input, "utf-8").toString("base64");
84
- return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
85
- }
86
-
87
- /**
88
- * Encode Uint8Array to base64url
89
- */
90
- export function base64urlEncodeFromBytes(bytes: Uint8Array): string {
91
- // For platforms that don't have Buffer
92
- if (typeof btoa !== "undefined") {
93
- const binaryString = Array.from(bytes)
94
- .map((byte) => String.fromCharCode(byte))
95
- .join("");
96
- const base64 = btoa(binaryString);
97
- return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
98
- }
99
-
100
- // For Node.js environments
101
- const base64 = Buffer.from(bytes).toString("base64");
102
- return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
103
- }
104
-
105
- /**
106
- * Convert bytes to standard base64 (not base64url)
107
- */
108
- export function bytesToBase64(bytes: Uint8Array): string {
109
- // For platforms that don't have Buffer
110
- if (typeof btoa !== "undefined") {
111
- const binaryString = Array.from(bytes)
112
- .map((byte) => String.fromCharCode(byte))
113
- .join("");
114
- return btoa(binaryString);
115
- }
116
-
117
- // For Node.js environments
118
- return Buffer.from(bytes).toString("base64");
119
- }
120
-
121
- /**
122
- * Convert standard base64 to bytes
123
- */
124
- export function base64ToBytes(base64: string): Uint8Array {
125
- // For platforms that don't have Buffer
126
- if (typeof atob !== "undefined") {
127
- const binaryString = atob(base64);
128
- const bytes = new Uint8Array(binaryString.length);
129
- for (let i = 0; i < binaryString.length; i++) {
130
- bytes[i] = binaryString.charCodeAt(i);
131
- }
132
- return bytes;
133
- }
134
-
135
- // For Node.js environments
136
- return new Uint8Array(Buffer.from(base64, "base64"));
137
- }
138
-
139
- /**
140
- * Add padding to base64url string if needed
141
- */
142
- function addPadding(input: string): string {
143
- const remainder = input.length % 4;
144
- if (remainder === 0) {
145
- return input;
146
- }
147
- return input + "=".repeat((4 - remainder) % 4);
148
- }