@enbox/agent 0.0.1

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 (348) hide show
  1. package/dist/browser.js +2215 -0
  2. package/dist/browser.js.map +7 -0
  3. package/dist/browser.mjs +2215 -0
  4. package/dist/browser.mjs.map +7 -0
  5. package/dist/cjs/index.js +8530 -0
  6. package/dist/cjs/index.js.map +7 -0
  7. package/dist/cjs/package.json +1 -0
  8. package/dist/esm/agent-did-resolver-cache.js +87 -0
  9. package/dist/esm/agent-did-resolver-cache.js.map +1 -0
  10. package/dist/esm/bearer-identity.js +41 -0
  11. package/dist/esm/bearer-identity.js.map +1 -0
  12. package/dist/esm/connect.js +191 -0
  13. package/dist/esm/connect.js.map +1 -0
  14. package/dist/esm/crypto-api.js +346 -0
  15. package/dist/esm/crypto-api.js.map +1 -0
  16. package/dist/esm/did-api.js +278 -0
  17. package/dist/esm/did-api.js.map +1 -0
  18. package/dist/esm/dwn-api.js +336 -0
  19. package/dist/esm/dwn-api.js.map +1 -0
  20. package/dist/esm/dwn-registrar.js +120 -0
  21. package/dist/esm/dwn-registrar.js.map +1 -0
  22. package/dist/esm/hd-identity-vault.js +729 -0
  23. package/dist/esm/hd-identity-vault.js.map +1 -0
  24. package/dist/esm/identity-api.js +262 -0
  25. package/dist/esm/identity-api.js.map +1 -0
  26. package/dist/esm/index.js +23 -0
  27. package/dist/esm/index.js.map +1 -0
  28. package/dist/esm/local-key-manager.js +498 -0
  29. package/dist/esm/local-key-manager.js.map +1 -0
  30. package/dist/esm/oidc.js +507 -0
  31. package/dist/esm/oidc.js.map +1 -0
  32. package/dist/esm/permissions-api.js +322 -0
  33. package/dist/esm/permissions-api.js.map +1 -0
  34. package/dist/esm/prototyping/clients/dwn-rpc-types.js +2 -0
  35. package/dist/esm/prototyping/clients/dwn-rpc-types.js.map +1 -0
  36. package/dist/esm/prototyping/clients/dwn-server-info-cache-memory.js +74 -0
  37. package/dist/esm/prototyping/clients/dwn-server-info-cache-memory.js.map +1 -0
  38. package/dist/esm/prototyping/clients/http-dwn-rpc-client.js +105 -0
  39. package/dist/esm/prototyping/clients/http-dwn-rpc-client.js.map +1 -0
  40. package/dist/esm/prototyping/clients/json-rpc-socket.js +150 -0
  41. package/dist/esm/prototyping/clients/json-rpc-socket.js.map +1 -0
  42. package/dist/esm/prototyping/clients/json-rpc.js +58 -0
  43. package/dist/esm/prototyping/clients/json-rpc.js.map +1 -0
  44. package/dist/esm/prototyping/clients/server-info-types.js +2 -0
  45. package/dist/esm/prototyping/clients/server-info-types.js.map +1 -0
  46. package/dist/esm/prototyping/clients/web-socket-clients.js +90 -0
  47. package/dist/esm/prototyping/clients/web-socket-clients.js.map +1 -0
  48. package/dist/esm/prototyping/common/object.js +14 -0
  49. package/dist/esm/prototyping/common/object.js.map +1 -0
  50. package/dist/esm/prototyping/common/type-utils.js +2 -0
  51. package/dist/esm/prototyping/common/type-utils.js.map +1 -0
  52. package/dist/esm/prototyping/crypto/algorithms/aes-gcm.js +147 -0
  53. package/dist/esm/prototyping/crypto/algorithms/aes-gcm.js.map +1 -0
  54. package/dist/esm/prototyping/crypto/algorithms/aes-kw.js +137 -0
  55. package/dist/esm/prototyping/crypto/algorithms/aes-kw.js.map +1 -0
  56. package/dist/esm/prototyping/crypto/algorithms/ecdsa.js +307 -0
  57. package/dist/esm/prototyping/crypto/algorithms/ecdsa.js.map +1 -0
  58. package/dist/esm/prototyping/crypto/algorithms/eddsa.js +264 -0
  59. package/dist/esm/prototyping/crypto/algorithms/eddsa.js.map +1 -0
  60. package/dist/esm/prototyping/crypto/algorithms/hkdf.js +39 -0
  61. package/dist/esm/prototyping/crypto/algorithms/hkdf.js.map +1 -0
  62. package/dist/esm/prototyping/crypto/algorithms/pbkdf2.js +41 -0
  63. package/dist/esm/prototyping/crypto/algorithms/pbkdf2.js.map +1 -0
  64. package/dist/esm/prototyping/crypto/crypto-error.js +41 -0
  65. package/dist/esm/prototyping/crypto/crypto-error.js.map +1 -0
  66. package/dist/esm/prototyping/crypto/dsa.js +236 -0
  67. package/dist/esm/prototyping/crypto/dsa.js.map +1 -0
  68. package/dist/esm/prototyping/crypto/jose/jwe-compact.js +130 -0
  69. package/dist/esm/prototyping/crypto/jose/jwe-compact.js.map +1 -0
  70. package/dist/esm/prototyping/crypto/jose/jwe-flattened.js +294 -0
  71. package/dist/esm/prototyping/crypto/jose/jwe-flattened.js.map +1 -0
  72. package/dist/esm/prototyping/crypto/jose/jwe.js +308 -0
  73. package/dist/esm/prototyping/crypto/jose/jwe.js.map +1 -0
  74. package/dist/esm/prototyping/crypto/primitives/aes-gcm.js +352 -0
  75. package/dist/esm/prototyping/crypto/primitives/aes-gcm.js.map +1 -0
  76. package/dist/esm/prototyping/crypto/primitives/aes-kw.js +247 -0
  77. package/dist/esm/prototyping/crypto/primitives/aes-kw.js.map +1 -0
  78. package/dist/esm/prototyping/crypto/primitives/hkdf.js +80 -0
  79. package/dist/esm/prototyping/crypto/primitives/hkdf.js.map +1 -0
  80. package/dist/esm/prototyping/crypto/primitives/pbkdf2.js +85 -0
  81. package/dist/esm/prototyping/crypto/primitives/pbkdf2.js.map +1 -0
  82. package/dist/esm/prototyping/crypto/types/cipher.js +2 -0
  83. package/dist/esm/prototyping/crypto/types/cipher.js.map +1 -0
  84. package/dist/esm/prototyping/crypto/types/crypto-api.js +2 -0
  85. package/dist/esm/prototyping/crypto/types/crypto-api.js.map +1 -0
  86. package/dist/esm/prototyping/crypto/types/key-converter.js +2 -0
  87. package/dist/esm/prototyping/crypto/types/key-converter.js.map +1 -0
  88. package/dist/esm/prototyping/crypto/types/key-deriver.js +2 -0
  89. package/dist/esm/prototyping/crypto/types/key-deriver.js.map +1 -0
  90. package/dist/esm/prototyping/crypto/types/key-io.js +2 -0
  91. package/dist/esm/prototyping/crypto/types/key-io.js.map +1 -0
  92. package/dist/esm/prototyping/crypto/types/key-manager.js +2 -0
  93. package/dist/esm/prototyping/crypto/types/key-manager.js.map +1 -0
  94. package/dist/esm/prototyping/crypto/types/key-wrapper.js +2 -0
  95. package/dist/esm/prototyping/crypto/types/key-wrapper.js.map +1 -0
  96. package/dist/esm/prototyping/crypto/types/params-direct.js +2 -0
  97. package/dist/esm/prototyping/crypto/types/params-direct.js.map +1 -0
  98. package/dist/esm/prototyping/crypto/types/params-kms.js +2 -0
  99. package/dist/esm/prototyping/crypto/types/params-kms.js.map +1 -0
  100. package/dist/esm/prototyping/crypto/utils.js +19 -0
  101. package/dist/esm/prototyping/crypto/utils.js.map +1 -0
  102. package/dist/esm/prototyping/dids/resolver-cache-memory.js +77 -0
  103. package/dist/esm/prototyping/dids/resolver-cache-memory.js.map +1 -0
  104. package/dist/esm/prototyping/dids/utils.js +9 -0
  105. package/dist/esm/prototyping/dids/utils.js.map +1 -0
  106. package/dist/esm/rpc-client.js +123 -0
  107. package/dist/esm/rpc-client.js.map +1 -0
  108. package/dist/esm/store-data-protocols.js +38 -0
  109. package/dist/esm/store-data-protocols.js.map +1 -0
  110. package/dist/esm/store-data.js +320 -0
  111. package/dist/esm/store-data.js.map +1 -0
  112. package/dist/esm/store-did.js +136 -0
  113. package/dist/esm/store-did.js.map +1 -0
  114. package/dist/esm/store-identity.js +140 -0
  115. package/dist/esm/store-identity.js.map +1 -0
  116. package/dist/esm/store-key.js +136 -0
  117. package/dist/esm/store-key.js.map +1 -0
  118. package/dist/esm/sync-api.js +61 -0
  119. package/dist/esm/sync-api.js.map +1 -0
  120. package/dist/esm/sync-engine-level.js +618 -0
  121. package/dist/esm/sync-engine-level.js.map +1 -0
  122. package/dist/esm/test-harness.js +239 -0
  123. package/dist/esm/test-harness.js.map +1 -0
  124. package/dist/esm/types/agent.js +2 -0
  125. package/dist/esm/types/agent.js.map +1 -0
  126. package/dist/esm/types/dwn.js +31 -0
  127. package/dist/esm/types/dwn.js.map +1 -0
  128. package/dist/esm/types/identity-vault.js +2 -0
  129. package/dist/esm/types/identity-vault.js.map +1 -0
  130. package/dist/esm/types/identity.js +2 -0
  131. package/dist/esm/types/identity.js.map +1 -0
  132. package/dist/esm/types/key-manager.js +2 -0
  133. package/dist/esm/types/key-manager.js.map +1 -0
  134. package/dist/esm/types/permissions.js +2 -0
  135. package/dist/esm/types/permissions.js.map +1 -0
  136. package/dist/esm/types/sync.js +2 -0
  137. package/dist/esm/types/sync.js.map +1 -0
  138. package/dist/esm/types/vc.js +5 -0
  139. package/dist/esm/types/vc.js.map +1 -0
  140. package/dist/esm/utils-internal.js +147 -0
  141. package/dist/esm/utils-internal.js.map +1 -0
  142. package/dist/esm/utils.js +161 -0
  143. package/dist/esm/utils.js.map +1 -0
  144. package/dist/types/agent-did-resolver-cache.d.ts +30 -0
  145. package/dist/types/agent-did-resolver-cache.d.ts.map +1 -0
  146. package/dist/types/bearer-identity.d.ts +31 -0
  147. package/dist/types/bearer-identity.d.ts.map +1 -0
  148. package/dist/types/connect.d.ts +88 -0
  149. package/dist/types/connect.d.ts.map +1 -0
  150. package/dist/types/crypto-api.d.ts +286 -0
  151. package/dist/types/crypto-api.d.ts.map +1 -0
  152. package/dist/types/did-api.d.ts +119 -0
  153. package/dist/types/did-api.d.ts.map +1 -0
  154. package/dist/types/dwn-api.d.ts +66 -0
  155. package/dist/types/dwn-api.d.ts.map +1 -0
  156. package/dist/types/dwn-registrar.d.ts +29 -0
  157. package/dist/types/dwn-registrar.d.ts.map +1 -0
  158. package/dist/types/hd-identity-vault.d.ts +306 -0
  159. package/dist/types/hd-identity-vault.d.ts.map +1 -0
  160. package/dist/types/identity-api.d.ts +107 -0
  161. package/dist/types/identity-api.d.ts.map +1 -0
  162. package/dist/types/index.d.ts +30 -0
  163. package/dist/types/index.d.ts.map +1 -0
  164. package/dist/types/local-key-manager.d.ts +311 -0
  165. package/dist/types/local-key-manager.d.ts.map +1 -0
  166. package/dist/types/oidc.d.ts +247 -0
  167. package/dist/types/oidc.d.ts.map +1 -0
  168. package/dist/types/permissions-api.d.ts +35 -0
  169. package/dist/types/permissions-api.d.ts.map +1 -0
  170. package/dist/types/prototyping/clients/dwn-rpc-types.d.ts +45 -0
  171. package/dist/types/prototyping/clients/dwn-rpc-types.d.ts.map +1 -0
  172. package/dist/types/prototyping/clients/dwn-server-info-cache-memory.d.ts +57 -0
  173. package/dist/types/prototyping/clients/dwn-server-info-cache-memory.d.ts.map +1 -0
  174. package/dist/types/prototyping/clients/http-dwn-rpc-client.d.ts +13 -0
  175. package/dist/types/prototyping/clients/http-dwn-rpc-client.d.ts.map +1 -0
  176. package/dist/types/prototyping/clients/json-rpc-socket.d.ts +43 -0
  177. package/dist/types/prototyping/clients/json-rpc-socket.d.ts.map +1 -0
  178. package/dist/types/prototyping/clients/json-rpc.d.ts +49 -0
  179. package/dist/types/prototyping/clients/json-rpc.d.ts.map +1 -0
  180. package/dist/types/prototyping/clients/server-info-types.d.ts +20 -0
  181. package/dist/types/prototyping/clients/server-info-types.d.ts.map +1 -0
  182. package/dist/types/prototyping/clients/web-socket-clients.d.ts +10 -0
  183. package/dist/types/prototyping/clients/web-socket-clients.d.ts.map +1 -0
  184. package/dist/types/prototyping/common/object.d.ts +2 -0
  185. package/dist/types/prototyping/common/object.d.ts.map +1 -0
  186. package/dist/types/prototyping/common/type-utils.d.ts +7 -0
  187. package/dist/types/prototyping/common/type-utils.d.ts.map +1 -0
  188. package/dist/types/prototyping/crypto/algorithms/aes-gcm.d.ts +151 -0
  189. package/dist/types/prototyping/crypto/algorithms/aes-gcm.d.ts.map +1 -0
  190. package/dist/types/prototyping/crypto/algorithms/aes-kw.d.ts +109 -0
  191. package/dist/types/prototyping/crypto/algorithms/aes-kw.d.ts.map +1 -0
  192. package/dist/types/prototyping/crypto/algorithms/ecdsa.d.ts +160 -0
  193. package/dist/types/prototyping/crypto/algorithms/ecdsa.d.ts.map +1 -0
  194. package/dist/types/prototyping/crypto/algorithms/eddsa.d.ts +157 -0
  195. package/dist/types/prototyping/crypto/algorithms/eddsa.d.ts.map +1 -0
  196. package/dist/types/prototyping/crypto/algorithms/hkdf.d.ts +21 -0
  197. package/dist/types/prototyping/crypto/algorithms/hkdf.d.ts.map +1 -0
  198. package/dist/types/prototyping/crypto/algorithms/pbkdf2.d.ts +21 -0
  199. package/dist/types/prototyping/crypto/algorithms/pbkdf2.d.ts.map +1 -0
  200. package/dist/types/prototyping/crypto/crypto-error.d.ts +29 -0
  201. package/dist/types/prototyping/crypto/crypto-error.d.ts.map +1 -0
  202. package/dist/types/prototyping/crypto/dsa.d.ts +169 -0
  203. package/dist/types/prototyping/crypto/dsa.d.ts.map +1 -0
  204. package/dist/types/prototyping/crypto/jose/jwe-compact.d.ts +135 -0
  205. package/dist/types/prototyping/crypto/jose/jwe-compact.d.ts.map +1 -0
  206. package/dist/types/prototyping/crypto/jose/jwe-flattened.d.ts +134 -0
  207. package/dist/types/prototyping/crypto/jose/jwe-flattened.d.ts.map +1 -0
  208. package/dist/types/prototyping/crypto/jose/jwe.d.ts +378 -0
  209. package/dist/types/prototyping/crypto/jose/jwe.d.ts.map +1 -0
  210. package/dist/types/prototyping/crypto/primitives/aes-gcm.d.ts +245 -0
  211. package/dist/types/prototyping/crypto/primitives/aes-gcm.d.ts.map +1 -0
  212. package/dist/types/prototyping/crypto/primitives/aes-kw.d.ts +103 -0
  213. package/dist/types/prototyping/crypto/primitives/aes-kw.d.ts.map +1 -0
  214. package/dist/types/prototyping/crypto/primitives/hkdf.d.ts +90 -0
  215. package/dist/types/prototyping/crypto/primitives/hkdf.d.ts.map +1 -0
  216. package/dist/types/prototyping/crypto/primitives/pbkdf2.d.ts +84 -0
  217. package/dist/types/prototyping/crypto/primitives/pbkdf2.d.ts.map +1 -0
  218. package/dist/types/prototyping/crypto/types/cipher.d.ts +14 -0
  219. package/dist/types/prototyping/crypto/types/cipher.d.ts.map +1 -0
  220. package/dist/types/prototyping/crypto/types/crypto-api.d.ts +35 -0
  221. package/dist/types/prototyping/crypto/types/crypto-api.d.ts.map +1 -0
  222. package/dist/types/prototyping/crypto/types/key-converter.d.ts +49 -0
  223. package/dist/types/prototyping/crypto/types/key-converter.d.ts.map +1 -0
  224. package/dist/types/prototyping/crypto/types/key-deriver.d.ts +50 -0
  225. package/dist/types/prototyping/crypto/types/key-deriver.d.ts.map +1 -0
  226. package/dist/types/prototyping/crypto/types/key-io.d.ts +49 -0
  227. package/dist/types/prototyping/crypto/types/key-io.d.ts.map +1 -0
  228. package/dist/types/prototyping/crypto/types/key-manager.d.ts +69 -0
  229. package/dist/types/prototyping/crypto/types/key-manager.d.ts.map +1 -0
  230. package/dist/types/prototyping/crypto/types/key-wrapper.d.ts +14 -0
  231. package/dist/types/prototyping/crypto/types/key-wrapper.d.ts.map +1 -0
  232. package/dist/types/prototyping/crypto/types/params-direct.d.ts +75 -0
  233. package/dist/types/prototyping/crypto/types/params-direct.d.ts.map +1 -0
  234. package/dist/types/prototyping/crypto/types/params-kms.d.ts +63 -0
  235. package/dist/types/prototyping/crypto/types/params-kms.d.ts.map +1 -0
  236. package/dist/types/prototyping/crypto/utils.d.ts +7 -0
  237. package/dist/types/prototyping/crypto/utils.d.ts.map +1 -0
  238. package/dist/types/prototyping/dids/resolver-cache-memory.d.ts +57 -0
  239. package/dist/types/prototyping/dids/resolver-cache-memory.d.ts.map +1 -0
  240. package/dist/types/prototyping/dids/utils.d.ts +3 -0
  241. package/dist/types/prototyping/dids/utils.d.ts.map +1 -0
  242. package/dist/types/rpc-client.d.ts +51 -0
  243. package/dist/types/rpc-client.d.ts.map +1 -0
  244. package/dist/types/store-data-protocols.d.ts +4 -0
  245. package/dist/types/store-data-protocols.d.ts.map +1 -0
  246. package/dist/types/store-data.d.ts +95 -0
  247. package/dist/types/store-data.d.ts.map +1 -0
  248. package/dist/types/store-did.d.ts +33 -0
  249. package/dist/types/store-did.d.ts.map +1 -0
  250. package/dist/types/store-identity.d.ts +34 -0
  251. package/dist/types/store-identity.d.ts.map +1 -0
  252. package/dist/types/store-key.d.ts +32 -0
  253. package/dist/types/store-key.d.ts.map +1 -0
  254. package/dist/types/sync-api.d.ts +41 -0
  255. package/dist/types/sync-api.d.ts.map +1 -0
  256. package/dist/types/sync-engine-level.d.ts +85 -0
  257. package/dist/types/sync-engine-level.d.ts.map +1 -0
  258. package/dist/types/test-harness.d.ts +69 -0
  259. package/dist/types/test-harness.d.ts.map +1 -0
  260. package/dist/types/types/agent.d.ts +172 -0
  261. package/dist/types/types/agent.d.ts.map +1 -0
  262. package/dist/types/types/dwn.d.ts +178 -0
  263. package/dist/types/types/dwn.d.ts.map +1 -0
  264. package/dist/types/types/identity-vault.d.ts +129 -0
  265. package/dist/types/types/identity-vault.d.ts.map +1 -0
  266. package/dist/types/types/identity.d.ts +16 -0
  267. package/dist/types/types/identity.d.ts.map +1 -0
  268. package/dist/types/types/key-manager.d.ts +9 -0
  269. package/dist/types/types/key-manager.d.ts.map +1 -0
  270. package/dist/types/types/permissions.d.ts +98 -0
  271. package/dist/types/types/permissions.d.ts.map +1 -0
  272. package/dist/types/types/sync.d.ts +66 -0
  273. package/dist/types/types/sync.d.ts.map +1 -0
  274. package/dist/types/types/vc.d.ts +7 -0
  275. package/dist/types/types/vc.d.ts.map +1 -0
  276. package/dist/types/utils-internal.d.ts +50 -0
  277. package/dist/types/utils-internal.d.ts.map +1 -0
  278. package/dist/types/utils.d.ts +37 -0
  279. package/dist/types/utils.d.ts.map +1 -0
  280. package/package.json +112 -0
  281. package/src/agent-did-resolver-cache.ts +95 -0
  282. package/src/bearer-identity.ts +42 -0
  283. package/src/connect.ts +296 -0
  284. package/src/crypto-api.ts +593 -0
  285. package/src/did-api.ts +429 -0
  286. package/src/dwn-api.ts +462 -0
  287. package/src/dwn-registrar.ts +127 -0
  288. package/src/hd-identity-vault.ts +853 -0
  289. package/src/identity-api.ts +324 -0
  290. package/src/index.ts +30 -0
  291. package/src/local-key-manager.ts +672 -0
  292. package/src/oidc.ts +857 -0
  293. package/src/permissions-api.ts +408 -0
  294. package/src/prototyping/clients/dwn-rpc-types.ts +55 -0
  295. package/src/prototyping/clients/dwn-server-info-cache-memory.ts +79 -0
  296. package/src/prototyping/clients/http-dwn-rpc-client.ts +110 -0
  297. package/src/prototyping/clients/json-rpc-socket.ts +169 -0
  298. package/src/prototyping/clients/json-rpc.ts +113 -0
  299. package/src/prototyping/clients/server-info-types.ts +21 -0
  300. package/src/prototyping/clients/web-socket-clients.ts +100 -0
  301. package/src/prototyping/common/object.ts +15 -0
  302. package/src/prototyping/common/type-utils.ts +6 -0
  303. package/src/prototyping/crypto/algorithms/aes-gcm.ts +211 -0
  304. package/src/prototyping/crypto/algorithms/aes-kw.ts +164 -0
  305. package/src/prototyping/crypto/algorithms/ecdsa.ts +365 -0
  306. package/src/prototyping/crypto/algorithms/eddsa.ts +310 -0
  307. package/src/prototyping/crypto/algorithms/hkdf.ts +40 -0
  308. package/src/prototyping/crypto/algorithms/pbkdf2.ts +44 -0
  309. package/src/prototyping/crypto/crypto-error.ts +45 -0
  310. package/src/prototyping/crypto/dsa.ts +367 -0
  311. package/src/prototyping/crypto/jose/jwe-compact.ts +225 -0
  312. package/src/prototyping/crypto/jose/jwe-flattened.ts +459 -0
  313. package/src/prototyping/crypto/jose/jwe.ts +653 -0
  314. package/src/prototyping/crypto/primitives/aes-gcm.ts +374 -0
  315. package/src/prototyping/crypto/primitives/aes-kw.ts +271 -0
  316. package/src/prototyping/crypto/primitives/hkdf.ts +121 -0
  317. package/src/prototyping/crypto/primitives/pbkdf2.ts +116 -0
  318. package/src/prototyping/crypto/types/cipher.ts +17 -0
  319. package/src/prototyping/crypto/types/crypto-api.ts +78 -0
  320. package/src/prototyping/crypto/types/key-converter.ts +53 -0
  321. package/src/prototyping/crypto/types/key-deriver.ts +56 -0
  322. package/src/prototyping/crypto/types/key-io.ts +51 -0
  323. package/src/prototyping/crypto/types/key-manager.ts +83 -0
  324. package/src/prototyping/crypto/types/key-wrapper.ts +17 -0
  325. package/src/prototyping/crypto/types/params-direct.ts +95 -0
  326. package/src/prototyping/crypto/types/params-kms.ts +76 -0
  327. package/src/prototyping/crypto/utils.ts +41 -0
  328. package/src/prototyping/dids/resolver-cache-memory.ts +83 -0
  329. package/src/prototyping/dids/utils.ts +10 -0
  330. package/src/rpc-client.ts +162 -0
  331. package/src/store-data-protocols.ts +40 -0
  332. package/src/store-data.ts +400 -0
  333. package/src/store-did.ts +105 -0
  334. package/src/store-identity.ts +109 -0
  335. package/src/store-key.ts +104 -0
  336. package/src/sync-api.ts +71 -0
  337. package/src/sync-engine-level.ts +714 -0
  338. package/src/test-harness.ts +330 -0
  339. package/src/types/agent.ts +195 -0
  340. package/src/types/dwn.ts +278 -0
  341. package/src/types/identity-vault.ts +137 -0
  342. package/src/types/identity.ts +18 -0
  343. package/src/types/key-manager.ts +15 -0
  344. package/src/types/permissions.ts +115 -0
  345. package/src/types/sync.ts +58 -0
  346. package/src/types/vc.ts +7 -0
  347. package/src/utils-internal.ts +157 -0
  348. package/src/utils.ts +181 -0
