@bitpay-labs/crypto-wallet-core 11.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (343) hide show
  1. package/README.md +13 -0
  2. package/package.json +58 -0
  3. package/src/constants/address.ts +7 -0
  4. package/src/constants/algorithms.ts +9 -0
  5. package/src/constants/chains.ts +64 -0
  6. package/src/constants/feeMinimums.ts +20 -0
  7. package/src/constants/index.ts +37 -0
  8. package/src/constants/tokens.ts +224 -0
  9. package/src/constants/units.ts +289 -0
  10. package/src/derivation/arb/index.ts +3 -0
  11. package/src/derivation/base/index.ts +3 -0
  12. package/src/derivation/bch/index.ts +11 -0
  13. package/src/derivation/btc/index.ts +39 -0
  14. package/src/derivation/doge/index.ts +6 -0
  15. package/src/derivation/eth/index.ts +58 -0
  16. package/src/derivation/index.ts +124 -0
  17. package/src/derivation/ltc/index.ts +6 -0
  18. package/src/derivation/matic/index.ts +3 -0
  19. package/src/derivation/op/index.ts +3 -0
  20. package/src/derivation/paths.ts +42 -0
  21. package/src/derivation/sol/index.ts +57 -0
  22. package/src/derivation/xrp/index.ts +38 -0
  23. package/src/index.ts +48 -0
  24. package/src/message/btc.ts +28 -0
  25. package/src/message/eth.ts +47 -0
  26. package/src/message/index.ts +70 -0
  27. package/src/message/sol.ts +3 -0
  28. package/src/message/xrp.ts +4 -0
  29. package/src/transactions/arb/index.ts +14 -0
  30. package/src/transactions/base/index.ts +13 -0
  31. package/src/transactions/bch/index.ts +25 -0
  32. package/src/transactions/btc/index.ts +227 -0
  33. package/src/transactions/doge/index.ts +31 -0
  34. package/src/transactions/erc20/abi.ts +418 -0
  35. package/src/transactions/erc20/index.ts +57 -0
  36. package/src/transactions/eth/index.ts +183 -0
  37. package/src/transactions/eth-multisig/abi.ts +724 -0
  38. package/src/transactions/eth-multisig/index.ts +172 -0
  39. package/src/transactions/index.ts +83 -0
  40. package/src/transactions/ltc/index.ts +31 -0
  41. package/src/transactions/matic/index.ts +14 -0
  42. package/src/transactions/matic-multisig/index.ts +172 -0
  43. package/src/transactions/op/index.ts +14 -0
  44. package/src/transactions/sol/index.ts +292 -0
  45. package/src/transactions/spl/index.ts +192 -0
  46. package/src/transactions/xrp/index.ts +129 -0
  47. package/src/types/derivation.ts +17 -0
  48. package/src/types/index.ts +3 -0
  49. package/src/types/message.ts +15 -0
  50. package/src/types/utils.ts +1 -0
  51. package/src/types/validation.ts +4 -0
  52. package/src/utils/bigint.ts +238 -0
  53. package/src/utils/index.ts +94 -0
  54. package/src/validation/arb/index.ts +8 -0
  55. package/src/validation/base/index.ts +8 -0
  56. package/src/validation/bch/index.ts +17 -0
  57. package/src/validation/btc/index.ts +17 -0
  58. package/src/validation/doge/index.ts +16 -0
  59. package/src/validation/eth/index.ts +29 -0
  60. package/src/validation/index.ts +43 -0
  61. package/src/validation/ltc/index.ts +16 -0
  62. package/src/validation/matic/index.ts +8 -0
  63. package/src/validation/op/index.ts +8 -0
  64. package/src/validation/sol/index.ts +34 -0
  65. package/src/validation/xrp/index.ts +50 -0
  66. package/test/address.test.ts +178 -0
  67. package/test/bigint.test.ts +373 -0
  68. package/test/transactions.test.ts +1777 -0
  69. package/test/utils.test.ts +121 -0
  70. package/test/validation.test.ts +261 -0
  71. package/ts_build/src/constants/address.d.ts +8 -0
  72. package/ts_build/src/constants/address.d.ts.map +1 -0
  73. package/ts_build/src/constants/address.js +11 -0
  74. package/ts_build/src/constants/address.js.map +1 -0
  75. package/ts_build/src/constants/algorithms.d.ts +9 -0
  76. package/ts_build/src/constants/algorithms.d.ts.map +1 -0
  77. package/ts_build/src/constants/algorithms.js +12 -0
  78. package/ts_build/src/constants/algorithms.js.map +1 -0
  79. package/ts_build/src/constants/chains.d.ts +42 -0
  80. package/ts_build/src/constants/chains.d.ts.map +1 -0
  81. package/ts_build/src/constants/chains.js +54 -0
  82. package/ts_build/src/constants/chains.js.map +1 -0
  83. package/ts_build/src/constants/feeMinimums.d.ts +21 -0
  84. package/ts_build/src/constants/feeMinimums.d.ts.map +1 -0
  85. package/ts_build/src/constants/feeMinimums.js +24 -0
  86. package/ts_build/src/constants/feeMinimums.js.map +1 -0
  87. package/ts_build/src/constants/index.d.ts +569 -0
  88. package/ts_build/src/constants/index.d.ts.map +1 -0
  89. package/ts_build/src/constants/index.js +31 -0
  90. package/ts_build/src/constants/index.js.map +1 -0
  91. package/ts_build/src/constants/tokens.d.ts +423 -0
  92. package/ts_build/src/constants/tokens.d.ts.map +1 -0
  93. package/ts_build/src/constants/tokens.js +222 -0
  94. package/ts_build/src/constants/tokens.js.map +1 -0
  95. package/ts_build/src/constants/units.d.ts +289 -0
  96. package/ts_build/src/constants/units.d.ts.map +1 -0
  97. package/ts_build/src/constants/units.js +292 -0
  98. package/ts_build/src/constants/units.js.map +1 -0
  99. package/ts_build/src/derivation/arb/index.d.ts +4 -0
  100. package/ts_build/src/derivation/arb/index.d.ts.map +1 -0
  101. package/ts_build/src/derivation/arb/index.js +8 -0
  102. package/ts_build/src/derivation/arb/index.js.map +1 -0
  103. package/ts_build/src/derivation/base/index.d.ts +4 -0
  104. package/ts_build/src/derivation/base/index.d.ts.map +1 -0
  105. package/ts_build/src/derivation/base/index.js +8 -0
  106. package/ts_build/src/derivation/base/index.js.map +1 -0
  107. package/ts_build/src/derivation/bch/index.d.ts +6 -0
  108. package/ts_build/src/derivation/bch/index.d.ts.map +1 -0
  109. package/ts_build/src/derivation/bch/index.js +17 -0
  110. package/ts_build/src/derivation/bch/index.js.map +1 -0
  111. package/ts_build/src/derivation/btc/index.d.ts +23 -0
  112. package/ts_build/src/derivation/btc/index.d.ts.map +1 -0
  113. package/ts_build/src/derivation/btc/index.js +40 -0
  114. package/ts_build/src/derivation/btc/index.js.map +1 -0
  115. package/ts_build/src/derivation/doge/index.d.ts +5 -0
  116. package/ts_build/src/derivation/doge/index.d.ts.map +1 -0
  117. package/ts_build/src/derivation/doge/index.js +13 -0
  118. package/ts_build/src/derivation/doge/index.js.map +1 -0
  119. package/ts_build/src/derivation/eth/index.d.ts +21 -0
  120. package/ts_build/src/derivation/eth/index.d.ts.map +1 -0
  121. package/ts_build/src/derivation/eth/index.js +58 -0
  122. package/ts_build/src/derivation/eth/index.js.map +1 -0
  123. package/ts_build/src/derivation/index.d.ts +12 -0
  124. package/ts_build/src/derivation/index.d.ts.map +1 -0
  125. package/ts_build/src/derivation/index.js +68 -0
  126. package/ts_build/src/derivation/index.js.map +1 -0
  127. package/ts_build/src/derivation/ltc/index.d.ts +5 -0
  128. package/ts_build/src/derivation/ltc/index.d.ts.map +1 -0
  129. package/ts_build/src/derivation/ltc/index.js +13 -0
  130. package/ts_build/src/derivation/ltc/index.js.map +1 -0
  131. package/ts_build/src/derivation/matic/index.d.ts +4 -0
  132. package/ts_build/src/derivation/matic/index.d.ts.map +1 -0
  133. package/ts_build/src/derivation/matic/index.js +8 -0
  134. package/ts_build/src/derivation/matic/index.js.map +1 -0
  135. package/ts_build/src/derivation/op/index.d.ts +4 -0
  136. package/ts_build/src/derivation/op/index.d.ts.map +1 -0
  137. package/ts_build/src/derivation/op/index.js +8 -0
  138. package/ts_build/src/derivation/op/index.js.map +1 -0
  139. package/ts_build/src/derivation/paths.d.ts +43 -0
  140. package/ts_build/src/derivation/paths.d.ts.map +1 -0
  141. package/ts_build/src/derivation/paths.js +46 -0
  142. package/ts_build/src/derivation/paths.js.map +1 -0
  143. package/ts_build/src/derivation/sol/index.d.ts +17 -0
  144. package/ts_build/src/derivation/sol/index.d.ts.map +1 -0
  145. package/ts_build/src/derivation/sol/index.js +85 -0
  146. package/ts_build/src/derivation/sol/index.js.map +1 -0
  147. package/ts_build/src/derivation/xrp/index.d.ts +19 -0
  148. package/ts_build/src/derivation/xrp/index.d.ts.map +1 -0
  149. package/ts_build/src/derivation/xrp/index.js +40 -0
  150. package/ts_build/src/derivation/xrp/index.js.map +1 -0
  151. package/ts_build/src/index.d.ts +30 -0
  152. package/ts_build/src/index.d.ts.map +1 -0
  153. package/ts_build/src/index.js +81 -0
  154. package/ts_build/src/index.js.map +1 -0
  155. package/ts_build/src/message/btc.d.ts +19 -0
  156. package/ts_build/src/message/btc.d.ts.map +1 -0
  157. package/ts_build/src/message/btc.js +32 -0
  158. package/ts_build/src/message/btc.js.map +1 -0
  159. package/ts_build/src/message/eth.d.ts +19 -0
  160. package/ts_build/src/message/eth.d.ts.map +1 -0
  161. package/ts_build/src/message/eth.js +39 -0
  162. package/ts_build/src/message/eth.js.map +1 -0
  163. package/ts_build/src/message/index.d.ts +25 -0
  164. package/ts_build/src/message/index.d.ts.map +1 -0
  165. package/ts_build/src/message/index.js +50 -0
  166. package/ts_build/src/message/index.js.map +1 -0
  167. package/ts_build/src/message/sol.d.ts +4 -0
  168. package/ts_build/src/message/sol.d.ts.map +1 -0
  169. package/ts_build/src/message/sol.js +9 -0
  170. package/ts_build/src/message/sol.js.map +1 -0
  171. package/ts_build/src/message/xrp.d.ts +4 -0
  172. package/ts_build/src/message/xrp.d.ts.map +1 -0
  173. package/ts_build/src/message/xrp.js +9 -0
  174. package/ts_build/src/message/xrp.js.map +1 -0
  175. package/ts_build/src/transactions/arb/index.d.ts +9 -0
  176. package/ts_build/src/transactions/arb/index.d.ts.map +1 -0
  177. package/ts_build/src/transactions/arb/index.js +18 -0
  178. package/ts_build/src/transactions/arb/index.js.map +1 -0
  179. package/ts_build/src/transactions/base/index.d.ts +8 -0
  180. package/ts_build/src/transactions/base/index.d.ts.map +1 -0
  181. package/ts_build/src/transactions/base/index.js +17 -0
  182. package/ts_build/src/transactions/base/index.js.map +1 -0
  183. package/ts_build/src/transactions/bch/index.d.ts +12 -0
  184. package/ts_build/src/transactions/bch/index.d.ts.map +1 -0
  185. package/ts_build/src/transactions/bch/index.js +32 -0
  186. package/ts_build/src/transactions/bch/index.js.map +1 -0
  187. package/ts_build/src/transactions/btc/index.d.ts +84 -0
  188. package/ts_build/src/transactions/btc/index.d.ts.map +1 -0
  189. package/ts_build/src/transactions/btc/index.js +184 -0
  190. package/ts_build/src/transactions/btc/index.js.map +1 -0
  191. package/ts_build/src/transactions/doge/index.d.ts +12 -0
  192. package/ts_build/src/transactions/doge/index.d.ts.map +1 -0
  193. package/ts_build/src/transactions/doge/index.js +38 -0
  194. package/ts_build/src/transactions/doge/index.js.map +1 -0
  195. package/ts_build/src/transactions/erc20/abi.d.ts +78 -0
  196. package/ts_build/src/transactions/erc20/abi.d.ts.map +1 -0
  197. package/ts_build/src/transactions/erc20/abi.js +421 -0
  198. package/ts_build/src/transactions/erc20/abi.js.map +1 -0
  199. package/ts_build/src/transactions/erc20/index.d.ts +29 -0
  200. package/ts_build/src/transactions/erc20/index.d.ts.map +1 -0
  201. package/ts_build/src/transactions/erc20/index.js +46 -0
  202. package/ts_build/src/transactions/erc20/index.js.map +1 -0
  203. package/ts_build/src/transactions/eth/index.d.ts +53 -0
  204. package/ts_build/src/transactions/eth/index.d.ts.map +1 -0
  205. package/ts_build/src/transactions/eth/index.js +156 -0
  206. package/ts_build/src/transactions/eth/index.js.map +1 -0
  207. package/ts_build/src/transactions/eth-multisig/abi.d.ts +51 -0
  208. package/ts_build/src/transactions/eth-multisig/abi.d.ts.map +1 -0
  209. package/ts_build/src/transactions/eth-multisig/abi.js +728 -0
  210. package/ts_build/src/transactions/eth-multisig/abi.js.map +1 -0
  211. package/ts_build/src/transactions/eth-multisig/index.d.ts +65 -0
  212. package/ts_build/src/transactions/eth-multisig/index.d.ts.map +1 -0
  213. package/ts_build/src/transactions/eth-multisig/index.js +143 -0
  214. package/ts_build/src/transactions/eth-multisig/index.js.map +1 -0
  215. package/ts_build/src/transactions/index.d.ts +15 -0
  216. package/ts_build/src/transactions/index.d.ts.map +1 -0
  217. package/ts_build/src/transactions/index.js +69 -0
  218. package/ts_build/src/transactions/index.js.map +1 -0
  219. package/ts_build/src/transactions/ltc/index.d.ts +12 -0
  220. package/ts_build/src/transactions/ltc/index.d.ts.map +1 -0
  221. package/ts_build/src/transactions/ltc/index.js +38 -0
  222. package/ts_build/src/transactions/ltc/index.js.map +1 -0
  223. package/ts_build/src/transactions/matic/index.d.ts +9 -0
  224. package/ts_build/src/transactions/matic/index.d.ts.map +1 -0
  225. package/ts_build/src/transactions/matic/index.js +18 -0
  226. package/ts_build/src/transactions/matic/index.js.map +1 -0
  227. package/ts_build/src/transactions/matic-multisig/index.d.ts +65 -0
  228. package/ts_build/src/transactions/matic-multisig/index.d.ts.map +1 -0
  229. package/ts_build/src/transactions/matic-multisig/index.js +143 -0
  230. package/ts_build/src/transactions/matic-multisig/index.js.map +1 -0
  231. package/ts_build/src/transactions/op/index.d.ts +9 -0
  232. package/ts_build/src/transactions/op/index.d.ts.map +1 -0
  233. package/ts_build/src/transactions/op/index.js +18 -0
  234. package/ts_build/src/transactions/op/index.js.map +1 -0
  235. package/ts_build/src/transactions/sol/index.d.ts +56 -0
  236. package/ts_build/src/transactions/sol/index.d.ts.map +1 -0
  237. package/ts_build/src/transactions/sol/index.js +261 -0
  238. package/ts_build/src/transactions/sol/index.js.map +1 -0
  239. package/ts_build/src/transactions/spl/index.d.ts +57 -0
  240. package/ts_build/src/transactions/spl/index.d.ts.map +1 -0
  241. package/ts_build/src/transactions/spl/index.js +129 -0
  242. package/ts_build/src/transactions/spl/index.js.map +1 -0
  243. package/ts_build/src/transactions/xrp/index.d.ts +50 -0
  244. package/ts_build/src/transactions/xrp/index.d.ts.map +1 -0
  245. package/ts_build/src/transactions/xrp/index.js +146 -0
  246. package/ts_build/src/transactions/xrp/index.js.map +1 -0
  247. package/ts_build/src/types/derivation.d.ts +13 -0
  248. package/ts_build/src/types/derivation.d.ts.map +1 -0
  249. package/ts_build/src/types/derivation.js +3 -0
  250. package/ts_build/src/types/derivation.js.map +1 -0
  251. package/ts_build/src/types/index.d.ts +4 -0
  252. package/ts_build/src/types/index.d.ts.map +1 -0
  253. package/ts_build/src/types/index.js +40 -0
  254. package/ts_build/src/types/index.js.map +1 -0
  255. package/ts_build/src/types/message.d.ts +26 -0
  256. package/ts_build/src/types/message.d.ts.map +1 -0
  257. package/ts_build/src/types/message.js +3 -0
  258. package/ts_build/src/types/message.js.map +1 -0
  259. package/ts_build/src/types/utils.d.ts +2 -0
  260. package/ts_build/src/types/utils.d.ts.map +1 -0
  261. package/ts_build/src/types/utils.js +3 -0
  262. package/ts_build/src/types/utils.js.map +1 -0
  263. package/ts_build/src/types/validation.d.ts +5 -0
  264. package/ts_build/src/types/validation.d.ts.map +1 -0
  265. package/ts_build/src/types/validation.js +3 -0
  266. package/ts_build/src/types/validation.js.map +1 -0
  267. package/ts_build/src/utils/bigint.d.ts +14 -0
  268. package/ts_build/src/utils/bigint.d.ts.map +1 -0
  269. package/ts_build/src/utils/bigint.js +165 -0
  270. package/ts_build/src/utils/bigint.js.map +1 -0
  271. package/ts_build/src/utils/index.d.ts +12 -0
  272. package/ts_build/src/utils/index.d.ts.map +1 -0
  273. package/ts_build/src/utils/index.js +118 -0
  274. package/ts_build/src/utils/index.js.map +1 -0
  275. package/ts_build/src/validation/arb/index.d.ts +5 -0
  276. package/ts_build/src/validation/arb/index.d.ts.map +1 -0
  277. package/ts_build/src/validation/arb/index.js +12 -0
  278. package/ts_build/src/validation/arb/index.js.map +1 -0
  279. package/ts_build/src/validation/base/index.d.ts +5 -0
  280. package/ts_build/src/validation/base/index.d.ts.map +1 -0
  281. package/ts_build/src/validation/base/index.js +12 -0
  282. package/ts_build/src/validation/base/index.js.map +1 -0
  283. package/ts_build/src/validation/bch/index.d.ts +6 -0
  284. package/ts_build/src/validation/bch/index.d.ts.map +1 -0
  285. package/ts_build/src/validation/bch/index.js +19 -0
  286. package/ts_build/src/validation/bch/index.js.map +1 -0
  287. package/ts_build/src/validation/btc/index.d.ts +6 -0
  288. package/ts_build/src/validation/btc/index.d.ts.map +1 -0
  289. package/ts_build/src/validation/btc/index.js +19 -0
  290. package/ts_build/src/validation/btc/index.js.map +1 -0
  291. package/ts_build/src/validation/doge/index.d.ts +6 -0
  292. package/ts_build/src/validation/doge/index.d.ts.map +1 -0
  293. package/ts_build/src/validation/doge/index.js +19 -0
  294. package/ts_build/src/validation/doge/index.js.map +1 -0
  295. package/ts_build/src/validation/eth/index.d.ts +9 -0
  296. package/ts_build/src/validation/eth/index.d.ts.map +1 -0
  297. package/ts_build/src/validation/eth/index.js +28 -0
  298. package/ts_build/src/validation/eth/index.js.map +1 -0
  299. package/ts_build/src/validation/index.d.ts +9 -0
  300. package/ts_build/src/validation/index.d.ts.map +1 -0
  301. package/ts_build/src/validation/index.js +42 -0
  302. package/ts_build/src/validation/index.js.map +1 -0
  303. package/ts_build/src/validation/ltc/index.d.ts +6 -0
  304. package/ts_build/src/validation/ltc/index.d.ts.map +1 -0
  305. package/ts_build/src/validation/ltc/index.js +19 -0
  306. package/ts_build/src/validation/ltc/index.js.map +1 -0
  307. package/ts_build/src/validation/matic/index.d.ts +5 -0
  308. package/ts_build/src/validation/matic/index.d.ts.map +1 -0
  309. package/ts_build/src/validation/matic/index.js +12 -0
  310. package/ts_build/src/validation/matic/index.js.map +1 -0
  311. package/ts_build/src/validation/op/index.d.ts +5 -0
  312. package/ts_build/src/validation/op/index.d.ts.map +1 -0
  313. package/ts_build/src/validation/op/index.js +12 -0
  314. package/ts_build/src/validation/op/index.js.map +1 -0
  315. package/ts_build/src/validation/sol/index.d.ts +9 -0
  316. package/ts_build/src/validation/sol/index.d.ts.map +1 -0
  317. package/ts_build/src/validation/sol/index.js +34 -0
  318. package/ts_build/src/validation/sol/index.js.map +1 -0
  319. package/ts_build/src/validation/xrp/index.d.ts +7 -0
  320. package/ts_build/src/validation/xrp/index.d.ts.map +1 -0
  321. package/ts_build/src/validation/xrp/index.js +50 -0
  322. package/ts_build/src/validation/xrp/index.js.map +1 -0
  323. package/ts_build/test/address.test.d.ts +2 -0
  324. package/ts_build/test/address.test.d.ts.map +1 -0
  325. package/ts_build/test/address.test.js +139 -0
  326. package/ts_build/test/address.test.js.map +1 -0
  327. package/ts_build/test/bigint.test.d.ts +2 -0
  328. package/ts_build/test/bigint.test.d.ts.map +1 -0
  329. package/ts_build/test/bigint.test.js +298 -0
  330. package/ts_build/test/bigint.test.js.map +1 -0
  331. package/ts_build/test/transactions.test.d.ts +2 -0
  332. package/ts_build/test/transactions.test.d.ts.map +1 -0
  333. package/ts_build/test/transactions.test.js +1549 -0
  334. package/ts_build/test/transactions.test.js.map +1 -0
  335. package/ts_build/test/utils.test.d.ts +2 -0
  336. package/ts_build/test/utils.test.d.ts.map +1 -0
  337. package/ts_build/test/utils.test.js +137 -0
  338. package/ts_build/test/utils.test.js.map +1 -0
  339. package/ts_build/test/validation.test.d.ts +2 -0
  340. package/ts_build/test/validation.test.d.ts.map +1 -0
  341. package/ts_build/test/validation.test.js +210 -0
  342. package/ts_build/test/validation.test.js.map +1 -0
  343. package/tsconfig.json +29 -0
