@ledgerhq/coin-framework 0.0.2

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 (471) hide show
  1. package/lib/account/accountId.d.ts +16 -0
  2. package/lib/account/accountId.d.ts.map +1 -0
  3. package/lib/account/accountId.js +85 -0
  4. package/lib/account/accountId.js.map +1 -0
  5. package/lib/account/accountName.d.ts +14 -0
  6. package/lib/account/accountName.d.ts.map +1 -0
  7. package/lib/account/accountName.js +17 -0
  8. package/lib/account/accountName.js.map +1 -0
  9. package/lib/account/addAccounts.d.ts +35 -0
  10. package/lib/account/addAccounts.d.ts.map +1 -0
  11. package/lib/account/addAccounts.js +209 -0
  12. package/lib/account/addAccounts.js.map +1 -0
  13. package/lib/account/balanceHistoryCache.d.ts +25 -0
  14. package/lib/account/balanceHistoryCache.d.ts.map +1 -0
  15. package/lib/account/balanceHistoryCache.js +115 -0
  16. package/lib/account/balanceHistoryCache.js.map +1 -0
  17. package/lib/account/groupOperations.d.ts +17 -0
  18. package/lib/account/groupOperations.d.ts.map +1 -0
  19. package/lib/account/groupOperations.js +127 -0
  20. package/lib/account/groupOperations.js.map +1 -0
  21. package/lib/account/helpers.d.ts +44 -0
  22. package/lib/account/helpers.d.ts.map +1 -0
  23. package/lib/account/helpers.js +333 -0
  24. package/lib/account/helpers.js.map +1 -0
  25. package/lib/account/helpers.test.d.ts +2 -0
  26. package/lib/account/helpers.test.d.ts.map +1 -0
  27. package/lib/account/helpers.test.js +366 -0
  28. package/lib/account/helpers.test.js.map +1 -0
  29. package/lib/account/index.d.ts +11 -0
  30. package/lib/account/index.d.ts.map +1 -0
  31. package/lib/account/index.js +27 -0
  32. package/lib/account/index.js.map +1 -0
  33. package/lib/account/ordering.d.ts +10 -0
  34. package/lib/account/ordering.d.ts.map +1 -0
  35. package/lib/account/ordering.js +110 -0
  36. package/lib/account/ordering.js.map +1 -0
  37. package/lib/account/pending.d.ts +33 -0
  38. package/lib/account/pending.d.ts.map +1 -0
  39. package/lib/account/pending.js +64 -0
  40. package/lib/account/pending.js.map +1 -0
  41. package/lib/account/serialization.d.ts +6 -0
  42. package/lib/account/serialization.d.ts.map +1 -0
  43. package/lib/account/serialization.js +118 -0
  44. package/lib/account/serialization.js.map +1 -0
  45. package/lib/account/support.d.ts +9 -0
  46. package/lib/account/support.d.ts.map +1 -0
  47. package/lib/account/support.js +72 -0
  48. package/lib/account/support.js.map +1 -0
  49. package/lib/account.test.d.ts +2 -0
  50. package/lib/account.test.d.ts.map +1 -0
  51. package/lib/account.test.js +257 -0
  52. package/lib/account.test.js.map +1 -0
  53. package/lib/bot/bot-test-context.d.ts +3 -0
  54. package/lib/bot/bot-test-context.d.ts.map +1 -0
  55. package/lib/bot/bot-test-context.js +26 -0
  56. package/lib/bot/bot-test-context.js.map +1 -0
  57. package/lib/bot/bot-test-context.test.d.ts +2 -0
  58. package/lib/bot/bot-test-context.test.d.ts.map +1 -0
  59. package/lib/bot/bot-test-context.test.js +41 -0
  60. package/lib/bot/bot-test-context.test.js.map +1 -0
  61. package/lib/bot/formatters.d.ts +2 -0
  62. package/lib/bot/formatters.d.ts.map +1 -0
  63. package/lib/bot/formatters.js +31 -0
  64. package/lib/bot/formatters.js.map +1 -0
  65. package/lib/bot/specs.d.ts +50 -0
  66. package/lib/bot/specs.d.ts.map +1 -0
  67. package/lib/bot/specs.js +204 -0
  68. package/lib/bot/specs.js.map +1 -0
  69. package/lib/bot/types.d.ts +178 -0
  70. package/lib/bot/types.d.ts.map +1 -0
  71. package/lib/bot/types.js +3 -0
  72. package/lib/bot/types.js.map +1 -0
  73. package/lib/bridge/getAddressWrapper.d.ts +6 -0
  74. package/lib/bridge/getAddressWrapper.d.ts.map +1 -0
  75. package/lib/bridge/getAddressWrapper.js +27 -0
  76. package/lib/bridge/getAddressWrapper.js.map +1 -0
  77. package/lib/bridge/jsHelpers.d.ts +63 -0
  78. package/lib/bridge/jsHelpers.d.ts.map +1 -0
  79. package/lib/bridge/jsHelpers.js +574 -0
  80. package/lib/bridge/jsHelpers.js.map +1 -0
  81. package/lib/cache.d.ts +10 -0
  82. package/lib/cache.d.ts.map +1 -0
  83. package/lib/cache.js +100 -0
  84. package/lib/cache.js.map +1 -0
  85. package/lib/cross.d.ts +42 -0
  86. package/lib/cross.d.ts.map +1 -0
  87. package/lib/cross.js +254 -0
  88. package/lib/cross.js.map +1 -0
  89. package/lib/cross.test.d.ts +2 -0
  90. package/lib/cross.test.d.ts.map +1 -0
  91. package/lib/cross.test.js +99 -0
  92. package/lib/cross.test.js.map +1 -0
  93. package/lib/currencies/BigNumberToLocaleString.d.ts +8 -0
  94. package/lib/currencies/BigNumberToLocaleString.d.ts.map +1 -0
  95. package/lib/currencies/BigNumberToLocaleString.js +65 -0
  96. package/lib/currencies/BigNumberToLocaleString.js.map +1 -0
  97. package/lib/currencies/BigNumberToLocaleString.test.d.ts +2 -0
  98. package/lib/currencies/BigNumberToLocaleString.test.d.ts.map +1 -0
  99. package/lib/currencies/BigNumberToLocaleString.test.js +108 -0
  100. package/lib/currencies/BigNumberToLocaleString.test.js.map +1 -0
  101. package/lib/currencies/CurrencyURIScheme.d.ts +13 -0
  102. package/lib/currencies/CurrencyURIScheme.d.ts.map +1 -0
  103. package/lib/currencies/CurrencyURIScheme.js +124 -0
  104. package/lib/currencies/CurrencyURIScheme.js.map +1 -0
  105. package/lib/currencies/chopCurrencyUnitDecimals.d.ts +3 -0
  106. package/lib/currencies/chopCurrencyUnitDecimals.d.ts.map +1 -0
  107. package/lib/currencies/chopCurrencyUnitDecimals.js +29 -0
  108. package/lib/currencies/chopCurrencyUnitDecimals.js.map +1 -0
  109. package/lib/currencies/formatCurrencyUnit.d.ts +33 -0
  110. package/lib/currencies/formatCurrencyUnit.d.ts.map +1 -0
  111. package/lib/currencies/formatCurrencyUnit.js +126 -0
  112. package/lib/currencies/formatCurrencyUnit.js.map +1 -0
  113. package/lib/currencies/formatShort.d.ts +8 -0
  114. package/lib/currencies/formatShort.d.ts.map +1 -0
  115. package/lib/currencies/formatShort.js +27 -0
  116. package/lib/currencies/formatShort.js.map +1 -0
  117. package/lib/currencies/index.d.ts +13 -0
  118. package/lib/currencies/index.d.ts.map +1 -0
  119. package/lib/currencies/index.js +65 -0
  120. package/lib/currencies/index.js.map +1 -0
  121. package/lib/currencies/localeUtility.d.ts +8 -0
  122. package/lib/currencies/localeUtility.d.ts.map +1 -0
  123. package/lib/currencies/localeUtility.js +49 -0
  124. package/lib/currencies/localeUtility.js.map +1 -0
  125. package/lib/currencies/parseCurrencyUnit.d.ts +4 -0
  126. package/lib/currencies/parseCurrencyUnit.d.ts.map +1 -0
  127. package/lib/currencies/parseCurrencyUnit.js +18 -0
  128. package/lib/currencies/parseCurrencyUnit.js.map +1 -0
  129. package/lib/currencies/sanitizeValueString.d.ts +6 -0
  130. package/lib/currencies/sanitizeValueString.d.ts.map +1 -0
  131. package/lib/currencies/sanitizeValueString.js +56 -0
  132. package/lib/currencies/sanitizeValueString.js.map +1 -0
  133. package/lib/currencies/support.d.ts +8 -0
  134. package/lib/currencies/support.d.ts.map +1 -0
  135. package/lib/currencies/support.js +101 -0
  136. package/lib/currencies/support.js.map +1 -0
  137. package/lib/currencies/valueFromUnit.d.ts +9 -0
  138. package/lib/currencies/valueFromUnit.d.ts.map +1 -0
  139. package/lib/currencies/valueFromUnit.js +14 -0
  140. package/lib/currencies/valueFromUnit.js.map +1 -0
  141. package/lib/derivation.d.ts +232 -0
  142. package/lib/derivation.d.ts.map +1 -0
  143. package/lib/derivation.js +501 -0
  144. package/lib/derivation.js.map +1 -0
  145. package/lib/env.d.ts +30 -0
  146. package/lib/env.d.ts.map +1 -0
  147. package/lib/env.js +773 -0
  148. package/lib/env.js.map +1 -0
  149. package/lib/errors.d.ts +7 -0
  150. package/lib/errors.d.ts.map +1 -0
  151. package/lib/errors.js +7 -0
  152. package/lib/errors.js.map +1 -0
  153. package/lib/errors.test.d.ts +2 -0
  154. package/lib/errors.test.d.ts.map +1 -0
  155. package/lib/errors.test.js +20 -0
  156. package/lib/errors.test.js.map +1 -0
  157. package/lib/mocks/account.d.ts +25 -0
  158. package/lib/mocks/account.d.ts.map +1 -0
  159. package/lib/mocks/account.js +368 -0
  160. package/lib/mocks/account.js.map +1 -0
  161. package/lib/mocks/fixtures/nfts.d.ts +27 -0
  162. package/lib/mocks/fixtures/nfts.d.ts.map +1 -0
  163. package/lib/mocks/fixtures/nfts.js +398 -0
  164. package/lib/mocks/fixtures/nfts.js.map +1 -0
  165. package/lib/mocks/fixtures/nfts.test.d.ts +2 -0
  166. package/lib/mocks/fixtures/nfts.test.d.ts.map +1 -0
  167. package/lib/mocks/fixtures/nfts.test.js +20 -0
  168. package/lib/mocks/fixtures/nfts.test.js.map +1 -0
  169. package/lib/mocks/helpers.d.ts +15 -0
  170. package/lib/mocks/helpers.d.ts.map +1 -0
  171. package/lib/mocks/helpers.js +32 -0
  172. package/lib/mocks/helpers.js.map +1 -0
  173. package/lib/network.d.ts +5 -0
  174. package/lib/network.d.ts.map +1 -0
  175. package/lib/network.js +125 -0
  176. package/lib/network.js.map +1 -0
  177. package/lib/nft/nftId.d.ts +8 -0
  178. package/lib/nft/nftId.d.ts.map +1 -0
  179. package/lib/nft/nftId.js +34 -0
  180. package/lib/nft/nftId.js.map +1 -0
  181. package/lib/operation.d.ts +24 -0
  182. package/lib/operation.d.ts.map +1 -0
  183. package/lib/operation.js +183 -0
  184. package/lib/operation.js.map +1 -0
  185. package/lib/promise.d.ts +21 -0
  186. package/lib/promise.d.ts.map +1 -0
  187. package/lib/promise.js +187 -0
  188. package/lib/promise.js.map +1 -0
  189. package/lib/promise.test.d.ts +2 -0
  190. package/lib/promise.test.d.ts.map +1 -0
  191. package/lib/promise.test.js +72 -0
  192. package/lib/promise.test.js.map +1 -0
  193. package/lib/rangeDates.d.ts +29 -0
  194. package/lib/rangeDates.d.ts.map +1 -0
  195. package/lib/rangeDates.js +94 -0
  196. package/lib/rangeDates.js.map +1 -0
  197. package/lib/rangeDates.test.d.ts +2 -0
  198. package/lib/rangeDates.test.d.ts.map +1 -0
  199. package/lib/rangeDates.test.js +51 -0
  200. package/lib/rangeDates.test.js.map +1 -0
  201. package/lib/test-helpers/staticTime.d.ts +2 -0
  202. package/lib/test-helpers/staticTime.d.ts.map +1 -0
  203. package/lib/test-helpers/staticTime.js +10 -0
  204. package/lib/test-helpers/staticTime.js.map +1 -0
  205. package/lib/transaction/common.d.ts +26 -0
  206. package/lib/transaction/common.d.ts.map +1 -0
  207. package/lib/transaction/common.js +123 -0
  208. package/lib/transaction/common.js.map +1 -0
  209. package/lib-es/account/accountId.d.ts +16 -0
  210. package/lib-es/account/accountId.d.ts.map +1 -0
  211. package/lib-es/account/accountId.js +74 -0
  212. package/lib-es/account/accountId.js.map +1 -0
  213. package/lib-es/account/accountName.d.ts +14 -0
  214. package/lib-es/account/accountName.d.ts.map +1 -0
  215. package/lib-es/account/accountName.js +12 -0
  216. package/lib-es/account/accountName.js.map +1 -0
  217. package/lib-es/account/addAccounts.d.ts +35 -0
  218. package/lib-es/account/addAccounts.d.ts.map +1 -0
  219. package/lib-es/account/addAccounts.js +200 -0
  220. package/lib-es/account/addAccounts.js.map +1 -0
  221. package/lib-es/account/balanceHistoryCache.d.ts +25 -0
  222. package/lib-es/account/balanceHistoryCache.d.ts.map +1 -0
  223. package/lib-es/account/balanceHistoryCache.js +109 -0
  224. package/lib-es/account/balanceHistoryCache.js.map +1 -0
  225. package/lib-es/account/groupOperations.d.ts +17 -0
  226. package/lib-es/account/groupOperations.d.ts.map +1 -0
  227. package/lib-es/account/groupOperations.js +122 -0
  228. package/lib-es/account/groupOperations.js.map +1 -0
  229. package/lib-es/account/helpers.d.ts +44 -0
  230. package/lib-es/account/helpers.d.ts.map +1 -0
  231. package/lib-es/account/helpers.js +303 -0
  232. package/lib-es/account/helpers.js.map +1 -0
  233. package/lib-es/account/helpers.test.d.ts +2 -0
  234. package/lib-es/account/helpers.test.d.ts.map +1 -0
  235. package/lib-es/account/helpers.test.js +361 -0
  236. package/lib-es/account/helpers.test.js.map +1 -0
  237. package/lib-es/account/index.d.ts +11 -0
  238. package/lib-es/account/index.d.ts.map +1 -0
  239. package/lib-es/account/index.js +11 -0
  240. package/lib-es/account/index.js.map +1 -0
  241. package/lib-es/account/ordering.d.ts +10 -0
  242. package/lib-es/account/ordering.d.ts.map +1 -0
  243. package/lib-es/account/ordering.js +103 -0
  244. package/lib-es/account/ordering.js.map +1 -0
  245. package/lib-es/account/pending.d.ts +33 -0
  246. package/lib-es/account/pending.d.ts.map +1 -0
  247. package/lib-es/account/pending.js +59 -0
  248. package/lib-es/account/pending.js.map +1 -0
  249. package/lib-es/account/serialization.d.ts +6 -0
  250. package/lib-es/account/serialization.d.ts.map +1 -0
  251. package/lib-es/account/serialization.js +112 -0
  252. package/lib-es/account/serialization.js.map +1 -0
  253. package/lib-es/account/support.d.ts +9 -0
  254. package/lib-es/account/support.d.ts.map +1 -0
  255. package/lib-es/account/support.js +64 -0
  256. package/lib-es/account/support.js.map +1 -0
  257. package/lib-es/account.test.d.ts +2 -0
  258. package/lib-es/account.test.d.ts.map +1 -0
  259. package/lib-es/account.test.js +252 -0
  260. package/lib-es/account.test.js.map +1 -0
  261. package/lib-es/bot/bot-test-context.d.ts +3 -0
  262. package/lib-es/bot/bot-test-context.d.ts.map +1 -0
  263. package/lib-es/bot/bot-test-context.js +21 -0
  264. package/lib-es/bot/bot-test-context.js.map +1 -0
  265. package/lib-es/bot/bot-test-context.test.d.ts +2 -0
  266. package/lib-es/bot/bot-test-context.test.d.ts.map +1 -0
  267. package/lib-es/bot/bot-test-context.test.js +39 -0
  268. package/lib-es/bot/bot-test-context.test.js.map +1 -0
  269. package/lib-es/bot/formatters.d.ts +2 -0
  270. package/lib-es/bot/formatters.d.ts.map +1 -0
  271. package/lib-es/bot/formatters.js +27 -0
  272. package/lib-es/bot/formatters.js.map +1 -0
  273. package/lib-es/bot/specs.d.ts +50 -0
  274. package/lib-es/bot/specs.d.ts.map +1 -0
  275. package/lib-es/bot/specs.js +193 -0
  276. package/lib-es/bot/specs.js.map +1 -0
  277. package/lib-es/bot/types.d.ts +178 -0
  278. package/lib-es/bot/types.d.ts.map +1 -0
  279. package/lib-es/bot/types.js +2 -0
  280. package/lib-es/bot/types.js.map +1 -0
  281. package/lib-es/bridge/getAddressWrapper.d.ts +6 -0
  282. package/lib-es/bridge/getAddressWrapper.d.ts.map +1 -0
  283. package/lib-es/bridge/getAddressWrapper.js +25 -0
  284. package/lib-es/bridge/getAddressWrapper.js.map +1 -0
  285. package/lib-es/bridge/jsHelpers.d.ts +63 -0
  286. package/lib-es/bridge/jsHelpers.d.ts.map +1 -0
  287. package/lib-es/bridge/jsHelpers.js +562 -0
  288. package/lib-es/bridge/jsHelpers.js.map +1 -0
  289. package/lib-es/cache.d.ts +10 -0
  290. package/lib-es/cache.d.ts.map +1 -0
  291. package/lib-es/cache.js +93 -0
  292. package/lib-es/cache.js.map +1 -0
  293. package/lib-es/cross.d.ts +42 -0
  294. package/lib-es/cross.d.ts.map +1 -0
  295. package/lib-es/cross.js +244 -0
  296. package/lib-es/cross.js.map +1 -0
  297. package/lib-es/cross.test.d.ts +2 -0
  298. package/lib-es/cross.test.d.ts.map +1 -0
  299. package/lib-es/cross.test.js +97 -0
  300. package/lib-es/cross.test.js.map +1 -0
  301. package/lib-es/currencies/BigNumberToLocaleString.d.ts +8 -0
  302. package/lib-es/currencies/BigNumberToLocaleString.d.ts.map +1 -0
  303. package/lib-es/currencies/BigNumberToLocaleString.js +61 -0
  304. package/lib-es/currencies/BigNumberToLocaleString.js.map +1 -0
  305. package/lib-es/currencies/BigNumberToLocaleString.test.d.ts +2 -0
  306. package/lib-es/currencies/BigNumberToLocaleString.test.d.ts.map +1 -0
  307. package/lib-es/currencies/BigNumberToLocaleString.test.js +106 -0
  308. package/lib-es/currencies/BigNumberToLocaleString.test.js.map +1 -0
  309. package/lib-es/currencies/CurrencyURIScheme.d.ts +13 -0
  310. package/lib-es/currencies/CurrencyURIScheme.d.ts.map +1 -0
  311. package/lib-es/currencies/CurrencyURIScheme.js +116 -0
  312. package/lib-es/currencies/CurrencyURIScheme.js.map +1 -0
  313. package/lib-es/currencies/chopCurrencyUnitDecimals.d.ts +3 -0
  314. package/lib-es/currencies/chopCurrencyUnitDecimals.d.ts.map +1 -0
  315. package/lib-es/currencies/chopCurrencyUnitDecimals.js +25 -0
  316. package/lib-es/currencies/chopCurrencyUnitDecimals.js.map +1 -0
  317. package/lib-es/currencies/formatCurrencyUnit.d.ts +33 -0
  318. package/lib-es/currencies/formatCurrencyUnit.d.ts.map +1 -0
  319. package/lib-es/currencies/formatCurrencyUnit.js +121 -0
  320. package/lib-es/currencies/formatCurrencyUnit.js.map +1 -0
  321. package/lib-es/currencies/formatShort.d.ts +8 -0
  322. package/lib-es/currencies/formatShort.d.ts.map +1 -0
  323. package/lib-es/currencies/formatShort.js +20 -0
  324. package/lib-es/currencies/formatShort.js.map +1 -0
  325. package/lib-es/currencies/index.d.ts +13 -0
  326. package/lib-es/currencies/index.d.ts.map +1 -0
  327. package/lib-es/currencies/index.js +16 -0
  328. package/lib-es/currencies/index.js.map +1 -0
  329. package/lib-es/currencies/localeUtility.d.ts +8 -0
  330. package/lib-es/currencies/localeUtility.d.ts.map +1 -0
  331. package/lib-es/currencies/localeUtility.js +43 -0
  332. package/lib-es/currencies/localeUtility.js.map +1 -0
  333. package/lib-es/currencies/parseCurrencyUnit.d.ts +4 -0
  334. package/lib-es/currencies/parseCurrencyUnit.d.ts.map +1 -0
  335. package/lib-es/currencies/parseCurrencyUnit.js +14 -0
  336. package/lib-es/currencies/parseCurrencyUnit.js.map +1 -0
  337. package/lib-es/currencies/sanitizeValueString.d.ts +6 -0
  338. package/lib-es/currencies/sanitizeValueString.d.ts.map +1 -0
  339. package/lib-es/currencies/sanitizeValueString.js +52 -0
  340. package/lib-es/currencies/sanitizeValueString.js.map +1 -0
  341. package/lib-es/currencies/support.d.ts +8 -0
  342. package/lib-es/currencies/support.d.ts.map +1 -0
  343. package/lib-es/currencies/support.js +92 -0
  344. package/lib-es/currencies/support.js.map +1 -0
  345. package/lib-es/currencies/valueFromUnit.d.ts +9 -0
  346. package/lib-es/currencies/valueFromUnit.d.ts.map +1 -0
  347. package/lib-es/currencies/valueFromUnit.js +10 -0
  348. package/lib-es/currencies/valueFromUnit.js.map +1 -0
  349. package/lib-es/derivation.d.ts +232 -0
  350. package/lib-es/derivation.d.ts.map +1 -0
  351. package/lib-es/derivation.js +473 -0
  352. package/lib-es/derivation.js.map +1 -0
  353. package/lib-es/env.d.ts +30 -0
  354. package/lib-es/env.d.ts.map +1 -0
  355. package/lib-es/env.js +758 -0
  356. package/lib-es/env.js.map +1 -0
  357. package/lib-es/errors.d.ts +7 -0
  358. package/lib-es/errors.d.ts.map +1 -0
  359. package/lib-es/errors.js +4 -0
  360. package/lib-es/errors.js.map +1 -0
  361. package/lib-es/errors.test.d.ts +2 -0
  362. package/lib-es/errors.test.d.ts.map +1 -0
  363. package/lib-es/errors.test.js +18 -0
  364. package/lib-es/errors.test.js.map +1 -0
  365. package/lib-es/mocks/account.d.ts +25 -0
  366. package/lib-es/mocks/account.d.ts.map +1 -0
  367. package/lib-es/mocks/account.js +358 -0
  368. package/lib-es/mocks/account.js.map +1 -0
  369. package/lib-es/mocks/fixtures/nfts.d.ts +27 -0
  370. package/lib-es/mocks/fixtures/nfts.d.ts.map +1 -0
  371. package/lib-es/mocks/fixtures/nfts.js +390 -0
  372. package/lib-es/mocks/fixtures/nfts.js.map +1 -0
  373. package/lib-es/mocks/fixtures/nfts.test.d.ts +2 -0
  374. package/lib-es/mocks/fixtures/nfts.test.d.ts.map +1 -0
  375. package/lib-es/mocks/fixtures/nfts.test.js +18 -0
  376. package/lib-es/mocks/fixtures/nfts.test.js.map +1 -0
  377. package/lib-es/mocks/helpers.d.ts +15 -0
  378. package/lib-es/mocks/helpers.d.ts.map +1 -0
  379. package/lib-es/mocks/helpers.js +26 -0
  380. package/lib-es/mocks/helpers.js.map +1 -0
  381. package/lib-es/network.d.ts +5 -0
  382. package/lib-es/network.d.ts.map +1 -0
  383. package/lib-es/network.js +118 -0
  384. package/lib-es/network.js.map +1 -0
  385. package/lib-es/nft/nftId.d.ts +8 -0
  386. package/lib-es/nft/nftId.d.ts.map +1 -0
  387. package/lib-es/nft/nftId.js +29 -0
  388. package/lib-es/nft/nftId.js.map +1 -0
  389. package/lib-es/operation.d.ts +24 -0
  390. package/lib-es/operation.d.ts.map +1 -0
  391. package/lib-es/operation.js +168 -0
  392. package/lib-es/operation.js.map +1 -0
  393. package/lib-es/promise.d.ts +21 -0
  394. package/lib-es/promise.d.ts.map +1 -0
  395. package/lib-es/promise.js +179 -0
  396. package/lib-es/promise.js.map +1 -0
  397. package/lib-es/promise.test.d.ts +2 -0
  398. package/lib-es/promise.test.d.ts.map +1 -0
  399. package/lib-es/promise.test.js +70 -0
  400. package/lib-es/promise.test.js.map +1 -0
  401. package/lib-es/rangeDates.d.ts +29 -0
  402. package/lib-es/rangeDates.d.ts.map +1 -0
  403. package/lib-es/rangeDates.js +84 -0
  404. package/lib-es/rangeDates.js.map +1 -0
  405. package/lib-es/rangeDates.test.d.ts +2 -0
  406. package/lib-es/rangeDates.test.d.ts.map +1 -0
  407. package/lib-es/rangeDates.test.js +49 -0
  408. package/lib-es/rangeDates.test.js.map +1 -0
  409. package/lib-es/test-helpers/staticTime.d.ts +2 -0
  410. package/lib-es/test-helpers/staticTime.d.ts.map +1 -0
  411. package/lib-es/test-helpers/staticTime.js +5 -0
  412. package/lib-es/test-helpers/staticTime.js.map +1 -0
  413. package/lib-es/transaction/common.d.ts +26 -0
  414. package/lib-es/transaction/common.d.ts.map +1 -0
  415. package/lib-es/transaction/common.js +115 -0
  416. package/lib-es/transaction/common.js.map +1 -0
  417. package/package.json +113 -0
  418. package/src/__snapshots__/account.test.ts.snap +2065 -0
  419. package/src/__snapshots__/cross.test.ts.snap +57 -0
  420. package/src/account/accountId.ts +93 -0
  421. package/src/account/accountName.ts +25 -0
  422. package/src/account/addAccounts.ts +236 -0
  423. package/src/account/balanceHistoryCache.ts +146 -0
  424. package/src/account/groupOperations.ts +164 -0
  425. package/src/account/helpers.test.ts +441 -0
  426. package/src/account/helpers.ts +380 -0
  427. package/src/account/index.ts +10 -0
  428. package/src/account/ordering.ts +109 -0
  429. package/src/account/pending.ts +66 -0
  430. package/src/account/serialization.ts +189 -0
  431. package/src/account/support.ts +96 -0
  432. package/src/account.test.ts +237 -0
  433. package/src/bot/bot-test-context.test.ts +37 -0
  434. package/src/bot/bot-test-context.ts +24 -0
  435. package/src/bot/formatters.ts +22 -0
  436. package/src/bot/specs.ts +275 -0
  437. package/src/bot/types.ts +224 -0
  438. package/src/bridge/getAddressWrapper.ts +40 -0
  439. package/src/bridge/jsHelpers.ts +645 -0
  440. package/src/cache.ts +65 -0
  441. package/src/cross.test.ts +112 -0
  442. package/src/cross.ts +366 -0
  443. package/src/currencies/BigNumberToLocaleString.test.ts +155 -0
  444. package/src/currencies/BigNumberToLocaleString.ts +80 -0
  445. package/src/currencies/CurrencyURIScheme.ts +114 -0
  446. package/src/currencies/chopCurrencyUnitDecimals.ts +34 -0
  447. package/src/currencies/formatCurrencyUnit.ts +167 -0
  448. package/src/currencies/formatShort.ts +24 -0
  449. package/src/currencies/index.ts +78 -0
  450. package/src/currencies/localeUtility.ts +50 -0
  451. package/src/currencies/parseCurrencyUnit.ts +16 -0
  452. package/src/currencies/sanitizeValueString.ts +58 -0
  453. package/src/currencies/support.ts +107 -0
  454. package/src/currencies/valueFromUnit.ts +10 -0
  455. package/src/derivation.ts +595 -0
  456. package/src/env.ts +780 -0
  457. package/src/errors.test.ts +21 -0
  458. package/src/errors.ts +9 -0
  459. package/src/mocks/account.ts +476 -0
  460. package/src/mocks/fixtures/nfts.test.ts +25 -0
  461. package/src/mocks/fixtures/nfts.ts +432 -0
  462. package/src/mocks/helpers.ts +38 -0
  463. package/src/network.ts +135 -0
  464. package/src/nft/nftId.ts +26 -0
  465. package/src/operation.ts +212 -0
  466. package/src/promise.test.ts +20 -0
  467. package/src/promise.ts +101 -0
  468. package/src/rangeDates.test.ts +66 -0
  469. package/src/rangeDates.ts +96 -0
  470. package/src/test-helpers/staticTime.ts +4 -0
  471. package/src/transaction/common.ts +157 -0
