@metamask/utils 8.4.0 → 9.0.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 +27 -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} +12 -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 -43
  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} +4 -4
  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 -296
  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 -316
  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} +4 -3
  141. package/dist/number.d.cts.map +1 -0
  142. package/dist/number.d.mts +74 -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} +2 -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 +26 -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 -44
  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-4RMX5YWE.js +0 -34
  204. package/dist/chunk-4RMX5YWE.js.map +0 -1
  205. package/dist/chunk-5AVWINSB.js +0 -1
  206. package/dist/chunk-5AVWINSB.js.map +0 -1
  207. package/dist/chunk-622IOGVI.mjs +0 -1
  208. package/dist/chunk-622IOGVI.mjs.map +0 -1
  209. package/dist/chunk-6C35XQOF.mjs +0 -257
  210. package/dist/chunk-6C35XQOF.mjs.map +0 -1
  211. package/dist/chunk-6NZW4WK4.js +0 -35
  212. package/dist/chunk-6NZW4WK4.js.map +0 -1
  213. package/dist/chunk-6ZDHSOUV.js +0 -59
  214. package/dist/chunk-6ZDHSOUV.js.map +0 -1
  215. package/dist/chunk-74DGVJVE.mjs +0 -59
  216. package/dist/chunk-74DGVJVE.mjs.map +0 -1
  217. package/dist/chunk-AY6FDCBT.mjs +0 -1
  218. package/dist/chunk-AY6FDCBT.mjs.map +0 -1
  219. package/dist/chunk-C6HGFEYL.js +0 -23
  220. package/dist/chunk-C6HGFEYL.js.map +0 -1
  221. package/dist/chunk-DHVKFDHQ.js +0 -95
  222. package/dist/chunk-DHVKFDHQ.js.map +0 -1
  223. package/dist/chunk-DYRKZQMK.mjs +0 -122
  224. package/dist/chunk-DYRKZQMK.mjs.map +0 -1
  225. package/dist/chunk-E4C7EW4R.js +0 -16
  226. package/dist/chunk-E4C7EW4R.js.map +0 -1
  227. package/dist/chunk-EQMZL4XU.js +0 -1
  228. package/dist/chunk-EQMZL4XU.js.map +0 -1
  229. package/dist/chunk-GZS3IQBZ.mjs +0 -16
  230. package/dist/chunk-GZS3IQBZ.mjs.map +0 -1
  231. package/dist/chunk-H4YFDLB7.mjs +0 -70
  232. package/dist/chunk-H4YFDLB7.mjs.map +0 -1
  233. package/dist/chunk-I575FZFH.mjs +0 -1
  234. package/dist/chunk-I575FZFH.mjs.map +0 -1
  235. package/dist/chunk-IZC266HS.js +0 -55
  236. package/dist/chunk-IZC266HS.js.map +0 -1
  237. package/dist/chunk-JPAL7Q5S.mjs +0 -90
  238. package/dist/chunk-JPAL7Q5S.mjs.map +0 -1
  239. package/dist/chunk-LC2CRSWD.js +0 -1
  240. package/dist/chunk-LC2CRSWD.js.map +0 -1
  241. package/dist/chunk-NQMRFZHB.mjs +0 -35
  242. package/dist/chunk-NQMRFZHB.mjs.map +0 -1
  243. package/dist/chunk-O3EIM33O.mjs +0 -95
  244. package/dist/chunk-O3EIM33O.mjs.map +0 -1
  245. package/dist/chunk-OLLG4H35.js +0 -257
  246. package/dist/chunk-OLLG4H35.js.map +0 -1
  247. package/dist/chunk-OUZ3LSLX.mjs +0 -94
  248. package/dist/chunk-OUZ3LSLX.mjs.map +0 -1
  249. package/dist/chunk-QEPVHEP7.js +0 -273
  250. package/dist/chunk-QVEKZRZ2.js +0 -70
  251. package/dist/chunk-QVEKZRZ2.js.map +0 -1
  252. package/dist/chunk-RIRDOQPX.mjs +0 -15
  253. package/dist/chunk-RIRDOQPX.mjs.map +0 -1
  254. package/dist/chunk-RKRGAFXY.js +0 -1
  255. package/dist/chunk-RKRGAFXY.js.map +0 -1
  256. package/dist/chunk-RNEXSCNP.js +0 -122
  257. package/dist/chunk-RNEXSCNP.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-THNLGEXV.mjs +0 -34
  265. package/dist/chunk-THNLGEXV.mjs.map +0 -1
  266. package/dist/chunk-UOTVU7OQ.js +0 -1
  267. package/dist/chunk-UOTVU7OQ.js.map +0 -1
  268. package/dist/chunk-VA2DRBDE.mjs +0 -273
  269. package/dist/chunk-VA2DRBDE.mjs.map +0 -1
  270. package/dist/chunk-VFXTVNXN.js +0 -44
  271. package/dist/chunk-VFXTVNXN.js.map +0 -1
  272. package/dist/chunk-W5XI7ERF.mjs +0 -23
  273. package/dist/chunk-W5XI7ERF.mjs.map +0 -1
  274. package/dist/chunk-WA4DHIND.mjs +0 -1
  275. package/dist/chunk-WA4DHIND.mjs.map +0 -1
  276. package/dist/chunk-X66SUIEF.mjs +0 -25
  277. package/dist/chunk-X66SUIEF.mjs.map +0 -1
  278. package/dist/chunk-XYGUOY6N.mjs +0 -55
  279. package/dist/chunk-XYGUOY6N.mjs.map +0 -1
  280. package/dist/chunk-YWAID473.js +0 -94
  281. package/dist/chunk-YWAID473.js.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 -297
  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 -317
  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/fs.mjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