@@ -0,0 +1,238 @@
1
+ import type { BigIntLike } from '../types/utils';
2
+
3
+
4
+ function isValidDenominator(val: BigIntLike): boolean {
5
+ return isBigIntLike(val) && BigInt(val) !== 0n;
6
+ };
7
+
8
+ /**
9
+ * Checks if a value is a BigInt-like type (bigint, number, or numeric string)
10
+ * @param value
11
+ * @returns {boolean}
12
+ */
13
+ export function isBigIntLike(value?: any): boolean {
14
+ return value != null &&
15
+ value !== '' &&
16
+ (
17
+ typeof value === 'bigint' ||
18
+ (typeof value === 'number' && !isNaN(value)) ||
19
+ !isNaN(Number(value))
20
+ );
21
+ };
22
+
23
+ /**
24
+ * This is a special wrapper for BigInt() that assumes a string _without_ a prefix is hex.
25
+ * @example sToBigInt('0x12') => 18n
26
+ * @example sToBigInt('12') => 18n // assumes hex
27
+ * @example sToBigInt(12) => 12n
28
+ * @example sToBigInt('0o12') => 10n // octal
29
+ * @returns {bigint}
30
+ */
31
+ export function sToBigInt(value: number | string): bigint {
32
+ const isNonPrefixedString = typeof value === 'string' &&
33
+ !value?.startsWith('0x') &&
34
+ !value?.startsWith('0o');
35
+ return BigInt(isNonPrefixedString ? ('0x' + value) : value);
36
+ };
37
+
38
+ /**
39
+ * Math.max() for a mixed array of BigInts, Numbers, and Strings
40
+ * @param {Array<T = BigIntLike>} arr Array of T (T by default can be a mix of BigInts, Numbers, and/or Strings)
41
+ * @returns {T = BigIntLike} Returns the max entry
42
+ */
43
+ export function max<T = BigIntLike>(arr: Array<T>): T {
44
+ if (!Array.isArray(arr)) throw new Error('Input must be an array');
45
+ if (!arr.every(isBigIntLike)) throw new Error('Array must contain only BigInt-like values');
46
+ return arr.reduce((max, cur) => cur > max ? cur : max, arr[0]);
47
+ };
48
+
49
+
50
+ /**
51
+ * Math.min() for a mixed array of BigInts, Numbers, and Strings
52
+ * @param {Array<T = BigIntLike>} arr Array of T (T by default can be a mix of BigInts, Numbers, and/or Strings)
53
+ * @returns {T = BigIntLike} Returns the min entry
54
+ */
55
+ export function min<T = BigIntLike>(arr: Array<T>): T {
56
+ if (!Array.isArray(arr)) throw new Error('Input must be an array');
57
+ if (!arr.every(isBigIntLike)) throw new Error('Array must contain only BigInt-like values');
58
+ return arr.reduce((min, cur) => cur < min ? cur : min, arr[0]);
59
+ };
60
+
61
+
62
+ /**
63
+ * Divide BigInts with precision. This works for Numbers and strings as well since it converts the inputs to BigInts
64
+ * @param {BigIntLike} numerator
65
+ * @param {BigIntLike} denominator
66
+ * @param {BigIntLike} precision (optional) Default: 18
67
+ * @returns {number}
68
+ */
69
+ export function divToFloat(numerator: BigIntLike, denominator: BigIntLike, precision?: number) {
70
+ if (!isBigIntLike(numerator)) throw new Error('Invalid numerator');
71
+ if (!isBigIntLike(denominator)) throw new Error('Invalid denominator');
72
+ if (!isValidDenominator(denominator)) throw new Error('Division by zero');
73
+ const [nLeft, nRightInitial = ''] = numerator.toString().split('.');
74
+ let nRight = nRightInitial;
75
+ const [, dRight = ''] = denominator.toString().split('.');
76
+ if (precision == null) {
77
+ precision = Math.max(nRight.length, dRight.length, 18);
78
+ }
79
+ nRight = nRight.padEnd(precision, '0');
80
+ const scaledNumerator = BigInt(nLeft + nRight);
81
+ const quotient = scaledNumerator / BigInt(denominator);
82
+ return Number(quotient) / (10 ** precision);
83
+ };
84
+
85
+
86
+ /**
87
+ * Divide BigInts with precision. This works for Numbers and strings as well since it converts the inputs to BigInts
88
+ * @param {BigIntLike} numerator
89
+ * @param {BigIntLike} denominator
90
+ * @returns {bigint} Rounded BigInt
91
+ */
92
+ export function div(numerator: BigIntLike, denominator: BigIntLike): bigint {
93
+ if (!isBigIntLike(numerator)) throw new Error('Invalid numerator');
94
+ if (!isBigIntLike(denominator)) throw new Error('Invalid denominator');
95
+ if (!isValidDenominator(denominator)) throw new Error('Division by zero');
96
+ const [nLeft, nRightInitial = ''] = numerator.toString().split('.');
97
+ let nRight = nRightInitial;
98
+ const [, dRight = ''] = denominator.toString().split('.');
99
+ const precision = Math.max(nRight.length, dRight.length, 18);
100
+ nRight = nRight.padEnd(precision, '0');
101
+
102
+ const scaledNumerator = BigInt(nLeft + nRight);
103
+ const quotient = (scaledNumerator / BigInt(denominator)).toString();
104
+ return BigInt(quotient.slice(0, -precision)) + BigInt(Math.round(Number('.' + quotient.slice(-precision))));
105
+ };
106
+
107
+
108
+ /**
109
+ * Multiply mixed numbers. If decimals are given, this maintains precision of any given decimals until the very end to get the most precise result.
110
+ * @example mul(2n, 1.4) => 2.8 => 3n
111
+ * @example mul(1n, 1.1) => 1.1 => 1n
112
+ *
113
+ * @param {...BigIntLike} nums
114
+ * @returns {bigint} Returns a rounded bigint
115
+ */
116
+ export function mul(...nums: Array<BigIntLike>): bigint {
117
+ if (!nums.length || !nums.every(isBigIntLike)) throw new Error('Input must contain only BigInt-like values');
118
+ const precision = nums.reduce<number>((p: number, num: BigIntLike) => Math.max(p, num.toString().split('.')[1]?.length || 0), 0);
119
+
120
+ if (precision == 0) { // all numbers are integers
121
+ return nums.reduce<bigint>((acc: bigint, cur: BigIntLike) => acc * BigInt(cur), 1n);
122
+ }
123
+
124
+ let productScaled = 1n;
125
+ for (const cur of nums) {
126
+ const [left, rightInitial = ''] = cur.toString().split('.');
127
+ const right = rightInitial.padEnd(precision, '0');
128
+ productScaled *= BigInt(left + right);
129
+ }
130
+ const whole = productScaled.toString().slice(0, -(precision * nums.length));
131
+ const decimal = productScaled.toString().slice(-(precision * nums.length));
132
+ return BigInt(whole) + BigInt(Math.round(Number('.' + decimal)));
133
+ };
134
+
135
+
136
+ /**
137
+ * Same as mul() but returns the floor of the result
138
+ * @example mul(2n, 1.4) => 2.8 => 2n
139
+ * @example mul(1n, 1.1) => 1.1 => 1n
140
+ *
141
+ * @param {...BigIntLike} nums
142
+ * @returns {bigint} Returns a floor-rounded BigInt
143
+ */
144
+ export function mulFloor(...nums: Array<BigIntLike>): bigint {
145
+ if (!nums.length || !nums.every(isBigIntLike)) throw new Error('Input must contain only BigInt-like values');
146
+ const precision = nums.reduce<number>((p: number, num: BigIntLike) => Math.max(p, num.toString().split('.')[1]?.length || 0), 0);
147
+
148
+ if (precision == 0) {
149
+ return nums.reduce<bigint>((acc: bigint, cur: BigIntLike) => acc * BigInt(cur), 1n);
150
+ }
151
+
152
+ let productScaled = 1n;
153
+ for (const cur of nums) {
154
+ const [left, rightInitial = ''] = cur.toString().split('.');
155
+ const right = rightInitial.padEnd(precision, '0');
156
+ productScaled *= BigInt(left + right);
157
+ }
158
+ const whole = productScaled.toString().slice(0, -(precision * nums.length));
159
+ // disregard the decimal since this is a floor
160
+ return BigInt(whole);
161
+ };
162
+
163
+
164
+ /**
165
+ * Same as mul() but returns the ceiling of the result
166
+ * @example mul(2n, 1.4) => 2.8 => 3n
167
+ * @example mul(1n, 1.1) => 1.1 => 2n
168
+ *
169
+ * @param {...BigIntLike} nums
170
+ * @returns {bigint} Returns a ceiling-rounded BigInt
171
+ */
172
+ export function mulCeil(...nums: Array<BigIntLike>): bigint {
173
+ if (!nums.length || !nums.every(isBigIntLike)) throw new Error('Input must contain only BigInt-like values');
174
+ const precision = nums.reduce<number>((p, num) => Math.max(p, num.toString().split('.')[1]?.length || 0), 0);
175
+
176
+ if (precision == 0) { // All nums are integers, so can just convert all to bigints
177
+ return nums.reduce<bigint>((acc, cur) => acc * BigInt(cur), 1n);
178
+ }
179
+
180
+ let productScaled = 1n;
181
+ for (const cur of nums) {
182
+ const [left, rightInitial = ''] = cur.toString().split('.');
183
+ const right = rightInitial.padEnd(precision, '0');
184
+ productScaled *= BigInt(left + right);
185
+ }
186
+ const whole = productScaled.toString().slice(0, -(precision * nums.length));
187
+ const decimal = productScaled.toString().slice(-(precision * nums.length));
188
+ return BigInt(whole) + (BigInt(decimal) > 0n ? 1n : 0n);
189
+ };
190
+
191
+
192
+ /**
193
+ * To be used with JSON.stringify to convert BigInt values to a serializable format
194
+ * @example
195
+ * const obj = { a: 123n, b: 'test' };
196
+ * JSON.stringify(obj, JSONStringifyBigIntReplacer);
197
+ * // Result: '{"a":{"data":"123","type":"BigInt"},"b":"test"}'
198
+ */
199
+ export function JSONStringifyBigIntReplacer(_key: string, value: any) {
200
+ return typeof value === 'bigint' ? { data: value.toString(), type: 'BigInt' } : value;
201
+ };
202
+
203
+
204
+ /**
205
+ * To be used with JSON.parse to revive BigInt values from the serializable format
206
+ * @example
207
+ * const str = '{"a":{"data":"123","type":"BigInt"},"b":"test"}';
208
+ * JSON.parse(str, JSONParseBigIntReviver);
209
+ * // Result: { a: 123n, b: 'test' }
210
+ */
211
+ export function JSONParseBigIntReviver(_key: string, value: any) {
212
+ if (value && typeof value === 'object' && value.type === 'BigInt' && typeof value.data === 'string') {
213
+ return BigInt(value.data);
214
+ }
215
+ return value;
216
+ };
217
+
218
+
219
+ /**
220
+ * Converts any BigInt values in an object to Numbers recursively
221
+ * @param {object|array} obj Object or Array to be scrubbed
222
+ * @param {'number'|'string'|'hex'} [destType='number'] Destination type for BigInt conversion
223
+ * @returns {object|array}
224
+ */
225
+ export function scrubBigIntsInObject<T>(obj: T, destType: 'number' | 'string' | 'hex' = 'number'): T {
226
+ if (!obj || typeof obj !== 'object') {
227
+ return obj;
228
+ }
229
+ const toDestType = destType === 'number' ? Number : destType === 'string' ? (val) => val.toString() : (val) => '0x' + val.toString(16);
230
+ for (const [key, val] of Object.entries(obj)) {
231
+ if (typeof val === 'bigint') {
232
+ obj[key] = toDestType(val);
233
+ } else if (val && typeof val === 'object') {
234
+ obj[key] = scrubBigIntsInObject(val, destType);
235
+ }
236
+ }
237
+ return obj;
238
+ }
@@ -0,0 +1,94 @@
1
+ import BitcoreLib from '@bitpay-labs/bitcore-lib';
2
+ import { Constants } from '../constants';
3
+
4
+
5
+ export * as BI from './bigint';
6
+
7
+ export function isNativeSegwit(addressType) {
8
+ return [
9
+ Constants.SCRIPT_TYPES.P2WPKH,
10
+ Constants.SCRIPT_TYPES.P2WSH,
11
+ Constants.SCRIPT_TYPES.P2TR,
12
+ ].includes(addressType);
13
+ }
14
+
15
+ export function getSegwitVersion(addressType) {
16
+ switch (addressType) {
17
+ case Constants.SCRIPT_TYPES.P2WPKH:
18
+ case Constants.SCRIPT_TYPES.P2WSH:
19
+ return 0;
20
+ case Constants.SCRIPT_TYPES.P2TR:
21
+ return 1;
22
+ default:
23
+ return undefined; // non-segwit addressType
24
+ }
25
+ }
26
+
27
+ export function isUtxoChain(chain: string): boolean {
28
+ return Constants.UTXO_CHAINS.includes(chain.toLowerCase());
29
+ }
30
+
31
+ export function isSvmChain(chain: string): boolean {
32
+ return Constants.SVM_CHAINS.includes(chain.toLowerCase());
33
+ }
34
+
35
+ export function isEvmChain(chain: string): boolean {
36
+ return Constants.EVM_CHAINS.includes(chain.toLowerCase());
37
+ }
38
+
39
+ export function isXrpChain(chain: string): boolean {
40
+ return ['xrp'].includes(chain.toLowerCase());
41
+ }
42
+
43
+ export function isSingleAddressChain(chain: string): boolean {
44
+ return !isUtxoChain(chain);
45
+ }
46
+
47
+ export function encodeBuffer(buffer: Buffer, encoding: BufferEncoding | 'base58'): Buffer | string {
48
+ if (!encoding) {
49
+ return buffer;
50
+ } else if (encoding === 'base58') {
51
+ return BitcoreLib.encoding.Base58.encode(buffer);
52
+ } else {
53
+ return buffer.toString(encoding);
54
+ }
55
+ }
56
+
57
+ /**
58
+ * Evaluates if the given string is a valid hex string (with or without 0x prefix)
59
+ * @param {string} str The string to evaluate
60
+ * @returns {boolean} True if the string is a valid hex string (with or without 0x prefix)
61
+ */
62
+ export function isHexString(str: string): boolean {
63
+ if (typeof str !== 'string' || str === '') {
64
+ return false;
65
+ }
66
+ const normalizedStr = str.toLowerCase().slice(0, 2) === '0x' ? str.toLowerCase().slice(2) : str.toLowerCase();
67
+ return Buffer.from(normalizedStr, 'hex').toString('hex') === normalizedStr;
68
+ }
69
+
70
+ /**
71
+ * Converts a number, string, or bigint to a hex string with 0x prefix
72
+ * This function makes the following assumption(s):
73
+ * 1. If the input is a non-prefixed numeric string (e.g. '123'), it is assumed to be a decimal number
74
+ * 2. If the input is a non-prefixed hex string (e.g. '123a'), it is assumed to be a hex number (as opposed to a utf8 string to be converted to hex)
75
+ * @param {number|string|bigint} input The input number, string, or bigint
76
+ * @returns {string} The hex string with 0x prefix
77
+ */
78
+ export function toHex(input: number | string | bigint): string {
79
+ if ((typeof input !== 'number' && typeof input !== 'string' && typeof input !== 'bigint') || input === '') {
80
+ throw new Error(`Input for toHex must be a number, string (non-empty), or bigint. Got ${!input ? JSON.stringify(input) : `typeof ${typeof input}`}`);
81
+ }
82
+ try {
83
+ return '0x' + BigInt(input).toString(16);
84
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
85
+ } catch (e) {
86
+ if (typeof input === 'string' && isHexString(input)) {
87
+ // BigInt() fails on non-prefixed hex strings (e.g. 'abc123')
88
+ return toHex('0x' + input);
89
+ }
90
+ throw new Error(`Invalid input for toHex: ${input === '' ? "''" : input}`);
91
+ }
92
+ }
93
+
94
+
@@ -0,0 +1,8 @@
1
+ import { EthValidation } from '../eth';
2
+
3
+ export class ArbValidation extends EthValidation {
4
+ constructor() {
5
+ super();
6
+ this.regex = /arbitrum/i;
7
+ }
8
+ }
@@ -0,0 +1,8 @@
1
+ import { EthValidation } from '../eth';
2
+
3
+ export class BaseValidation extends EthValidation {
4
+ constructor() {
5
+ super();
6
+ this.regex = /base/i;
7
+ }
8
+ }
@@ -0,0 +1,17 @@
1
+ import BitcoreCash from '@bitpay-labs/bitcore-lib-cash';
2
+ import type { IValidation } from '../../types/validation';
3
+
4
+ export class BchValidation implements IValidation {
5
+ validateAddress(network: string, address: string): boolean {
6
+ const AddressCash = BitcoreCash.Address;
7
+ // Regular Address: try Bitcoin Cash
8
+ return AddressCash.isValid(address, network);
9
+ }
10
+
11
+ validateUri(addressUri: string): boolean {
12
+ // Check if the input is a valid uri or address
13
+ const URICash = BitcoreCash.URI;
14
+ // Bip21 uri
15
+ return URICash.isValid(addressUri);
16
+ }
17
+ }
@@ -0,0 +1,17 @@
1
+ import Bitcore from '@bitpay-labs/bitcore-lib';
2
+ import type { IValidation } from '../../types/validation';
3
+
4
+ export class BtcValidation implements IValidation {
5
+ validateAddress(network: string, address: string): boolean {
6
+ const Address = Bitcore.Address;
7
+ // Regular Address: try Bitcoin
8
+ return Address.isValid(address, network);
9
+ }
10
+
11
+ validateUri(addressUri: string): boolean {
12
+ // Check if the input is a valid uri or address
13
+ const URI = Bitcore.URI;
14
+ // Bip21 uri
15
+ return URI.isValid(addressUri);
16
+ }
17
+ }
@@ -0,0 +1,16 @@
1
+ import BitcoreDoge from '@bitpay-labs/bitcore-lib-doge';
2
+ import type { IValidation } from '../../types/validation';
3
+
4
+ export class DogeValidation implements IValidation {
5
+ validateAddress(network: string, address: string): boolean {
6
+ const Address = BitcoreDoge.Address;
7
+ return Address.isValid(address, network);
8
+ }
9
+
10
+ validateUri(addressUri: string): boolean {
11
+ // Check if the input is a valid uri or address
12
+ const URICash = BitcoreDoge.URI;
13
+ // Bip21 uri
14
+ return URICash.isValid(addressUri);
15
+ }
16
+ }
@@ -0,0 +1,29 @@
1
+ import { utils } from 'web3';
2
+ import type { IValidation } from '../../types/validation';
3
+
4
+ export class EthValidation implements IValidation {
5
+ regex: RegExp;
6
+
7
+ constructor() {
8
+ this.regex = /ethereum/i;
9
+ }
10
+
11
+ validateAddress(_network: string, address: string): boolean {
12
+ return utils.isAddress(address);
13
+ }
14
+
15
+ validateUri(addressUri: string): boolean {
16
+ if (!addressUri) {
17
+ return false;
18
+ }
19
+ const address = this.extractAddress(addressUri);
20
+ const prefix = this.regex.exec(addressUri);
21
+ return !!prefix && utils.isAddress(address);
22
+ }
23
+
24
+ protected extractAddress(data) {
25
+ const prefix = /^[a-z]+:/i;
26
+ const params = /([?&](value|gas|gasPrice|gasLimit)=(\d+([,.]\d+)?))+/i;
27
+ return data.replace(prefix, '').replace(params, '');
28
+ }
29
+ }
@@ -0,0 +1,43 @@
1
+ import { ArbValidation } from './arb';
2
+ import { BaseValidation } from './base';
3
+ import { BchValidation } from './bch';
4
+ import { BtcValidation } from './btc';
5
+ import { DogeValidation } from './doge';
6
+ import { EthValidation } from './eth';
7
+ import { LtcValidation } from './ltc';
8
+ import { MaticValidation } from './matic';
9
+ import { OpValidation } from './op';
10
+ import { SolValidation } from './sol';
11
+ import { XrpValidation } from './xrp';
12
+ import type { IValidation } from '../types/validation';
13
+
14
+ const validation: { [chain: string]: IValidation } = {
15
+ BTC: new BtcValidation(),
16
+ BCH: new BchValidation(),
17
+ ETH: new EthValidation(),
18
+ XRP: new XrpValidation(),
19
+ DOGE: new DogeValidation(),
20
+ LTC: new LtcValidation(),
21
+ MATIC: new MaticValidation(),
22
+ ARB: new ArbValidation(),
23
+ BASE: new BaseValidation(),
24
+ OP: new OpValidation(),
25
+ SOL: new SolValidation(),
26
+ };
27
+
28
+ export class ValidationProxy {
29
+ get(chain) {
30
+ const normalizedChain = chain.toUpperCase();
31
+ return validation[normalizedChain];
32
+ }
33
+
34
+ validateAddress(chain, network, address) {
35
+ return this.get(chain).validateAddress(network, address);
36
+ }
37
+
38
+ validateUri(chain, addressUri) {
39
+ return this.get(chain).validateUri(addressUri);
40
+ }
41
+ }
42
+
43
+ export default new ValidationProxy();
@@ -0,0 +1,16 @@
1
+ import BitcoreLtc from '@bitpay-labs/bitcore-lib-ltc';
2
+ import type { IValidation } from '../../types/validation';
3
+
4
+ export class LtcValidation implements IValidation {
5
+ validateAddress(network: string, address: string): boolean {
6
+ const Address = BitcoreLtc.Address;
7
+ return Address.isValid(address, network);
8
+ }
9
+
10
+ validateUri(addressUri: string): boolean {
11
+ // Check if the input is a valid uri or address
12
+ const URICash = BitcoreLtc.URI;
13
+ // Bip21 uri
14
+ return URICash.isValid(addressUri);
15
+ }
16
+ }
@@ -0,0 +1,8 @@
1
+ import { EthValidation } from '../eth';
2
+
3
+ export class MaticValidation extends EthValidation {
4
+ constructor() {
5
+ super();
6
+ this.regex = /matic|polygon/i;
7
+ }
8
+ }
@@ -0,0 +1,8 @@
1
+ import { EthValidation } from '../eth';
2
+
3
+ export class OpValidation extends EthValidation {
4
+ constructor() {
5
+ super();
6
+ this.regex = /optimism/i;
7
+ }
8
+ }
@@ -0,0 +1,34 @@
1
+ import { address as SolAddress } from '@solana/kit';
2
+ import type { IValidation } from '../../types/validation';
3
+
4
+ export class SolValidation implements IValidation {
5
+ regex: RegExp;
6
+
7
+ constructor() {
8
+ this.regex = /solana/i;
9
+ }
10
+
11
+ validateAddress(_network: string, address: string): boolean {
12
+ try {
13
+ SolAddress(address);
14
+ return true;
15
+ } catch {
16
+ return false;
17
+ }
18
+ }
19
+
20
+ validateUri(addressUri: string): boolean {
21
+ if (!addressUri) {
22
+ return false;
23
+ }
24
+ const address = this.extractAddress(addressUri);
25
+ const prefix = this.regex.exec(addressUri);
26
+ return !!prefix && this.validateAddress('livenet', address);
27
+ }
28
+
29
+ private extractAddress(data) {
30
+ const prefix = /^[a-z]+:/i;
31
+ const params = /([?&](amount|fee)=(\d+([,.]\d+)?))+/i;
32
+ return data.replace(prefix, '').replace(params, '');
33
+ }
34
+ }
@@ -0,0 +1,50 @@
1
+ import Bitcore from '@bitpay-labs/bitcore-lib';
2
+ import baseX from 'base-x';
3
+ import type { IValidation } from '../../types/validation';
4
+
5
+ const RIPPLE_ALPHABET = 'rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz';
6
+
7
+ export class XrpValidation implements IValidation {
8
+ validateAddress(_network: string, address: string): boolean {
9
+ // First ensure it matches regex
10
+ const RippleAddressRegex = new RegExp(/^r[rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz]{27,35}$/);
11
+ if (!address.match(RippleAddressRegex)) {
12
+ return false;
13
+ }
14
+
15
+ // Then ensure it is a valid base58check encoding
16
+ const base58 = baseX(RIPPLE_ALPHABET);
17
+ const buffer = Buffer.from(base58.decode(address));
18
+ const prefix = buffer.subarray(0, 1);
19
+ const data = buffer.subarray(1, -4);
20
+ let hash = Buffer.concat([prefix, data]);
21
+ hash = Bitcore.crypto.Hash.sha256(hash);
22
+ hash = Bitcore.crypto.Hash.sha256(hash);
23
+ const checksum = buffer.subarray(-4).reduce((acc, check, index) => {
24
+ if (check !== hash[index]) {
25
+ // Invalid checksum
26
+ return 0;
27
+ } else return acc || 1;
28
+ });
29
+ if (checksum === 0) {
30
+ return false;
31
+ }
32
+
33
+ return true;
34
+ }
35
+
36
+ validateUri(addressUri: string): boolean {
37
+ if (!addressUri) {
38
+ return false;
39
+ }
40
+ const address = this.extractAddress(addressUri);
41
+ const ripplePrefix = /ripple/i.exec(addressUri);
42
+ return !!ripplePrefix && this.validateAddress('livenet', address);
43
+ }
44
+
45
+ private extractAddress(data) {
46
+ const prefix = /^[a-z]+:/i;
47
+ const params = /([?&](amount|dt)=(\d+([,.]\d+)?))+/i;
48
+ return data.replace(prefix, '').replace(params, '');
49
+ }
50
+ }