@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,653 @@
1
+ import type { JoseHeaderParams, Jwk, KeyIdentifier } from '@enbox/crypto';
2
+
3
+ import { Convert } from '@enbox/common';
4
+
5
+ import type { CryptoApi } from '../types/crypto-api.js';
6
+ import type { KeyManager } from '../types/key-manager.js';
7
+
8
+ import { CryptoError, CryptoErrorCode } from '../crypto-error.js';
9
+
10
+ /**
11
+ * Specifies options for decrypting a JWE, allowing the caller to define constraints on the JWE
12
+ * decryption process, particularly regarding the algorithms used.
13
+ *
14
+ * These options ensure that only expected and permitted algorithms are utilized during the
15
+ * decryption, enhancing security by preventing unexpected algorithm usage.
16
+ */
17
+ export interface JweDecryptOptions {
18
+ /**
19
+ * The allowed "alg" (Algorithm) Header Parameter values.
20
+ *
21
+ * These values specify the cryptographic algorithms that are permissible for decrypting
22
+ * the Content Encryption Key (CEK) or for key agreement to determine the CEK.
23
+ *
24
+ * Note: If not specified, all algorithm values are considered allowed, which might not be
25
+ * desirable in all contexts.
26
+ */
27
+ allowedAlgValues?: string[];
28
+
29
+ /**
30
+ * The allowed "enc" (Encryption) Header Parameter values.
31
+ *
32
+ * These values determine the cryptographic algorithms that can be used for decrypting the
33
+ * ciphertext and protecting the integrity of the plaintext and Additional Authenticated Data.
34
+ *
35
+ * Note: If left unspecified, it implies that all encryption algorithms are acceptable, which may
36
+ * not be secure in every scenario.
37
+ *
38
+ */
39
+ allowedEncValues?: string[];
40
+ }
41
+
42
+ /**
43
+ * Placeholder for specifying options during the JWE encryption process. Currently, this interface
44
+ * does not define any specific options but can be extended in the future to include parameters
45
+ * that control various aspects of the JWE encryption workflow.
46
+ */
47
+ export interface JweEncryptOptions {}
48
+
49
+ /**
50
+ * JSON Web Encryption (JWE) Header Parameters
51
+ *
52
+ * The Header Parameter names for use in JWEs are registered in the IANA "JSON Web Signature and
53
+ * Encryption Header Parameters" registry.
54
+ *
55
+ * @see {@link https://datatracker.ietf.org/doc/html/rfc7516#section-4.1 | RFC 7516, Section 4.1}
56
+ */
57
+ export interface JweHeaderParams extends JoseHeaderParams {
58
+ /**
59
+ * Algorithm Header Parameter
60
+ *
61
+ * Identifies the cryptographic algorithm used to encrypt or determine the value of the Content
62
+ * Encryption Key (CEK). The encrypted content is not usable if the "alg" value does not represent
63
+ * a supported algorithm, or if the recipient does not have a key that can be used with that
64
+ * algorithm.
65
+ *
66
+ * "alg" values should either be registered in the IANA "JSON Web Signature and Encryption
67
+ * Algorithms" registry or be a value that contains a Collision-Resistant Name. The "alg" value is
68
+ * a case-sensitive ASCII string. This Header Parameter MUST be present and MUST be understood
69
+ * and processed by implementations.
70
+ *
71
+ * @see {@link https://datatracker.ietf.org/doc/html/rfc7516#section-4.1.1 | RFC 7516, Section 4.1.1}
72
+ */
73
+ alg:
74
+ // AES Key Wrap with default initial value using 128-bit key
75
+ | 'A128KW'
76
+ // AES Key Wrap with default initial value using 192-bit key
77
+ | 'A192KW'
78
+ // AES Key Wrap with default initial value using 256-bit key
79
+ | 'A256KW'
80
+ // Direct use of a shared symmetric key as the CEK
81
+ | 'dir'
82
+ // Elliptic Curve Diffie-Hellman Ephemeral Static key agreement using Concat KDF
83
+ | 'ECDH-ES'
84
+ // ECDH-ES using Concat KDF and CEK wrapped with "A128KW"
85
+ | 'ECDH-ES+A128KW'
86
+ // ECDH-ES using Concat KDF and CEK wrapped with "A192KW"
87
+ | 'ECDH-ES+A192KW'
88
+ // ECDH-ES using Concat KDF and CEK wrapped with "A256KW"
89
+ | 'ECDH-ES+A256KW'
90
+ // Key wrapping with AES GCM using 128-bit key
91
+ | 'A128GCMKW'
92
+ // Key wrapping with AES GCM using 192-bit key
93
+ | 'A192GCMKW'
94
+ // Key wrapping with AES GCM using 256-bit key
95
+ | 'A256GCMKW'
96
+ // PBES2 with HMAC SHA-256 and "A128KW" wrapping
97
+ | 'PBES2-HS256+A128KW'
98
+ // PBES2 with HMAC SHA-384 and "A192KW" wrapping
99
+ | 'PBES2-HS384+A192KW'
100
+ // PBES2 with HMAC SHA-512 and "A256KW" wrapping
101
+ | 'PBES2-HS512+A256KW'
102
+ // PBES2 with HMAC SHA-512 and "XC20PKW" wrapping
103
+ | 'PBES2-HS512+XC20PKW'
104
+ // an unregistered, case-sensitive, collision-resistant string
105
+ | string;
106
+
107
+ /**
108
+ * Agreement PartyUInfo Header Parameter
109
+ *
110
+ * The "apu" (agreement PartyUInfo) value is a base64url-encoded octet sequence containing
111
+ * information about the producer of the JWE. This information is used by the recipient to
112
+ * determine the key agreement algorithm and key encryption algorithm to use to decrypt the JWE.
113
+ *
114
+ * Note: This parameter is intended only for use when the recipient is a key agreement algorithm
115
+ * that uses public key cryptography.
116
+ */
117
+ apu?: string;
118
+
119
+ /**
120
+ * Agreement PartyVInfo Header Parameter
121
+ *
122
+ * The "apv" (agreement PartyVInfo) value is a base64url-encoded octet sequence containing
123
+ * information about the recipient of the JWE. This information is used by the recipient to
124
+ * determine the key agreement algorithm and key encryption algorithm to use to decrypt the JWE.
125
+ *
126
+ * Note: This parameter is intended only for use when the recipient is a key agreement algorithm
127
+ * that uses public key cryptography.
128
+ */
129
+ apv?: string;
130
+
131
+ /**
132
+ * Critical Header Parameter
133
+ *
134
+ * Indicates that extensions to JOSE RFCs are being used that MUST be understood and processed.
135
+ */
136
+ crit?: string[];
137
+
138
+ /**
139
+ * Encryption Algorithm Header Parameter
140
+ *
141
+ * Identifies the content encryption algorithm used to encrypt and integrity-protect (also
142
+ * known as "authenticated encryption") the plaintext and to integrity-protect the Additional
143
+ * Authenticated Data (AAD), if any. This algorithm MUST be an AEAD algorithm with a specified
144
+ * key length.
145
+ *
146
+ * The encrypted content is not usable if the "enc" value does not represent a supported
147
+ * algorithm. "enc" values should either be registered in the IANA "JSON Web Signature and
148
+ * Encryption Algorithms" registry or be a value that contains a Collision-Resistant Name. The
149
+ * "enc" value is a case-sensitive ASCII string containing a StringOrURI value. This Header
150
+ * Parameter MUST be present and MUST be understood and processed by implementations.
151
+ *
152
+ * @see {@link https://datatracker.ietf.org/doc/html/rfc7516#section-4.1.2 | RFC 7516, Section 4.1.2}
153
+ */
154
+ enc:
155
+ // AES_128_CBC_HMAC_SHA_256 authenticated encryption algorithm,
156
+ // as defined in RFC 7518, Section 5.2.3
157
+ | 'A128CBC-HS256'
158
+ // AES_192_CBC_HMAC_SHA_384 authenticated encryption algorithm,
159
+ // as defined in RFC 7518, Section 5.2.4
160
+ | 'A192CBC-HS384'
161
+ // AES_256_CBC_HMAC_SHA_512 authenticated encryption algorithm,
162
+ // as defined in RFC 7518, Section 5.2.5
163
+ | 'A256CBC-HS512'
164
+ // AES GCM using 128-bit key
165
+ | 'A128GCM'
166
+ // AES GCM using 192-bit key
167
+ | 'A192GCM'
168
+ // AES GCM using 256-bit key
169
+ | 'A256GCM'
170
+ // XChaCha20-Poly1305 authenticated encryption algorithm
171
+ | 'XC20P'
172
+ // an unregistered, case-sensitive, collision-resistant string
173
+ | string;
174
+
175
+ /**
176
+ * Ephemeral Public Key Header Parameter
177
+ *
178
+ * The "epk" (ephemeral public key) value created by the originator for the use in key agreement
179
+ * algorithms. It is the ephemeral public key that corresponds to the key used to encrypt the
180
+ * JWE. This value is represented as a JSON Web Key (JWK).
181
+ *
182
+ * Note: This parameter is intended only for use when the recipient is a key agreement algorithm
183
+ * that uses public key cryptography.
184
+ */
185
+ epk?: Jwk;
186
+
187
+ /**
188
+ * Initialization Vector Header Parameter
189
+ *
190
+ * The "iv" (initialization vector) value is a base64url-encoded octet sequence used by the
191
+ * specified "enc" algorithm. The length of this Initialization Vector value MUST be exactly
192
+ * equal to the value that would be produced by the "enc" algorithm.
193
+ *
194
+ * Note: With symmetric encryption algorithms such as AES GCM, this Header Parameter MUST
195
+ * be present and MUST be understood and processed by implementations.
196
+ */
197
+ iv?: string;
198
+
199
+ /**
200
+ * PBES2 Count Header Parameter
201
+ *
202
+ * The "p2c" (PBES2 count) value is an integer indicating the number of iterations of the PBKDF2
203
+ * algorithm performed during key derivation.
204
+ *
205
+ * Note: The iteration count adds computational expense, ideally compounded by the possible range
206
+ * of keys introduced by the salt. A minimum iteration count of 1000 is RECOMMENDED.
207
+ */
208
+ p2c?: number;
209
+
210
+ /**
211
+ * PBES2 Salt Input Header Parameter
212
+ *
213
+ * The "p2s" (PBES2 salt) value is a base64url-encoded octet sequence used as the salt value
214
+ * input to the PBKDF2 algorithm during key derivation.
215
+ *
216
+ * The salt value used is (UTF8(Alg) || 0x00 || Salt Input), where Alg is the "alg" (algorithm)
217
+ * Header Parameter value.
218
+ *
219
+ * Note: The salt value is used to ensure that each key derived from the master key is
220
+ * independent of every other key. A suitable source of salt value is a sequence of
221
+ * cryptographically random bytes containing 8 or more octets.
222
+ */
223
+ p2s?: string;
224
+
225
+ /**
226
+ * Authentication Tag Header Parameter
227
+ *
228
+ * The "tag" value is a base64url-encoded octet sequence containing the value of the
229
+ * Authentication Tag output by the specified "enc" algorithm. The length of this
230
+ * Authentication Tag value MUST be exactly equal to the value that would be produced by the
231
+ * "enc" algorithm.
232
+ *
233
+ * Note: With authenticated encryption algorithms such as AES GCM, this Header Parameter MUST
234
+ * be present and MUST be understood and processed by implementations.
235
+ */
236
+ tag?: string;
237
+
238
+ /**
239
+ * Additional Public or Private Header Parameter names.
240
+ */
241
+ [key: string]: unknown;
242
+ }
243
+
244
+ /**
245
+ * Represents the result of the JWE key management encryption process, encapsulating the Content
246
+ * Encryption Key (CEK) and optionally the encrypted CEK.
247
+ */
248
+ export interface JweKeyManagementEncryptResult {
249
+ /**
250
+ * The Content Encryption Key (CEK) used for encrypting the JWE payload. It can be a Key
251
+ * Identifier such as a KMS URI or a JSON Web Key (JWK).
252
+ */
253
+ cek: KeyIdentifier | Jwk;
254
+
255
+ /**
256
+ * The encrypted version of the CEK, provided as a byte array. The encrypted version of the CEK
257
+ * is returned for all key management modes other than "dir" (Direct Encryption Mode).
258
+ */
259
+ encryptedKey?: Uint8Array;
260
+ }
261
+
262
+ /**
263
+ * Defines the parameters required to decrypt a JWE encrypted key, including the key management
264
+ * details.
265
+ *
266
+ * @typeParam TKeyManager - The Key Manager used to manage cryptographic keys.
267
+ * @typeParam TCrypto - The Crypto API used to perform cryptographic operations.
268
+ */
269
+ export interface JweKeyManagementDecryptParams<TKeyManager, TCrypto> {
270
+ /**
271
+ * The decryption key which can be a Key Identifier such as a KMS key URI, a JSON Web Key (JWK),
272
+ * or raw key material represented as a byte array.
273
+ */
274
+ key: KeyIdentifier | Jwk | Uint8Array;
275
+
276
+ /**
277
+ * The encrypted key extracted from the JWE, represented as a byte array. This parameter is
278
+ * optional and is used when the key is wrapped.
279
+ */
280
+ encryptedKey?: Uint8Array;
281
+
282
+ /**
283
+ * The JWE header parameters that define the characteristics of the decryption process, specifying
284
+ * the algorithm and encryption method among other settings.
285
+ */
286
+ joseHeader: JweHeaderParams;
287
+
288
+ /** Key Manager instanceß responsible for managing cryptographic keys. */
289
+ keyManager: TKeyManager;
290
+
291
+ /** Crypto API instance that provides the necessary cryptographic operations. */
292
+ crypto: TCrypto;
293
+ }
294
+
295
+ /**
296
+ * Defines the parameters required for encrypting a JWE CEK, including the key management details.
297
+ *
298
+ * @typeParam TKeyManager - The Key Manager used to manage cryptographic keys.
299
+ * @typeParam TCrypto - The Crypto API used to perform cryptographic operations.
300
+ */
301
+ export interface JweKeyManagementEncryptParams<TKeyManager, TCrypto> {
302
+ /**
303
+ * The encryption key which can be a Key Identifier such as a KMS key URI, a JSON Web Key (JWK),
304
+ * or raw key material represented as a byte array.
305
+ */
306
+ key: KeyIdentifier | Jwk | Uint8Array;
307
+
308
+ /**
309
+ * The JWE header parameters that define the characteristics of the encryption process, specifying
310
+ * the algorithm and encryption method among other settings.
311
+ */
312
+ joseHeader: JweHeaderParams;
313
+
314
+ /** Key Manager instanceß responsible for managing cryptographic keys. */
315
+ keyManager: TKeyManager;
316
+
317
+ /** Crypto API instance that provides the necessary cryptographic operations. */
318
+ crypto: TCrypto;
319
+ }
320
+
321
+ /**
322
+ * Checks if the provided object is a valid JWE (JSON Web Encryption) header.
323
+ *
324
+ * This function evaluates whether the given object adheres to the structure expected for
325
+ * a JWE header, specifically looking for the presence and proper format of the "alg" (algorithm)
326
+ * and "enc" (encryption algorithm) properties, which are essential for defining the JWE's
327
+ * cryptographic operations.
328
+ *
329
+ * @example
330
+ * ```ts
331
+ * const header = {
332
+ * alg: 'dir',
333
+ * enc: 'A256GCM'
334
+ * };
335
+ *
336
+ * if (isValidJweHeader(header)) {
337
+ * console.log('The object is a valid JWE header.');
338
+ * } else {
339
+ * console.log('The object is not a valid JWE header.');
340
+ * }
341
+ * ```
342
+ *
343
+ * @param obj - The object to be validated as a JWE header.
344
+ * @returns Returns `true` if the object is a valid JWE header, otherwise `false`.
345
+ */
346
+ export function isValidJweHeader(obj: unknown): obj is JweHeaderParams {
347
+ return typeof obj === 'object' && obj !== null
348
+ && 'alg' in obj && obj.alg !== undefined
349
+ && 'enc' in obj && obj.enc !== undefined;
350
+ }
351
+
352
+ /**
353
+ * The `JweKeyManagement` class implements the key management aspects of JSON Web Encryption (JWE)
354
+ * as specified in {@link https://datatracker.ietf.org/doc/html/rfc7516 | RFC 7516}.
355
+ *
356
+ * It supports algorithms for encrypting and decrypting keys, thereby enabling the secure
357
+ * transmission of information where the payload is encrypted, and the encryption key is also
358
+ * encrypted or agreed upon using key agreement techniques.
359
+ *
360
+ * The choice of algorithm is determined by the "alg" parameter in the JWE
361
+ * header, and the class is designed to handle the intricacies associated with each algorithm,
362
+ * ensuring the secure handling of the encryption keys.
363
+ *
364
+ * Supported algorithms include:
365
+ * - `"dir"`: Direct Encryption Mode
366
+ * - `"PBES2-HS256+A128KW"`, `"PBES2-HS384+A192KW"`, `"PBES2-HS512+A256KW"`: Password-Based
367
+ * Encryption Mode with Key Wrapping (PBES2) using HMAC-SHA and AES Key Wrap algorithms for key
368
+ * wrapping and encryption.
369
+ *
370
+ * @example
371
+ * // To encrypt a key:
372
+ * const keyEncryptionKey = Convert.string(passphrase).toUint8Array()
373
+ * const { cek, encryptedKey: encryptedCek } = await JweKeyManagement.encrypt({
374
+ * key: keyEncryptionKey,
375
+ * joseHeader: {
376
+ * alg: 'PBES2-HS512+A256KW',
377
+ * enc: 'A256GCM',
378
+ * p2c : 210_000,
379
+ p2s : Convert.uint8Array(saltInput).toBase64Url()
380
+ * },
381
+ * crypto: new AgentCryptoApi(),
382
+ * });
383
+ *
384
+ * // To decrypt a key:
385
+ * const cek = await JweKeyManagement.decrypt({
386
+ * key: keyEncryptionKey,
387
+ * encryptedKey: encryptedCek,
388
+ * joseHeader: {
389
+ * alg: 'PBES2-HS512+A256KW',
390
+ * enc: 'A256GCM',
391
+ * p2c : 210_000,
392
+ p2s : Convert.uint8Array(saltInput).toBase64Url()
393
+ * },
394
+ * crypto: new AgentCryptoApi(),
395
+ * });
396
+ */
397
+ export class JweKeyManagement {
398
+ /**
399
+ * Decrypts the encrypted key (JWE Encrypted Key) using the specified key encryption algorithm
400
+ * defined in the JWE Header's "alg" parameter.
401
+ *
402
+ * This method supports multiple key management algorithms, including Direct Encryption (dir) and
403
+ * PBES2 schemes with key wrapping.
404
+ *
405
+ * The method takes a key, which can be a Key Identifier, JWK, or raw byte array, and the
406
+ * encrypted key along with the JWE header. It returns the decrypted Content Encryption Key (CEK)
407
+ * which can then be used to decrypt the JWE ciphertext.
408
+ *
409
+ * @example
410
+ * ```ts
411
+ * // Decrypting the CEK with the PBES2-HS512+A256KW algorithm
412
+ * const cek = await JweKeyManagement.decrypt({
413
+ * key: Convert.string(passphrase).toUint8Array(),
414
+ * encryptedKey: encryptedCek,
415
+ * joseHeader: {
416
+ * alg: 'PBES2-HS512+A256KW',
417
+ * enc: 'A256GCM',
418
+ * p2c: 210_000,
419
+ * p2s: Convert.uint8Array(saltInput).toBase64Url(),
420
+ * },
421
+ * crypto: new AgentCryptoApi()
422
+ * });
423
+ * ```
424
+ *
425
+ * @param params - The decryption parameters.
426
+ * @throws Throws an error if the key management algorithm is not supported or if required
427
+ * parameters are missing or invalid.
428
+ */
429
+ public static async decrypt<TKeyManager extends KeyManager, TCrypto extends CryptoApi>({
430
+ key, encryptedKey, joseHeader, crypto
431
+ }: JweKeyManagementDecryptParams<TKeyManager, TCrypto>
432
+ ): Promise<KeyIdentifier | Jwk> {
433
+ // Determine the Key Management Mode employed by the algorithm specified by the "alg"
434
+ // (algorithm) Header Parameter.
435
+ switch (joseHeader.alg) {
436
+ case 'dir': {
437
+ // In Direct Encryption mode, a JWE "Encrypted Key" is not provided. Instead, the
438
+ // provided key management `key` is directly used as the Content Encryption Key (CEK) to
439
+ // decrypt the JWE payload.
440
+
441
+ // Verify that the JWE Encrypted Key value is empty.
442
+ if (encryptedKey !== undefined) {
443
+ throw new CryptoError(CryptoErrorCode.InvalidJwe, 'JWE "encrypted_key" is not allowed when using "dir" (Direct Encryption Mode).');
444
+ }
445
+
446
+ // Verify the key management `key` is a Key Identifier or JWK.
447
+ if (key instanceof Uint8Array) {
448
+ throw new CryptoError(CryptoErrorCode.InvalidJwe, 'Key management "key" must be a Key URI or JWK when using "dir" (Direct Encryption Mode).');
449
+ }
450
+
451
+ // return the key management `key` as the CEK.
452
+ return key;
453
+ }
454
+
455
+ case 'PBES2-HS256+A128KW':
456
+ case 'PBES2-HS384+A192KW':
457
+ case 'PBES2-HS512+A256KW': {
458
+ // In Key Encryption mode (PBES2) with key wrapping (A128KW, A192KW, A256KW), the given
459
+ // passphrase, salt (p2s), and iteration count (p2c) are used with the PBKDF2 key derivation
460
+ // function to derive the Key Encryption Key (KEK). The KEK is then used to decrypt the JWE
461
+ // Encrypted Key to obtain the Content Encryption Key (CEK).
462
+
463
+ if (typeof joseHeader.p2c !== 'number') {
464
+ throw new CryptoError(CryptoErrorCode.InvalidJwe, 'JOSE Header "p2c" (PBES2 Count) is missing or not a number.');
465
+ }
466
+
467
+ if (typeof joseHeader.p2s !== 'string') {
468
+ throw new CryptoError(CryptoErrorCode.InvalidJwe, 'JOSE Header "p2s" (PBES2 salt) is missing or not a string.');
469
+ }
470
+
471
+ // Throw an error if the key management `key` is not a byte array. For PBES2, the key is
472
+ // expected to be a low-entropy passphrase as a byte array.
473
+ if (!(key instanceof Uint8Array)) {
474
+ throw new CryptoError(CryptoErrorCode.InvalidJwe, 'Key management "key" must be a Uint8Array when using "PBES2" (Key Encryption Mode).');
475
+ }
476
+
477
+ // Verify that the JWE Encrypted Key value is present.
478
+ if (encryptedKey === undefined) {
479
+ throw new CryptoError(CryptoErrorCode.InvalidJwe, 'JWE "encrypted_key" is required when using "PBES2" (Key Encryption Mode).');
480
+ }
481
+
482
+ // Per {@link https://www.rfc-editor.org/rfc/rfc7518.html#section-4.8.1.1 | RFC 7518, Section 4.8.1.1},
483
+ // the salt value used with PBES2 should be of the format (UTF8(Alg) || 0x00 || Salt Input),
484
+ // where Alg is the "alg" (algorithm) Header Parameter value. This reduces the potential for
485
+ // a precomputed dictionary attack (also known as a rainbow table attack).
486
+ let salt: Uint8Array;
487
+ try {
488
+ salt = new Uint8Array([
489
+ ...Convert.string(joseHeader.alg).toUint8Array(),
490
+ 0x00,
491
+ ...Convert.base64Url(joseHeader.p2s).toUint8Array()
492
+ ]);
493
+ } catch {
494
+ throw new CryptoError(CryptoErrorCode.EncodingError, 'Failed to decode the JOSE Header "p2s" (PBES2 salt) value.');
495
+ }
496
+
497
+ // Derive the Key Encryption Key (KEK) from the given passphrase, salt, and iteration count.
498
+ const kek = await crypto.deriveKey({
499
+ algorithm : joseHeader.alg,
500
+ baseKeyBytes : key,
501
+ iterations : joseHeader.p2c,
502
+ salt
503
+ });
504
+
505
+ if (!(kek.alg && ['A128KW', 'A192KW', 'A256KW'].includes(kek.alg))) {
506
+ throw new CryptoError(CryptoErrorCode.AlgorithmNotSupported, `Unsupported Key Encryption Algorithm (alg) value: ${kek.alg}`);
507
+ }
508
+
509
+ // Decrypt the Content Encryption Key (CEK) with the derived KEK.
510
+ return await crypto.unwrapKey({
511
+ decryptionKey : kek,
512
+ wrappedKeyBytes : encryptedKey,
513
+ wrappedKeyAlgorithm : joseHeader.enc
514
+ });
515
+ }
516
+
517
+ default: {
518
+ throw new CryptoError(
519
+ CryptoErrorCode.AlgorithmNotSupported,
520
+ `Unsupported "alg" (Algorithm) Header Parameter value: ${joseHeader.alg}`
521
+ );
522
+ }
523
+ }
524
+ }
525
+
526
+ /**
527
+ * Encrypts a Content Encryption Key (CEK) using the key management algorithm specified in the
528
+ * JWE Header's "alg" parameter.
529
+ *
530
+ * This method supports various key management algorithms, including Direct Encryption (dir) and
531
+ * PBES2 with key wrapping.
532
+ *
533
+ * It generates a random CEK for the specified encryption algorithm in the JWE header, which
534
+ * can then be used to encrypt the actual payload. For algorithms that require an encrypted key,
535
+ * it returns the CEK along with the encrypted key.
536
+ *
537
+ * @example
538
+ * ```ts
539
+ * // Encrypting the CEK with the PBES2-HS512+A256KW algorithm
540
+ * const { cek, encryptedKey } = await JweKeyManagement.encrypt({
541
+ * key: Convert.string(passphrase).toUint8Array(),
542
+ * joseHeader: {
543
+ * alg: 'PBES2-HS512+A256KW',
544
+ * enc: 'A256GCM',
545
+ * p2c: 210_000,
546
+ * p2s: Convert.uint8Array(saltInput).toBase64Url(),
547
+ * },
548
+ * crypto: crypto: new AgentCryptoApi()
549
+ * });
550
+ * ```
551
+ *
552
+ * @param params - The encryption parameters.
553
+ * @returns The encrypted key result containing the CEK and optionally the encrypted CEK
554
+ * (JWE Encrypted Key).
555
+ * @throws Throws an error if the key management algorithm is not supported or if required
556
+ * parameters are missing or invalid.
557
+ */
558
+ public static async encrypt<TKeyManager extends KeyManager, TCrypto extends CryptoApi>({
559
+ key, joseHeader, crypto
560
+ }: JweKeyManagementEncryptParams<TKeyManager, TCrypto>
561
+ ): Promise<JweKeyManagementEncryptResult> {
562
+ let cek: KeyIdentifier | Jwk;
563
+ let encryptedKey: Uint8Array | undefined;
564
+
565
+ // Determine the Key Management Mode employed by the algorithm specified by the "alg"
566
+ // (algorithm) Header Parameter.
567
+ switch (joseHeader.alg) {
568
+ case 'dir': {
569
+ // In Direct Encryption mode (dir), a JWE "Encrypted Key" is not provided. Instead, the
570
+ // provided key management `key` is directly used as the Content Encryption Key (CEK) to
571
+ // decrypt the JWE payload.
572
+
573
+ // Verify that the JWE Encrypted Key value is empty.
574
+ if (encryptedKey !== undefined) {
575
+ throw new CryptoError(CryptoErrorCode.InvalidJwe, 'JWE "encrypted_key" is not allowed when using "dir" (Direct Encryption Mode).');
576
+ }
577
+
578
+ // Verify the key management `key` is a Key Identifier or JWK.
579
+ if (key instanceof Uint8Array) {
580
+ throw new CryptoError(CryptoErrorCode.InvalidJwe, 'Key management "key" must be a Key URI or JWK when using "dir" (Direct Encryption Mode).');
581
+ }
582
+
583
+ // Set the CEK to the key management `key`.
584
+ cek = key;
585
+
586
+ break;
587
+ }
588
+
589
+ case 'PBES2-HS256+A128KW':
590
+ case 'PBES2-HS384+A192KW':
591
+ case 'PBES2-HS512+A256KW': {
592
+ // In Key Encryption mode (PBES2) with key wrapping (A128KW, A192KW, A256KW), a randomly
593
+ // generated Content Encryption Key (CEK) is encrypted with a Key Encryption Key (KEK)
594
+ // derived from the given passphrase, salt (p2s), and iteration count (p2c) using the
595
+ // PBKDF2 key derivation function.
596
+
597
+ if (typeof joseHeader.p2c !== 'number') {
598
+ throw new CryptoError(CryptoErrorCode.InvalidJwe, 'JOSE Header "p2c" (PBES2 Count) is missing or not a number.');
599
+ }
600
+
601
+ if (typeof joseHeader.p2s !== 'string') {
602
+ throw new CryptoError(CryptoErrorCode.InvalidJwe, 'JOSE Header "p2s" (PBES2 salt) is missing or not a string.');
603
+ }
604
+
605
+ // Throw an error if the key management `key` is not a byte array.
606
+ if (!(key instanceof Uint8Array)) {
607
+ throw new CryptoError(CryptoErrorCode.InvalidJwe, 'Key management "key" must be a Uint8Array when using "PBES2" (Key Encryption Mode).');
608
+ }
609
+
610
+ // Generate a random Content Encryption Key (CEK) using the algorithm specified by the "enc"
611
+ // (encryption) Header Parameter.
612
+ cek = await crypto.generateKey({ algorithm: joseHeader.enc });
613
+
614
+ // Per {@link https://www.rfc-editor.org/rfc/rfc7518.html#section-4.8.1.1 | RFC 7518, Section 4.8.1.1},
615
+ // the salt value used with PBES2 should be of the format (UTF8(Alg) || 0x00 || Salt Input),
616
+ // where Alg is the "alg" (algorithm) Header Parameter value. This reduces the potential for
617
+ // a precomputed dictionary attack (also known as a rainbow table attack).
618
+ let salt: Uint8Array;
619
+ try {
620
+ salt = new Uint8Array([
621
+ ...Convert.string(joseHeader.alg).toUint8Array(),
622
+ 0x00,
623
+ ...Convert.base64Url(joseHeader.p2s).toUint8Array()
624
+ ]);
625
+ } catch {
626
+ throw new CryptoError(CryptoErrorCode.EncodingError, 'Failed to decode the JOSE Header "p2s" (PBES2 salt) value.');
627
+ }
628
+
629
+ // Derive a Key Encryption Key (KEK) from the given passphrase, salt, and iteration count.
630
+ const kek = await crypto.deriveKey({
631
+ algorithm : joseHeader.alg,
632
+ baseKeyBytes : key,
633
+ iterations : joseHeader.p2c,
634
+ salt
635
+ });
636
+
637
+ // Encrypt the randomly generated CEK with the derived Key Encryption Key (KEK).
638
+ encryptedKey = await crypto.wrapKey({ encryptionKey: kek, unwrappedKey: cek });
639
+
640
+ break;
641
+ }
642
+
643
+ default: {
644
+ throw new CryptoError(
645
+ CryptoErrorCode.AlgorithmNotSupported,
646
+ `Unsupported "alg" (Algorithm) Header Parameter value: ${joseHeader.alg}`
647
+ );
648
+ }
649
+ }
650
+
651
+ return { cek, encryptedKey };
652
+ }
653
+ }