@metamask/utils 8.3.0 → 8.5.0

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 (346) hide show
  1. package/CHANGELOG.md +26 -1
  2. package/dist/assert.cjs +127 -0
  3. package/dist/assert.cjs.map +1 -0
  4. package/dist/{types/assert.d.ts → assert.d.cts} +3 -3
  5. package/dist/assert.d.cts.map +1 -0
  6. package/dist/assert.d.mts +61 -0
  7. package/dist/assert.d.mts.map +1 -0
  8. package/dist/assert.mjs +119 -15
  9. package/dist/assert.mjs.map +1 -1
  10. package/dist/base64.cjs +34 -0
  11. package/dist/base64.cjs.map +1 -0
  12. package/dist/{types/base64.d.ts → base64.d.cts} +3 -3
  13. package/dist/base64.d.cts.map +1 -0
  14. package/dist/base64.d.mts +25 -0
  15. package/dist/base64.d.mts.map +1 -0
  16. package/dist/base64.mjs +28 -9
  17. package/dist/base64.mjs.map +1 -1
  18. package/dist/bytes.cjs +400 -0
  19. package/dist/{chunk-QEPVHEP7.js.map → bytes.cjs.map} +1 -1
  20. package/dist/{types/bytes.d.ts → bytes.d.cts} +3 -3
  21. package/dist/bytes.d.cts.map +1 -0
  22. package/dist/bytes.d.mts +183 -0
  23. package/dist/bytes.d.mts.map +1 -0
  24. package/dist/bytes.mjs +379 -42
  25. package/dist/bytes.mjs.map +1 -1
  26. package/dist/caip-types.cjs +151 -0
  27. package/dist/caip-types.cjs.map +1 -0
  28. package/dist/{types/caip-types.d.ts → caip-types.d.cts} +33 -12
  29. package/dist/caip-types.d.cts.map +1 -0
  30. package/dist/caip-types.d.mts +114 -0
  31. package/dist/caip-types.d.mts.map +1 -0
  32. package/dist/caip-types.mjs +139 -39
  33. package/dist/caip-types.mjs.map +1 -1
  34. package/dist/checksum.cjs +7 -0
  35. package/dist/checksum.cjs.map +1 -0
  36. package/dist/checksum.d.cts +2 -0
  37. package/dist/checksum.d.cts.map +1 -0
  38. package/dist/checksum.d.mts +2 -0
  39. package/dist/checksum.d.mts.map +1 -0
  40. package/dist/checksum.mjs +3 -11
  41. package/dist/checksum.mjs.map +1 -1
  42. package/dist/coercers.cjs +162 -0
  43. package/dist/coercers.cjs.map +1 -0
  44. package/dist/{types/coercers.d.ts → coercers.d.cts} +7 -7
  45. package/dist/coercers.d.cts.map +1 -0
  46. package/dist/coercers.d.mts +97 -0
  47. package/dist/coercers.d.mts.map +1 -0
  48. package/dist/coercers.mjs +154 -17
  49. package/dist/coercers.mjs.map +1 -1
  50. package/dist/collections.cjs +109 -0
  51. package/dist/collections.cjs.map +1 -0
  52. package/dist/{types/collections.d.ts → collections.d.cts} +1 -1
  53. package/dist/collections.d.cts.map +1 -0
  54. package/dist/collections.d.mts +39 -0
  55. package/dist/collections.d.mts.map +1 -0
  56. package/dist/collections.mjs +103 -8
  57. package/dist/collections.mjs.map +1 -1
  58. package/dist/encryption-types.cjs +3 -0
  59. package/dist/encryption-types.cjs.map +1 -0
  60. package/dist/{types/encryption-types.d.ts → encryption-types.d.cts} +2 -2
  61. package/dist/encryption-types.d.cts.map +1 -0
  62. package/dist/encryption-types.d.mts +7 -0
  63. package/dist/encryption-types.d.mts.map +1 -0
  64. package/dist/encryption-types.mjs +1 -1
  65. package/dist/encryption-types.mjs.map +1 -1
  66. package/dist/errors.cjs +111 -0
  67. package/dist/errors.cjs.map +1 -0
  68. package/dist/{types/errors.d.ts → errors.d.cts} +1 -1
  69. package/dist/errors.d.cts.map +1 -0
  70. package/dist/errors.d.mts +58 -0
  71. package/dist/errors.d.mts.map +1 -0
  72. package/dist/errors.mjs +102 -16
  73. package/dist/errors.mjs.map +1 -1
  74. package/dist/fs.cjs +248 -0
  75. package/dist/fs.cjs.map +1 -0
  76. package/dist/{types/fs.d.ts → fs.d.cts} +3 -3
  77. package/dist/fs.d.cts.map +1 -0
  78. package/dist/fs.d.mts +133 -0
  79. package/dist/fs.d.mts.map +1 -0
  80. package/dist/fs.mjs +209 -25
  81. package/dist/fs.mjs.map +1 -1
  82. package/dist/hex.cjs +134 -0
  83. package/dist/hex.cjs.map +1 -0
  84. package/dist/{types/hex.d.ts → hex.d.cts} +3 -3
  85. package/dist/hex.d.cts.map +1 -0
  86. package/dist/hex.d.mts +77 -0
  87. package/dist/hex.d.mts.map +1 -0
  88. package/dist/hex.mjs +121 -34
  89. package/dist/hex.mjs.map +1 -1
  90. package/dist/index.cjs +37 -0
  91. package/dist/index.cjs.map +1 -0
  92. package/dist/index.d.cts +21 -0
  93. package/dist/index.d.cts.map +1 -0
  94. package/dist/index.d.mts +21 -0
  95. package/dist/index.d.mts.map +1 -0
  96. package/dist/index.mjs +20 -292
  97. package/dist/index.mjs.map +1 -1
  98. package/dist/json.cjs +413 -0
  99. package/dist/json.cjs.map +1 -0
  100. package/dist/{types/json.d.ts → json.d.cts} +21 -22
  101. package/dist/json.d.cts.map +1 -0
  102. package/dist/json.d.mts +383 -0
  103. package/dist/json.d.mts.map +1 -0
  104. package/dist/json.mjs +389 -74
  105. package/dist/json.mjs.map +1 -1
  106. package/dist/keyring.cjs +3 -0
  107. package/dist/keyring.cjs.map +1 -0
  108. package/dist/{types/keyring.d.ts → keyring.d.cts} +7 -7
  109. package/dist/keyring.d.cts.map +1 -0
  110. package/dist/keyring.d.mts +224 -0
  111. package/dist/keyring.d.mts.map +1 -0
  112. package/dist/keyring.mjs +1 -1
  113. package/dist/keyring.mjs.map +1 -1
  114. package/dist/logging.cjs +43 -0
  115. package/dist/logging.cjs.map +1 -0
  116. package/dist/{types/logging.d.ts → logging.d.cts} +2 -2
  117. package/dist/logging.d.cts.map +1 -0
  118. package/dist/logging.d.mts +30 -0
  119. package/dist/logging.d.mts.map +1 -0
  120. package/dist/logging.mjs +34 -9
  121. package/dist/logging.mjs.map +1 -1
  122. package/dist/misc.cjs +149 -0
  123. package/dist/misc.cjs.map +1 -0
  124. package/dist/{types/misc.d.ts → misc.d.cts} +6 -6
  125. package/dist/misc.d.cts.map +1 -0
  126. package/dist/misc.d.mts +119 -0
  127. package/dist/misc.d.mts.map +1 -0
  128. package/dist/misc.mjs +136 -27
  129. package/dist/misc.mjs.map +1 -1
  130. package/dist/node.cjs +19 -0
  131. package/dist/node.cjs.map +1 -0
  132. package/dist/node.d.cts +3 -0
  133. package/dist/node.d.cts.map +1 -0
  134. package/dist/node.d.mts +3 -0
  135. package/dist/node.d.mts.map +1 -0
  136. package/dist/node.mjs +2 -312
  137. package/dist/node.mjs.map +1 -1
  138. package/dist/number.cjs +102 -0
  139. package/dist/number.cjs.map +1 -0
  140. package/dist/{types/number.d.ts → number.d.cts} +1 -1
  141. package/dist/number.d.cts.map +1 -0
  142. package/dist/number.d.mts +73 -0
  143. package/dist/number.d.mts.map +1 -0
  144. package/dist/number.mjs +93 -16
  145. package/dist/number.mjs.map +1 -1
  146. package/dist/opaque.cjs +3 -0
  147. package/dist/opaque.cjs.map +1 -0
  148. package/dist/opaque.d.cts +6 -0
  149. package/dist/opaque.d.cts.map +1 -0
  150. package/dist/opaque.d.mts +6 -0
  151. package/dist/opaque.d.mts.map +1 -0
  152. package/dist/opaque.mjs +1 -1
  153. package/dist/opaque.mjs.map +1 -1
  154. package/dist/promise.cjs +40 -0
  155. package/dist/promise.cjs.map +1 -0
  156. package/dist/{types/promise.d.ts → promise.d.cts} +12 -2
  157. package/dist/promise.d.cts.map +1 -0
  158. package/dist/promise.d.mts +45 -0
  159. package/dist/promise.d.mts.map +1 -0
  160. package/dist/promise.mjs +35 -7
  161. package/dist/promise.mjs.map +1 -1
  162. package/dist/time.cjs +67 -0
  163. package/dist/time.cjs.map +1 -0
  164. package/dist/{types/time.d.ts → time.d.cts} +1 -1
  165. package/dist/time.d.cts.map +1 -0
  166. package/dist/time.d.mts +49 -0
  167. package/dist/time.d.mts.map +1 -0
  168. package/dist/time.mjs +60 -10
  169. package/dist/time.mjs.map +1 -1
  170. package/dist/transaction-types.cjs +3 -0
  171. package/dist/transaction-types.cjs.map +1 -0
  172. package/dist/{types/transaction-types.d.ts → transaction-types.d.cts} +11 -11
  173. package/dist/transaction-types.d.cts.map +1 -0
  174. package/dist/transaction-types.d.mts +117 -0
  175. package/dist/transaction-types.d.mts.map +1 -0
  176. package/dist/transaction-types.mjs +1 -1
  177. package/dist/transaction-types.mjs.map +1 -1
  178. package/dist/versions.cjs +95 -0
  179. package/dist/versions.cjs.map +1 -0
  180. package/dist/{types/versions.d.ts → versions.d.cts} +5 -5
  181. package/dist/versions.d.cts.map +1 -0
  182. package/dist/versions.d.mts +101 -0
  183. package/dist/versions.d.mts.map +1 -0
  184. package/dist/versions.mjs +85 -26
  185. package/dist/versions.mjs.map +1 -1
  186. package/package.json +28 -17
  187. package/dist/assert.js +0 -16
  188. package/dist/assert.js.map +0 -1
  189. package/dist/base64.js +0 -11
  190. package/dist/base64.js.map +0 -1
  191. package/dist/bytes.js +0 -43
  192. package/dist/bytes.js.map +0 -1
  193. package/dist/caip-types.js +0 -40
  194. package/dist/caip-types.js.map +0 -1
  195. package/dist/checksum.js +0 -12
  196. package/dist/checksum.js.map +0 -1
  197. package/dist/chunk-2LBGT4GH.js +0 -15
  198. package/dist/chunk-2LBGT4GH.js.map +0 -1
  199. package/dist/chunk-3W5G4CYI.js +0 -25
  200. package/dist/chunk-3W5G4CYI.js.map +0 -1
  201. package/dist/chunk-4D6XQBHA.js +0 -69
  202. package/dist/chunk-4D6XQBHA.js.map +0 -1
  203. package/dist/chunk-4NIRTM4M.js +0 -23
  204. package/dist/chunk-4NIRTM4M.js.map +0 -1
  205. package/dist/chunk-4RMX5YWE.js +0 -34
  206. package/dist/chunk-4RMX5YWE.js.map +0 -1
  207. package/dist/chunk-52OU772R.mjs +0 -122
  208. package/dist/chunk-52OU772R.mjs.map +0 -1
  209. package/dist/chunk-5AVWINSB.js +0 -1
  210. package/dist/chunk-5AVWINSB.js.map +0 -1
  211. package/dist/chunk-622IOGVI.mjs +0 -1
  212. package/dist/chunk-622IOGVI.mjs.map +0 -1
  213. package/dist/chunk-6C35XQOF.mjs +0 -257
  214. package/dist/chunk-6C35XQOF.mjs.map +0 -1
  215. package/dist/chunk-6NZW4WK4.js +0 -35
  216. package/dist/chunk-6NZW4WK4.js.map +0 -1
  217. package/dist/chunk-6ZDHSOUV.js +0 -59
  218. package/dist/chunk-6ZDHSOUV.js.map +0 -1
  219. package/dist/chunk-74DGVJVE.mjs +0 -59
  220. package/dist/chunk-74DGVJVE.mjs.map +0 -1
  221. package/dist/chunk-AY6FDCBT.mjs +0 -1
  222. package/dist/chunk-AY6FDCBT.mjs.map +0 -1
  223. package/dist/chunk-B7LIM2PK.mjs +0 -23
  224. package/dist/chunk-B7LIM2PK.mjs.map +0 -1
  225. package/dist/chunk-BFQDMI3M.js +0 -122
  226. package/dist/chunk-BFQDMI3M.js.map +0 -1
  227. package/dist/chunk-DHVKFDHQ.js +0 -95
  228. package/dist/chunk-DHVKFDHQ.js.map +0 -1
  229. package/dist/chunk-E4C7EW4R.js +0 -16
  230. package/dist/chunk-E4C7EW4R.js.map +0 -1
  231. package/dist/chunk-EQMZL4XU.js +0 -1
  232. package/dist/chunk-EQMZL4XU.js.map +0 -1
  233. package/dist/chunk-GZS3IQBZ.mjs +0 -16
  234. package/dist/chunk-GZS3IQBZ.mjs.map +0 -1
  235. package/dist/chunk-H4YFDLB7.mjs +0 -70
  236. package/dist/chunk-H4YFDLB7.mjs.map +0 -1
  237. package/dist/chunk-I575FZFH.mjs +0 -1
  238. package/dist/chunk-I575FZFH.mjs.map +0 -1
  239. package/dist/chunk-IZC266HS.js +0 -55
  240. package/dist/chunk-IZC266HS.js.map +0 -1
  241. package/dist/chunk-JPAL7Q5S.mjs +0 -90
  242. package/dist/chunk-JPAL7Q5S.mjs.map +0 -1
  243. package/dist/chunk-LC2CRSWD.js +0 -1
  244. package/dist/chunk-LC2CRSWD.js.map +0 -1
  245. package/dist/chunk-NQMRFZHB.mjs +0 -35
  246. package/dist/chunk-NQMRFZHB.mjs.map +0 -1
  247. package/dist/chunk-O3EIM33O.mjs +0 -95
  248. package/dist/chunk-O3EIM33O.mjs.map +0 -1
  249. package/dist/chunk-OLLG4H35.js +0 -257
  250. package/dist/chunk-OLLG4H35.js.map +0 -1
  251. package/dist/chunk-QEPVHEP7.js +0 -273
  252. package/dist/chunk-QVEKZRZ2.js +0 -70
  253. package/dist/chunk-QVEKZRZ2.js.map +0 -1
  254. package/dist/chunk-RIRDOQPX.mjs +0 -15
  255. package/dist/chunk-RIRDOQPX.mjs.map +0 -1
  256. package/dist/chunk-RKRGAFXY.js +0 -1
  257. package/dist/chunk-RKRGAFXY.js.map +0 -1
  258. package/dist/chunk-ROQSKDP5.mjs +0 -69
  259. package/dist/chunk-ROQSKDP5.mjs.map +0 -1
  260. package/dist/chunk-RRYOWRCV.mjs +0 -1
  261. package/dist/chunk-RRYOWRCV.mjs.map +0 -1
  262. package/dist/chunk-S3UHBN2Z.mjs +0 -44
  263. package/dist/chunk-S3UHBN2Z.mjs.map +0 -1
  264. package/dist/chunk-TGOMTREC.mjs +0 -75
  265. package/dist/chunk-TGOMTREC.mjs.map +0 -1
  266. package/dist/chunk-THNLGEXV.mjs +0 -34
  267. package/dist/chunk-THNLGEXV.mjs.map +0 -1
  268. package/dist/chunk-U7ZUGCE7.js +0 -75
  269. package/dist/chunk-U7ZUGCE7.js.map +0 -1
  270. package/dist/chunk-UOTVU7OQ.js +0 -1
  271. package/dist/chunk-UOTVU7OQ.js.map +0 -1
  272. package/dist/chunk-VA2DRBDE.mjs +0 -273
  273. package/dist/chunk-VA2DRBDE.mjs.map +0 -1
  274. package/dist/chunk-VFXTVNXN.js +0 -44
  275. package/dist/chunk-VFXTVNXN.js.map +0 -1
  276. package/dist/chunk-WA4DHIND.mjs +0 -1
  277. package/dist/chunk-WA4DHIND.mjs.map +0 -1
  278. package/dist/chunk-X66SUIEF.mjs +0 -25
  279. package/dist/chunk-X66SUIEF.mjs.map +0 -1
  280. package/dist/chunk-XYGUOY6N.mjs +0 -55
  281. package/dist/chunk-XYGUOY6N.mjs.map +0 -1
  282. package/dist/chunk-Z2RGWDD7.js +0 -90
  283. package/dist/chunk-Z2RGWDD7.js.map +0 -1
  284. package/dist/coercers.js +0 -18
  285. package/dist/coercers.js.map +0 -1
  286. package/dist/collections.js +0 -10
  287. package/dist/collections.js.map +0 -1
  288. package/dist/encryption-types.js +0 -2
  289. package/dist/encryption-types.js.map +0 -1
  290. package/dist/errors.js +0 -17
  291. package/dist/errors.js.map +0 -1
  292. package/dist/fs.js +0 -26
  293. package/dist/fs.js.map +0 -1
  294. package/dist/hex.js +0 -35
  295. package/dist/hex.js.map +0 -1
  296. package/dist/index.js +0 -293
  297. package/dist/index.js.map +0 -1
  298. package/dist/json.js +0 -75
  299. package/dist/json.js.map +0 -1
  300. package/dist/keyring.js +0 -2
  301. package/dist/keyring.js.map +0 -1
  302. package/dist/logging.js +0 -10
  303. package/dist/logging.js.map +0 -1
  304. package/dist/misc.js +0 -28
  305. package/dist/misc.js.map +0 -1
  306. package/dist/node.js +0 -313
  307. package/dist/node.js.map +0 -1
  308. package/dist/number.js +0 -18
  309. package/dist/number.js.map +0 -1
  310. package/dist/opaque.js +0 -2
  311. package/dist/opaque.js.map +0 -1
  312. package/dist/promise.js +0 -8
  313. package/dist/promise.js.map +0 -1
  314. package/dist/time.js +0 -12
  315. package/dist/time.js.map +0 -1
  316. package/dist/transaction-types.js +0 -2
  317. package/dist/transaction-types.js.map +0 -1
  318. package/dist/types/assert.d.ts.map +0 -1
  319. package/dist/types/base64.d.ts.map +0 -1
  320. package/dist/types/bytes.d.ts.map +0 -1
  321. package/dist/types/caip-types.d.ts.map +0 -1
  322. package/dist/types/checksum.d.ts +0 -2
  323. package/dist/types/checksum.d.ts.map +0 -1
  324. package/dist/types/coercers.d.ts.map +0 -1
  325. package/dist/types/collections.d.ts.map +0 -1
  326. package/dist/types/encryption-types.d.ts.map +0 -1
  327. package/dist/types/errors.d.ts.map +0 -1
  328. package/dist/types/fs.d.ts.map +0 -1
  329. package/dist/types/hex.d.ts.map +0 -1
  330. package/dist/types/index.d.ts +0 -21
  331. package/dist/types/index.d.ts.map +0 -1
  332. package/dist/types/json.d.ts.map +0 -1
  333. package/dist/types/keyring.d.ts.map +0 -1
  334. package/dist/types/logging.d.ts.map +0 -1
  335. package/dist/types/misc.d.ts.map +0 -1
  336. package/dist/types/node.d.ts +0 -3
  337. package/dist/types/node.d.ts.map +0 -1
  338. package/dist/types/number.d.ts.map +0 -1
  339. package/dist/types/opaque.d.ts +0 -6
  340. package/dist/types/opaque.d.ts.map +0 -1
  341. package/dist/types/promise.d.ts.map +0 -1
  342. package/dist/types/time.d.ts.map +0 -1
  343. package/dist/types/transaction-types.d.ts.map +0 -1
  344. package/dist/types/versions.d.ts.map +0 -1
  345. package/dist/versions.js +0 -27
  346. package/dist/versions.js.map +0 -1