1
+ {"version":3,"file":"fs.mjs","sourceRoot":"","sources":["../src/fs.ts"],"names":[],"mappings":"AAAA,8DAA8D;AAC9D,6CAA6C;AAE7C,OAAO,EAAE,WAAW;AACpB,OAAO,EAAE,WAAW;AACpB,OAAO,IAAI,aAAa;AACxB,OAAO,KAAK,IAAI,aAAa;AAE7B,OAAO,EAAE,eAAe,EAAE,SAAS,EAAE,qBAAiB;AActD;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAC,QAAgB;IAC7C,IAAI;QACF,OAAO,MAAM,EAAE,CAAC,QAAQ,CAAC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;KACrD;IAAC,OAAO,KAAK,EAAE;QACd,MAAM,SAAS,CAAC,KAAK,EAAE,wBAAwB,QAAQ,GAAG,CAAC,CAAC;KAC7D;AACH,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,SAAS,CAC7B,QAAgB,EAChB,OAAe;IAEf,IAAI;QACF,MAAM,EAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,OAAO,CA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AC;KACH;AACH,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAAC,SAAiB;IACjD,IAAI;QACF,OAAO,MAAM,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,SAAS,EAAE;YACrC,SAAS,EAAE,IAAI;YACf,KAAK,EAAE,IAAI;SACZ,CAAC,CAAC;KACJ;IAAC,OAAO,KAAK,EAAE;QACd,MAAM,SAAS,CAAC,KAAK,EAAE,uCAAuC,SAAS,GAAG,CAAC,CAAC;KAC7E;AACH,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,UAAU,aAAa,CAAC,WAAmB;IAC/C,MAAM,aAAa,GAAG,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,MAAM,EAAE,EAAE,WAAW,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,CAAC;IAErE,OAAO;QACL,aAAa;QACb,KAAK,CAAC,aAAa,CACjB,IAAwD;YAExD,IAAI,MAAM,eAAe,CAAC,aAAa,CAAC,EAAE;gBACxC,MAAM,IAAI,KAAK,CAAC,GAAG,aAAa,mCAAmC,CAAC,CAAC;aACtE;YAED,MAAM,8BAA8B,CAAC,aAAa,CAAC,CAAC;YAEpD,IAAI;gBACF,MAAM,IAAI,CAAC,EAAE,aAAa,EAAE,CAAC,CAAC;aAC/B;oBAAS;gBACR,MAAM,WAAW,CAAC,aAAa,CAAC,CAAC;aAClC;QACH,CAAC;KACF,CAAC;AACJ,CAAC","sourcesContent":["// This file is intended to be used only in a Node.js context.\n/* eslint-disable import/no-nodejs-modules */\n\nimport fs from 'fs';\nimport os from 'os';\nimport path from 'path';\nimport * as uuid from 'uuid';\n\nimport { isErrorWithCode, wrapError } from './errors';\nimport type { Json } from './json';\n\n/**\n * Information about the file sandbox provided to tests that need temporary\n * access to the filesystem.\n */\nexport type FileSandbox = {\n directoryPath: string;\n withinSandbox: (\n test: (args: { directoryPath: string }) => Promise<void>,\n ) => Promise<void>;\n};\n\n/**\n * Read the file at the given path, assuming its content is encoded as UTF-8.\n *\n * @param filePath - The path to the file.\n * @returns The content of the file.\n * @throws An error with a stack trace if reading fails in any way.\n */\nexport async function readFile(filePath: string): Promise<string> {\n try {\n return await fs.promises.readFile(filePath, 'utf8');\n } catch (error) {\n throw wrapError(error, `Could not read file '${filePath}'`);\n }\n}\n\n/**\n * Write content to the file at the given path, creating the directory structure\n * for the file automatically if necessary.\n *\n * @param filePath - The path to the file.\n * @param content - The new content of the file.\n * @throws An error with a stack trace if writing fails in any way.\n */\nexport async function writeFile(\n filePath: string,\n content: string,\n): Promise<void> {\n try {\n await fs.promises.mkdir(path.dirname(filePath), { recursive: true });\n await fs.promises.writeFile(filePath, content);\n } catch (error) {\n throw wrapError(error, `Could not write file '${filePath}'`);\n }\n}\n\n/**\n * Read the assumed JSON file at the given path, attempts to parse it, and\n * get the resulting object. Supports a custom parser (in case you want to\n * use the [JSON5](https://www.npmjs.com/package/json5) package instead).\n *\n * @param filePath - The path segments pointing to the JSON file. Will be passed\n * to path.join().\n * @param options - Options to this function.\n * @param options.parser - The parser object to use. Defaults to `JSON`.\n * @param options.parser.parse - A function that parses JSON data.\n * @returns The object corresponding to the parsed JSON file, typed against the\n * struct.\n * @throws An error with a stack trace if reading fails in any way, or if the\n * parsed value is not a plain object.\n */\nexport async function readJsonFile<Value extends Json>(\n filePath: string,\n {\n parser = JSON,\n }: {\n parser?: {\n parse: (\n text: Parameters<typeof JSON.parse>[0],\n ) => ReturnType<typeof JSON.parse>;\n };\n } = {},\n): Promise<Value> {\n try {\n const content = await fs.promises.readFile(filePath, 'utf8');\n return parser.parse(content);\n } catch (error) {\n throw wrapError(error, `Could not read JSON file '${filePath}'`);\n }\n}\n\n/**\n * Attempt to write the given JSON-like value to the file at the given path,\n * creating the directory structure for the file automatically if necessary.\n * Adds a newline to the end of the file. Supports a custom parser (in case you\n * want to use the [JSON5](https://www.npmjs.com/package/json5) package\n * instead).\n *\n * @param filePath - The path to write the JSON file to, including the file\n * itself.\n * @param jsonValue - The JSON-like value to write to the file. Make sure that\n * JSON.stringify can handle it.\n * @param options - The options to this function.\n * @param options.prettify - Whether to format the JSON as it is turned into a\n * string such that it is broken up into separate lines (using 2 spaces as\n * indentation).\n * @param options.stringifier - The stringifier to use. Defaults to `JSON`.\n * @param options.stringifier.stringify - A function that stringifies JSON.\n * @returns The object corresponding to the parsed JSON file, typed against the\n * struct.\n * @throws An error with a stack trace if writing fails in any way.\n */\nexport async function writeJsonFile(\n filePath: string,\n jsonValue: Json,\n {\n stringifier = JSON,\n prettify = false,\n }: {\n stringifier?: {\n stringify: typeof JSON.stringify;\n };\n prettify?: boolean;\n } = {},\n): Promise<void> {\n try {\n await fs.promises.mkdir(path.dirname(filePath), { recursive: true });\n const json = prettify\n ? stringifier.stringify(jsonValue, null, ' ')\n : stringifier.stringify(jsonValue);\n await fs.promises.writeFile(filePath, json);\n } catch (error) {\n throw wrapError(error, `Could not write JSON file '${filePath}'`);\n }\n}\n\n/**\n * Test the given path to determine whether it represents a file.\n *\n * @param filePath - The path to a (supposed) file on the filesystem.\n * @returns A promise for true if the file exists or false otherwise.\n * @throws An error with a stack trace if reading fails in any way.\n */\nexport async function fileExists(filePath: string): Promise<boolean> {\n try {\n const stats = await fs.promises.stat(filePath);\n return stats.isFile();\n } catch (error) {\n if (isErrorWithCode(error) && error.code === 'ENOENT') {\n return false;\n }\n\n throw wrapError(error, `Could not determine if file exists '${filePath}'`);\n }\n}\n\n/**\n * Test the given path to determine whether it represents a directory.\n *\n * @param directoryPath - The path to a (supposed) directory on the filesystem.\n * @returns A promise for true if the file exists or false otherwise.\n * @throws An error with a stack trace if reading fails in any way.\n */\nexport async function directoryExists(directoryPath: string): Promise<boolean> {\n try {\n const stats = await fs.promises.stat(directoryPath);\n return stats.isDirectory();\n } catch (error) {\n if (isErrorWithCode(error) && error.code === 'ENOENT') {\n return false;\n }\n\n throw wrapError(\n error,\n `Could not determine if directory exists '${directoryPath}'`,\n );\n }\n}\n\n/**\n * Create the given directory along with any directories leading up to the\n * directory, or do nothing if the directory already exists.\n *\n * @param directoryPath - The path to the desired directory.\n * @throws An error with a stack trace if reading fails in any way.\n */\nexport async function ensureDirectoryStructureExists(\n directoryPath: string,\n): Promise<void> {\n try {\n await fs.promises.mkdir(directoryPath, { recursive: true });\n } catch (error) {\n throw wrapError(\n error,\n `Could not create directory structure '${directoryPath}'`,\n );\n }\n}\n\n/**\n * Remove the given file or directory if it exists, or do nothing if it does\n * not.\n *\n * @param entryPath - The path to the file or directory.\n * @throws An error with a stack trace if removal fails in any way.\n */\nexport async function forceRemove(entryPath: string): Promise<void> {\n try {\n return await fs.promises.rm(entryPath, {\n recursive: true,\n force: true,\n });\n } catch (error) {\n throw wrapError(error, `Could not remove file or directory '${entryPath}'`);\n }\n}\n\n/**\n * Construct a sandbox object which can be used in tests that need temporary\n * access to the filesystem.\n *\n * @param projectName - The name of the project.\n * @returns The sandbox object. This contains a `withinSandbox` function which\n * can be used in tests (see example).\n * @example\n * ```typescript\n * const { withinSandbox } = createSandbox('utils');\n *\n * // ... later ...\n *\n * it('does something with the filesystem', async () => {\n * await withinSandbox(async ({ directoryPath }) => {\n * await fs.promises.writeFile(\n * path.join(directoryPath, 'some-file'),\n * 'some content',\n * 'utf8'\n * );\n * })\n * });\n * ```\n */\nexport function createSandbox(projectName: string): FileSandbox {\n const directoryPath = path.join(os.tmpdir(), projectName, uuid.v4());\n\n return {\n directoryPath,\n async withinSandbox(\n test: (args: { directoryPath: string }) => Promise<void>,\n ) {\n if (await directoryExists(directoryPath)) {\n throw new Error(`${directoryPath} already exists. Cannot continue.`);\n }\n\n await ensureDirectoryStructureExists(directoryPath);\n\n try {\n await test({ directoryPath });\n } finally {\n await forceRemove(directoryPath);\n }\n },\n };\n}\n"]}
package/dist/hex.cjs ADDED
@@ -0,0 +1,134 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.remove0x = exports.add0x = exports.isValidChecksumAddress = exports.getChecksumAddress = exports.isValidHexAddress = exports.assertIsStrictHexString = exports.assertIsHexString = exports.isStrictHexString = exports.isHexString = exports.HexChecksumAddressStruct = exports.HexAddressStruct = exports.StrictHexStruct = exports.HexStruct = void 0;
4
+ const superstruct_1 = require("@metamask/superstruct");
5
+ const sha3_1 = require("@noble/hashes/sha3");
6
+ const assert_1 = require("./assert.cjs");
7
+ const bytes_1 = require("./bytes.cjs");
8
+ exports.HexStruct = (0, superstruct_1.pattern)((0, superstruct_1.string)(), /^(?:0x)?[0-9a-f]+$/iu);
9
+ exports.StrictHexStruct = (0, superstruct_1.pattern)((0, superstruct_1.string)(), /^0x[0-9a-f]+$/iu);
10
+ exports.HexAddressStruct = (0, superstruct_1.pattern)((0, superstruct_1.string)(), /^0x[0-9a-f]{40}$/u);
11
+ exports.HexChecksumAddressStruct = (0, superstruct_1.pattern)((0, superstruct_1.string)(), /^0x[0-9a-fA-F]{40}$/u);
12
+ /**
13
+ * Check if a string is a valid hex string.
14
+ *
15
+ * @param value - The value to check.
16
+ * @returns Whether the value is a valid hex string.
17
+ */
18
+ function isHexString(value) {
19
+ return (0, superstruct_1.is)(value, exports.HexStruct);
20
+ }
21
+ exports.isHexString = isHexString;
22
+ /**
23
+ * Strictly check if a string is a valid hex string. A valid hex string must
24
+ * start with the "0x"-prefix.
25
+ *
26
+ * @param value - The value to check.
27
+ * @returns Whether the value is a valid hex string.
28
+ */
29
+ function isStrictHexString(value) {
30
+ return (0, superstruct_1.is)(value, exports.StrictHexStruct);
31
+ }
32
+ exports.isStrictHexString = isStrictHexString;
33
+ /**
34
+ * Assert that a value is a valid hex string.
35
+ *
36
+ * @param value - The value to check.
37
+ * @throws If the value is not a valid hex string.
38
+ */
39
+ function assertIsHexString(value) {
40
+ (0, assert_1.assert)(isHexString(value), 'Value must be a hexadecimal string.');
41
+ }
42
+ exports.assertIsHexString = assertIsHexString;
43
+ /**
44
+ * Assert that a value is a valid hex string. A valid hex string must start with
45
+ * the "0x"-prefix.
46
+ *
47
+ * @param value - The value to check.
48
+ * @throws If the value is not a valid hex string.
49
+ */
50
+ function assertIsStrictHexString(value) {
51
+ (0, assert_1.assert)(isStrictHexString(value), 'Value must be a hexadecimal string, starting with "0x".');
52
+ }
53
+ exports.assertIsStrictHexString = assertIsStrictHexString;
54
+ /**
55
+ * Validate that the passed prefixed hex string is an all-lowercase
56
+ * hex address, or a valid mixed-case checksum address.
57
+ *
58
+ * @param possibleAddress - Input parameter to check against.
59
+ * @returns Whether or not the input is a valid hex address.
60
+ */
61
+ function isValidHexAddress(possibleAddress) {
62
+ return ((0, superstruct_1.is)(possibleAddress, exports.HexAddressStruct) ||
63
+ isValidChecksumAddress(possibleAddress));
64
+ }
65
+ exports.isValidHexAddress = isValidHexAddress;
66
+ /**
67
+ * Encode a passed hex string as an ERC-55 mixed-case checksum address.
68
+ *
69
+ * @param address - The hex address to encode.
70
+ * @returns The address encoded according to ERC-55.
71
+ * @see https://eips.ethereum.org/EIPS/eip-55
72
+ */
73
+ function getChecksumAddress(address) {
74
+ (0, assert_1.assert)((0, superstruct_1.is)(address, exports.HexChecksumAddressStruct), 'Invalid hex address.');
75
+ const unPrefixed = remove0x(address.toLowerCase());
76
+ const unPrefixedHash = remove0x((0, bytes_1.bytesToHex)((0, sha3_1.keccak_256)(unPrefixed)));
77
+ return `0x${unPrefixed
78
+ .split('')
79
+ .map((character, nibbleIndex) => {
80
+ const hashCharacter = unPrefixedHash[nibbleIndex];
81
+ (0, assert_1.assert)((0, superstruct_1.is)(hashCharacter, (0, superstruct_1.string)()), 'Hash shorter than address.');
82
+ return parseInt(hashCharacter, 16) > 7
83
+ ? character.toUpperCase()
84
+ : character;
85
+ })
86
+ .join('')}`;
87
+ }
88
+ exports.getChecksumAddress = getChecksumAddress;
89
+ /**
90
+ * Validate that the passed hex string is a valid ERC-55 mixed-case
91
+ * checksum address.
92
+ *
93
+ * @param possibleChecksum - The hex address to check.
94
+ * @returns True if the address is a checksum address.
95
+ */
96
+ function isValidChecksumAddress(possibleChecksum) {
97
+ if (!(0, superstruct_1.is)(possibleChecksum, exports.HexChecksumAddressStruct)) {
98
+ return false;
99
+ }
100
+ return getChecksumAddress(possibleChecksum) === possibleChecksum;
101
+ }
102
+ exports.isValidChecksumAddress = isValidChecksumAddress;
103
+ /**
104
+ * Add the `0x`-prefix to a hexadecimal string. If the string already has the
105
+ * prefix, it is returned as-is.
106
+ *
107
+ * @param hexadecimal - The hexadecimal string to add the prefix to.
108
+ * @returns The prefixed hexadecimal string.
109
+ */
110
+ function add0x(hexadecimal) {
111
+ if (hexadecimal.startsWith('0x')) {
112
+ return hexadecimal;
113
+ }
114
+ if (hexadecimal.startsWith('0X')) {
115
+ return `0x${hexadecimal.substring(2)}`;
116
+ }
117
+ return `0x${hexadecimal}`;
118
+ }
119
+ exports.add0x = add0x;
120
+ /**
121
+ * Remove the `0x`-prefix from a hexadecimal string. If the string doesn't have
122
+ * the prefix, it is returned as-is.
123
+ *
124
+ * @param hexadecimal - The hexadecimal string to remove the prefix from.
125
+ * @returns The un-prefixed hexadecimal string.
126
+ */
127
+ function remove0x(hexadecimal) {
128
+ if (hexadecimal.startsWith('0x') || hexadecimal.startsWith('0X')) {
129
+ return hexadecimal.substring(2);
130
+ }
131
+ return hexadecimal;
132
+ }
133
+ exports.remove0x = remove0x;
134
+ //# sourceMappingURL=hex.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hex.cjs","sourceRoot":"","sources":["../src/hex.ts"],"names":[],"mappings":";;;AACA,uDAA4D;AAC5D,6CAA6D;AAE7D,yCAAkC;AAClC,uCAAqC;AAIxB,QAAA,SAAS,GAAG,IAAA,qBAAO,EAAC,IAAA,oBAAM,GAAE,EAAE,sBAAsB,CAAC,CAAC;AACtD,QAAA,eAAe,GAAG,IAAA,qBAAO,EAAC,IAAA,oBAAM,GAAE,EAAE,iBAAiB,CAGjE,CAAC;AACW,QAAA,gBAAgB,GAAG,IAAA,qBAAO,EACrC,IAAA,oBAAM,GAAE,EACR,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AAC;AACrB,CAAC;AAND,4BAMC","sourcesContent":["import type { Struct } from '@metamask/superstruct';\nimport { is, pattern, string } from '@metamask/superstruct';\nimport { keccak_256 as keccak256 } from '@noble/hashes/sha3';\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): 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"]}
@@ -1,5 +1,5 @@
1
- import type { Struct } from 'superstruct';
2
- export declare type Hex = `0x${string}`;
1
+ import type { Struct } from "@metamask/superstruct";
2
+ export type Hex = `0x${string}`;
3
3
  export declare const HexStruct: Struct<string, null>;
