@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,294 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { Convert } from '@enbox/common';
11
+ import { LocalKeyManager, CryptoUtils } from '@enbox/crypto';
12
+ import { isCipher } from '../utils.js';
13
+ import { AgentCryptoApi } from '../../../crypto-api.js';
14
+ import { JweKeyManagement, isValidJweHeader } from './jwe.js';
15
+ import { hasDuplicateProperties } from '../../common/object.js';
16
+ import { CryptoError, CryptoErrorCode } from '../crypto-error.js';
17
+ /**
18
+ * A helper utility function used internally to decode a JWE header parameter from a Base64 URL
19
+ * encoded string to a Uint8Array. It's designed to process individual JWE header parameter values,
20
+ * ensuring they are correctly formatted and decoded.
21
+ *
22
+ * @param param - The name of the JWE header parameter being decoded; used for error messaging.
23
+ * @param value - The Base64 URL encoded string value of the header parameter to decode.
24
+ * @returns The decoded parameter as a Uint8Array, or undefined if the input value is undefined.
25
+ * @throws {@link CryptoError} if the value is not a properly encoded Base64 URL string or if it's
26
+ * not a string.
27
+ */
28
+ function decodeHeaderParam(param, value) {
29
+ // If the parameter value is not present, return undefined.
30
+ if (value === undefined)
31
+ return undefined;
32
+ try {
33
+ if (typeof value !== 'string')
34
+ throw new Error();
35
+ return Convert.base64Url(value).toUint8Array();
36
+ }
37
+ catch (_a) {
38
+ throw new CryptoError(CryptoErrorCode.InvalidJwe, `Failed to decode the JWE Header parameter '${param}' from Base64 URL format to ` +
39
+ 'Uint8Array. Ensure the value is properly encoded in Base64 URL format without padding.');
40
+ }
41
+ }
42
+ /**
43
+ * The `FlattenedJwe` class handles the encryption and decryption of JSON Web Encryption (JWE)
44
+ * objects in the flattened serialization format. This format is a compact, URL-safe means of
45
+ * representing encrypted content, typically used when dealing with a single recipient or when
46
+ * bandwidth efficiency is important.
47
+ *
48
+ * This class provides methods to encrypt plaintext to a flattened JWE and decrypt a flattened JWE
49
+ * back to plaintext, utilizing a variety of supported cryptographic algorithms as specified in the
50
+ * JWE header parameters.
51
+ *
52
+ * @example
53
+ * ```ts
54
+ * // Example usage of encrypt method
55
+ * const plaintext = new TextEncoder().encode("Secret Message");
56
+ * const key = { kty: "oct", k: "your-secret-key" }; // Example symmetric key
57
+ * const protectedHeader = { alg: "dir", enc: "A256GCM" };
58
+ * const encryptedJwe = await FlattenedJwe.encrypt({
59
+ * plaintext,
60
+ * protectedHeader,
61
+ * key,
62
+ * });
63
+ * ```
64
+ *
65
+ * @example
66
+ * // Decryption example
67
+ * const { plaintext, protectedHeader } = await FlattenedJwe.decrypt({
68
+ * jwe: yourFlattenedJweObject,
69
+ * key: yourDecryptionKey,
70
+ * crypto: new YourCryptoApi(),
71
+ * });
72
+ */
73
+ export class FlattenedJwe {
74
+ constructor(params) {
75
+ /** Base64URL encoded ciphertext. */
76
+ this.ciphertext = '';
77
+ Object.assign(this, params);
78
+ }
79
+ static decrypt({ jwe, key, keyManager = new LocalKeyManager(), crypto = new AgentCryptoApi(), options = {} }) {
80
+ var _a, _b;
81
+ return __awaiter(this, void 0, void 0, function* () {
82
+ // Verify that the provided Crypto API supports the decrypt operation before proceeding.
83
+ if (!isCipher(crypto)) {
84
+ throw new CryptoError(CryptoErrorCode.OperationNotSupported, 'Crypto API does not support the "encrypt" operation.');
85
+ }
86
+ // Verify that the provided Key Manager supports the decrypt operation before proceeding.
87
+ if (!isCipher(keyManager)) {
88
+ throw new CryptoError(CryptoErrorCode.OperationNotSupported, 'Key Manager does not support the "decrypt" operation.');
89
+ }
90
+ // Verify that at least one of the JOSE header objects is present.
91
+ if (!jwe.protected && !jwe.header && !jwe.unprotected) {
92
+ throw new CryptoError(CryptoErrorCode.InvalidJwe, 'JWE is missing the required JOSE header parameters. ' +
93
+ 'Please provide at least one of the following: "protected", "header", or "unprotected"');
94
+ }
95
+ // Verify that the JWE Ciphertext is present.
96
+ if (typeof jwe.ciphertext !== 'string') {
97
+ throw new CryptoError(CryptoErrorCode.InvalidJwe, 'JWE Ciphertext is missing or not a string.');
98
+ }
99
+ // Parse the JWE Protected Header, if present.
100
+ let parsedProtectedHeader;
101
+ if (jwe.protected) {
102
+ try {
103
+ parsedProtectedHeader = Convert.base64Url(jwe.protected).toObject();
104
+ }
105
+ catch (_c) {
106
+ throw new Error('JWE Protected Header is invalid');
107
+ }
108
+ }
109
+ // Per {@link https://www.rfc-editor.org/rfc/rfc7516#section-5.2 | RFC7516 Section 5.2}
110
+ // the resulting JOSE Header MUST NOT contain duplicate Header Parameter names. In other words,
111
+ // the same Header Parameter name MUST NOT occur in the `header`, `protected`, and
112
+ // `unprotected` JSON object values that together comprise the JOSE Header.
113
+ if (hasDuplicateProperties(parsedProtectedHeader, jwe.header, jwe.unprotected)) {
114
+ throw new Error('Duplicate properties detected. Please ensure that each parameter is defined only once ' +
115
+ 'across the JWE "header", "protected", and "unprotected" objects.');
116
+ }
117
+ // The JOSE Header is the union of the members of the JWE Protected Header (`protected`), the
118
+ // JWE Shared Unprotected Header (`unprotected`), and the corresponding JWE Per-Recipient
119
+ // Unprotected Header (`header`).
120
+ const joseHeader = Object.assign(Object.assign(Object.assign({}, parsedProtectedHeader), jwe.header), jwe.unprotected);
121
+ if (!isValidJweHeader(joseHeader)) {
122
+ throw new Error('JWE Header is missing required "alg" (Algorithm) and/or "enc" (Encryption) Header Parameters');
123
+ }
124
+ if (Array.isArray(options.allowedAlgValues)
125
+ && !options.allowedAlgValues.includes(joseHeader.alg)) {
126
+ throw new Error(`"alg" (Algorithm) Header Parameter value not allowed: ${joseHeader.alg}`);
127
+ }
128
+ if (Array.isArray(options.allowedEncValues)
129
+ && !options.allowedEncValues.includes(joseHeader.enc)) {
130
+ throw new Error(`"enc" (Encryption Algorithm) Header Parameter value not allowed: ${joseHeader.enc}`);
131
+ }
132
+ let cek;
133
+ try {
134
+ const encryptedKey = jwe.encrypted_key
135
+ ? Convert.base64Url(jwe.encrypted_key).toUint8Array()
136
+ : undefined;
137
+ cek = yield JweKeyManagement.decrypt({ key, encryptedKey, joseHeader, keyManager, crypto });
138
+ }
139
+ catch (error) {
140
+ // If the error is a CryptoError with code "InvalidJwe" or "AlgorithmNotSupported", re-throw.
141
+ if (error instanceof CryptoError
142
+ && (error.code === CryptoErrorCode.InvalidJwe || error.code === CryptoErrorCode.AlgorithmNotSupported)) {
143
+ throw error;
144
+ }
145
+ // Otherwise, generate a random CEK and proceed to the next step.
146
+ // As noted in
147
+ // {@link https://datatracker.ietf.org/doc/html/rfc7516#section-11.5 | RFC 7516 Section 11.5},
148
+ // to mitigate the attacks described in
149
+ // {@link https://datatracker.ietf.org/doc/html/rfc3218 | RFC 3218}, the recipient MUST NOT
150
+ // distinguish between format, padding, and length errors of encrypted keys. It is strongly
151
+ // recommended, in the event of receiving an improperly formatted key, that the recipient
152
+ // substitute a randomly generated CEK and proceed to the next step, to mitigate timing
153
+ // attacks.
154
+ cek = typeof key === 'string'
155
+ ? yield keyManager.generateKey({ algorithm: joseHeader.enc })
156
+ : yield crypto.generateKey({ algorithm: joseHeader.enc });
157
+ }
158
+ // If present, decode the JWE Initialization Vector (IV) and Authentication Tag.
159
+ const iv = decodeHeaderParam('iv', jwe.iv);
160
+ const tag = decodeHeaderParam('tag', jwe.tag);
161
+ // Decode the JWE Ciphertext to a byte array, and if present, append the Authentication Tag.
162
+ const ciphertext = tag !== undefined
163
+ ? new Uint8Array([
164
+ ...Convert.base64Url(jwe.ciphertext).toUint8Array(),
165
+ ...(tag !== null && tag !== void 0 ? tag : [])
166
+ ])
167
+ : Convert.base64Url(jwe.ciphertext).toUint8Array();
168
+ // If the JWE Additional Authenticated Data (AAD) is present, the Additional Authenticated Data
169
+ // input to the Content Encryption Algorithm is
170
+ // ASCII(Encoded Protected Header || '.' || BASE64URL(JWE AAD)). If the JWE AAD is absent, the
171
+ // Additional Authenticated Data is ASCII(BASE64URL(UTF8(JWE Protected Header))).
172
+ const additionalData = jwe.aad !== undefined
173
+ ? new Uint8Array([
174
+ ...Convert.string((_a = jwe.protected) !== null && _a !== void 0 ? _a : '').toUint8Array(),
175
+ ...Convert.string('.').toUint8Array(),
176
+ ...Convert.string(jwe.aad).toUint8Array()
177
+ ])
178
+ : Convert.string((_b = jwe.protected) !== null && _b !== void 0 ? _b : '').toUint8Array();
179
+ // Decrypt the JWE using the Content Encryption Key (CEK) with:
180
+ // - Key Manager: If the CEK is a Key Identifier.
181
+ // - Crypto API: If the CEK is a JWK.
182
+ const plaintext = typeof cek === 'string'
183
+ ? yield keyManager.decrypt({ keyUri: cek, data: ciphertext, iv, additionalData })
184
+ : yield crypto.decrypt({ key: cek, data: ciphertext, iv, additionalData });
185
+ return {
186
+ plaintext,
187
+ protectedHeader: parsedProtectedHeader,
188
+ additionalAuthenticatedData: decodeHeaderParam('aad', jwe.aad),
189
+ sharedUnprotectedHeader: jwe.unprotected,
190
+ unprotectedHeader: jwe.header
191
+ };
192
+ });
193
+ }
194
+ static encrypt({ key, plaintext, additionalAuthenticatedData, protectedHeader, sharedUnprotectedHeader, unprotectedHeader, keyManager = new LocalKeyManager(), crypto = new AgentCryptoApi(), }) {
195
+ return __awaiter(this, void 0, void 0, function* () {
196
+ // Verify that the provided Crypto API supports the decrypt operation before proceeding.
197
+ if (!isCipher(crypto)) {
198
+ throw new CryptoError(CryptoErrorCode.OperationNotSupported, 'Crypto API does not support the "encrypt" operation.');
199
+ }
200
+ // Verify that the provided Key Manager supports the decrypt operation before proceeding.
201
+ if (!isCipher(keyManager)) {
202
+ throw new CryptoError(CryptoErrorCode.OperationNotSupported, 'Key Manager does not support the "decrypt" operation.');
203
+ }
204
+ // Verify that at least one of the JOSE header objects is present.
205
+ if (!protectedHeader && !sharedUnprotectedHeader && !unprotectedHeader) {
206
+ throw new CryptoError(CryptoErrorCode.InvalidJwe, 'JWE is missing the required JOSE header parameters. ' +
207
+ 'Please provide at least one of the following: "protectedHeader", "sharedUnprotectedHeader", or "unprotectedHeader"');
208
+ }
209
+ // Verify that the Plaintext is present.
210
+ if (!(plaintext instanceof Uint8Array)) {
211
+ throw new CryptoError(CryptoErrorCode.InvalidJwe, 'Plaintext is missing or not a byte array.');
212
+ }
213
+ // Per {@link https://www.rfc-editor.org/rfc/rfc7516#section-5.2 | RFC7516 Section 5.2}
214
+ // the resulting JOSE Header MUST NOT contain duplicate Header Parameter names. In other words,
215
+ // the same Header Parameter name MUST NOT occur in the `header`, `protected`, and
216
+ // `unprotected` JSON object values that together comprise the JOSE Header.
217
+ if (hasDuplicateProperties(protectedHeader, sharedUnprotectedHeader, unprotectedHeader)) {
218
+ throw new Error('Duplicate properties detected. Please ensure that each parameter is defined only once ' +
219
+ 'across the JWE "protectedHeader", "sharedUnprotectedHeader", and "unprotectedHeader" objects.');
220
+ }
221
+ // The JOSE Header is the union of the members of the JWE Protected Header (`protectedHeader`),
222
+ // the JWE Shared Unprotected Header (`sharedUnprotectedHeader`), and the corresponding JWE
223
+ // Per-Recipient Unprotected Header (`unprotectedHeader`).
224
+ const joseHeader = Object.assign(Object.assign(Object.assign({}, protectedHeader), sharedUnprotectedHeader), unprotectedHeader);
225
+ if (!isValidJweHeader(joseHeader)) {
226
+ throw new Error('JWE Header is missing required "alg" (Algorithm) and/or "enc" (Encryption) Header Parameters');
227
+ }
228
+ const { cek, encryptedKey } = yield JweKeyManagement.encrypt({ key, joseHeader, keyManager, crypto });
229
+ // If required for the Content Encryption Algorithm, generate a random JWE Initialization
230
+ // Vector (IV) of the correct size; otherwise, let the JWE Initialization Vector be the empty
231
+ // octet sequence.
232
+ let iv;
233
+ switch (joseHeader.enc) {
234
+ case 'A128GCM':
235
+ case 'A192GCM':
236
+ case 'A256GCM':
237
+ iv = CryptoUtils.randomBytes(12);
238
+ break;
239
+ default:
240
+ iv = new Uint8Array(0);
241
+ }
242
+ // Compute the Encoded Protected Header value BASE64URL(UTF8(JWE Protected Header)). If the JWE
243
+ // Protected Header is not present, let this value be the empty string.
244
+ const encodedProtectedHeader = protectedHeader
245
+ ? Convert.object(protectedHeader).toBase64Url()
246
+ : '';
247
+ // If the JWE Additional Authenticated Data (AAD) is present, the Additional Authenticated Data
248
+ // input to the Content Encryption Algorithm is
249
+ // ASCII(Encoded Protected Header || '.' || BASE64URL(JWE AAD)). If the JWE AAD is absent, the
250
+ // Additional Authenticated Data is ASCII(BASE64URL(UTF8(JWE Protected Header))).
251
+ let additionalData;
252
+ let encodedAad;
253
+ if (additionalAuthenticatedData) {
254
+ encodedAad = Convert.uint8Array(additionalAuthenticatedData).toBase64Url();
255
+ additionalData = Convert.string(encodedProtectedHeader + '.' + encodedAad).toUint8Array();
256
+ }
257
+ else {
258
+ additionalData = Convert.string(encodedProtectedHeader).toUint8Array();
259
+ }
260
+ // Encrypt the plaintext using the CEK, the JWE Initialization Vector, and the Additional
261
+ // Authenticated Data value using the specified content encryption algorithm to create the JWE
262
+ // Ciphertext value and the JWE Authentication Tag.
263
+ const ciphertextWithTag = typeof cek === 'string'
264
+ ? yield keyManager.encrypt({ keyUri: cek, data: plaintext, iv, additionalData })
265
+ : yield crypto.encrypt({ key: cek, data: plaintext, iv, additionalData });
266
+ const ciphertext = ciphertextWithTag.slice(0, -16);
267
+ const authenticationTag = ciphertextWithTag.slice(-16);
268
+ // Create the Flattened JWE JSON Serialization output, which is based upon the General syntax,
269
+ // but flattens it, optimizing it for the single-recipient case. It flattens it by removing the
270
+ // "recipients" member and instead placing those members defined for use in the "recipients"
271
+ // array (the "header" and "encrypted_key" members) in the top-level JSON object (at the same
272
+ // level as the "ciphertext" member).
273
+ const jwe = new FlattenedJwe({
274
+ ciphertext: Convert.uint8Array(ciphertext).toBase64Url(),
275
+ });
276
+ if (encryptedKey)
277
+ jwe.encrypted_key = Convert.uint8Array(encryptedKey).toBase64Url();
278
+ if (protectedHeader)
279
+ jwe.protected = encodedProtectedHeader;
280
+ if (sharedUnprotectedHeader)
281
+ jwe.unprotected = sharedUnprotectedHeader;
282
+ if (unprotectedHeader)
283
+ jwe.header = unprotectedHeader;
284
+ if (iv)
285
+ jwe.iv = Convert.uint8Array(iv).toBase64Url();
286
+ if (encodedAad)
287
+ jwe.aad = encodedAad;
288
+ if (authenticationTag)
289
+ jwe.tag = Convert.uint8Array(authenticationTag).toBase64Url();
290
+ return jwe;
291
+ });
292
+ }
293
+ }
294
+ //# sourceMappingURL=jwe-flattened.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jwe-flattened.js","sourceRoot":"","sources":["../../../../../src/prototyping/crypto/jose/jwe-flattened.ts"],"names":[],"mappings":";;;;;;;;;AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACxC,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAM7D,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAE,cAAc,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;gBAAE,GAAG,CAAC,SAAS,GAAG,sBAAsB,CAAC;YAC5D,IAAI,uBAAuB;gBAAE,GAAG,CAAC,WAAW,GAAG,uBAAuB,CAAC;YACvE,IAAI,iBAAiB;gBAAE,GAAG,CAAC,MAAM,GAAG,iBAAiB,CAAC;YACtD,IAAI,EAAE;gBAAE,GAAG,CAAC,EAAE,GAAG,OAAO,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC,WAAW,EAAE,CAAC;YACtD,IAAI,UAAU;gBAAE,GAAG,CAAC,GAAG,GAAG,UAAU,CAAC;YACrC,IAAI,iBAAiB;gBAAE,GAAG,CAAC,GAAG,GAAG,OAAO,CAAC,UAAU,CAAC,iBAAiB,CAAC,CAAC,WAAW,EAAE,CAAC;YAErF,OAAO,GAAG,CAAC;QACb,CAAC;KAAA;CACF"}
@@ -0,0 +1,308 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { Convert } from '@enbox/common';
11
+ import { CryptoError, CryptoErrorCode } from '../crypto-error.js';
12
+ /**
13
+ * Checks if the provided object is a valid JWE (JSON Web Encryption) header.
14
+ *
15
+ * This function evaluates whether the given object adheres to the structure expected for
16
+ * a JWE header, specifically looking for the presence and proper format of the "alg" (algorithm)
17
+ * and "enc" (encryption algorithm) properties, which are essential for defining the JWE's
18
+ * cryptographic operations.
19
+ *
20
+ * @example
21
+ * ```ts
22
+ * const header = {
23
+ * alg: 'dir',
24
+ * enc: 'A256GCM'
25
+ * };
26
+ *
27
+ * if (isValidJweHeader(header)) {
28
+ * console.log('The object is a valid JWE header.');
29
+ * } else {
30
+ * console.log('The object is not a valid JWE header.');
31
+ * }
32
+ * ```
33
+ *
34
+ * @param obj - The object to be validated as a JWE header.
35
+ * @returns Returns `true` if the object is a valid JWE header, otherwise `false`.
36
+ */
37
+ export function isValidJweHeader(obj) {
38
+ return typeof obj === 'object' && obj !== null
39
+ && 'alg' in obj && obj.alg !== undefined
40
+ && 'enc' in obj && obj.enc !== undefined;
41
+ }
42
+ /**
43
+ * The `JweKeyManagement` class implements the key management aspects of JSON Web Encryption (JWE)
44
+ * as specified in {@link https://datatracker.ietf.org/doc/html/rfc7516 | RFC 7516}.
45
+ *
46
+ * It supports algorithms for encrypting and decrypting keys, thereby enabling the secure
47
+ * transmission of information where the payload is encrypted, and the encryption key is also
48
+ * encrypted or agreed upon using key agreement techniques.
49
+ *
50
+ * The choice of algorithm is determined by the "alg" parameter in the JWE
51
+ * header, and the class is designed to handle the intricacies associated with each algorithm,
52
+ * ensuring the secure handling of the encryption keys.
53
+ *
54
+ * Supported algorithms include:
55
+ * - `"dir"`: Direct Encryption Mode
56
+ * - `"PBES2-HS256+A128KW"`, `"PBES2-HS384+A192KW"`, `"PBES2-HS512+A256KW"`: Password-Based
57
+ * Encryption Mode with Key Wrapping (PBES2) using HMAC-SHA and AES Key Wrap algorithms for key
58
+ * wrapping and encryption.
59
+ *
60
+ * @example
61
+ * // To encrypt a key:
62
+ * const keyEncryptionKey = Convert.string(passphrase).toUint8Array()
63
+ * const { cek, encryptedKey: encryptedCek } = await JweKeyManagement.encrypt({
64
+ * key: keyEncryptionKey,
65
+ * joseHeader: {
66
+ * alg: 'PBES2-HS512+A256KW',
67
+ * enc: 'A256GCM',
68
+ * p2c : 210_000,
69
+ p2s : Convert.uint8Array(saltInput).toBase64Url()
70
+ * },
71
+ * crypto: new AgentCryptoApi(),
72
+ * });
73
+ *
74
+ * // To decrypt a key:
75
+ * const cek = await JweKeyManagement.decrypt({
76
+ * key: keyEncryptionKey,
77
+ * encryptedKey: encryptedCek,
78
+ * joseHeader: {
79
+ * alg: 'PBES2-HS512+A256KW',
80
+ * enc: 'A256GCM',
81
+ * p2c : 210_000,
82
+ p2s : Convert.uint8Array(saltInput).toBase64Url()
83
+ * },
84
+ * crypto: new AgentCryptoApi(),
85
+ * });
86
+ */
87
+ export class JweKeyManagement {
88
+ /**
89
+ * Decrypts the encrypted key (JWE Encrypted Key) using the specified key encryption algorithm
90
+ * defined in the JWE Header's "alg" parameter.
91
+ *
92
+ * This method supports multiple key management algorithms, including Direct Encryption (dir) and
93
+ * PBES2 schemes with key wrapping.
94
+ *
95
+ * The method takes a key, which can be a Key Identifier, JWK, or raw byte array, and the
96
+ * encrypted key along with the JWE header. It returns the decrypted Content Encryption Key (CEK)
97
+ * which can then be used to decrypt the JWE ciphertext.
98
+ *
99
+ * @example
100
+ * ```ts
101
+ * // Decrypting the CEK with the PBES2-HS512+A256KW algorithm
102
+ * const cek = await JweKeyManagement.decrypt({
103
+ * key: Convert.string(passphrase).toUint8Array(),
104
+ * encryptedKey: encryptedCek,
105
+ * joseHeader: {
106
+ * alg: 'PBES2-HS512+A256KW',
107
+ * enc: 'A256GCM',
108
+ * p2c: 210_000,
109
+ * p2s: Convert.uint8Array(saltInput).toBase64Url(),
110
+ * },
111
+ * crypto: new AgentCryptoApi()
112
+ * });
113
+ * ```
114
+ *
115
+ * @param params - The decryption parameters.
116
+ * @throws Throws an error if the key management algorithm is not supported or if required
117
+ * parameters are missing or invalid.
118
+ */
119
+ static decrypt({ key, encryptedKey, joseHeader, crypto }) {
120
+ return __awaiter(this, void 0, void 0, function* () {
121
+ // Determine the Key Management Mode employed by the algorithm specified by the "alg"
122
+ // (algorithm) Header Parameter.
123
+ switch (joseHeader.alg) {
124
+ case 'dir': {
125
+ // In Direct Encryption mode, a JWE "Encrypted Key" is not provided. Instead, the
126
+ // provided key management `key` is directly used as the Content Encryption Key (CEK) to
127
+ // decrypt the JWE payload.
128
+ // Verify that the JWE Encrypted Key value is empty.
129
+ if (encryptedKey !== undefined) {
130
+ throw new CryptoError(CryptoErrorCode.InvalidJwe, 'JWE "encrypted_key" is not allowed when using "dir" (Direct Encryption Mode).');
131
+ }
132
+ // Verify the key management `key` is a Key Identifier or JWK.
133
+ if (key instanceof Uint8Array) {
134
+ throw new CryptoError(CryptoErrorCode.InvalidJwe, 'Key management "key" must be a Key URI or JWK when using "dir" (Direct Encryption Mode).');
135
+ }
136
+ // return the key management `key` as the CEK.
137
+ return key;
138
+ }
139
+ case 'PBES2-HS256+A128KW':
140
+ case 'PBES2-HS384+A192KW':
141
+ case 'PBES2-HS512+A256KW': {
142
+ // In Key Encryption mode (PBES2) with key wrapping (A128KW, A192KW, A256KW), the given
143
+ // passphrase, salt (p2s), and iteration count (p2c) are used with the PBKDF2 key derivation
144
+ // function to derive the Key Encryption Key (KEK). The KEK is then used to decrypt the JWE
145
+ // Encrypted Key to obtain the Content Encryption Key (CEK).
146
+ if (typeof joseHeader.p2c !== 'number') {
147
+ throw new CryptoError(CryptoErrorCode.InvalidJwe, 'JOSE Header "p2c" (PBES2 Count) is missing or not a number.');
148
+ }
149
+ if (typeof joseHeader.p2s !== 'string') {
150
+ throw new CryptoError(CryptoErrorCode.InvalidJwe, 'JOSE Header "p2s" (PBES2 salt) is missing or not a string.');
151
+ }
152
+ // Throw an error if the key management `key` is not a byte array. For PBES2, the key is
153
+ // expected to be a low-entropy passphrase as a byte array.
154
+ if (!(key instanceof Uint8Array)) {
155
+ throw new CryptoError(CryptoErrorCode.InvalidJwe, 'Key management "key" must be a Uint8Array when using "PBES2" (Key Encryption Mode).');
156
+ }
157
+ // Verify that the JWE Encrypted Key value is present.
158
+ if (encryptedKey === undefined) {
159
+ throw new CryptoError(CryptoErrorCode.InvalidJwe, 'JWE "encrypted_key" is required when using "PBES2" (Key Encryption Mode).');
160
+ }
161
+ // Per {@link https://www.rfc-editor.org/rfc/rfc7518.html#section-4.8.1.1 | RFC 7518, Section 4.8.1.1},
162
+ // the salt value used with PBES2 should be of the format (UTF8(Alg) || 0x00 || Salt Input),
163
+ // where Alg is the "alg" (algorithm) Header Parameter value. This reduces the potential for
164
+ // a precomputed dictionary attack (also known as a rainbow table attack).
165
+ let salt;
166
+ try {
167
+ salt = new Uint8Array([
168
+ ...Convert.string(joseHeader.alg).toUint8Array(),
169
+ 0x00,
170
+ ...Convert.base64Url(joseHeader.p2s).toUint8Array()
171
+ ]);
172
+ }
173
+ catch (_a) {
174
+ throw new CryptoError(CryptoErrorCode.EncodingError, 'Failed to decode the JOSE Header "p2s" (PBES2 salt) value.');
175
+ }
176
+ // Derive the Key Encryption Key (KEK) from the given passphrase, salt, and iteration count.
177
+ const kek = yield crypto.deriveKey({
178
+ algorithm: joseHeader.alg,
179
+ baseKeyBytes: key,
180
+ iterations: joseHeader.p2c,
181
+ salt
182
+ });
183
+ if (!(kek.alg && ['A128KW', 'A192KW', 'A256KW'].includes(kek.alg))) {
184
+ throw new CryptoError(CryptoErrorCode.AlgorithmNotSupported, `Unsupported Key Encryption Algorithm (alg) value: ${kek.alg}`);
185
+ }
186
+ // Decrypt the Content Encryption Key (CEK) with the derived KEK.
187
+ return yield crypto.unwrapKey({
188
+ decryptionKey: kek,
189
+ wrappedKeyBytes: encryptedKey,
190
+ wrappedKeyAlgorithm: joseHeader.enc
191
+ });
192
+ }
193
+ default: {
194
+ throw new CryptoError(CryptoErrorCode.AlgorithmNotSupported, `Unsupported "alg" (Algorithm) Header Parameter value: ${joseHeader.alg}`);
195
+ }
196
+ }
197
+ });
198
+ }
199
+ /**
200
+ * Encrypts a Content Encryption Key (CEK) using the key management algorithm specified in the
201
+ * JWE Header's "alg" parameter.
202
+ *
203
+ * This method supports various key management algorithms, including Direct Encryption (dir) and
204
+ * PBES2 with key wrapping.
205
+ *
206
+ * It generates a random CEK for the specified encryption algorithm in the JWE header, which
207
+ * can then be used to encrypt the actual payload. For algorithms that require an encrypted key,
208
+ * it returns the CEK along with the encrypted key.
209
+ *
210
+ * @example
211
+ * ```ts
212
+ * // Encrypting the CEK with the PBES2-HS512+A256KW algorithm
213
+ * const { cek, encryptedKey } = await JweKeyManagement.encrypt({
214
+ * key: Convert.string(passphrase).toUint8Array(),
215
+ * joseHeader: {
216
+ * alg: 'PBES2-HS512+A256KW',
217
+ * enc: 'A256GCM',
218
+ * p2c: 210_000,
219
+ * p2s: Convert.uint8Array(saltInput).toBase64Url(),
220
+ * },
221
+ * crypto: crypto: new AgentCryptoApi()
222
+ * });
223
+ * ```
224
+ *
225
+ * @param params - The encryption parameters.
226
+ * @returns The encrypted key result containing the CEK and optionally the encrypted CEK
227
+ * (JWE Encrypted Key).
228
+ * @throws Throws an error if the key management algorithm is not supported or if required
229
+ * parameters are missing or invalid.
230
+ */
231
+ static encrypt({ key, joseHeader, crypto }) {
232
+ return __awaiter(this, void 0, void 0, function* () {
233
+ let cek;
234
+ let encryptedKey;
235
+ // Determine the Key Management Mode employed by the algorithm specified by the "alg"
236
+ // (algorithm) Header Parameter.
237
+ switch (joseHeader.alg) {
238
+ case 'dir': {
239
+ // In Direct Encryption mode (dir), a JWE "Encrypted Key" is not provided. Instead, the
240
+ // provided key management `key` is directly used as the Content Encryption Key (CEK) to
241
+ // decrypt the JWE payload.
242
+ // Verify that the JWE Encrypted Key value is empty.
243
+ if (encryptedKey !== undefined) {
244
+ throw new CryptoError(CryptoErrorCode.InvalidJwe, 'JWE "encrypted_key" is not allowed when using "dir" (Direct Encryption Mode).');
245
+ }
246
+ // Verify the key management `key` is a Key Identifier or JWK.
247
+ if (key instanceof Uint8Array) {
248
+ throw new CryptoError(CryptoErrorCode.InvalidJwe, 'Key management "key" must be a Key URI or JWK when using "dir" (Direct Encryption Mode).');
249
+ }
250
+ // Set the CEK to the key management `key`.
251
+ cek = key;
252
+ break;
253
+ }
254
+ case 'PBES2-HS256+A128KW':
255
+ case 'PBES2-HS384+A192KW':
256
+ case 'PBES2-HS512+A256KW': {
257
+ // In Key Encryption mode (PBES2) with key wrapping (A128KW, A192KW, A256KW), a randomly
258
+ // generated Content Encryption Key (CEK) is encrypted with a Key Encryption Key (KEK)
259
+ // derived from the given passphrase, salt (p2s), and iteration count (p2c) using the
260
+ // PBKDF2 key derivation function.
261
+ if (typeof joseHeader.p2c !== 'number') {
262
+ throw new CryptoError(CryptoErrorCode.InvalidJwe, 'JOSE Header "p2c" (PBES2 Count) is missing or not a number.');
263
+ }
264
+ if (typeof joseHeader.p2s !== 'string') {
265
+ throw new CryptoError(CryptoErrorCode.InvalidJwe, 'JOSE Header "p2s" (PBES2 salt) is missing or not a string.');
266
+ }
267
+ // Throw an error if the key management `key` is not a byte array.
268
+ if (!(key instanceof Uint8Array)) {
269
+ throw new CryptoError(CryptoErrorCode.InvalidJwe, 'Key management "key" must be a Uint8Array when using "PBES2" (Key Encryption Mode).');
270
+ }
271
+ // Generate a random Content Encryption Key (CEK) using the algorithm specified by the "enc"
272
+ // (encryption) Header Parameter.
273
+ cek = yield crypto.generateKey({ algorithm: joseHeader.enc });
274
+ // Per {@link https://www.rfc-editor.org/rfc/rfc7518.html#section-4.8.1.1 | RFC 7518, Section 4.8.1.1},
275
+ // the salt value used with PBES2 should be of the format (UTF8(Alg) || 0x00 || Salt Input),
276
+ // where Alg is the "alg" (algorithm) Header Parameter value. This reduces the potential for
277
+ // a precomputed dictionary attack (also known as a rainbow table attack).
278
+ let salt;
279
+ try {
280
+ salt = new Uint8Array([
281
+ ...Convert.string(joseHeader.alg).toUint8Array(),
282
+ 0x00,
283
+ ...Convert.base64Url(joseHeader.p2s).toUint8Array()
284
+ ]);
285
+ }
286
+ catch (_a) {
287
+ throw new CryptoError(CryptoErrorCode.EncodingError, 'Failed to decode the JOSE Header "p2s" (PBES2 salt) value.');
288
+ }
289
+ // Derive a Key Encryption Key (KEK) from the given passphrase, salt, and iteration count.
290
+ const kek = yield crypto.deriveKey({
291
+ algorithm: joseHeader.alg,
292
+ baseKeyBytes: key,
293
+ iterations: joseHeader.p2c,
294
+ salt
295
+ });
296
+ // Encrypt the randomly generated CEK with the derived Key Encryption Key (KEK).
297
+ encryptedKey = yield crypto.wrapKey({ encryptionKey: kek, unwrappedKey: cek });
298
+ break;
299
+ }
300
+ default: {
301
+ throw new CryptoError(CryptoErrorCode.AlgorithmNotSupported, `Unsupported "alg" (Algorithm) Header Parameter value: ${joseHeader.alg}`);
302
+ }
303
+ }
304
+ return { cek, encryptedKey };
305
+ });
306
+ }
307
+ }
308
+ //# sourceMappingURL=jwe.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jwe.js","sourceRoot":"","sources":["../../../../../src/prototyping/crypto/jose/jwe.ts"],"names":[],"mappings":";;;;;;;;;AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAKxC,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAyTlE;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,UAAU,gBAAgB,CAAC,GAAY;IAC3C,OAAO,OAAO,GAAG,KAAK,QAAQ,IAAI,GAAG,KAAK,IAAI;WACzC,KAAK,IAAI,GAAG,IAAI,GAAG,CAAC,GAAG,KAAK,SAAS;WACrC,KAAK,IAAI,GAAG,IAAI,GAAG,CAAC,GAAG,KAAK,SAAS,CAAC;AAC7C,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AACH,MAAM,OAAO,gBAAgB;IAC3B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA8BG;IACI,MAAM,CAAO,OAAO,CAA4D,EACrF,GAAG,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,EACe;;YAEpD,qFAAqF;YACrF,gCAAgC;YAChC,QAAQ,UAAU,CAAC,GAAG,EAAE;gBACtB,KAAK,KAAK,CAAC,CAAC;oBACV,iFAAiF;oBACjF,wFAAwF;oBACxF,2BAA2B;oBAE3B,oDAAoD;oBACpD,IAAI,YAAY,KAAK,SAAS,EAAE;wBAC9B,MAA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