@enbox/crypto 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (283) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +708 -0
  3. package/dist/browser.js +64 -0
  4. package/dist/browser.js.map +7 -0
  5. package/dist/browser.mjs +64 -0
  6. package/dist/browser.mjs.map +7 -0
  7. package/dist/cjs/algorithms/aes-ctr.js +188 -0
  8. package/dist/cjs/algorithms/aes-ctr.js.map +1 -0
  9. package/dist/cjs/algorithms/aes-gcm.js +196 -0
  10. package/dist/cjs/algorithms/aes-gcm.js.map +1 -0
  11. package/dist/cjs/algorithms/crypto-algorithm.js +13 -0
  12. package/dist/cjs/algorithms/crypto-algorithm.js.map +1 -0
  13. package/dist/cjs/algorithms/ecdsa.js +352 -0
  14. package/dist/cjs/algorithms/ecdsa.js.map +1 -0
  15. package/dist/cjs/algorithms/eddsa.js +325 -0
  16. package/dist/cjs/algorithms/eddsa.js.map +1 -0
  17. package/dist/cjs/algorithms/sha-2.js +119 -0
  18. package/dist/cjs/algorithms/sha-2.js.map +1 -0
  19. package/dist/cjs/index.js +41 -0
  20. package/dist/cjs/index.js.map +1 -0
  21. package/dist/cjs/jose/jwe.js +3 -0
  22. package/dist/cjs/jose/jwe.js.map +1 -0
  23. package/dist/cjs/jose/jwk.js +278 -0
  24. package/dist/cjs/jose/jwk.js.map +1 -0
  25. package/dist/cjs/jose/jws.js +3 -0
  26. package/dist/cjs/jose/jws.js.map +1 -0
  27. package/dist/cjs/jose/jwt.js +3 -0
  28. package/dist/cjs/jose/jwt.js.map +1 -0
  29. package/dist/cjs/jose/utils.js +60 -0
  30. package/dist/cjs/jose/utils.js.map +1 -0
  31. package/dist/cjs/local-key-manager.js +521 -0
  32. package/dist/cjs/local-key-manager.js.map +1 -0
  33. package/dist/cjs/package.json +1 -0
  34. package/dist/cjs/primitives/aes-ctr.js +398 -0
  35. package/dist/cjs/primitives/aes-ctr.js.map +1 -0
  36. package/dist/cjs/primitives/aes-gcm.js +425 -0
  37. package/dist/cjs/primitives/aes-gcm.js.map +1 -0
  38. package/dist/cjs/primitives/concat-kdf.js +215 -0
  39. package/dist/cjs/primitives/concat-kdf.js.map +1 -0
  40. package/dist/cjs/primitives/ed25519.js +651 -0
  41. package/dist/cjs/primitives/ed25519.js.map +1 -0
  42. package/dist/cjs/primitives/pbkdf2.js +120 -0
  43. package/dist/cjs/primitives/pbkdf2.js.map +1 -0
  44. package/dist/cjs/primitives/secp256k1.js +958 -0
  45. package/dist/cjs/primitives/secp256k1.js.map +1 -0
  46. package/dist/cjs/primitives/secp256r1.js +959 -0
  47. package/dist/cjs/primitives/secp256r1.js.map +1 -0
  48. package/dist/cjs/primitives/sha256.js +93 -0
  49. package/dist/cjs/primitives/sha256.js.map +1 -0
  50. package/dist/cjs/primitives/x25519.js +498 -0
  51. package/dist/cjs/primitives/x25519.js.map +1 -0
  52. package/dist/cjs/primitives/xchacha20-poly1305.js +340 -0
  53. package/dist/cjs/primitives/xchacha20-poly1305.js.map +1 -0
  54. package/dist/cjs/primitives/xchacha20.js +316 -0
  55. package/dist/cjs/primitives/xchacha20.js.map +1 -0
  56. package/dist/cjs/types/cipher.js +3 -0
  57. package/dist/cjs/types/cipher.js.map +1 -0
  58. package/dist/cjs/types/crypto-api.js +3 -0
  59. package/dist/cjs/types/crypto-api.js.map +1 -0
  60. package/dist/cjs/types/hasher.js +3 -0
  61. package/dist/cjs/types/hasher.js.map +1 -0
  62. package/dist/cjs/types/identifier.js +3 -0
  63. package/dist/cjs/types/identifier.js.map +1 -0
  64. package/dist/cjs/types/key-compressor.js +3 -0
  65. package/dist/cjs/types/key-compressor.js.map +1 -0
  66. package/dist/cjs/types/key-converter.js +3 -0
  67. package/dist/cjs/types/key-converter.js.map +1 -0
  68. package/dist/cjs/types/key-deriver.js +3 -0
  69. package/dist/cjs/types/key-deriver.js.map +1 -0
  70. package/dist/cjs/types/key-generator.js +3 -0
  71. package/dist/cjs/types/key-generator.js.map +1 -0
  72. package/dist/cjs/types/key-io.js +3 -0
  73. package/dist/cjs/types/key-io.js.map +1 -0
  74. package/dist/cjs/types/key-wrapper.js +3 -0
  75. package/dist/cjs/types/key-wrapper.js.map +1 -0
  76. package/dist/cjs/types/params-direct.js +3 -0
  77. package/dist/cjs/types/params-direct.js.map +1 -0
  78. package/dist/cjs/types/params-enclosed.js +3 -0
  79. package/dist/cjs/types/params-enclosed.js.map +1 -0
  80. package/dist/cjs/types/params-kms.js +3 -0
  81. package/dist/cjs/types/params-kms.js.map +1 -0
  82. package/dist/cjs/types/signer.js +3 -0
  83. package/dist/cjs/types/signer.js.map +1 -0
  84. package/dist/cjs/utils.js +173 -0
  85. package/dist/cjs/utils.js.map +1 -0
  86. package/dist/esm/algorithms/aes-ctr.js +124 -0
  87. package/dist/esm/algorithms/aes-ctr.js.map +1 -0
  88. package/dist/esm/algorithms/aes-gcm.js +132 -0
  89. package/dist/esm/algorithms/aes-gcm.js.map +1 -0
  90. package/dist/esm/algorithms/crypto-algorithm.js +6 -0
  91. package/dist/esm/algorithms/crypto-algorithm.js.map +1 -0
  92. package/dist/esm/algorithms/ecdsa.js +237 -0
  93. package/dist/esm/algorithms/ecdsa.js.map +1 -0
  94. package/dist/esm/algorithms/eddsa.js +213 -0
  95. package/dist/esm/algorithms/eddsa.js.map +1 -0
  96. package/dist/esm/algorithms/sha-2.js +57 -0
  97. package/dist/esm/algorithms/sha-2.js.map +1 -0
  98. package/dist/esm/index.js +25 -0
  99. package/dist/esm/index.js.map +1 -0
  100. package/dist/esm/jose/jwe.js +2 -0
  101. package/dist/esm/jose/jwe.js.map +1 -0
  102. package/dist/esm/jose/jwk.js +241 -0
  103. package/dist/esm/jose/jwk.js.map +1 -0
  104. package/dist/esm/jose/jws.js +2 -0
  105. package/dist/esm/jose/jws.js.map +1 -0
  106. package/dist/esm/jose/jwt.js +2 -0
  107. package/dist/esm/jose/jwt.js.map +1 -0
  108. package/dist/esm/jose/utils.js +34 -0
  109. package/dist/esm/jose/utils.js.map +1 -0
  110. package/dist/esm/local-key-manager.js +417 -0
  111. package/dist/esm/local-key-manager.js.map +1 -0
  112. package/dist/esm/primitives/aes-ctr.js +327 -0
  113. package/dist/esm/primitives/aes-ctr.js.map +1 -0
  114. package/dist/esm/primitives/aes-gcm.js +347 -0
  115. package/dist/esm/primitives/aes-gcm.js.map +1 -0
  116. package/dist/esm/primitives/concat-kdf.js +185 -0
  117. package/dist/esm/primitives/concat-kdf.js.map +1 -0
  118. package/dist/esm/primitives/ed25519.js +521 -0
  119. package/dist/esm/primitives/ed25519.js.map +1 -0
  120. package/dist/esm/primitives/pbkdf2.js +78 -0
  121. package/dist/esm/primitives/pbkdf2.js.map +1 -0
  122. package/dist/esm/primitives/secp256k1.js +805 -0
  123. package/dist/esm/primitives/secp256k1.js.map +1 -0
  124. package/dist/esm/primitives/secp256r1.js +806 -0
  125. package/dist/esm/primitives/secp256r1.js.map +1 -0
  126. package/dist/esm/primitives/sha256.js +55 -0
  127. package/dist/esm/primitives/sha256.js.map +1 -0
  128. package/dist/esm/primitives/x25519.js +392 -0
  129. package/dist/esm/primitives/x25519.js.map +1 -0
  130. package/dist/esm/primitives/xchacha20-poly1305.js +270 -0
  131. package/dist/esm/primitives/xchacha20-poly1305.js.map +1 -0
  132. package/dist/esm/primitives/xchacha20.js +246 -0
  133. package/dist/esm/primitives/xchacha20.js.map +1 -0
  134. package/dist/esm/types/cipher.js +2 -0
  135. package/dist/esm/types/cipher.js.map +1 -0
  136. package/dist/esm/types/crypto-api.js +2 -0
  137. package/dist/esm/types/crypto-api.js.map +1 -0
  138. package/dist/esm/types/hasher.js +2 -0
  139. package/dist/esm/types/hasher.js.map +1 -0
  140. package/dist/esm/types/identifier.js +2 -0
  141. package/dist/esm/types/identifier.js.map +1 -0
  142. package/dist/esm/types/key-compressor.js +2 -0
  143. package/dist/esm/types/key-compressor.js.map +1 -0
  144. package/dist/esm/types/key-converter.js +2 -0
  145. package/dist/esm/types/key-converter.js.map +1 -0
  146. package/dist/esm/types/key-deriver.js +2 -0
  147. package/dist/esm/types/key-deriver.js.map +1 -0
  148. package/dist/esm/types/key-generator.js +2 -0
  149. package/dist/esm/types/key-generator.js.map +1 -0
  150. package/dist/esm/types/key-io.js +2 -0
  151. package/dist/esm/types/key-io.js.map +1 -0
  152. package/dist/esm/types/key-wrapper.js +2 -0
  153. package/dist/esm/types/key-wrapper.js.map +1 -0
  154. package/dist/esm/types/params-direct.js +2 -0
  155. package/dist/esm/types/params-direct.js.map +1 -0
  156. package/dist/esm/types/params-enclosed.js +2 -0
  157. package/dist/esm/types/params-enclosed.js.map +1 -0
  158. package/dist/esm/types/params-kms.js +2 -0
  159. package/dist/esm/types/params-kms.js.map +1 -0
  160. package/dist/esm/types/signer.js +2 -0
  161. package/dist/esm/types/signer.js.map +1 -0
  162. package/dist/esm/utils.js +165 -0
  163. package/dist/esm/utils.js.map +1 -0
  164. package/dist/types/algorithms/aes-ctr.d.ts +121 -0
  165. package/dist/types/algorithms/aes-ctr.d.ts.map +1 -0
  166. package/dist/types/algorithms/aes-gcm.d.ts +152 -0
  167. package/dist/types/algorithms/aes-gcm.d.ts.map +1 -0
  168. package/dist/types/algorithms/crypto-algorithm.d.ts +6 -0
  169. package/dist/types/algorithms/crypto-algorithm.d.ts.map +1 -0
  170. package/dist/types/algorithms/ecdsa.d.ts +154 -0
  171. package/dist/types/algorithms/ecdsa.d.ts.map +1 -0
  172. package/dist/types/algorithms/eddsa.d.ts +151 -0
  173. package/dist/types/algorithms/eddsa.d.ts.map +1 -0
  174. package/dist/types/algorithms/sha-2.d.ts +51 -0
  175. package/dist/types/algorithms/sha-2.d.ts.map +1 -0
  176. package/dist/types/index.d.ts +39 -0
  177. package/dist/types/index.d.ts.map +1 -0
  178. package/dist/types/jose/jwe.d.ts +135 -0
  179. package/dist/types/jose/jwe.d.ts.map +1 -0
  180. package/dist/types/jose/jwk.d.ts +439 -0
  181. package/dist/types/jose/jwk.d.ts.map +1 -0
  182. package/dist/types/jose/jws.d.ts +67 -0
  183. package/dist/types/jose/jws.d.ts.map +1 -0
  184. package/dist/types/jose/jwt.d.ts +139 -0
  185. package/dist/types/jose/jwt.d.ts.map +1 -0
  186. package/dist/types/jose/utils.d.ts +14 -0
  187. package/dist/types/jose/utils.d.ts.map +1 -0
  188. package/dist/types/local-key-manager.d.ts +307 -0
  189. package/dist/types/local-key-manager.d.ts.map +1 -0
  190. package/dist/types/primitives/aes-ctr.d.ts +219 -0
  191. package/dist/types/primitives/aes-ctr.d.ts.map +1 -0
  192. package/dist/types/primitives/aes-gcm.d.ts +245 -0
  193. package/dist/types/primitives/aes-gcm.d.ts.map +1 -0
  194. package/dist/types/primitives/concat-kdf.d.ts +160 -0
  195. package/dist/types/primitives/concat-kdf.d.ts.map +1 -0
  196. package/dist/types/primitives/ed25519.d.ts +359 -0
  197. package/dist/types/primitives/ed25519.d.ts.map +1 -0
  198. package/dist/types/primitives/pbkdf2.d.ts +94 -0
  199. package/dist/types/primitives/pbkdf2.d.ts.map +1 -0
  200. package/dist/types/primitives/secp256k1.d.ts +598 -0
  201. package/dist/types/primitives/secp256k1.d.ts.map +1 -0
  202. package/dist/types/primitives/secp256r1.d.ts +599 -0
  203. package/dist/types/primitives/secp256r1.d.ts.map +1 -0
  204. package/dist/types/primitives/sha256.d.ts +42 -0
  205. package/dist/types/primitives/sha256.d.ts.map +1 -0
  206. package/dist/types/primitives/x25519.d.ts +283 -0
  207. package/dist/types/primitives/x25519.d.ts.map +1 -0
  208. package/dist/types/primitives/xchacha20-poly1305.d.ts +210 -0
  209. package/dist/types/primitives/xchacha20-poly1305.d.ts.map +1 -0
  210. package/dist/types/primitives/xchacha20.d.ts +186 -0
  211. package/dist/types/primitives/xchacha20.d.ts.map +1 -0
  212. package/dist/types/types/cipher.d.ts +49 -0
  213. package/dist/types/types/cipher.d.ts.map +1 -0
  214. package/dist/types/types/crypto-api.d.ts +40 -0
  215. package/dist/types/types/crypto-api.d.ts.map +1 -0
  216. package/dist/types/types/hasher.d.ts +33 -0
  217. package/dist/types/types/hasher.d.ts.map +1 -0
  218. package/dist/types/types/identifier.d.ts +16 -0
  219. package/dist/types/types/identifier.d.ts.map +1 -0
  220. package/dist/types/types/key-compressor.d.ts +28 -0
  221. package/dist/types/types/key-compressor.d.ts.map +1 -0
  222. package/dist/types/types/key-converter.d.ts +57 -0
  223. package/dist/types/types/key-converter.d.ts.map +1 -0
  224. package/dist/types/types/key-deriver.d.ts +39 -0
  225. package/dist/types/types/key-deriver.d.ts.map +1 -0
  226. package/dist/types/types/key-generator.d.ts +105 -0
  227. package/dist/types/types/key-generator.d.ts.map +1 -0
  228. package/dist/types/types/key-io.d.ts +37 -0
  229. package/dist/types/types/key-io.d.ts.map +1 -0
  230. package/dist/types/types/key-wrapper.d.ts +38 -0
  231. package/dist/types/types/key-wrapper.d.ts.map +1 -0
  232. package/dist/types/types/params-direct.d.ts +90 -0
  233. package/dist/types/types/params-direct.d.ts.map +1 -0
  234. package/dist/types/types/params-enclosed.d.ts +47 -0
  235. package/dist/types/types/params-enclosed.d.ts.map +1 -0
  236. package/dist/types/types/params-kms.d.ts +131 -0
  237. package/dist/types/types/params-kms.d.ts.map +1 -0
  238. package/dist/types/types/signer.d.ts +46 -0
  239. package/dist/types/types/signer.d.ts.map +1 -0
  240. package/dist/types/utils.d.ts +112 -0
  241. package/dist/types/utils.d.ts.map +1 -0
  242. package/dist/utils.js +7 -0
  243. package/dist/utils.js.map +7 -0
  244. package/package.json +103 -0
  245. package/src/algorithms/aes-ctr.ts +156 -0
  246. package/src/algorithms/aes-gcm.ts +187 -0
  247. package/src/algorithms/crypto-algorithm.ts +4 -0
  248. package/src/algorithms/ecdsa.ts +269 -0
  249. package/src/algorithms/eddsa.ts +243 -0
  250. package/src/algorithms/sha-2.ts +65 -0
  251. package/src/index.ts +42 -0
  252. package/src/jose/jwe.ts +196 -0
  253. package/src/jose/jwk.ts +632 -0
  254. package/src/jose/jws.ts +95 -0
  255. package/src/jose/jwt.ts +147 -0
  256. package/src/jose/utils.ts +34 -0
  257. package/src/local-key-manager.ts +540 -0
  258. package/src/primitives/aes-ctr.ts +352 -0
  259. package/src/primitives/aes-gcm.ts +378 -0
  260. package/src/primitives/concat-kdf.ts +240 -0
  261. package/src/primitives/ed25519.ts +548 -0
  262. package/src/primitives/pbkdf2.ts +122 -0
  263. package/src/primitives/secp256k1.ts +848 -0
  264. package/src/primitives/secp256r1.ts +850 -0
  265. package/src/primitives/sha256.ts +47 -0
  266. package/src/primitives/x25519.ts +403 -0
  267. package/src/primitives/xchacha20-poly1305.ts +274 -0
  268. package/src/primitives/xchacha20.ts +247 -0
  269. package/src/types/cipher.ts +53 -0
  270. package/src/types/crypto-api.ts +56 -0
  271. package/src/types/hasher.ts +32 -0
  272. package/src/types/identifier.ts +16 -0
  273. package/src/types/key-compressor.ts +25 -0
  274. package/src/types/key-converter.ts +53 -0
  275. package/src/types/key-deriver.ts +43 -0
  276. package/src/types/key-generator.ts +119 -0
  277. package/src/types/key-io.ts +42 -0
  278. package/src/types/key-wrapper.ts +42 -0
  279. package/src/types/params-direct.ts +106 -0
  280. package/src/types/params-enclosed.ts +50 -0
  281. package/src/types/params-kms.ts +156 -0
  282. package/src/types/signer.ts +50 -0
  283. package/src/utils.ts +181 -0
