@fgv/ts-extras 5.1.0-4 → 5.1.0-41

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 (368) hide show
  1. package/dist/index.browser.js +4 -2
  2. package/dist/index.browser.js.map +1 -0
  3. package/dist/index.js.map +1 -0
  4. package/dist/packlets/ai-assist/apiClient.js +886 -158
  5. package/dist/packlets/ai-assist/apiClient.js.map +1 -0
  6. package/dist/packlets/ai-assist/chatRequestBuilders.js +282 -0
  7. package/dist/packlets/ai-assist/chatRequestBuilders.js.map +1 -0
  8. package/dist/packlets/ai-assist/converters.js +47 -3
  9. package/dist/packlets/ai-assist/converters.js.map +1 -0
  10. package/dist/packlets/ai-assist/embeddingClient.js +345 -0
  11. package/dist/packlets/ai-assist/embeddingClient.js.map +1 -0
  12. package/dist/packlets/ai-assist/endpoint.js +78 -0
  13. package/dist/packlets/ai-assist/endpoint.js.map +1 -0
  14. package/dist/packlets/ai-assist/http.js +75 -0
  15. package/dist/packlets/ai-assist/http.js.map +1 -0
  16. package/dist/packlets/ai-assist/imageOptionsResolver.js +193 -0
  17. package/dist/packlets/ai-assist/imageOptionsResolver.js.map +1 -0
  18. package/dist/packlets/ai-assist/index.js +10 -4
  19. package/dist/packlets/ai-assist/index.js.map +1 -0
  20. package/dist/packlets/ai-assist/jsonCompletion.js +93 -0
  21. package/dist/packlets/ai-assist/jsonCompletion.js.map +1 -0
  22. package/dist/packlets/ai-assist/jsonResponse.js +149 -0
  23. package/dist/packlets/ai-assist/jsonResponse.js.map +1 -0
  24. package/dist/packlets/ai-assist/model.js +193 -9
  25. package/dist/packlets/ai-assist/model.js.map +1 -0
  26. package/dist/packlets/ai-assist/registry.js +314 -12
  27. package/dist/packlets/ai-assist/registry.js.map +1 -0
  28. package/dist/packlets/ai-assist/sseParser.js +123 -0
  29. package/dist/packlets/ai-assist/sseParser.js.map +1 -0
  30. package/dist/packlets/ai-assist/streamingAdapters/anthropic.js +368 -0
  31. package/dist/packlets/ai-assist/streamingAdapters/anthropic.js.map +1 -0
  32. package/dist/packlets/ai-assist/streamingAdapters/clientToolContinuationBuilder.js +687 -0
  33. package/dist/packlets/ai-assist/streamingAdapters/clientToolContinuationBuilder.js.map +1 -0
  34. package/dist/packlets/ai-assist/streamingAdapters/common.js +187 -0
  35. package/dist/packlets/ai-assist/streamingAdapters/common.js.map +1 -0
  36. package/dist/packlets/ai-assist/streamingAdapters/gemini.js +204 -0
  37. package/dist/packlets/ai-assist/streamingAdapters/gemini.js.map +1 -0
  38. package/dist/packlets/ai-assist/streamingAdapters/openaiChat.js +170 -0
  39. package/dist/packlets/ai-assist/streamingAdapters/openaiChat.js.map +1 -0
  40. package/dist/packlets/ai-assist/streamingAdapters/openaiResponses.js +386 -0
  41. package/dist/packlets/ai-assist/streamingAdapters/openaiResponses.js.map +1 -0
  42. package/dist/packlets/ai-assist/streamingAdapters/proxy.js +173 -0
  43. package/dist/packlets/ai-assist/streamingAdapters/proxy.js.map +1 -0
  44. package/dist/packlets/ai-assist/streamingClient.js +140 -0
  45. package/dist/packlets/ai-assist/streamingClient.js.map +1 -0
  46. package/dist/packlets/ai-assist/thinkingOptionsResolver.js +288 -0
  47. package/dist/packlets/ai-assist/thinkingOptionsResolver.js.map +1 -0
  48. package/dist/packlets/ai-assist/toolFormats.js +106 -10
  49. package/dist/packlets/ai-assist/toolFormats.js.map +1 -0
  50. package/dist/packlets/conversion/converters.js +35 -1
  51. package/dist/packlets/conversion/converters.js.map +1 -0
  52. package/dist/packlets/conversion/index.js.map +1 -0
  53. package/dist/packlets/crypto-utils/constants.js.map +1 -0
  54. package/dist/packlets/crypto-utils/converters.js +42 -4
  55. package/dist/packlets/crypto-utils/converters.js.map +1 -0
  56. package/dist/packlets/crypto-utils/directEncryptionProvider.js.map +1 -0
  57. package/dist/packlets/crypto-utils/encryptedFile.js.map +1 -0
  58. package/dist/packlets/crypto-utils/hpkeProvider.js +355 -0
  59. package/dist/packlets/crypto-utils/hpkeProvider.js.map +1 -0
  60. package/dist/packlets/crypto-utils/index.browser.js +10 -2
  61. package/dist/packlets/crypto-utils/index.browser.js.map +1 -0
  62. package/dist/packlets/crypto-utils/index.js +6 -0
  63. package/dist/packlets/crypto-utils/index.js.map +1 -0
  64. package/dist/packlets/crypto-utils/keyPairAlgorithmParams.js +71 -0
  65. package/dist/packlets/crypto-utils/keyPairAlgorithmParams.js.map +1 -0
  66. package/dist/packlets/crypto-utils/keystore/converters.js +105 -14
  67. package/dist/packlets/crypto-utils/keystore/converters.js.map +1 -0
  68. package/dist/packlets/crypto-utils/keystore/encryptedFilePrivateKeyStorage.js +287 -0
  69. package/dist/packlets/crypto-utils/keystore/encryptedFilePrivateKeyStorage.js.map +1 -0
  70. package/dist/packlets/crypto-utils/keystore/index.browser.js +36 -0
  71. package/dist/packlets/crypto-utils/keystore/index.browser.js.map +1 -0
  72. package/dist/packlets/crypto-utils/keystore/index.js +3 -0
  73. package/dist/packlets/crypto-utils/keystore/index.js.map +1 -0
  74. package/dist/packlets/crypto-utils/keystore/keyStore.js +773 -119
  75. package/dist/packlets/crypto-utils/keystore/keyStore.js.map +1 -0
  76. package/dist/packlets/crypto-utils/keystore/model.js +30 -4
  77. package/dist/packlets/crypto-utils/keystore/model.js.map +1 -0
  78. package/dist/packlets/crypto-utils/keystore/privateKeyStorage.js +21 -0
  79. package/dist/packlets/crypto-utils/keystore/privateKeyStorage.js.map +1 -0
  80. package/dist/packlets/crypto-utils/model.js +32 -0
  81. package/dist/packlets/crypto-utils/model.js.map +1 -0
  82. package/dist/packlets/crypto-utils/nodeCryptoProvider.js +336 -1
  83. package/dist/packlets/crypto-utils/nodeCryptoProvider.js.map +1 -0
  84. package/dist/packlets/crypto-utils/spkiHelpers.js +240 -0
  85. package/dist/packlets/crypto-utils/spkiHelpers.js.map +1 -0
  86. package/dist/packlets/csv/csvFileHelpers.js +0 -14
  87. package/dist/packlets/csv/csvFileHelpers.js.map +1 -0
  88. package/dist/packlets/csv/csvHelpers.js +14 -0
  89. package/dist/packlets/csv/csvHelpers.js.map +1 -0
  90. package/dist/packlets/csv/index.browser.js +1 -3
  91. package/dist/packlets/csv/index.browser.js.map +1 -0
  92. package/dist/packlets/csv/index.js.map +1 -0
  93. package/dist/packlets/experimental/extendedArray.js.map +1 -0
  94. package/dist/packlets/experimental/formatter.js.map +1 -0
  95. package/dist/packlets/experimental/index.js.map +1 -0
  96. package/dist/packlets/experimental/rangeOf.js.map +1 -0
  97. package/dist/packlets/hash/index.browser.js.map +1 -0
  98. package/dist/packlets/hash/index.js.map +1 -0
  99. package/dist/packlets/hash/index.node.js.map +1 -0
  100. package/dist/packlets/hash/md5Normalizer.browser.js.map +1 -0
  101. package/dist/packlets/hash/md5Normalizer.js.map +1 -0
  102. package/dist/packlets/mustache/index.js.map +1 -0
  103. package/dist/packlets/mustache/interfaces.js.map +1 -0
  104. package/dist/packlets/mustache/mustacheTemplate.js +42 -4
  105. package/dist/packlets/mustache/mustacheTemplate.js.map +1 -0
  106. package/dist/packlets/record-jar/index.browser.js +1 -3
  107. package/dist/packlets/record-jar/index.browser.js.map +1 -0
  108. package/dist/packlets/record-jar/index.js.map +1 -0
  109. package/dist/packlets/record-jar/recordJarFileHelpers.js +0 -18
  110. package/dist/packlets/record-jar/recordJarFileHelpers.js.map +1 -0
  111. package/dist/packlets/record-jar/recordJarHelpers.js +18 -0
  112. package/dist/packlets/record-jar/recordJarHelpers.js.map +1 -0
  113. package/dist/packlets/yaml/converters.js.map +1 -0
  114. package/dist/packlets/yaml/index.js +1 -0
  115. package/dist/packlets/yaml/index.js.map +1 -0
  116. package/dist/packlets/yaml/serializers.js +48 -0
  117. package/dist/packlets/yaml/serializers.js.map +1 -0
  118. package/dist/packlets/zip-file-tree/index.js.map +1 -0
  119. package/dist/packlets/zip-file-tree/zipFileTreeAccessors.js +2 -2
  120. package/dist/packlets/zip-file-tree/zipFileTreeAccessors.js.map +1 -0
  121. package/dist/packlets/zip-file-tree/zipFileTreeWriter.js.map +1 -0
  122. package/dist/ts-extras.d.ts +4342 -285
  123. package/dist/tsdoc-metadata.json +1 -1
  124. package/lib/index.browser.d.ts +4 -2
  125. package/lib/index.browser.d.ts.map +1 -0
  126. package/lib/index.browser.js +8 -3
  127. package/lib/index.browser.js.map +1 -0
  128. package/lib/index.d.ts.map +1 -0
  129. package/lib/index.js.map +1 -0
  130. package/lib/packlets/ai-assist/apiClient.d.ts +115 -32
  131. package/lib/packlets/ai-assist/apiClient.d.ts.map +1 -0
  132. package/lib/packlets/ai-assist/apiClient.js +888 -156
  133. package/lib/packlets/ai-assist/apiClient.js.map +1 -0
  134. package/lib/packlets/ai-assist/chatRequestBuilders.d.ts +149 -0
  135. package/lib/packlets/ai-assist/chatRequestBuilders.d.ts.map +1 -0
  136. package/lib/packlets/ai-assist/chatRequestBuilders.js +293 -0
  137. package/lib/packlets/ai-assist/chatRequestBuilders.js.map +1 -0
  138. package/lib/packlets/ai-assist/converters.d.ts +15 -1
  139. package/lib/packlets/ai-assist/converters.d.ts.map +1 -0
  140. package/lib/packlets/ai-assist/converters.js +47 -3
  141. package/lib/packlets/ai-assist/converters.js.map +1 -0
  142. package/lib/packlets/ai-assist/embeddingClient.d.ts +69 -0
  143. package/lib/packlets/ai-assist/embeddingClient.d.ts.map +1 -0
  144. package/lib/packlets/ai-assist/embeddingClient.js +349 -0
  145. package/lib/packlets/ai-assist/embeddingClient.js.map +1 -0
  146. package/lib/packlets/ai-assist/endpoint.d.ts +28 -0
  147. package/lib/packlets/ai-assist/endpoint.d.ts.map +1 -0
  148. package/lib/packlets/ai-assist/endpoint.js +82 -0
  149. package/lib/packlets/ai-assist/endpoint.js.map +1 -0
  150. package/lib/packlets/ai-assist/http.d.ts +24 -0
  151. package/lib/packlets/ai-assist/http.d.ts.map +1 -0
  152. package/lib/packlets/ai-assist/http.js +78 -0
  153. package/lib/packlets/ai-assist/http.js.map +1 -0
  154. package/lib/packlets/ai-assist/imageOptionsResolver.d.ts +66 -0
  155. package/lib/packlets/ai-assist/imageOptionsResolver.d.ts.map +1 -0
  156. package/lib/packlets/ai-assist/imageOptionsResolver.js +197 -0
  157. package/lib/packlets/ai-assist/imageOptionsResolver.js.map +1 -0
  158. package/lib/packlets/ai-assist/index.d.ts +10 -4
  159. package/lib/packlets/ai-assist/index.d.ts.map +1 -0
  160. package/lib/packlets/ai-assist/index.js +36 -1
  161. package/lib/packlets/ai-assist/index.js.map +1 -0
  162. package/lib/packlets/ai-assist/jsonCompletion.d.ts +93 -0
  163. package/lib/packlets/ai-assist/jsonCompletion.d.ts.map +1 -0
  164. package/lib/packlets/ai-assist/jsonCompletion.js +97 -0
  165. package/lib/packlets/ai-assist/jsonCompletion.js.map +1 -0
  166. package/lib/packlets/ai-assist/jsonResponse.d.ts +91 -0
  167. package/lib/packlets/ai-assist/jsonResponse.d.ts.map +1 -0
  168. package/lib/packlets/ai-assist/jsonResponse.js +154 -0
  169. package/lib/packlets/ai-assist/jsonResponse.js.map +1 -0
  170. package/lib/packlets/ai-assist/model.d.ts +1202 -12
  171. package/lib/packlets/ai-assist/model.d.ts.map +1 -0
  172. package/lib/packlets/ai-assist/model.js +198 -10
  173. package/lib/packlets/ai-assist/model.js.map +1 -0
  174. package/lib/packlets/ai-assist/registry.d.ts +56 -1
  175. package/lib/packlets/ai-assist/registry.d.ts.map +1 -0
  176. package/lib/packlets/ai-assist/registry.js +319 -13
  177. package/lib/packlets/ai-assist/registry.js.map +1 -0
  178. package/lib/packlets/ai-assist/sseParser.d.ts +45 -0
  179. package/lib/packlets/ai-assist/sseParser.d.ts.map +1 -0
  180. package/lib/packlets/ai-assist/sseParser.js +128 -0
  181. package/lib/packlets/ai-assist/sseParser.js.map +1 -0
  182. package/lib/packlets/ai-assist/streamingAdapters/anthropic.d.ts +72 -0
  183. package/lib/packlets/ai-assist/streamingAdapters/anthropic.d.ts.map +1 -0
  184. package/lib/packlets/ai-assist/streamingAdapters/anthropic.js +371 -0
  185. package/lib/packlets/ai-assist/streamingAdapters/anthropic.js.map +1 -0
  186. package/lib/packlets/ai-assist/streamingAdapters/clientToolContinuationBuilder.d.ts +260 -0
  187. package/lib/packlets/ai-assist/streamingAdapters/clientToolContinuationBuilder.d.ts.map +1 -0
  188. package/lib/packlets/ai-assist/streamingAdapters/clientToolContinuationBuilder.js +694 -0
  189. package/lib/packlets/ai-assist/streamingAdapters/clientToolContinuationBuilder.js.map +1 -0
  190. package/lib/packlets/ai-assist/streamingAdapters/common.d.ts +154 -0
  191. package/lib/packlets/ai-assist/streamingAdapters/common.d.ts.map +1 -0
  192. package/lib/packlets/ai-assist/streamingAdapters/common.js +193 -0
  193. package/lib/packlets/ai-assist/streamingAdapters/common.js.map +1 -0
  194. package/lib/packlets/ai-assist/streamingAdapters/gemini.d.ts +43 -0
  195. package/lib/packlets/ai-assist/streamingAdapters/gemini.d.ts.map +1 -0
  196. package/lib/packlets/ai-assist/streamingAdapters/gemini.js +207 -0
  197. package/lib/packlets/ai-assist/streamingAdapters/gemini.js.map +1 -0
  198. package/lib/packlets/ai-assist/streamingAdapters/openaiChat.d.ts +19 -0
  199. package/lib/packlets/ai-assist/streamingAdapters/openaiChat.d.ts.map +1 -0
  200. package/lib/packlets/ai-assist/streamingAdapters/openaiChat.js +173 -0
  201. package/lib/packlets/ai-assist/streamingAdapters/openaiChat.js.map +1 -0
  202. package/lib/packlets/ai-assist/streamingAdapters/openaiResponses.d.ts +33 -0
  203. package/lib/packlets/ai-assist/streamingAdapters/openaiResponses.d.ts.map +1 -0
  204. package/lib/packlets/ai-assist/streamingAdapters/openaiResponses.js +389 -0
  205. package/lib/packlets/ai-assist/streamingAdapters/openaiResponses.js.map +1 -0
  206. package/lib/packlets/ai-assist/streamingAdapters/proxy.d.ts +34 -0
  207. package/lib/packlets/ai-assist/streamingAdapters/proxy.d.ts.map +1 -0
  208. package/lib/packlets/ai-assist/streamingAdapters/proxy.js +176 -0
  209. package/lib/packlets/ai-assist/streamingAdapters/proxy.js.map +1 -0
  210. package/lib/packlets/ai-assist/streamingClient.d.ts +50 -0
  211. package/lib/packlets/ai-assist/streamingClient.d.ts.map +1 -0
  212. package/lib/packlets/ai-assist/streamingClient.js +146 -0
  213. package/lib/packlets/ai-assist/streamingClient.js.map +1 -0
  214. package/lib/packlets/ai-assist/thinkingOptionsResolver.d.ts +87 -0
  215. package/lib/packlets/ai-assist/thinkingOptionsResolver.d.ts.map +1 -0
  216. package/lib/packlets/ai-assist/thinkingOptionsResolver.js +294 -0
  217. package/lib/packlets/ai-assist/thinkingOptionsResolver.js.map +1 -0
  218. package/lib/packlets/ai-assist/toolFormats.d.ts +40 -9
  219. package/lib/packlets/ai-assist/toolFormats.d.ts.map +1 -0
  220. package/lib/packlets/ai-assist/toolFormats.js +107 -10
  221. package/lib/packlets/ai-assist/toolFormats.js.map +1 -0
  222. package/lib/packlets/conversion/converters.d.ts +8 -1
  223. package/lib/packlets/conversion/converters.d.ts.map +1 -0
  224. package/lib/packlets/conversion/converters.js +36 -2
  225. package/lib/packlets/conversion/converters.js.map +1 -0
  226. package/lib/packlets/conversion/index.d.ts.map +1 -0
  227. package/lib/packlets/conversion/index.js.map +1 -0
  228. package/lib/packlets/crypto-utils/constants.d.ts.map +1 -0
  229. package/lib/packlets/crypto-utils/constants.js.map +1 -0
  230. package/lib/packlets/crypto-utils/converters.d.ts +20 -1
  231. package/lib/packlets/crypto-utils/converters.d.ts.map +1 -0
  232. package/lib/packlets/crypto-utils/converters.js +43 -5
  233. package/lib/packlets/crypto-utils/converters.js.map +1 -0
  234. package/lib/packlets/crypto-utils/directEncryptionProvider.d.ts.map +1 -0
  235. package/lib/packlets/crypto-utils/directEncryptionProvider.js.map +1 -0
  236. package/lib/packlets/crypto-utils/encryptedFile.d.ts.map +1 -0
  237. package/lib/packlets/crypto-utils/encryptedFile.js.map +1 -0
  238. package/lib/packlets/crypto-utils/hpkeProvider.d.ts +150 -0
  239. package/lib/packlets/crypto-utils/hpkeProvider.d.ts.map +1 -0
  240. package/lib/packlets/crypto-utils/hpkeProvider.js +359 -0
  241. package/lib/packlets/crypto-utils/hpkeProvider.js.map +1 -0
  242. package/lib/packlets/crypto-utils/index.browser.d.ts +4 -1
  243. package/lib/packlets/crypto-utils/index.browser.d.ts.map +1 -0
  244. package/lib/packlets/crypto-utils/index.browser.js +22 -3
  245. package/lib/packlets/crypto-utils/index.browser.js.map +1 -0
  246. package/lib/packlets/crypto-utils/index.d.ts +3 -0
  247. package/lib/packlets/crypto-utils/index.d.ts.map +1 -0
  248. package/lib/packlets/crypto-utils/index.js +18 -1
  249. package/lib/packlets/crypto-utils/index.js.map +1 -0
  250. package/lib/packlets/crypto-utils/keyPairAlgorithmParams.d.ts +54 -0
  251. package/lib/packlets/crypto-utils/keyPairAlgorithmParams.d.ts.map +1 -0
  252. package/lib/packlets/crypto-utils/keyPairAlgorithmParams.js +74 -0
  253. package/lib/packlets/crypto-utils/keyPairAlgorithmParams.js.map +1 -0
  254. package/lib/packlets/crypto-utils/keystore/converters.d.ts +68 -6
  255. package/lib/packlets/crypto-utils/keystore/converters.d.ts.map +1 -0
  256. package/lib/packlets/crypto-utils/keystore/converters.js +103 -12
  257. package/lib/packlets/crypto-utils/keystore/converters.js.map +1 -0
  258. package/lib/packlets/crypto-utils/keystore/encryptedFilePrivateKeyStorage.d.ts +148 -0
  259. package/lib/packlets/crypto-utils/keystore/encryptedFilePrivateKeyStorage.d.ts.map +1 -0
  260. package/lib/packlets/crypto-utils/keystore/encryptedFilePrivateKeyStorage.js +324 -0
  261. package/lib/packlets/crypto-utils/keystore/encryptedFilePrivateKeyStorage.js.map +1 -0
  262. package/lib/packlets/crypto-utils/keystore/index.browser.d.ts +10 -0
  263. package/lib/packlets/crypto-utils/keystore/index.browser.d.ts.map +1 -0
  264. package/lib/packlets/crypto-utils/keystore/index.browser.js +76 -0
  265. package/lib/packlets/crypto-utils/keystore/index.browser.js.map +1 -0
  266. package/lib/packlets/crypto-utils/keystore/index.d.ts +2 -0
  267. package/lib/packlets/crypto-utils/keystore/index.d.ts.map +1 -0
  268. package/lib/packlets/crypto-utils/keystore/index.js +5 -1
  269. package/lib/packlets/crypto-utils/keystore/index.js.map +1 -0
  270. package/lib/packlets/crypto-utils/keystore/keyStore.d.ts +270 -13
  271. package/lib/packlets/crypto-utils/keystore/keyStore.d.ts.map +1 -0
  272. package/lib/packlets/crypto-utils/keystore/keyStore.js +778 -124
  273. package/lib/packlets/crypto-utils/keystore/keyStore.js.map +1 -0
  274. package/lib/packlets/crypto-utils/keystore/model.d.ts +350 -21
  275. package/lib/packlets/crypto-utils/keystore/model.d.ts.map +1 -0
  276. package/lib/packlets/crypto-utils/keystore/model.js +32 -5
  277. package/lib/packlets/crypto-utils/keystore/model.js.map +1 -0
  278. package/lib/packlets/crypto-utils/keystore/privateKeyStorage.d.ts +53 -0
  279. package/lib/packlets/crypto-utils/keystore/privateKeyStorage.d.ts.map +1 -0
  280. package/lib/packlets/crypto-utils/keystore/privateKeyStorage.js +22 -0
  281. package/lib/packlets/crypto-utils/keystore/privateKeyStorage.js.map +1 -0
  282. package/lib/packlets/crypto-utils/model.d.ts +437 -10
  283. package/lib/packlets/crypto-utils/model.d.ts.map +1 -0
  284. package/lib/packlets/crypto-utils/model.js +33 -1
  285. package/lib/packlets/crypto-utils/model.js.map +1 -0
  286. package/lib/packlets/crypto-utils/nodeCryptoProvider.d.ts +134 -2
  287. package/lib/packlets/crypto-utils/nodeCryptoProvider.d.ts.map +1 -0
  288. package/lib/packlets/crypto-utils/nodeCryptoProvider.js +335 -0
  289. package/lib/packlets/crypto-utils/nodeCryptoProvider.js.map +1 -0
  290. package/lib/packlets/crypto-utils/spkiHelpers.d.ts +125 -0
  291. package/lib/packlets/crypto-utils/spkiHelpers.d.ts.map +1 -0
  292. package/lib/packlets/crypto-utils/spkiHelpers.js +251 -0
  293. package/lib/packlets/crypto-utils/spkiHelpers.js.map +1 -0
  294. package/lib/packlets/csv/csvFileHelpers.d.ts +0 -10
  295. package/lib/packlets/csv/csvFileHelpers.d.ts.map +1 -0
  296. package/lib/packlets/csv/csvFileHelpers.js +0 -15
  297. package/lib/packlets/csv/csvFileHelpers.js.map +1 -0
  298. package/lib/packlets/csv/csvHelpers.d.ts +10 -0
  299. package/lib/packlets/csv/csvHelpers.d.ts.map +1 -0
  300. package/lib/packlets/csv/csvHelpers.js +15 -0
  301. package/lib/packlets/csv/csvHelpers.js.map +1 -0
  302. package/lib/packlets/csv/index.browser.d.ts +0 -1
  303. package/lib/packlets/csv/index.browser.d.ts.map +1 -0
  304. package/lib/packlets/csv/index.browser.js +1 -5
  305. package/lib/packlets/csv/index.browser.js.map +1 -0
  306. package/lib/packlets/csv/index.d.ts.map +1 -0
  307. package/lib/packlets/csv/index.js.map +1 -0
  308. package/lib/packlets/experimental/extendedArray.d.ts.map +1 -0
  309. package/lib/packlets/experimental/extendedArray.js.map +1 -0
  310. package/lib/packlets/experimental/formatter.d.ts.map +1 -0
  311. package/lib/packlets/experimental/formatter.js.map +1 -0
  312. package/lib/packlets/experimental/index.d.ts.map +1 -0
  313. package/lib/packlets/experimental/index.js.map +1 -0
  314. package/lib/packlets/experimental/rangeOf.d.ts.map +1 -0
  315. package/lib/packlets/experimental/rangeOf.js.map +1 -0
  316. package/lib/packlets/hash/index.browser.d.ts.map +1 -0
  317. package/lib/packlets/hash/index.browser.js.map +1 -0
  318. package/lib/packlets/hash/index.d.ts.map +1 -0
  319. package/lib/packlets/hash/index.js.map +1 -0
  320. package/lib/packlets/hash/index.node.d.ts.map +1 -0
  321. package/lib/packlets/hash/index.node.js.map +1 -0
  322. package/lib/packlets/hash/md5Normalizer.browser.d.ts.map +1 -0
  323. package/lib/packlets/hash/md5Normalizer.browser.js.map +1 -0
  324. package/lib/packlets/hash/md5Normalizer.d.ts.map +1 -0
  325. package/lib/packlets/hash/md5Normalizer.js.map +1 -0
  326. package/lib/packlets/mustache/index.d.ts +1 -1
  327. package/lib/packlets/mustache/index.d.ts.map +1 -0
  328. package/lib/packlets/mustache/index.js.map +1 -0
  329. package/lib/packlets/mustache/interfaces.d.ts +34 -0
  330. package/lib/packlets/mustache/interfaces.d.ts.map +1 -0
  331. package/lib/packlets/mustache/interfaces.js.map +1 -0
  332. package/lib/packlets/mustache/mustacheTemplate.d.ts +2 -0
  333. package/lib/packlets/mustache/mustacheTemplate.d.ts.map +1 -0
  334. package/lib/packlets/mustache/mustacheTemplate.js +42 -4
  335. package/lib/packlets/mustache/mustacheTemplate.js.map +1 -0
  336. package/lib/packlets/record-jar/index.browser.d.ts +0 -1
  337. package/lib/packlets/record-jar/index.browser.d.ts.map +1 -0
  338. package/lib/packlets/record-jar/index.browser.js +1 -5
  339. package/lib/packlets/record-jar/index.browser.js.map +1 -0
  340. package/lib/packlets/record-jar/index.d.ts.map +1 -0
  341. package/lib/packlets/record-jar/index.js.map +1 -0
  342. package/lib/packlets/record-jar/recordJarFileHelpers.d.ts +0 -11
  343. package/lib/packlets/record-jar/recordJarFileHelpers.d.ts.map +1 -0
  344. package/lib/packlets/record-jar/recordJarFileHelpers.js +0 -19
  345. package/lib/packlets/record-jar/recordJarFileHelpers.js.map +1 -0
  346. package/lib/packlets/record-jar/recordJarHelpers.d.ts +11 -0
  347. package/lib/packlets/record-jar/recordJarHelpers.d.ts.map +1 -0
  348. package/lib/packlets/record-jar/recordJarHelpers.js +19 -0
  349. package/lib/packlets/record-jar/recordJarHelpers.js.map +1 -0
  350. package/lib/packlets/yaml/converters.d.ts.map +1 -0
  351. package/lib/packlets/yaml/converters.js.map +1 -0
  352. package/lib/packlets/yaml/index.d.ts +1 -0
  353. package/lib/packlets/yaml/index.d.ts.map +1 -0
  354. package/lib/packlets/yaml/index.js +1 -0
  355. package/lib/packlets/yaml/index.js.map +1 -0
  356. package/lib/packlets/yaml/serializers.d.ts +45 -0
  357. package/lib/packlets/yaml/serializers.d.ts.map +1 -0
  358. package/lib/packlets/yaml/serializers.js +84 -0
  359. package/lib/packlets/yaml/serializers.js.map +1 -0
  360. package/lib/packlets/zip-file-tree/index.d.ts.map +1 -0
  361. package/lib/packlets/zip-file-tree/index.js.map +1 -0
  362. package/lib/packlets/zip-file-tree/zipFileTreeAccessors.d.ts +2 -2
  363. package/lib/packlets/zip-file-tree/zipFileTreeAccessors.d.ts.map +1 -0
  364. package/lib/packlets/zip-file-tree/zipFileTreeAccessors.js +2 -2
  365. package/lib/packlets/zip-file-tree/zipFileTreeAccessors.js.map +1 -0
  366. package/lib/packlets/zip-file-tree/zipFileTreeWriter.d.ts.map +1 -0
  367. package/lib/packlets/zip-file-tree/zipFileTreeWriter.js.map +1 -0
  368. package/package.json +20 -15