package/dist/bytes.cjs ADDED
@@ -0,0 +1,400 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createDataView = exports.concatBytes = exports.valueToBytes = exports.base64ToBytes = exports.stringToBytes = exports.numberToBytes = exports.signedBigIntToBytes = exports.bigIntToBytes = exports.hexToBytes = exports.bytesToBase64 = exports.bytesToString = exports.bytesToNumber = exports.bytesToSignedBigInt = exports.bytesToBigInt = exports.bytesToHex = exports.assertIsBytes = exports.isBytes = void 0;
4
+ const base_1 = require("@scure/base");
5
+ const assert_1 = require("./assert.cjs");
6
+ const hex_1 = require("./hex.cjs");
7
+ // '0'.charCodeAt(0) === 48
8
+ const HEX_MINIMUM_NUMBER_CHARACTER = 48;
9
+ // '9'.charCodeAt(0) === 57
10
+ const HEX_MAXIMUM_NUMBER_CHARACTER = 58;
11
+ const HEX_CHARACTER_OFFSET = 87;
12
+ /**
13
+ * Memoized function that returns an array to be used as a lookup table for
14
+ * converting bytes to hexadecimal values.
15
+ *
16
+ * The array is created lazily and then cached for future use. The benefit of
17
+ * this approach is that the performance of converting bytes to hex is much
18
+ * better than if we were to call `toString(16)` on each byte.
19
+ *
20
+ * The downside is that the array is created once and then never garbage
21
+ * collected. This is not a problem in practice because the array is only 256
22
+ * elements long.
23
+ *
24
+ * @returns A function that returns the lookup table.
25
+ */
26
+ function getPrecomputedHexValuesBuilder() {
27
+ // To avoid issues with tree shaking, we need to use a function to return the
28
+ // array. This is because the array is only used in the `bytesToHex` function
29
+ // and if we were to use a global variable, the array might be removed by the
30
+ // tree shaker.
31
+ const lookupTable = [];
32
+ return () => {
33
+ if (lookupTable.length === 0) {
34
+ for (let i = 0; i < 256; i++) {
35
+ lookupTable.push(i.toString(16).padStart(2, '0'));
36
+ }
37
+ }
38
+ return lookupTable;
39
+ };
40
+ }
41
+ /**
42
+ * Function implementation of the {@link getPrecomputedHexValuesBuilder}
43
+ * function.
44
+ */
45
+ const getPrecomputedHexValues = getPrecomputedHexValuesBuilder();
46
+ /**
47
+ * Check if a value is a `Uint8Array`.
48
+ *
49
+ * @param value - The value to check.
50
+ * @returns Whether the value is a `Uint8Array`.
51
+ */
52
+ function isBytes(value) {
53
+ return value instanceof Uint8Array;
54
+ }
55
+ exports.isBytes = isBytes;
56
+ /**
57
+ * Assert that a value is a `Uint8Array`.
58
+ *
59
+ * @param value - The value to check.
60
+ * @throws If the value is not a `Uint8Array`.
61
+ */
62
+ function assertIsBytes(value) {
63
+ (0, assert_1.assert)(isBytes(value), 'Value must be a Uint8Array.');
64
+ }
65
+ exports.assertIsBytes = assertIsBytes;
66
+ /**
67
+ * Convert a `Uint8Array` to a hexadecimal string.
68
+ *
69
+ * @param bytes - The bytes to convert to a hexadecimal string.
70
+ * @returns The hexadecimal string.
71
+ */
72
+ function bytesToHex(bytes) {
73
+ assertIsBytes(bytes);
74
+ if (bytes.length === 0) {
75
+ return '0x';
76
+ }
77
+ const lookupTable = getPrecomputedHexValues();
78
+ const hexadecimal = new Array(bytes.length);
79
+ for (let i = 0; i < bytes.length; i++) {
80
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
81
+ hexadecimal[i] = lookupTable[bytes[i]];
82
+ }
83
+ return (0, hex_1.add0x)(hexadecimal.join(''));
84
+ }
85
+ exports.bytesToHex = bytesToHex;
86
+ /**
87
+ * Convert a `Uint8Array` to a `bigint`.
88
+ *
89
+ * To convert a `Uint8Array` to a `number` instead, use {@link bytesToNumber}.
90
+ * To convert a two's complement encoded `Uint8Array` to a `bigint`, use
91
+ * {@link bytesToSignedBigInt}.
92
+ *
93
+ * @param bytes - The bytes to convert to a `bigint`.
94
+ * @returns The `bigint`.
95
+ */
96
+ function bytesToBigInt(bytes) {
97
+ assertIsBytes(bytes);
98
+ const hexadecimal = bytesToHex(bytes);
99
+ return BigInt(hexadecimal);
100
+ }
101
+ exports.bytesToBigInt = bytesToBigInt;
102
+ /**
103
+ * Convert a `Uint8Array` to a signed `bigint`. This assumes that the bytes are
104
+ * encoded in two's complement.
105
+ *
106
+ * To convert a `Uint8Array` to an unsigned `bigint` instead, use
107
+ * {@link bytesToBigInt}.
108
+ *
109
+ * @see https://en.wikipedia.org/wiki/Two%27s_complement
110
+ * @param bytes - The bytes to convert to a signed `bigint`.
111
+ * @returns The signed `bigint`.
112
+ */
113
+ function bytesToSignedBigInt(bytes) {
114
+ assertIsBytes(bytes);
115
+ let value = BigInt(0);
116
+ for (const byte of bytes) {
117
+ // eslint-disable-next-line no-bitwise
118
+ value = (value << BigInt(8)) + BigInt(byte);
119
+ }
120
+ return BigInt.asIntN(bytes.length * 8, value);
121
+ }
122
+ exports.bytesToSignedBigInt = bytesToSignedBigInt;
123
+ /**
124
+ * Convert a `Uint8Array` to a `number`.
125
+ *
126
+ * To convert a `Uint8Array` to a `bigint` instead, use {@link bytesToBigInt}.
127
+ *
128
+ * @param bytes - The bytes to convert to a number.
129
+ * @returns The number.
130
+ * @throws If the resulting number is not a safe integer.
131
+ */
132
+ function bytesToNumber(bytes) {
133
+ assertIsBytes(bytes);
134
+ const bigint = bytesToBigInt(bytes);
135
+ (0, assert_1.assert)(bigint <= BigInt(Number.MAX_SAFE_INTEGER), 'Number is not a safe integer. Use `bytesToBigInt` instead.');
136
+ return Number(bigint);
137
+ }
138
+ exports.bytesToNumber = bytesToNumber;
139
+ /**
140
+ * Convert a UTF-8 encoded `Uint8Array` to a `string`.
141
+ *
142
+ * @param bytes - The bytes to convert to a string.
143
+ * @returns The string.
144
+ */
145
+ function bytesToString(bytes) {
146
+ assertIsBytes(bytes);
147
+ return new TextDecoder().decode(bytes);
148
+ }
149
+ exports.bytesToString = bytesToString;
150
+ /**
151
+ * Convert a `Uint8Array` to a base64 encoded string.
152
+ *
153
+ * @param bytes - The bytes to convert to a base64 encoded string.
154
+ * @returns The base64 encoded string.
155
+ */
156
+ function bytesToBase64(bytes) {
157
+ assertIsBytes(bytes);
158
+ return base_1.base64.encode(bytes);
159
+ }
160
+ exports.bytesToBase64 = bytesToBase64;
161
+ /**
162
+ * Convert a hexadecimal string to a `Uint8Array`. The string can optionally be
163
+ * prefixed with `0x`. It accepts even and odd length strings.
164
+ *
165
+ * If the value is "0x", an empty `Uint8Array` is returned.
166
+ *
167
+ * @param value - The hexadecimal string to convert to bytes.
168
+ * @returns The bytes as `Uint8Array`.
169
+ */
170
+ function hexToBytes(value) {
171
+ // "0x" is often used as empty byte array.
172
+ if (value?.toLowerCase?.() === '0x') {
173
+ return new Uint8Array();
174
+ }
175
+ (0, hex_1.assertIsHexString)(value);
176
+ // Remove the `0x` prefix if it exists, and pad the string to have an even
177
+ // number of characters.
178
+ const strippedValue = (0, hex_1.remove0x)(value).toLowerCase();
179
+ const normalizedValue = strippedValue.length % 2 === 0 ? strippedValue : `0${strippedValue}`;
180
+ const bytes = new Uint8Array(normalizedValue.length / 2);
181
+ for (let i = 0; i < bytes.length; i++) {
182
+ // While this is not the prettiest way to convert a hexadecimal string to a
183
+ // `Uint8Array`, it is a lot faster than using `parseInt` to convert each
184
+ // character.
185
+ const c1 = normalizedValue.charCodeAt(i * 2);
186
+ const c2 = normalizedValue.charCodeAt(i * 2 + 1);
187
+ const n1 = c1 -
188
+ (c1 < HEX_MAXIMUM_NUMBER_CHARACTER
189
+ ? HEX_MINIMUM_NUMBER_CHARACTER
190
+ : HEX_CHARACTER_OFFSET);
191
+ const n2 = c2 -
192
+ (c2 < HEX_MAXIMUM_NUMBER_CHARACTER
193
+ ? HEX_MINIMUM_NUMBER_CHARACTER
194
+ : HEX_CHARACTER_OFFSET);
195
+ bytes[i] = n1 * 16 + n2;
196
+ }
197
+ return bytes;
198
+ }
199
+ exports.hexToBytes = hexToBytes;
200
+ /**
201
+ * Convert a `bigint` to a `Uint8Array`.
202
+ *
203
+ * This assumes that the `bigint` is an unsigned integer. To convert a signed
204
+ * `bigint` instead, use {@link signedBigIntToBytes}.
205
+ *
206
+ * @param value - The bigint to convert to bytes.
207
+ * @returns The bytes as `Uint8Array`.
208
+ */
209
+ function bigIntToBytes(value) {
210
+ (0, assert_1.assert)(typeof value === 'bigint', 'Value must be a bigint.');
211
+ (0, assert_1.assert)(value >= BigInt(0), 'Value must be a non-negative bigint.');
212
+ const hexadecimal = value.toString(16);
213
+ return hexToBytes(hexadecimal);
214
+ }
215
+ exports.bigIntToBytes = bigIntToBytes;
216
+ /**
217
+ * Check if a `bigint` fits in a certain number of bytes.
218
+ *
219
+ * @param value - The `bigint` to check.
220
+ * @param bytes - The number of bytes.
221
+ * @returns Whether the `bigint` fits in the number of bytes.
222
+ */
223
+ function bigIntFits(value, bytes) {
224
+ (0, assert_1.assert)(bytes > 0);
225
+ /* eslint-disable no-bitwise */
226
+ const mask = value >> BigInt(31);
227
+ return !(((~value & mask) + (value & ~mask)) >> BigInt(bytes * 8 + ~0));
228
+ /* eslint-enable no-bitwise */
229
+ }
230
+ /**
231
+ * Convert a signed `bigint` to a `Uint8Array`. This uses two's complement
232
+ * encoding to represent negative numbers.
233
+ *
234
+ * To convert an unsigned `bigint` to a `Uint8Array` instead, use
235
+ * {@link bigIntToBytes}.
236
+ *
237
+ * @see https://en.wikipedia.org/wiki/Two%27s_complement
238
+ * @param value - The number to convert to bytes.
239
+ * @param byteLength - The length of the resulting `Uint8Array`. If the number
240
+ * is larger than the maximum value that can be represented by the given length,
241
+ * an error is thrown.
242
+ * @returns The bytes as `Uint8Array`.
243
+ */
244
+ function signedBigIntToBytes(value, byteLength) {
245
+ (0, assert_1.assert)(typeof value === 'bigint', 'Value must be a bigint.');
246
+ (0, assert_1.assert)(typeof byteLength === 'number', 'Byte length must be a number.');
247
+ (0, assert_1.assert)(byteLength > 0, 'Byte length must be greater than 0.');
248
+ (0, assert_1.assert)(bigIntFits(value, byteLength), 'Byte length is too small to represent the given value.');
249
+ // ESLint doesn't like mutating function parameters, so to avoid having to
250
+ // disable the rule, we create a new variable.
251
+ let numberValue = value;
252
+ const bytes = new Uint8Array(byteLength);
253
+ for (let i = 0; i < bytes.length; i++) {
254
+ bytes[i] = Number(BigInt.asUintN(8, numberValue));
255
+ // eslint-disable-next-line no-bitwise
256
+ numberValue >>= BigInt(8);
257
+ }
258
+ return bytes.reverse();
259
+ }
260
+ exports.signedBigIntToBytes = signedBigIntToBytes;
261
+ /**
262
+ * Convert a `number` to a `Uint8Array`.
263
+ *
264
+ * @param value - The number to convert to bytes.
265
+ * @returns The bytes as `Uint8Array`.
266
+ * @throws If the number is not a safe integer.
267
+ */
268
+ function numberToBytes(value) {
269
+ (0, assert_1.assert)(typeof value === 'number', 'Value must be a number.');
270
+ (0, assert_1.assert)(value >= 0, 'Value must be a non-negative number.');
271
+ (0, assert_1.assert)(Number.isSafeInteger(value), 'Value is not a safe integer. Use `bigIntToBytes` instead.');
272
+ const hexadecimal = value.toString(16);
273
+ return hexToBytes(hexadecimal);
274
+ }
275
+ exports.numberToBytes = numberToBytes;
276
+ /**
277
+ * Convert a `string` to a UTF-8 encoded `Uint8Array`.
278
+ *
279
+ * @param value - The string to convert to bytes.
280
+ * @returns The bytes as `Uint8Array`.
281
+ */
282
+ function stringToBytes(value) {
283
+ (0, assert_1.assert)(typeof value === 'string', 'Value must be a string.');
284
+ return new TextEncoder().encode(value);
285
+ }
286
+ exports.stringToBytes = stringToBytes;
287
+ /**
288
+ * Convert a base64 encoded string to a `Uint8Array`.
289
+ *
290
+ * @param value - The base64 encoded string to convert to bytes.
291
+ * @returns The bytes as `Uint8Array`.
292
+ */
293
+ function base64ToBytes(value) {
294
+ (0, assert_1.assert)(typeof value === 'string', 'Value must be a string.');
295
+ return base_1.base64.decode(value);
296
+ }
297
+ exports.base64ToBytes = base64ToBytes;
298
+ /**
299
+ * Convert a byte-like value to a `Uint8Array`. The value can be a `Uint8Array`,
300
+ * a `bigint`, a `number`, or a `string`.
301
+ *
302
+ * This will attempt to guess the type of the value based on its type and
303
+ * contents. For more control over the conversion, use the more specific
304
+ * conversion functions, such as {@link hexToBytes} or {@link stringToBytes}.
305
+ *
306
+ * If the value is a `string`, and it is prefixed with `0x`, it will be
307
+ * interpreted as a hexadecimal string. Otherwise, it will be interpreted as a
308
+ * UTF-8 string. To convert a hexadecimal string to bytes without interpreting
309
+ * it as a UTF-8 string, use {@link hexToBytes} instead.
310
+ *
311
+ * If the value is a `bigint`, it is assumed to be unsigned. To convert a signed
312
+ * `bigint` to bytes, use {@link signedBigIntToBytes} instead.
313
+ *
314
+ * If the value is a `Uint8Array`, it will be returned as-is.
315
+ *
316
+ * @param value - The value to convert to bytes.
317
+ * @returns The bytes as `Uint8Array`.
318
+ */
319
+ function valueToBytes(value) {
320
+ if (typeof value === 'bigint') {
321
+ return bigIntToBytes(value);
322
+ }
323
+ if (typeof value === 'number') {
324
+ return numberToBytes(value);
325
+ }
326
+ if (typeof value === 'string') {
327
+ if (value.startsWith('0x')) {
328
+ return hexToBytes(value);
329
+ }
330
+ return stringToBytes(value);
331
+ }
332
+ if (isBytes(value)) {
333
+ return value;
334
+ }
335
+ throw new TypeError(`Unsupported value type: "${typeof value}".`);
336
+ }
337
+ exports.valueToBytes = valueToBytes;
338
+ /**
339
+ * Concatenate multiple byte-like values into a single `Uint8Array`. The values
340
+ * can be `Uint8Array`, `bigint`, `number`, or `string`. This uses
341
+ * {@link valueToBytes} under the hood to convert each value to bytes. Refer to
342
+ * the documentation of that function for more information.
343
+ *
344
+ * @param values - The values to concatenate.
345
+ * @returns The concatenated bytes as `Uint8Array`.
346
+ */
347
+ function concatBytes(values) {
348
+ const normalizedValues = new Array(values.length);
349
+ let byteLength = 0;
350
+ for (let i = 0; i < values.length; i++) {
351
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
352
+ const value = valueToBytes(values[i]);
353
+ normalizedValues[i] = value;
354
+ byteLength += value.length;
355
+ }
356
+ const bytes = new Uint8Array(byteLength);
357
+ for (let i = 0, offset = 0; i < normalizedValues.length; i++) {
358
+ // While we could simply spread the values into an array and use
359
+ // `Uint8Array.from`, that is a lot slower than using `Uint8Array.set`.
360
+ bytes.set(normalizedValues[i], offset);
361
+ offset += normalizedValues[i].length;
362
+ }
363
+ return bytes;
364
+ }
365
+ exports.concatBytes = concatBytes;
366
+ /**
367
+ * Create a {@link DataView} from a {@link Uint8Array}. This is a convenience
368
+ * function that avoids having to create a {@link DataView} manually, which
369
+ * requires passing the `byteOffset` and `byteLength` parameters every time.
370
+ *
371
+ * Not passing the `byteOffset` and `byteLength` parameters can result in
372
+ * unexpected behavior when the {@link Uint8Array} is a view of a larger
373
+ * {@link ArrayBuffer}, e.g., when using {@link Uint8Array.subarray}.
374
+ *
375
+ * This function also supports Node.js {@link Buffer}s.
376
+ *
377
+ * @example
378
+ * ```typescript
379
+ * const bytes = new Uint8Array([1, 2, 3]);
380
+ *
381
+ * // This is equivalent to:
382
+ * // const dataView = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
383
+ * const dataView = createDataView(bytes);
384
+ * ```
385
+ * @param bytes - The bytes to create the {@link DataView} from.
386
+ * @returns The {@link DataView}.
387
+ */
388
+ function createDataView(bytes) {
389
+ // To maintain compatibility with Node.js, we need to check if the bytes are
390
+ // a Buffer. If so, we need to slice the buffer to get the underlying
391
+ // ArrayBuffer.
392
+ // eslint-disable-next-line no-restricted-globals
393
+ if (typeof Buffer !== 'undefined' && bytes instanceof Buffer) {
394
+ const buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
395
+ return new DataView(buffer);
396
+ }
397
+ return new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
398
+ }
399
+ exports.createDataView = createDataView;
400
+ //# sourceMappingURL=bytes.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/hex.ts","../src/bytes.ts"],"names":[],"mappings":";;;;;AAAA,SAAS,cAAc,iBAAiB;AAExC,SAAS,IAAI,SAAS,cAAc;;;ACFpC,SAAS,cAAc;AAOvB,IAAM,+BAA+B;AAGrC,IAAM,+BAA+B;AACrC,IAAM,uBAAuB;AAkB7B,SAAS,iCAAiD;AAKxD,QAAM,cAAwB,CAAC;AAE/B,SAAO,MAAM;AACX,QAAI,YAAY,WAAW,GAAG;AAC5B,eAAS,IAAI,GAAG,IAAI,KAAK,KAAK;AAC5B,oBAAY,KAAK,EAAE,SAAS,EAAE,EAAE,SAAS,GAAG,GAAG,CAAC;AAAA,MAClD;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AACF;AAMA,IAAM,0BAA0B,+BAA+B;AAQxD,SAAS,QAAQ,OAAqC;AAC3D,SAAO,iBAAiB;AAC1B;AAQO,SAAS,cAAc,OAA6C;AACzE,SAAO,QAAQ,KAAK,GAAG,6BAA6B;AACtD;AAQO,SAAS,WAAW,OAAwB;AACjD,gBAAc,KAAK;AAEnB,MAAI,MAAM,WAAW,GAAG;AACtB,WAAO;AAAA,EACT;AAEA,QAAM,cAAc,wBAAwB;AAC5C,QAAM,cAAc,IAAI,MAAM,MAAM,MAAM;AAE1C,WAAS,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK;AAErC,gBAAY,CAAC,IAAI,YAAY,MAAM,CAAC,CAAE;AAAA,EACxC;AAEA,SAAO,MAAM,YAAY,KAAK,EAAE,CAAC;AACnC;AAYO,SAAS,cAAc,OAA2B;AACvD,gBAAc,KAAK;AAEnB,QAAM,cAAc,WAAW,KAAK;AACpC,SAAO,OAAO,WAAW;AAC3B;AAaO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W,IAAI,KAAK,YAAY,WAAW,IAAI,GAAG;AAChE,WAAO,YAAY,UAAU,CAAC;AAAA,EAChC;AAEA,SAAO;AACT","sourcesContent":["import { keccak_256 as keccak256 } from '@noble/hashes/sha3';\nimport type { Struct } from 'superstruct';\nimport { is, pattern, string } from 'superstruct';\n\nimport { assert } from './assert';\nimport { bytesToHex } from './bytes';\n\nexport type Hex = `0x${string}`;\n\nexport const HexStruct = pattern(string(), /^(?:0x)?[0-9a-f]+$/iu);\nexport const StrictHexStruct = pattern(string(), /^0x[0-9a-f]+$/iu) as Struct<\n Hex,\n null\n>;\nexport const HexAddressStruct = pattern(\n string(),\n /^0x[0-9a-f]{40}$/u,\n) as Struct<Hex, null>;\nexport const HexChecksumAddressStruct = pattern(\n string(),\n /^0x[0-9a-fA-F]{40}$/u,\n) as Struct<Hex, null>;\n\n/**\n * Check if a string is a valid hex string.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid hex string.\n */\nexport function isHexString(value: unknown): value is string {\n return is(value, HexStruct);\n}\n\n/**\n * Strictly check if a string is a valid hex string. A valid hex string must\n * start with the \"0x\"-prefix.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid hex string.\n */\nexport function isStrictHexString(value: unknown): value is Hex {\n return is(value, StrictHexStruct);\n}\n\n/**\n * Assert that a value is a valid hex string.\n *\n * @param value - The value to check.\n * @throws If the value is not a valid hex string.\n */\nexport function assertIsHexString(value: unknown): asserts value is string {\n assert(isHexString(value), 'Value must be a hexadecimal string.');\n}\n\n/**\n * Assert that a value is a valid hex string. A valid hex string must start with\n * the \"0x\"-prefix.\n *\n * @param value - The value to check.\n * @throws If the value is not a valid hex string.\n */\nexport function assertIsStrictHexString(value: unknown): asserts value is Hex {\n assert(\n isStrictHexString(value),\n 'Value must be a hexadecimal string, starting with \"0x\".',\n );\n}\n\n/**\n * Validate that the passed prefixed hex string is an all-lowercase\n * hex address, or a valid mixed-case checksum address.\n *\n * @param possibleAddress - Input parameter to check against.\n * @returns Whether or not the input is a valid hex address.\n */\nexport function isValidHexAddress(possibleAddress: Hex) {\n return (\n is(possibleAddress, HexAddressStruct) ||\n isValidChecksumAddress(possibleAddress)\n );\n}\n\n/**\n * Encode a passed hex string as an ERC-55 mixed-case checksum address.\n *\n * @param address - The hex address to encode.\n * @returns The address encoded according to ERC-55.\n * @see https://eips.ethereum.org/EIPS/eip-55\n */\nexport function getChecksumAddress(address: Hex) {\n assert(is(address, HexChecksumAddressStruct), 'Invalid hex address.');\n const unPrefixed = remove0x(address.toLowerCase());\n const unPrefixedHash = remove0x(bytesToHex(keccak256(unPrefixed)));\n return `0x${unPrefixed\n .split('')\n .map((character, nibbleIndex) => {\n const hashCharacter = unPrefixedHash[nibbleIndex];\n assert(is(hashCharacter, string()), 'Hash shorter than address.');\n return parseInt(hashCharacter, 16) > 7\n ? character.toUpperCase()\n : character;\n })\n .join('')}`;\n}\n\n/**\n * Validate that the passed hex string is a valid ERC-55 mixed-case\n * checksum address.\n *\n * @param possibleChecksum - The hex address to check.\n * @returns True if the address is a checksum address.\n */\nexport function isValidChecksumAddress(possibleChecksum: Hex) {\n if (!is(possibleChecksum, HexChecksumAddressStruct)) {\n return false;\n }\n\n return getChecksumAddress(possibleChecksum) === possibleChecksum;\n}\n\n/**\n * Add the `0x`-prefix to a hexadecimal string. If the string already has the\n * prefix, it is returned as-is.\n *\n * @param hexadecimal - The hexadecimal string to add the prefix to.\n * @returns The prefixed hexadecimal string.\n */\nexport function add0x(hexadecimal: string): Hex {\n if (hexadecimal.startsWith('0x')) {\n return hexadecimal as Hex;\n }\n\n if (hexadecimal.startsWith('0X')) {\n return `0x${hexadecimal.substring(2)}`;\n }\n\n return `0x${hexadecimal}`;\n}\n\n/**\n * Remove the `0x`-prefix from a hexadecimal string. If the string doesn't have\n * the prefix, it is returned as-is.\n *\n * @param hexadecimal - The hexadecimal string to remove the prefix from.\n * @returns The un-prefixed hexadecimal string.\n */\nexport function remove0x(hexadecimal: string): string {\n if (hexadecimal.startsWith('0x') || hexadecimal.startsWith('0X')) {\n return hexadecimal.substring(2);\n }\n\n return hexadecimal;\n}\n","import { base64 } from '@scure/base';\n\nimport { assert } from './assert';\nimport type { Hex } from './hex';\nimport { add0x, assertIsHexString, remove0x } from './hex';\n\n// '0'.charCodeAt(0) === 48\nconst HEX_MINIMUM_NUMBER_CHARACTER = 48;\n\n// '9'.charCodeAt(0) === 57\nconst HEX_MAXIMUM_NUMBER_CHARACTER = 58;\nconst HEX_CHARACTER_OFFSET = 87;\n\nexport type Bytes = bigint | number | string | Uint8Array;\n\n/**\n * Memoized function that returns an array to be used as a lookup table for\n * converting bytes to hexadecimal values.\n *\n * The array is created lazily and then cached for future use. The benefit of\n * this approach is that the performance of converting bytes to hex is much\n * better than if we were to call `toString(16)` on each byte.\n *\n * The downside is that the array is created once and then never garbage\n * collected. This is not a problem in practice because the array is only 256\n * elements long.\n *\n * @returns A function that returns the lookup table.\n */\nfunction getPrecomputedHexValuesBuilder(): () => string[] {\n // To avoid issues with tree shaking, we need to use a function to return the\n // array. This is because the array is only used in the `bytesToHex` function\n // and if we were to use a global variable, the array might be removed by the\n // tree shaker.\n const lookupTable: string[] = [];\n\n return () => {\n if (lookupTable.length === 0) {\n for (let i = 0; i < 256; i++) {\n lookupTable.push(i.toString(16).padStart(2, '0'));\n }\n }\n\n return lookupTable;\n };\n}\n\n/**\n * Function implementation of the {@link getPrecomputedHexValuesBuilder}\n * function.\n */\nconst getPrecomputedHexValues = getPrecomputedHexValuesBuilder();\n\n/**\n * Check if a value is a `Uint8Array`.\n *\n * @param value - The value to check.\n * @returns Whether the value is a `Uint8Array`.\n */\nexport function isBytes(value: unknown): value is Uint8Array {\n return value instanceof Uint8Array;\n}\n\n/**\n * Assert that a value is a `Uint8Array`.\n *\n * @param value - The value to check.\n * @throws If the value is not a `Uint8Array`.\n */\nexport function assertIsBytes(value: unknown): asserts value is Uint8Array {\n assert(isBytes(value), 'Value must be a Uint8Array.');\n}\n\n/**\n * Convert a `Uint8Array` to a hexadecimal string.\n *\n * @param bytes - The bytes to convert to a hexadecimal string.\n * @returns The hexadecimal string.\n */\nexport function bytesToHex(bytes: Uint8Array): Hex {\n assertIsBytes(bytes);\n\n if (bytes.length === 0) {\n return '0x';\n }\n\n const lookupTable = getPrecomputedHexValues();\n const hexadecimal = new Array(bytes.length);\n\n for (let i = 0; i < bytes.length; i++) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n hexadecimal[i] = lookupTable[bytes[i]!];\n }\n\n return add0x(hexadecimal.join(''));\n}\n\n/**\n * Convert a `Uint8Array` to a `bigint`.\n *\n * To convert a `Uint8Array` to a `number` instead, use {@link bytesToNumber}.\n * To convert a two's complement encoded `Uint8Array` to a `bigint`, use\n * {@link bytesToSignedBigInt}.\n *\n * @param bytes - The bytes to convert to a `bigint`.\n * @returns The `bigint`.\n */\nexport function bytesToBigInt(bytes: Uint8Array): bigint {\n assertIsBytes(bytes);\n\n const hexadecimal = bytesToHex(bytes);\n return BigInt(hexadecimal);\n}\n\n/**\n * Convert a `Uint8Array` to a signed `bigint`. This assumes that the bytes are\n * encoded in two's complement.\n *\n * To convert a `Uint8Array` to an unsigned `bigint` instead, use\n * {@link bytesToBigInt}.\n *\n * @see https://en.wikipedia.org/wiki/Two%27s_complement\n * @param bytes - The bytes to convert to a signed `bigint`.\n * @returns The signed `bigint`.\n */\nexport function bytesToSignedBigInt(bytes: Uint8Array): bigint {\n assertIsBytes(bytes);\n\n let value = BigInt(0);\n for (const byte of bytes) {\n // eslint-disable-next-line no-bitwise\n value = (value << BigInt(8)) + BigInt(byte);\n }\n\n return BigInt.asIntN(bytes.length * 8, value);\n}\n\n/**\n * Convert a `Uint8Array` to a `number`.\n *\n * To convert a `Uint8Array` to a `bigint` instead, use {@link bytesToBigInt}.\n *\n * @param bytes - The bytes to convert to a number.\n * @returns The number.\n * @throws If the resulting number is not a safe integer.\n */\nexport function bytesToNumber(bytes: Uint8Array): number {\n assertIsBytes(bytes);\n\n const bigint = bytesToBigInt(bytes);\n\n assert(\n bigint <= BigInt(Number.MAX_SAFE_INTEGER),\n 'Number is not a safe integer. Use `bytesToBigInt` instead.',\n );\n\n return Number(bigint);\n}\n\n/**\n * Convert a UTF-8 encoded `Uint8Array` to a `string`.\n *\n * @param bytes - The bytes to convert to a string.\n * @returns The string.\n */\nexport function bytesToString(bytes: Uint8Array): string {\n assertIsBytes(bytes);\n\n return new TextDecoder().decode(bytes);\n}\n\n/**\n * Convert a `Uint8Array` to a base64 encoded string.\n *\n * @param bytes - The bytes to convert to a base64 encoded string.\n * @returns The base64 encoded string.\n */\nexport function bytesToBase64(bytes: Uint8Array): string {\n assertIsBytes(bytes);\n\n return base64.encode(bytes);\n}\n\n/**\n * Convert a hexadecimal string to a `Uint8Array`. The string can optionally be\n * prefixed with `0x`. It accepts even and odd length strings.\n *\n * If the value is \"0x\", an empty `Uint8Array` is returned.\n *\n * @param value - The hexadecimal string to convert to bytes.\n * @returns The bytes as `Uint8Array`.\n */\nexport function hexToBytes(value: string): Uint8Array {\n // \"0x\" is often used as empty byte array.\n if (value?.toLowerCase?.() === '0x') {\n return new Uint8Array();\n }\n\n assertIsHexString(value);\n\n // Remove the `0x` prefix if it exists, and pad the string to have an even\n // number of characters.\n const strippedValue = remove0x(value).toLowerCase();\n const normalizedValue =\n strippedValue.length % 2 === 0 ? strippedValue : `0${strippedValue}`;\n const bytes = new Uint8Array(normalizedValue.length / 2);\n\n for (let i = 0; i < bytes.length; i++) {\n // While this is not the prettiest way to convert a hexadecimal string to a\n // `Uint8Array`, it is a lot faster than using `parseInt` to convert each\n // character.\n const c1 = normalizedValue.charCodeAt(i * 2);\n const c2 = normalizedValue.charCodeAt(i * 2 + 1);\n const n1 =\n c1 -\n (c1 < HEX_MAXIMUM_NUMBER_CHARACTER\n ? HEX_MINIMUM_NUMBER_CHARACTER\n : HEX_CHARACTER_OFFSET);\n const n2 =\n c2 -\n (c2 < HEX_MAXIMUM_NUMBER_CHARACTER\n ? HEX_MINIMUM_NUMBER_CHARACTER\n : HEX_CHARACTER_OFFSET);\n\n bytes[i] = n1 * 16 + n2;\n }\n\n return bytes;\n}\n\n/**\n * Convert a `bigint` to a `Uint8Array`.\n *\n * This assumes that the `bigint` is an unsigned integer. To convert a signed\n * `bigint` instead, use {@link signedBigIntToBytes}.\n *\n * @param value - The bigint to convert to bytes.\n * @returns The bytes as `Uint8Array`.\n */\nexport function bigIntToBytes(value: bigint): Uint8Array {\n assert(typeof value === 'bigint', 'Value must be a bigint.');\n assert(value >= BigInt(0), 'Value must be a non-negative bigint.');\n\n const hexadecimal = value.toString(16);\n return hexToBytes(hexadecimal);\n}\n\n/**\n * Check if a `bigint` fits in a certain number of bytes.\n *\n * @param value - The `bigint` to check.\n * @param bytes - The number of bytes.\n * @returns Whether the `bigint` fits in the number of bytes.\n */\nfunction bigIntFits(value: bigint, bytes: number): boolean {\n assert(bytes > 0);\n\n /* eslint-disable no-bitwise */\n const mask = value >> BigInt(31);\n return !(((~value & mask) + (value & ~mask)) >> BigInt(bytes * 8 + ~0));\n /* eslint-enable no-bitwise */\n}\n\n/**\n * Convert a signed `bigint` to a `Uint8Array`. This uses two's complement\n * encoding to represent negative numbers.\n *\n * To convert an unsigned `bigint` to a `Uint8Array` instead, use\n * {@link bigIntToBytes}.\n *\n * @see https://en.wikipedia.org/wiki/Two%27s_complement\n * @param value - The number to convert to bytes.\n * @param byteLength - The length of the resulting `Uint8Array`. If the number\n * is larger than the maximum value that can be represented by the given length,\n * an error is thrown.\n * @returns The bytes as `Uint8Array`.\n */\nexport function signedBigIntToBytes(\n value: bigint,\n byteLength: number,\n): Uint8Array {\n assert(typeof value === 'bigint', 'Value must be a bigint.');\n assert(typeof byteLength === 'number', 'Byte length must be a number.');\n assert(byteLength > 0, 'Byte length must be greater than 0.');\n assert(\n bigIntFits(value, byteLength),\n 'Byte length is too small to represent the given value.',\n );\n\n // ESLint doesn't like mutating function parameters, so to avoid having to\n // disable the rule, we create a new variable.\n let numberValue = value;\n const bytes = new Uint8Array(byteLength);\n\n for (let i = 0; i < bytes.length; i++) {\n bytes[i] = Number(BigInt.asUintN(8, numberValue));\n // eslint-disable-next-line no-bitwise\n numberValue >>= BigInt(8);\n }\n\n return bytes.reverse();\n}\n\n/**\n * Convert a `number` to a `Uint8Array`.\n *\n * @param value - The number to convert to bytes.\n * @returns The bytes as `Uint8Array`.\n * @throws If the number is not a safe integer.\n */\nexport function numberToBytes(value: number): Uint8Array {\n assert(typeof value === 'number', 'Value must be a number.');\n assert(value >= 0, 'Value must be a non-negative number.');\n assert(\n Number.isSafeInteger(value),\n 'Value is not a safe integer. Use `bigIntToBytes` instead.',\n );\n\n const hexadecimal = value.toString(16);\n return hexToBytes(hexadecimal);\n}\n\n/**\n * Convert a `string` to a UTF-8 encoded `Uint8Array`.\n *\n * @param value - The string to convert to bytes.\n * @returns The bytes as `Uint8Array`.\n */\nexport function stringToBytes(value: string): Uint8Array {\n assert(typeof value === 'string', 'Value must be a string.');\n\n return new TextEncoder().encode(value);\n}\n\n/**\n * Convert a base64 encoded string to a `Uint8Array`.\n *\n * @param value - The base64 encoded string to convert to bytes.\n * @returns The bytes as `Uint8Array`.\n */\nexport function base64ToBytes(value: string): Uint8Array {\n assert(typeof value === 'string', 'Value must be a string.');\n\n return base64.decode(value);\n}\n\n/**\n * Convert a byte-like value to a `Uint8Array`. The value can be a `Uint8Array`,\n * a `bigint`, a `number`, or a `string`.\n *\n * This will attempt to guess the type of the value based on its type and\n * contents. For more control over the conversion, use the more specific\n * conversion functions, such as {@link hexToBytes} or {@link stringToBytes}.\n *\n * If the value is a `string`, and it is prefixed with `0x`, it will be\n * interpreted as a hexadecimal string. Otherwise, it will be interpreted as a\n * UTF-8 string. To convert a hexadecimal string to bytes without interpreting\n * it as a UTF-8 string, use {@link hexToBytes} instead.\n *\n * If the value is a `bigint`, it is assumed to be unsigned. To convert a signed\n * `bigint` to bytes, use {@link signedBigIntToBytes} instead.\n *\n * If the value is a `Uint8Array`, it will be returned as-is.\n *\n * @param value - The value to convert to bytes.\n * @returns The bytes as `Uint8Array`.\n */\nexport function valueToBytes(value: Bytes): Uint8Array {\n if (typeof value === 'bigint') {\n return bigIntToBytes(value);\n }\n\n if (typeof value === 'number') {\n return numberToBytes(value);\n }\n\n if (typeof value === 'string') {\n if (value.startsWith('0x')) {\n return hexToBytes(value);\n }\n\n return stringToBytes(value);\n }\n\n if (isBytes(value)) {\n return value;\n }\n\n throw new TypeError(`Unsupported value type: \"${typeof value}\".`);\n}\n\n/**\n * Concatenate multiple byte-like values into a single `Uint8Array`. The values\n * can be `Uint8Array`, `bigint`, `number`, or `string`. This uses\n * {@link valueToBytes} under the hood to convert each value to bytes. Refer to\n * the documentation of that function for more information.\n *\n * @param values - The values to concatenate.\n * @returns The concatenated bytes as `Uint8Array`.\n */\nexport function concatBytes(values: Bytes[]): Uint8Array {\n const normalizedValues = new Array(values.length);\n let byteLength = 0;\n\n for (let i = 0; i < values.length; i++) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const value = valueToBytes(values[i]!);\n\n normalizedValues[i] = value;\n byteLength += value.length;\n }\n\n const bytes = new Uint8Array(byteLength);\n for (let i = 0, offset = 0; i < normalizedValues.length; i++) {\n // While we could simply spread the values into an array and use\n // `Uint8Array.from`, that is a lot slower than using `Uint8Array.set`.\n bytes.set(normalizedValues[i], offset);\n offset += normalizedValues[i].length;\n }\n\n return bytes;\n}\n\n/**\n * Create a {@link DataView} from a {@link Uint8Array}. This is a convenience\n * function that avoids having to create a {@link DataView} manually, which\n * requires passing the `byteOffset` and `byteLength` parameters every time.\n *\n * Not passing the `byteOffset` and `byteLength` parameters can result in\n * unexpected behavior when the {@link Uint8Array} is a view of a larger\n * {@link ArrayBuffer}, e.g., when using {@link Uint8Array.subarray}.\n *\n * This function also supports Node.js {@link Buffer}s.\n *\n * @example\n * ```typescript\n * const bytes = new Uint8Array([1, 2, 3]);\n *\n * // This is equivalent to:\n * // const dataView = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);\n * const dataView = createDataView(bytes);\n * ```\n * @param bytes - The bytes to create the {@link DataView} from.\n * @returns The {@link DataView}.\n */\nexport function createDataView(bytes: Uint8Array): DataView {\n // To maintain compatibility with Node.js, we need to check if the bytes are\n // a Buffer. If so, we need to slice the buffer to get the underlying\n // ArrayBuffer.\n // eslint-disable-next-line no-restricted-globals\n if (typeof Buffer !== 'undefined' && bytes instanceof Buffer) {\n const buffer = bytes.buffer.slice(\n bytes.byteOffset,\n bytes.byteOffset + bytes.byteLength,\n );\n\n return new DataView(buffer);\n }\n\n return new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);\n}\n"]}
1
+ {"version":3,"file":"bytes.cjs","sourceRoot":"","sources":["../src/bytes.ts"],"names":[],"mappings":";;;AAAA,sCAAqC;AAErC,yCAAkC;AAElC,mCAA2D;AAE3D,2BAA2B;AAC3B,MAAM,4BAA4B,GAAG,EAAE,CAAC;AAExC,2BAA2B;AAC3B,MAAM,4BAA4B,GAAG,EAAE,CAAC;AACxC,MAAM,oBAAoB,GAAG,EAAE,CAAC;AAIhC;;;;;;;;;;;;;GAaG;AACH,SAAS,8BAA8B;IACrC,6EAA6E;IAC7E,6EAA6E;IAC7E,6EAA6E;IAC7E,eAAe;IACf,MAAM,WAAW,GAAa,EAAE,CAAC;IAEjC,OAAO,GAAG,EAAE;QACV,IAAI,WAAW,CAAC,MAAM,KAAK,CAAC,EAAE;YAC5B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,EAAE,CAAC,EAAE,EAAE;gBAC5B,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC;aACnD;SACF;QAED,OAAO,WAAW,CAAC;IACrB,CAAC,CAAC;AACJ,CAAC;AAED;;;GAGG;AACH,MAAM,uBAAuB,GAAG,8BAA8B,EAAE,CAAC;AAEjE;;;;;GAKG;AACH,SAAgB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AAK,WAAW,IAAI,KAAK,YAAY,MAAM,EAAE;QAC5D,MAAM,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC,KAAK,CAC/B,KAAK,CAAC,UAAU,EAChB,KAAK,CAAC,UAAU,GAAG,KAAK,CAAC,UAAU,CACpC,CAAC;QAEF,OAAO,IAAI,QAAQ,CAAC,MAAM,CAAC,CAAC;KAC7B;IAED,OAAO,IAAI,QAAQ,CAAC,KAAK,CAAC,MAAM,EAAE,KAAK,CAAC,UAAU,EAAE,KAAK,CAAC,UAAU,CAAC,CAAC;AACxE,CAAC;AAfD,wCAeC","sourcesContent":["import { base64 } from '@scure/base';\n\nimport { assert } from './assert';\nimport type { Hex } from './hex';\nimport { add0x, assertIsHexString, remove0x } from './hex';\n\n// '0'.charCodeAt(0) === 48\nconst HEX_MINIMUM_NUMBER_CHARACTER = 48;\n\n// '9'.charCodeAt(0) === 57\nconst HEX_MAXIMUM_NUMBER_CHARACTER = 58;\nconst HEX_CHARACTER_OFFSET = 87;\n\nexport type Bytes = bigint | number | string | Uint8Array;\n\n/**\n * Memoized function that returns an array to be used as a lookup table for\n * converting bytes to hexadecimal values.\n *\n * The array is created lazily and then cached for future use. The benefit of\n * this approach is that the performance of converting bytes to hex is much\n * better than if we were to call `toString(16)` on each byte.\n *\n * The downside is that the array is created once and then never garbage\n * collected. This is not a problem in practice because the array is only 256\n * elements long.\n *\n * @returns A function that returns the lookup table.\n */\nfunction getPrecomputedHexValuesBuilder(): () => string[] {\n // To avoid issues with tree shaking, we need to use a function to return the\n // array. This is because the array is only used in the `bytesToHex` function\n // and if we were to use a global variable, the array might be removed by the\n // tree shaker.\n const lookupTable: string[] = [];\n\n return () => {\n if (lookupTable.length === 0) {\n for (let i = 0; i < 256; i++) {\n lookupTable.push(i.toString(16).padStart(2, '0'));\n }\n }\n\n return lookupTable;\n };\n}\n\n/**\n * Function implementation of the {@link getPrecomputedHexValuesBuilder}\n * function.\n */\nconst getPrecomputedHexValues = getPrecomputedHexValuesBuilder();\n\n/**\n * Check if a value is a `Uint8Array`.\n *\n * @param value - The value to check.\n * @returns Whether the value is a `Uint8Array`.\n */\nexport function isBytes(value: unknown): value is Uint8Array {\n return value instanceof Uint8Array;\n}\n\n/**\n * Assert that a value is a `Uint8Array`.\n *\n * @param value - The value to check.\n * @throws If the value is not a `Uint8Array`.\n */\nexport function assertIsBytes(value: unknown): asserts value is Uint8Array {\n assert(isBytes(value), 'Value must be a Uint8Array.');\n}\n\n/**\n * Convert a `Uint8Array` to a hexadecimal string.\n *\n * @param bytes - The bytes to convert to a hexadecimal string.\n * @returns The hexadecimal string.\n */\nexport function bytesToHex(bytes: Uint8Array): Hex {\n assertIsBytes(bytes);\n\n if (bytes.length === 0) {\n return '0x';\n }\n\n const lookupTable = getPrecomputedHexValues();\n const hexadecimal = new Array(bytes.length);\n\n for (let i = 0; i < bytes.length; i++) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n hexadecimal[i] = lookupTable[bytes[i]!];\n }\n\n return add0x(hexadecimal.join(''));\n}\n\n/**\n * Convert a `Uint8Array` to a `bigint`.\n *\n * To convert a `Uint8Array` to a `number` instead, use {@link bytesToNumber}.\n * To convert a two's complement encoded `Uint8Array` to a `bigint`, use\n * {@link bytesToSignedBigInt}.\n *\n * @param bytes - The bytes to convert to a `bigint`.\n * @returns The `bigint`.\n */\nexport function bytesToBigInt(bytes: Uint8Array): bigint {\n assertIsBytes(bytes);\n\n const hexadecimal = bytesToHex(bytes);\n return BigInt(hexadecimal);\n}\n\n/**\n * Convert a `Uint8Array` to a signed `bigint`. This assumes that the bytes are\n * encoded in two's complement.\n *\n * To convert a `Uint8Array` to an unsigned `bigint` instead, use\n * {@link bytesToBigInt}.\n *\n * @see https://en.wikipedia.org/wiki/Two%27s_complement\n * @param bytes - The bytes to convert to a signed `bigint`.\n * @returns The signed `bigint`.\n */\nexport function bytesToSignedBigInt(bytes: Uint8Array): bigint {\n assertIsBytes(bytes);\n\n let value = BigInt(0);\n for (const byte of bytes) {\n // eslint-disable-next-line no-bitwise\n value = (value << BigInt(8)) + BigInt(byte);\n }\n\n return BigInt.asIntN(bytes.length * 8, value);\n}\n\n/**\n * Convert a `Uint8Array` to a `number`.\n *\n * To convert a `Uint8Array` to a `bigint` instead, use {@link bytesToBigInt}.\n *\n * @param bytes - The bytes to convert to a number.\n * @returns The number.\n * @throws If the resulting number is not a safe integer.\n */\nexport function bytesToNumber(bytes: Uint8Array): number {\n assertIsBytes(bytes);\n\n const bigint = bytesToBigInt(bytes);\n\n assert(\n bigint <= BigInt(Number.MAX_SAFE_INTEGER),\n 'Number is not a safe integer. Use `bytesToBigInt` instead.',\n );\n\n return Number(bigint);\n}\n\n/**\n * Convert a UTF-8 encoded `Uint8Array` to a `string`.\n *\n * @param bytes - The bytes to convert to a string.\n * @returns The string.\n */\nexport function bytesToString(bytes: Uint8Array): string {\n assertIsBytes(bytes);\n\n return new TextDecoder().decode(bytes);\n}\n\n/**\n * Convert a `Uint8Array` to a base64 encoded string.\n *\n * @param bytes - The bytes to convert to a base64 encoded string.\n * @returns The base64 encoded string.\n */\nexport function bytesToBase64(bytes: Uint8Array): string {\n assertIsBytes(bytes);\n\n return base64.encode(bytes);\n}\n\n/**\n * Convert a hexadecimal string to a `Uint8Array`. The string can optionally be\n * prefixed with `0x`. It accepts even and odd length strings.\n *\n * If the value is \"0x\", an empty `Uint8Array` is returned.\n *\n * @param value - The hexadecimal string to convert to bytes.\n * @returns The bytes as `Uint8Array`.\n */\nexport function hexToBytes(value: string): Uint8Array {\n // \"0x\" is often used as empty byte array.\n if (value?.toLowerCase?.() === '0x') {\n return new Uint8Array();\n }\n\n assertIsHexString(value);\n\n // Remove the `0x` prefix if it exists, and pad the string to have an even\n // number of characters.\n const strippedValue = remove0x(value).toLowerCase();\n const normalizedValue =\n strippedValue.length % 2 === 0 ? strippedValue : `0${strippedValue}`;\n const bytes = new Uint8Array(normalizedValue.length / 2);\n\n for (let i = 0; i < bytes.length; i++) {\n // While this is not the prettiest way to convert a hexadecimal string to a\n // `Uint8Array`, it is a lot faster than using `parseInt` to convert each\n // character.\n const c1 = normalizedValue.charCodeAt(i * 2);\n const c2 = normalizedValue.charCodeAt(i * 2 + 1);\n const n1 =\n c1 -\n (c1 < HEX_MAXIMUM_NUMBER_CHARACTER\n ? HEX_MINIMUM_NUMBER_CHARACTER\n : HEX_CHARACTER_OFFSET);\n const n2 =\n c2 -\n (c2 < HEX_MAXIMUM_NUMBER_CHARACTER\n ? HEX_MINIMUM_NUMBER_CHARACTER\n : HEX_CHARACTER_OFFSET);\n\n bytes[i] = n1 * 16 + n2;\n }\n\n return bytes;\n}\n\n/**\n * Convert a `bigint` to a `Uint8Array`.\n *\n * This assumes that the `bigint` is an unsigned integer. To convert a signed\n * `bigint` instead, use {@link signedBigIntToBytes}.\n *\n * @param value - The bigint to convert to bytes.\n * @returns The bytes as `Uint8Array`.\n */\nexport function bigIntToBytes(value: bigint): Uint8Array {\n assert(typeof value === 'bigint', 'Value must be a bigint.');\n assert(value >= BigInt(0), 'Value must be a non-negative bigint.');\n\n const hexadecimal = value.toString(16);\n return hexToBytes(hexadecimal);\n}\n\n/**\n * Check if a `bigint` fits in a certain number of bytes.\n *\n * @param value - The `bigint` to check.\n * @param bytes - The number of bytes.\n * @returns Whether the `bigint` fits in the number of bytes.\n */\nfunction bigIntFits(value: bigint, bytes: number): boolean {\n assert(bytes > 0);\n\n /* eslint-disable no-bitwise */\n const mask = value >> BigInt(31);\n return !(((~value & mask) + (value & ~mask)) >> BigInt(bytes * 8 + ~0));\n /* eslint-enable no-bitwise */\n}\n\n/**\n * Convert a signed `bigint` to a `Uint8Array`. This uses two's complement\n * encoding to represent negative numbers.\n *\n * To convert an unsigned `bigint` to a `Uint8Array` instead, use\n * {@link bigIntToBytes}.\n *\n * @see https://en.wikipedia.org/wiki/Two%27s_complement\n * @param value - The number to convert to bytes.\n * @param byteLength - The length of the resulting `Uint8Array`. If the number\n * is larger than the maximum value that can be represented by the given length,\n * an error is thrown.\n * @returns The bytes as `Uint8Array`.\n */\nexport function signedBigIntToBytes(\n value: bigint,\n byteLength: number,\n): Uint8Array {\n assert(typeof value === 'bigint', 'Value must be a bigint.');\n assert(typeof byteLength === 'number', 'Byte length must be a number.');\n assert(byteLength > 0, 'Byte length must be greater than 0.');\n assert(\n bigIntFits(value, byteLength),\n 'Byte length is too small to represent the given value.',\n );\n\n // ESLint doesn't like mutating function parameters, so to avoid having to\n // disable the rule, we create a new variable.\n let numberValue = value;\n const bytes = new Uint8Array(byteLength);\n\n for (let i = 0; i < bytes.length; i++) {\n bytes[i] = Number(BigInt.asUintN(8, numberValue));\n // eslint-disable-next-line no-bitwise\n numberValue >>= BigInt(8);\n }\n\n return bytes.reverse();\n}\n\n/**\n * Convert a `number` to a `Uint8Array`.\n *\n * @param value - The number to convert to bytes.\n * @returns The bytes as `Uint8Array`.\n * @throws If the number is not a safe integer.\n */\nexport function numberToBytes(value: number): Uint8Array {\n assert(typeof value === 'number', 'Value must be a number.');\n assert(value >= 0, 'Value must be a non-negative number.');\n assert(\n Number.isSafeInteger(value),\n 'Value is not a safe integer. Use `bigIntToBytes` instead.',\n );\n\n const hexadecimal = value.toString(16);\n return hexToBytes(hexadecimal);\n}\n\n/**\n * Convert a `string` to a UTF-8 encoded `Uint8Array`.\n *\n * @param value - The string to convert to bytes.\n * @returns The bytes as `Uint8Array`.\n */\nexport function stringToBytes(value: string): Uint8Array {\n assert(typeof value === 'string', 'Value must be a string.');\n\n return new TextEncoder().encode(value);\n}\n\n/**\n * Convert a base64 encoded string to a `Uint8Array`.\n *\n * @param value - The base64 encoded string to convert to bytes.\n * @returns The bytes as `Uint8Array`.\n */\nexport function base64ToBytes(value: string): Uint8Array {\n assert(typeof value === 'string', 'Value must be a string.');\n\n return base64.decode(value);\n}\n\n/**\n * Convert a byte-like value to a `Uint8Array`. The value can be a `Uint8Array`,\n * a `bigint`, a `number`, or a `string`.\n *\n * This will attempt to guess the type of the value based on its type and\n * contents. For more control over the conversion, use the more specific\n * conversion functions, such as {@link hexToBytes} or {@link stringToBytes}.\n *\n * If the value is a `string`, and it is prefixed with `0x`, it will be\n * interpreted as a hexadecimal string. Otherwise, it will be interpreted as a\n * UTF-8 string. To convert a hexadecimal string to bytes without interpreting\n * it as a UTF-8 string, use {@link hexToBytes} instead.\n *\n * If the value is a `bigint`, it is assumed to be unsigned. To convert a signed\n * `bigint` to bytes, use {@link signedBigIntToBytes} instead.\n *\n * If the value is a `Uint8Array`, it will be returned as-is.\n *\n * @param value - The value to convert to bytes.\n * @returns The bytes as `Uint8Array`.\n */\nexport function valueToBytes(value: Bytes): Uint8Array {\n if (typeof value === 'bigint') {\n return bigIntToBytes(value);\n }\n\n if (typeof value === 'number') {\n return numberToBytes(value);\n }\n\n if (typeof value === 'string') {\n if (value.startsWith('0x')) {\n return hexToBytes(value);\n }\n\n return stringToBytes(value);\n }\n\n if (isBytes(value)) {\n return value;\n }\n\n throw new TypeError(`Unsupported value type: \"${typeof value}\".`);\n}\n\n/**\n * Concatenate multiple byte-like values into a single `Uint8Array`. The values\n * can be `Uint8Array`, `bigint`, `number`, or `string`. This uses\n * {@link valueToBytes} under the hood to convert each value to bytes. Refer to\n * the documentation of that function for more information.\n *\n * @param values - The values to concatenate.\n * @returns The concatenated bytes as `Uint8Array`.\n */\nexport function concatBytes(values: Bytes[]): Uint8Array {\n const normalizedValues = new Array(values.length);\n let byteLength = 0;\n\n for (let i = 0; i < values.length; i++) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const value = valueToBytes(values[i]!);\n\n normalizedValues[i] = value;\n byteLength += value.length;\n }\n\n const bytes = new Uint8Array(byteLength);\n for (let i = 0, offset = 0; i < normalizedValues.length; i++) {\n // While we could simply spread the values into an array and use\n // `Uint8Array.from`, that is a lot slower than using `Uint8Array.set`.\n bytes.set(normalizedValues[i], offset);\n offset += normalizedValues[i].length;\n }\n\n return bytes;\n}\n\n/**\n * Create a {@link DataView} from a {@link Uint8Array}. This is a convenience\n * function that avoids having to create a {@link DataView} manually, which\n * requires passing the `byteOffset` and `byteLength` parameters every time.\n *\n * Not passing the `byteOffset` and `byteLength` parameters can result in\n * unexpected behavior when the {@link Uint8Array} is a view of a larger\n * {@link ArrayBuffer}, e.g., when using {@link Uint8Array.subarray}.\n *\n * This function also supports Node.js {@link Buffer}s.\n *\n * @example\n * ```typescript\n * const bytes = new Uint8Array([1, 2, 3]);\n *\n * // This is equivalent to:\n * // const dataView = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);\n * const dataView = createDataView(bytes);\n * ```\n * @param bytes - The bytes to create the {@link DataView} from.\n * @returns The {@link DataView}.\n */\nexport function createDataView(bytes: Uint8Array): DataView {\n // To maintain compatibility with Node.js, we need to check if the bytes are\n // a Buffer. If so, we need to slice the buffer to get the underlying\n // ArrayBuffer.\n // eslint-disable-next-line no-restricted-globals\n if (typeof Buffer !== 'undefined' && bytes instanceof Buffer) {\n const buffer = bytes.buffer.slice(\n bytes.byteOffset,\n bytes.byteOffset + bytes.byteLength,\n );\n\n return new DataView(buffer);\n }\n\n return new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);\n}\n"]}
@@ -1,5 +1,5 @@
1
- import type { Hex } from './hex';
2
- export declare type Bytes = bigint | number | string | Uint8Array;
1
+ import type { Hex } from "./hex.cjs";
2
+ export type Bytes = bigint | number | string | Uint8Array;
3
3
  /**
4
4
  * Check if a value is a `Uint8Array`.
5
5
  *
@@ -180,4 +180,4 @@ export declare function concatBytes(values: Bytes[]): Uint8Array;
180
180
  * @returns The {@link DataView}.
181
181
  */
182
182
  export declare function createDataView(bytes: Uint8Array): DataView;
183
- //# sourceMappingURL=bytes.d.ts.map
183
+ //# sourceMappingURL=bytes.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bytes.d.cts","sourceRoot":"","sources":["../src/bytes.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,GAAG,EAAE,kBAAc;AAUjC,MAAM,MAAM,KAAK,GAAG,MAAM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