@@ -0,0 +1,270 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __rest = (this && this.__rest) || function (s, e) {
11
+ var t = {};
12
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
13
+ t[p] = s[p];
14
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
15
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
16
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
17
+ t[p[i]] = s[p[i]];
18
+ }
19
+ return t;
20
+ };
21
+ import { Convert } from '@enbox/common';
22
+ import { xchacha20poly1305 } from '@noble/ciphers/chacha';
23
+ import { getWebcryptoSubtle } from '@noble/ciphers/webcrypto';
24
+ import { computeJwkThumbprint, isOctPrivateJwk } from '../jose/jwk.js';
25
+ /**
26
+ * Constant defining the length of the authentication tag in bytes for XChaCha20-Poly1305.
27
+ *
28
+ * @remarks
29
+ * The `POLY1305_TAG_LENGTH` is set to 16 bytes (128 bits), which is the standard size for the
30
+ * Poly1305 authentication tag in XChaCha20-Poly1305 encryption. This tag length ensures
31
+ * a strong level of security for message authentication, verifying the integrity and
32
+ * authenticity of the data during decryption.
33
+ */
34
+ export const POLY1305_TAG_LENGTH = 16;
35
+ /**
36
+ * The `XChaCha20Poly1305` class provides a suite of utilities for cryptographic operations
37
+ * using the XChaCha20-Poly1305 algorithm, a combination of the XChaCha20 stream cipher and the
38
+ * Poly1305 message authentication code (MAC). This class encompasses methods for key generation,
39
+ * encryption, decryption, and conversions between raw byte arrays and JSON Web Key (JWK) formats.
40
+ *
41
+ * XChaCha20-Poly1305 is renowned for its high security and efficiency, especially in scenarios
42
+ * involving large data volumes or where data integrity and confidentiality are paramount. The
43
+ * extended nonce size of XChaCha20 reduces the risks of nonce reuse, while Poly1305 provides
44
+ * a strong MAC ensuring data integrity.
45
+ *
46
+ * Key Features:
47
+ * - Key Generation: Generate XChaCha20-Poly1305 symmetric keys in JWK format.
48
+ * - Key Conversion: Transform keys between raw byte arrays and JWK formats.
49
+ * - Encryption: Encrypt data using XChaCha20-Poly1305, returning both ciphertext and MAC tag.
50
+ * - Decryption: Decrypt data and verify integrity using the XChaCha20-Poly1305 algorithm.
51
+ *
52
+ * The methods in this class are asynchronous, returning Promises to accommodate various
53
+ * JavaScript environments.
54
+ *
55
+ * @example
56
+ * ```ts
57
+ * // Key Generation
58
+ * const privateKey = await XChaCha20Poly1305.generateKey();
59
+ *
60
+ * // Encryption
61
+ * const data = new TextEncoder().encode('Messsage');
62
+ * const nonce = utils.randomBytes(24); // 24-byte nonce
63
+ * const additionalData = new TextEncoder().encode('Associated data');
64
+ * const { ciphertext, tag } = await XChaCha20Poly1305.encrypt({
65
+ * data,
66
+ * nonce,
67
+ * additionalData,
68
+ * key: privateKey
69
+ * });
70
+ *
71
+ * // Decryption
72
+ * const decryptedData = await XChaCha20Poly1305.decrypt({
73
+ * data: ciphertext,
74
+ * nonce,
75
+ * tag,
76
+ * additionalData,
77
+ * key: privateKey
78
+ * });
79
+ *
80
+ * // Key Conversion
81
+ * const privateKeyBytes = await XChaCha20Poly1305.privateKeyToBytes({ privateKey });
82
+ * ```
83
+ */
84
+ export class XChaCha20Poly1305 {
85
+ /**
86
+ * Converts a raw private key in bytes to its corresponding JSON Web Key (JWK) format.
87
+ *
88
+ * @remarks
89
+ * This method takes a symmetric key represented as a byte array (Uint8Array) and converts it into
90
+ * a JWK object for use with the XChaCha20-Poly1305 algorithm. The process involves encoding the
91
+ * key into base64url format and setting the appropriate JWK parameters.
92
+ *
93
+ * The resulting JWK object includes the following properties:
94
+ * - `kty`: Key Type, set to 'oct' for Octet Sequence (representing a symmetric key).
95
+ * - `k`: The symmetric key, base64url-encoded.
96
+ * - `kid`: Key ID, generated based on the JWK thumbprint.
97
+ *
98
+ * @example
99
+ * ```ts
100
+ * const privateKeyBytes = new Uint8Array([...]); // Replace with actual symmetric key bytes
101
+ * const privateKey = await XChaCha20Poly1305.bytesToPrivateKey({ privateKeyBytes });
102
+ * ```
103
+ *
104
+ * @param params - The parameters for the symmetric key conversion.
105
+ * @param params.privateKeyBytes - The raw symmetric key as a Uint8Array.
106
+ *
107
+ * @returns A Promise that resolves to the symmetric key in JWK format.
108
+ */
109
+ static bytesToPrivateKey(_a) {
110
+ return __awaiter(this, arguments, void 0, function* ({ privateKeyBytes }) {
111
+ // Construct the private key in JWK format.
112
+ const privateKey = {
113
+ k: Convert.uint8Array(privateKeyBytes).toBase64Url(),
114
+ kty: 'oct'
115
+ };
116
+ // Compute the JWK thumbprint and set as the key ID.
117
+ privateKey.kid = yield computeJwkThumbprint({ jwk: privateKey });
118
+ return privateKey;
119
+ });
120
+ }
121
+ /**
122
+ * Decrypts the provided data using XChaCha20-Poly1305.
123
+ *
124
+ * @remarks
125
+ * This method performs XChaCha20-Poly1305 decryption on the given encrypted data using the
126
+ * specified key, nonce, and authentication tag. It supports optional additional authenticated
127
+ * data (AAD) for enhanced security. The nonce must be 24 bytes long, consistent with XChaCha20's
128
+ * specifications.
129
+ *
130
+ * @example
131
+ * ```ts
132
+ * const encryptedData = new Uint8Array([...]); // Encrypted data
133
+ * const nonce = new Uint8Array(24); // 24-byte nonce
134
+ * const additionalData = new Uint8Array([...]); // Optional AAD
135
+ * const key = { ... }; // A Jwk object representing the XChaCha20-Poly1305 key
136
+ * const decryptedData = await XChaCha20Poly1305.decrypt({
137
+ * data: encryptedData,
138
+ * nonce,
139
+ * additionalData,
140
+ * key
141
+ * });
142
+ * ```
143
+ *
144
+ * @param params - The parameters for the decryption operation.
145
+ * @param params.data - The encrypted data to decrypt including the authentication tag,
146
+ * represented as a Uint8Array.
147
+ * @param params.key - The key to use for decryption, represented in JWK format.
148
+ * @param params.nonce - The nonce used during the encryption process.
149
+ * @param params.additionalData - Optional additional authenticated data.
150
+ *
151
+ * @returns A Promise that resolves to the decrypted data as a Uint8Array.
152
+ */
153
+ static decrypt(_a) {
154
+ return __awaiter(this, arguments, void 0, function* ({ data, key, nonce, additionalData }) {
155
+ // Convert the private key from JWK format to bytes.
156
+ const privateKeyBytes = yield XChaCha20Poly1305.privateKeyToBytes({ privateKey: key });
157
+ const xc20p = xchacha20poly1305(privateKeyBytes, nonce, additionalData);
158
+ const plaintext = xc20p.decrypt(data);
159
+ return plaintext;
160
+ });
161
+ }
162
+ /**
163
+ * Encrypts the provided data using XChaCha20-Poly1305.
164
+ *
165
+ * @remarks
166
+ * This method performs XChaCha20-Poly1305 encryption on the given data using the specified key
167
+ * and nonce. It supports optional additional authenticated data (AAD) for enhanced security. The
168
+ * nonce must be 24 bytes long, as per XChaCha20's specifications. The method returns the
169
+ * encrypted data along with an authentication tag as a Uint8Array, ensuring both confidentiality
170
+ * and integrity of the data.
171
+ *
172
+ * @example
173
+ * ```ts
174
+ * const data = new TextEncoder().encode('Messsage');
175
+ * const nonce = utils.randomBytes(24); // 24-byte nonce
176
+ * const additionalData = new TextEncoder().encode('Associated data'); // Optional AAD
177
+ * const key = { ... }; // A Jwk object representing an XChaCha20-Poly1305 key
178
+ * const encryptedData = await XChaCha20Poly1305.encrypt({
179
+ * data,
180
+ * nonce,
181
+ * additionalData,
182
+ * key
183
+ * });
184
+ * ```
185
+ *
186
+ * @param params - The parameters for the encryption operation.
187
+ * @param params.data - The data to encrypt, represented as a Uint8Array.
188
+ * @param params.key - The key to use for encryption, represented in JWK format.
189
+ * @param params.nonce - A 24-byte nonce for the encryption process.
190
+ * @param params.additionalData - Optional additional authenticated data.
191
+ *
192
+ * @returns A Promise that resolves to a byte array containing the encrypted data and the
193
+ * authentication tag.
194
+ */
195
+ static encrypt(_a) {
196
+ return __awaiter(this, arguments, void 0, function* ({ data, key, nonce, additionalData }) {
197
+ // Convert the private key from JWK format to bytes.
198
+ const privateKeyBytes = yield XChaCha20Poly1305.privateKeyToBytes({ privateKey: key });
199
+ const xc20p = xchacha20poly1305(privateKeyBytes, nonce, additionalData);
200
+ const ciphertext = xc20p.encrypt(data);
201
+ return ciphertext;
202
+ });
203
+ }
204
+ /**
205
+ * Generates a symmetric key for XChaCha20-Poly1305 in JSON Web Key (JWK) format.
206
+ *
207
+ * @remarks
208
+ * This method creates a new symmetric key suitable for use with the XChaCha20-Poly1305 algorithm.
209
+ * The key is generated using cryptographically secure random number generation to ensure its
210
+ * uniqueness and security. The XChaCha20-Poly1305 algorithm requires a 256-bit key (32 bytes),
211
+ * and this method adheres to that specification.
212
+ *
213
+ * Key components included in the JWK:
214
+ * - `kty`: Key Type, set to 'oct' for Octet Sequence.
215
+ * - `k`: The symmetric key component, base64url-encoded.
216
+ * - `kid`: Key ID, generated based on the JWK thumbprint.
217
+ *
218
+ * @example
219
+ * ```ts
220
+ * const privateKey = await XChaCha20Poly1305.generateKey();
221
+ * ```
222
+ *
223
+ * @returns A Promise that resolves to the generated symmetric key in JWK format.
224
+ */
225
+ static generateKey() {
226
+ return __awaiter(this, void 0, void 0, function* () {
227
+ // Get the Web Crypto API interface.
228
+ const webCrypto = getWebcryptoSubtle();
229
+ // Generate a random private key.
230
+ // See https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues#usage_notes for
231
+ // an explanation for why Web Crypto generateKey() is used instead of getRandomValues().
232
+ const webCryptoKey = yield webCrypto.generateKey({ name: 'AES-CTR', length: 256 }, true, ['encrypt']);
233
+ // Export the private key in JWK format.
234
+ const _a = yield webCrypto.exportKey('jwk', webCryptoKey), { alg, ext, key_ops } = _a, privateKey = __rest(_a, ["alg", "ext", "key_ops"]);
235
+ // Compute the JWK thumbprint and set as the key ID.
236
+ privateKey.kid = yield computeJwkThumbprint({ jwk: privateKey });
237
+ return privateKey;
238
+ });
239
+ }
240
+ /**
241
+ * Converts a private key from JSON Web Key (JWK) format to a raw byte array (Uint8Array).
242
+ *
243
+ * This method takes a symmetric key in JWK format and extracts its raw byte representation.
244
+ * It decodes the 'k' parameter of the JWK value, which represents the symmetric key in base64url
245
+ * encoding, into a byte array.
246
+ *
247
+ * @example
248
+ * ```ts
249
+ * const privateKey = { ... }; // A symmetric key in JWK format
250
+ * const privateKeyBytes = await XChaCha20Poly1305.privateKeyToBytes({ privateKey });
251
+ * ```
252
+ *
253
+ * @param params - The parameters for the symmetric key conversion.
254
+ * @param params.privateKey - The symmetric key in JWK format.
255
+ *
256
+ * @returns A Promise that resolves to the symmetric key as a Uint8Array.
257
+ */
258
+ static privateKeyToBytes(_a) {
259
+ return __awaiter(this, arguments, void 0, function* ({ privateKey }) {
260
+ // Verify the provided JWK represents a valid oct private key.
261
+ if (!isOctPrivateJwk(privateKey)) {
262
+ throw new Error(`XChaCha20Poly1305: The provided key is not a valid oct private key.`);
263
+ }
264
+ // Decode the provided private key to bytes.
265
+ const privateKeyBytes = Convert.base64Url(privateKey.k).toUint8Array();
266
+ return privateKeyBytes;
267
+ });
268
+ }
269
+ }
270
+ //# sourceMappingURL=xchacha20-poly1305.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"xchacha20-poly1305.js","sourceRoot":"","sources":["../../../src/primitives/xchacha20-poly1305.ts"],"names":[],"mappings":"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"}
@@ -0,0 +1,246 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __rest = (this && this.__rest) || function (s, e) {
11
+ var t = {};
12
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
13
+ t[p] = s[p];
14
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
15
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
16
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
17
+ t[p[i]] = s[p[i]];
18
+ }
19
+ return t;
20
+ };
21
+ import { Convert } from '@enbox/common';
22
+ import { xchacha20 } from '@noble/ciphers/chacha';
23
+ import { getWebcryptoSubtle } from '@noble/ciphers/webcrypto';
24
+ import { computeJwkThumbprint, isOctPrivateJwk } from '../jose/jwk.js';
25
+ /**
26
+ * The `XChaCha20` class provides a comprehensive suite of utilities for cryptographic operations
27
+ * using the XChaCha20 symmetric encryption algorithm. This class includes methods for key
28
+ * generation, encryption, decryption, and conversions between raw byte arrays and JSON Web Key
29
+ * (JWK) formats. XChaCha20 is an extended nonce variant of ChaCha20, a stream cipher designed for
30
+ * high-speed encryption with substantial security margins.
31
+ *
32
+ * The XChaCha20 algorithm is particularly well-suited for encrypting large volumes of data or
33
+ * data streams, especially where random access is required. The class adheres to standard
34
+ * cryptographic practices, ensuring robustness and security in its implementations.
35
+ *
36
+ * Key Features:
37
+ * - Key Generation: Generate XChaCha20 symmetric keys in JWK format.
38
+ * - Key Conversion: Transform keys between raw byte arrays and JWK formats.
39
+ * - Encryption: Encrypt data using XChaCha20 with the provided symmetric key.
40
+ * - Decryption: Decrypt data encrypted with XChaCha20 using the corresponding symmetric key.
41
+ *
42
+ * The methods in this class are asynchronous, returning Promises to accommodate various
43
+ * JavaScript environments.
44
+ *
45
+ * @example
46
+ * ```ts
47
+ * // Key Generation
48
+ * const privateKey = await XChaCha20.generateKey();
49
+ *
50
+ * // Encryption
51
+ * const data = new TextEncoder().encode('Messsage');
52
+ * const nonce = utils.randomBytes(24); // 24-byte nonce for XChaCha20
53
+ * const encryptedData = await XChaCha20.encrypt({
54
+ * data,
55
+ * nonce,
56
+ * key: privateKey
57
+ * });
58
+ *
59
+ * // Decryption
60
+ * const decryptedData = await XChaCha20.decrypt({
61
+ * data: encryptedData,
62
+ * nonce,
63
+ * key: privateKey
64
+ * });
65
+ *
66
+ * // Key Conversion
67
+ * const privateKeyBytes = await XChaCha20.privateKeyToBytes({ privateKey });
68
+ * ```
69
+ */
70
+ export class XChaCha20 {
71
+ /**
72
+ * Converts a raw private key in bytes to its corresponding JSON Web Key (JWK) format.
73
+ *
74
+ * @remarks
75
+ * This method takes a symmetric key represented as a byte array (Uint8Array) and
76
+ * converts it into a JWK object for use with the XChaCha20 symmetric encryption algorithm. The
77
+ * conversion process involves encoding the key into base64url format and setting the appropriate
78
+ * JWK parameters.
79
+ *
80
+ * The resulting JWK object includes the following properties:
81
+ * - `kty`: Key Type, set to 'oct' for Octet Sequence (representing a symmetric key).
82
+ * - `k`: The symmetric key, base64url-encoded.
83
+ * - `kid`: Key ID, generated based on the JWK thumbprint.
84
+ *
85
+ * @example
86
+ * ```ts
87
+ * const privateKeyBytes = new Uint8Array([...]); // Replace with actual symmetric key bytes
88
+ * const privateKey = await XChaCha20.bytesToPrivateKey({ privateKeyBytes });
89
+ * ```
90
+ *
91
+ * @param params - The parameters for the symmetric key conversion.
92
+ * @param params.privateKeyBytes - The raw symmetric key as a Uint8Array.
93
+ *
94
+ * @returns A Promise that resolves to the symmetric key in JWK format.
95
+ */
96
+ static bytesToPrivateKey(_a) {
97
+ return __awaiter(this, arguments, void 0, function* ({ privateKeyBytes }) {
98
+ // Construct the private key in JWK format.
99
+ const privateKey = {
100
+ k: Convert.uint8Array(privateKeyBytes).toBase64Url(),
101
+ kty: 'oct'
102
+ };
103
+ // Compute the JWK thumbprint and set as the key ID.
104
+ privateKey.kid = yield computeJwkThumbprint({ jwk: privateKey });
105
+ return privateKey;
106
+ });
107
+ }
108
+ /**
109
+ * Decrypts the provided data using XChaCha20.
110
+ *
111
+ * @remarks
112
+ * This method performs XChaCha20 decryption on the given encrypted data using the specified key
113
+ * and nonce. The nonce should be the same as used in the encryption process and must be 24 bytes
114
+ * long. The method returns the decrypted data as a Uint8Array.
115
+ *
116
+ * @example
117
+ * ```ts
118
+ * const encryptedData = new Uint8Array([...]); // Encrypted data
119
+ * const nonce = new Uint8Array(24); // 24-byte nonce used during encryption
120
+ * const key = { ... }; // A Jwk object representing the XChaCha20 key
121
+ * const decryptedData = await XChaCha20.decrypt({
122
+ * data: encryptedData,
123
+ * nonce,
124
+ * key
125
+ * });
126
+ * ```
127
+ *
128
+ * @param params - The parameters for the decryption operation.
129
+ * @param params.data - The encrypted data to decrypt, represented as a Uint8Array.
130
+ * @param params.key - The key to use for decryption, represented in JWK format.
131
+ * @param params.nonce - The nonce used during the encryption process.
132
+ *
133
+ * @returns A Promise that resolves to the decrypted data as a Uint8Array.
134
+ */
135
+ static decrypt(_a) {
136
+ return __awaiter(this, arguments, void 0, function* ({ data, key, nonce }) {
137
+ // Convert the private key from JWK format to bytes.
138
+ const privateKeyBytes = yield XChaCha20.privateKeyToBytes({ privateKey: key });
139
+ const ciphertext = xchacha20(privateKeyBytes, nonce, data);
140
+ return ciphertext;
141
+ });
142
+ }
143
+ /**
144
+ * Encrypts the provided data using XChaCha20.
145
+ *
146
+ * @remarks
147
+ * This method performs XChaCha20 encryption on the given data using the specified key and nonce.
148
+ * The nonce must be 24 bytes long, ensuring a high level of security through a vast nonce space,
149
+ * reducing the risks associated with nonce reuse. The method returns the encrypted data as a
150
+ * Uint8Array.
151
+ *
152
+ * @example
153
+ * ```ts
154
+ * const data = new TextEncoder().encode('Messsage');
155
+ * const nonce = utils.randomBytes(24); // 24-byte nonce for XChaCha20
156
+ * const key = { ... }; // A Jwk object representing an XChaCha20 key
157
+ * const encryptedData = await XChaCha20.encrypt({
158
+ * data,
159
+ * nonce,
160
+ * key
161
+ * });
162
+ * ```
163
+ *
164
+ * @param params - The parameters for the encryption operation.
165
+ * @param params.data - The data to encrypt, represented as a Uint8Array.
166
+ * @param params.key - The key to use for encryption, represented in JWK format.
167
+ * @param params.nonce - A 24-byte nonce for the encryption process.
168
+ *
169
+ * @returns A Promise that resolves to the encrypted data as a Uint8Array.
170
+ */
171
+ static encrypt(_a) {
172
+ return __awaiter(this, arguments, void 0, function* ({ data, key, nonce }) {
173
+ // Convert the private key from JWK format to bytes.
174
+ const privateKeyBytes = yield XChaCha20.privateKeyToBytes({ privateKey: key });
175
+ const plaintext = xchacha20(privateKeyBytes, nonce, data);
176
+ return plaintext;
177
+ });
178
+ }
179
+ /**
180
+ * Generates a symmetric key for XChaCha20 in JSON Web Key (JWK) format.
181
+ *
182
+ * @remarks
183
+ * This method creates a new symmetric key suitable for use with the XChaCha20 encryption
184
+ * algorithm. The key is generated using cryptographically secure random number generation
185
+ * to ensure its uniqueness and security. The XChaCha20 algorithm requires a 256-bit key
186
+ * (32 bytes), and this method adheres to that specification.
187
+ *
188
+ * Key components included in the JWK:
189
+ * - `kty`: Key Type, set to 'oct' for Octet Sequence.
190
+ * - `k`: The symmetric key component, base64url-encoded.
191
+ * - `kid`: Key ID, generated based on the JWK thumbprint.
192
+ *
193
+ * @example
194
+ * ```ts
195
+ * const privateKey = await XChaCha20.generateKey();
196
+ * ```
197
+ *
198
+ * @returns A Promise that resolves to the generated symmetric key in JWK format.
199
+ */
200
+ static generateKey() {
201
+ return __awaiter(this, void 0, void 0, function* () {
202
+ // Get the Web Crypto API interface.
203
+ const webCrypto = getWebcryptoSubtle();
204
+ // Generate a random private key.
205
+ // See https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues#usage_notes for
206
+ // an explanation for why Web Crypto generateKey() is used instead of getRandomValues().
207
+ const webCryptoKey = yield webCrypto.generateKey({ name: 'AES-CTR', length: 256 }, true, ['encrypt']);
208
+ // Export the private key in JWK format.
209
+ const _a = yield webCrypto.exportKey('jwk', webCryptoKey), { alg, ext, key_ops } = _a, privateKey = __rest(_a, ["alg", "ext", "key_ops"]);
210
+ // Compute the JWK thumbprint and set as the key ID.
211
+ privateKey.kid = yield computeJwkThumbprint({ jwk: privateKey });
212
+ return privateKey;
213
+ });
214
+ }
215
+ /**
216
+ * Converts a private key from JSON Web Key (JWK) format to a raw byte array (Uint8Array).
217
+ *
218
+ * @remarks
219
+ * This method takes a symmetric key in JWK format and extracts its raw byte representation.
220
+ * It decodes the 'k' parameter of the JWK value, which represents the symmetric key in base64url
221
+ * encoding, into a byte array.
222
+ *
223
+ * @example
224
+ * ```ts
225
+ * const privateKey = { ... }; // A symmetric key in JWK format
226
+ * const privateKeyBytes = await XChaCha20.privateKeyToBytes({ privateKey });
227
+ * ```
228
+ *
229
+ * @param params - The parameters for the symmetric key conversion.
230
+ * @param params.privateKey - The symmetric key in JWK format.
231
+ *
232
+ * @returns A Promise that resolves to the symmetric key as a Uint8Array.
233
+ */
234
+ static privateKeyToBytes(_a) {
235
+ return __awaiter(this, arguments, void 0, function* ({ privateKey }) {
236
+ // Verify the provided JWK represents a valid oct private key.
237
+ if (!isOctPrivateJwk(privateKey)) {
238
+ throw new Error(`XChaCha20: The provided key is not a valid oct private key.`);
239
+ }
240
+ // Decode the provided private key to bytes.
241
+ const privateKeyBytes = Convert.base64Url(privateKey.k).toUint8Array();
242
+ return privateKeyBytes;
243
+ });
244
+ }
245
+ }
246
+ //# sourceMappingURL=xchacha20.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"xchacha20.js","sourceRoot":"","sources":["../../../src/primitives/xchacha20.ts"],"names":[],"mappings":"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"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=cipher.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cipher.js","sourceRoot":"","sources":["../../../src/types/cipher.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=crypto-api.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"crypto-api.js","sourceRoot":"","sources":["../../../src/types/crypto-api.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=hasher.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hasher.js","sourceRoot":"","sources":["../../../src/types/hasher.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=identifier.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"identifier.js","sourceRoot":"","sources":["../../../src/types/identifier.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=key-compressor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"key-compressor.js","sourceRoot":"","sources":["../../../src/types/key-compressor.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=key-converter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"key-converter.js","sourceRoot":"","sources":["../../../src/types/key-converter.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=key-deriver.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"key-deriver.js","sourceRoot":"","sources":["../../../src/types/key-deriver.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=key-generator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"key-generator.js","sourceRoot":"","sources":["../../../src/types/key-generator.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=key-io.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"key-io.js","sourceRoot":"","sources":["../../../src/types/key-io.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=key-wrapper.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"key-wrapper.js","sourceRoot":"","sources":["../../../src/types/key-wrapper.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=params-direct.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"params-direct.js","sourceRoot":"","sources":["../../../src/types/params-direct.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=params-enclosed.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"params-enclosed.js","sourceRoot":"","sources":["../../../src/types/params-enclosed.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=params-kms.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"params-kms.js","sourceRoot":"","sources":["../../../src/types/params-kms.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=signer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signer.js","sourceRoot":"","sources":["../../../src/types/signer.ts"],"names":[],"mappings":""}