@fgv/ts-web-extras 5.1.0-3 → 5.1.0-31

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 (275) hide show
  1. package/dist/packlets/crypto-utils/browserCryptoProvider.js +330 -18
  2. package/dist/packlets/crypto-utils/browserCryptoProvider.js.map +1 -1
  3. package/dist/packlets/crypto-utils/index.js +12 -0
  4. package/dist/packlets/crypto-utils/index.js.map +1 -1
  5. package/dist/packlets/file-tree/directoryHandleStore.js.map +1 -1
  6. package/dist/packlets/file-tree/fileApiTreeAccessors.js +1 -1
  7. package/dist/packlets/file-tree/fileApiTreeAccessors.js.map +1 -1
  8. package/dist/packlets/file-tree/fileSystemAccessTreeAccessors.js +2 -2
  9. package/dist/packlets/file-tree/fileSystemAccessTreeAccessors.js.map +1 -1
  10. package/dist/packlets/file-tree/httpTreeAccessors.js +74 -44
  11. package/dist/packlets/file-tree/httpTreeAccessors.js.map +1 -1
  12. package/dist/packlets/file-tree/localStorageTreeAccessors.js.map +1 -1
  13. package/dist/packlets/helpers/fileTreeHelpers.js +1 -1
  14. package/dist/packlets/helpers/fileTreeHelpers.js.map +1 -1
  15. package/dist/ts-web-extras.d.ts +141 -10
  16. package/dist/tsdoc-metadata.json +1 -1
  17. package/eslint.config.js +32 -0
  18. package/lib/packlets/crypto-utils/browserCryptoProvider.d.ts +115 -6
  19. package/lib/packlets/crypto-utils/browserCryptoProvider.d.ts.map +1 -1
  20. package/lib/packlets/crypto-utils/browserCryptoProvider.js +329 -17
  21. package/lib/packlets/crypto-utils/browserCryptoProvider.js.map +1 -1
  22. package/lib/packlets/crypto-utils/index.d.ts +13 -0
  23. package/lib/packlets/crypto-utils/index.d.ts.map +1 -1
  24. package/lib/packlets/crypto-utils/index.js +13 -0
  25. package/lib/packlets/crypto-utils/index.js.map +1 -1
  26. package/lib/packlets/file-tree/directoryHandleStore.d.ts +2 -2
  27. package/lib/packlets/file-tree/directoryHandleStore.d.ts.map +1 -1
  28. package/lib/packlets/file-tree/directoryHandleStore.js.map +1 -1
  29. package/lib/packlets/file-tree/fileApiTreeAccessors.d.ts +2 -2
  30. package/lib/packlets/file-tree/fileApiTreeAccessors.d.ts.map +1 -1
  31. package/lib/packlets/file-tree/fileApiTreeAccessors.js +1 -1
  32. package/lib/packlets/file-tree/fileApiTreeAccessors.js.map +1 -1
  33. package/lib/packlets/file-tree/fileSystemAccessTreeAccessors.d.ts.map +1 -1
  34. package/lib/packlets/file-tree/fileSystemAccessTreeAccessors.js +2 -2
  35. package/lib/packlets/file-tree/fileSystemAccessTreeAccessors.js.map +1 -1
  36. package/lib/packlets/file-tree/httpTreeAccessors.d.ts +6 -0
  37. package/lib/packlets/file-tree/httpTreeAccessors.d.ts.map +1 -1
  38. package/lib/packlets/file-tree/httpTreeAccessors.js +74 -44
  39. package/lib/packlets/file-tree/httpTreeAccessors.js.map +1 -1
  40. package/lib/packlets/file-tree/localStorageTreeAccessors.js.map +1 -1
  41. package/lib/packlets/helpers/fileTreeHelpers.d.ts +1 -1
  42. package/lib/packlets/helpers/fileTreeHelpers.d.ts.map +1 -1
  43. package/lib/packlets/helpers/fileTreeHelpers.js +6 -6
  44. package/lib/packlets/helpers/fileTreeHelpers.js.map +1 -1
  45. package/package.json +16 -15
  46. package/.rush/temp/61c1d4a91d98f048b475a5bb3e6541c5d27819de.tar.log +0 -237
  47. package/.rush/temp/chunked-rush-logs/ts-web-extras.build.chunks.jsonl +0 -55
  48. package/.rush/temp/operation/build/all.log +0 -55
  49. package/.rush/temp/operation/build/log-chunks.jsonl +0 -55
  50. package/.rush/temp/operation/build/state.json +0 -3
  51. package/.rush/temp/shrinkwrap-deps.json +0 -635
  52. package/CHANGELOG.md +0 -23
  53. package/config/api-extractor.json +0 -343
  54. package/config/jest.config.json +0 -19
  55. package/config/rig.json +0 -16
  56. package/config/typedoc.json +0 -6
  57. package/dist/test/mocks/idb-keyval.js +0 -6
  58. package/dist/test/mocks/idb-keyval.js.map +0 -1
  59. package/dist/test/setupTests.js +0 -74
  60. package/dist/test/setupTests.js.map +0 -1
  61. package/dist/test/unit/browserHashProvider.test.js +0 -140
  62. package/dist/test/unit/browserHashProvider.test.js.map +0 -1
  63. package/dist/test/unit/directoryHandleStore.test.js +0 -190
  64. package/dist/test/unit/directoryHandleStore.test.js.map +0 -1
  65. package/dist/test/unit/fileApiTreeAccessors.test.js +0 -1188
  66. package/dist/test/unit/fileApiTreeAccessors.test.js.map +0 -1
  67. package/dist/test/unit/fileApiTypes.test.js +0 -472
  68. package/dist/test/unit/fileApiTypes.test.js.map +0 -1
  69. package/dist/test/unit/fileSystemAccessTreeAccessors.test.js +0 -622
  70. package/dist/test/unit/fileSystemAccessTreeAccessors.test.js.map +0 -1
  71. package/dist/test/unit/fileTreeHelpers.test.js +0 -590
  72. package/dist/test/unit/fileTreeHelpers.test.js.map +0 -1
  73. package/dist/test/unit/httpTreeAccessors.test.js +0 -1229
  74. package/dist/test/unit/httpTreeAccessors.test.js.map +0 -1
  75. package/dist/test/unit/localStorageTreeAccessors.test.js +0 -812
  76. package/dist/test/unit/localStorageTreeAccessors.test.js.map +0 -1
  77. package/dist/test/unit/urlParams.test.js +0 -393
  78. package/dist/test/unit/urlParams.test.js.map +0 -1
  79. package/dist/test/utils/fileSystemAccessMocks.js +0 -271
  80. package/dist/test/utils/fileSystemAccessMocks.js.map +0 -1
  81. package/dist/test/utils/testHelpers.js +0 -124
  82. package/dist/test/utils/testHelpers.js.map +0 -1
  83. package/docs/@fgv/namespaces/CryptoUtils/README.md +0 -18
  84. package/docs/@fgv/namespaces/CryptoUtils/classes/BrowserCryptoProvider.md +0 -203
  85. package/docs/@fgv/namespaces/CryptoUtils/classes/BrowserHashProvider.md +0 -63
  86. package/docs/@fgv/namespaces/CryptoUtils/functions/createBrowserCryptoProvider.md +0 -18
  87. package/docs/@fgv/namespaces/FileTreeHelpers/README.md +0 -19
  88. package/docs/@fgv/namespaces/FileTreeHelpers/functions/extractFileListMetadata.md +0 -23
  89. package/docs/@fgv/namespaces/FileTreeHelpers/functions/extractFileMetadata.md +0 -23
  90. package/docs/@fgv/namespaces/FileTreeHelpers/functions/fromDirectoryUpload.md +0 -33
  91. package/docs/@fgv/namespaces/FileTreeHelpers/functions/fromFileList.md +0 -33
  92. package/docs/@fgv/namespaces/FileTreeHelpers/functions/getOriginalFile.md +0 -25
  93. package/docs/@fgv/namespaces/FileTreeHelpers/variables/defaultFileApiTreeInitParams.md +0 -11
  94. package/docs/README.md +0 -78
  95. package/docs/classes/DirectoryHandleStore.md +0 -116
  96. package/docs/classes/FileApiTreeAccessors.md +0 -286
  97. package/docs/classes/FileSystemAccessTreeAccessors.md +0 -557
  98. package/docs/classes/HttpTreeAccessors.md +0 -508
  99. package/docs/classes/LocalStorageTreeAccessors.md +0 -520
  100. package/docs/functions/exportAsJson.md +0 -23
  101. package/docs/functions/exportUsingFileSystemAPI.md +0 -26
  102. package/docs/functions/extractDirectoryPath.md +0 -23
  103. package/docs/functions/isDirectoryHandle.md +0 -23
  104. package/docs/functions/isFileHandle.md +0 -23
  105. package/docs/functions/isFilePath.md +0 -21
  106. package/docs/functions/parseContextFilter.md +0 -22
  107. package/docs/functions/parseQualifierDefaults.md +0 -22
  108. package/docs/functions/parseResourceTypes.md +0 -22
  109. package/docs/functions/parseUrlParameters.md +0 -15
  110. package/docs/functions/safeShowDirectoryPicker.md +0 -24
  111. package/docs/functions/safeShowOpenFilePicker.md +0 -24
  112. package/docs/functions/safeShowSaveFilePicker.md +0 -24
  113. package/docs/functions/supportsFileSystemAccess.md +0 -23
  114. package/docs/interfaces/FilePickerAcceptType.md +0 -16
  115. package/docs/interfaces/FileSystemCreateWritableOptions.md +0 -15
  116. package/docs/interfaces/FileSystemDirectoryHandle.md +0 -187
  117. package/docs/interfaces/FileSystemFileHandle.md +0 -106
  118. package/docs/interfaces/FileSystemGetDirectoryOptions.md +0 -15
  119. package/docs/interfaces/FileSystemGetFileOptions.md +0 -15
  120. package/docs/interfaces/FileSystemHandle.md +0 -69
  121. package/docs/interfaces/FileSystemHandlePermissionDescriptor.md +0 -15
  122. package/docs/interfaces/FileSystemRemoveOptions.md +0 -15
  123. package/docs/interfaces/FileSystemWritableFileStream.md +0 -127
  124. package/docs/interfaces/IDirectoryHandleTreeInitializer.md +0 -17
  125. package/docs/interfaces/IFileHandleTreeInitializer.md +0 -16
  126. package/docs/interfaces/IFileListTreeInitializer.md +0 -15
  127. package/docs/interfaces/IFileMetadata.md +0 -19
  128. package/docs/interfaces/IFileSystemAccessTreeParams.md +0 -30
  129. package/docs/interfaces/IFsAccessApis.md +0 -57
  130. package/docs/interfaces/IHttpTreeParams.md +0 -32
  131. package/docs/interfaces/ILocalStorageTreeParams.md +0 -30
  132. package/docs/interfaces/IUrlConfigOptions.md +0 -27
  133. package/docs/interfaces/ShowDirectoryPickerOptions.md +0 -17
  134. package/docs/interfaces/ShowOpenFilePickerOptions.md +0 -19
  135. package/docs/interfaces/ShowSaveFilePickerOptions.md +0 -19
  136. package/docs/type-aliases/TreeInitializer.md +0 -11
  137. package/docs/type-aliases/WellKnownDirectory.md +0 -11
  138. package/docs/type-aliases/WindowWithFsAccess.md +0 -11
  139. package/docs/variables/DEFAULT_DIRECTORY_HANDLE_DB.md +0 -11
  140. package/docs/variables/DEFAULT_DIRECTORY_HANDLE_STORE.md +0 -11
  141. package/etc/ts-web-extras.api.md +0 -433
  142. package/lib/test/mocks/idb-keyval.d.ts +0 -6
  143. package/lib/test/mocks/idb-keyval.d.ts.map +0 -1
  144. package/lib/test/mocks/idb-keyval.js +0 -9
  145. package/lib/test/mocks/idb-keyval.js.map +0 -1
  146. package/lib/test/setupTests.d.ts +0 -2
  147. package/lib/test/setupTests.d.ts.map +0 -1
  148. package/lib/test/setupTests.js +0 -76
  149. package/lib/test/setupTests.js.map +0 -1
  150. package/lib/test/unit/browserHashProvider.test.d.ts +0 -2
  151. package/lib/test/unit/browserHashProvider.test.d.ts.map +0 -1
  152. package/lib/test/unit/browserHashProvider.test.js +0 -142
  153. package/lib/test/unit/browserHashProvider.test.js.map +0 -1
  154. package/lib/test/unit/directoryHandleStore.test.d.ts +0 -2
  155. package/lib/test/unit/directoryHandleStore.test.d.ts.map +0 -1
  156. package/lib/test/unit/directoryHandleStore.test.js +0 -192
  157. package/lib/test/unit/directoryHandleStore.test.js.map +0 -1
  158. package/lib/test/unit/fileApiTreeAccessors.test.d.ts +0 -2
  159. package/lib/test/unit/fileApiTreeAccessors.test.d.ts.map +0 -1
  160. package/lib/test/unit/fileApiTreeAccessors.test.js +0 -1190
  161. package/lib/test/unit/fileApiTreeAccessors.test.js.map +0 -1
  162. package/lib/test/unit/fileApiTypes.test.d.ts +0 -2
  163. package/lib/test/unit/fileApiTypes.test.d.ts.map +0 -1
  164. package/lib/test/unit/fileApiTypes.test.js +0 -474
  165. package/lib/test/unit/fileApiTypes.test.js.map +0 -1
  166. package/lib/test/unit/fileSystemAccessTreeAccessors.test.d.ts +0 -2
  167. package/lib/test/unit/fileSystemAccessTreeAccessors.test.d.ts.map +0 -1
  168. package/lib/test/unit/fileSystemAccessTreeAccessors.test.js +0 -624
  169. package/lib/test/unit/fileSystemAccessTreeAccessors.test.js.map +0 -1
  170. package/lib/test/unit/fileTreeHelpers.test.d.ts +0 -2
  171. package/lib/test/unit/fileTreeHelpers.test.d.ts.map +0 -1
  172. package/lib/test/unit/fileTreeHelpers.test.js +0 -592
  173. package/lib/test/unit/fileTreeHelpers.test.js.map +0 -1
  174. package/lib/test/unit/httpTreeAccessors.test.d.ts +0 -2
  175. package/lib/test/unit/httpTreeAccessors.test.d.ts.map +0 -1
  176. package/lib/test/unit/httpTreeAccessors.test.js +0 -1231
  177. package/lib/test/unit/httpTreeAccessors.test.js.map +0 -1
  178. package/lib/test/unit/localStorageTreeAccessors.test.d.ts +0 -2
  179. package/lib/test/unit/localStorageTreeAccessors.test.d.ts.map +0 -1
  180. package/lib/test/unit/localStorageTreeAccessors.test.js +0 -814
  181. package/lib/test/unit/localStorageTreeAccessors.test.js.map +0 -1
  182. package/lib/test/unit/urlParams.test.d.ts +0 -2
  183. package/lib/test/unit/urlParams.test.d.ts.map +0 -1
  184. package/lib/test/unit/urlParams.test.js +0 -395
  185. package/lib/test/unit/urlParams.test.js.map +0 -1
  186. package/lib/test/utils/fileSystemAccessMocks.d.ts +0 -53
  187. package/lib/test/utils/fileSystemAccessMocks.d.ts.map +0 -1
  188. package/lib/test/utils/fileSystemAccessMocks.js +0 -277
  189. package/lib/test/utils/fileSystemAccessMocks.js.map +0 -1
  190. package/lib/test/utils/testHelpers.d.ts +0 -51
  191. package/lib/test/utils/testHelpers.d.ts.map +0 -1
  192. package/lib/test/utils/testHelpers.js +0 -133
  193. package/lib/test/utils/testHelpers.js.map +0 -1
  194. package/rush-logs/ts-web-extras.build.cache.log +0 -3
  195. package/rush-logs/ts-web-extras.build.log +0 -55
  196. package/src/index.browser.ts +0 -24
  197. package/src/index.ts +0 -47
  198. package/src/packlets/crypto-utils/browserCryptoProvider.ts +0 -311
  199. package/src/packlets/crypto-utils/browserHashProvider.ts +0 -73
  200. package/src/packlets/crypto-utils/index.ts +0 -29
  201. package/src/packlets/file-api-types/index.ts +0 -366
  202. package/src/packlets/file-tree/directoryHandleStore.ts +0 -136
  203. package/src/packlets/file-tree/fileApiTreeAccessors.ts +0 -528
  204. package/src/packlets/file-tree/fileSystemAccessTreeAccessors.ts +0 -519
  205. package/src/packlets/file-tree/httpTreeAccessors.ts +0 -448
  206. package/src/packlets/file-tree/index.ts +0 -32
  207. package/src/packlets/file-tree/localStorageTreeAccessors.ts +0 -430
  208. package/src/packlets/helpers/fileTreeHelpers.ts +0 -107
  209. package/src/packlets/helpers/index.ts +0 -28
  210. package/src/packlets/url-utils/index.ts +0 -28
  211. package/src/packlets/url-utils/urlParams.ts +0 -245
  212. package/src/test/mocks/idb-keyval.ts +0 -5
  213. package/src/test/setupTests.ts +0 -87
  214. package/src/test/unit/browserHashProvider.test.ts +0 -155
  215. package/src/test/unit/browserHashProvider.test.ts.bak +0 -376
  216. package/src/test/unit/directoryHandleStore.test.ts +0 -251
  217. package/src/test/unit/fileApiTreeAccessors.test.ts +0 -1387
  218. package/src/test/unit/fileApiTypes.test.ts +0 -587
  219. package/src/test/unit/fileSystemAccessTreeAccessors.test.ts +0 -885
  220. package/src/test/unit/fileTreeHelpers.test.ts +0 -694
  221. package/src/test/unit/httpTreeAccessors.test.ts +0 -1571
  222. package/src/test/unit/localStorageTreeAccessors.test.ts +0 -1014
  223. package/src/test/unit/urlParams.test.ts +0 -464
  224. package/src/test/utils/fileSystemAccessMocks.ts +0 -353
  225. package/src/test/utils/testHelpers.ts +0 -155
  226. package/temp/build/typescript/ts_8nwakTlr.json +0 -1
  227. package/temp/coverage/base.css +0 -224
  228. package/temp/coverage/block-navigation.js +0 -87
  229. package/temp/coverage/crypto-utils/browserCryptoProvider.ts.html +0 -1018
  230. package/temp/coverage/crypto-utils/browserHashProvider.ts.html +0 -304
  231. package/temp/coverage/crypto-utils/index.html +0 -131
  232. package/temp/coverage/favicon.png +0 -0
  233. package/temp/coverage/file-tree/directoryHandleStore.ts.html +0 -493
  234. package/temp/coverage/file-tree/fileApiTreeAccessors.ts.html +0 -1669
  235. package/temp/coverage/file-tree/fileSystemAccessTreeAccessors.ts.html +0 -1642
  236. package/temp/coverage/file-tree/httpTreeAccessors.ts.html +0 -1429
  237. package/temp/coverage/file-tree/index.html +0 -176
  238. package/temp/coverage/file-tree/localStorageTreeAccessors.ts.html +0 -1375
  239. package/temp/coverage/helpers/fileTreeHelpers.ts.html +0 -406
  240. package/temp/coverage/helpers/index.html +0 -116
  241. package/temp/coverage/index.html +0 -161
  242. package/temp/coverage/lcov-report/base.css +0 -224
  243. package/temp/coverage/lcov-report/block-navigation.js +0 -87
  244. package/temp/coverage/lcov-report/crypto-utils/browserCryptoProvider.ts.html +0 -1018
  245. package/temp/coverage/lcov-report/crypto-utils/browserHashProvider.ts.html +0 -304
  246. package/temp/coverage/lcov-report/crypto-utils/index.html +0 -131
  247. package/temp/coverage/lcov-report/favicon.png +0 -0
  248. package/temp/coverage/lcov-report/file-tree/directoryHandleStore.ts.html +0 -493
  249. package/temp/coverage/lcov-report/file-tree/fileApiTreeAccessors.ts.html +0 -1669
  250. package/temp/coverage/lcov-report/file-tree/fileSystemAccessTreeAccessors.ts.html +0 -1642
  251. package/temp/coverage/lcov-report/file-tree/httpTreeAccessors.ts.html +0 -1429
  252. package/temp/coverage/lcov-report/file-tree/index.html +0 -176
  253. package/temp/coverage/lcov-report/file-tree/localStorageTreeAccessors.ts.html +0 -1375
  254. package/temp/coverage/lcov-report/helpers/fileTreeHelpers.ts.html +0 -406
  255. package/temp/coverage/lcov-report/helpers/index.html +0 -116
  256. package/temp/coverage/lcov-report/index.html +0 -161
  257. package/temp/coverage/lcov-report/prettify.css +0 -1
  258. package/temp/coverage/lcov-report/prettify.js +0 -2
  259. package/temp/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  260. package/temp/coverage/lcov-report/sorter.js +0 -210
  261. package/temp/coverage/lcov-report/url-utils/index.html +0 -116
  262. package/temp/coverage/lcov-report/url-utils/urlParams.ts.html +0 -820
  263. package/temp/coverage/lcov.info +0 -3597
  264. package/temp/coverage/prettify.css +0 -1
  265. package/temp/coverage/prettify.js +0 -2
  266. package/temp/coverage/sort-arrow-sprite.png +0 -0
  267. package/temp/coverage/sorter.js +0 -210
  268. package/temp/coverage/url-utils/index.html +0 -116
  269. package/temp/coverage/url-utils/urlParams.ts.html +0 -820
  270. package/temp/test/jest/haste-map-7492f1b44480e0cdd1f220078fb3afd8-c8dd6c3430605adeb2f1cadf4f75e791-8c9336785555d572065b28c111982ba4 +0 -0
  271. package/temp/test/jest/jest-transform-cache-7492f1b44480e0cdd1f220078fb3afd8-79ef2876fae7ca75eedb2aa53dc48338/d6/package_d6e3b0fa94752e16b0b2a2777739b973 +0 -53
  272. package/temp/test/jest/perf-cache-7492f1b44480e0cdd1f220078fb3afd8-da39a3ee5e6b4b0d3255bfef95601890 +0 -1
  273. package/temp/ts-web-extras.api.json +0 -8850
  274. package/temp/ts-web-extras.api.md +0 -433
  275. package/tsconfig.json +0 -7