@@ -0,0 +1,672 @@
1
+ import type {
2
+ Jwk,
3
+ Cipher,
4
+ Signer,
5
+ KeyWrapper,
6
+ SignParams,
7
+ AesGcmParams,
8
+ KeyGenerator,
9
+ VerifyParams,
10
+ KeyIdentifier,
11
+ KmsSignParams,
12
+ KmsDigestParams,
13
+ KmsVerifyParams,
14
+ GetPublicKeyParams,
15
+ KmsExportKeyParams,
16
+ KmsGetKeyUriParams,
17
+ KmsImportKeyParams,
18
+ KmsGenerateKeyParams,
19
+ KmsGetPublicKeyParams,
20
+ AsymmetricKeyGenerator,
21
+ } from '@enbox/crypto';
22
+
23
+ import {
24
+ isPrivateJwk,
25
+ Sha2Algorithm,
26
+ EcdsaAlgorithm,
27
+ EdDsaAlgorithm,
28
+ AesGcmAlgorithm,
29
+ CryptoAlgorithm,
30
+ KEY_URI_PREFIX_JWK,
31
+ computeJwkThumbprint,
32
+ } from '@enbox/crypto';
33
+
34
+ import type { AgentDataStore } from './store-data.js';
35
+ import type { Web5PlatformAgent } from './types/agent.js';
36
+ import type { AgentKeyManager } from './types/key-manager.js';
37
+ import type { InferType } from './prototyping/common/type-utils.js';
38
+ import type { CipherParams, UnwrapKeyParams, WrapKeyParams } from './prototyping/crypto/types/params-direct.js';
39
+ import type { KmsCipherParams, KmsUnwrapKeyParams, KmsWrapKeyParams } from './prototyping/crypto/types/params-kms.js';
40
+
41
+ import { InMemoryKeyStore } from './store-key.js';
42
+ import { AesKwAlgorithm } from './prototyping/crypto/algorithms/aes-kw.js';
43
+ import { CryptoError, CryptoErrorCode } from './prototyping/crypto/crypto-error.js';
44
+
45
+ /**
46
+ * `supportedAlgorithms` is an object mapping algorithm names to their respective implementations
47
+ * Each entry in this map specifies the algorithm name and its associated properties, including the
48
+ * implementation class and any relevant names or identifiers for the algorithm. This structure
49
+ * allows for easy retrieval and instantiation of algorithm implementations based on the algorithm
50
+ * name or key specification. It facilitates the support of multiple algorithms within the
51
+ * `LocalKeyManager` class.
52
+ */
53
+ const supportedAlgorithms = {
54
+ 'AES-GCM': {
55
+ implementation : AesGcmAlgorithm,
56
+ names : ['A128GCM', 'A192GCM', 'A256GCM'] as const,
57
+ },
58
+ 'AES-KW': {
59
+ implementation : AesKwAlgorithm,
60
+ names : ['A128KW', 'A192KW', 'A256KW'] as const,
61
+ },
62
+ 'Ed25519': {
63
+ implementation : EdDsaAlgorithm,
64
+ names : ['Ed25519'] as const,
65
+ },
66
+ 'secp256k1': {
67
+ implementation : EcdsaAlgorithm,
68
+ names : ['ES256K', 'secp256k1'] as const,
69
+ },
70
+ 'secp256r1': {
71
+ implementation : EcdsaAlgorithm,
72
+ names : ['ES256', 'secp256r1'] as const,
73
+ },
74
+ 'SHA-256': {
75
+ implementation : Sha2Algorithm,
76
+ names : ['SHA-256'] as const
77
+ }
78
+ } satisfies {
79
+ [key: string]: {
80
+ implementation : typeof CryptoAlgorithm;
81
+ names : readonly string[];
82
+ }
83
+ };
84
+
85
+ /* Helper type for `supportedAlgorithms`. */
86
+ type SupportedAlgorithm = keyof typeof supportedAlgorithms;
87
+
88
+ /* Helper type for `supportedAlgorithms` implementations. */
89
+ type AlgorithmConstructor = typeof supportedAlgorithms[SupportedAlgorithm]['implementation'];
90
+
91
+ /* Commented out but retaining in case it ends up being useful. */
92
+ // type AlgorithmNames = typeof supportedAlgorithms[SupportedAlgorithm]['names'][number];
93
+
94
+ /* Helper type for supported key generator algorithms. */
95
+ type SupportedKeyGeneratorAlgorithm =
96
+ | 'Ed25519' // Edwards Curve Digital Signature Algorithm (EdDSA)
97
+ | 'secp256k1' | 'ES256K' | 'secp256r1' | 'ES256' // Elliptic Curve Digital Signature Algorithm (ECDSA)
98
+ | 'A128GCM' | 'A192GCM' | 'A256GCM' // AES GCM with a 128-bit, 192-bit, or 256-bit key
99
+ | 'A128KW' | 'A192KW' | 'A256KW'; // AES Key Wrap with a 128-bit, 192-bit, or 256-bit key
100
+
101
+ /**
102
+ * The `LocalKmsParams` interface specifies the parameters for initializing an instance of
103
+ * {@link LocalKeyManager}. It allows the optional inclusion of a {@link AgentDataStore} instance
104
+ * for key management. If not provided, a default {@link InMemoryKeyStore} instance will be used for
105
+ * storing keys. Note that the {@link InMemoryKeyStore} is not persistent and will be cleared when
106
+ * the application exits.
107
+ */
108
+ export type LocalKmsParams = {
109
+ agent?: Web5PlatformAgent;
110
+
111
+ /**
112
+ * An optional property to specify a custom {@link AgentDataStore} instance for key management. If
113
+ * not provided, {@link LocalKeyManager} uses a default {@link InMemoryKeyStore} instance. This
114
+ * store is responsible for managing cryptographic keys, allowing them to be retrieved, stored,
115
+ * and managed during cryptographic operations.
116
+ */
117
+ keyStore?: AgentDataStore<Jwk>;
118
+ };
119
+
120
+ /**
121
+ * The `LocalKmsGenerateKeyParams` interface defines the algorithm-specific parameters that
122
+ * should be passed into the {@link LocalKeyManager.generateKey | `LocalKeyManager.generateKey()`}
123
+ * method when generating a key in the local KMS.
124
+ */
125
+ export interface LocalKmsGenerateKeyParams extends KmsGenerateKeyParams {
126
+ /**
127
+ * A string defining the type of key to generate.
128
+ */
129
+ algorithm: InferType<SupportedKeyGeneratorAlgorithm>
130
+ }
131
+
132
+ /**
133
+ * The `LocalKmsUnwrapKeyParams` interface defines the algorithm-specific parameters that
134
+ * should be passed into the {@link LocalKeyManager.wrapKey} method when wrapping a key using a
135
+ * key stored in the local KMS to encrypt the key material.
136
+ */
137
+ export interface LocalKmsUnwrapKeyParams extends KmsUnwrapKeyParams {
138
+ /**
139
+ * A string defining the type of wrapped key. The value must be one of the following:
140
+ * - `"A128GCM"`: AES GCM using a 128-bit key.
141
+ * - `"A192GCM"`: AES GCM using a 192-bit key.
142
+ * - `"A256GCM"`: AES GCM using a 256-bit key.
143
+ * - `"A128KW"`: AES Key Wrap using a 128-bit key.
144
+ * - `"A192KW"`: AES Key Wrap using a 192-bit key.
145
+ * - `"A256KW"`: AES Key Wrap using a 256-bit key.
146
+ */
147
+ wrappedKeyAlgorithm: 'A128GCM' | 'A192GCM' | 'A256GCM' | 'A128KW' | 'A192KW' | 'A256KW';
148
+ }
149
+
150
+ export class LocalKeyManager implements AgentKeyManager {
151
+ /**
152
+ * Holds the instance of a `Web5PlatformAgent` that represents the current execution context for
153
+ * the `LocalKeyManager`. This agent is used to interact with other Web5 agent components. It's
154
+ * vital to ensure this instance is set to correctly contextualize operations within the broader
155
+ * Web5 Agent framework.
156
+ */
157
+ private _agent?: Web5PlatformAgent;
158
+
159
+ /**
160
+ * A private map that stores instances of cryptographic algorithm implementations. Each key in
161
+ * this map is an `AlgorithmConstructor`, and its corresponding value is an instance of a class
162
+ * that implements a specific cryptographic algorithm. This map is used to cache and reuse
163
+ * instances for performance optimization, ensuring that each algorithm is instantiated only once.
164
+ */
165
+ private _algorithmInstances: Map<AlgorithmConstructor, InstanceType<typeof CryptoAlgorithm>> = new Map();
166
+
167
+ /**
168
+ * The `_keyStore` private variable in `LocalKeyManager` is a {@link AgentDataStore} instance used
169
+ * for storing and managing cryptographic keys. It allows the `LocalKeyManager` class to save,
170
+ * retrieve, and handle keys efficiently within the local Key Management System (KMS) context.
171
+ * This variable can be configured to use different storage backends, like in-memory storage or
172
+ * persistent storage, providing flexibility in key management according to the application's
173
+ * requirements.
174
+ */
175
+ private _keyStore: AgentDataStore<Jwk>;
176
+
177
+ constructor({ agent, keyStore }: LocalKmsParams = {}) {
178
+ this._agent = agent;
179
+
180
+ this._keyStore = keyStore ?? new InMemoryKeyStore();
181
+ }
182
+
183
+ /**
184
+ * Retrieves the `Web5PlatformAgent` execution context.
185
+ *
186
+ * @returns The `Web5PlatformAgent` instance that represents the current execution context.
187
+ * @throws Will throw an error if the `agent` instance property is undefined.
188
+ */
189
+ get agent(): Web5PlatformAgent {
190
+ if (this._agent === undefined) {
191
+ throw new Error('LocalKeyManager: Unable to determine agent execution context.');
192
+ }
193
+
194
+ return this._agent;
195
+ }
196
+
197
+ set agent(agent: Web5PlatformAgent) {
198
+ this._agent = agent;
199
+ }
200
+
201
+ public async decrypt({ keyUri, ...params }:
202
+ KmsCipherParams & AesGcmParams
203
+ ): Promise<Uint8Array> {
204
+ // Get the private key from the key store.
205
+ const privateKey = await this.getPrivateKey({ keyUri });
206
+
207
+ // Determine the algorithm name based on the JWK's `alg` property.
208
+ const algorithm = this.getAlgorithmName({ key: privateKey });
209
+
210
+ // Get the cipher algorithm based on the algorithm name.
211
+ const cipher = this.getAlgorithm({ algorithm }) as Cipher<CipherParams, CipherParams>;
212
+
213
+ // Encrypt the data.
214
+ const ciphertext = await cipher.decrypt({ key: privateKey, ...params });
215
+
216
+ return ciphertext;
217
+ }
218
+
219
+ digest(_params: KmsDigestParams): Promise<Uint8Array> {
220
+ throw new Error('Method not implemented.');
221
+ }
222
+
223
+ public async encrypt({ keyUri, ...params }:
224
+ KmsCipherParams & AesGcmParams
225
+ ): Promise<Uint8Array> {
226
+ // Get the private key from the key store.
227
+ const privateKey = await this.getPrivateKey({ keyUri });
228
+
229
+ // Determine the algorithm name based on the JWK's `alg` property.
230
+ const algorithm = this.getAlgorithmName({ key: privateKey });
231
+
232
+ // Get the cipher algorithm based on the algorithm name.
233
+ const cipher = this.getAlgorithm({ algorithm }) as Cipher<CipherParams, CipherParams>;
234
+
235
+ // Encrypt the data.
236
+ const ciphertext = await cipher.encrypt({ key: privateKey, ...params });
237
+
238
+ return ciphertext;
239
+ }
240
+
241
+ /**
242
+ * Exports a private key identified by the provided key URI from the local KMS.
243
+ *
244
+ * @remarks
245
+ * This method retrieves the key from the key store and returns it. It is primarily used
246
+ * for extracting keys for backup or transfer purposes.
247
+ *
248
+ * @example
249
+ * ```ts
250
+ * const keyManager = new LocalKeyManager();
251
+ * const keyUri = await keyManager.generateKey({ algorithm: 'Ed25519' });
252
+ * const privateKey = await keyManager.exportKey({ keyUri });
253
+ * ```
254
+ *
255
+ * @param params - Parameters for exporting the key.
256
+ * @param params.keyUri - The key URI identifying the key to export.
257
+ *
258
+ * @returns A Promise resolving to the JWK representation of the exported key.
259
+ */
260
+ public async exportKey({ keyUri }:
261
+ KmsExportKeyParams
262
+ ): Promise<Jwk> {
263
+ // Get the private key from the key store.
264
+ const privateKey = await this.getPrivateKey({ keyUri });
265
+
266
+ return privateKey;
267
+ }
268
+
269
+ /**
270
+ * Generates a new cryptographic key in the local KMS with the specified algorithm and returns a
271
+ * unique key URI which can be used to reference the key in subsequent operations.
272
+ *
273
+ * @example
274
+ * ```ts
275
+ * const keyManager = new LocalKeyManager();
276
+ * const keyUri = await keyManager.generateKey({ algorithm: 'Ed25519' });
277
+ * console.log(keyUri); // Outputs the key URI
278
+ * ```
279
+ *
280
+ * @param params - The parameters for key generation.
281
+ * @param params.algorithm - The algorithm to use for key generation, defined in `SupportedAlgorithm`.
282
+ *
283
+ * @returns A Promise that resolves to the key URI, a unique identifier for the generated key.
284
+ */
285
+ public async generateKey({ algorithm: algorithmIdentifier }:
286
+ LocalKmsGenerateKeyParams
287
+ ): Promise<KeyIdentifier> {
288
+ // Determine the algorithm name based on the given algorithm identifier.
289
+ const algorithm = this.getAlgorithmName({ key: { alg: algorithmIdentifier } });
290
+
291
+ // Get the key generator implementation based on the algorithm.
292
+ const keyGenerator = this.getAlgorithm({ algorithm }) as KeyGenerator<LocalKmsGenerateKeyParams, Jwk>;
293
+
294
+ // Generate the key.
295
+ const privateKey = await keyGenerator.generateKey({ algorithm: algorithmIdentifier });
296
+
297
+ // If the key ID is undefined, set it to the JWK thumbprint.
298
+ privateKey.kid ??= await computeJwkThumbprint({ jwk: privateKey });
299
+
300
+ // Compute the key URI for the key.
301
+ const keyUri = await this.getKeyUri({ key: privateKey });
302
+
303
+ // Store the key in the key store.
304
+ await this._keyStore.set({
305
+ id : keyUri,
306
+ data : privateKey,
307
+ agent : this.agent,
308
+ preventDuplicates : false,
309
+ useCache : true
310
+ });
311
+
312
+ return keyUri;
313
+ }
314
+
315
+ /**
316
+ * Computes the Key URI for a given public JWK (JSON Web Key).
317
+ *
318
+ * @remarks
319
+ * This method generates a {@link https://datatracker.ietf.org/doc/html/rfc3986 | URI}
320
+ * (Uniform Resource Identifier) for the given JWK, which uniquely identifies the key across all
321
+ * `CryptoApi` implementations. The key URI is constructed by appending the
322
+ * {@link https://datatracker.ietf.org/doc/html/rfc7638 | JWK thumbprint} to the prefix
323
+ * `urn:jwk:`. The JWK thumbprint is deterministically computed from the JWK and is consistent
324
+ * regardless of property order or optional property inclusion in the JWK. This ensures that the
325
+ * same key material represented as a JWK will always yield the same thumbprint, and therefore,
326
+ * the same key URI.
327
+ *
328
+ * @example
329
+ * ```ts
330
+ * const keyManager = new LocalKeyManager();
331
+ * const keyUri = await keyManager.generateKey({ algorithm: 'Ed25519' });
332
+ * const publicKey = await keyManager.getPublicKey({ keyUri });
333
+ * const keyUriFromPublicKey = await keyManager.getKeyUri({ key: publicKey });
334
+ * console.log(keyUri === keyUriFromPublicKey); // Outputs `true`
335
+ * ```
336
+ *
337
+ * @param params - The parameters for getting the key URI.
338
+ * @param params.key - The JWK for which to compute the key URI.
339
+ *
340
+ * @returns A Promise that resolves to the key URI as a string.
341
+ */
342
+ public async getKeyUri({ key }:
343
+ KmsGetKeyUriParams
344
+ ): Promise<KeyIdentifier> {
345
+ // Compute the JWK thumbprint.
346
+ const jwkThumbprint = await computeJwkThumbprint({ jwk: key });
347
+
348
+ // Construct the key URI by appending the JWK thumbprint to the key URI prefix.
349
+ const keyUri = `${KEY_URI_PREFIX_JWK}${jwkThumbprint}`;
350
+
351
+ return keyUri;
352
+ }
353
+
354
+ /**
355
+ * Retrieves the public key associated with a previously generated private key, identified by
356
+ * the provided key URI.
357
+ *
358
+ * @example
359
+ * ```ts
360
+ * const keyManager = new LocalKeyManager();
361
+ * const keyUri = await keyManager.generateKey({ algorithm: 'Ed25519' });
362
+ * const publicKey = await keyManager.getPublicKey({ keyUri });
363
+ * ```
364
+ *
365
+ * @param params - The parameters for retrieving the public key.
366
+ * @param params.keyUri - The key URI of the private key to retrieve the public key for.
367
+ *
368
+ * @returns A Promise that resolves to the public key in JWK format.
369
+ */
370
+ public async getPublicKey({ keyUri }:
371
+ KmsGetPublicKeyParams
372
+ ): Promise<Jwk> {
373
+ // Get the private key from the key store.
374
+ const privateKey = await this.getPrivateKey({ keyUri });
375
+
376
+ // Determine the algorithm name based on the JWK's `alg` and `crv` properties.
377
+ const algorithm = this.getAlgorithmName({ key: privateKey });
378
+
379
+ // Get the key generator based on the algorithm name.
380
+ const keyGenerator = this.getAlgorithm({ algorithm }) as AsymmetricKeyGenerator<LocalKmsGenerateKeyParams, Jwk, GetPublicKeyParams>;
381
+
382
+ // Get the public key properties from the private JWK.
383
+ const publicKey = await keyGenerator.getPublicKey({ key: privateKey });
384
+
385
+ return publicKey;
386
+ }
387
+
388
+ /**
389
+ * Imports a private key into the local KMS.
390
+ *
391
+ * @remarks
392
+ * This method stores the provided JWK in the key store, making it available for subsequent
393
+ * cryptographic operations. It is particularly useful for initializing the KMS with pre-existing
394
+ * keys or for restoring keys from backups.
395
+ *
396
+ * Note that, if defined, the `kid` (key ID) property of the JWK is used as the key URI for the
397
+ * imported key. If the `kid` property is not provided, the key URI is computed from the JWK
398
+ * thumbprint of the key.
399
+ *
400
+ * @example
401
+ * ```ts
402
+ * const keyManager = new LocalKeyManager();
403
+ * const privateKey = { ... } // A private key in JWK format
404
+ * const keyUri = await keyManager.importKey({ key: privateKey });
405
+ * ```
406
+ *
407
+ * @param params - Parameters for importing the key.
408
+ * @param params.key - The private key to import to in JWK format.
409
+ *
410
+ * @returns A Promise resolving to the key URI, uniquely identifying the imported key.
411
+ */
412
+ public async importKey({ key }:
413
+ KmsImportKeyParams
414
+ ): Promise<KeyIdentifier> {
415
+ if (!isPrivateJwk(key)) throw new TypeError('Invalid key provided. Must be a private key in JWK format.');
416
+
417
+ // Make a deep copy of the key to avoid mutating the original.
418
+ const privateKey = structuredClone(key);
419
+
420
+ // If the key ID is undefined, set it to the JWK thumbprint.
421
+ privateKey.kid ??= await computeJwkThumbprint({ jwk: privateKey });
422
+
423
+ // Compute the key URI for the key.
424
+ const keyUri = await this.getKeyUri({ key: privateKey });
425
+
426
+ // Store the key in the key store.
427
+ await this._keyStore.set({
428
+ id : keyUri,
429
+ data : privateKey,
430
+ agent : this.agent,
431
+ preventDuplicates : true,
432
+ useCache : true
433
+ });
434
+
435
+ return keyUri;
436
+ }
437
+
438
+ /**
439
+ * Signs the provided data using the private key identified by the provided key URI.
440
+ *
441
+ * @remarks
442
+ * This method uses the signature algorithm determined by the `alg` and/or `crv` properties of the
443
+ * private key identified by the provided key URI to sign the provided data. The signature can
444
+ * later be verified by parties with access to the corresponding public key, ensuring that the
445
+ * data has not been tampered with and was indeed signed by the holder of the private key.
446
+ *
447
+ * @example
448
+ * ```ts
449
+ * const keyManager = new LocalKeyManager();
450
+ * const keyUri = await keyManager.generateKey({ algorithm: 'Ed25519' });
451
+ * const data = new TextEncoder().encode('Message to sign');
452
+ * const signature = await keyManager.sign({ keyUri, data });
453
+ * ```
454
+ *
455
+ * @param params - The parameters for the signing operation.
456
+ * @param params.keyUri - The key URI of the private key to use for signing.
457
+ * @param params.data - The data to sign.
458
+ *
459
+ * @returns A Promise resolving to the digital signature as a `Uint8Array`.
460
+ */
461
+ public async sign({ keyUri, data }:
462
+ KmsSignParams
463
+ ): Promise<Uint8Array> {
464
+ // Get the private key from the key store.
465
+ const privateKey = await this.getPrivateKey({ keyUri });
466
+
467
+ // Determine the algorithm name based on the JWK's `alg` and `crv` properties.
468
+ const algorithm = this.getAlgorithmName({ key: privateKey });
469
+
470
+ // Get the signature algorithm based on the algorithm name.
471
+ const signer = this.getAlgorithm({ algorithm }) as Signer<SignParams, VerifyParams>;
472
+
473
+ // Sign the data.
474
+ const signature = signer.sign({ data, key: privateKey });
475
+
476
+ return signature;
477
+ }
478
+
479
+ public async unwrapKey({ wrappedKeyBytes, wrappedKeyAlgorithm, decryptionKeyUri }:
480
+ LocalKmsUnwrapKeyParams
481
+ ): Promise<Jwk> {
482
+ // Get the private key from the key store.
483
+ const decryptionKey = await this.getPrivateKey({ keyUri: decryptionKeyUri });
484
+
485
+ // Determine the algorithm name based on the JWK's `alg` property.
486
+ const algorithm = this.getAlgorithmName({ key: decryptionKey });
487
+
488
+ // Get the key wrapping algorithm based on the algorithm name.
489
+ const keyWrapper = this.getAlgorithm({ algorithm }) as KeyWrapper<WrapKeyParams, UnwrapKeyParams>;
490
+
491
+ // Decrypt the key.
492
+ const unwrappedKey = await keyWrapper.unwrapKey({ wrappedKeyBytes, wrappedKeyAlgorithm, decryptionKey });
493
+
494
+ return unwrappedKey;
495
+ }
496
+
497
+ /**
498
+ * Verifies a digital signature associated the provided data using the provided key.
499
+ *
500
+ * @remarks
501
+ * This method uses the signature algorithm determined by the `alg` and/or `crv` properties of the
502
+ * provided key to check the validity of a digital signature against the original data. It
503
+ * confirms whether the signature was created by the holder of the corresponding private key and
504
+ * that the data has not been tampered with.
505
+ *
506
+ * @example
507
+ * ```ts
508
+ * const keyManager = new LocalKeyManager();
509
+ * const keyUri = await keyManager.generateKey({ algorithm: 'Ed25519' });
510
+ * const data = new TextEncoder().encode('Message to sign');
511
+ * const signature = await keyManager.sign({ keyUri, data });
512
+ * const isSignatureValid = await keyManager.verify({ keyUri, data, signature });
513
+ * ```
514
+ *
515
+ * @param params - The parameters for the verification operation.
516
+ * @param params.key - The key to use for verification.
517
+ * @param params.signature - The signature to verify.
518
+ * @param params.data - The data to verify.
519
+ *
520
+ * @returns A Promise resolving to a boolean indicating whether the signature is valid.
521
+ */
522
+ public async verify({ key, signature, data }:
523
+ KmsVerifyParams
524
+ ): Promise<boolean> {
525
+ // Determine the algorithm name based on the JWK's `alg` and `crv` properties.
526
+ const algorithm = this.getAlgorithmName({ key });
527
+
528
+ // Get the signature algorithm based on the algorithm name.
529
+ const signer = this.getAlgorithm({ algorithm }) as Signer<SignParams, VerifyParams>;
530
+
531
+ // Verify the signature.
532
+ const isSignatureValid = signer.verify({ key, signature, data });
533
+
534
+ return isSignatureValid;
535
+ }
536
+
537
+ public async wrapKey({ unwrappedKey, encryptionKeyUri }:
538
+ KmsWrapKeyParams
539
+ ): Promise<Uint8Array> {
540
+ // Get the private key from the key store.
541
+ const encryptionKey = await this.getPrivateKey({ keyUri: encryptionKeyUri });
542
+
543
+ // Determine the algorithm name based on the JWK's `alg` property.
544
+ const algorithm = this.getAlgorithmName({ key: encryptionKey });
545
+
546
+ // Get the key wrapping algorithm based on the algorithm name.
547
+ const keyWrapper = this.getAlgorithm({ algorithm }) as KeyWrapper<WrapKeyParams, UnwrapKeyParams>;
548
+
549
+ // Encrypt the key.
550
+ const wrappedKeyBytes = await keyWrapper.wrapKey({ unwrappedKey, encryptionKey });
551
+
552
+ return wrappedKeyBytes;
553
+ }
554
+
555
+ public async deleteKey({ keyUri }:{ keyUri: KeyIdentifier }): Promise<void> {
556
+ // Get the private key from the key store.
557
+ const jwk = await this._keyStore.get({ id: keyUri, agent: this.agent, useCache: true });
558
+ if (!jwk) {
559
+ throw new Error(`Key not found: ${keyUri}`);
560
+ }
561
+
562
+ await this._keyStore.delete({ id: keyUri, agent: this.agent });
563
+ }
564
+
565
+ /**
566
+ * Retrieves an algorithm implementation instance based on the provided algorithm name.
567
+ *
568
+ * @remarks
569
+ * This method checks if the requested algorithm is supported and returns a cached instance
570
+ * if available. If an instance does not exist, it creates and caches a new one. This approach
571
+ * optimizes performance by reusing algorithm instances across cryptographic operations.
572
+ *
573
+ * @example
574
+ * ```ts
575
+ * const signer = this.getAlgorithm({ algorithm: 'Ed25519' });
576
+ * ```
577
+ *
578
+ * @param params - The parameters for retrieving the algorithm implementation.
579
+ * @param params.algorithm - The name of the algorithm to retrieve.
580
+ *
581
+ * @returns An instance of the requested algorithm implementation.
582
+ *
583
+ * @throws Error if the requested algorithm is not supported.
584
+ */
585
+ private getAlgorithm({ algorithm }: {
586
+ algorithm: SupportedAlgorithm;
587
+ }): InstanceType<typeof CryptoAlgorithm> {
588
+ // Check if algorithm is supported.
589
+ const AlgorithmImplementation = supportedAlgorithms[algorithm]?.['implementation'];
590
+ if (!AlgorithmImplementation) {
591
+ throw new CryptoError(CryptoErrorCode.AlgorithmNotSupported, `Algorithm not supported: ${algorithm}`);
592
+ }
593
+
594
+ // Check if instance already exists for the `AlgorithmImplementation`.
595
+ if (!this._algorithmInstances.has(AlgorithmImplementation)) {
596
+ // If not, create a new instance and store it in the cache
597
+ this._algorithmInstances.set(AlgorithmImplementation, new AlgorithmImplementation());
598
+ }
599
+
600
+ // Return the cached instance
601
+ return this._algorithmInstances.get(AlgorithmImplementation)!;
602
+ }
603
+
604
+ /**
605
+ * Determines the algorithm name based on the key's properties.
606
+ *
607
+ * @remarks
608
+ * This method facilitates the identification of the correct algorithm for cryptographic
609
+ * operations based on the `alg` or `crv` properties of a {@link Jwk | JWK}.
610
+ *
611
+ * @example
612
+ * ```ts
613
+ * const publicKey = { ... }; // Public key in JWK format
614
+ * const algorithm = this.getAlgorithmName({ key: publicKey });
615
+ * ```
616
+ *
617
+ * @param params - The parameters for determining the algorithm name.
618
+ * @param params.key - A JWK containing the `alg` or `crv` properties.
619
+ *
620
+ * @returns The algorithm name associated with the key.
621
+ *
622
+ * @throws Error if the algorithm name cannot be determined from the provided input.
623
+ */
624
+ private getAlgorithmName({ key }: {
625
+ key: { alg?: string, crv?: string };
626
+ }): SupportedAlgorithm {
627
+ const algProperty = key.alg;
628
+ const crvProperty = key.crv;
629
+
630
+ for (const algorithmIdentifier of Object.keys(supportedAlgorithms) as SupportedAlgorithm[]) {
631
+ const algorithmNames = supportedAlgorithms[algorithmIdentifier].names as readonly string[];
632
+ if (algProperty && algorithmNames.includes(algProperty)) {
633
+ return algorithmIdentifier;
634
+ } else if (crvProperty && algorithmNames.includes(crvProperty)) {
635
+ return algorithmIdentifier;
636
+ }
637
+ }
638
+
639
+ throw new CryptoError(CryptoErrorCode.AlgorithmNotSupported,
640
+ `Algorithm not supported based on provided input: alg=${algProperty}, crv=${crvProperty}. ` +
641
+ 'Please check the documentation for the list of supported algorithms.'
642
+ );
643
+ }
644
+
645
+ /**
646
+ * Retrieves a private key from the key store based on the provided key URI.
647
+ *
648
+ * @example
649
+ * ```ts
650
+ * const privateKey = this.getPrivateKey({ keyUri: 'urn:jwk:...' });
651
+ * ```
652
+ *
653
+ * @param params - Parameters for retrieving the private key.
654
+ * @param params.keyUri - The key URI identifying the private key to retrieve.
655
+ *
656
+ * @returns A Promise resolving to the JWK representation of the private key.
657
+ *
658
+ * @throws Error if the key is not found in the key store.
659
+ */
660
+ private async getPrivateKey({ keyUri }: {
661
+ keyUri: KeyIdentifier;
662
+ }): Promise<Jwk> {
663
+ // Get the private key from the key store.
664
+ const privateKey = await this._keyStore.get({ id: keyUri, agent: this.agent, useCache: true });
665
+
666
+ if (!privateKey) {
667
+ throw new Error(`Key not found: ${keyUri}`);
668
+ }
669
+
670
+ return privateKey;
671
+ }
672
+ }