@kya-os/mcp-i-runtime 1.0.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 (366) hide show
  1. package/.turbo/turbo-build.log +4 -0
  2. package/dist/auth/handshake.d.ts +136 -0
  3. package/dist/auth/handshake.d.ts.map +1 -0
  4. package/dist/auth/handshake.js +276 -0
  5. package/dist/auth/handshake.js.map +1 -0
  6. package/dist/auth/index.d.ts +6 -0
  7. package/dist/auth/index.d.ts.map +1 -0
  8. package/dist/auth/index.js +12 -0
  9. package/dist/auth/index.js.map +1 -0
  10. package/dist/auth/types.d.ts +52 -0
  11. package/dist/auth/types.d.ts.map +1 -0
  12. package/dist/auth/types.js +10 -0
  13. package/dist/auth/types.js.map +1 -0
  14. package/dist/cache/oauth-config-cache.d.ts +69 -0
  15. package/dist/cache/oauth-config-cache.d.ts.map +1 -0
  16. package/dist/cache/oauth-config-cache.js +76 -0
  17. package/dist/cache/oauth-config-cache.js.map +1 -0
  18. package/dist/cache/tool-protection-cache.d.ts +102 -0
  19. package/dist/cache/tool-protection-cache.d.ts.map +1 -0
  20. package/dist/cache/tool-protection-cache.js +131 -0
  21. package/dist/cache/tool-protection-cache.js.map +1 -0
  22. package/dist/compliance/index.d.ts +8 -0
  23. package/dist/compliance/index.d.ts.map +1 -0
  24. package/dist/compliance/index.js +24 -0
  25. package/dist/compliance/index.js.map +1 -0
  26. package/dist/compliance/schema-registry.d.ts +50 -0
  27. package/dist/compliance/schema-registry.d.ts.map +1 -0
  28. package/dist/compliance/schema-registry.js +438 -0
  29. package/dist/compliance/schema-registry.js.map +1 -0
  30. package/dist/compliance/schema-verifier.d.ts +110 -0
  31. package/dist/compliance/schema-verifier.d.ts.map +1 -0
  32. package/dist/compliance/schema-verifier.js +518 -0
  33. package/dist/compliance/schema-verifier.js.map +1 -0
  34. package/dist/config/remote-config.d.ts +188 -0
  35. package/dist/config/remote-config.d.ts.map +1 -0
  36. package/dist/config/remote-config.js +268 -0
  37. package/dist/config/remote-config.js.map +1 -0
  38. package/dist/config.d.ts +182 -0
  39. package/dist/config.d.ts.map +1 -0
  40. package/dist/config.js +148 -0
  41. package/dist/config.js.map +1 -0
  42. package/dist/identity/idp-token-resolver.d.ts +69 -0
  43. package/dist/identity/idp-token-resolver.d.ts.map +1 -0
  44. package/dist/identity/idp-token-resolver.js +136 -0
  45. package/dist/identity/idp-token-resolver.js.map +1 -0
  46. package/dist/identity/idp-token-storage.interface.d.ts +80 -0
  47. package/dist/identity/idp-token-storage.interface.d.ts.map +1 -0
  48. package/dist/identity/idp-token-storage.interface.js +14 -0
  49. package/dist/identity/idp-token-storage.interface.js.map +1 -0
  50. package/dist/identity/user-did-manager.d.ts +228 -0
  51. package/dist/identity/user-did-manager.d.ts.map +1 -0
  52. package/dist/identity/user-did-manager.js +356 -0
  53. package/dist/identity/user-did-manager.js.map +1 -0
  54. package/dist/index.d.ts +69 -0
  55. package/dist/index.d.ts.map +1 -0
  56. package/dist/index.js +193 -0
  57. package/dist/index.js.map +1 -0
  58. package/dist/logging/index.d.ts +5 -0
  59. package/dist/logging/index.d.ts.map +1 -0
  60. package/dist/logging/index.js +10 -0
  61. package/dist/logging/index.js.map +1 -0
  62. package/dist/logging/logger.d.ts +33 -0
  63. package/dist/logging/logger.d.ts.map +1 -0
  64. package/dist/logging/logger.js +104 -0
  65. package/dist/logging/logger.js.map +1 -0
  66. package/dist/proof/generator.d.ts +109 -0
  67. package/dist/proof/generator.d.ts.map +1 -0
  68. package/dist/proof/generator.js +236 -0
  69. package/dist/proof/generator.js.map +1 -0
  70. package/dist/proof/index.d.ts +5 -0
  71. package/dist/proof/index.d.ts.map +1 -0
  72. package/dist/proof/index.js +11 -0
  73. package/dist/proof/index.js.map +1 -0
  74. package/dist/providers/base.d.ts +95 -0
  75. package/dist/providers/base.d.ts.map +1 -0
  76. package/dist/providers/base.js +47 -0
  77. package/dist/providers/base.js.map +1 -0
  78. package/dist/providers/memory.d.ts +42 -0
  79. package/dist/providers/memory.d.ts.map +1 -0
  80. package/dist/providers/memory.js +121 -0
  81. package/dist/providers/memory.js.map +1 -0
  82. package/dist/runtime/audit-logger.d.ts +37 -0
  83. package/dist/runtime/audit-logger.d.ts.map +1 -0
  84. package/dist/runtime/audit-logger.js +9 -0
  85. package/dist/runtime/audit-logger.js.map +1 -0
  86. package/dist/runtime/base.d.ts +392 -0
  87. package/dist/runtime/base.d.ts.map +1 -0
  88. package/dist/runtime/base.js +1527 -0
  89. package/dist/runtime/base.js.map +1 -0
  90. package/dist/runtime/dif-core.d.ts +33 -0
  91. package/dist/runtime/dif-core.d.ts.map +1 -0
  92. package/dist/runtime/dif-core.js +14 -0
  93. package/dist/runtime/dif-core.js.map +1 -0
  94. package/dist/runtime/ext-apps-constants.d.ts +20 -0
  95. package/dist/runtime/ext-apps-constants.d.ts.map +1 -0
  96. package/dist/runtime/ext-apps-constants.js +23 -0
  97. package/dist/runtime/ext-apps-constants.js.map +1 -0
  98. package/dist/services/access-control.service.d.ts +138 -0
  99. package/dist/services/access-control.service.d.ts.map +1 -0
  100. package/dist/services/access-control.service.js +721 -0
  101. package/dist/services/access-control.service.js.map +1 -0
  102. package/dist/services/authorization/authorization-registry.d.ts +29 -0
  103. package/dist/services/authorization/authorization-registry.d.ts.map +1 -0
  104. package/dist/services/authorization/authorization-registry.js +58 -0
  105. package/dist/services/authorization/authorization-registry.js.map +1 -0
  106. package/dist/services/authorization/types.d.ts +53 -0
  107. package/dist/services/authorization/types.d.ts.map +1 -0
  108. package/dist/services/authorization/types.js +10 -0
  109. package/dist/services/authorization/types.js.map +1 -0
  110. package/dist/services/batch-delegation.service.d.ts +53 -0
  111. package/dist/services/batch-delegation.service.d.ts.map +1 -0
  112. package/dist/services/batch-delegation.service.js +95 -0
  113. package/dist/services/batch-delegation.service.js.map +1 -0
  114. package/dist/services/crypto.service.d.ts +69 -0
  115. package/dist/services/crypto.service.d.ts.map +1 -0
  116. package/dist/services/crypto.service.js +225 -0
  117. package/dist/services/crypto.service.js.map +1 -0
  118. package/dist/services/delegation-error-formatter.d.ts +124 -0
  119. package/dist/services/delegation-error-formatter.d.ts.map +1 -0
  120. package/dist/services/delegation-error-formatter.js +214 -0
  121. package/dist/services/delegation-error-formatter.js.map +1 -0
  122. package/dist/services/errors.d.ts +49 -0
  123. package/dist/services/errors.d.ts.map +1 -0
  124. package/dist/services/errors.js +66 -0
  125. package/dist/services/errors.js.map +1 -0
  126. package/dist/services/index.d.ts +9 -0
  127. package/dist/services/index.d.ts.map +1 -0
  128. package/dist/services/index.js +15 -0
  129. package/dist/services/index.js.map +1 -0
  130. package/dist/services/oauth-config.service.d.ts +53 -0
  131. package/dist/services/oauth-config.service.d.ts.map +1 -0
  132. package/dist/services/oauth-config.service.js +141 -0
  133. package/dist/services/oauth-config.service.js.map +1 -0
  134. package/dist/services/oauth-provider-registry.d.ts +88 -0
  135. package/dist/services/oauth-provider-registry.d.ts.map +1 -0
  136. package/dist/services/oauth-provider-registry.js +128 -0
  137. package/dist/services/oauth-provider-registry.js.map +1 -0
  138. package/dist/services/oauth-service.d.ts +122 -0
  139. package/dist/services/oauth-service.d.ts.map +1 -0
  140. package/dist/services/oauth-service.js +442 -0
  141. package/dist/services/oauth-service.js.map +1 -0
  142. package/dist/services/oauth-token-retrieval.service.d.ts +49 -0
  143. package/dist/services/oauth-token-retrieval.service.d.ts.map +1 -0
  144. package/dist/services/oauth-token-retrieval.service.js +150 -0
  145. package/dist/services/oauth-token-retrieval.service.js.map +1 -0
  146. package/dist/services/policy.service.d.ts +130 -0
  147. package/dist/services/policy.service.d.ts.map +1 -0
  148. package/dist/services/policy.service.js +399 -0
  149. package/dist/services/policy.service.js.map +1 -0
  150. package/dist/services/proof-verifier.d.ts +103 -0
  151. package/dist/services/proof-verifier.d.ts.map +1 -0
  152. package/dist/services/proof-verifier.js +190 -0
  153. package/dist/services/proof-verifier.js.map +1 -0
  154. package/dist/services/provider-resolver.d.ts +89 -0
  155. package/dist/services/provider-resolver.d.ts.map +1 -0
  156. package/dist/services/provider-resolver.js +196 -0
  157. package/dist/services/provider-resolver.js.map +1 -0
  158. package/dist/services/provider-validator.d.ts +55 -0
  159. package/dist/services/provider-validator.d.ts.map +1 -0
  160. package/dist/services/provider-validator.js +135 -0
  161. package/dist/services/provider-validator.js.map +1 -0
  162. package/dist/services/session-registration.service.d.ts +80 -0
  163. package/dist/services/session-registration.service.d.ts.map +1 -0
  164. package/dist/services/session-registration.service.js +174 -0
  165. package/dist/services/session-registration.service.js.map +1 -0
  166. package/dist/services/storage.service.d.ts +116 -0
  167. package/dist/services/storage.service.d.ts.map +1 -0
  168. package/dist/services/storage.service.js +411 -0
  169. package/dist/services/storage.service.js.map +1 -0
  170. package/dist/services/tool-context-builder.d.ts +74 -0
  171. package/dist/services/tool-context-builder.d.ts.map +1 -0
  172. package/dist/services/tool-context-builder.js +215 -0
  173. package/dist/services/tool-context-builder.js.map +1 -0
  174. package/dist/services/tool-protection.service.d.ts +209 -0
  175. package/dist/services/tool-protection.service.d.ts.map +1 -0
  176. package/dist/services/tool-protection.service.js +752 -0
  177. package/dist/services/tool-protection.service.js.map +1 -0
  178. package/dist/session/index.d.ts +5 -0
  179. package/dist/session/index.d.ts.map +1 -0
  180. package/dist/session/index.js +11 -0
  181. package/dist/session/index.js.map +1 -0
  182. package/dist/session/manager.d.ts +118 -0
  183. package/dist/session/manager.d.ts.map +1 -0
  184. package/dist/session/manager.js +285 -0
  185. package/dist/session/manager.js.map +1 -0
  186. package/dist/types/oauth-required-error.d.ts +46 -0
  187. package/dist/types/oauth-required-error.d.ts.map +1 -0
  188. package/dist/types/oauth-required-error.js +42 -0
  189. package/dist/types/oauth-required-error.js.map +1 -0
  190. package/dist/types/tool-protection.d.ts +144 -0
  191. package/dist/types/tool-protection.d.ts.map +1 -0
  192. package/dist/types/tool-protection.js +48 -0
  193. package/dist/types/tool-protection.js.map +1 -0
  194. package/dist/utils/base58.d.ts +31 -0
  195. package/dist/utils/base58.d.ts.map +1 -0
  196. package/dist/utils/base58.js +103 -0
  197. package/dist/utils/base58.js.map +1 -0
  198. package/dist/utils/base64.d.ts +32 -0
  199. package/dist/utils/base64.d.ts.map +1 -0
  200. package/dist/utils/base64.js +172 -0
  201. package/dist/utils/base64.js.map +1 -0
  202. package/dist/utils/cors.d.ts +52 -0
  203. package/dist/utils/cors.d.ts.map +1 -0
  204. package/dist/utils/cors.js +79 -0
  205. package/dist/utils/cors.js.map +1 -0
  206. package/dist/utils/did-helpers.d.ts +168 -0
  207. package/dist/utils/did-helpers.d.ts.map +1 -0
  208. package/dist/utils/did-helpers.js +222 -0
  209. package/dist/utils/did-helpers.js.map +1 -0
  210. package/dist/utils/index.d.ts +8 -0
  211. package/dist/utils/index.d.ts.map +1 -0
  212. package/dist/utils/index.js +24 -0
  213. package/dist/utils/index.js.map +1 -0
  214. package/dist/utils/storage-keys.d.ts +120 -0
  215. package/dist/utils/storage-keys.d.ts.map +1 -0
  216. package/dist/utils/storage-keys.js +217 -0
  217. package/dist/utils/storage-keys.js.map +1 -0
  218. package/dist/wire-format.d.ts +62 -0
  219. package/dist/wire-format.d.ts.map +1 -0
  220. package/dist/wire-format.js +89 -0
  221. package/dist/wire-format.js.map +1 -0
  222. package/package.json +83 -0
  223. package/src/__tests__/cache/tool-protection-cache.test.ts +640 -0
  224. package/src/__tests__/config/provider-runtime-config.test.ts +309 -0
  225. package/src/__tests__/config.test.ts +307 -0
  226. package/src/__tests__/identity/user-did-manager.test.ts +232 -0
  227. package/src/__tests__/index.test.ts +56 -0
  228. package/src/__tests__/integration/full-flow.test.ts +779 -0
  229. package/src/__tests__/integration.test.ts +281 -0
  230. package/src/__tests__/providers/base.test.ts +173 -0
  231. package/src/__tests__/providers/memory.test.ts +319 -0
  232. package/src/__tests__/regression/phase2-regression.test.ts +406 -0
  233. package/src/__tests__/runtime/audit-logger.test.ts +154 -0
  234. package/src/__tests__/runtime/base-extensions.test.ts +595 -0
  235. package/src/__tests__/runtime/base.test.ts +1328 -0
  236. package/src/__tests__/runtime/delegation-flow.test.ts +159 -0
  237. package/src/__tests__/runtime/process-tool-call-security.test.ts +1024 -0
  238. package/src/__tests__/runtime/proof-client-did.test.ts +376 -0
  239. package/src/__tests__/runtime/provider-selection.test.ts +292 -0
  240. package/src/__tests__/runtime/route-interception.test.ts +670 -0
  241. package/src/__tests__/runtime/tool-protection-enforcement.test.ts +989 -0
  242. package/src/__tests__/services/access-control-retry.test.ts +595 -0
  243. package/src/__tests__/services/agentshield-integration.test.ts +709 -0
  244. package/src/__tests__/services/cache-busting.test.ts +125 -0
  245. package/src/__tests__/services/oauth-service-errors.test.ts +704 -0
  246. package/src/__tests__/services/oauth-service-pkce.test.ts +556 -0
  247. package/src/__tests__/services/oauth-service-secret-resolution.test.ts +349 -0
  248. package/src/__tests__/services/provider-resolver-edge-cases.test.ts +591 -0
  249. package/src/__tests__/services/tool-protection-failsafe.test.ts +683 -0
  250. package/src/__tests__/services/tool-protection-merged-config.test.ts +664 -0
  251. package/src/__tests__/services/tool-protection-oauth-provider.test.ts +300 -0
  252. package/src/__tests__/services/tool-protection.service.test.ts +1201 -0
  253. package/src/__tests__/utils/mock-providers.ts +340 -0
  254. package/src/__tests__/wire-format.test.ts +123 -0
  255. package/src/auth/__tests__/resume-token.test.ts +42 -0
  256. package/src/auth/handshake.ts +476 -0
  257. package/src/auth/index.ts +15 -0
  258. package/src/auth/types.ts +68 -0
  259. package/src/cache/__tests__/oauth-config-cache.test.ts +215 -0
  260. package/src/cache/oauth-config-cache.d.ts +69 -0
  261. package/src/cache/oauth-config-cache.d.ts.map +1 -0
  262. package/src/cache/oauth-config-cache.js.map +1 -0
  263. package/src/cache/oauth-config-cache.ts +123 -0
  264. package/src/cache/tool-protection-cache.ts +184 -0
  265. package/src/compliance/EXAMPLE.md +412 -0
  266. package/src/compliance/__tests__/schema-verifier.test.ts +791 -0
  267. package/src/compliance/index.ts +8 -0
  268. package/src/compliance/schema-registry.ts +460 -0
  269. package/src/compliance/schema-verifier.ts +708 -0
  270. package/src/config/__tests__/merged-config.spec.ts +445 -0
  271. package/src/config/__tests__/remote-config-edge-cases.test.ts +424 -0
  272. package/src/config/__tests__/remote-config-helpers.test.ts +280 -0
  273. package/src/config/__tests__/remote-config.spec.ts +265 -0
  274. package/src/config/remote-config.ts +368 -0
  275. package/src/config.ts +312 -0
  276. package/src/identity/__tests__/idp-token-resolver.test.ts +340 -0
  277. package/src/identity/__tests__/user-did-manager.test.ts +487 -0
  278. package/src/identity/idp-token-resolver.ts +181 -0
  279. package/src/identity/idp-token-storage.interface.ts +102 -0
  280. package/src/identity/user-did-manager.ts +592 -0
  281. package/src/index.ts +331 -0
  282. package/src/logging/__tests__/logger.test.ts +366 -0
  283. package/src/logging/index.ts +10 -0
  284. package/src/logging/logger.ts +112 -0
  285. package/src/proof/__tests__/crypto-parity.test.ts +202 -0
  286. package/src/proof/__tests__/proof-generator.test.ts +484 -0
  287. package/src/proof/generator.ts +347 -0
  288. package/src/proof/index.ts +12 -0
  289. package/src/providers/base.d.ts +91 -0
  290. package/src/providers/base.d.ts.map +1 -0
  291. package/src/providers/base.js.map +1 -0
  292. package/src/providers/base.ts +100 -0
  293. package/src/providers/memory.ts +142 -0
  294. package/src/runtime/__tests__/dif-esm-bridge.test.ts +33 -0
  295. package/src/runtime/__tests__/ext-apps-consent.test.ts +334 -0
  296. package/src/runtime/audit-logger.ts +39 -0
  297. package/src/runtime/base.ts +2100 -0
  298. package/src/runtime/dif-core.ts +39 -0
  299. package/src/runtime/ext-apps-constants.ts +20 -0
  300. package/src/services/__tests__/access-control.integration.test.ts +441 -0
  301. package/src/services/__tests__/access-control.proof-response-validation.test.ts +578 -0
  302. package/src/services/__tests__/access-control.service.test.ts +1132 -0
  303. package/src/services/__tests__/batch-delegation.service.test.ts +351 -0
  304. package/src/services/__tests__/crypto.service.test.ts +531 -0
  305. package/src/services/__tests__/delegation-error-formatter.test.ts +643 -0
  306. package/src/services/__tests__/oauth-config.service.test.ts +359 -0
  307. package/src/services/__tests__/oauth-provider-registry.test.ts +142 -0
  308. package/src/services/__tests__/oauth-token-retrieval.service.test.ts +365 -0
  309. package/src/services/__tests__/proof-verifier.integration.test.ts +494 -0
  310. package/src/services/__tests__/proof-verifier.test.ts +489 -0
  311. package/src/services/__tests__/provider-resolution.integration.test.ts +202 -0
  312. package/src/services/__tests__/provider-resolver.test.ts +213 -0
  313. package/src/services/__tests__/provider-validator.test.ts +370 -0
  314. package/src/services/__tests__/session-registration.service.test.ts +312 -0
  315. package/src/services/__tests__/storage.service.test.ts +358 -0
  316. package/src/services/__tests__/tool-context-builder.test.ts +438 -0
  317. package/src/services/access-control.service.ts +1048 -0
  318. package/src/services/authorization/authorization-registry.ts +67 -0
  319. package/src/services/authorization/types.ts +71 -0
  320. package/src/services/batch-delegation.service.ts +136 -0
  321. package/src/services/crypto.service.ts +296 -0
  322. package/src/services/delegation-error-formatter.ts +299 -0
  323. package/src/services/errors.ts +76 -0
  324. package/src/services/index.ts +34 -0
  325. package/src/services/oauth-config.service.d.ts +53 -0
  326. package/src/services/oauth-config.service.d.ts.map +1 -0
  327. package/src/services/oauth-config.service.js.map +1 -0
  328. package/src/services/oauth-config.service.ts +192 -0
  329. package/src/services/oauth-provider-registry.d.ts +57 -0
  330. package/src/services/oauth-provider-registry.d.ts.map +1 -0
  331. package/src/services/oauth-provider-registry.js.map +1 -0
  332. package/src/services/oauth-provider-registry.ts +141 -0
  333. package/src/services/oauth-service.ts +652 -0
  334. package/src/services/oauth-token-retrieval.service.ts +245 -0
  335. package/src/services/policy.service.ts +527 -0
  336. package/src/services/proof-verifier.ts +295 -0
  337. package/src/services/provider-resolver.d.ts +48 -0
  338. package/src/services/provider-resolver.d.ts.map +1 -0
  339. package/src/services/provider-resolver.js.map +1 -0
  340. package/src/services/provider-resolver.ts +236 -0
  341. package/src/services/provider-validator.ts +170 -0
  342. package/src/services/session-registration.service.ts +259 -0
  343. package/src/services/storage.service.ts +566 -0
  344. package/src/services/tool-context-builder.ts +285 -0
  345. package/src/services/tool-protection.service.ts +971 -0
  346. package/src/session/__tests__/session-manager.test.ts +327 -0
  347. package/src/session/index.ts +10 -0
  348. package/src/session/manager.ts +380 -0
  349. package/src/types/__tests__/oauth-required-error.test.ts +79 -0
  350. package/src/types/__tests__/tool-protection.test.ts +121 -0
  351. package/src/types/oauth-required-error.ts +71 -0
  352. package/src/types/tool-protection.ts +179 -0
  353. package/src/utils/__tests__/base58.test.ts +281 -0
  354. package/src/utils/__tests__/base64.test.ts +239 -0
  355. package/src/utils/__tests__/cors.test.ts +184 -0
  356. package/src/utils/__tests__/did-helpers.test.ts +156 -0
  357. package/src/utils/__tests__/storage-keys.test.ts +372 -0
  358. package/src/utils/base58.ts +109 -0
  359. package/src/utils/base64.ts +185 -0
  360. package/src/utils/cors.ts +93 -0
  361. package/src/utils/did-helpers.ts +229 -0
  362. package/src/utils/index.ts +8 -0
  363. package/src/utils/storage-keys.ts +285 -0
  364. package/src/wire-format.ts +114 -0
  365. package/tsconfig.json +21 -0
  366. package/vitest.config.mts +57 -0