@@ -1,7 +1,5 @@
1
- import { Result } from '@fgv/ts-utils';
1
+ import { Result, Uuid } from '@fgv/ts-utils';
2
2
  import { CryptoUtils } from '@fgv/ts-extras';
3
- type ICryptoProvider = CryptoUtils.ICryptoProvider;
4
- type IEncryptionResult = CryptoUtils.IEncryptionResult;
5
3
  /**
6
4
  * Browser implementation of `ICryptoProvider` using the Web Crypto API.
7
5
  * Uses AES-256-GCM for authenticated encryption.
@@ -11,7 +9,7 @@ type IEncryptionResult = CryptoUtils.IEncryptionResult;
11
9
  *
12
10
  * @public
13
11
  */
14
- export declare class BrowserCryptoProvider implements ICryptoProvider {
12
+ export declare class BrowserCryptoProvider implements CryptoUtils.ICryptoProvider {
15
13
  private readonly _crypto;
16
14
  /**
17
15
  * Creates a new {@link CryptoUtils.BrowserCryptoProvider | BrowserCryptoProvider}.
@@ -24,7 +22,7 @@ export declare class BrowserCryptoProvider implements ICryptoProvider {
24
22
  * @param key - 32-byte encryption key
25
23
  * @returns `Success` with encryption result, or `Failure` with an error.
26
24
  */
27
- encrypt(plaintext: string, key: Uint8Array): Promise<Result<IEncryptionResult>>;
25
+ encrypt(plaintext: string, key: Uint8Array): Promise<Result<CryptoUtils.IEncryptionResult>>;
28
26
  /**
29
27
  * Decrypts ciphertext using AES-256-GCM.
30
28
  * @param encryptedData - Encrypted bytes
@@ -47,12 +45,25 @@ export declare class BrowserCryptoProvider implements ICryptoProvider {
47
45
  * @returns Success with derived 32-byte key, or Failure with error
48
46
  */
49
47
  deriveKey(password: string, salt: Uint8Array, iterations: number): Promise<Result<Uint8Array>>;
48
+ /**
49
+ * Computes a SHA-256 hash of the given data.
50
+ * @param data - UTF-8 string to hash
51
+ * @returns `Success` with hex-encoded hash string, or `Failure` with an error.
52
+ */
53
+ sha256(data: string): Promise<Result<string>>;
50
54
  /**
51
55
  * Generates cryptographically secure random bytes.
52
56
  * @param length - Number of bytes to generate
53
57
  * @returns Success with random bytes, or Failure with error
54
58
  */
55
59
  generateRandomBytes(length: number): Result<Uint8Array>;
60
+ /**
61
+ * Generates a cryptographically random UUIDv4 using the injected
62
+ * `Crypto` instance.
63
+ * @returns `Success` with the generated UUID, or `Failure` if the underlying
64
+ * `Crypto` instance does not expose `randomUUID`.
65
+ */
66
+ generateUuid(): Result<Uuid>;
56
67
  /**
57
68
  * Encodes binary data to base64 string.
58
69
  * @param data - Binary data to encode
@@ -65,6 +76,105 @@ export declare class BrowserCryptoProvider implements ICryptoProvider {
65
76
  * @returns Success with decoded bytes, or Failure if invalid base64
66
77
  */
67
78
  fromBase64(base64: string): Result<Uint8Array>;
79
+ /**
80
+ * Generates a new asymmetric keypair via Web Crypto.
81
+ * @param algorithm - The algorithm to use.
82
+ * @param extractable - Whether the resulting keys may be exported.
83
+ * @returns `Success` with the generated `CryptoKeyPair`, or `Failure` with an error.
84
+ */
85
+ generateKeyPair(algorithm: CryptoUtils.KeyPairAlgorithm, extractable: boolean): Promise<Result<CryptoKeyPair>>;
86
+ /**
87
+ * Exports a public `CryptoKey` as a JSON Web Key.
88
+ * @remarks
89
+ * Rejects non-public keys at runtime. WebCrypto's `exportKey('jwk', ...)`
90
+ * does not enforce public-vs-private; without this guard a caller that
91
+ * passed an extractable private key would receive its private fields
92
+ * (`d`, `p`, `q`, ...) as JWK, defeating the method's name.
93
+ * @param publicKey - Extractable public key to export.
94
+ * @returns `Success` with the JWK, or `Failure` if not a public key or if export fails.
95
+ */
96
+ exportPublicKeyJwk(publicKey: CryptoKey): Promise<Result<JsonWebKey>>;
97
+ /**
98
+ * Imports a public-key JWK as a `CryptoKey` for the requested algorithm.
99
+ * @param jwk - The JSON Web Key produced by a prior export.
100
+ * @param algorithm - The algorithm the key was generated for.
101
+ * @returns `Success` with the imported public `CryptoKey`, or `Failure` with an error.
102
+ */
103
+ importPublicKeyJwk(jwk: JsonWebKey, algorithm: CryptoUtils.KeyPairAlgorithm): Promise<Result<CryptoKey>>;
104
+ /**
105
+ * Exports a public `CryptoKey` as a DER-encoded SPKI blob.
106
+ * @param publicKey - The public `CryptoKey` to export.
107
+ * @returns `Success` with the raw SPKI bytes, or `Failure` with error context.
108
+ */
109
+ exportPublicKeySpki(publicKey: CryptoKey): Promise<Result<Uint8Array>>;
110
+ /**
111
+ * Imports a public key from a DER-encoded SPKI blob.
112
+ * @param spkiBytes - The raw SPKI bytes.
113
+ * @param algorithm - The algorithm the key was generated for.
114
+ * @returns `Success` with the imported public `CryptoKey`, or `Failure` with error context.
115
+ */
116
+ importPublicKeySpki(spkiBytes: Uint8Array, algorithm: CryptoUtils.KeyPairAlgorithm): Promise<Result<CryptoKey>>;
117
+ /**
118
+ * Signs `data` with `privateKey` using the algorithm inferred from the key.
119
+ * @param privateKey - A signing `CryptoKey` (`'ecdsa-p256'` or `'ed25519'`).
120
+ * @param data - The bytes to sign.
121
+ * @returns `Success` with the raw signature bytes, or `Failure` with error context.
122
+ */
123
+ sign(privateKey: CryptoKey, data: Uint8Array): Promise<Result<Uint8Array>>;
124
+ /**
125
+ * Verifies a signature produced by {@link BrowserCryptoProvider.sign}.
126
+ * @param publicKey - A verify `CryptoKey` (`'ecdsa-p256'` or `'ed25519'`).
127
+ * @param signature - The raw signature bytes.
128
+ * @param data - The original data that was signed.
129
+ * @returns `Success` with `true` if valid, `false` if not, or `Failure` with error context.
130
+ */
131
+ verify(publicKey: CryptoKey, signature: Uint8Array, data: Uint8Array): Promise<Result<boolean>>;
132
+ /**
133
+ * Compares two byte arrays in constant time.
134
+ *
135
+ * Accumulates XOR differences with bitwise-OR; no early-return is possible
136
+ * once the length check passes, making timing independent of the byte
137
+ * values. Returns `false` immediately on length mismatch (length is not
138
+ * secret in normal use).
139
+ * @param a - First byte array.
140
+ * @param b - Second byte array.
141
+ * @returns `true` if lengths match and all bytes are equal, `false` otherwise.
142
+ */
143
+ timingSafeEqual(a: Uint8Array, b: Uint8Array): boolean;
144
+ /**
145
+ * Computes an HMAC-SHA256 MAC for `data` using `key`.
146
+ * @param key - An HMAC `CryptoKey` with `'sign'` usage.
147
+ * @param data - The bytes to authenticate.
148
+ * @returns `Success` with the 32-byte MAC, or `Failure` with error context.
149
+ */
150
+ hmacSha256(key: CryptoKey, data: Uint8Array): Promise<Result<Uint8Array>>;
151
+ /**
152
+ * Verifies an HMAC-SHA256 MAC in constant time.
153
+ * @param key - An HMAC `CryptoKey` with `'sign'` usage.
154
+ * @param signature - The MAC bytes to verify.
155
+ * @param data - The original data that was authenticated.
156
+ * @returns `Success` with `true` if valid, `false` if not, or `Failure` with error context.
157
+ */
158
+ verifyHmacSha256(key: CryptoKey, signature: Uint8Array, data: Uint8Array): Promise<Result<boolean>>;
159
+ /**
160
+ * Wraps `plaintext` for the holder of `recipientPublicKey` using
161
+ * ECIES (ECDH P-256 + HKDF-SHA256 + AES-GCM-256). See
162
+ * {@link CryptoUtils.ICryptoProvider.wrapBytes | ICryptoProvider.wrapBytes}.
163
+ * @param plaintext - The bytes to wrap.
164
+ * @param recipientPublicKey - The recipient's ECDH P-256 public `CryptoKey`.
165
+ * @param options - HKDF salt and info; see {@link CryptoUtils.IWrapBytesOptions | IWrapBytesOptions}.
166
+ * @returns `Success` with the wrapped payload, or `Failure` with an error.
167
+ */
168
+ wrapBytes(plaintext: Uint8Array, recipientPublicKey: CryptoKey, options: CryptoUtils.IWrapBytesOptions): Promise<Result<CryptoUtils.IWrappedBytes>>;
169
+ /**
170
+ * Unwraps a payload produced by `wrapBytes` using the recipient's private
171
+ * key. See {@link CryptoUtils.ICryptoProvider.unwrapBytes | ICryptoProvider.unwrapBytes}.
172
+ * @param wrapped - The wrapped payload.
173
+ * @param recipientPrivateKey - The recipient's ECDH P-256 private `CryptoKey`.
174
+ * @param options - HKDF salt and info matching the wrap call.
175
+ * @returns `Success` with the original `plaintext`, or `Failure` with an error.
176
+ */
177
+ unwrapBytes(wrapped: CryptoUtils.IWrappedBytes, recipientPrivateKey: CryptoKey, options: CryptoUtils.IWrapBytesOptions): Promise<Result<Uint8Array>>;
68
178
  }
69
179
  /**
70
180
  * Creates a {@link CryptoUtils.BrowserCryptoProvider | BrowserCryptoProvider} if Web
@@ -73,5 +183,4 @@ export declare class BrowserCryptoProvider implements ICryptoProvider {
73
183
  * @public
74
184
  */
75
185
  export declare function createBrowserCryptoProvider(): Result<BrowserCryptoProvider>;
76
- export {};
77
186
  //# sourceMappingURL=browserCryptoProvider.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"browserCryptoProvider.d.ts","sourceRoot":"","sources":["../../../src/packlets/crypto-utils/browserCryptoProvider.ts"],"names":[],"mappings":"AAqBA,OAAO,EAA0B,MAAM,EAAW,MAAM,eAAe,CAAC;AACxE,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAE7C,KAAK,eAAe,GAAG,WAAW,CAAC,eAAe,CAAC;AACnD,KAAK,iBAAiB,GAAG,WAAW,CAAC,iBAAiB,CAAC;AAevD;;;;;;;;GAQG;AACH,qBAAa,qBAAsB,YAAW,eAAe;IAC3D,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IAEjC;;;OAGG;gBACgB,SAAS,CAAC,EAAE,MAAM;IAYrC;;;;;OAKG;IACU,OAAO,CAAC,SAAS,EAAE,MAAM,EAAE,GAAG,EAAE,UAAU,GAAG,OAAO,CAAC,MAAM,CAAC,iBAAiB,CAAC,CAAC;IAmD5F;;;;;;;OAOG;IACU,OAAO,CAClB,aAAa,EAAE,UAAU,EACzB,GAAG,EAAE,UAAU,EACf,EAAE,EAAE,UAAU,EACd,OAAO,EAAE,UAAU,GAClB,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAgD1B;;;OAGG;IACU,WAAW,IAAI,OAAO,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;IASvD;;;;;;OAMG;IACU,SAAS,CACpB,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,UAAU,EAChB,UAAU,EAAE,MAAM,GACjB,OAAO,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;IAyC9B;;;;OAIG;IACI,mBAAmB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,UAAU,CAAC;IAY9D;;;;OAIG;IACI,QAAQ,CAAC,IAAI,EAAE,UAAU,GAAG,MAAM;IASzC;;;;OAIG;IACI,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,UAAU,CAAC;CAYtD;AAED;;;;;GAKG;AACH,wBAAgB,2BAA2B,IAAI,MAAM,CAAC,qBAAqB,CAAC,CAE3E"}
1
+ {"version":3,"file":"browserCryptoProvider.d.ts","sourceRoot":"","sources":["../../../src/packlets/crypto-utils/browserCryptoProvider.ts"],"names":[],"mappings":"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"}
@@ -21,10 +21,9 @@
21
21
  Object.defineProperty(exports, "__esModule", { value: true });
22
22
  exports.BrowserCryptoProvider = void 0;
23
23
  exports.createBrowserCryptoProvider = createBrowserCryptoProvider;
24
- /* c8 ignore start - Browser-only implementation cannot be tested in Node.js environment */
25
24
  const ts_utils_1 = require("@fgv/ts-utils");
26
25
  const ts_extras_1 = require("@fgv/ts-extras");
27
- const CryptoConstants = ts_extras_1.CryptoUtils.Constants;
26
+ /* c8 ignore start - Used only by browser-only methods that cannot be tested in Node.js environment */
28
27
  /**
29
28
  * Extracts an `ArrayBuffer` from a Uint8Array, handling the potential SharedArrayBuffer case.
30
29
  * @param arr - The Uint8Array to extract from
@@ -36,6 +35,24 @@ function toArrayBuffer(arr) {
36
35
  new Uint8Array(buffer).set(arr);
37
36
  return buffer;
38
37
  }
38
+ /* c8 ignore stop */
39
+ /**
40
+ * Returns a fresh Uint8Array view over a non-shared ArrayBuffer copy of `arr`.
41
+ * Used by {@link BrowserCryptoProvider.wrapBytes | wrapBytes} and
42
+ * {@link BrowserCryptoProvider.unwrapBytes | unwrapBytes}: Node 20's
43
+ * webcrypto.subtle rejects raw `ArrayBuffer` for several `BufferSource`
44
+ * parameters with "is not instance of ArrayBuffer, Buffer, TypedArray, or
45
+ * DataView" even though `ArrayBuffer` should be valid per the spec; a
46
+ * TypedArray view is accepted on Node 20+ and on browsers, and the explicit
47
+ * `Uint8Array<ArrayBuffer>` return type also satisfies TypeScript's `BufferSource`
48
+ * (which excludes the `SharedArrayBuffer` branch of `Uint8Array`'s buffer type).
49
+ */
50
+ function toBufferView(arr) {
51
+ const buffer = new ArrayBuffer(arr.byteLength);
52
+ const view = new Uint8Array(buffer);
53
+ view.set(arr);
54
+ return view;
55
+ }
39
56
  /**
40
57
  * Browser implementation of `ICryptoProvider` using the Web Crypto API.
41
58
  * Uses AES-256-GCM for authenticated encryption.
@@ -46,6 +63,7 @@ function toArrayBuffer(arr) {
46
63
  * @public
47
64
  */
48
65
  class BrowserCryptoProvider {
66
+ /* c8 ignore start - Existing browser-only methods cannot be tested in Node.js environment */
49
67
  /**
50
68
  * Creates a new {@link CryptoUtils.BrowserCryptoProvider | BrowserCryptoProvider}.
51
69
  * @param cryptoApi - Optional Crypto instance (defaults to globalThis.crypto)
@@ -71,12 +89,12 @@ class BrowserCryptoProvider {
71
89
  * @returns `Success` with encryption result, or `Failure` with an error.
72
90
  */
73
91
  async encrypt(plaintext, key) {
74
- if (key.length !== CryptoConstants.AES_256_KEY_SIZE) {
75
- return ts_utils_1.Failure.with(`Key must be ${CryptoConstants.AES_256_KEY_SIZE} bytes, got ${key.length}`);
92
+ if (key.length !== ts_extras_1.CryptoUtils.Constants.AES_256_KEY_SIZE) {
93
+ return ts_utils_1.Failure.with(`Key must be ${ts_extras_1.CryptoUtils.Constants.AES_256_KEY_SIZE} bytes, got ${key.length}`);
76
94
  }
77
95
  try {
78
96
  // Generate random IV
79
- const iv = this._crypto.getRandomValues(new Uint8Array(CryptoConstants.GCM_IV_SIZE));
97
+ const iv = this._crypto.getRandomValues(new Uint8Array(ts_extras_1.CryptoUtils.Constants.GCM_IV_SIZE));
80
98
  // Import the key
81
99
  const cryptoKey = await this._crypto.subtle.importKey('raw', toArrayBuffer(key), { name: 'AES-GCM' }, false, ['encrypt']);
82
100
  // Encode plaintext to bytes
@@ -86,12 +104,12 @@ class BrowserCryptoProvider {
86
104
  const encryptedWithTag = await this._crypto.subtle.encrypt({
87
105
  name: 'AES-GCM',
88
106
  iv: iv,
89
- tagLength: CryptoConstants.GCM_AUTH_TAG_SIZE * 8 // bits
107
+ tagLength: ts_extras_1.CryptoUtils.Constants.GCM_AUTH_TAG_SIZE * 8 // bits
90
108
  }, cryptoKey, plaintextBytes);
91
109
  // Split ciphertext and auth tag (auth tag is last 16 bytes)
92
110
  const encryptedArray = new Uint8Array(encryptedWithTag);
93
- const encryptedData = encryptedArray.slice(0, encryptedArray.length - CryptoConstants.GCM_AUTH_TAG_SIZE);
94
- const authTag = encryptedArray.slice(encryptedArray.length - CryptoConstants.GCM_AUTH_TAG_SIZE);
111
+ const encryptedData = encryptedArray.slice(0, encryptedArray.length - ts_extras_1.CryptoUtils.Constants.GCM_AUTH_TAG_SIZE);
112
+ const authTag = encryptedArray.slice(encryptedArray.length - ts_extras_1.CryptoUtils.Constants.GCM_AUTH_TAG_SIZE);
95
113
  return ts_utils_1.Success.with({
96
114
  iv,
97
115
  authTag,
@@ -112,14 +130,14 @@ class BrowserCryptoProvider {
112
130
  * @returns `Success` with decrypted UTF-8 string, or `Failure` with an error.
113
131
  */
114
132
  async decrypt(encryptedData, key, iv, authTag) {
115
- if (key.length !== CryptoConstants.AES_256_KEY_SIZE) {
116
- return ts_utils_1.Failure.with(`Key must be ${CryptoConstants.AES_256_KEY_SIZE} bytes, got ${key.length}`);
133
+ if (key.length !== ts_extras_1.CryptoUtils.Constants.AES_256_KEY_SIZE) {
134
+ return ts_utils_1.Failure.with(`Key must be ${ts_extras_1.CryptoUtils.Constants.AES_256_KEY_SIZE} bytes, got ${key.length}`);
117
135
  }
118
- if (iv.length !== CryptoConstants.GCM_IV_SIZE) {
119
- return ts_utils_1.Failure.with(`IV must be ${CryptoConstants.GCM_IV_SIZE} bytes, got ${iv.length}`);
136
+ if (iv.length !== ts_extras_1.CryptoUtils.Constants.GCM_IV_SIZE) {
137
+ return ts_utils_1.Failure.with(`IV must be ${ts_extras_1.CryptoUtils.Constants.GCM_IV_SIZE} bytes, got ${iv.length}`);
120
138
  }
121
- if (authTag.length !== CryptoConstants.GCM_AUTH_TAG_SIZE) {
122
- return ts_utils_1.Failure.with(`Auth tag must be ${CryptoConstants.GCM_AUTH_TAG_SIZE} bytes, got ${authTag.length}`);
139
+ if (authTag.length !== ts_extras_1.CryptoUtils.Constants.GCM_AUTH_TAG_SIZE) {
140
+ return ts_utils_1.Failure.with(`Auth tag must be ${ts_extras_1.CryptoUtils.Constants.GCM_AUTH_TAG_SIZE} bytes, got ${authTag.length}`);
123
141
  }
124
142
  try {
125
143
  // Import the key
@@ -132,7 +150,7 @@ class BrowserCryptoProvider {
132
150
  const decrypted = await this._crypto.subtle.decrypt({
133
151
  name: 'AES-GCM',
134
152
  iv: toArrayBuffer(iv),
135
- tagLength: CryptoConstants.GCM_AUTH_TAG_SIZE * 8 // bits
153
+ tagLength: ts_extras_1.CryptoUtils.Constants.GCM_AUTH_TAG_SIZE * 8 // bits
136
154
  }, cryptoKey, encryptedWithTag);
137
155
  // Decode to string
138
156
  const decoder = new TextDecoder();
@@ -149,7 +167,7 @@ class BrowserCryptoProvider {
149
167
  */
150
168
  async generateKey() {
151
169
  try {
152
- return ts_utils_1.Success.with(this._crypto.getRandomValues(new Uint8Array(CryptoConstants.AES_256_KEY_SIZE)));
170
+ return ts_utils_1.Success.with(this._crypto.getRandomValues(new Uint8Array(ts_extras_1.CryptoUtils.Constants.AES_256_KEY_SIZE)));
153
171
  }
154
172
  catch (e) {
155
173
  const message = e instanceof Error ? e.message : String(e);
@@ -184,7 +202,7 @@ class BrowserCryptoProvider {
184
202
  salt: toArrayBuffer(salt),
185
203
  iterations: iterations,
186
204
  hash: 'SHA-256'
187
- }, keyMaterial, CryptoConstants.AES_256_KEY_SIZE * 8 // bits
205
+ }, keyMaterial, ts_extras_1.CryptoUtils.Constants.AES_256_KEY_SIZE * 8 // bits
188
206
  );
189
207
  return ts_utils_1.Success.with(new Uint8Array(derivedBits));
190
208
  }
@@ -193,6 +211,27 @@ class BrowserCryptoProvider {
193
211
  return ts_utils_1.Failure.with(`Key derivation failed: ${message}`);
194
212
  }
195
213
  }
214
+ /**
215
+ * Computes a SHA-256 hash of the given data.
216
+ * @param data - UTF-8 string to hash
217
+ * @returns `Success` with hex-encoded hash string, or `Failure` with an error.
218
+ */
219
+ async sha256(data) {
220
+ try {
221
+ const encoder = new TextEncoder();
222
+ const dataBuffer = encoder.encode(data);
223
+ const hashBuffer = await this._crypto.subtle.digest('SHA-256', dataBuffer);
224
+ const hashArray = new Uint8Array(hashBuffer);
225
+ const hashHex = Array.from(hashArray)
226
+ .map((b) => b.toString(16).padStart(2, '0'))
227
+ .join('');
228
+ return (0, ts_utils_1.succeed)(hashHex);
229
+ }
230
+ catch (e) {
231
+ const message = e instanceof Error ? e.message : String(e);
232
+ return (0, ts_utils_1.fail)(`SHA-256 hash failed: ${message}`);
233
+ }
234
+ }
196
235
  // ============================================================================
197
236
  // Platform Utility Methods
198
237
  // ============================================================================
@@ -213,6 +252,21 @@ class BrowserCryptoProvider {
213
252
  return ts_utils_1.Failure.with(`Random bytes generation failed: ${message}`);
214
253
  }
215
254
  }
255
+ /* c8 ignore stop */
256
+ /**
257
+ * Generates a cryptographically random UUIDv4 using the injected
258
+ * `Crypto` instance.
259
+ * @returns `Success` with the generated UUID, or `Failure` if the underlying
260
+ * `Crypto` instance does not expose `randomUUID`.
261
+ */
262
+ generateUuid() {
263
+ /* c8 ignore next 3 - randomUUID is always available in supported runtimes (Node 22+, modern browsers) */
264
+ if (typeof this._crypto.randomUUID !== 'function') {
265
+ return ts_utils_1.Failure.with('Crypto instance does not expose randomUUID');
266
+ }
267
+ return (0, ts_utils_1.captureResult)(() => this._crypto.randomUUID());
268
+ }
269
+ /* c8 ignore start - browser-only methods continue */
216
270
  /**
217
271
  * Encodes binary data to base64 string.
218
272
  * @param data - Binary data to encode
@@ -244,8 +298,266 @@ class BrowserCryptoProvider {
244
298
  return ts_utils_1.Failure.with('Invalid base64 string');
245
299
  }
246
300
  }
301
+ // ============================================================================
302
+ // Asymmetric Key Operations
303
+ // ============================================================================
304
+ /**
305
+ * Generates a new asymmetric keypair via Web Crypto.
306
+ * @param algorithm - The algorithm to use.
307
+ * @param extractable - Whether the resulting keys may be exported.
308
+ * @returns `Success` with the generated `CryptoKeyPair`, or `Failure` with an error.
309
+ */
310
+ async generateKeyPair(algorithm, extractable) {
311
+ const params = ts_extras_1.CryptoUtils.keyPairAlgorithmParams[algorithm];
312
+ // Widening upcast to `AlgorithmIdentifier` steers TS to subtle.generateKey's
313
+ // broad overload, which accepts the Ed25519 `{ name: 'Ed25519' }` shape and
314
+ // returns `CryptoKey | CryptoKeyPair`. The narrowing back to `CryptoKeyPair`
315
+ // is a runtime check via the `in` operator, not a type assertion.
316
+ const result = await (0, ts_utils_1.captureAsyncResult)(async () => {
317
+ const generated = await this._crypto.subtle.generateKey(params.generateKey, extractable, [...params.keyPairUsages]);
318
+ if ('privateKey' in generated && 'publicKey' in generated) {
319
+ return generated;
320
+ }
321
+ /* c8 ignore next - unreachable: every entry in keyPairAlgorithmParams produces a keypair */
322
+ throw new Error(`${algorithm} unexpectedly produced a single CryptoKey`);
323
+ });
324
+ return result.withErrorFormat((e) => `Failed to generate ${algorithm} keypair: ${e}`);
325
+ }
326
+ /**
327
+ * Exports a public `CryptoKey` as a JSON Web Key.
328
+ * @remarks
329
+ * Rejects non-public keys at runtime. WebCrypto's `exportKey('jwk', ...)`
330
+ * does not enforce public-vs-private; without this guard a caller that
331
+ * passed an extractable private key would receive its private fields
332
+ * (`d`, `p`, `q`, ...) as JWK, defeating the method's name.
333
+ * @param publicKey - Extractable public key to export.
334
+ * @returns `Success` with the JWK, or `Failure` if not a public key or if export fails.
335
+ */
336
+ async exportPublicKeyJwk(publicKey) {
337
+ if (publicKey.type !== 'public') {
338
+ return ts_utils_1.Failure.with(`exportPublicKeyJwk requires a public CryptoKey, got '${publicKey.type}'`);
339
+ }
340
+ const result = await (0, ts_utils_1.captureAsyncResult)(() => this._crypto.subtle.exportKey('jwk', publicKey));
341
+ return result.withErrorFormat((e) => `Failed to export public key as JWK: ${e}`);
342
+ }
343
+ /**
344
+ * Imports a public-key JWK as a `CryptoKey` for the requested algorithm.
345
+ * @param jwk - The JSON Web Key produced by a prior export.
346
+ * @param algorithm - The algorithm the key was generated for.
347
+ * @returns `Success` with the imported public `CryptoKey`, or `Failure` with an error.
348
+ */
349
+ async importPublicKeyJwk(jwk, algorithm) {
350
+ const params = ts_extras_1.CryptoUtils.keyPairAlgorithmParams[algorithm];
351
+ const result = await (0, ts_utils_1.captureAsyncResult)(() => this._crypto.subtle.importKey('jwk', jwk, params.importPublicKey, true, params.publicKeyUsages));
352
+ return result.withErrorFormat((e) => `Failed to import ${algorithm} public key from JWK: ${e}`);
353
+ }
354
+ /**
355
+ * Exports a public `CryptoKey` as a DER-encoded SPKI blob.
356
+ * @param publicKey - The public `CryptoKey` to export.
357
+ * @returns `Success` with the raw SPKI bytes, or `Failure` with error context.
358
+ */
359
+ async exportPublicKeySpki(publicKey) {
360
+ if (publicKey.type !== 'public') {
361
+ return ts_utils_1.Failure.with(`exportPublicKeySpki requires a public CryptoKey, got '${publicKey.type}'`);
362
+ }
363
+ const result = await (0, ts_utils_1.captureAsyncResult)(() => this._crypto.subtle.exportKey('spki', publicKey));
364
+ return result
365
+ .withErrorFormat((e) => `exportPublicKeySpki: failed to export key: ${e}`)
366
+ .onSuccess((buf) => (0, ts_utils_1.succeed)(new Uint8Array(buf)));
367
+ }
368
+ /**
369
+ * Imports a public key from a DER-encoded SPKI blob.
370
+ * @param spkiBytes - The raw SPKI bytes.
371
+ * @param algorithm - The algorithm the key was generated for.
372
+ * @returns `Success` with the imported public `CryptoKey`, or `Failure` with error context.
373
+ */
374
+ async importPublicKeySpki(spkiBytes, algorithm) {
375
+ const params = ts_extras_1.CryptoUtils.keyPairAlgorithmParams[algorithm];
376
+ const result = await (0, ts_utils_1.captureAsyncResult)(() => this._crypto.subtle.importKey('spki', toBufferView(spkiBytes), params.importPublicKey, true, [...params.publicKeyUsages]));
377
+ return result.withErrorFormat((e) => `importPublicKeySpki: failed to import ${algorithm} public key from SPKI: ${e}`);
378
+ }
379
+ /* c8 ignore stop */
380
+ /**
381
+ * Signs `data` with `privateKey` using the algorithm inferred from the key.
382
+ * @param privateKey - A signing `CryptoKey` (`'ecdsa-p256'` or `'ed25519'`).
383
+ * @param data - The bytes to sign.
384
+ * @returns `Success` with the raw signature bytes, or `Failure` with error context.
385
+ */
386
+ async sign(privateKey, data) {
387
+ const algorithm = signAlgorithmFromKey(privateKey);
388
+ const result = await (0, ts_utils_1.captureAsyncResult)(() => this._crypto.subtle.sign(algorithm, privateKey, toBufferView(data)));
389
+ return result
390
+ .withErrorFormat((e) => `sign failed: ${e}`)
391
+ .onSuccess((buf) => (0, ts_utils_1.succeed)(new Uint8Array(buf)));
392
+ }
393
+ /**
394
+ * Verifies a signature produced by {@link BrowserCryptoProvider.sign}.
395
+ * @param publicKey - A verify `CryptoKey` (`'ecdsa-p256'` or `'ed25519'`).
396
+ * @param signature - The raw signature bytes.
397
+ * @param data - The original data that was signed.
398
+ * @returns `Success` with `true` if valid, `false` if not, or `Failure` with error context.
399
+ */
400
+ async verify(publicKey, signature, data) {
401
+ const algorithm = signAlgorithmFromKey(publicKey);
402
+ const result = await (0, ts_utils_1.captureAsyncResult)(() => this._crypto.subtle.verify(algorithm, publicKey, toBufferView(signature), toBufferView(data)));
403
+ return result.withErrorFormat((e) => `verify failed: ${e}`);
404
+ }
405
+ /**
406
+ * Compares two byte arrays in constant time.
407
+ *
408
+ * Accumulates XOR differences with bitwise-OR; no early-return is possible
409
+ * once the length check passes, making timing independent of the byte
410
+ * values. Returns `false` immediately on length mismatch (length is not
411
+ * secret in normal use).
412
+ * @param a - First byte array.
413
+ * @param b - Second byte array.
414
+ * @returns `true` if lengths match and all bytes are equal, `false` otherwise.
415
+ */
416
+ timingSafeEqual(a, b) {
417
+ if (a.length !== b.length)
418
+ return false;
419
+ let diff = 0;
420
+ for (let i = 0; i < a.length; i++) {
421
+ // eslint-disable-next-line no-bitwise
422
+ diff |= a[i] ^ b[i];
423
+ }
424
+ return diff === 0;
425
+ }
426
+ /**
427
+ * Computes an HMAC-SHA256 MAC for `data` using `key`.
428
+ * @param key - An HMAC `CryptoKey` with `'sign'` usage.
429
+ * @param data - The bytes to authenticate.
430
+ * @returns `Success` with the 32-byte MAC, or `Failure` with error context.
431
+ */
432
+ async hmacSha256(key, data) {
433
+ const result = await (0, ts_utils_1.captureAsyncResult)(() => this._crypto.subtle.sign({ name: 'HMAC' }, key, toBufferView(data)));
434
+ return result
435
+ .withErrorFormat((e) => `hmacSha256 failed: ${e}`)
436
+ .onSuccess((buf) => (0, ts_utils_1.succeed)(new Uint8Array(buf)));
437
+ }
438
+ /**
439
+ * Verifies an HMAC-SHA256 MAC in constant time.
440
+ * @param key - An HMAC `CryptoKey` with `'sign'` usage.
441
+ * @param signature - The MAC bytes to verify.
442
+ * @param data - The original data that was authenticated.
443
+ * @returns `Success` with `true` if valid, `false` if not, or `Failure` with error context.
444
+ */
445
+ async verifyHmacSha256(key, signature, data) {
446
+ return (await this.hmacSha256(key, data))
447
+ .withErrorFormat((e) => `verifyHmacSha256 failed: ${e}`)
448
+ .onSuccess((mac) => (0, ts_utils_1.succeed)(this.timingSafeEqual(mac, signature)));
449
+ }
450
+ /**
451
+ * Wraps `plaintext` for the holder of `recipientPublicKey` using
452
+ * ECIES (ECDH P-256 + HKDF-SHA256 + AES-GCM-256). See
453
+ * {@link CryptoUtils.ICryptoProvider.wrapBytes | ICryptoProvider.wrapBytes}.
454
+ * @param plaintext - The bytes to wrap.
455
+ * @param recipientPublicKey - The recipient's ECDH P-256 public `CryptoKey`.
456
+ * @param options - HKDF salt and info; see {@link CryptoUtils.IWrapBytesOptions | IWrapBytesOptions}.
457
+ * @returns `Success` with the wrapped payload, or `Failure` with an error.
458
+ */
459
+ async wrapBytes(plaintext, recipientPublicKey, options) {
460
+ const recipientCheck = checkEcdhP256(recipientPublicKey, 'public', 'recipient public key');
461
+ if (recipientCheck.isFailure()) {
462
+ return ts_utils_1.Failure.with(`wrapBytes failed: ${recipientCheck.message}`);
463
+ }
464
+ const subtle = this._crypto.subtle;
465
+ const result = await (0, ts_utils_1.captureAsyncResult)(async () => {
466
+ const ephemeral = (await subtle.generateKey({ name: 'ECDH', namedCurve: 'P-256' }, true, [
467
+ 'deriveKey'
468
+ ]));
469
+ const hkdfBase = await subtle.deriveKey({ name: 'ECDH', public: recipientPublicKey }, ephemeral.privateKey, { name: 'HKDF' }, false, ['deriveKey']);
470
+ const wrapKey = await subtle.deriveKey({ name: 'HKDF', salt: toBufferView(options.salt), info: toBufferView(options.info), hash: 'SHA-256' }, hkdfBase, { name: 'AES-GCM', length: 256 }, false, ['encrypt']);
471
+ const nonce = this._crypto.getRandomValues(new Uint8Array(ts_extras_1.CryptoUtils.Constants.GCM_IV_SIZE));
472
+ const ctBuf = await subtle.encrypt({ name: 'AES-GCM', iv: nonce }, wrapKey, toBufferView(plaintext));
473
+ const ephemeralPublicKey = await subtle.exportKey('jwk', ephemeral.publicKey);
474
+ return {
475
+ ephemeralPublicKey,
476
+ nonce: this.toBase64(nonce),
477
+ ciphertext: this.toBase64(new Uint8Array(ctBuf))
478
+ };
479
+ });
480
+ return result.withErrorFormat((e) => `wrapBytes failed: ${e}`);
481
+ }
482
+ /**
483
+ * Unwraps a payload produced by `wrapBytes` using the recipient's private
484
+ * key. See {@link CryptoUtils.ICryptoProvider.unwrapBytes | ICryptoProvider.unwrapBytes}.
485
+ * @param wrapped - The wrapped payload.
486
+ * @param recipientPrivateKey - The recipient's ECDH P-256 private `CryptoKey`.
487
+ * @param options - HKDF salt and info matching the wrap call.
488
+ * @returns `Success` with the original `plaintext`, or `Failure` with an error.
489
+ */
490
+ async unwrapBytes(wrapped, recipientPrivateKey, options) {
491
+ const recipientCheck = checkEcdhP256(recipientPrivateKey, 'private', 'recipient private key');
492
+ if (recipientCheck.isFailure()) {
493
+ return ts_utils_1.Failure.with(`unwrapBytes failed: ${recipientCheck.message}`);
494
+ }
495
+ const nonceResult = this.fromBase64(wrapped.nonce);
496
+ if (nonceResult.isFailure()) {
497
+ return ts_utils_1.Failure.with(`unwrapBytes failed: nonce: ${nonceResult.message}`);
498
+ }
499
+ if (nonceResult.value.length !== ts_extras_1.CryptoUtils.Constants.GCM_IV_SIZE) {
500
+ return ts_utils_1.Failure.with(`unwrapBytes failed: nonce must be ${ts_extras_1.CryptoUtils.Constants.GCM_IV_SIZE} bytes (got ${nonceResult.value.length})`);
501
+ }
502
+ const ciphertextResult = this.fromBase64(wrapped.ciphertext);
503
+ if (ciphertextResult.isFailure()) {
504
+ return ts_utils_1.Failure.with(`unwrapBytes failed: ciphertext: ${ciphertextResult.message}`);
505
+ }
506
+ if (ciphertextResult.value.length < ts_extras_1.CryptoUtils.Constants.GCM_AUTH_TAG_SIZE) {
507
+ return ts_utils_1.Failure.with(`unwrapBytes failed: ciphertext must be at least ${ts_extras_1.CryptoUtils.Constants.GCM_AUTH_TAG_SIZE} bytes (got ${ciphertextResult.value.length})`);
508
+ }
509
+ const subtle = this._crypto.subtle;
510
+ const result = await (0, ts_utils_1.captureAsyncResult)(async () => {
511
+ const ephemeralPub = await subtle.importKey('jwk', wrapped.ephemeralPublicKey, { name: 'ECDH', namedCurve: 'P-256' }, false, []);
512
+ const hkdfBase = await subtle.deriveKey({ name: 'ECDH', public: ephemeralPub }, recipientPrivateKey, { name: 'HKDF' }, false, ['deriveKey']);
513
+ const wrapKey = await subtle.deriveKey({ name: 'HKDF', salt: toBufferView(options.salt), info: toBufferView(options.info), hash: 'SHA-256' }, hkdfBase, { name: 'AES-GCM', length: 256 }, false, ['decrypt']);
514
+ const ptBuf = await subtle.decrypt({ name: 'AES-GCM', iv: toBufferView(nonceResult.value) }, wrapKey, toBufferView(ciphertextResult.value));
515
+ return new Uint8Array(ptBuf);
516
+ });
517
+ return result.withErrorFormat((e) => `unwrapBytes failed: ${e}`);
518
+ }
247
519
  }
248
520
  exports.BrowserCryptoProvider = BrowserCryptoProvider;
521
+ /**
522
+ * Derives the algorithm identifier needed by `crypto.subtle.sign/verify`
523
+ * from the key's embedded `algorithm` property. ECDSA requires an explicit
524
+ * `hash` parameter that is not stored on the key object itself; all other
525
+ * supported signing algorithms (`Ed25519`) use the key algorithm as-is.
526
+ */
527
+ function signAlgorithmFromKey(key) {
528
+ if (key.algorithm.name === 'ECDSA') {
529
+ return { name: 'ECDSA', hash: 'SHA-256' };
530
+ }
531
+ return key.algorithm;
532
+ }
533
+ /**
534
+ * Verifies that `key` is an ECDH P-256 `CryptoKey` of the expected `keyType`
535
+ * (public or private). Used by the wrap/unwrap methods to surface a clean
536
+ * `Failure` instead of letting the WebCrypto deriveKey call throw a less
537
+ * informative error later in the pipeline. Key usages are intentionally not
538
+ * checked here: WebCrypto already produces a specific error if `deriveKey` is
539
+ * not in `usages`, and `deriveBits` is an equally valid alternative usage that
540
+ * an explicit check would have to track.
541
+ * @param key - The CryptoKey to validate.
542
+ * @param keyType - The required `key.type` ('public' for wrap, 'private' for unwrap).
543
+ * @param label - Human-readable role label included in the failure message.
544
+ * @returns `Success` with the key (unchanged) when the algorithm, curve, and
545
+ * type all match; otherwise `Failure` with `<label> must be ECDH P-256 (...)`.
546
+ */
547
+ function checkEcdhP256(key, keyType, label) {
548
+ if (key.algorithm.name !== 'ECDH') {
549
+ return ts_utils_1.Failure.with(`${label} must be ECDH P-256 (got algorithm '${key.algorithm.name}')`);
550
+ }
551
+ const namedCurve = key.algorithm.namedCurve;
552
+ if (namedCurve !== 'P-256') {
553
+ return ts_utils_1.Failure.with(`${label} must be ECDH P-256 (got curve '${namedCurve}')`);
554
+ }
555
+ if (key.type !== keyType) {
556
+ return ts_utils_1.Failure.with(`${label} must be a ${keyType} CryptoKey (got '${key.type}')`);
557
+ }
558
+ return (0, ts_utils_1.succeed)(key);
559
+ }
560
+ /* c8 ignore start - Constructs a provider; only meaningful in a real browser environment */
249
561
  /**
250
562
  * Creates a {@link CryptoUtils.BrowserCryptoProvider | BrowserCryptoProvider} if Web
251
563
  * Crypto API is available.