4
4
  export declare const StrictHexStruct: Struct<`0x${string}`, null>;
5
5
  export declare const HexAddressStruct: Struct<`0x${string}`, null>;
@@ -49,7 +49,7 @@ export declare function isValidHexAddress(possibleAddress: Hex): boolean;
49
49
  * @returns The address encoded according to ERC-55.
50
50
  * @see https://eips.ethereum.org/EIPS/eip-55
51
51
  */
52
- export declare function getChecksumAddress(address: Hex): string;
52
+ export declare function getChecksumAddress(address: Hex): Hex;
53
53
  /**
54
54
  * Validate that the passed hex string is a valid ERC-55 mixed-case
55
55
  * checksum address.
@@ -74,4 +74,4 @@ export declare function add0x(hexadecimal: string): Hex;
74
74
  * @returns The un-prefixed hexadecimal string.
75
75
  */
76
76
  export declare function remove0x(hexadecimal: string): string;
77
- //# sourceMappingURL=hex.d.ts.map
77
+ //# sourceMappingURL=hex.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hex.d.cts","sourceRoot":"","sources":["../src/hex.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,8BAA8B;AAOpD,MAAM,MAAM,GAAG,GAAG,KAAK,MAAM,EAAE,CAAC;AAEhC,eAAO,MAAM,SAAS,sBAA4C,CAAC;AACnE,eAAO,MAAM,eAAe,6BAG3B,CAAC;AACF,eAAO,MAAM,gBAAgB,6BAGP,CAAC;AACvB,eAAO,MAAM,wBAAwB,6BAGf,CAAC;AAEvB;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAE3D;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,GAAG,CAE9D;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,KAAK,IAAI,MAAM,CAEzE;AAED;;;;;;GAMG;AACH,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,KAAK,IAAI,GAAG,CAK5E;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,eAAe,EAAE,GAAG,WAKrD;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,GAAG,GAAG,GAAG,CAcpD;AAED;;;;;;GAMG;AACH,wBAAgB,sBAAsB,CAAC,gBAAgB,EAAE,GAAG,WAM3D;AAED;;;;;;GAMG;AACH,wBAAgB,KAAK,CAAC,WAAW,EAAE,MAAM,GAAG,GAAG,CAU9C;AAED;;;;;;GAMG;AACH,wBAAgB,QAAQ,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CAMpD"}
package/dist/hex.d.mts ADDED
@@ -0,0 +1,77 @@
1
+ import type { Struct } from "@metamask/superstruct";
2
+ export type Hex = `0x${string}`;
3
+ export declare const HexStruct: Struct<string, null>;
4
+ export declare const StrictHexStruct: Struct<`0x${string}`, null>;
5
+ export declare const HexAddressStruct: Struct<`0x${string}`, null>;
6
+ export declare const HexChecksumAddressStruct: Struct<`0x${string}`, null>;
7
+ /**
8
+ * Check if a string is a valid hex string.
9
+ *
10
+ * @param value - The value to check.
11
+ * @returns Whether the value is a valid hex string.
12
+ */
13
+ export declare function isHexString(value: unknown): value is string;
14
+ /**
15
+ * Strictly check if a string is a valid hex string. A valid hex string must
16
+ * start with the "0x"-prefix.
17
+ *
18
+ * @param value - The value to check.
19
+ * @returns Whether the value is a valid hex string.
20
+ */
21
+ export declare function isStrictHexString(value: unknown): value is Hex;
22
+ /**
23
+ * Assert that a value is a valid hex string.
24
+ *
25
+ * @param value - The value to check.
26
+ * @throws If the value is not a valid hex string.
27
+ */
28
+ export declare function assertIsHexString(value: unknown): asserts value is string;
29
+ /**
30
+ * Assert that a value is a valid hex string. A valid hex string must start with
31
+ * the "0x"-prefix.
32
+ *
33
+ * @param value - The value to check.
34
+ * @throws If the value is not a valid hex string.
35
+ */
36
+ export declare function assertIsStrictHexString(value: unknown): asserts value is Hex;
37
+ /**
38
+ * Validate that the passed prefixed hex string is an all-lowercase
39
+ * hex address, or a valid mixed-case checksum address.
40
+ *
41
+ * @param possibleAddress - Input parameter to check against.
42
+ * @returns Whether or not the input is a valid hex address.
43
+ */
44
+ export declare function isValidHexAddress(possibleAddress: Hex): boolean;
45
+ /**
46
+ * Encode a passed hex string as an ERC-55 mixed-case checksum address.
47
+ *
48
+ * @param address - The hex address to encode.
49
+ * @returns The address encoded according to ERC-55.
50
+ * @see https://eips.ethereum.org/EIPS/eip-55
51
+ */
52
+ export declare function getChecksumAddress(address: Hex): Hex;
53
+ /**
54
+ * Validate that the passed hex string is a valid ERC-55 mixed-case
55
+ * checksum address.
56
+ *
57
+ * @param possibleChecksum - The hex address to check.
58
+ * @returns True if the address is a checksum address.
59
+ */
60
+ export declare function isValidChecksumAddress(possibleChecksum: Hex): boolean;
61
+ /**
62
+ * Add the `0x`-prefix to a hexadecimal string. If the string already has the
63
+ * prefix, it is returned as-is.
64
+ *
65
+ * @param hexadecimal - The hexadecimal string to add the prefix to.
66
+ * @returns The prefixed hexadecimal string.
67
+ */
68
+ export declare function add0x(hexadecimal: string): Hex;
69
+ /**
70
+ * Remove the `0x`-prefix from a hexadecimal string. If the string doesn't have
71
+ * the prefix, it is returned as-is.
72
+ *
73
+ * @param hexadecimal - The hexadecimal string to remove the prefix from.
74
+ * @returns The un-prefixed hexadecimal string.
75
+ */
76
+ export declare function remove0x(hexadecimal: string): string;
77
+ //# sourceMappingURL=hex.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hex.d.mts","sourceRoot":"","sources":["../src/hex.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,8BAA8B;AAOpD,MAAM,MAAM,GAAG,GAAG,KAAK,MAAM,EAAE,CAAC;AAEhC,eAAO,MAAM,SAAS,sBAA4C,CAAC;AACnE,eAAO,MAAM,eAAe,6BAG3B,CAAC;AACF,eAAO,MAAM,gBAAgB,6BAGP,CAAC;AACvB,eAAO,MAAM,wBAAwB,6BAGf,CAAC;AAEvB;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAE3D;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,GAAG,CAE9D;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,KAAK,IAAI,MAAM,CAEzE;AAED;;;;;;GAMG;AACH,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,KAAK,IAAI,GAAG,CAK5E;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,eAAe,EAAE,GAAG,WAKrD;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,GAAG,GAAG,GAAG,CAcpD;AAED;;;;;;GAMG;AACH,wBAAgB,sBAAsB,CAAC,gBAAgB,EAAE,GAAG,WAM3D;AAED;;;;;;GAMG;AACH,wBAAgB,KAAK,CAAC,WAAW,EAAE,MAAM,GAAG,GAAG,CAU9C;AAED;;;;;;GAMG;AACH,wBAAgB,QAAQ,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CAMpD"}
package/dist/hex.mjs CHANGED
@@ -1,35 +1,122 @@
1
- import {
2
- HexAddressStruct,
3
- HexChecksumAddressStruct,
4
- HexStruct,
5
- StrictHexStruct,
6
- add0x,
7
- assertIsHexString,
8
- assertIsStrictHexString,
9
- getChecksumAddress,
10
- isHexString,
11
- isStrictHexString,
12
- isValidChecksumAddress,
13
- isValidHexAddress,
14
- remove0x
15
- } from "./chunk-VA2DRBDE.mjs";
16
- import "./chunk-74DGVJVE.mjs";
17
- import "./chunk-XYGUOY6N.mjs";
18
- import "./chunk-H4YFDLB7.mjs";
19
- import "./chunk-X66SUIEF.mjs";
20
- export {
21
- HexAddressStruct,
22
- HexChecksumAddressStruct,
23
- HexStruct,
24
- StrictHexStruct,
25
- add0x,
26
- assertIsHexString,
27
- assertIsStrictHexString,
28
- getChecksumAddress,
29
- isHexString,
30
- isStrictHexString,
31
- isValidChecksumAddress,
32
- isValidHexAddress,
33
- remove0x
34
- };
1
+ import { is, pattern, string } from "@metamask/superstruct";
2
+ import { keccak_256 as keccak256 } from "@noble/hashes/sha3";
3
+ import { assert } from "./assert.mjs";
4
+ import { bytesToHex } from "./bytes.mjs";
5
+ export const HexStruct = pattern(string(), /^(?:0x)?[0-9a-f]+$/iu);
6
+ export const StrictHexStruct = pattern(string(), /^0x[0-9a-f]+$/iu);
7
+ export const HexAddressStruct = pattern(string(), /^0x[0-9a-f]{40}$/u);
8
+ export const HexChecksumAddressStruct = pattern(string(), /^0x[0-9a-fA-F]{40}$/u);
9
+ /**
10
+ * Check if a string is a valid hex string.
11
+ *
12
+ * @param value - The value to check.
13
+ * @returns Whether the value is a valid hex string.
14
+ */
15
+ export function isHexString(value) {
16
+ return is(value, HexStruct);
17
+ }
18
+ /**
19
+ * Strictly check if a string is a valid hex string. A valid hex string must
20
+ * start with the "0x"-prefix.
21
+ *
22
+ * @param value - The value to check.
23
+ * @returns Whether the value is a valid hex string.
24
+ */
25
+ export function isStrictHexString(value) {
26
+ return is(value, StrictHexStruct);
27
+ }
28
+ /**
29
+ * Assert that a value is a valid hex string.
30
+ *
31
+ * @param value - The value to check.
32
+ * @throws If the value is not a valid hex string.
33
+ */
34
+ export function assertIsHexString(value) {
35
+ assert(isHexString(value), 'Value must be a hexadecimal string.');
36
+ }
37
+ /**
38
+ * Assert that a value is a valid hex string. A valid hex string must start with
39
+ * the "0x"-prefix.
40
+ *
41
+ * @param value - The value to check.
42
+ * @throws If the value is not a valid hex string.
43
+ */
44
+ export function assertIsStrictHexString(value) {
45
+ assert(isStrictHexString(value), 'Value must be a hexadecimal string, starting with "0x".');
46
+ }
47
+ /**
48
+ * Validate that the passed prefixed hex string is an all-lowercase
49
+ * hex address, or a valid mixed-case checksum address.
50
+ *
51
+ * @param possibleAddress - Input parameter to check against.
52
+ * @returns Whether or not the input is a valid hex address.
53
+ */
54
+ export function isValidHexAddress(possibleAddress) {
55
+ return (is(possibleAddress, HexAddressStruct) ||
56
+ isValidChecksumAddress(possibleAddress));
57
+ }
58
+ /**
59
+ * Encode a passed hex string as an ERC-55 mixed-case checksum address.
60
+ *
61
+ * @param address - The hex address to encode.
62
+ * @returns The address encoded according to ERC-55.
63
+ * @see https://eips.ethereum.org/EIPS/eip-55
64
+ */
65
+ export function getChecksumAddress(address) {
66
+ assert(is(address, HexChecksumAddressStruct), 'Invalid hex address.');
67
+ const unPrefixed = remove0x(address.toLowerCase());
68
+ const unPrefixedHash = remove0x(bytesToHex(keccak256(unPrefixed)));
69
+ return `0x${unPrefixed
70
+ .split('')
71
+ .map((character, nibbleIndex) => {
72
+ const hashCharacter = unPrefixedHash[nibbleIndex];
73
+ assert(is(hashCharacter, string()), 'Hash shorter than address.');
74
+ return parseInt(hashCharacter, 16) > 7
75
+ ? character.toUpperCase()
76
+ : character;
77
+ })
78
+ .join('')}`;
79
+ }
80
+ /**
81
+ * Validate that the passed hex string is a valid ERC-55 mixed-case
82
+ * checksum address.
83
+ *
84
+ * @param possibleChecksum - The hex address to check.
85
+ * @returns True if the address is a checksum address.
86
+ */
87
+ export function isValidChecksumAddress(possibleChecksum) {
88
+ if (!is(possibleChecksum, HexChecksumAddressStruct)) {
89
+ return false;
90
+ }
91
+ return getChecksumAddress(possibleChecksum) === possibleChecksum;
92
+ }
93
+ /**
94
+ * Add the `0x`-prefix to a hexadecimal string. If the string already has the
95
+ * prefix, it is returned as-is.
96
+ *
97
+ * @param hexadecimal - The hexadecimal string to add the prefix to.
98
+ * @returns The prefixed hexadecimal string.
99
+ */
100
+ export function add0x(hexadecimal) {
101
+ if (hexadecimal.startsWith('0x')) {
102
+ return hexadecimal;
103
+ }
104
+ if (hexadecimal.startsWith('0X')) {
105
+ return `0x${hexadecimal.substring(2)}`;
106
+ }
107
+ return `0x${hexadecimal}`;
108
+ }
109
+ /**
110
+ * Remove the `0x`-prefix from a hexadecimal string. If the string doesn't have
111
+ * the prefix, it is returned as-is.
112
+ *
113
+ * @param hexadecimal - The hexadecimal string to remove the prefix from.
114
+ * @returns The un-prefixed hexadecimal string.
115
+ */
116
+ export function remove0x(hexadecimal) {
117
+ if (hexadecimal.startsWith('0x') || hexadecimal.startsWith('0X')) {
118
+ return hexadecimal.substring(2);
119
+ }
120
+ return hexadecimal;
121
+ }
35
122
  //# sourceMappingURL=hex.mjs.map