@@ -17,9 +17,11 @@
17
17
  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
18
  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
19
  // SOFTWARE.
20
- import { captureResult, fail, succeed } from '@fgv/ts-utils';
20
+ import { captureAsyncResult, captureResult, fail, succeed } from '@fgv/ts-utils';
21
21
  import * as Constants from '../constants';
22
22
  import { createEncryptedFile } from '../encryptedFile';
23
+ import { keyPairAlgorithmParams } from '../keyPairAlgorithmParams';
24
+ import { ARGON2ID_OWASP_MIN } from '../model';
23
25
  import { DEFAULT_KEYSTORE_ITERATIONS, DEFAULT_SECRET_ITERATIONS, KEYSTORE_FORMAT, MIN_SALT_LENGTH } from './model';
24
26
  import { keystoreFile, keystoreVaultContents } from './converters';
25
27
  /**
@@ -28,6 +30,11 @@ import { keystoreFile, keystoreVaultContents } from './converters';
28
30
  function getCurrentTimestamp() {
29
31
  return new Date().toISOString();
30
32
  }
33
+ // WebCrypto key usages that only ever apply to a public key. Filtering these
34
+ // out of the keypair usages yields the usages valid for the private half, which
35
+ // `crypto.subtle.importKey` requires when re-importing a private JWK. Mirrors
36
+ // the same list in `EncryptedFilePrivateKeyStorage`.
37
+ const PUBLIC_ONLY_USAGES = ['verify', 'encrypt', 'wrapKey'];
31
38
  // ============================================================================
32
39
  // KeyStore Class
33
40
  // ============================================================================
@@ -60,11 +67,26 @@ function getCurrentTimestamp() {
60
67
  * const encryptionConfig = keystore2.getEncryptionConfig().orThrow();
61
68
  * ```
62
69
  *
70
+ * @remarks
71
+ * SECURITY — private-key escrow. By default an asymmetric keypair's private
72
+ * key lives only in the per-device {@link CryptoUtils.KeyStore.IPrivateKeyStorage}
73
+ * backend; the vault carries only the public JWK, so a vault recovered on a new
74
+ * device reconstitutes with no private keys (a lost device is a lost identity).
75
+ * `addKeyPair(name, { escrow: true })` opts into carrying an encrypted private-key
76
+ * copy inside the vault ciphertext, so the vault file plus the master password can
77
+ * recover the identity on a fresh device via `getKeyPair(name, { rehydrate: true })`.
78
+ *
79
+ * When escrow is used, **the master password becomes the sole gate** protecting a
80
+ * private signing/decryption key that was previously unrecoverable from the vault.
81
+ * Configure escrow-bearing stores with a strong KDF — an Argon2id-derived master
82
+ * key or a high-iteration PBKDF2 count — and treat the vault file accordingly.
83
+ *
63
84
  * @public
64
85
  */
