@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,374 @@
1
+ import { Convert } from '@enbox/common';
2
+ import { getWebcryptoSubtle } from '@noble/ciphers/webcrypto';
3
+
4
+ import type { Jwk } from '@enbox/crypto';
5
+
6
+ import { computeJwkThumbprint, isOctPrivateJwk } from '@enbox/crypto';
7
+
8
+ /**
9
+ * Const defining the AES-GCM initialization vector (IV) length in bits.
10
+ *
11
+ * @remarks
12
+ * NIST Special Publication 800-38D, Section 5.2.1.1 states that the IV length:
13
+ * > For IVs, it is recommended that implementations restrict support to the length of 96 bits, to
14
+ * > promote interoperability, efficiency, and simplicity of design.
15
+ *
16
+ * This implementation does not support IV lengths that are different from the value defined by
17
+ * this constant.
18
+ *
19
+ * @see {@link https://doi.org/10.6028/NIST.SP.800-38D | NIST SP 800-38D}
20
+ */
21
+ const AES_GCM_IV_LENGTH = 96;
22
+
23
+ /**
24
+ * Constant defining the AES key length values in bits.
25
+ *
26
+ * @remarks
27
+ * NIST publication FIPS 197 states:
28
+ * > The AES algorithm is capable of using cryptographic keys of 128, 192, and 256 bits to encrypt
29
+ * > and decrypt data in blocks of 128 bits.
30
+ *
31
+ * This implementation does not support key lengths that are different from the three values
32
+ * defined by this constant.
33
+ *
34
+ * @see {@link https://doi.org/10.6028/NIST.FIPS.197-upd1 | NIST FIPS 197}
35
+ */
36
+ const AES_KEY_LENGTHS = [128, 192, 256] as const;
37
+
38
+ /**
39
+ * Constant defining the AES-GCM tag length values in bits.
40
+ *
41
+ * @remarks
42
+ * NIST Special Publication 800-38D, Section 5.2.1.2 states that the tag length:
43
+ * > may be any one of the following five values: 128, 120, 112, 104, or 96
44
+ *
45
+ * Although the NIST specification allows for tag lengths of 32 or 64 bits in certain applications,
46
+ * the use of shorter tag lengths can be problematic for GCM due to targeted forgery attacks. As a
47
+ * precaution, this implementation does not support tag lengths that are different from the five
48
+ * values defined by this constant. See Appendix C of the NIST SP 800-38D specification for
49
+ * additional guidance and details.
50
+ *
51
+ * @see {@link https://doi.org/10.6028/NIST.SP.800-38D | NIST SP 800-38D}
52
+ */
53
+ export const AES_GCM_TAG_LENGTHS = [96, 104, 112, 120, 128] as const;
54
+
55
+ /**
56
+ * The `AesGcm` class provides a comprehensive set of utilities for cryptographic operations
57
+ * using the Advanced Encryption Standard (AES) in Galois/Counter Mode (GCM). This class includes
58
+ * methods for key generation, encryption, decryption, and conversions between raw byte arrays
59
+ * and JSON Web Key (JWK) formats. It is designed to support AES-GCM, a symmetric key algorithm
60
+ * that is widely used for its efficiency, security, and provision of authenticated encryption.
61
+ *
62
+ * AES-GCM is particularly favored for scenarios that require both confidentiality and integrity
63
+ * of data. It integrates the counter mode of encryption with the Galois mode of authentication,
64
+ * offering high performance and parallel processing capabilities.
65
+ *
66
+ * Key Features:
67
+ * - Key Generation: Generate AES symmetric keys in JWK format.
68
+ * - Key Conversion: Transform keys between raw byte arrays and JWK formats.
69
+ * - Encryption: Encrypt data using AES-GCM with the provided symmetric key.
70
+ * - Decryption: Decrypt data encrypted with AES-GCM using the corresponding symmetric key.
71
+ *
72
+ * The methods in this class are asynchronous, returning Promises to accommodate various
73
+ * JavaScript environments.
74
+ *
75
+ * @example
76
+ * ```ts
77
+ * // Key Generation
78
+ * const length = 256; // Length of the key in bits (e.g., 128, 192, 256)
79
+ * const privateKey = await AesGcm.generateKey({ length });
80
+ *
81
+ * // Encryption
82
+ * const data = new TextEncoder().encode('Messsage');
83
+ * const iv = new Uint8Array(12); // 12-byte initialization vector
84
+ * const encryptedData = await AesGcm.encrypt({
85
+ * data,
86
+ * iv,
87
+ * key: privateKey
88
+ * });
89
+ *
90
+ * // Decryption
91
+ * const decryptedData = await AesGcm.decrypt({
92
+ * data: encryptedData,
93
+ * iv,
94
+ * key: privateKey
95
+ * });
96
+ *
97
+ * // Key Conversion
98
+ * const privateKeyBytes = await AesGcm.privateKeyToBytes({ privateKey });
99
+ * ```
100
+ */
101
+ export class AesGcm {
102
+ /**
103
+ * Converts a raw private key in bytes to its corresponding JSON Web Key (JWK) format.
104
+ *
105
+ * @remarks
106
+ * This method accepts a symmetric key represented as a byte array (Uint8Array) and
107
+ * converts it into a JWK object for use with AES-GCM (Advanced Encryption Standard -
108
+ * Galois/Counter Mode). The conversion process involves encoding the key into
109
+ * base64url format and setting the appropriate JWK parameters.
110
+ *
111
+ * The resulting JWK object includes the following properties:
112
+ * - `kty`: Key Type, set to 'oct' for Octet Sequence (representing a symmetric key).
113
+ * - `k`: The symmetric key, base64url-encoded.
114
+ * - `kid`: Key ID, generated based on the JWK thumbprint.
115
+ *
116
+ * @example
117
+ * ```ts
118
+ * const privateKeyBytes = new Uint8Array([...]); // Replace with actual symmetric key bytes
119
+ * const privateKey = await AesGcm.bytesToPrivateKey({ privateKeyBytes });
120
+ * ```
121
+ *
122
+ * @param params - The parameters for the symmetric key conversion.
123
+ * @param params.privateKeyBytes - The raw symmetric key as a Uint8Array.
124
+ *
125
+ * @returns A Promise that resolves to the symmetric key in JWK format.
126
+ */
127
+ public static async bytesToPrivateKey({ privateKeyBytes }: {
128
+ privateKeyBytes: Uint8Array;
129
+ }): Promise<Jwk> {
130
+ // Construct the private key in JWK format.
131
+ const privateKey: Jwk = {
132
+ k : Convert.uint8Array(privateKeyBytes).toBase64Url(),
133
+ kty : 'oct'
134
+ };
135
+
136
+ // Compute the JWK thumbprint and set as the key ID.
137
+ privateKey.kid = await computeJwkThumbprint({ jwk: privateKey });
138
+
139
+ // Add algorithm identifier based on key length.
140
+ const lengthInBits = privateKeyBytes.length * 8;
141
+ privateKey.alg = { 128: 'A128GCM', 192: 'A192GCM', 256: 'A256GCM' }[lengthInBits];
142
+
143
+ return privateKey;
144
+ }
145
+
146
+ /**
147
+ * Decrypts the provided data using AES-GCM.
148
+ *
149
+ * @remarks
150
+ * This method performs AES-GCM decryption on the given encrypted data using the specified key.
151
+ * It requires an initialization vector (IV), the encrypted data along with the decryption key,
152
+ * and optionally, additional authenticated data (AAD). The method returns the decrypted data as a
153
+ * Uint8Array. The optional `tagLength` parameter specifies the size in bits of the authentication
154
+ * tag used when encrypting the data. If not specified, the default tag length of 128 bits is
155
+ * used.
156
+ *
157
+ * @example
158
+ * ```ts
159
+ * const encryptedData = new Uint8Array([...]); // Encrypted data
160
+ * const iv = new Uint8Array([...]); // Initialization vector used during encryption
161
+ * const additionalData = new Uint8Array([...]); // Optional additional authenticated data
162
+ * const key = { ... }; // A Jwk object representing the AES key
163
+ * const decryptedData = await AesGcm.decrypt({
164
+ * data: encryptedData,
165
+ * iv,
166
+ * additionalData,
167
+ * key,
168
+ * tagLength: 128 // Optional tag length in bits
169
+ * });
170
+ * ```
171
+ *
172
+ * @param params - The parameters for the decryption operation.
173
+ * @param params.key - The key to use for decryption, represented in JWK format.
174
+ * @param params.data - The encrypted data to decrypt, represented as a Uint8Array.
175
+ * @param params.iv - The initialization vector, represented as a Uint8Array.
176
+ * @param params.additionalData - Optional additional authenticated data. Optional.
177
+ * @param params.tagLength - The length of the authentication tag in bits. Optional.
178
+ *
179
+ * @returns A Promise that resolves to the decrypted data as a Uint8Array.
180
+ */
181
+ public static async decrypt({ key, data, iv, additionalData, tagLength }: {
182
+ key: Jwk;
183
+ data: Uint8Array;
184
+ iv: Uint8Array;
185
+ additionalData?: Uint8Array;
186
+ tagLength?: typeof AES_GCM_TAG_LENGTHS[number];
187
+ }): Promise<Uint8Array> {
188
+ // Validate the initialization vector length.
189
+ if (iv.byteLength !== AES_GCM_IV_LENGTH / 8) {
190
+ throw new TypeError(`The initialization vector must be ${AES_GCM_IV_LENGTH} bits in length`);
191
+ }
192
+
193
+ // Validate the tag length.
194
+ if (tagLength && !AES_GCM_TAG_LENGTHS.includes(tagLength as any)) {
195
+ throw new RangeError(`The tag length is invalid: Must be ${AES_GCM_TAG_LENGTHS.join(', ')} bits`);
196
+ }
197
+
198
+ // Get the Web Crypto API interface.
199
+ const webCrypto = getWebcryptoSubtle();
200
+
201
+ // Import the JWK into the Web Crypto API to use for the decrypt operation.
202
+ const webCryptoKey = await webCrypto.importKey('jwk', key, { name: 'AES-GCM' }, true, ['decrypt']);
203
+
204
+ // Browser implementations of the Web Crypto API throw an error if additionalData is undefined.
205
+ const algorithm = (additionalData === undefined)
206
+ ? { name: 'AES-GCM', iv, tagLength }
207
+ : { name: 'AES-GCM', additionalData, iv, tagLength };
208
+
209
+ // Decrypt the data.
210
+ const plaintextBuffer = await webCrypto.decrypt(algorithm, webCryptoKey, data);
211
+
212
+ // Convert from ArrayBuffer to Uint8Array.
213
+ const plaintext = new Uint8Array(plaintextBuffer);
214
+
215
+ return plaintext;
216
+ }
217
+
218
+ /**
219
+ * Encrypts the provided data using AES-GCM.
220
+ *
221
+ * @remarks
222
+ * This method performs AES-GCM encryption on the given data using the specified key.
223
+ * It requires an initialization vector (IV), the encrypted data along with the decryption key,
224
+ * and optionally, additional authenticated data (AAD). The method returns the encrypted data as a
225
+ * Uint8Array. The optional `tagLength` parameter specifies the size in bits of the authentication
226
+ * tag generated in the encryption operation and used for authentication in the corresponding
227
+ * decryption. If not specified, the default tag length of 128 bits is used.
228
+ *
229
+ * @example
230
+ * ```ts
231
+ * const data = new TextEncoder().encode('Messsage');
232
+ * const iv = new Uint8Array([...]); // Initialization vector
233
+ * const additionalData = new Uint8Array([...]); // Optional additional authenticated data
234
+ * const key = { ... }; // A Jwk object representing an AES key
235
+ * const encryptedData = await AesGcm.encrypt({
236
+ * data,
237
+ * iv,
238
+ * additionalData,
239
+ * key,
240
+ * tagLength: 128 // Optional tag length in bits
241
+ * });
242
+ * ```
243
+ *
244
+ * @param params - The parameters for the encryption operation.
245
+ * @param params.key - The key to use for encryption, represented in JWK format.
246
+ * @param params.data - The data to encrypt, represented as a Uint8Array.
247
+ * @param params.iv - The initialization vector, represented as a Uint8Array.
248
+ * @param params.additionalData - Optional additional authenticated data. Optional.
249
+ * @param params.tagLength - The length of the authentication tag in bits. Optional.
250
+ *
251
+ * @returns A Promise that resolves to the encrypted data as a Uint8Array.
252
+ */
253
+ public static async encrypt({ data, iv, key, additionalData, tagLength }: {
254
+ key: Jwk;
255
+ data: Uint8Array;
256
+ iv: Uint8Array;
257
+ additionalData?: Uint8Array;
258
+ tagLength?: typeof AES_GCM_TAG_LENGTHS[number];
259
+ }): Promise<Uint8Array> {
260
+ // Validate the initialization vector length.
261
+ if (iv.byteLength !== AES_GCM_IV_LENGTH / 8) {
262
+ throw new TypeError(`The initialization vector must be ${AES_GCM_IV_LENGTH} bits in length`);
263
+ }
264
+
265
+ // Validate the tag length.
266
+ if (tagLength && !AES_GCM_TAG_LENGTHS.includes(tagLength as any)) {
267
+ throw new RangeError(`The tag length is invalid: Must be ${AES_GCM_TAG_LENGTHS.join(', ')} bits`);
268
+ }
269
+
270
+ // Get the Web Crypto API interface.
271
+ const webCrypto = getWebcryptoSubtle();
272
+
273
+ // Import the JWK into the Web Crypto API to use for the encrypt operation.
274
+ const webCryptoKey = await webCrypto.importKey('jwk', key, { name: 'AES-GCM' }, true, ['encrypt']);
275
+
276
+ // Browser implementations of the Web Crypto API throw an error if additionalData is undefined.
277
+ const algorithm = (additionalData === undefined)
278
+ ? { name: 'AES-GCM', iv, tagLength }
279
+ : { name: 'AES-GCM', additionalData, iv, tagLength };
280
+
281
+ // Encrypt the data.
282
+ const ciphertextBuffer = await webCrypto.encrypt(algorithm, webCryptoKey, data);
283
+
284
+ // Convert from ArrayBuffer to Uint8Array.
285
+ const ciphertext = new Uint8Array(ciphertextBuffer);
286
+
287
+ return ciphertext;
288
+ }
289
+
290
+ /**
291
+ * Generates a symmetric key for AES in Galois/Counter Mode (GCM) in JSON Web Key (JWK) format.
292
+ *
293
+ * @remarks
294
+ * This method creates a new symmetric key of a specified length suitable for use with
295
+ * AES-GCM encryption. It leverages cryptographically secure random number generation
296
+ * to ensure the uniqueness and security of the key. The generated key adheres to the JWK
297
+ * format, facilitating compatibility with common cryptographic standards and ease of use
298
+ * in various cryptographic applications.
299
+ *
300
+ * The generated key includes these components:
301
+ * - `kty`: Key Type, set to 'oct' for Octet Sequence, indicating a symmetric key.
302
+ * - `k`: The symmetric key component, base64url-encoded.
303
+ * - `kid`: Key ID, generated based on the JWK thumbprint, providing a unique identifier.
304
+ *
305
+ * @example
306
+ * ```ts
307
+ * const length = 256; // Length of the key in bits (e.g., 128, 192, 256)
308
+ * const privateKey = await AesGcm.generateKey({ length });
309
+ * ```
310
+ *
311
+ * @param params - The parameters for the key generation.
312
+ * @param params.length - The length of the key in bits. Common lengths are 128, 192, and 256 bits.
313
+ *
314
+ * @returns A Promise that resolves to the generated symmetric key in JWK format.
315
+ */
316
+ public static async generateKey({ length }: {
317
+ length: typeof AES_KEY_LENGTHS[number];
318
+ }): Promise<Jwk> {
319
+ // Validate the key length.
320
+ if (!AES_KEY_LENGTHS.includes(length as any)) {
321
+ throw new RangeError(`The key length is invalid: Must be ${AES_KEY_LENGTHS.join(', ')} bits`);
322
+ }
323
+
324
+ // Get the Web Crypto API interface.
325
+ const webCrypto = getWebcryptoSubtle();
326
+
327
+ // Generate a random private key.
328
+ // See https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues#usage_notes for
329
+ // an explanation for why Web Crypto generateKey() is used instead of getRandomValues().
330
+ const webCryptoKey = await webCrypto.generateKey( { name: 'AES-GCM', length }, true, ['encrypt']);
331
+
332
+ // Export the private key in JWK format.
333
+ const { ext, key_ops, ...privateKey } = await webCrypto.exportKey('jwk', webCryptoKey);
334
+
335
+ // Compute the JWK thumbprint and set as the key ID.
336
+ privateKey.kid = await computeJwkThumbprint({ jwk: privateKey });
337
+
338
+ return privateKey;
339
+ }
340
+
341
+ /**
342
+ * Converts a private key from JSON Web Key (JWK) format to a raw byte array (Uint8Array).
343
+ *
344
+ * @remarks
345
+ * This method takes a symmetric key in JWK format and extracts its raw byte representation.
346
+ * It focuses on the 'k' parameter of the JWK, which represents the symmetric key component
347
+ * in base64url encoding. The method decodes this value into a byte array, providing
348
+ * the symmetric key in its raw binary form.
349
+ *
350
+ * @example
351
+ * ```ts
352
+ * const privateKey = { ... }; // A symmetric key in JWK format
353
+ * const privateKeyBytes = await AesGcm.privateKeyToBytes({ privateKey });
354
+ * ```
355
+ *
356
+ * @param params - The parameters for the symmetric key conversion.
357
+ * @param params.privateKey - The symmetric key in JWK format.
358
+ *
359
+ * @returns A Promise that resolves to the symmetric key as a Uint8Array.
360
+ */
361
+ public static async privateKeyToBytes({ privateKey }: {
362
+ privateKey: Jwk;
363
+ }): Promise<Uint8Array> {
364
+ // Verify the provided JWK represents a valid oct private key.
365
+ if (!isOctPrivateJwk(privateKey)) {
366
+ throw new Error(`AesGcm: The provided key is not a valid oct private key.`);
367
+ }
368
+
369
+ // Decode the provided private key to bytes.
370
+ const privateKeyBytes = Convert.base64Url(privateKey.k).toUint8Array();
371
+
372
+ return privateKeyBytes;
373
+ }
374
+ }
@@ -0,0 +1,271 @@
1
+ // ! TODO : Make sure I remove `@noble/ciphers` from the Agent package.json once this is moved to the `@enbox/crypto` package.
2
+ import { getWebcryptoSubtle } from '@noble/ciphers/webcrypto';
3
+
4
+ import type { Jwk } from '@enbox/crypto';
5
+
6
+ import { Convert } from '@enbox/common';
7
+ import { computeJwkThumbprint, isOctPrivateJwk } from '@enbox/crypto';
8
+
9
+ import type { UnwrapKeyParams, WrapKeyParams } from '../types/params-direct.js';
10
+ import { CryptoError, CryptoErrorCode } from '../crypto-error.js';
11
+
12
+ /**
13
+ * Constant defining the AES key length values in bits.
14
+ *
15
+ * @remarks
16
+ * NIST publication FIPS 197 states:
17
+ * > The AES algorithm is capable of using cryptographic keys of 128, 192, and 256 bits to encrypt
18
+ * > and decrypt data in blocks of 128 bits.
19
+ *
20
+ * This implementation does not support key lengths that are different from the three values
21
+ * defined by this constant.
22
+ *
23
+ * @see {@link https://doi.org/10.6028/NIST.FIPS.197-upd1 | NIST FIPS 197}
24
+ */
25
+ const AES_KEY_LENGTHS = [128, 192, 256] as const;
26
+
27
+ export class AesKw {
28
+ /**
29
+ * Converts a raw private key in bytes to its corresponding JSON Web Key (JWK) format.
30
+ *
31
+ * @remarks
32
+ * This method takes a symmetric key represented as a byte array (Uint8Array) and
33
+ * converts it into a JWK object for use with AES (Advanced Encryption Standard)
34
+ * for key wrapping. The conversion process involves encoding the key into
35
+ * base64url format and setting the appropriate JWK parameters.
36
+ *
37
+ * The resulting JWK object includes the following properties:
38
+ * - `kty`: Key Type, set to 'oct' for Octet Sequence (representing a symmetric key).
39
+ * - `k`: The symmetric key, base64url-encoded.
40
+ * - `kid`: Key ID, generated based on the JWK thumbprint.
41
+ *
42
+ * @example
43
+ * ```ts
44
+ * const privateKeyBytes = new Uint8Array([...]); // Replace with actual symmetric key bytes
45
+ * const privateKey = await AesKw.bytesToPrivateKey({ privateKeyBytes });
46
+ * ```
47
+ *
48
+ * @param params - The parameters for the symmetric key conversion.
49
+ * @param params.privateKeyBytes - The raw symmetric key as a Uint8Array.
50
+ *
51
+ * @returns A Promise that resolves to the symmetric key in JWK format.
52
+ */
53
+ public static async bytesToPrivateKey({ privateKeyBytes }: {
54
+ privateKeyBytes: Uint8Array;
55
+ }): Promise<Jwk> {
56
+ // Construct the private key in JWK format.
57
+ const privateKey: Jwk = {
58
+ k : Convert.uint8Array(privateKeyBytes).toBase64Url(),
59
+ kty : 'oct'
60
+ };
61
+
62
+ // Compute the JWK thumbprint and set as the key ID.
63
+ privateKey.kid = await computeJwkThumbprint({ jwk: privateKey });
64
+
65
+ // Add algorithm identifier based on key length.
66
+ const lengthInBits = privateKeyBytes.length * 8;
67
+ privateKey.alg = { 128: 'A128KW', 192: 'A192KW', 256: 'A256KW' }[lengthInBits];
68
+
69
+ return privateKey;
70
+ }
71
+
72
+ /**
73
+ * Generates a symmetric key for AES for key wrapping in JSON Web Key (JWK) format.
74
+ *
75
+ * @remarks
76
+ * This method creates a new symmetric key of a specified length suitable for use with
77
+ * AES key wrapping. It uses cryptographically secure random number generation to
78
+ * ensure the uniqueness and security of the key. The generated key adheres to the JWK
79
+ * format, making it compatible with common cryptographic standards and easy to use in
80
+ * various cryptographic processes.
81
+ *
82
+ * The generated key includes the following components:
83
+ * - `kty`: Key Type, set to 'oct' for Octet Sequence.
84
+ * - `k`: The symmetric key component, base64url-encoded.
85
+ * - `kid`: Key ID, generated based on the JWK thumbprint.
86
+ * - `alg`: Algorithm, set to 'A128KW', 'A192KW', or 'A256KW' for AES Key Wrap with the
87
+ * specified key length.
88
+ *
89
+ * @example
90
+ * ```ts
91
+ * const length = 256; // Length of the key in bits (e.g., 128, 192, 256)
92
+ * const privateKey = await AesKw.generateKey({ length });
93
+ * ```
94
+ *
95
+ * @param params - The parameters for the key generation.
96
+ * @param params.length - The length of the key in bits. Common lengths are 128, 192, and 256 bits.
97
+ *
98
+ * @returns A Promise that resolves to the generated symmetric key in JWK format.
99
+ */
100
+ public static async generateKey({ length }: {
101
+ length: typeof AES_KEY_LENGTHS[number];
102
+ }): Promise<Jwk> {
103
+ // Validate the key length.
104
+ if (!AES_KEY_LENGTHS.includes(length as any)) {
105
+ throw new RangeError(`The key length is invalid: Must be ${AES_KEY_LENGTHS.join(', ')} bits`);
106
+ }
107
+
108
+ // Get the Web Crypto API interface.
109
+ const webCrypto = getWebcryptoSubtle() as SubtleCrypto;
110
+
111
+ // Generate a random private key.
112
+ // See https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues#usage_notes for
113
+ // an explanation for why Web Crypto generateKey() is used instead of getRandomValues().
114
+ const webCryptoKey = await webCrypto.generateKey( { name: 'AES-KW', length }, true, ['wrapKey', 'unwrapKey']);
115
+
116
+ // Export the private key in JWK format.
117
+ const { ext, key_ops, ...privateKey } = await webCrypto.exportKey('jwk', webCryptoKey) as Jwk;
118
+
119
+ // Compute the JWK thumbprint and set as the key ID.
120
+ privateKey.kid = await computeJwkThumbprint({ jwk: privateKey });
121
+
122
+ return privateKey;
123
+ }
124
+
125
+ /**
126
+ * Converts a private key from JSON Web Key (JWK) format to a raw byte array (Uint8Array).
127
+ *
128
+ * @remarks
129
+ * This method takes a symmetric key in JWK format and extracts its raw byte representation.
130
+ * It decodes the 'k' parameter of the JWK value, which represents the symmetric key in base64url
131
+ * encoding, into a byte array.
132
+ *
133
+ * @example
134
+ * ```ts
135
+ * const privateKey = { ... }; // A symmetric key in JWK format
136
+ * const privateKeyBytes = await AesKw.privateKeyToBytes({ privateKey });
137
+ * ```
138
+ *
139
+ * @param params - The parameters for the symmetric key conversion.
140
+ * @param params.privateKey - The symmetric key in JWK format.
141
+ *
142
+ * @returns A Promise that resolves to the symmetric key as a Uint8Array.
143
+ */
144
+ public static async privateKeyToBytes({ privateKey }: {
145
+ privateKey: Jwk;
146
+ }): Promise<Uint8Array> {
147
+ // Verify the provided JWK represents a valid oct private key.
148
+ if (!isOctPrivateJwk(privateKey)) {
149
+ throw new Error(`AesKw: The provided key is not a valid oct private key.`);
150
+ }
151
+
152
+ // Decode the provided private key to bytes.
153
+ const privateKeyBytes = Convert.base64Url(privateKey.k).toUint8Array();
154
+
155
+ return privateKeyBytes;
156
+ }
157
+
158
+ public static async unwrapKey({ wrappedKeyBytes, wrappedKeyAlgorithm, decryptionKey }:
159
+ UnwrapKeyParams
160
+ ): Promise<Jwk> {
161
+ if (!('alg' in decryptionKey && decryptionKey.alg)) {
162
+ throw new CryptoError(CryptoErrorCode.InvalidJwk, `The decryption key is missing the 'alg' property.`);
163
+ }
164
+
165
+ if (!['A128KW', 'A192KW', 'A256KW'].includes(decryptionKey.alg)) {
166
+ throw new CryptoError(CryptoErrorCode.AlgorithmNotSupported, `The 'decryptionKey' algorithm is not supported: ${decryptionKey.alg}`);
167
+ }
168
+
169
+ // Get the Web Crypto API interface.
170
+ const webCrypto = getWebcryptoSubtle() as SubtleCrypto;
171
+
172
+ // Import the decryption key for use with the Web Crypto API.
173
+ const decryptionCryptoKey = await webCrypto.importKey(
174
+ 'jwk', // key format
175
+ decryptionKey as JsonWebKey, // key data
176
+ { name: 'AES-KW' }, // algorithm identifier
177
+ true, // key is extractable
178
+ ['unwrapKey'] // key usages
179
+ );
180
+
181
+ // Map the private key's JOSE algorithm name to the Web Crypto API algorithm identifier.
182
+ const webCryptoAlgorithm = {
183
+ A128KW : 'AES-KW', A192KW : 'AES-KW', A256KW : 'AES-KW',
184
+ A128GCM : 'AES-GCM', A192GCM : 'AES-GCM', A256GCM : 'AES-GCM',
185
+ }[wrappedKeyAlgorithm];
186
+
187
+ if (!webCryptoAlgorithm) {
188
+ throw new CryptoError(CryptoErrorCode.AlgorithmNotSupported, `The 'wrappedKeyAlgorithm' is not supported: ${wrappedKeyAlgorithm}`);
189
+ }
190
+
191
+ // Unwrap the key using the Web Crypto API.
192
+ const unwrappedCryptoKey = await webCrypto.unwrapKey(
193
+ 'raw', // output format
194
+ wrappedKeyBytes.buffer, // key to unwrap
195
+ decryptionCryptoKey, // unwrapping key
196
+ 'AES-KW', // algorithm identifier
197
+ { name: webCryptoAlgorithm }, // unwrapped key algorithm identifier
198
+ true, // key is extractable
199
+ ['unwrapKey'] // key usages
200
+ );
201
+
202
+ // Export the unwrapped key in JWK format.
203
+ const { ext, key_ops, ...unwrappedJsonWebKey } = await webCrypto.exportKey('jwk', unwrappedCryptoKey);
204
+ const unwrappedKey = unwrappedJsonWebKey as Jwk;
205
+
206
+ // Compute the JWK thumbprint and set as the key ID.
207
+ unwrappedKey.kid = await computeJwkThumbprint({ jwk: unwrappedKey });
208
+
209
+ return unwrappedKey;
210
+ }
211
+
212
+ public static async wrapKey({ unwrappedKey, encryptionKey }:
213
+ WrapKeyParams
214
+ ): Promise<Uint8Array> {
215
+ if (!('alg' in encryptionKey && encryptionKey.alg)) {
216
+ throw new CryptoError(CryptoErrorCode.InvalidJwk, `The encryption key is missing the 'alg' property.`);
217
+ }
218
+
219
+ if (!['A128KW', 'A192KW', 'A256KW'].includes(encryptionKey.alg)) {
220
+ throw new CryptoError(CryptoErrorCode.AlgorithmNotSupported, `The 'encryptionKey' algorithm is not supported: ${encryptionKey.alg}`);
221
+ }
222
+
223
+ if (!('alg' in unwrappedKey && unwrappedKey.alg)) {
224
+ throw new CryptoError(CryptoErrorCode.InvalidJwk, `The private key to wrap is missing the 'alg' property.`);
225
+ }
226
+
227
+ // Get the Web Crypto API interface.
228
+ const webCrypto = getWebcryptoSubtle() as SubtleCrypto;
229
+
230
+ // Import the encryption key for use with the Web Crypto API.
231
+ const encryptionCryptoKey = await webCrypto.importKey(
232
+ 'jwk', // key format
233
+ encryptionKey as JsonWebKey, // key data
234
+ { name: 'AES-KW' }, // algorithm identifier
235
+ true, // key is extractable
236
+ ['wrapKey'] // key usages
237
+ );
238
+
239
+ // Map the private key's JOSE algorithm name to the Web Crypto API algorithm identifier.
240
+ const webCryptoAlgorithm = {
241
+ A128KW : 'AES-KW', A192KW : 'AES-KW', A256KW : 'AES-KW',
242
+ A128GCM : 'AES-GCM', A192GCM : 'AES-GCM', A256GCM : 'AES-GCM',
243
+ }[unwrappedKey.alg];
244
+
245
+ if (!webCryptoAlgorithm) {
246
+ throw new CryptoError(CryptoErrorCode.AlgorithmNotSupported, `The 'unwrappedKey' algorithm is not supported: ${unwrappedKey.alg}`);
247
+ }
248
+
249
+ // Import the private key to wrap for use with the Web Crypto API.
250
+ const unwrappedCryptoKey = await webCrypto.importKey(
251
+ 'jwk', // key format
252
+ unwrappedKey as JsonWebKey, // key data
253
+ { name: webCryptoAlgorithm }, // algorithm identifier
254
+ true, // key is extractable
255
+ ['unwrapKey'] // key usages
256
+ );
257
+
258
+ // Wrap the key using the Web Crypto API.
259
+ const wrappedKeyBuffer = await webCrypto.wrapKey(
260
+ 'raw', // output format
261
+ unwrappedCryptoKey, // key to wrap
262
+ encryptionCryptoKey, // wrapping key
263
+ 'AES-KW' // algorithm identifier
264
+ );
265
+
266
+ // Convert from ArrayBuffer to Uint8Array.
267
+ const wrappedKeyBytes = new Uint8Array(wrappedKeyBuffer);
268
+
269
+ return wrappedKeyBytes;
270
+ }
271
+ }