package/dist/hex.mjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
1
+ {"version":3,"file":"hex.mjs","sourceRoot":"","sources":["../src/hex.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,8BAA8B;AAC5D,OAAO,EAAE,UAAU,IAAI,SAAS,EAAE,2BAA2B;AAE7D,OAAO,EAAE,MAAM,EAAE,qBAAiB;AAClC,OAAO,EAAE,UAAU,EAAE,oBAAgB;AAIrC,MAAM,CAAC,MAAM,SAAS,GAAG,OAAO,CAAC,MAAM,EAAE,EAAE,sBAAsB,CAAC,CAAC;AACnE,MAAM,CAAC,MAAM,eAAe,GAAG,OAAO,CAAC,MAAM,EAAE,EAAE,iBAAiB,CAGjE,CAAC;AACF,MAAM,CAAC,MAAM,gBAAgB,GAAG,OAAO,CACrC,MAAM,EAAE,EACR,mBAAmB,CACC,CAAC;AACvB,MAAM,CAAC,MAAM,wBAAwB,GAAG,OAAO,CAC7C,MAAM,EAAE,EACR,sBAAsB,CACF,CAAC;AAEvB;;;;;GAKG;AACH,MAAM,UAAU,WAAW,CAAC,KAA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,GAAG,CAAC;YACpC,CAAC,CAAC,SAAS,CAAC,WAAW,EAAE;YACzB,CAAC,CAAC,SAAS,CAAC;IAChB,CAAC,CAAC;SACD,IAAI,CAAC,EAAE,CAAC,EAAE,CAAC;AAChB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,sBAAsB,CAAC,gBAAqB;IAC1D,IAAI,CAAC,EAAE,CAAC,gBAAgB,EAAE,wBAAwB,CAAC,EAAE;QACnD,OAAO,KAAK,CAAC;KACd;IAED,OAAO,kBAAkB,CAAC,gBAAgB,CAAC,KAAK,gBAAgB,CAAC;AACnE,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,KAAK,CAAC,WAAmB;IACvC,IAAI,WAAW,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE;QAChC,OAAO,WAAkB,CAAC;KAC3B;IAED,IAAI,WAAW,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE;QAChC,OAAO,KAAK,WAAW,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC;KACxC;IAED,OAAO,KAAK,WAAW,EAAE,CAAC;AAC5B,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,QAAQ,CAAC,WAAmB;IAC1C,IAAI,WAAW,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,WAAW,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE;QAChE,OAAO,WAAW,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;KACjC;IAED,OAAO,WAAW,CAAC;AACrB,CAAC","sourcesContent":["import type { Struct } from '@metamask/superstruct';\nimport { is, pattern, string } from '@metamask/superstruct';\nimport { keccak_256 as keccak256 } from '@noble/hashes/sha3';\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): 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"]}
package/dist/index.cjs ADDED
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./assert.cjs"), exports);
18
+ __exportStar(require("./base64.cjs"), exports);
19
+ __exportStar(require("./bytes.cjs"), exports);
20
+ __exportStar(require("./caip-types.cjs"), exports);
21
+ __exportStar(require("./checksum.cjs"), exports);
22
+ __exportStar(require("./coercers.cjs"), exports);
23
+ __exportStar(require("./collections.cjs"), exports);
24
+ __exportStar(require("./encryption-types.cjs"), exports);
25
+ __exportStar(require("./errors.cjs"), exports);
26
+ __exportStar(require("./hex.cjs"), exports);
27
+ __exportStar(require("./json.cjs"), exports);
28
+ __exportStar(require("./keyring.cjs"), exports);
29
+ __exportStar(require("./logging.cjs"), exports);
30
+ __exportStar(require("./misc.cjs"), exports);
31
+ __exportStar(require("./number.cjs"), exports);
32
+ __exportStar(require("./opaque.cjs"), exports);
33
+ __exportStar(require("./promise.cjs"), exports);
34
+ __exportStar(require("./time.cjs"), exports);
35
+ __exportStar(require("./transaction-types.cjs"), exports);
36
+ __exportStar(require("./versions.cjs"), exports);
37
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,+CAAyB;AACzB,+CAAyB;AACzB,8CAAwB;AACxB,mDAA6B;AAC7B,iDAA2B;AAC3B,iDAA2B;AAC3B,oDAA8B;AAC9B,yDAAmC;AACnC,+CAAyB;AACzB,4CAAsB;AACtB,6CAAuB;AACvB,gDAA0B;AAC1B,gDAA0B;AAC1B,6CAAuB;AACvB,+CAAyB;AACzB,+CAAyB;AACzB,gDAA0B;AAC1B,6CAAuB;AACvB,0DAAoC;AACpC,iDAA2B","sourcesContent":["export * from './assert';\nexport * from './base64';\nexport * from './bytes';\nexport * from './caip-types';\nexport * from './checksum';\nexport * from './coercers';\nexport * from './collections';\nexport * from './encryption-types';\nexport * from './errors';\nexport * from './hex';\nexport * from './json';\nexport * from './keyring';\nexport * from './logging';\nexport * from './misc';\nexport * from './number';\nexport * from './opaque';\nexport * from './promise';\nexport * from './time';\nexport * from './transaction-types';\nexport * from './versions';\n"]}
@@ -0,0 +1,21 @@
1
+ export * from "./assert.cjs";
2
+ export * from "./base64.cjs";
3
+ export * from "./bytes.cjs";
4
+ export * from "./caip-types.cjs";
5
+ export * from "./checksum.cjs";
6
+ export * from "./coercers.cjs";
7
+ export * from "./collections.cjs";
8
+ export * from "./encryption-types.cjs";
9
+ export * from "./errors.cjs";
10
+ export * from "./hex.cjs";
11
+ export * from "./json.cjs";
12
+ export * from "./keyring.cjs";
13
+ export * from "./logging.cjs";
14
+ export * from "./misc.cjs";
15
+ export * from "./number.cjs";
16
+ export * from "./opaque.cjs";
17
+ export * from "./promise.cjs";
18
+ export * from "./time.cjs";
19
+ export * from "./transaction-types.cjs";
20
+ export * from "./versions.cjs";
21
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,6BAAyB;AACzB,6BAAyB;AACzB,4BAAwB;AACxB,iCAA6B;AAC7B,+BAA2B;AAC3B,+BAA2B;AAC3B,kCAA8B;AAC9B,uCAAmC;AACnC,6BAAyB;AACzB,0BAAsB;AACtB,2BAAuB;AACvB,8BAA0B;AAC1B,8BAA0B;AAC1B,2BAAuB;AACvB,6BAAyB;AACzB,6BAAyB;AACzB,8BAA0B;AAC1B,2BAAuB;AACvB,wCAAoC;AACpC,+BAA2B"}
@@ -0,0 +1,21 @@
1
+ export * from "./assert.mjs";
2
+ export * from "./base64.mjs";
3
+ export * from "./bytes.mjs";
4
+ export * from "./caip-types.mjs";
5
+ export * from "./checksum.mjs";
6
+ export * from "./coercers.mjs";
7
+ export * from "./collections.mjs";
8
+ export * from "./encryption-types.mjs";
9
+ export * from "./errors.mjs";
10
+ export * from "./hex.mjs";
11
+ export * from "./json.mjs";
12
+ export * from "./keyring.mjs";
13
+ export * from "./logging.mjs";
14
+ export * from "./misc.mjs";
15
+ export * from "./number.mjs";
16
+ export * from "./opaque.mjs";
17
+ export * from "./promise.mjs";
18
+ export * from "./time.mjs";
19
+ export * from "./transaction-types.mjs";
20
+ export * from "./versions.mjs";
21
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,6BAAyB;AACzB,6BAAyB;AACzB,4BAAwB;AACxB,iCAA6B;AAC7B,+BAA2B;AAC3B,+BAA2B;AAC3B,kCAA8B;AAC9B,uCAAmC;AACnC,6BAAyB;AACzB,0BAAsB;AACtB,2BAAuB;AACvB,8BAA0B;AAC1B,8BAA0B;AAC1B,2BAAuB;AACvB,6BAAyB;AACzB,6BAAyB;AACzB,8BAA0B;AAC1B,2BAAuB;AACvB,wCAAoC;AACpC,+BAA2B"}