@@ -0,0 +1,347 @@
1
+ /**
2
+ * Proof Generation — Platform-agnostic Protocol Reference
3
+ *
4
+ * Handles JCS canonicalization, SHA-256 digest generation, and Ed25519 JWS
5
+ * signing (compact format) according to MCP-I requirements 5.1, 5.2, 5.3, 5.6.
6
+ *
7
+ * This module is the authoritative proof implementation. All platform adapters
8
+ * (Node.js, Cloudflare Workers) inject a CryptoProvider and delegate here.
9
+ */
10
+
11
+ import { SignJWT, importPKCS8 } from "jose";
12
+ import { canonicalize } from "json-canonicalize";
13
+ import {
14
+ DetachedProof,
15
+ ProofMeta,
16
+ CanonicalHashes,
17
+ } from "@kya-os/contracts/proof";
18
+ import { SessionContext } from "@kya-os/contracts/handshake";
19
+ import { CryptoProvider } from "../providers/base";
20
+ import { CryptoService, type Ed25519JWK } from "../services/crypto.service";
21
+
22
+ /**
23
+ * Minimal agent identity required by ProofGenerator.
24
+ * Structurally compatible with both AgentIdentity definitions
25
+ * in mcp-i-core (providers/base) and packages/mcp-i (runtime/identity).
26
+ */
27
+ export interface ProofAgentIdentity {
28
+ did: string;
29
+ kid: string;
30
+ privateKey: string;
31
+ publicKey: string;
32
+ }
33
+
34
+ /**
35
+ * Tool request structure for proof generation
36
+ */
37
+ export interface ToolRequest {
38
+ method: string;
39
+ params?: unknown;
40
+ }
41
+
42
+ /**
43
+ * Tool response structure for proof generation
44
+ */
45
+ export interface ToolResponse {
46
+ data: unknown;
47
+ meta?: {
48
+ proof?: DetachedProof;
49
+ [key: string]: unknown;
50
+ };
51
+ }
52
+
53
+ /**
54
+ * Proof generation options
55
+ */
56
+ export interface ProofOptions {
57
+ scopeId?: string;
58
+ delegationRef?: string;
59
+ clientDid?: string;
60
+ }
61
+
62
+ /**
63
+ * Platform-agnostic proof generator.
64
+ * Requires a CryptoProvider for all cryptographic operations.
65
+ */
66
+ export class ProofGenerator {
67
+ private identity: ProofAgentIdentity;
68
+ private cryptoProvider: CryptoProvider;
69
+
70
+ constructor(identity: ProofAgentIdentity, cryptoProvider: CryptoProvider) {
71
+ this.identity = identity;
72
+ this.cryptoProvider = cryptoProvider;
73
+ }
74
+
75
+ /**
76
+ * Generate proof for tool request/response.
77
+ * Requirements: 5.1, 5.2, 5.3, 5.6
78
+ */
79
+ async generateProof(
80
+ request: ToolRequest,
81
+ response: ToolResponse,
82
+ session: SessionContext,
83
+ options: ProofOptions = {}
84
+ ): Promise<DetachedProof> {
85
+ const hashes = await this.generateCanonicalHashes(request, response);
86
+
87
+ const meta: ProofMeta = {
88
+ did: this.identity.did,
89
+ kid: this.identity.kid,
90
+ ts: Math.floor(Date.now() / 1000),
91
+ nonce: session.nonce,
92
+ audience: session.audience,
93
+ sessionId: session.sessionId,
94
+ requestHash: hashes.requestHash,
95
+ responseHash: hashes.responseHash,
96
+ ...options,
97
+ };
98
+
99
+ const jws = await this.generateJWS(meta);
100
+
101
+ return { jws, meta };
102
+ }
103
+
104
+ /**
105
+ * Generate canonical hashes for request and response.
106
+ * Requirement: 5.1
107
+ */
108
+ private async generateCanonicalHashes(
109
+ request: ToolRequest,
110
+ response: ToolResponse
111
+ ): Promise<CanonicalHashes> {
112
+ const canonicalRequest = {
113
+ method: request.method,
114
+ ...(request.params ? { params: request.params } : {}),
115
+ };
116
+ const canonicalResponse = response.data;
117
+
118
+ const requestHash = await this.generateSHA256Hash(canonicalRequest);
119
+ const responseHash = await this.generateSHA256Hash(canonicalResponse);
120
+
121
+ return { requestHash, responseHash };
122
+ }
123
+
124
+ /**
125
+ * Generate SHA-256 hash with JCS canonicalization.
126
+ * Requirement: 5.2
127
+ *
128
+ * Delegates to CryptoProvider.hash() which must return "sha256:<hex>".
129
+ */
130
+ private async generateSHA256Hash(data: unknown): Promise<string> {
131
+ const canonicalJson = this.canonicalizeJSON(data);
132
+ const encoded = new TextEncoder().encode(canonicalJson);
133
+ return this.cryptoProvider.hash(encoded);
134
+ }
135
+
136
+ /**
137
+ * JCS canonicalization implementation (RFC 8785)
138
+ */
139
+ private canonicalizeJSON(obj: unknown): string {
140
+ return canonicalize(obj as Parameters<typeof canonicalize>[0]);
141
+ }
142
+
143
+ /**
144
+ * Generate Ed25519 JWS in compact format (header.payload.signature).
145
+ * Requirement: 5.3
146
+ *
147
+ * Uses standard JWT claims (aud, sub, iss) in addition to custom claims.
148
+ */
149
+ private async generateJWS(meta: ProofMeta): Promise<string> {
150
+ try {
151
+ const privateKeyPem = this.formatPrivateKeyAsPEM(this.identity.privateKey);
152
+ const privateKey = await importPKCS8(privateKeyPem, "EdDSA");
153
+
154
+ const payload = {
155
+ aud: meta.audience,
156
+ sub: meta.did,
157
+ iss: meta.did,
158
+ requestHash: meta.requestHash,
159
+ responseHash: meta.responseHash,
160
+ ts: meta.ts,
161
+ nonce: meta.nonce,
162
+ sessionId: meta.sessionId,
163
+ ...(meta.scopeId && { scopeId: meta.scopeId }),
164
+ ...(meta.delegationRef && { delegationRef: meta.delegationRef }),
165
+ ...(meta.clientDid && { clientDid: meta.clientDid }),
166
+ };
167
+
168
+ const jwt = await new SignJWT(payload)
169
+ .setProtectedHeader({
170
+ alg: "EdDSA",
171
+ kid: this.identity.kid,
172
+ })
173
+ .sign(privateKey);
174
+
175
+ return jwt;
176
+ } catch (error) {
177
+ throw new Error(
178
+ `Failed to generate JWS: ${error instanceof Error ? error.message : "Unknown error"}`
179
+ );
180
+ }
181
+ }
182
+
183
+ /**
184
+ * Format base64 private key as PKCS#8 PEM for JOSE library.
185
+ */
186
+ private formatPrivateKeyAsPEM(base64PrivateKey: string): string {
187
+ // Decode the base64 private key bytes
188
+ // Use platform-agnostic approach (atob + Uint8Array)
189
+ const binaryStr = atob(base64PrivateKey);
190
+ const keyData = new Uint8Array(binaryStr.length);
191
+ for (let i = 0; i < binaryStr.length; i++) {
192
+ keyData[i] = binaryStr.charCodeAt(i);
193
+ }
194
+
195
+ // Ed25519 PKCS#8 header (ASN.1 encoding)
196
+ const pkcs8Header = new Uint8Array([
197
+ 0x30,
198
+ 0x2e, // SEQUENCE, length 46
199
+ 0x02,
200
+ 0x01,
201
+ 0x00, // INTEGER version 0
202
+ 0x30,
203
+ 0x05, // SEQUENCE, length 5
204
+ 0x06,
205
+ 0x03,
206
+ 0x2b,
207
+ 0x65,
208
+ 0x70, // OID for Ed25519
209
+ 0x04,
210
+ 0x22, // OCTET STRING, length 34
211
+ 0x04,
212
+ 0x20, // OCTET STRING, length 32 (the actual key)
213
+ ]);
214
+
215
+ // Take first 32 bytes of key (Ed25519 raw private key)
216
+ const rawKey = keyData.subarray(0, 32);
217
+ const fullKey = new Uint8Array(pkcs8Header.length + rawKey.length);
218
+ fullKey.set(pkcs8Header);
219
+ fullKey.set(rawKey, pkcs8Header.length);
220
+
221
+ // Base64-encode the full DER-encoded key
222
+ let binaryStrOut = "";
223
+ for (let i = 0; i < fullKey.length; i++) {
224
+ binaryStrOut += String.fromCharCode(fullKey[i]);
225
+ }
226
+ const base64Key = btoa(binaryStrOut);
227
+
228
+ // Format as PEM with 64-char line breaks
229
+ const formattedKey = base64Key.match(/.{1,64}/g)?.join("\n") ?? base64Key;
230
+
231
+ return (
232
+ "-----BEGIN PRIVATE KEY-----\n" +
233
+ formattedKey +
234
+ "\n-----END PRIVATE KEY-----"
235
+ );
236
+ }
237
+
238
+ /**
239
+ * Verify a proof (for testing/validation).
240
+ */
241
+ async verifyProof(
242
+ proof: DetachedProof,
243
+ request: ToolRequest,
244
+ response: ToolResponse
245
+ ): Promise<boolean> {
246
+ try {
247
+ const expectedHashes = await this.generateCanonicalHashes(
248
+ request,
249
+ response
250
+ );
251
+
252
+ if (
253
+ proof.meta.requestHash !== expectedHashes.requestHash ||
254
+ proof.meta.responseHash !== expectedHashes.responseHash
255
+ ) {
256
+ return false;
257
+ }
258
+
259
+ const publicKeyJwk = this.base64PublicKeyToJWK(this.identity.publicKey);
260
+ const cryptoService = new CryptoService(this.cryptoProvider);
261
+
262
+ return cryptoService.verifyJWS(proof.jws, publicKeyJwk, {
263
+ expectedKid: this.identity.kid,
264
+ alg: "EdDSA",
265
+ });
266
+ } catch {
267
+ return false;
268
+ }
269
+ }
270
+
271
+ /**
272
+ * Convert base64 public key to Ed25519 JWK format.
273
+ */
274
+ private base64PublicKeyToJWK(publicKeyBase64: string): Ed25519JWK {
275
+ // Decode base64 to bytes (platform-agnostic)
276
+ const binaryStr = atob(publicKeyBase64);
277
+ const publicKeyBytes = new Uint8Array(binaryStr.length);
278
+ for (let i = 0; i < binaryStr.length; i++) {
279
+ publicKeyBytes[i] = binaryStr.charCodeAt(i);
280
+ }
281
+
282
+ if (publicKeyBytes.length !== 32) {
283
+ throw new Error(
284
+ `Invalid Ed25519 public key length: ${publicKeyBytes.length}`
285
+ );
286
+ }
287
+
288
+ // Convert to base64url
289
+ let binaryStrOut = "";
290
+ for (let i = 0; i < publicKeyBytes.length; i++) {
291
+ binaryStrOut += String.fromCharCode(publicKeyBytes[i]);
292
+ }
293
+ const base64url = btoa(binaryStrOut)
294
+ .replace(/\+/g, "-")
295
+ .replace(/\//g, "_")
296
+ .replace(/=/g, "");
297
+
298
+ return {
299
+ kty: "OKP",
300
+ crv: "Ed25519",
301
+ x: base64url,
302
+ kid: this.identity.kid,
303
+ };
304
+ }
305
+ }
306
+
307
+ /**
308
+ * Create a tool response with proof.
309
+ */
310
+ export async function createProofResponse(
311
+ request: ToolRequest,
312
+ data: unknown,
313
+ identity: ProofAgentIdentity,
314
+ session: SessionContext,
315
+ cryptoProvider: CryptoProvider,
316
+ options: ProofOptions = {}
317
+ ): Promise<ToolResponse> {
318
+ const response: ToolResponse = { data };
319
+ const proofGenerator = new ProofGenerator(identity, cryptoProvider);
320
+ const proof = await proofGenerator.generateProof(
321
+ request,
322
+ response,
323
+ session,
324
+ options
325
+ );
326
+ response.meta = { proof };
327
+ return response;
328
+ }
329
+
330
+ /**
331
+ * Extract canonical data for hashing (utility for testing).
332
+ */
333
+ export function extractCanonicalData(
334
+ request: ToolRequest,
335
+ response: ToolResponse
336
+ ): {
337
+ request: unknown;
338
+ response: unknown;
339
+ } {
340
+ return {
341
+ request: {
342
+ method: request.method,
343
+ ...(request.params ? { params: request.params } : {}),
344
+ },
345
+ response: response.data,
346
+ };
347
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Proof module — platform-agnostic proof generation reference implementation.
3
+ */
4
+ export {
5
+ ProofGenerator,
6
+ createProofResponse,
7
+ extractCanonicalData,
8
+ type ProofAgentIdentity,
9
+ type ToolRequest,
10
+ type ToolResponse,
11
+ type ProofOptions,
12
+ } from "./generator";
@@ -0,0 +1,91 @@
1
+ /**
2
+ * Base Provider Classes
3
+ *
4
+ * Abstract classes that define the provider interfaces for
5
+ * platform-specific implementations.
6
+ */
7
+ /**
8
+ * Cryptographic operations provider
9
+ */
10
+ export declare abstract class CryptoProvider {
11
+ abstract sign(data: Uint8Array, privateKey: string): Promise<Uint8Array>;
12
+ abstract verify(data: Uint8Array, signature: Uint8Array, publicKey: string): Promise<boolean>;
13
+ abstract generateKeyPair(): Promise<{
14
+ privateKey: string;
15
+ publicKey: string;
16
+ }>;
17
+ abstract hash(data: Uint8Array): Promise<Uint8Array>;
18
+ abstract randomBytes(length: number): Promise<Uint8Array>;
19
+ }
20
+ /**
21
+ * Clock/timing operations provider
22
+ */
23
+ export declare abstract class ClockProvider {
24
+ abstract now(): number;
25
+ abstract isWithinSkew(timestamp: number, skewSeconds: number): boolean;
26
+ abstract hasExpired(expiresAt: number): boolean;
27
+ abstract calculateExpiry(ttlSeconds: number): number;
28
+ abstract format(timestamp: number): string;
29
+ }
30
+ /**
31
+ * Network fetch operations provider
32
+ */
33
+ export declare abstract class FetchProvider {
34
+ abstract resolveDID(did: string): Promise<any>;
35
+ abstract fetchStatusList(url: string): Promise<any>;
36
+ abstract fetchDelegationChain(id: string): Promise<any[]>;
37
+ abstract fetch(url: string, options?: any): Promise<Response>;
38
+ }
39
+ /**
40
+ * Storage operations provider
41
+ */
42
+ export declare abstract class StorageProvider {
43
+ abstract get(key: string): Promise<string | null>;
44
+ abstract set(key: string, value: string): Promise<void>;
45
+ abstract delete(key: string): Promise<void>;
46
+ abstract exists(key: string): Promise<boolean>;
47
+ abstract list(prefix?: string): Promise<string[]>;
48
+ }
49
+ /**
50
+ * Nonce cache provider
51
+ * Handles replay prevention
52
+ *
53
+ * Nonces should be scoped per agent to prevent cross-agent replay attacks.
54
+ * When agentDid is provided, implementations should use agent-scoped keys.
55
+ */
56
+ export declare abstract class NonceCacheProvider {
57
+ /**
58
+ * Check if a nonce has been used
59
+ * @param nonce - The nonce to check
60
+ * @param agentDid - Optional agent DID for scoping (prevents cross-agent replay attacks)
61
+ */
62
+ abstract has(nonce: string, agentDid?: string): Promise<boolean>;
63
+ /**
64
+ * Add a nonce to the cache
65
+ * @param nonce - The nonce to cache
66
+ * @param ttlSeconds - Time to live in seconds (callers now pass TTL, not absolute timestamp)
67
+ * @param agentDid - Optional agent DID for scoping (prevents cross-agent replay attacks)
68
+ */
69
+ abstract add(nonce: string, ttlSeconds: number, agentDid?: string): Promise<void>;
70
+ abstract cleanup(): Promise<void>;
71
+ abstract destroy(): Promise<void>;
72
+ }
73
+ /**
74
+ * Identity provider for managing agent identities
75
+ */
76
+ export interface AgentIdentity {
77
+ did: string;
78
+ kid: string;
79
+ privateKey: string;
80
+ publicKey: string;
81
+ createdAt: string;
82
+ type: 'development' | 'production';
83
+ metadata?: Record<string, any>;
84
+ }
85
+ export declare abstract class IdentityProvider {
86
+ abstract getIdentity(): Promise<AgentIdentity>;
87
+ abstract saveIdentity(identity: AgentIdentity): Promise<void>;
88
+ abstract rotateKeys(): Promise<AgentIdentity>;
89
+ abstract deleteIdentity(): Promise<void>;
90
+ }
91
+ //# sourceMappingURL=base.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base.d.ts","sourceRoot":"","sources":["base.ts"],"names":[],"mappings":"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"}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base.js","sourceRoot":"","sources":["base.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;GAEG;AACH,MAAM,OAAgB,cAAc;CAMnC;AAED;;GAEG;AACH,MAAM,OAAgB,aAAa;CAMlC;AAED;;GAEG;AACH,MAAM,OAAgB,aAAa;CAKlC;AAED;;GAEG;AACH,MAAM,OAAgB,eAAe;CAMpC;AAED;;;;;;GAMG;AACH,MAAM,OAAgB,kBAAkB;CAkBvC;AAeD,MAAM,OAAgB,gBAAgB;CAKrC"}
@@ -0,0 +1,100 @@
1
+ /**
2
+ * Base Provider Classes
3
+ *
4
+ * Abstract classes that define the provider interfaces for
5
+ * platform-specific implementations.
6
+ */
7
+
8
+ /**
9
+ * Cryptographic operations provider
10
+ */
11
+ export abstract class CryptoProvider {
12
+ abstract sign(data: Uint8Array, privateKey: string): Promise<Uint8Array>;
13
+ abstract verify(data: Uint8Array, signature: Uint8Array, publicKey: string): Promise<boolean>;
14
+ abstract generateKeyPair(): Promise<{ privateKey: string; publicKey: string }>;
15
+ /**
16
+ * Compute SHA-256 hash of data.
17
+ * Returns "sha256:<hex>" format for cross-platform parity.
18
+ */
19
+ abstract hash(data: Uint8Array): Promise<string>;
20
+ abstract randomBytes(length: number): Promise<Uint8Array>;
21
+ }
22
+
23
+ /**
24
+ * Clock/timing operations provider
25
+ */
26
+ export abstract class ClockProvider {
27
+ abstract now(): number;
28
+ abstract isWithinSkew(timestamp: number, skewSeconds: number): boolean;
29
+ abstract hasExpired(expiresAt: number): boolean;
30
+ abstract calculateExpiry(ttlSeconds: number): number;
31
+ abstract format(timestamp: number): string;
32
+ }
33
+
34
+ /**
35
+ * Network fetch operations provider
36
+ */
37
+ export abstract class FetchProvider {
38
+ abstract resolveDID(did: string): Promise<any>;
39
+ abstract fetchStatusList(url: string): Promise<any>;
40
+ abstract fetchDelegationChain(id: string): Promise<any[]>;
41
+ abstract fetch(url: string, options?: any): Promise<Response>;
42
+ }
43
+
44
+ /**
45
+ * Storage operations provider
46
+ */
47
+ export abstract class StorageProvider {
48
+ abstract get(key: string): Promise<string | null>;
49
+ abstract set(key: string, value: string): Promise<void>;
50
+ abstract delete(key: string): Promise<void>;
51
+ abstract exists(key: string): Promise<boolean>;
52
+ abstract list(prefix?: string): Promise<string[]>;
53
+ }
54
+
55
+ /**
56
+ * Nonce cache provider
57
+ * Handles replay prevention
58
+ *
59
+ * Nonces should be scoped per agent to prevent cross-agent replay attacks.
60
+ * When agentDid is provided, implementations should use agent-scoped keys.
61
+ */
62
+ export abstract class NonceCacheProvider {
63
+ /**
64
+ * Check if a nonce has been used
65
+ * @param nonce - The nonce to check
66
+ * @param agentDid - Optional agent DID for scoping (prevents cross-agent replay attacks)
67
+ */
68
+ abstract has(nonce: string, agentDid?: string): Promise<boolean>;
69
+
70
+ /**
71
+ * Add a nonce to the cache
72
+ * @param nonce - The nonce to cache
73
+ * @param ttlSeconds - Time to live in seconds (callers now pass TTL, not absolute timestamp)
74
+ * @param agentDid - Optional agent DID for scoping (prevents cross-agent replay attacks)
75
+ */
76
+ abstract add(nonce: string, ttlSeconds: number, agentDid?: string): Promise<void>;
77
+
78
+ abstract cleanup(): Promise<void>;
79
+ abstract destroy(): Promise<void>;
80
+ }
81
+
82
+ /**
83
+ * Identity provider for managing agent identities
84
+ */
85
+ export interface AgentIdentity {
86
+ did: string;
87
+ kid: string;
88
+ privateKey: string;
89
+ publicKey: string;
90
+ createdAt: string;
91
+ type: 'development' | 'production';
92
+ metadata?: Record<string, any>;
93
+ }
94
+
95
+ export abstract class IdentityProvider {
96
+ abstract getIdentity(): Promise<AgentIdentity>;
97
+ abstract saveIdentity(identity: AgentIdentity): Promise<void>;
98
+ abstract rotateKeys(): Promise<AgentIdentity>;
99
+ abstract deleteIdentity(): Promise<void>;
100
+ }
@@ -0,0 +1,142 @@
1
+ /**
2
+ * Memory-based provider implementations
3
+ *
4
+ * Simple in-memory implementations for development and testing.
5
+ */
6
+
7
+ import {
8
+ StorageProvider,
9
+ NonceCacheProvider,
10
+ IdentityProvider,
11
+ AgentIdentity
12
+ } from './base';
13
+
14
+ /**
15
+ * In-memory storage provider
16
+ */
17
+ export class MemoryStorageProvider extends StorageProvider {
18
+ private store: Map<string, string> = new Map();
19
+
20
+ async get(key: string): Promise<string | null> {
21
+ return this.store.get(key) ?? null;
22
+ }
23
+
24
+ async set(key: string, value: string): Promise<void> {
25
+ this.store.set(key, value);
26
+ }
27
+
28
+ async delete(key: string): Promise<void> {
29
+ this.store.delete(key);
30
+ }
31
+
32
+ async exists(key: string): Promise<boolean> {
33
+ return this.store.has(key);
34
+ }
35
+
36
+ async list(prefix?: string): Promise<string[]> {
37
+ const keys = Array.from(this.store.keys());
38
+ if (prefix) {
39
+ return keys.filter(k => k.startsWith(prefix));
40
+ }
41
+ return keys;
42
+ }
43
+ }
44
+
45
+ /**
46
+ * In-memory nonce cache provider
47
+ */
48
+ export class MemoryNonceCacheProvider extends NonceCacheProvider {
49
+ private nonces: Map<string, number> = new Map();
50
+
51
+ async has(nonce: string, agentDid?: string): Promise<boolean> {
52
+ const key = agentDid ? `nonce:${agentDid}:${nonce}` : `nonce:${nonce}`;
53
+ const expiry = this.nonces.get(key);
54
+ if (!expiry) return false;
55
+
56
+ if (Date.now() > expiry) {
57
+ this.nonces.delete(key);
58
+ return false;
59
+ }
60
+
61
+ return true;
62
+ }
63
+
64
+ async add(nonce: string, ttlSeconds: number, agentDid?: string): Promise<void> {
65
+ const key = agentDid ? `nonce:${agentDid}:${nonce}` : `nonce:${nonce}`;
66
+ // Convert TTL seconds to absolute expiration timestamp for storage
67
+ const expiresAt = Date.now() + (ttlSeconds * 1000);
68
+ this.nonces.set(key, expiresAt);
69
+ }
70
+
71
+ async cleanup(): Promise<void> {
72
+ const now = Date.now();
73
+ for (const [nonce, expiry] of this.nonces) {
74
+ if (now > expiry) {
75
+ this.nonces.delete(nonce);
76
+ }
77
+ }
78
+ }
79
+
80
+ async destroy(): Promise<void> {
81
+ this.nonces.clear();
82
+ }
83
+ }
84
+
85
+ /**
86
+ * In-memory identity provider
87
+ */
88
+ export class MemoryIdentityProvider extends IdentityProvider {
89
+ private identity?: AgentIdentity;
90
+ private cryptoProvider: any;
91
+
92
+ constructor(cryptoProvider?: any) {
93
+ super();
94
+ this.cryptoProvider = cryptoProvider;
95
+ }
96
+
97
+ async getIdentity(): Promise<AgentIdentity> {
98
+ if (!this.identity) {
99
+ this.identity = await this.generateIdentity();
100
+ }
101
+ return this.identity;
102
+ }
103
+
104
+ async saveIdentity(identity: AgentIdentity): Promise<void> {
105
+ this.identity = identity;
106
+ }
107
+
108
+ async rotateKeys(): Promise<AgentIdentity> {
109
+ this.identity = await this.generateIdentity();
110
+ return this.identity;
111
+ }
112
+
113
+ async deleteIdentity(): Promise<void> {
114
+ this.identity = undefined;
115
+ }
116
+
117
+ private async generateIdentity(): Promise<AgentIdentity> {
118
+ if (!this.cryptoProvider) {
119
+ throw new Error('Crypto provider required for identity generation');
120
+ }
121
+
122
+ const keyPair = await this.cryptoProvider.generateKeyPair();
123
+ const did = this.generateDIDFromPublicKey(keyPair.publicKey);
124
+
125
+ return {
126
+ did,
127
+ kid: `${did}#key-1`,
128
+ privateKey: keyPair.privateKey,
129
+ publicKey: keyPair.publicKey,
130
+ createdAt: new Date().toISOString(),
131
+ type: 'development'
132
+ };
133
+ }
134
+
135
+ private generateDIDFromPublicKey(publicKey: string): string {
136
+ // Simplified DID generation
137
+ const keyHash = Buffer.from(publicKey, 'base64')
138
+ .toString('base64url')
139
+ .substring(0, 32);
140
+ return `did:key:z${keyHash}`;
141
+ }
142
+ }