@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,729 @@
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 { HDKey } from 'ed25519-keygen/hdkey';
11
+ import { BearerDid, DidDht } from '@enbox/dids';
12
+ import { Convert, MemoryStore } from '@enbox/common';
13
+ import { wordlist } from '@scure/bip39/wordlists/english';
14
+ import { generateMnemonic, mnemonicToSeed, validateMnemonic } from '@scure/bip39';
15
+ import { AgentCryptoApi } from './crypto-api.js';
16
+ import { LocalKeyManager } from './local-key-manager.js';
17
+ import { isPortableDid } from './prototyping/dids/utils.js';
18
+ import { DeterministicKeyGenerator } from './utils-internal.js';
19
+ import { CompactJwe } from './prototyping/crypto/jose/jwe-compact.js';
20
+ /**
21
+ * Type guard function to check if a given object is an empty string or a string containing only
22
+ * whitespace.
23
+ *
24
+ * This is an internal utility function used to validate password inputs, ensuring they are not
25
+ * empty or filled with only whitespace characters, which are considered invalid for password
26
+ * purposes.
27
+ *
28
+ * @param obj - The object to be checked, typically expected to be a password string.
29
+ * @returns A boolean value indicating whether the object is an empty string or a string with only
30
+ * whitespace.
31
+ */
32
+ function isEmptyString(obj) {
33
+ return typeof obj !== 'string' || obj.trim().length === 0;
34
+ }
35
+ /**
36
+ * Type guard function to check if a given object conforms to the {@link IdentityVaultBackup}
37
+ * interface.
38
+ *
39
+ * This function is an internal utility meant to ensure the integrity and structure of the data
40
+ * assumed to be an {@link IdentityVaultBackup}. It verifies the presence and types of the
41
+ * `dateCreated`, `size`, and `data` properties, aligning with the expected structure of a backup
42
+ * object in the context of an {@link IdentityVault}.
43
+ *
44
+ * @param obj - The object to be verified against the {@link IdentityVaultBackup} interface.
45
+ * @returns A boolean value indicating whether the object is a valid {@link IdentityVaultBackup}.
46
+ */
47
+ function isIdentityVaultBackup(obj) {
48
+ return typeof obj === 'object' && obj !== null
49
+ && 'dateCreated' in obj && typeof obj.dateCreated === 'string'
50
+ && 'size' in obj && typeof obj.size === 'number'
51
+ && 'data' in obj && typeof obj.data === 'string';
52
+ }
53
+ /**
54
+ * Internal-only type guard function that checks if a given object conforms to the
55
+ * {@link IdentityVaultStatus} interface.
56
+ *
57
+ * This function is utilized within the {@link HdIdentityVault} implementation to ensure the
58
+ * integrity of the object representing the vault's status, verifying the presence and types of
59
+ * required properties. It aasserts the presence and correct types of `initialized`, `lastBackup`,
60
+ * and `lastRestore` properties, ensuring they align with the expected structure of an identity
61
+ * vault's status.
62
+ *
63
+ * @param obj - The object to be checked against the {{@link IdentityVaultStatus} interface.
64
+ * @returns A boolean indicating whether the object is an instance of {@link IdentityVaultStatus}.
65
+ */
66
+ function isIdentityVaultStatus(obj) {
67
+ return typeof obj === 'object' && obj !== null
68
+ && 'initialized' in obj && typeof obj.initialized === 'boolean'
69
+ && 'lastBackup' in obj
70
+ && 'lastRestore' in obj;
71
+ }
72
+ /**
73
+ * The `HdIdentityVault` class provides secure storage and management of identity data.
74
+ *
75
+ * The `HdIdentityVault` class implements the `IdentityVault` interface, providing secure storage
76
+ * and management of identity data with an added layer of security using Hierarchical Deterministic
77
+ * (HD) key derivation based on the SLIP-0010 standard for Ed25519 keys. It enhances identity
78
+ * protection by generating and securing the identity using a derived HD key, allowing for the
79
+ * deterministic regeneration of keys from a recovery phrase.
80
+ *
81
+ * The vault is capable of:
82
+ * - Secure initialization with a password and an optional recovery phrase, employing HD key
83
+ * derivation.
84
+ * - Encrypting the identity data using a derived content encryption key (CEK) which is securely
85
+ * encrypted and stored, accessible only by the correct password.
86
+ * - Securely backing up and restoring the vault’s contents, including the HD-derived keys and
87
+ * associated DID.
88
+ * - Locking and unlocking the vault, which encrypts and decrypts the CEK for secure access to the
89
+ * vault's contents.
90
+ * - Managing the DID associated with the identity, providing a secure identity layer for
91
+ * applications.
92
+ *
93
+ * Usage involves initializing the vault with a secure password (and optionally a recovery phrase),
94
+ * which then allows for the secure storage, backup, and retrieval of the identity data.
95
+ *
96
+ * Note: Ensure the password is strong and securely managed, as it is crucial for the security of the
97
+ * vault's encrypted contents.
98
+ *
99
+ * @example
100
+ * ```typescript
101
+ * const vault = new HdIdentityVault();
102
+ * await vault.initialize({ password: 'secure-unique-phrase', recoveryPhrase: 'twelve words ...' });
103
+ * const backup = await vault.backup();
104
+ * await vault.restore({ backup, password: 'secure-unique-phrase' });
105
+ * ```
106
+ */
107
+ export class HdIdentityVault {
108
+ /**
109
+ * Constructs an instance of `HdIdentityVault`, initializing the key derivation factor and data
110
+ * store. It sets the default key derivation work factor and initializes the internal data store,
111
+ * either with the provided store or a default in-memory store. It also establishes the initial
112
+ * status of the vault as uninitialized and locked.
113
+ *
114
+ * @param params - Optional parameters when constructing a vault instance.
115
+ * @param params.keyDerivationWorkFactor - Optionally set the computational effort for key derivation.
116
+ * @param params.store - Optionally specify a custom key-value store for vault data.
117
+ */
118
+ constructor({ keyDerivationWorkFactor, store } = {}) {
119
+ /** Provides cryptographic functions needed for secure storage and management of the vault. */
120
+ this.crypto = new AgentCryptoApi();
121
+ this._keyDerivationWorkFactor = keyDerivationWorkFactor !== null && keyDerivationWorkFactor !== void 0 ? keyDerivationWorkFactor : 210000;
122
+ this._store = store !== null && store !== void 0 ? store : new MemoryStore();
123
+ }
124
+ /**
125
+ * Creates a backup of the vault's current state, including the encrypted DID and content
126
+ * encryption key, and returns it as an `IdentityVaultBackup` object. The backup includes a
127
+ * Base64Url-encoded string representing the vault's encrypted data, encapsulating the
128
+ * {@link PortableDid}, the content encryption key, and the vault's status.
129
+ *
130
+ * This method ensures that the vault is initialized and unlocked before proceeding with the
131
+ * backup operation.
132
+ *
133
+ * @throws Error if the vault is not initialized or is locked, preventing the backup.
134
+ * @returns A promise that resolves to the `IdentityVaultBackup` object containing the vault's
135
+ * encrypted backup data.
136
+ */
137
+ backup() {
138
+ return __awaiter(this, void 0, void 0, function* () {
139
+ // Verify the identity vault has already been initialized and unlocked.
140
+ if (this.isLocked() || (yield this.isInitialized()) === false) {
141
+ throw new Error('HdIdentityVault: Unable to proceed with the backup operation because the identity vault ' +
142
+ 'has not been initialized and unlocked. Please ensure the vault is properly initialized ' +
143
+ 'with a secure password before attempting to backup its contents.');
144
+ }
145
+ // Encode the encrypted CEK and DID as a single Base64Url string.
146
+ const backupData = {
147
+ did: yield this.getStoredDid(),
148
+ contentEncryptionKey: yield this.getStoredContentEncryptionKey(),
149
+ status: yield this.getStatus()
150
+ };
151
+ const backupDataString = Convert.object(backupData).toBase64Url();
152
+ // Create a backup object containing the encrypted vault contents.
153
+ const backup = {
154
+ data: backupDataString,
155
+ dateCreated: new Date().toISOString(),
156
+ size: backupDataString.length
157
+ };
158
+ // Update the last backup timestamp in the data store.
159
+ yield this.setStatus({ lastBackup: backup.dateCreated });
160
+ return backup;
161
+ });
162
+ }
163
+ /**
164
+ * Changes the password used to secure the vault.
165
+ *
166
+ * This method decrypts the existing content encryption key (CEK) with the old password, then
167
+ * re-encrypts it with the new password, updating the vault's stored encrypted CEK. It ensures
168
+ * that the vault is initialized and unlocks the vault if the password is successfully changed.
169
+ *
170
+ * @param params - Parameters required for changing the vault password.
171
+ * @param params.oldPassword - The current password used to unlock the vault.
172
+ * @param params.newPassword - The new password to replace the existing one.
173
+ * @throws Error if the vault is not initialized or the old password is incorrect.
174
+ * @returns A promise that resolves when the password change is complete.
175
+ */
176
+ changePassword({ oldPassword, newPassword }) {
177
+ return __awaiter(this, void 0, void 0, function* () {
178
+ // Verify the identity vault has already been initialized.
179
+ if ((yield this.isInitialized()) === false) {
180
+ throw new Error('HdIdentityVault: Unable to proceed with the change password operation because the ' +
181
+ 'identity vault has not been initialized. Please ensure the vault is properly ' +
182
+ 'initialized with a secure password before trying again.');
183
+ }
184
+ // Lock the vault.
185
+ yield this.lock();
186
+ // Retrieve the content encryption key (CEK) record as a compact JWE from the data store.
187
+ const cekJwe = yield this.getStoredContentEncryptionKey();
188
+ // Decrypt the compact JWE using the given `oldPassword` to verify it is correct.
189
+ let protectedHeader;
190
+ let contentEncryptionKey;
191
+ try {
192
+ let contentEncryptionKeyBytes;
193
+ ({ plaintext: contentEncryptionKeyBytes, protectedHeader } = yield CompactJwe.decrypt({
194
+ jwe: cekJwe,
195
+ key: Convert.string(oldPassword).toUint8Array(),
196
+ crypto: this.crypto,
197
+ keyManager: new LocalKeyManager()
198
+ }));
199
+ contentEncryptionKey = Convert.uint8Array(contentEncryptionKeyBytes).toObject();
200
+ }
201
+ catch (error) {
202
+ throw new Error(`HdIdentityVault: Unable to change the vault password due to an incorrectly entered old password.`);
203
+ }
204
+ // Re-encrypt the vault content encryption key (CEK) using the new password.
205
+ const newCekJwe = yield CompactJwe.encrypt({
206
+ key: Convert.string(newPassword).toUint8Array(),
207
+ protectedHeader,
208
+ plaintext: Convert.object(contentEncryptionKey).toUint8Array(),
209
+ crypto: this.crypto,
210
+ keyManager: new LocalKeyManager()
211
+ });
212
+ // Update the vault with the new CEK JWE.
213
+ yield this._store.set('contentEncryptionKey', newCekJwe);
214
+ // Update the vault CEK in memory, effectively unlocking the vault.
215
+ this._contentEncryptionKey = contentEncryptionKey;
216
+ });
217
+ }
218
+ /**
219
+ * Retrieves the DID (Decentralized Identifier) associated with the vault.
220
+ *
221
+ * This method ensures the vault is initialized and unlocked before decrypting and returning the
222
+ * DID. The DID is stored encrypted and is decrypted using the vault's content encryption key.
223
+ *
224
+ * @throws Error if the vault is not initialized, is locked, or the DID cannot be decrypted.
225
+ * @returns A promise that resolves with a {@link BearerDid}.
226
+ */
227
+ getDid() {
228
+ return __awaiter(this, void 0, void 0, function* () {
229
+ // Verify the identity vault is unlocked.
230
+ if (this.isLocked()) {
231
+ throw new Error(`HdIdentityVault: Vault has not been initialized and unlocked.`);
232
+ }
233
+ // Retrieve the encrypted DID record as compact JWE from the vault store.
234
+ const didJwe = yield this.getStoredDid();
235
+ // Decrypt the compact JWE to obtain the PortableDid as a byte array.
236
+ const { plaintext: portableDidBytes } = yield CompactJwe.decrypt({
237
+ jwe: didJwe,
238
+ key: this._contentEncryptionKey,
239
+ crypto: this.crypto,
240
+ keyManager: new LocalKeyManager()
241
+ });
242
+ // Convert the DID from a byte array to PortableDid format.
243
+ const portableDid = Convert.uint8Array(portableDidBytes).toObject();
244
+ if (!isPortableDid(portableDid)) {
245
+ throw new Error('HdIdentityVault: Unable to decode malformed DID in identity vault');
246
+ }
247
+ // Return the DID in Bearer DID format.
248
+ return yield BearerDid.import({ portableDid });
249
+ });
250
+ }
251
+ /**
252
+ * Fetches the current status of the `HdIdentityVault`, providing details on whether it's
253
+ * initialized and the timestamps of the last backup and restore operations.
254
+ *
255
+ * @returns A promise that resolves with the current status of the `HdIdentityVault`, detailing
256
+ * its initialization, lock state, and the timestamps of the last backup and restore.
257
+ */
258
+ getStatus() {
259
+ return __awaiter(this, void 0, void 0, function* () {
260
+ const storedStatus = yield this._store.get('vaultStatus');
261
+ // On the first run, the store will not contain an IdentityVaultStatus object yet, so return an
262
+ // uninitialized status.
263
+ if (!storedStatus) {
264
+ return {
265
+ initialized: false,
266
+ lastBackup: null,
267
+ lastRestore: null
268
+ };
269
+ }
270
+ const vaultStatus = Convert.string(storedStatus).toObject();
271
+ if (!isIdentityVaultStatus(vaultStatus)) {
272
+ throw new Error('HdIdentityVault: Invalid IdentityVaultStatus object in store');
273
+ }
274
+ return vaultStatus;
275
+ });
276
+ }
277
+ /**
278
+ * Initializes the `HdIdentityVault` with a password and an optional recovery phrase.
279
+ *
280
+ * If a recovery phrase is not provided, a new one is generated. This process sets up the vault,
281
+ * deriving the necessary cryptographic keys and preparing the vault for use. It ensures the vault
282
+ * is ready to securely store and manage identity data.
283
+ *
284
+ * @example
285
+ * ```ts
286
+ * const identityVault = new HdIdentityVault();
287
+ * const recoveryPhrase = await identityVault.initialize({
288
+ * password: 'your-secure-phrase'
289
+ * });
290
+ * console.log('Vault initialized. Recovery phrase:', recoveryPhrase);
291
+ * ```
292
+ *
293
+ * @param params - The initialization parameters.
294
+ * @param params.password - The password used to secure the vault.
295
+ * @param params.recoveryPhrase - An optional 12-word recovery phrase for key derivation. If
296
+ * omitted, a new recovery is generated.
297
+ * @returns A promise that resolves with the recovery phrase used during the initialization, which
298
+ * should be securely stored by the user.
299
+ */
300
+ initialize({ password, recoveryPhrase, dwnEndpoints }) {
301
+ return __awaiter(this, void 0, void 0, function* () {
302
+ /**
303
+ * STEP 0: Validate the input parameters and verify the identity vault is not already
304
+ * initialized.
305
+ */
306
+ // Verify that the identity vault was not previously initialized.
307
+ if (yield this.isInitialized()) {
308
+ throw new Error(`HdIdentityVault: Vault has already been initialized.`);
309
+ }
310
+ // Verify that the password is not empty.
311
+ if (isEmptyString(password)) {
312
+ throw new Error(`HdIdentityVault: The password is required and cannot be blank. Please provide a ' +
313
+ 'valid, non-empty password.`);
314
+ }
315
+ // If provided, verify that the recovery phrase is not empty.
316
+ if (recoveryPhrase && isEmptyString(recoveryPhrase)) {
317
+ throw new Error(`HdIdentityVault: The password is required and cannot be blank. Please provide a ' +
318
+ 'valid, non-empty password.`);
319
+ }
320
+ /**
321
+ * STEP 1: Derive a Hierarchical Deterministic (HD) key pair from the given (or generated)
322
+ * recoveryPhrase.
323
+ */
324
+ // Generate a 12-word (128-bit) mnemonic, if one was not provided.
325
+ recoveryPhrase !== null && recoveryPhrase !== void 0 ? recoveryPhrase : (recoveryPhrase = generateMnemonic(wordlist, 128));
326
+ // Validate the mnemonic for being 12-24 words contained in `wordlist`.
327
+ if (!validateMnemonic(recoveryPhrase, wordlist)) {
328
+ throw new Error('HdIdentityVault: The provided recovery phrase is invalid. Please ensure that the ' +
329
+ 'recovery phrase is a correctly formatted series of 12 words.');
330
+ }
331
+ // Derive a root seed from the mnemonic.
332
+ const rootSeed = yield mnemonicToSeed(recoveryPhrase);
333
+ // Derive a root key for the DID from the root seed.
334
+ const rootHdKey = HDKey.fromMasterSeed(rootSeed);
335
+ /**
336
+ * STEP 2: Derive the vault HD key pair from the root key.
337
+ */
338
+ // The vault HD key is derived using account 0 and index 0 so that it can be
339
+ // deterministically re-derived. The vault key pair serves as input keying material for:
340
+ // - deriving the vault content encryption key (CEK)
341
+ // - deriving the salt that serves as input to derive the key that encrypts the vault CEK
342
+ const vaultHdKey = rootHdKey.derive(`m/44'/0'/0'/0'/0'`);
343
+ /**
344
+ * STEP 3: Derive the vault Content Encryption Key (CEK) from the vault private
345
+ * key and a non-secret static info value.
346
+ */
347
+ // A non-secret static info value is combined with the vault private key as input to HKDF
348
+ // (Hash-based Key Derivation Function) to derive a 32-byte content encryption key (CEK).
349
+ const contentEncryptionKey = yield this.crypto.deriveKey({
350
+ algorithm: 'HKDF-512',
351
+ baseKeyBytes: vaultHdKey.privateKey,
352
+ salt: '',
353
+ info: 'vault_cek',
354
+ derivedKeyAlgorithm: 'A256GCM' // derived key algorithm
355
+ });
356
+ /**
357
+ * STEP 4: Using the given `password` and a `salt` derived from the vault public key, encrypt
358
+ * the vault CEK and store it in the data store as a compact JWE.
359
+ */
360
+ // A non-secret static info value is combined with the vault public key as input to HKDF
361
+ // (Hash-based Key Derivation Function) to derive a new 32-byte salt.
362
+ const saltInput = yield this.crypto.deriveKeyBytes({
363
+ algorithm: 'HKDF-512',
364
+ baseKeyBytes: vaultHdKey.publicKey,
365
+ salt: '',
366
+ info: 'vault_unlock_salt',
367
+ length: 256, // derived key length, in bits
368
+ });
369
+ // Construct the JWE header.
370
+ const cekJweProtectedHeader = {
371
+ alg: 'PBES2-HS512+A256KW',
372
+ enc: 'A256GCM',
373
+ cty: 'text/plain',
374
+ p2c: this._keyDerivationWorkFactor,
375
+ p2s: Convert.uint8Array(saltInput).toBase64Url()
376
+ };
377
+ // Encrypt the vault content encryption key (CEK) to compact JWE format.
378
+ const cekJwe = yield CompactJwe.encrypt({
379
+ key: Convert.string(password).toUint8Array(),
380
+ protectedHeader: cekJweProtectedHeader,
381
+ plaintext: Convert.object(contentEncryptionKey).toUint8Array(),
382
+ crypto: this.crypto,
383
+ keyManager: new LocalKeyManager()
384
+ });
385
+ // Store the compact JWE in the data store.
386
+ yield this._store.set('contentEncryptionKey', cekJwe);
387
+ /**
388
+ * STEP 5: Create a DID using identity, signing, and encryption keys derived from the root key.
389
+ */
390
+ // Derive the identity key pair using index 0 and convert to JWK format.
391
+ // Note: The account is set to Unix epoch time so that in the future, the keys for a DID DHT
392
+ // document can be deterministically derived based on the versionId returned in a DID
393
+ // resolution result.
394
+ const identityHdKey = rootHdKey.derive(`m/44'/0'/1708523827'/0'/0'`);
395
+ const identityPrivateKey = yield this.crypto.bytesToPrivateKey({
396
+ algorithm: 'Ed25519',
397
+ privateKeyBytes: identityHdKey.privateKey
398
+ });
399
+ // Derive the signing key using index 1 and convert to JWK format.
400
+ let signingHdKey = rootHdKey.derive(`m/44'/0'/1708523827'/0'/1'`);
401
+ const signingPrivateKey = yield this.crypto.bytesToPrivateKey({
402
+ algorithm: 'Ed25519',
403
+ privateKeyBytes: signingHdKey.privateKey
404
+ });
405
+ // TODO: Enable this once DID DHT supports X25519 keys.
406
+ // Derive the encryption key using index 1 and convert to JWK format.
407
+ // const encryptionHdKey = rootHdKey.derive(`m/44'/0'/1708523827'/0'/1'`);
408
+ // const encryptionKeyEd25519 = await this.crypto.bytesToPrivateKey({
409
+ // algorithm : 'Ed25519',
410
+ // privateKeyBytes : encryptionHdKey.privateKey
411
+ // });
412
+ // const encryptionPrivateKey = await Ed25519.convertPrivateKeyToX25519({ privateKey: encryptionKeyEd25519 });
413
+ // Add the identity and signing keys to the deterministic key generator so that when the DID is
414
+ // created it will use the derived keys.
415
+ const deterministicKeyGenerator = new DeterministicKeyGenerator();
416
+ yield deterministicKeyGenerator.addPredefinedKeys({
417
+ privateKeys: [identityPrivateKey, signingPrivateKey]
418
+ });
419
+ // Create the DID using the derived identity, signing, and encryption keys.
420
+ const options = {
421
+ verificationMethods: [
422
+ {
423
+ algorithm: 'Ed25519',
424
+ id: 'sig',
425
+ purposes: ['assertionMethod', 'authentication']
426
+ },
427
+ ]
428
+ };
429
+ if (dwnEndpoints && !!dwnEndpoints.length) {
430
+ options.services = [
431
+ {
432
+ id: 'dwn',
433
+ type: 'DecentralizedWebNode',
434
+ serviceEndpoint: dwnEndpoints,
435
+ enc: '#enc',
436
+ sig: '#sig',
437
+ }
438
+ ];
439
+ }
440
+ const did = yield DidDht.create({ keyManager: deterministicKeyGenerator, options });
441
+ /**
442
+ * STEP 6: Convert the DID to portable format and store it in the data store as a
443
+ * compact JWE.
444
+ */
445
+ // Convert the DID to a portable format.
446
+ const portableDid = yield did.export();
447
+ // Construct the JWE header.
448
+ const didJweProtectedHeader = {
449
+ alg: 'dir',
450
+ enc: 'A256GCM',
451
+ cty: 'json'
452
+ };
453
+ // Encrypt the DID to compact JWE format.
454
+ const didJwe = yield CompactJwe.encrypt({
455
+ key: contentEncryptionKey,
456
+ plaintext: Convert.object(portableDid).toUint8Array(),
457
+ protectedHeader: didJweProtectedHeader,
458
+ crypto: this.crypto,
459
+ keyManager: new LocalKeyManager()
460
+ });
461
+ // Store the compact JWE in the data store.
462
+ yield this._store.set('did', didJwe);
463
+ /**
464
+ * STEP 7: Set the vault CEK (effectively unlocking the vault), set the status to initialized,
465
+ * and return the mnemonic used to generate the vault key.
466
+ */
467
+ this._contentEncryptionKey = contentEncryptionKey;
468
+ yield this.setStatus({ initialized: true });
469
+ // Return the recovery phrase in case it was generated so that it can be displayed to the user
470
+ // for safekeeping.
471
+ return recoveryPhrase;
472
+ });
473
+ }
474
+ /**
475
+ * Determines whether the vault has been initialized.
476
+ *
477
+ * This method checks the vault's current status to determine if it has been
478
+ * initialized. Initialization is a prerequisite for most operations on the vault,
479
+ * ensuring that it is ready for use.
480
+ *
481
+ * @example
482
+ * ```ts
483
+ * const isInitialized = await identityVault.isInitialized();
484
+ * console.log('Is the vault initialized?', isInitialized);
485
+ * ```
486
+ *
487
+ * @returns A promise that resolves to `true` if the vault has been initialized, otherwise `false`.
488
+ */
489
+ isInitialized() {
490
+ return __awaiter(this, void 0, void 0, function* () {
491
+ return this.getStatus().then(({ initialized }) => initialized);
492
+ });
493
+ }
494
+ /**
495
+ * Checks if the vault is currently locked.
496
+ *
497
+ * This method assesses the vault's current state to determine if it is locked.
498
+ * A locked vault restricts access to its contents, requiring the correct password
499
+ * to unlock and access the stored identity data. The vault must be unlocked to
500
+ * perform operations that access or modify its contents.
501
+ *
502
+ * @example
503
+ * ```ts
504
+ * const isLocked = await identityVault.isLocked();
505
+ * console.log('Is the vault locked?', isLocked);
506
+ * ```
507
+ *
508
+ * @returns `true` if the vault is locked, otherwise `false`.
509
+ */
510
+ isLocked() {
511
+ return !this._contentEncryptionKey;
512
+ }
513
+ /**
514
+ * Locks the `HdIdentityVault`, securing its contents by clearing the in-memory encryption key.
515
+ *
516
+ * This method ensures that the vault's sensitive data cannot be accessed without unlocking the
517
+ * vault again with the correct password. It's an essential security feature for safeguarding
518
+ * the vault's contents against unauthorized access.
519
+ *
520
+ * @example
521
+ * ```ts
522
+ * const identityVault = new HdIdentityVault();
523
+ * await identityVault.lock();
524
+ * console.log('Vault is now locked.');
525
+ * ```
526
+ * @throws An error if the identity vault has not been initialized.
527
+ * @returns A promise that resolves when the vault is successfully locked.
528
+ */
529
+ lock() {
530
+ return __awaiter(this, void 0, void 0, function* () {
531
+ // Verify the identity vault has already been initialized.
532
+ if ((yield this.isInitialized()) === false) {
533
+ throw new Error(`HdIdentityVault: Lock operation failed. Vault has not been initialized.`);
534
+ }
535
+ // Clear the vault content encryption key (CEK), effectively locking the vault.
536
+ if (this._contentEncryptionKey)
537
+ this._contentEncryptionKey.k = '';
538
+ this._contentEncryptionKey = undefined;
539
+ });
540
+ }
541
+ /**
542
+ * Restores the vault's data from a backup object, decrypting and reinitializing the vault's
543
+ * content with the provided backup data.
544
+ *
545
+ * This operation is crucial for data recovery scenarios, allowing users to regain access to their
546
+ * encrypted data using a previously saved backup and their password.
547
+ *
548
+ * @example
549
+ * ```ts
550
+ * const identityVault = new HdIdentityVault();
551
+ * await identityVault.initialize({ password: 'your-secure-phrase' });
552
+ * // Create a backup of the vault's contents.
553
+ * const backup = await identityVault.backup();
554
+ * // Restore the vault with the same password.
555
+ * await identityVault.restore({ backup: backup, password: 'your-secure-phrase' });
556
+ * console.log('Vault restored successfully.');
557
+ * ```
558
+ *
559
+ * @param params - The parameters required for the restore operation.
560
+ * @param params.backup - The backup object containing the encrypted vault data.
561
+ * @param params.password - The password used to encrypt the backup, necessary for decryption.
562
+ * @returns A promise that resolves when the vault has been successfully restored.
563
+ * @throws An error if the backup object is invalid or if the password is incorrect.
564
+ */
565
+ restore({ backup, password }) {
566
+ return __awaiter(this, void 0, void 0, function* () {
567
+ // Validate the backup object.
568
+ if (!isIdentityVaultBackup(backup)) {
569
+ throw new Error(`HdIdentityVault: Restore operation failed due to invalid backup object.`);
570
+ }
571
+ // Temporarily save the status and contents of the data store while attempting to restore the
572
+ // backup so that they are not lost in case the restore operation fails.
573
+ let previousStatus;
574
+ let previousContentEncryptionKey;
575
+ let previousDid;
576
+ try {
577
+ previousDid = yield this.getStoredDid();
578
+ previousContentEncryptionKey = yield this.getStoredContentEncryptionKey();
579
+ previousStatus = yield this.getStatus();
580
+ }
581
+ catch (_a) {
582
+ throw new Error('HdIdentityVault: The restore operation cannot proceed because the existing vault ' +
583
+ 'contents are missing or inaccessible. If the problem persists consider re-initializing ' +
584
+ 'the vault and retrying the restore.');
585
+ }
586
+ try {
587
+ // Convert the backup data to a JSON object.
588
+ const backupData = Convert.base64Url(backup.data).toObject();
589
+ // Restore the backup to the data store.
590
+ yield this._store.set('did', backupData.did);
591
+ yield this._store.set('contentEncryptionKey', backupData.contentEncryptionKey);
592
+ yield this.setStatus(backupData.status);
593
+ // Attempt to unlock the vault with the given `password`.
594
+ yield this.unlock({ password });
595
+ }
596
+ catch (error) {
597
+ // If the restore operation fails, revert the data store to the status and contents that were
598
+ // saved before the restore operation was attempted.
599
+ yield this.setStatus(previousStatus);
600
+ yield this._store.set('contentEncryptionKey', previousContentEncryptionKey);
601
+ yield this._store.set('did', previousDid);
602
+ throw new Error('HdIdentityVault: Restore operation failed due to invalid backup data or an incorrect ' +
603
+ 'password. Please verify the password is correct for the provided backup and try again.');
604
+ }
605
+ // Update the last restore timestamp in the data store.
606
+ yield this.setStatus({ lastRestore: new Date().toISOString() });
607
+ });
608
+ }
609
+ /**
610
+ * Unlocks the vault by decrypting the stored content encryption key (CEK) using the provided
611
+ * password.
612
+ *
613
+ * This method is essential for accessing the vault's encrypted contents, enabling the decryption
614
+ * of stored data and the execution of further operations requiring the vault to be unlocked.
615
+ *
616
+ * @example
617
+ * ```ts
618
+ * const identityVault = new HdIdentityVault();
619
+ * await identityVault.initialize({ password: 'your-initial-phrase' });
620
+ * // Unlock the vault with the correct password before accessing its contents
621
+ * await identityVault.unlock({ password: 'your-initial-phrase' });
622
+ * console.log('Vault unlocked successfully.');
623
+ * ```
624
+ *
625
+ *
626
+ * @param params - The parameters required for the unlock operation.
627
+ * @param params.password - The password used to encrypt the vault's CEK, necessary for
628
+ * decryption.
629
+ * @returns A promise that resolves when the vault has been successfully unlocked.
630
+ * @throws An error if the vault has not been initialized or if the provided password is
631
+ * incorrect.
632
+ */
633
+ unlock({ password }) {
634
+ return __awaiter(this, void 0, void 0, function* () {
635
+ // Lock the vault.
636
+ yield this.lock();
637
+ // Retrieve the content encryption key (CEK) record as a compact JWE from the data store.
638
+ const cekJwe = yield this.getStoredContentEncryptionKey();
639
+ // Decrypt the compact JWE.
640
+ try {
641
+ const { plaintext: contentEncryptionKeyBytes } = yield CompactJwe.decrypt({
642
+ jwe: cekJwe,
643
+ key: Convert.string(password).toUint8Array(),
644
+ crypto: this.crypto,
645
+ keyManager: new LocalKeyManager()
646
+ });
647
+ const contentEncryptionKey = Convert.uint8Array(contentEncryptionKeyBytes).toObject();
648
+ // Save the content encryption key in memory, thereby unlocking the vault.
649
+ this._contentEncryptionKey = contentEncryptionKey;
650
+ }
651
+ catch (error) {
652
+ throw new Error(`HdIdentityVault: Unable to unlock the vault due to an incorrect password.`);
653
+ }
654
+ });
655
+ }
656
+ /**
657
+ * Retrieves the Decentralized Identifier (DID) associated with the identity vault from the vault
658
+ * store.
659
+ *
660
+ * This DID is encrypted in compact JWE format and needs to be decrypted after the vault is
661
+ * unlocked. The method is intended to be used internally within the HdIdentityVault class to access
662
+ * the encrypted PortableDid.
663
+ *
664
+ * @returns A promise that resolves to the encrypted DID stored in the vault as a compact JWE.
665
+ * @throws Will throw an error if the DID cannot be retrieved from the vault.
666
+ */
667
+ getStoredDid() {
668
+ return __awaiter(this, void 0, void 0, function* () {
669
+ // Retrieve the DID record as a compact JWE from the data store.
670
+ const didJwe = yield this._store.get('did');
671
+ if (!didJwe) {
672
+ throw new Error('HdIdentityVault: Unable to retrieve the DID record from the vault. Please check the ' +
673
+ 'vault status and if the problem persists consider re-initializing the vault and ' +
674
+ 'restoring the contents from a previous backup.');
675
+ }
676
+ return didJwe;
677
+ });
678
+ }
679
+ /**
680
+ * Retrieves the encrypted Content Encryption Key (CEK) from the vault's storage.
681
+ *
682
+ * This CEK is used for encrypting and decrypting the vault's contents. It is stored as a
683
+ * compact JWE and should be decrypted with the user's password to be used for further
684
+ * cryptographic operations.
685
+ *
686
+ * @returns A promise that resolves to the stored CEK as a string in compact JWE format.
687
+ * @throws Will throw an error if the CEK cannot be retrieved, indicating potential issues with
688
+ * the vault's integrity or state.
689
+ */
690
+ getStoredContentEncryptionKey() {
691
+ return __awaiter(this, void 0, void 0, function* () {
692
+ // Retrieve the content encryption key (CEK) record as a compact JWE from the data store.
693
+ const cekJwe = yield this._store.get('contentEncryptionKey');
694
+ if (!cekJwe) {
695
+ throw new Error('HdIdentityVault: Unable to retrieve the Content Encryption Key record from the vault. ' +
696
+ 'Please check the vault status and if the problem persists consider re-initializing the ' +
697
+ 'vault and restoring the contents from a previous backup.');
698
+ }
699
+ return cekJwe;
700
+ });
701
+ }
702
+ /**
703
+ * Updates the status of the `HdIdentityVault`, reflecting changes in its initialization, lock
704
+ * state, and the timestamps of the last backup and restore operations.
705
+ *
706
+ * This method directly manipulates the internal state stored in the vault's key-value store.
707
+ *
708
+ * @param params - The status properties to be updated.
709
+ * @param params.initialized - Updates the initialization state of the vault.
710
+ * @param params.lastBackup - Updates the timestamp of the last successful backup.
711
+ * @param params.lastRestore - Updates the timestamp of the last successful restore.
712
+ * @returns A promise that resolves to a boolean indicating successful status update.
713
+ * @throws Will throw an error if the status cannot be updated in the key-value store.
714
+ */
715
+ setStatus({ initialized, lastBackup, lastRestore }) {
716
+ return __awaiter(this, void 0, void 0, function* () {
717
+ // Get the current status values from the store, if any.
718
+ let vaultStatus = yield this.getStatus();
719
+ // Update the status properties with new values specified, if any.
720
+ vaultStatus.initialized = initialized !== null && initialized !== void 0 ? initialized : vaultStatus.initialized;
721
+ vaultStatus.lastBackup = lastBackup !== null && lastBackup !== void 0 ? lastBackup : vaultStatus.lastBackup;
722
+ vaultStatus.lastRestore = lastRestore !== null && lastRestore !== void 0 ? lastRestore : vaultStatus.lastRestore;
723
+ // Write the changes to the store.
724
+ yield this._store.set('vaultStatus', JSON.stringify(vaultStatus));
725
+ return true;
726
+ });
727
+ }
728
+ }
729
+ //# sourceMappingURL=hd-identity-vault.js.map