@@ -0,0 +1,112 @@
1
+ import { genAccount } from "./mocks/account";
2
+ import { getDerivationModesForCurrency } from "./derivation";
3
+ import { listCryptoCurrencies, setSupportedCurrencies } from "./currencies";
4
+ import {
5
+ accountDataToAccount,
6
+ accountToAccountData,
7
+ encode,
8
+ decode,
9
+ } from "./cross";
10
+ import { Account } from "@ledgerhq/types-live";
11
+
12
+ setSupportedCurrencies(["ethereum", "ethereum_classic"]);
13
+
14
+ test("accountDataToAccount / accountToAccountData", () => {
15
+ listCryptoCurrencies().forEach((currency) => {
16
+ getDerivationModesForCurrency(currency).forEach((derivationMode) => {
17
+ const account = genAccount(`${currency.id}_${derivationMode}`, {
18
+ currency,
19
+ });
20
+ const data = accountToAccountData(account);
21
+ expect(accountToAccountData(accountDataToAccount(data))).toMatchObject(
22
+ data
23
+ );
24
+ });
25
+ });
26
+ });
27
+ test("encode/decode", () => {
28
+ const accounts = listCryptoCurrencies().reduce(
29
+ (acc: Account[], currency) =>
30
+ acc.concat(
31
+ getDerivationModesForCurrency(currency).map((derivationMode) => {
32
+ const account = genAccount(`${currency.id}_${derivationMode}`, {
33
+ currency,
34
+ });
35
+ return account;
36
+ })
37
+ ),
38
+ <Account[]>[]
39
+ );
40
+ const data = {
41
+ accounts,
42
+ settings: {
43
+ currenciesSettings: {},
44
+ pairExchanges: {},
45
+ },
46
+ exporterName: "test你好👋",
47
+ exporterVersion: "0.0.0",
48
+ };
49
+ const exp = decode(encode(data));
50
+ expect(exp.meta.exporterName).toEqual(data.exporterName);
51
+ expect(exp.accounts.length).toEqual(data.accounts.length);
52
+ expect(exp.accounts).toMatchObject(data.accounts.map(accountToAccountData));
53
+ });
54
+ test("encode/decode", () => {
55
+ const accounts = Array(3)
56
+ .fill(null)
57
+ .map((_, i) => genAccount("export_" + i));
58
+ const arg = {
59
+ accounts,
60
+ settings: {
61
+ counterValue: "USD",
62
+ pairExchanges: {
63
+ BTC_USD: "KRAKEN",
64
+ },
65
+ currenciesSettings: {
66
+ bitcoin: {
67
+ confirmationsNb: 3,
68
+ },
69
+ },
70
+ blacklistedTokenIds: ["tokenid1", "tokenid2"],
71
+ },
72
+ exporterName: "test",
73
+ exporterVersion: "0.0.0",
74
+ chunkSize: 100,
75
+ };
76
+ const data = encode(arg);
77
+ const res = decode(data);
78
+ expect(res.accounts).toMatchObject(
79
+ accounts.map((a) => ({
80
+ balance: a.balance.toString(),
81
+ currencyId: a.currency.id,
82
+ id: a.id,
83
+ name: a.name,
84
+ index: a.index,
85
+ }))
86
+ );
87
+ expect(res.settings).toMatchObject({
88
+ counterValue: "USD",
89
+ pairExchanges: {
90
+ BTC_USD: "KRAKEN",
91
+ },
92
+ currenciesSettings: {
93
+ bitcoin: {
94
+ confirmationsNb: 3,
95
+ },
96
+ },
97
+ blacklistedTokenIds: ["tokenid1", "tokenid2"],
98
+ });
99
+ expect(res.settings).not.toMatchObject({
100
+ counterValue: "USD",
101
+ pairExchanges: {
102
+ BTC_USD: "KRAKEN",
103
+ },
104
+ currenciesSettings: {
105
+ bitcoin: {
106
+ confirmationsNb: 3,
107
+ },
108
+ },
109
+ blacklistedTokenIds: ["tokenid3"],
110
+ });
111
+ expect(res).toMatchSnapshot();
112
+ });
package/src/cross.ts ADDED
@@ -0,0 +1,366 @@
1
+ // cross helps dealing with cross-project feature like export/import & cross project conversions
2
+ import { BigNumber } from "bignumber.js";
3
+ import compressjs from "@ledgerhq/compressjs";
4
+ import {
5
+ runDerivationScheme,
6
+ getDerivationScheme,
7
+ asDerivationMode,
8
+ } from "./derivation";
9
+ import { decodeAccountId, emptyHistoryCache } from "./account";
10
+ import { getCryptoCurrencyById } from "./currencies";
11
+ import type { Account, CryptoCurrencyIds } from "@ledgerhq/types-live";
12
+
13
+ export type AccountData = {
14
+ id: string;
15
+ currencyId: string;
16
+ freshAddress?: string;
17
+ seedIdentifier: string;
18
+ derivationMode: string;
19
+ // we are unsafe at this stage, validation is done later
20
+ name: string;
21
+ index: number;
22
+ balance: string;
23
+ };
24
+
25
+ export type CryptoSettings = {
26
+ confirmationsNb?: number;
27
+ };
28
+
29
+ export type Settings = {
30
+ counterValue?: string;
31
+ currenciesSettings: Record<CryptoCurrencyIds, CryptoSettings>;
32
+ pairExchanges: Record<string, string | null | undefined>;
33
+ blacklistedTokenIds?: string[];
34
+ hideEmptyTokenAccounts?: boolean;
35
+ };
36
+
37
+ export type DataIn = {
38
+ // accounts to export (filter them to only be the visible ones)
39
+ accounts: Account[];
40
+ // settings
41
+ settings: Settings;
42
+ // the name of the exporter. e.g. "desktop" for the desktop app
43
+ exporterName: string;
44
+ // the version of the exporter. e.g. the desktop app version
45
+ exporterVersion: string;
46
+ };
47
+
48
+ type Meta = {
49
+ exporterName: string;
50
+ exporterVersion: string;
51
+ };
52
+
53
+ export type Result = {
54
+ accounts: AccountData[];
55
+ settings: Settings;
56
+ meta: Meta;
57
+ };
58
+
59
+ export function encode({
60
+ accounts,
61
+ settings,
62
+ exporterName,
63
+ exporterVersion,
64
+ }: DataIn): string {
65
+ return Buffer.from(
66
+ compressjs.Bzip2.compressFile(
67
+ Buffer.from(
68
+ JSON.stringify({
69
+ meta: {
70
+ exporterName,
71
+ exporterVersion,
72
+ },
73
+ accounts: accounts.map(accountToAccountData),
74
+ settings,
75
+ })
76
+ )
77
+ )
78
+ ).toString("binary");
79
+ }
80
+
81
+ const asResultMeta = (unsafe: Record<string, any>): Meta => {
82
+ if (typeof unsafe !== "object" || !unsafe) {
83
+ throw new Error("invalid meta data");
84
+ }
85
+
86
+ const { exporterName, exporterVersion } = unsafe;
87
+
88
+ if (typeof exporterName !== "string") {
89
+ throw new Error("invalid meta.exporterName");
90
+ }
91
+
92
+ if (typeof exporterVersion !== "string") {
93
+ throw new Error("invalid meta.exporterVersion");
94
+ }
95
+
96
+ return {
97
+ exporterName,
98
+ exporterVersion,
99
+ };
100
+ };
101
+
102
+ const asResultAccount = (unsafe: Record<string, any>): AccountData => {
103
+ if (typeof unsafe !== "object" || !unsafe) {
104
+ throw new Error("invalid account data");
105
+ }
106
+
107
+ const {
108
+ id,
109
+ currencyId,
110
+ freshAddress,
111
+ seedIdentifier,
112
+ derivationMode,
113
+ name,
114
+ index,
115
+ balance,
116
+ } = unsafe;
117
+
118
+ if (typeof id !== "string") {
119
+ throw new Error("invalid account.id");
120
+ }
121
+
122
+ if (typeof currencyId !== "string") {
123
+ throw new Error("invalid account.currencyId");
124
+ }
125
+
126
+ if (typeof seedIdentifier !== "string") {
127
+ throw new Error("invalid account.seedIdentifier");
128
+ }
129
+
130
+ if (typeof derivationMode !== "string") {
131
+ throw new Error("invalid account.derivationMode");
132
+ }
133
+
134
+ if (typeof name !== "string") {
135
+ throw new Error("invalid account.name");
136
+ }
137
+
138
+ if (typeof index !== "number") {
139
+ throw new Error("invalid account.index");
140
+ }
141
+
142
+ if (typeof balance !== "string") {
143
+ throw new Error("invalid account.balance");
144
+ }
145
+
146
+ const o: AccountData = {
147
+ id,
148
+ currencyId,
149
+ seedIdentifier,
150
+ derivationMode,
151
+ name,
152
+ index,
153
+ balance,
154
+ };
155
+
156
+ if (typeof freshAddress === "string" && freshAddress) {
157
+ o.freshAddress = freshAddress;
158
+ }
159
+
160
+ return o;
161
+ };
162
+
163
+ const asResultAccounts = (unsafe: unknown): AccountData[] => {
164
+ if (typeof unsafe !== "object" || !unsafe || !Array.isArray(unsafe)) {
165
+ throw new Error("invalid accounts data");
166
+ }
167
+
168
+ return unsafe.map(asResultAccount);
169
+ };
170
+
171
+ const asCryptoSettings = (unsafe: Record<string, any>): CryptoSettings => {
172
+ if (typeof unsafe !== "object" || !unsafe) {
173
+ throw new Error("invalid currency settings data");
174
+ }
175
+
176
+ const { confirmationsNb } = unsafe;
177
+
178
+ if (typeof confirmationsNb === "number") {
179
+ return {
180
+ confirmationsNb,
181
+ };
182
+ }
183
+
184
+ return {};
185
+ };
186
+
187
+ const asResultSettings = (unsafe: Record<string, any>): Settings => {
188
+ if (typeof unsafe !== "object" || !unsafe) {
189
+ throw new Error("invalid settings data");
190
+ }
191
+
192
+ const {
193
+ counterValue,
194
+ currenciesSettings,
195
+ pairExchanges,
196
+ blacklistedTokenIds,
197
+ hideEmptyTokenAccounts,
198
+ } = unsafe;
199
+ const currenciesSettingsSafe: Record<CryptoCurrencyIds, CryptoSettings> = {};
200
+
201
+ if (currenciesSettings && typeof currenciesSettings === "object") {
202
+ for (const k in currenciesSettings) {
203
+ currenciesSettingsSafe[k] = asCryptoSettings(currenciesSettings[k]);
204
+ }
205
+ }
206
+
207
+ const pairExchangesSafe: Record<string, string> = {};
208
+
209
+ if (pairExchanges && typeof pairExchanges === "object") {
210
+ for (const k in pairExchanges) {
211
+ const v = pairExchanges[k];
212
+
213
+ if (v && typeof v === "string") {
214
+ pairExchangesSafe[k] = v;
215
+ }
216
+ }
217
+ }
218
+
219
+ const res: Settings = {
220
+ currenciesSettings: currenciesSettingsSafe,
221
+ pairExchanges: pairExchangesSafe,
222
+ };
223
+
224
+ if (counterValue && typeof counterValue === "string") {
225
+ res.counterValue = counterValue;
226
+ }
227
+
228
+ if (hideEmptyTokenAccounts && typeof hideEmptyTokenAccounts === "boolean") {
229
+ res.hideEmptyTokenAccounts = hideEmptyTokenAccounts;
230
+ }
231
+
232
+ const blacklistedTokenIdsSafe: string[] = [];
233
+
234
+ if (blacklistedTokenIds && Array.isArray(blacklistedTokenIds)) {
235
+ for (const b of blacklistedTokenIds) {
236
+ if (typeof b === "string") {
237
+ blacklistedTokenIdsSafe.push(b);
238
+ }
239
+ }
240
+
241
+ res.blacklistedTokenIds = blacklistedTokenIdsSafe;
242
+ }
243
+
244
+ return res;
245
+ };
246
+
247
+ export function decode(bytes: string): Result {
248
+ const unsafe: Record<string, any> = JSON.parse(
249
+ Buffer.from(
250
+ compressjs.Bzip2.decompressFile(Buffer.from(bytes, "binary"))
251
+ ).toString()
252
+ );
253
+
254
+ if (typeof unsafe !== "object" || !unsafe) {
255
+ throw new Error("invalid data");
256
+ }
257
+
258
+ return {
259
+ meta: asResultMeta(unsafe.meta),
260
+ accounts: asResultAccounts(unsafe.accounts),
261
+ settings: asResultSettings(unsafe.settings),
262
+ };
263
+ }
264
+ export function accountToAccountData({
265
+ id,
266
+ name,
267
+ seedIdentifier,
268
+ derivationMode,
269
+ freshAddress,
270
+ currency,
271
+ index,
272
+ balance,
273
+ }: Account): AccountData {
274
+ const res: AccountData = {
275
+ id,
276
+ name,
277
+ seedIdentifier,
278
+ derivationMode,
279
+ freshAddress,
280
+ currencyId: currency.id,
281
+ index,
282
+ balance: balance.toString(),
283
+ };
284
+
285
+ return res;
286
+ }
287
+ // reverse the account data to an account.
288
+ // this restore the essential data of an account and the result of the fields
289
+ // are assumed to be restored during first sync
290
+ export const accountDataToAccount = ({
291
+ id,
292
+ currencyId,
293
+ freshAddress: inputFreshAddress,
294
+ name,
295
+ index,
296
+ balance,
297
+ derivationMode: derivationModeStr,
298
+ seedIdentifier,
299
+ }: AccountData): Account => {
300
+ const { xpubOrAddress } = decodeAccountId(id); // TODO rename in AccountId xpubOrAddress
301
+
302
+ const derivationMode = asDerivationMode(derivationModeStr);
303
+ const currency = getCryptoCurrencyById(currencyId);
304
+ let xpub = "";
305
+ let freshAddress = inputFreshAddress || "";
306
+ let freshAddressPath = "";
307
+
308
+ if (
309
+ // FIXME Dirty hack, since we have no way here to know if "xpubOrAddress" is one or the other.
310
+ // Proposed fix: https://ledgerhq.atlassian.net/browse/LL-7437
311
+ currency.family === "bitcoin" ||
312
+ currency.family === "cardano"
313
+ ) {
314
+ // In bitcoin implementation, xpubOrAddress field always go in the xpub
315
+ xpub = xpubOrAddress;
316
+ } else {
317
+ if (currency.family === "tezos") {
318
+ xpub = xpubOrAddress;
319
+ } else if (!freshAddress) {
320
+ // otherwise, it's the freshAddress
321
+ freshAddress = xpubOrAddress;
322
+ }
323
+
324
+ freshAddressPath = runDerivationScheme(
325
+ getDerivationScheme({
326
+ currency,
327
+ derivationMode,
328
+ }),
329
+ currency,
330
+ {
331
+ account: index,
332
+ }
333
+ );
334
+ }
335
+
336
+ const balanceBN = new BigNumber(balance);
337
+ const account: Account = {
338
+ type: "Account",
339
+ id,
340
+ derivationMode,
341
+ seedIdentifier,
342
+ xpub,
343
+ name,
344
+ starred: false,
345
+ used: false,
346
+ currency,
347
+ index,
348
+ freshAddress,
349
+ freshAddressPath,
350
+ swapHistory: [],
351
+ // these fields will be completed as we will sync
352
+ freshAddresses: [],
353
+ blockHeight: 0,
354
+ balance: balanceBN,
355
+ spendableBalance: balanceBN,
356
+ operationsCount: 0,
357
+ operations: [],
358
+ pendingOperations: [],
359
+ unit: currency.units[0],
360
+ lastSyncDate: new Date(0),
361
+ creationDate: new Date(),
362
+ balanceHistoryCache: emptyHistoryCache,
363
+ };
364
+
365
+ return account;
366
+ };
@@ -0,0 +1,155 @@
1
+ import { BigNumber } from "bignumber.js";
2
+ import { toLocaleString } from "./BigNumberToLocaleString";
3
+ test("basic toLocaleString usage", () => {
4
+ expect(toLocaleString(new BigNumber(0))).toBe("0");
5
+ expect(toLocaleString(new BigNumber(8))).toBe("8");
6
+ expect(toLocaleString(new BigNumber(123))).toBe("123");
7
+ expect(toLocaleString(new BigNumber(0.001))).toBe("0.001");
8
+ expect(toLocaleString(new BigNumber(13.1))).toBe("13.1");
9
+ expect(toLocaleString(new BigNumber(123.01))).toBe("123.01");
10
+ expect(toLocaleString(new BigNumber(123.012))).toBe("123.012");
11
+ expect(toLocaleString(new BigNumber(1123))).toBe("1,123");
12
+ expect(toLocaleString(new BigNumber("9999999999999999"))).toBe(
13
+ "9,999,999,999,999,999"
14
+ );
15
+ expect(toLocaleString(new BigNumber("9999999999999.99"))).toBe(
16
+ "9,999,999,999,999.99"
17
+ );
18
+ });
19
+ test("toLocaleString to default maximumFractionDigits to 3", () => {
20
+ expect(toLocaleString(new BigNumber(4.44444))).toBe("4.444");
21
+ expect(toLocaleString(new BigNumber(444444.444444444))).toBe("444,444.444");
22
+ expect(toLocaleString(new BigNumber(0.444444444))).toBe("0.444");
23
+ expect(toLocaleString(new BigNumber(9.99999))).toBe("9.999");
24
+ expect(toLocaleString(new BigNumber(111111.111111111))).toBe("111,111.111");
25
+ expect(toLocaleString(new BigNumber(0.999999999))).toBe("0.999");
26
+ expect(toLocaleString(new BigNumber(9.5))).toBe("9.5");
27
+ expect(toLocaleString(new BigNumber(9.9))).toBe("9.9");
28
+ expect(toLocaleString(new BigNumber(99.6))).toBe("99.6");
29
+ expect(toLocaleString(new BigNumber(99.8))).toBe("99.8");
30
+ expect(toLocaleString(new BigNumber(999.7))).toBe("999.7");
31
+ expect(toLocaleString(new BigNumber(999.9))).toBe("999.9");
32
+ expect(toLocaleString(new BigNumber(999999.7))).toBe("999,999.7");
33
+ expect(toLocaleString(new BigNumber(999999.9))).toBe("999,999.9");
34
+ });
35
+ test("toLocaleString minimumFractionDigits", () => {
36
+ expect(
37
+ toLocaleString(new BigNumber(0), "en", {
38
+ minimumFractionDigits: 1,
39
+ })
40
+ ).toBe("0.0");
41
+ expect(
42
+ toLocaleString(new BigNumber(8), "en", {
43
+ minimumFractionDigits: 1,
44
+ })
45
+ ).toBe("8.0");
46
+ expect(
47
+ toLocaleString(new BigNumber(123), "en", {
48
+ minimumFractionDigits: 1,
49
+ })
50
+ ).toBe("123.0");
51
+ expect(
52
+ toLocaleString(new BigNumber(0.001), "en", {
53
+ minimumFractionDigits: 1,
54
+ })
55
+ ).toBe("0.001");
56
+ expect(
57
+ toLocaleString(new BigNumber(9.5), "en", {
58
+ minimumFractionDigits: 1,
59
+ })
60
+ ).toBe("9.5");
61
+ expect(
62
+ toLocaleString(new BigNumber(9.6), "en", {
63
+ minimumFractionDigits: 2,
64
+ })
65
+ ).toBe("9.60");
66
+ expect(
67
+ toLocaleString(new BigNumber(13.1), "en", {
68
+ minimumFractionDigits: 3,
69
+ })
70
+ ).toBe("13.100");
71
+ expect(
72
+ toLocaleString(new BigNumber(123.01), "en", {
73
+ minimumFractionDigits: 5,
74
+ })
75
+ ).toBe("123.01000");
76
+ expect(
77
+ toLocaleString(new BigNumber(123.012), "en", {
78
+ minimumFractionDigits: 10,
79
+ })
80
+ ).toBe("123.0120000000");
81
+ expect(
82
+ toLocaleString(new BigNumber(1123), "en", {
83
+ minimumFractionDigits: 1,
84
+ })
85
+ ).toBe("1,123.0");
86
+ expect(
87
+ toLocaleString(new BigNumber("9999999999999999"), "en", {
88
+ minimumFractionDigits: 1,
89
+ })
90
+ ).toBe("9,999,999,999,999,999.0");
91
+ expect(
92
+ toLocaleString(new BigNumber("9999999999999.999"), "en", {
93
+ minimumFractionDigits: 5,
94
+ })
95
+ ).toBe("9,999,999,999,999.99900");
96
+ });
97
+ test("toLocaleString minimumFractionDigits and maximumFractionDigits", () => {
98
+ expect(
99
+ toLocaleString(new BigNumber(1), "en", {
100
+ minimumFractionDigits: 1,
101
+ maximumFractionDigits: 5,
102
+ })
103
+ ).toBe("1.0");
104
+ expect(
105
+ toLocaleString(new BigNumber(1.003), "en", {
106
+ minimumFractionDigits: 1,
107
+ maximumFractionDigits: 5,
108
+ })
109
+ ).toBe("1.003");
110
+ expect(
111
+ toLocaleString(new BigNumber(1.000003), "en", {
112
+ minimumFractionDigits: 1,
113
+ maximumFractionDigits: 5,
114
+ })
115
+ ).toBe("1.0");
116
+ expect(
117
+ toLocaleString(new BigNumber(1.333333333333), "en", {
118
+ minimumFractionDigits: 1,
119
+ maximumFractionDigits: 5,
120
+ })
121
+ ).toBe("1.33333");
122
+ expect(
123
+ toLocaleString(new BigNumber(0), "en", {
124
+ minimumFractionDigits: 1,
125
+ maximumFractionDigits: 5,
126
+ })
127
+ ).toBe("0.0");
128
+ expect(
129
+ toLocaleString(new BigNumber(0.003), "en", {
130
+ minimumFractionDigits: 1,
131
+ maximumFractionDigits: 5,
132
+ })
133
+ ).toBe("0.003");
134
+ expect(
135
+ toLocaleString(new BigNumber(0.000003), "en", {
136
+ minimumFractionDigits: 1,
137
+ maximumFractionDigits: 5,
138
+ })
139
+ ).toBe("0.0");
140
+ expect(
141
+ toLocaleString(new BigNumber(9.7), "en", {
142
+ minimumFractionDigits: 1,
143
+ maximumFractionDigits: 1,
144
+ })
145
+ ).toBe("9.7");
146
+ expect(
147
+ toLocaleString(
148
+ new BigNumber("4.4444444444444444444411111111111111"),
149
+ "en",
150
+ {
151
+ maximumFractionDigits: 20,
152
+ }
153
+ )
154
+ ).toBe("4.44444444444444444444");
155
+ });
@@ -0,0 +1,80 @@
1
+ // Implement a subset of Number#toLocaleString for BigNumber.js
2
+ // https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Number/toLocaleString
3
+ import { BigNumber } from "bignumber.js";
4
+ import { getSeparators } from "./localeUtility";
5
+ export type SupportedOptions = {
6
+ minimumFractionDigits: number;
7
+ maximumFractionDigits: number;
8
+ useGrouping: boolean;
9
+ };
10
+
11
+ // FIXME later, might want to expose this format!
12
+ const getFormatForLocale = (locale: string) => {
13
+ const { decimal, thousands } = getSeparators(locale);
14
+ const opts = {
15
+ decimalSeparator: ".",
16
+ groupSeparator: ",",
17
+ groupSize: 3,
18
+ secondaryGroupSize: 0,
19
+ fractionGroupSeparator: "\xA0",
20
+ // non-breaking space
21
+ fractionGroupSize: 0,
22
+ };
23
+ if (typeof decimal === "string") opts.decimalSeparator = decimal;
24
+ if (typeof thousands === "string") opts.groupSeparator = thousands;
25
+ return opts;
26
+ };
27
+
28
+ export const toLocaleString = (
29
+ n: BigNumber,
30
+ localeInput?: string,
31
+ options: Partial<SupportedOptions> = {}
32
+ ): string => {
33
+ let locale = localeInput;
34
+ if (!locale) locale = "en";
35
+ const minimumFractionDigits: number =
36
+ "minimumFractionDigits" in options
37
+ ? (options.minimumFractionDigits as number)
38
+ : 0;
39
+ const maximumFractionDigits: number =
40
+ "maximumFractionDigits" in options
41
+ ? (options.maximumFractionDigits as number)
42
+ : Math.max(minimumFractionDigits, 3);
43
+ const useGrouping = "useGrouping" in options ? options.useGrouping : true;
44
+ const format = getFormatForLocale(locale);
45
+
46
+ if (!useGrouping) {
47
+ format.groupSeparator = "";
48
+ }
49
+
50
+ const BN = BigNumber.clone({
51
+ FORMAT: format,
52
+ });
53
+ const bn = new BN(n);
54
+ const maxDecimals = bn.toFormat(maximumFractionDigits, BigNumber.ROUND_FLOOR);
55
+
56
+ if (maximumFractionDigits !== minimumFractionDigits) {
57
+ const minDecimals = bn.toFormat(
58
+ minimumFractionDigits,
59
+ BigNumber.ROUND_FLOOR
60
+ );
61
+ let i = maxDecimals.length;
62
+
63
+ // cleanup useless '0's from the right until the minimumFractionDigits
64
+ while (i > minDecimals.length) {
65
+ if (maxDecimals[i - 1] !== "0") {
66
+ if (maxDecimals[i - 1] === format.decimalSeparator) {
67
+ i--;
68
+ }
69
+
70
+ break; // we reach decimal. stop now.
71
+ }
72
+
73
+ i--;
74
+ }
75
+
76
+ return maxDecimals.slice(0, i);
77
+ }
78
+
79
+ return maxDecimals;
80
+ };