65
86
  export class KeyStore {
66
- constructor(cryptoProvider, iterations, keystoreFile, isNew = true) {
87
+ constructor(cryptoProvider, iterations, keystoreFile, isNew, privateKeyStorage) {
67
88
  this._cryptoProvider = cryptoProvider;
89
+ this._privateKeyStorage = privateKeyStorage;
68
90
  this._iterations = iterations;
69
91
  this._keystoreFile = keystoreFile;
70
92
  this._state = 'locked';
@@ -87,7 +109,7 @@ export class KeyStore {
87
109
  if (iterations < 1) {
88
110
  return fail('Iterations must be at least 1');
89
111
  }
90
- return succeed(new KeyStore(params.cryptoProvider, iterations, undefined, true));
112
+ return succeed(new KeyStore(params.cryptoProvider, iterations, undefined, true, params.privateKeyStorage));
91
113
  }
92
114
  /**
93
115
  * Opens an existing encrypted key store.
@@ -103,7 +125,7 @@ export class KeyStore {
103
125
  return fail(`Invalid key store file: ${fileResult.message}`);
104
126
  }
105
127
  const iterations = fileResult.value.keyDerivation.iterations;
106
- return succeed(new KeyStore(params.cryptoProvider, iterations, fileResult.value, false));
128
+ return succeed(new KeyStore(params.cryptoProvider, iterations, fileResult.value, false, params.privateKeyStorage));
107
129
  }
108
130
  // ============================================================================
109
131
  // Lifecycle Methods
@@ -146,7 +168,6 @@ export class KeyStore {
146
168
  * @public
147
169
  */
148
170
  async unlock(password) {
149
- var _a;
150
171
  if (this._isNew) {
151
172
  return fail('Cannot unlock a new key store - use initialize() instead');
152
173
  }
@@ -170,57 +191,37 @@ export class KeyStore {
170
191
  if (keyResult.isFailure()) {
171
192
  return fail(`Key derivation failed: ${keyResult.message}`);
172
193
  }
173
- // Decrypt the vault
174
- const ivResult = this._cryptoProvider.fromBase64(this._keystoreFile.iv);
175
- const authTagResult = this._cryptoProvider.fromBase64(this._keystoreFile.authTag);
176
- const encryptedDataResult = this._cryptoProvider.fromBase64(this._keystoreFile.encryptedData);
177
- /* c8 ignore next 9 - base64 decode errors tested but coverage intermittently missed */
178
- if (ivResult.isFailure()) {
179
- return fail(`Invalid IV in key store file: ${ivResult.message}`);
180
- }
181
- if (authTagResult.isFailure()) {
182
- return fail(`Invalid auth tag in key store file: ${authTagResult.message}`);
183
- }
184
- if (encryptedDataResult.isFailure()) {
185
- return fail(`Invalid encrypted data in key store file: ${encryptedDataResult.message}`);
186
- }
187
- const decryptResult = await this._cryptoProvider.decrypt(encryptedDataResult.value, keyResult.value, ivResult.value, authTagResult.value);
188
- if (decryptResult.isFailure()) {
189
- return fail('Incorrect password or corrupted key store');
194
+ return this._decryptVault(keyResult.value);
195
+ }
196
+ /**
197
+ * Unlocks an existing key store with a pre-derived key, bypassing
198
+ * PBKDF2 key derivation. Use this when the derived key has been
199
+ * stored externally (e.g., in another key store) and the original
200
+ * password is no longer available.
201
+ *
202
+ * The supplied key must have been derived from the correct password
203
+ * using the key store file's own PBKDF2 parameters (salt and
204
+ * iteration count).
205
+ *
206
+ * @param derivedKey - The pre-derived master key (32 bytes for AES-256)
207
+ * @returns Success with this instance when unlocked, Failure if key is incorrect
208
+ * @public
209
+ */
210
+ async unlockWithKey(derivedKey) {
211
+ if (this._isNew) {
212
+ return fail('Cannot unlock a new key store - use initialize() instead');
190
213
  }
191
- // Parse the vault contents
192
- const parseResult = captureResult(() => JSON.parse(decryptResult.value));
193
- /* c8 ignore next 3 - error path tested but coverage intermittently missed */
194
- if (parseResult.isFailure()) {
195
- return fail(`Failed to parse vault contents: ${parseResult.message}`);
214
+ if (this._state === 'unlocked') {
215
+ return fail('Key store is already unlocked');
196
216
  }
197
- const vaultResult = keystoreVaultContents.convert(parseResult.value);
198
- /* c8 ignore next 3 - error path tested but coverage intermittently missed */
199
- if (vaultResult.isFailure()) {
200
- return fail(`Invalid vault format: ${vaultResult.message}`);
217
+ if (derivedKey.length !== Constants.AES_256_KEY_SIZE) {
218
+ return fail(`Key must be ${Constants.AES_256_KEY_SIZE} bytes, got ${derivedKey.length}`);
201
219
  }
202
- // Load secrets into memory
203
- this._salt = salt;
204
- this._secrets = new Map();
205
- for (const [name, jsonEntry] of Object.entries(vaultResult.value.secrets)) {
206
- const keyBytesResult = this._cryptoProvider.fromBase64(jsonEntry.key);
207
- /* c8 ignore next 3 - error path tested but coverage intermittently missed */
208
- if (keyBytesResult.isFailure()) {
209
- return fail(`Invalid key for secret '${name}': ${keyBytesResult.message}`);
210
- }
211
- const entry = {
212
- name,
213
- /* c8 ignore next 1 - backwards compatibility: old vaults may lack type field */
214
- type: (_a = jsonEntry.type) !== null && _a !== void 0 ? _a : 'encryption-key',
215
- key: keyBytesResult.value,
216
- description: jsonEntry.description,
217
- createdAt: jsonEntry.createdAt
218
- };
219
- this._secrets.set(name, entry);
220
+ /* c8 ignore next 3 - defensive coding: unreachable via public API (open sets file, create sets isNew) */
221
+ if (!this._keystoreFile) {
222
+ return fail('No key store file to unlock');
220
223
  }
221
- this._state = 'unlocked';
222
- this._dirty = false;
223
- return succeed(this);
224
+ return this._decryptVault(derivedKey);
224
225
  }
225
226
  /**
226
227
  * Locks the key store, clearing all secrets from memory.
@@ -238,7 +239,9 @@ export class KeyStore {
238
239
  // Clear secrets from memory (overwrite for security)
239
240
  if (this._secrets) {
240
241
  for (const entry of this._secrets.values()) {
241
- entry.key.fill(0);
242
+ if (entry.type !== 'asymmetric-keypair') {
243
+ entry.key.fill(0);
244
+ }
242
245
  }
243
246
  this._secrets.clear();
244
247
  this._secrets = undefined;
@@ -300,7 +303,15 @@ export class KeyStore {
300
303
  return succeed(Array.from(this._secrets.keys()));
301
304
  }
302
305
  /**
303
- * Gets a secret by name.
306
+ * Gets a secret by name. Returns the {@link CryptoUtils.KeyStore.IKeyStoreEntry | discriminated union}
307
+ * — callers must check `entry.type` before accessing `key`/`id` since asymmetric
308
+ * entries carry no raw key material.
309
+ *
310
+ * SECURITY: for an escrow-enabled asymmetric-keypair entry the returned object
311
+ * carries `escrowedPrivateKeyJwk` in cleartext (same custody class as
312
+ * `publicKeyJwk`, gated by the same unlock) — do not log or serialize a
313
+ * `getSecret()` result casually.
314
+ *
304
315
  * @param name - Name of the secret
305
316
  * @returns Success with secret entry, Failure if not found or locked
306
317
  * @public
@@ -315,6 +326,27 @@ export class KeyStore {
315
326
  }
316
327
  return succeed(entry);
317
328
  }
329
+ /**
330
+ * Returns the public-key JWK for an asymmetric-keypair entry.
331
+ * Available without {@link CryptoUtils.KeyStore.IPrivateKeyStorage} since the
332
+ * public key lives in the vault metadata directly.
333
+ * @param name - Name of the entry
334
+ * @returns Success with the JWK, Failure if not found, locked, or wrong type
335
+ * @public
336
+ */
337
+ getPublicKeyJwk(name) {
338
+ if (!this._secrets) {
339
+ return fail('Key store is locked');
340
+ }
341
+ const entry = this._secrets.get(name);
342
+ if (!entry) {
343
+ return fail(`Secret '${name}' not found`);
344
+ }
345
+ if (entry.type !== 'asymmetric-keypair') {
346
+ return fail(`Secret '${name}' is not an asymmetric keypair (type: ${entry.type})`);
347
+ }
348
+ return succeed(entry.publicKeyJwk);
349
+ }
318
350
  /**
319
351
  * Checks if a secret exists.
320
352
  * @param name - Name of the secret
@@ -341,7 +373,6 @@ export class KeyStore {
341
373
  if (!name || name.length === 0) {
342
374
  return fail('Secret name cannot be empty');
343
375
  }
344
- const replaced = this._secrets.has(name);
345
376
  // Generate a new random key
346
377
  const keyResult = await this._cryptoProvider.generateKey();
347
378
  /* c8 ignore next 3 - crypto provider errors tested but coverage intermittently missed */
@@ -355,19 +386,28 @@ export class KeyStore {
355
386
  description: options === null || options === void 0 ? void 0 : options.description,
356
387
  createdAt: getCurrentTimestamp()
357
388
  };
389
+ const existing = this._secrets.get(name);
390
+ const warning = existing ? await this._releaseEntryResources(existing) : undefined;
358
391
  this._secrets.set(name, entry);
359
392
  this._dirty = true;
360
- return succeed({ entry, replaced });
393
+ return succeed({ entry, replaced: existing !== undefined, warning });
361
394
  }
362
395
  /**
363
- * Imports an existing secret key.
396
+ * Imports raw 32-byte key material into the vault.
397
+ *
398
+ * Always validates that the key is exactly 32 bytes (AES-256). The optional
399
+ * `type` field is a classification label stored with the entry; it does not
400
+ * change the validation rules. For importing UTF-8 API key strings (variable
401
+ * length), use {@link KeyStore.importApiKey} instead.
402
+ *
364
403
  * @param name - Unique name for the secret
365
- * @param key - The 32-byte AES-256 key
366
- * @param options - Optional description, whether to replace existing
404
+ * @param key - The 32-byte AES-256 key material
405
+ * @param options - Optional type classification, description, whether to replace existing
367
406
  * @returns Success with entry, Failure if locked, key invalid, or exists and !replace
368
407
  * @public
369
408
  */
370
- importSecret(name, key, options) {
409
+ async importSecret(name, key, options) {
410
+ var _a;
371
411
  if (!this._secrets) {
372
412
  return fail('Key store is locked');
373
413
  }
@@ -377,20 +417,21 @@ export class KeyStore {
377
417
  if (key.length !== Constants.AES_256_KEY_SIZE) {
378
418
  return fail(`Key must be ${Constants.AES_256_KEY_SIZE} bytes, got ${key.length}`);
379
419
  }
380
- const exists = this._secrets.has(name);
381
- if (exists && !(options === null || options === void 0 ? void 0 : options.replace)) {
420
+ const existing = this._secrets.get(name);
421
+ if (existing && !(options === null || options === void 0 ? void 0 : options.replace)) {
382
422
  return fail(`Secret '${name}' already exists - use replace=true to overwrite`);
383
423
  }
384
424
  const entry = {
385
425
  name,
386
- type: 'encryption-key',
426
+ type: (_a = options === null || options === void 0 ? void 0 : options.type) !== null && _a !== void 0 ? _a : 'encryption-key',
387
427
  key: new Uint8Array(key), // Copy to prevent external modification
388
428
  description: options === null || options === void 0 ? void 0 : options.description,
389
429
  createdAt: getCurrentTimestamp()
390
430
  };
431
+ const warning = existing ? await this._releaseEntryResources(existing) : undefined;
391
432
  this._secrets.set(name, entry);
392
433
  this._dirty = true;
393
- return succeed({ entry, replaced: exists });
434
+ return succeed({ entry, replaced: existing !== undefined, warning });
394
435
  }
395
436
  /**
396
437
  * Adds a secret derived from a password using PBKDF2.
@@ -417,8 +458,8 @@ export class KeyStore {
417
458
  if (!password || password.length === 0) {
418
459
  return fail('Password cannot be empty');
419
460
  }
420
- const exists = this._secrets.has(name);
421
- if (exists && !(options === null || options === void 0 ? void 0 : options.replace)) {
461
+ const existing = this._secrets.get(name);
462
+ if (existing && !(options === null || options === void 0 ? void 0 : options.replace)) {
422
463
  return fail(`Secret '${name}' already exists - use replace=true to overwrite`);
423
464
  }
424
465
  const iterations = (_a = options === null || options === void 0 ? void 0 : options.iterations) !== null && _a !== void 0 ? _a : DEFAULT_SECRET_ITERATIONS;
@@ -441,11 +482,13 @@ export class KeyStore {
441
482
  description: options === null || options === void 0 ? void 0 : options.description,
442
483
  createdAt: getCurrentTimestamp()
443
484
  };
485
+ const warning = existing ? await this._releaseEntryResources(existing) : undefined;
444
486
  this._secrets.set(name, entry);
445
487
  this._dirty = true;
446
488
  return succeed({
447
489
  entry,
448
- replaced: exists,
490
+ replaced: existing !== undefined,
491
+ warning,
449
492
  keyDerivation: {
450
493
  kdf: 'pbkdf2',
451
494
  salt: this._cryptoProvider.toBase64(saltResult.value),
@@ -454,12 +497,183 @@ export class KeyStore {
454
497
  });
455
498
  }
456
499
  /**
457
- * Removes a secret by name.
500
+ * Verifies that a candidate password derives the same key material currently
501
+ * stored under `name`, using the supplied
502
+ * {@link CryptoUtils.IKeyDerivationParams | key derivation parameters}.
503
+ *
504
+ * The keystore does not persist per-slot key derivation parameters with the
505
+ * entry — callers receive them from `addSecretFromPassword` and store them
506
+ * alongside the encrypted artifact (or wherever else makes sense). Pass
507
+ * those same parameters here for verification.
508
+ *
509
+ * Re-derives a key from `password` + `keyDerivation`, then compares it to
510
+ * the stored key material in constant time. Restricted to entries of type
511
+ * `'encryption-key'` — the type produced by `addSecretFromPassword`. Other
512
+ * symmetric types (`'api-key'`) and asymmetric entries are rejected so
513
+ * the boolean result reflects "this slot accepts this password" rather
514
+ * than an incidental byte-equality match against unrelated material.
515
+ *
516
+ * Note: the keystore does not currently flag whether an `'encryption-key'`
517
+ * entry was actually password-derived (vs. random via `addSecret` or raw
518
+ * via `importSecret`). A `true` result therefore means "the candidate
519
+ * password produces the same 32 bytes currently stored", which is what
520
+ * the equivalent consumer-side helper (`verifyGatePassword`) already
521
+ * implies for entries it manages.
522
+ *
523
+ * @param name - Name of the secret to verify against
524
+ * @param password - Candidate password to test
525
+ * @param keyDerivation - The key derivation parameters returned by
526
+ * `addSecretFromPassword` when the secret was created. Only
527
+ * `kdf: 'pbkdf2'` is supported.
528
+ * @returns Success(true) when the candidate matches the stored key,
529
+ * Success(false) when it does not, Failure if locked, secret missing,
530
+ * wrong type, unsupported `kdf`, or key derivation fails
531
+ * @public
532
+ */
533
+ async verifySecretFromPassword(name, password, keyDerivation) {
534
+ if (!this._secrets) {
535
+ return fail('Key store is locked');
536
+ }
537
+ if (!password || password.length === 0) {
538
+ return fail('Password cannot be empty');
539
+ }
540
+ if (keyDerivation.kdf !== 'pbkdf2') {
541
+ return fail(`Unsupported kdf '${keyDerivation.kdf}' (expected 'pbkdf2')`);
542
+ }
543
+ const entry = this._secrets.get(name);
544
+ if (!entry) {
545
+ return fail(`Secret '${name}' not found`);
546
+ }
547
+ if (entry.type !== 'encryption-key') {
548
+ return fail(`Secret '${name}' is not a password-verifiable encryption key (type: ${entry.type})`);
549
+ }
550
+ const saltResult = this._cryptoProvider.fromBase64(keyDerivation.salt);
551
+ if (saltResult.isFailure()) {
552
+ return fail(`Invalid salt: ${saltResult.message}`);
553
+ }
554
+ const derivedResult = await this._cryptoProvider.deriveKey(password, saltResult.value, keyDerivation.iterations);
555
+ /* c8 ignore next 3 - crypto provider errors covered in nodeCryptoProvider tests */
556
+ if (derivedResult.isFailure()) {
557
+ return fail(`Key derivation failed: ${derivedResult.message}`);
558
+ }
559
+ return succeed(KeyStore._timingSafeEqual(derivedResult.value, entry.key));
560
+ }
561
+ /**
562
+ * Adds a secret derived from a password using Argon2id (RFC 9106).
563
+ *
564
+ * The Argon2id provider must be supplied explicitly; the KeyStore does not
565
+ * hold one by default (consumers opt in by depending on the argon2 package).
566
+ *
567
+ * Returns the key derivation parameters so callers can store them alongside
568
+ * encrypted artifacts, enabling future re-derivation and verification.
569
+ *
570
+ * @param name - Unique name for the secret
571
+ * @param password - Password or passphrase
572
+ * @param argon2idProvider - Argon2id provider (Node or Browser implementation)
573
+ * @param options - Optional: Argon2id params (defaults to ARGON2ID_OWASP_MIN), description, replace flag
574
+ * @returns Success with entry and keyDerivation params, Failure if locked or invalid
575
+ * @public
576
+ */
577
+ async addSecretFromPasswordArgon2id(name, password, argon2idProvider, options) {
578
+ var _a;
579
+ if (!this._secrets) {
580
+ return fail('Key store is locked');
581
+ }
582
+ if (!name || name.length === 0) {
583
+ return fail('Secret name cannot be empty');
584
+ }
585
+ if (!password || password.length === 0) {
586
+ return fail('Password cannot be empty');
587
+ }
588
+ const existing = this._secrets.get(name);
589
+ if (existing && !(options === null || options === void 0 ? void 0 : options.replace)) {
590
+ return fail(`Secret '${name}' already exists - use replace=true to overwrite`);
591
+ }
592
+ const params = (_a = options === null || options === void 0 ? void 0 : options.params) !== null && _a !== void 0 ? _a : ARGON2ID_OWASP_MIN;
593
+ const saltResult = this._cryptoProvider.generateRandomBytes(MIN_SALT_LENGTH);
594
+ /* c8 ignore next 3 - crypto provider errors tested but coverage intermittently missed */
595
+ if (saltResult.isFailure()) {
596
+ return fail(`Failed to generate salt: ${saltResult.message}`);
597
+ }
598
+ const keyResult = await argon2idProvider.argon2id(password, saltResult.value, params);
599
+ if (keyResult.isFailure()) {
600
+ return fail(`Argon2id key derivation failed: ${keyResult.message}`);
601
+ }
602
+ if (keyResult.value.length !== Constants.AES_256_KEY_SIZE) {
603
+ return fail(`Argon2id outputBytes must be ${Constants.AES_256_KEY_SIZE} for KeyStore secrets, got ${keyResult.value.length}`);
604
+ }
605
+ const entry = {
606
+ name,
607
+ type: 'encryption-key',
608
+ key: keyResult.value,
609
+ description: options === null || options === void 0 ? void 0 : options.description,
610
+ createdAt: getCurrentTimestamp()
611
+ };
612
+ const warning = existing ? await this._releaseEntryResources(existing) : undefined;
613
+ this._secrets.set(name, entry);
614
+ this._dirty = true;
615
+ const keyDerivation = {
616
+ kdf: 'argon2id',
617
+ salt: this._cryptoProvider.toBase64(saltResult.value),
618
+ memoryKiB: params.memoryKiB,
619
+ iterations: params.iterations,
620
+ parallelism: params.parallelism
621
+ };
622
+ return succeed({ entry, replaced: existing !== undefined, warning, keyDerivation });
623
+ }
624
+ /**
625
+ * Verifies a candidate password against an Argon2id-derived entry using the
626
+ * supplied key derivation parameters. Constant-time comparison.
627
+ *
628
+ * @param name - Name of the secret to verify against
629
+ * @param password - Candidate password to test
630
+ * @param argon2idProvider - Argon2id provider (must produce bit-identical output for identical inputs)
631
+ * @param keyDerivation - The Argon2id key derivation parameters returned by `addSecretFromPasswordArgon2id`
632
+ * @returns Success(true) if candidate matches stored key, Success(false) if not,
633
+ * Failure if locked, secret missing, wrong type, or derivation fails
634
+ * @public
635
+ */
636
+ async verifySecretFromPasswordArgon2id(name, password, argon2idProvider, keyDerivation) {
637
+ if (!this._secrets) {
638
+ return fail('Key store is locked');
639
+ }
640
+ if (!password || password.length === 0) {
641
+ return fail('Password cannot be empty');
642
+ }
643
+ const entry = this._secrets.get(name);
644
+ if (!entry) {
645
+ return fail(`Secret '${name}' not found`);
646
+ }
647
+ if (entry.type !== 'encryption-key') {
648
+ return fail(`Secret '${name}' is not a password-verifiable encryption key (type: ${entry.type})`);
649
+ }
650
+ const saltResult = this._cryptoProvider.fromBase64(keyDerivation.salt);
651
+ if (saltResult.isFailure()) {
652
+ return fail(`Invalid salt: ${saltResult.message}`);
653
+ }
654
+ const params = {
655
+ memoryKiB: keyDerivation.memoryKiB,
656
+ iterations: keyDerivation.iterations,
657
+ parallelism: keyDerivation.parallelism,
658
+ outputBytes: entry.key.length
659
+ };
660
+ const derivedResult = await argon2idProvider.argon2id(password, saltResult.value, params);
661
+ if (derivedResult.isFailure()) {
662
+ return fail(`Argon2id key derivation failed: ${derivedResult.message}`);
663
+ }
664
+ return succeed(KeyStore._timingSafeEqual(derivedResult.value, entry.key));
665
+ }
666
+ /**
667
+ * Removes a secret by name. Vault-first: the in-memory vault entry is dropped
668
+ * before any storage cleanup runs. For asymmetric-keypair entries, best-effort
669
+ * calls {@link CryptoUtils.KeyStore.IPrivateKeyStorage}.delete on the entry's
670
+ * `id`; a failure is reported via `warning` on the result but does not roll
671
+ * back the vault removal.
458
672
  * @param name - Name of the secret to remove
459
- * @returns Success with removed entry, Failure if not found or locked
673
+ * @returns Success with removed entry (and optional warning), Failure if not found or locked
460
674
  * @public
461
675
  */
462
- removeSecret(name) {
676
+ async removeSecret(name) {
463
677
  if (!this._secrets) {
464
678
  return fail('Key store is locked');
465
679
  }
@@ -467,11 +681,12 @@ export class KeyStore {
467
681
  if (!entry) {
468
682
  return fail(`Secret '${name}' not found`);
469
683
  }
470
- // Clear the key before removing (security)
471
- entry.key.fill(0);
684
+ // Vault-first: drop the in-memory entry before touching storage so a
685
+ // storage failure cannot block removal.
472
686
  this._secrets.delete(name);
473
687
  this._dirty = true;
474
- return succeed(entry);
688
+ const warning = await this._releaseEntryResources(entry);
689
+ return succeed({ entry, warning });
475
690
  }
476
691
  /**
477
692
  * Imports an API key string into the vault.
@@ -482,7 +697,7 @@ export class KeyStore {
482
697
  * @returns Success with entry, Failure if locked, empty, or exists and !replace
483
698
  * @public
484
699
  */
485
- importApiKey(name, apiKey, options) {
700
+ async importApiKey(name, apiKey, options) {
486
701
  if (!this._secrets) {
487
702
  return fail('Key store is locked');
488
703
  }
@@ -492,8 +707,8 @@ export class KeyStore {
492
707
  if (!apiKey || apiKey.length === 0) {
493
708
  return fail('API key cannot be empty');
494
709
  }
495
- const exists = this._secrets.has(name);
496
- if (exists && !(options === null || options === void 0 ? void 0 : options.replace)) {
710
+ const existing = this._secrets.get(name);
711
+ if (existing && !(options === null || options === void 0 ? void 0 : options.replace)) {
497
712
  return fail(`Secret '${name}' already exists - use replace=true to overwrite`);
498
713
  }
499
714
  const encoder = new TextEncoder();
@@ -504,9 +719,10 @@ export class KeyStore {
504
719
  description: options === null || options === void 0 ? void 0 : options.description,
505
720
  createdAt: getCurrentTimestamp()
506
721
  };
722
+ const warning = existing ? await this._releaseEntryResources(existing) : undefined;
507
723
  this._secrets.set(name, entry);
508
724
  this._dirty = true;
509
- return succeed({ entry, replaced: exists });
725
+ return succeed({ entry, replaced: existing !== undefined, warning });
510
726
  }
511
727
  /**
512
728
  * Retrieves an API key string by name.
@@ -529,6 +745,174 @@ export class KeyStore {
529
745
  const decoder = new TextDecoder();
530
746
  return succeed(decoder.decode(entry.key));
531
747
  }
748
+ // ============================================================================
749
+ // Asymmetric Keypair Management
750
+ // ============================================================================
751
+ /**
752
+ * Adds a new asymmetric keypair to the vault. Storage-first: the private key
753
+ * is stored under a freshly-minted `id` before the public-key vault entry is
754
+ * committed. If the storage call fails, no vault entry is written and the
755
+ * operation returns Failure.
756
+ *
757
+ * When `replace: true` displaces an existing entry (asymmetric or symmetric),
758
+ * a fresh `id` is minted; the displaced entry's resources are released
759
+ * best-effort. Failure of the storage delete is reported via `warning` on the
760
+ * result but does not roll back the replacement.
761
+ *
762
+ * Requires a {@link CryptoUtils.KeyStore.IPrivateKeyStorage} backend
763
+ * supplied at construction.
764
+ *
765
+ * Extractability of the generated private key defaults to the storage
766
+ * backend's {@link CryptoUtils.KeyStore.IPrivateKeyStorage.supportsNonExtractable}.
767
+ * `options.extractable` overrides that default for this key only; requesting
768
+ * `extractable: false` against a backend that cannot store non-extractable
769
+ * keys fails loudly rather than silently generating an extractable key.
770
+ *
771
+ * @param name - Unique name for the entry
772
+ * @param options - Algorithm, optional description, replace flag, extractable override
773
+ * @returns Success with the new entry, Failure if locked, no provider, storage write failed,
774
+ * or the requested extractability cannot be honored by the backend
775
+ * @public
776
+ */
777
+ async addKeyPair(name, options) {
778
+ var _a;
779
+ if (!this._secrets) {
780
+ return fail('Key store is locked');
781
+ }
782
+ if (!name || name.length === 0) {
783
+ return fail('Entry name cannot be empty');
784
+ }
785
+ if (!this._privateKeyStorage) {
786
+ return fail('No private key storage configured');
787
+ }
788
+ const existing = this._secrets.get(name);
789
+ if (existing && !options.replace) {
790
+ return fail(`Secret '${name}' already exists - use replace=true to overwrite`);
791
+ }
792
+ // Generate the keypair before touching storage. extractable=true on backends
793
+ // that round-trip via JWK; extractable=false on backends that hold CryptoKey
794
+ // refs directly. `options.extractable` overrides the backend-derived default
795
+ // for this key only; a caller-requested non-extractable key against a
796
+ // backend that cannot store one fails loudly rather than silently
797
+ // upgrading to extractable.
798
+ const backendExtractable = !this._privateKeyStorage.supportsNonExtractable;
799
+ if (options.extractable === false && backendExtractable) {
800
+ return fail(`Cannot create non-extractable keypair for '${name}': storage backend does not support non-extractable keys`);
801
+ }
802
+ // Extractability of the LIVE stored key. Escrow is orthogonal — it never
803
+ // changes this; the escrow copy is captured separately from a transient
804
+ // extractable key.
805
+ const liveExtractable = (_a = options.extractable) !== null && _a !== void 0 ? _a : backendExtractable;
806
+ // For escrow we must be able to export the private key to JWK, which only
807
+ // works on an extractable key, so the transient keypair is generated
808
+ // extractable regardless of `liveExtractable`. Without escrow it is
809
+ // generated directly at the live setting.
810
+ const generateExtractable = options.escrow === true ? true : liveExtractable;
811
+ const keyPairResult = await this._cryptoProvider.generateKeyPair(options.algorithm, generateExtractable);
812
+ /* c8 ignore next 3 - crypto provider errors covered in nodeCryptoProvider tests; cannot be triggered here without mocking */
813
+ if (keyPairResult.isFailure()) {
814
+ return fail(`Failed to generate keypair for '${name}': ${keyPairResult.message}`);
815
+ }
816
+ const { publicKey, privateKey } = keyPairResult.value;
817
+ const jwkResult = await this._cryptoProvider.exportPublicKeyJwk(publicKey);
818
+ /* c8 ignore next 3 - export of an extractable freshly-generated public key is hard to fail */
819
+ if (jwkResult.isFailure()) {
820
+ return fail(`Failed to export public key for '${name}': ${jwkResult.message}`);
821
+ }
822
+ // Escrow (opt-in). The bare private key exists only as three local
823
+ // artifacts, none of which is returned or logged: (1) the transient
824
+ // extractable `privateKey` above; (2) the exported `escrowedPrivateKeyJwk`,
825
+ // which is carried in the vault entry (same custody class as the public
826
+ // JWK); (3) a freshly re-imported non-extractable key handed to `store()`
827
+ // when the live copy must be non-extractable. When the live copy is
828
+ // extractable the transient key IS the live copy and is stored directly.
829
+ let escrowedPrivateKeyJwk;
830
+ let keyToStore = privateKey;
831
+ if (options.escrow === true) {
832
+ // Chain the export → (conditional) non-extractable re-import so the
833
+ // failure dispatch lives in ts-utils rather than in local branch nodes.
834
+ // When the live copy is extractable the transient key IS the live copy;
835
+ // otherwise a freshly re-imported non-extractable key is stored instead.
836
+ const escrowPrepResult = await (await this._exportPrivateKeyJwk(privateKey, name)).thenOnSuccess(async (jwk) => {
837
+ if (liveExtractable) {
838
+ return succeed({ jwk, keyToStore: privateKey });
839
+ }
840
+ return (await this._importEscrowedPrivateKey(jwk, options.algorithm, false))
841
+ .withErrorFormat((msg) => `Failed to prepare non-extractable key for '${name}': ${msg}`)
842
+ .onSuccess((reimported) => succeed({ jwk, keyToStore: reimported }));
843
+ });
844
+ /* c8 ignore next 3 - escrowPrepResult only fails if exporting or re-importing a freshly-generated valid key fails, which a healthy provider cannot do (same untestable class as the keyPairResult/jwkResult/idResult guards); the import-failure path itself is covered via getKeyPair rehydration of a malformed escrow JWK */
845
+ if (escrowPrepResult.isFailure()) {
846
+ return fail(escrowPrepResult.message);
847
+ }
848
+ ({ jwk: escrowedPrivateKeyJwk, keyToStore } = escrowPrepResult.value);
849
+ }
850
+ const idResult = this._generateId();
851
+ /* c8 ignore next 3 - random-bytes failure is hard to trigger with a healthy provider */
852
+ if (idResult.isFailure()) {
853
+ return fail(`Failed to mint storage id for '${name}': ${idResult.message}`);
854
+ }
855
+ const id = idResult.value;
856
+ // Storage-first: write the private key before committing the vault entry.
857
+ const storeResult = await this._privateKeyStorage.store(id, keyToStore);
858
+ if (storeResult.isFailure()) {
859
+ return fail(`Failed to persist private key for '${name}': ${storeResult.message}`);
860
+ }
861
+ const entry = {
862
+ name,
863
+ type: 'asymmetric-keypair',
864
+ id,
865
+ algorithm: options.algorithm,
866
+ publicKeyJwk: jwkResult.value,
867
+ escrowedPrivateKeyJwk,
868
+ description: options.description,
869
+ createdAt: getCurrentTimestamp()
870
+ };
871
+ const warning = existing ? await this._releaseEntryResources(existing) : undefined;
872
+ this._secrets.set(name, entry);
873
+ this._dirty = true;
874
+ return succeed({ entry, replaced: existing !== undefined, warning });
875
+ }
876
+ /**
877
+ * Retrieves the keypair for an asymmetric-keypair entry. The private key is
878
+ * loaded from {@link CryptoUtils.KeyStore.IPrivateKeyStorage} on every call —
879
+ * the keystore never caches private `CryptoKey` references between calls.
880
+ * The public key is re-imported from the vault's JWK so callers always
881
+ * receive a `CryptoKey` rather than the JWK form.
882
+ *
883
+ * With `options.rehydrate: true`, if the storage backend holds no blob for
884
+ * the entry's `id` and the entry carries an `escrowedPrivateKeyJwk` (see
885
+ * `addKeyPair(name, { escrow: true })`), the escrowed JWK is imported and
886
+ * stored under the entry's `id` before being returned — recovering the
887
+ * private key on a fresh device from the vault plus master password. This is
888
+ * fill-a-gap only: an existing storage blob is never overwritten.
889
+ *
890
+ * @param name - Name of the entry
891
+ * @param options - Optional {@link CryptoUtils.KeyStore.IGetKeyPairOptions}
892
+ * (currently the `rehydrate` escrow-recovery flag).
893
+ * @returns Success with `{ publicKey, privateKey }`, Failure if not found,
894
+ * locked, wrong type, no provider, or storage load (and any escrow
895
+ * rehydration) failed.
896
+ * @public
897
+ */
898
+ async getKeyPair(name, options) {
899
+ if (!this._secrets) {
900
+ return fail('Key store is locked');
901
+ }
902
+ const entry = this._secrets.get(name);
903
+ if (!entry) {
904
+ return fail(`Secret '${name}' not found`);
905
+ }
906
+ if (entry.type !== 'asymmetric-keypair') {
907
+ return fail(`Secret '${name}' is not an asymmetric keypair (type: ${entry.type})`);
908
+ }
909
+ if (!this._privateKeyStorage) {
910
+ return fail('No private key storage configured');
911
+ }
912
+ return (await this._loadOrRehydratePrivateKey(name, entry, options)).thenOnSuccess(async (privateKey) => (await this._cryptoProvider.importPublicKeyJwk(entry.publicKeyJwk, entry.algorithm))
913
+ .withErrorFormat((msg) => `Failed to re-import public key for '${name}': ${msg}`)
914
+ .onSuccess((publicKey) => succeed({ publicKey, privateKey })));
915
+ }
532
916
  /**
533
917
  * Lists secret names filtered by type.
534
918
  * @param type - The secret type to filter by
@@ -568,7 +952,8 @@ export class KeyStore {
568
952
  if (oldName !== newName && this._secrets.has(newName)) {
569
953
  return fail(`Secret '${newName}' already exists`);
570
954
  }
571
- // Create new entry with new name (preserve type)
955
+ // Create new entry with new name. For asymmetric entries the spread
956
+ // preserves `id` so the storage handle survives the rename.
572
957
  const newEntry = Object.assign(Object.assign({}, entry), { name: newName });
573
958
  this._secrets.delete(oldName);
574
959
  this._secrets.set(newName, newEntry);
@@ -598,49 +983,29 @@ export class KeyStore {
598
983
  if (keyResult.isFailure()) {
599
984
  return fail(`Key derivation failed: ${keyResult.message}`);
600
985
  }
601
- // Build vault contents
602
- const secrets = {};
603
- for (const [name, entry] of this._secrets) {
604
- secrets[name] = {
605
- name: entry.name,
606
- type: entry.type,
607
- key: this._cryptoProvider.toBase64(entry.key),
608
- description: entry.description,
609
- createdAt: entry.createdAt
610
- };
986
+ return this._encryptVault(keyResult.value);
987
+ }
988
+ /**
989
+ * Saves the key store using a pre-derived key, bypassing PBKDF2 key
990
+ * derivation. Use this when the derived key has been stored externally
991
+ * (e.g., in another key store) and the original password is no longer
992
+ * available.
993
+ *
994
+ * The supplied key must be the same key that was (or would be) derived
995
+ * from the master password using the key store's PBKDF2 parameters.
996
+ *
997
+ * @param derivedKey - The pre-derived master key (32 bytes for AES-256)
998
+ * @returns Success with IKeyStoreFile, Failure if locked or key invalid
999
+ * @public
1000
+ */
1001
+ async saveWithKey(derivedKey) {
1002
+ if (!this._secrets || !this._salt) {
1003
+ return fail('Key store is locked');
611
1004
  }
612
- const vaultContents = {
613
- version: KEYSTORE_FORMAT,
614
- secrets
615
- };
616
- // Serialize and encrypt
617
- const jsonResult = captureResult(() => JSON.stringify(vaultContents));
618
- /* c8 ignore next 3 - error path tested but coverage intermittently missed */
619
- if (jsonResult.isFailure()) {
620
- return fail(`Failed to serialize vault: ${jsonResult.message}`);
1005
+ if (derivedKey.length !== Constants.AES_256_KEY_SIZE) {
1006
+ return fail(`Key must be ${Constants.AES_256_KEY_SIZE} bytes, got ${derivedKey.length}`);
621
1007
  }
622
- const encryptResult = await this._cryptoProvider.encrypt(jsonResult.value, keyResult.value);
623
- /* c8 ignore next 3 - crypto provider errors tested but coverage intermittently missed */
624
- if (encryptResult.isFailure()) {
625
- return fail(`Encryption failed: ${encryptResult.message}`);
626
- }
627
- const { iv, authTag, encryptedData } = encryptResult.value;
628
- const keystoreFileData = {
629
- format: KEYSTORE_FORMAT,
630
- algorithm: Constants.DEFAULT_ALGORITHM,
631
- iv: this._cryptoProvider.toBase64(iv),
632
- authTag: this._cryptoProvider.toBase64(authTag),
633
- encryptedData: this._cryptoProvider.toBase64(encryptedData),
634
- keyDerivation: {
635
- kdf: 'pbkdf2',
636
- salt: this._cryptoProvider.toBase64(this._salt),
637
- iterations: this._iterations
638
- }
639
- };
640
- this._keystoreFile = keystoreFileData;
641
- this._dirty = false;
642
- this._isNew = false;
643
- return succeed(keystoreFileData);
1008
+ return this._encryptVault(derivedKey);
644
1009
  }
645
1010
  /**
646
1011
  * Changes the master password.
@@ -708,6 +1073,9 @@ export class KeyStore {
708
1073
  if (secretResult.isFailure()) {
709
1074
  return fail(`encryptByName: ${secretResult.message}`);
710
1075
  }
1076
+ if (secretResult.value.type === 'asymmetric-keypair') {
1077
+ return fail(`encryptByName: secret '${secretName}' is an asymmetric keypair, not symmetric key material`);
1078
+ }
711
1079
  return createEncryptedFile({
712
1080
  content,
713
1081
  secretName,
@@ -735,6 +1103,9 @@ export class KeyStore {
735
1103
  if (!entry) {
736
1104
  return fail(`Secret '${secretName}' not found in key store`);
737
1105
  }
1106
+ if (entry.type === 'asymmetric-keypair') {
1107
+ return fail(`Secret '${secretName}' is an asymmetric keypair, not symmetric key material`);
1108
+ }
738
1109
  return succeed(entry.key);
739
1110
  };
740
1111
  return succeed(provider);
@@ -754,5 +1125,288 @@ export class KeyStore {
754
1125
  cryptoProvider: this._cryptoProvider
755
1126
  });
756
1127
  }
1128
+ // ============================================================================
1129
+ // Private: Vault Encryption / Decryption
1130
+ // ============================================================================
1131
+ /**
1132
+ * Encrypts the vault with a derived key and returns the key store file.
1133
+ * Shared by `save()` and `saveWithKey()`.
1134
+ */
1135
+ async _encryptVault(derivedKey) {
1136
+ // _secrets and _salt are guaranteed non-undefined by callers
1137
+ const secrets = this._secrets;
1138
+ const salt = this._salt;
1139
+ // Build vault contents
1140
+ const secretEntries = {};
1141
+ for (const [name, entry] of secrets) {
1142
+ if (entry.type === 'asymmetric-keypair') {
1143
+ secretEntries[name] = {
1144
+ name: entry.name,
1145
+ type: entry.type,
1146
+ id: entry.id,
1147
+ algorithm: entry.algorithm,
1148
+ publicKeyJwk: entry.publicKeyJwk,
1149
+ escrowedPrivateKeyJwk: entry.escrowedPrivateKeyJwk,
1150
+ description: entry.description,
1151
+ createdAt: entry.createdAt
1152
+ };
1153
+ }
1154
+ else {
1155
+ secretEntries[name] = {
1156
+ name: entry.name,
1157
+ type: entry.type,
1158
+ key: this._cryptoProvider.toBase64(entry.key),
1159
+ description: entry.description,
1160
+ createdAt: entry.createdAt
1161
+ };
1162
+ }
1163
+ }
1164
+ const vaultContents = {
1165
+ version: KEYSTORE_FORMAT,
1166
+ secrets: secretEntries
1167
+ };
1168
+ // Serialize and encrypt
1169
+ const jsonResult = captureResult(() => JSON.stringify(vaultContents));
1170
+ /* c8 ignore next 3 - error path tested but coverage intermittently missed */
1171
+ if (jsonResult.isFailure()) {
1172
+ return fail(`Failed to serialize vault: ${jsonResult.message}`);
1173
+ }
1174
+ const encryptResult = await this._cryptoProvider.encrypt(jsonResult.value, derivedKey);
1175
+ /* c8 ignore next 3 - crypto provider errors tested but coverage intermittently missed */
1176
+ if (encryptResult.isFailure()) {
1177
+ return fail(`Encryption failed: ${encryptResult.message}`);
1178
+ }
1179
+ const { iv, authTag, encryptedData } = encryptResult.value;
1180
+ const keystoreFileData = {
1181
+ format: KEYSTORE_FORMAT,
1182
+ algorithm: Constants.DEFAULT_ALGORITHM,
1183
+ iv: this._cryptoProvider.toBase64(iv),
1184
+ authTag: this._cryptoProvider.toBase64(authTag),
1185
+ encryptedData: this._cryptoProvider.toBase64(encryptedData),
1186
+ keyDerivation: {
1187
+ kdf: 'pbkdf2',
1188
+ salt: this._cryptoProvider.toBase64(salt),
1189
+ iterations: this._iterations
1190
+ }
1191
+ };
1192
+ this._keystoreFile = keystoreFileData;
1193
+ this._dirty = false;
1194
+ this._isNew = false;
1195
+ return succeed(keystoreFileData);
1196
+ }
1197
+ /**
1198
+ * Decrypts the vault with a derived key and loads secrets into memory.
1199
+ * Shared by `unlock()` and `unlockWithKey()`.
1200
+ */
1201
+ async _decryptVault(derivedKey) {
1202
+ const keystoreFile = this._keystoreFile;
1203
+ /* c8 ignore next 3 - defensive: _decryptVault is only called after a successful open() or create() */
1204
+ if (keystoreFile === undefined) {
1205
+ return fail('No key store file loaded');
1206
+ }
1207
+ const ivResult = this._cryptoProvider.fromBase64(keystoreFile.iv);
1208
+ const authTagResult = this._cryptoProvider.fromBase64(keystoreFile.authTag);
1209
+ const encryptedDataResult = this._cryptoProvider.fromBase64(keystoreFile.encryptedData);
1210
+ /* c8 ignore next 9 - base64 decode errors tested but coverage intermittently missed */
1211
+ if (ivResult.isFailure()) {
1212
+ return fail(`Invalid IV in key store file: ${ivResult.message}`);
1213
+ }
1214
+ if (authTagResult.isFailure()) {
1215
+ return fail(`Invalid auth tag in key store file: ${authTagResult.message}`);
1216
+ }
1217
+ if (encryptedDataResult.isFailure()) {
1218
+ return fail(`Invalid encrypted data in key store file: ${encryptedDataResult.message}`);
1219
+ }
1220
+ const decryptResult = await this._cryptoProvider.decrypt(encryptedDataResult.value, derivedKey, ivResult.value, authTagResult.value);
1221
+ if (decryptResult.isFailure()) {
1222
+ return fail('Incorrect password or corrupted key store');
1223
+ }
1224
+ // Parse the vault contents
1225
+ const parseResult = captureResult(() => JSON.parse(decryptResult.value));
1226
+ /* c8 ignore next 3 - error path tested but coverage intermittently missed */
1227
+ if (parseResult.isFailure()) {
1228
+ return fail(`Failed to parse vault contents: ${parseResult.message}`);
1229
+ }
1230
+ const vaultResult = keystoreVaultContents.convert(parseResult.value);
1231
+ /* c8 ignore next 3 - error path tested but coverage intermittently missed */
1232
+ if (vaultResult.isFailure()) {
1233
+ return fail(`Invalid vault format: ${vaultResult.message}`);
1234
+ }
1235
+ // Build secrets into local variables to avoid partial state on failure
1236
+ const saltResult = this._cryptoProvider.fromBase64(keystoreFile.keyDerivation.salt);
1237
+ if (saltResult.isFailure()) {
1238
+ return fail(`Invalid salt in key store file: ${saltResult.message}`);
1239
+ }
1240
+ const secrets = new Map();
1241
+ for (const [name, jsonEntry] of Object.entries(vaultResult.value.secrets)) {
1242
+ if (jsonEntry.type === 'asymmetric-keypair') {
1243
+ const entry = {
1244
+ name,
1245
+ type: jsonEntry.type,
1246
+ id: jsonEntry.id,
1247
+ algorithm: jsonEntry.algorithm,
1248
+ publicKeyJwk: jsonEntry.publicKeyJwk,
1249
+ escrowedPrivateKeyJwk: jsonEntry.escrowedPrivateKeyJwk,
1250
+ description: jsonEntry.description,
1251
+ createdAt: jsonEntry.createdAt
1252
+ };
1253
+ secrets.set(name, entry);
1254
+ }
1255
+ else {
1256
+ const keyBytesResult = this._cryptoProvider.fromBase64(jsonEntry.key);
1257
+ /* c8 ignore next 3 - error path tested but coverage intermittently missed */
1258
+ if (keyBytesResult.isFailure()) {
1259
+ return fail(`Invalid key for secret '${name}': ${keyBytesResult.message}`);
1260
+ }
1261
+ const entry = {
1262
+ name,
1263
+ type: jsonEntry.type,
1264
+ key: keyBytesResult.value,
1265
+ description: jsonEntry.description,
1266
+ createdAt: jsonEntry.createdAt
1267
+ };
1268
+ secrets.set(name, entry);
1269
+ }
1270
+ }
1271
+ // All validation passed — commit state atomically
1272
+ this._salt = saltResult.value;
1273
+ this._secrets = secrets;
1274
+ this._state = 'unlocked';
1275
+ this._dirty = false;
1276
+ return succeed(this);
1277
+ }
1278
+ // ============================================================================
1279
+ // Private: Helpers for asymmetric flows
1280
+ // ============================================================================
1281
+ /**
1282
+ * Releases the resources held by an entry being displaced from the vault.
1283
+ * Symmetric entries get their key buffer zeroed in place. Asymmetric entries
1284
+ * have their private-key blob best-effort deleted from
1285
+ * {@link CryptoUtils.KeyStore.IPrivateKeyStorage}; if the storage call fails,
1286
+ * a warning string is returned but the displacement still proceeds — the
1287
+ * orphaned blob is left for consumer-side GC. Without a configured provider,
1288
+ * asymmetric cleanup is silently skipped.
1289
+ * @returns A warning string if storage cleanup failed, otherwise undefined.
1290
+ */
1291
+ async _releaseEntryResources(entry) {
1292
+ if (entry.type === 'asymmetric-keypair') {
1293
+ if (!this._privateKeyStorage) {
1294
+ return undefined;
1295
+ }
1296
+ const deleteResult = await this._privateKeyStorage.delete(entry.id);
1297
+ if (deleteResult.isFailure()) {
1298
+ return `Failed to delete prior storage blob for '${entry.name}' (id ${entry.id}): ${deleteResult.message}`;
1299
+ }
1300
+ return undefined;
1301
+ }
1302
+ entry.key.fill(0);
1303
+ return undefined;
1304
+ }
1305
+ /**
1306
+ * Exports the transient extractable private `CryptoKey` to a JWK for escrow,
1307
+ * via WebCrypto's `exportKey('jwk', ...)` (cross-runtime through
1308
+ * `globalThis.crypto.subtle`). The result is carried in the vault entry — the
1309
+ * same custody class as the public JWK — and is never returned from a public
1310
+ * method nor logged.
1311
+ */
1312
+ async _exportPrivateKeyJwk(privateKey, name) {
1313
+ return captureAsyncResult(() => globalThis.crypto.subtle.exportKey('jwk', privateKey)).withErrorFormat((msg) => `Failed to export private key for escrow of '${name}': ${msg}`);
1314
+ }
1315
+ /**
1316
+ * Re-imports an escrowed private-key JWK as a `CryptoKey` for `algorithm`
1317
+ * with the requested extractability. The WebCrypto JWK-import descriptor is
1318
+ * shared between the public and private halves for every supported algorithm,
1319
+ * so `IKeyPairAlgorithmParams.importPublicKey` is reused; the private/public
1320
+ * distinction is carried by the requested usages (see
1321
+ * {@link KeyStore._privateKeyUsagesFor}). Cross-runtime through
1322
+ * `globalThis.crypto.subtle`.
1323
+ */
1324
+ async _importEscrowedPrivateKey(jwk, algorithm, extractable) {
1325
+ const params = keyPairAlgorithmParams[algorithm];
1326
+ const usages = KeyStore._privateKeyUsagesFor(jwk, params);
1327
+ return captureAsyncResult(() => globalThis.crypto.subtle.importKey('jwk', jwk, params.importPublicKey, extractable, usages)).withErrorFormat((msg) => `Failed to import escrowed private key: ${msg}`);
1328
+ }
1329
+ /**
1330
+ * Loads the private key for `entry` from storage, or — when
1331
+ * `options.rehydrate` is set, storage holds no blob, and the entry carries an
1332
+ * escrowed JWK — imports the escrowed key, persists it under the entry's `id`
1333
+ * (fill-a-gap; never overwrites an existing blob), and returns it.
1334
+ */
1335
+ async _loadOrRehydratePrivateKey(name, entry, options) {
1336
+ // Non-undefined by getKeyPair's precondition check.
1337
+ const storage = this._privateKeyStorage;
1338
+ const loadResult = await storage.load(entry.id);
1339
+ if (loadResult.isSuccess()) {
1340
+ return succeed(loadResult.value);
1341
+ }
1342
+ if ((options === null || options === void 0 ? void 0 : options.rehydrate) !== true || entry.escrowedPrivateKeyJwk === undefined) {
1343
+ return fail(`Failed to load private key for '${name}': ${loadResult.message}`);
1344
+ }
1345
+ // Fill-a-gap recovery: import the escrowed key non-extractable where the
1346
+ // backend supports it (extractable otherwise, since a JWK-round-tripping
1347
+ // backend cannot store a non-extractable key), then persist it.
1348
+ const backendExtractable = !storage.supportsNonExtractable;
1349
+ const importResult = await this._importEscrowedPrivateKey(entry.escrowedPrivateKeyJwk, entry.algorithm, backendExtractable);
1350
+ if (importResult.isFailure()) {
1351
+ return fail(`Failed to rehydrate private key for '${name}' from escrow: ${importResult.message}`);
1352
+ }
1353
+ const storeResult = await storage.store(entry.id, importResult.value);
1354
+ if (storeResult.isFailure()) {
1355
+ return fail(`Failed to persist rehydrated private key for '${name}': ${storeResult.message}`);
1356
+ }
1357
+ return succeed(importResult.value);
1358
+ }
1359
+ /**
1360
+ * Computes the key usages to request when importing an escrowed private JWK.
1361
+ * Mirrors `EncryptedFilePrivateKeyStorage`: intersect the algorithm's private
1362
+ * usages (its keypair usages minus the public-only ones) with the JWK's
1363
+ * recorded `key_ops` so we request exactly the operations the stored key
1364
+ * supports; fall back to the algorithm's private usages when `key_ops` is
1365
+ * absent.
1366
+ */
1367
+ static _privateKeyUsagesFor(jwk, params) {
1368
+ const privateUsages = params.keyPairUsages.filter((usage) => !PUBLIC_ONLY_USAGES.includes(usage));
1369
+ const keyOps = jwk.key_ops;
1370
+ if (keyOps === undefined) {
1371
+ return [...privateUsages];
1372
+ }
1373
+ return privateUsages.filter((usage) => keyOps.includes(usage));
1374
+ }
1375
+ /**
1376
+ * Constant-time byte comparison. Returns false immediately for length
1377
+ * mismatch (length is not secret); for equal-length inputs, walks the full
1378
+ * buffer accumulating differences via XOR so the running time does not leak
1379
+ * the position of the first differing byte.
1380
+ */
1381
+ static _timingSafeEqual(a, b) {
1382
+ /* c8 ignore next 3 - defensive: callers compare equal-length 32-byte PBKDF2 keys */
1383
+ if (a.length !== b.length) {
1384
+ return false;
1385
+ }
1386
+ let diff = 0;
1387
+ for (let i = 0; i < a.length; i++) {
1388
+ // eslint-disable-next-line no-bitwise
1389
+ diff |= a[i] ^ b[i];
1390
+ }
1391
+ return diff === 0;
1392
+ }
1393
+ /**
1394
+ * Mints a fresh UUID v4 storage handle using the crypto provider's
1395
+ * {@link CryptoUtils.ICryptoProvider.generateRandomBytes | generateRandomBytes}.
1396
+ * Random-bytes failures propagate as Failure.
1397
+ */
1398
+ _generateId() {
1399
+ return this._cryptoProvider.generateRandomBytes(16).onSuccess((bytes) => {
1400
+ // Per RFC 4122 §4.4: set version (4) and variant (10xx) bits.
1401
+ // eslint-disable-next-line no-bitwise
1402
+ bytes[6] = (bytes[6] & 0x0f) | 0x40;
1403
+ // eslint-disable-next-line no-bitwise
1404
+ bytes[8] = (bytes[8] & 0x3f) | 0x80;
1405
+ const hex = Array.from(bytes)
1406
+ .map((b) => b.toString(16).padStart(2, '0'))
1407
+ .join('');
1408
+ return succeed(`${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20, 32)}`);
1409
+ });
1410
+ }
757
1411
  }
758
1412
  //# sourceMappingURL=keyStore.js.map