@ledgerhq/coin-icon 0.4.1-next.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 (341) hide show
  1. package/.eslintrc.js +20 -0
  2. package/.turbo/turbo-build.log +4 -0
  3. package/.unimportedrc.json +41 -0
  4. package/CHANGELOG.md +495 -0
  5. package/LICENSE.txt +21 -0
  6. package/jest.config.js +11 -0
  7. package/lib/__test__/api/index.unit.test.d.ts +2 -0
  8. package/lib/__test__/api/index.unit.test.d.ts.map +1 -0
  9. package/lib/__test__/api/index.unit.test.js +173 -0
  10. package/lib/__test__/api/index.unit.test.js.map +1 -0
  11. package/lib/__test__/unit/buildTransaction.test.d.ts +2 -0
  12. package/lib/__test__/unit/buildTransaction.test.d.ts.map +1 -0
  13. package/lib/__test__/unit/buildTransaction.test.js +76 -0
  14. package/lib/__test__/unit/buildTransaction.test.js.map +1 -0
  15. package/lib/__test__/unit/getFeesForTransaction.unit.test.d.ts +2 -0
  16. package/lib/__test__/unit/getFeesForTransaction.unit.test.d.ts.map +1 -0
  17. package/lib/__test__/unit/getFeesForTransaction.unit.test.js +101 -0
  18. package/lib/__test__/unit/getFeesForTransaction.unit.test.js.map +1 -0
  19. package/lib/__test__/unit/getTransactionStatus.unit.test.d.ts +2 -0
  20. package/lib/__test__/unit/getTransactionStatus.unit.test.d.ts.map +1 -0
  21. package/lib/__test__/unit/getTransactionStatus.unit.test.js +178 -0
  22. package/lib/__test__/unit/getTransactionStatus.unit.test.js.map +1 -0
  23. package/lib/__test__/unit/logic.unit.test.d.ts +2 -0
  24. package/lib/__test__/unit/logic.unit.test.d.ts.map +1 -0
  25. package/lib/__test__/unit/logic.unit.test.js +112 -0
  26. package/lib/__test__/unit/logic.unit.test.js.map +1 -0
  27. package/lib/__test__/unit/serializations.unit.test.d.ts +2 -0
  28. package/lib/__test__/unit/serializations.unit.test.d.ts.map +1 -0
  29. package/lib/__test__/unit/serializations.unit.test.js +73 -0
  30. package/lib/__test__/unit/serializations.unit.test.js.map +1 -0
  31. package/lib/__test__/unit/transaction.unit.test.d.ts +2 -0
  32. package/lib/__test__/unit/transaction.unit.test.d.ts.map +1 -0
  33. package/lib/__test__/unit/transaction.unit.test.js +130 -0
  34. package/lib/__test__/unit/transaction.unit.test.js.map +1 -0
  35. package/lib/account.d.ts +7 -0
  36. package/lib/account.d.ts.map +1 -0
  37. package/lib/account.js +26 -0
  38. package/lib/account.js.map +1 -0
  39. package/lib/api/api-type.d.ts +62 -0
  40. package/lib/api/api-type.d.ts.map +1 -0
  41. package/lib/api/api-type.js +3 -0
  42. package/lib/api/api-type.js.map +1 -0
  43. package/lib/api/index.d.ts +13 -0
  44. package/lib/api/index.d.ts.map +1 -0
  45. package/lib/api/index.js +134 -0
  46. package/lib/api/index.js.map +1 -0
  47. package/lib/api/node.d.ts +31 -0
  48. package/lib/api/node.d.ts.map +1 -0
  49. package/lib/api/node.js +112 -0
  50. package/lib/api/node.js.map +1 -0
  51. package/lib/bridge/index.d.ts +13 -0
  52. package/lib/bridge/index.d.ts.map +1 -0
  53. package/lib/bridge/index.js +71 -0
  54. package/lib/bridge/index.js.map +1 -0
  55. package/lib/bridge.integration.test.d.ts +4 -0
  56. package/lib/bridge.integration.test.d.ts.map +1 -0
  57. package/lib/bridge.integration.test.js +105 -0
  58. package/lib/bridge.integration.test.js.map +1 -0
  59. package/lib/broadcast.d.ts +9 -0
  60. package/lib/broadcast.d.ts.map +1 -0
  61. package/lib/broadcast.js +25 -0
  62. package/lib/broadcast.js.map +1 -0
  63. package/lib/buildTransaction.d.ts +12 -0
  64. package/lib/buildTransaction.d.ts.map +1 -0
  65. package/lib/buildTransaction.js +55 -0
  66. package/lib/buildTransaction.js.map +1 -0
  67. package/lib/cli-transaction.d.ts +16 -0
  68. package/lib/cli-transaction.d.ts.map +1 -0
  69. package/lib/cli-transaction.js +35 -0
  70. package/lib/cli-transaction.js.map +1 -0
  71. package/lib/config.d.ts +15 -0
  72. package/lib/config.d.ts.map +1 -0
  73. package/lib/config.js +17 -0
  74. package/lib/config.js.map +1 -0
  75. package/lib/constants.d.ts +14 -0
  76. package/lib/constants.d.ts.map +1 -0
  77. package/lib/constants.js +17 -0
  78. package/lib/constants.js.map +1 -0
  79. package/lib/createTransaction.d.ts +8 -0
  80. package/lib/createTransaction.d.ts.map +1 -0
  81. package/lib/createTransaction.js +19 -0
  82. package/lib/createTransaction.js.map +1 -0
  83. package/lib/deviceTransactionConfig.d.ts +15 -0
  84. package/lib/deviceTransactionConfig.d.ts.map +1 -0
  85. package/lib/deviceTransactionConfig.js +24 -0
  86. package/lib/deviceTransactionConfig.js.map +1 -0
  87. package/lib/errors.d.ts +10 -0
  88. package/lib/errors.d.ts.map +1 -0
  89. package/lib/errors.js +8 -0
  90. package/lib/errors.js.map +1 -0
  91. package/lib/estimateMaxSpendable.d.ts +14 -0
  92. package/lib/estimateMaxSpendable.d.ts.map +1 -0
  93. package/lib/estimateMaxSpendable.js +38 -0
  94. package/lib/estimateMaxSpendable.js.map +1 -0
  95. package/lib/getFeesForTransaction.d.ts +14 -0
  96. package/lib/getFeesForTransaction.d.ts.map +1 -0
  97. package/lib/getFeesForTransaction.js +44 -0
  98. package/lib/getFeesForTransaction.js.map +1 -0
  99. package/lib/getTransactionStatus.d.ts +4 -0
  100. package/lib/getTransactionStatus.d.ts.map +1 -0
  101. package/lib/getTransactionStatus.js +114 -0
  102. package/lib/getTransactionStatus.js.map +1 -0
  103. package/lib/hw-getAddress.d.ts +6 -0
  104. package/lib/hw-getAddress.d.ts.map +1 -0
  105. package/lib/hw-getAddress.js +23 -0
  106. package/lib/hw-getAddress.js.map +1 -0
  107. package/lib/initAccount.d.ts +3 -0
  108. package/lib/initAccount.d.ts.map +1 -0
  109. package/lib/initAccount.js +13 -0
  110. package/lib/initAccount.js.map +1 -0
  111. package/lib/logic.d.ts +55 -0
  112. package/lib/logic.d.ts.map +1 -0
  113. package/lib/logic.js +124 -0
  114. package/lib/logic.js.map +1 -0
  115. package/lib/prepareTransaction.d.ts +9 -0
  116. package/lib/prepareTransaction.d.ts.map +1 -0
  117. package/lib/prepareTransaction.js +33 -0
  118. package/lib/prepareTransaction.js.map +1 -0
  119. package/lib/serialization.d.ts +7 -0
  120. package/lib/serialization.d.ts.map +1 -0
  121. package/lib/serialization.js +36 -0
  122. package/lib/serialization.js.map +1 -0
  123. package/lib/signOperation.d.ts +10 -0
  124. package/lib/signOperation.d.ts.map +1 -0
  125. package/lib/signOperation.js +96 -0
  126. package/lib/signOperation.js.map +1 -0
  127. package/lib/signer.d.ts +14 -0
  128. package/lib/signer.d.ts.map +1 -0
  129. package/lib/signer.js +3 -0
  130. package/lib/signer.js.map +1 -0
  131. package/lib/specs.d.ts +7 -0
  132. package/lib/specs.d.ts.map +1 -0
  133. package/lib/specs.js +100 -0
  134. package/lib/specs.js.map +1 -0
  135. package/lib/speculos-deviceActions.d.ts +4 -0
  136. package/lib/speculos-deviceActions.d.ts.map +1 -0
  137. package/lib/speculos-deviceActions.js +44 -0
  138. package/lib/speculos-deviceActions.js.map +1 -0
  139. package/lib/synchronization.d.ts +3 -0
  140. package/lib/synchronization.d.ts.map +1 -0
  141. package/lib/synchronization.js +76 -0
  142. package/lib/synchronization.js.map +1 -0
  143. package/lib/transaction.d.ts +15 -0
  144. package/lib/transaction.d.ts.map +1 -0
  145. package/lib/transaction.js +39 -0
  146. package/lib/transaction.js.map +1 -0
  147. package/lib/types/bridge.fixture.d.ts +5 -0
  148. package/lib/types/bridge.fixture.d.ts.map +1 -0
  149. package/lib/types/bridge.fixture.js +77 -0
  150. package/lib/types/bridge.fixture.js.map +1 -0
  151. package/lib/types/index.d.ts +46 -0
  152. package/lib/types/index.d.ts.map +1 -0
  153. package/lib/types/index.js +3 -0
  154. package/lib/types/index.js.map +1 -0
  155. package/lib-es/__test__/api/index.unit.test.d.ts +2 -0
  156. package/lib-es/__test__/api/index.unit.test.d.ts.map +1 -0
  157. package/lib-es/__test__/api/index.unit.test.js +145 -0
  158. package/lib-es/__test__/api/index.unit.test.js.map +1 -0
  159. package/lib-es/__test__/unit/buildTransaction.test.d.ts +2 -0
  160. package/lib-es/__test__/unit/buildTransaction.test.d.ts.map +1 -0
  161. package/lib-es/__test__/unit/buildTransaction.test.js +71 -0
  162. package/lib-es/__test__/unit/buildTransaction.test.js.map +1 -0
  163. package/lib-es/__test__/unit/getFeesForTransaction.unit.test.d.ts +2 -0
  164. package/lib-es/__test__/unit/getFeesForTransaction.unit.test.d.ts.map +1 -0
  165. package/lib-es/__test__/unit/getFeesForTransaction.unit.test.js +73 -0
  166. package/lib-es/__test__/unit/getFeesForTransaction.unit.test.js.map +1 -0
  167. package/lib-es/__test__/unit/getTransactionStatus.unit.test.d.ts +2 -0
  168. package/lib-es/__test__/unit/getTransactionStatus.unit.test.d.ts.map +1 -0
  169. package/lib-es/__test__/unit/getTransactionStatus.unit.test.js +153 -0
  170. package/lib-es/__test__/unit/getTransactionStatus.unit.test.js.map +1 -0
  171. package/lib-es/__test__/unit/logic.unit.test.d.ts +2 -0
  172. package/lib-es/__test__/unit/logic.unit.test.d.ts.map +1 -0
  173. package/lib-es/__test__/unit/logic.unit.test.js +107 -0
  174. package/lib-es/__test__/unit/logic.unit.test.js.map +1 -0
  175. package/lib-es/__test__/unit/serializations.unit.test.d.ts +2 -0
  176. package/lib-es/__test__/unit/serializations.unit.test.d.ts.map +1 -0
  177. package/lib-es/__test__/unit/serializations.unit.test.js +71 -0
  178. package/lib-es/__test__/unit/serializations.unit.test.js.map +1 -0
  179. package/lib-es/__test__/unit/transaction.unit.test.d.ts +2 -0
  180. package/lib-es/__test__/unit/transaction.unit.test.d.ts.map +1 -0
  181. package/lib-es/__test__/unit/transaction.unit.test.js +128 -0
  182. package/lib-es/__test__/unit/transaction.unit.test.js.map +1 -0
  183. package/lib-es/account.d.ts +7 -0
  184. package/lib-es/account.d.ts.map +1 -0
  185. package/lib-es/account.js +24 -0
  186. package/lib-es/account.js.map +1 -0
  187. package/lib-es/api/api-type.d.ts +62 -0
  188. package/lib-es/api/api-type.d.ts.map +1 -0
  189. package/lib-es/api/api-type.js +2 -0
  190. package/lib-es/api/api-type.js.map +1 -0
  191. package/lib-es/api/index.d.ts +13 -0
  192. package/lib-es/api/index.d.ts.map +1 -0
  193. package/lib-es/api/index.js +123 -0
  194. package/lib-es/api/index.js.map +1 -0
  195. package/lib-es/api/node.d.ts +31 -0
  196. package/lib-es/api/node.d.ts.map +1 -0
  197. package/lib-es/api/node.js +99 -0
  198. package/lib-es/api/node.js.map +1 -0
  199. package/lib-es/bridge/index.d.ts +13 -0
  200. package/lib-es/bridge/index.d.ts.map +1 -0
  201. package/lib-es/bridge/index.js +62 -0
  202. package/lib-es/bridge/index.js.map +1 -0
  203. package/lib-es/bridge.integration.test.d.ts +4 -0
  204. package/lib-es/bridge.integration.test.d.ts.map +1 -0
  205. package/lib-es/bridge.integration.test.js +99 -0
  206. package/lib-es/bridge.integration.test.js.map +1 -0
  207. package/lib-es/broadcast.d.ts +9 -0
  208. package/lib-es/broadcast.d.ts.map +1 -0
  209. package/lib-es/broadcast.js +21 -0
  210. package/lib-es/broadcast.js.map +1 -0
  211. package/lib-es/buildTransaction.d.ts +12 -0
  212. package/lib-es/buildTransaction.d.ts.map +1 -0
  213. package/lib-es/buildTransaction.js +48 -0
  214. package/lib-es/buildTransaction.js.map +1 -0
  215. package/lib-es/cli-transaction.d.ts +16 -0
  216. package/lib-es/cli-transaction.d.ts.map +1 -0
  217. package/lib-es/cli-transaction.js +29 -0
  218. package/lib-es/cli-transaction.js.map +1 -0
  219. package/lib-es/config.d.ts +15 -0
  220. package/lib-es/config.d.ts.map +1 -0
  221. package/lib-es/config.js +12 -0
  222. package/lib-es/config.js.map +1 -0
  223. package/lib-es/constants.d.ts +14 -0
  224. package/lib-es/constants.d.ts.map +1 -0
  225. package/lib-es/constants.js +14 -0
  226. package/lib-es/constants.js.map +1 -0
  227. package/lib-es/createTransaction.d.ts +8 -0
  228. package/lib-es/createTransaction.d.ts.map +1 -0
  229. package/lib-es/createTransaction.js +15 -0
  230. package/lib-es/createTransaction.js.map +1 -0
  231. package/lib-es/deviceTransactionConfig.d.ts +15 -0
  232. package/lib-es/deviceTransactionConfig.d.ts.map +1 -0
  233. package/lib-es/deviceTransactionConfig.js +22 -0
  234. package/lib-es/deviceTransactionConfig.js.map +1 -0
  235. package/lib-es/errors.d.ts +10 -0
  236. package/lib-es/errors.d.ts.map +1 -0
  237. package/lib-es/errors.js +5 -0
  238. package/lib-es/errors.js.map +1 -0
  239. package/lib-es/estimateMaxSpendable.d.ts +14 -0
  240. package/lib-es/estimateMaxSpendable.d.ts.map +1 -0
  241. package/lib-es/estimateMaxSpendable.js +31 -0
  242. package/lib-es/estimateMaxSpendable.js.map +1 -0
  243. package/lib-es/getFeesForTransaction.d.ts +14 -0
  244. package/lib-es/getFeesForTransaction.d.ts.map +1 -0
  245. package/lib-es/getFeesForTransaction.js +42 -0
  246. package/lib-es/getFeesForTransaction.js.map +1 -0
  247. package/lib-es/getTransactionStatus.d.ts +4 -0
  248. package/lib-es/getTransactionStatus.d.ts.map +1 -0
  249. package/lib-es/getTransactionStatus.js +109 -0
  250. package/lib-es/getTransactionStatus.js.map +1 -0
  251. package/lib-es/hw-getAddress.d.ts +6 -0
  252. package/lib-es/hw-getAddress.d.ts.map +1 -0
  253. package/lib-es/hw-getAddress.js +21 -0
  254. package/lib-es/hw-getAddress.js.map +1 -0
  255. package/lib-es/initAccount.d.ts +3 -0
  256. package/lib-es/initAccount.d.ts.map +1 -0
  257. package/lib-es/initAccount.js +9 -0
  258. package/lib-es/initAccount.js.map +1 -0
  259. package/lib-es/logic.d.ts +55 -0
  260. package/lib-es/logic.d.ts.map +1 -0
  261. package/lib-es/logic.js +109 -0
  262. package/lib-es/logic.js.map +1 -0
  263. package/lib-es/prepareTransaction.d.ts +9 -0
  264. package/lib-es/prepareTransaction.d.ts.map +1 -0
  265. package/lib-es/prepareTransaction.js +26 -0
  266. package/lib-es/prepareTransaction.js.map +1 -0
  267. package/lib-es/serialization.d.ts +7 -0
  268. package/lib-es/serialization.d.ts.map +1 -0
  269. package/lib-es/serialization.js +29 -0
  270. package/lib-es/serialization.js.map +1 -0
  271. package/lib-es/signOperation.d.ts +10 -0
  272. package/lib-es/signOperation.d.ts.map +1 -0
  273. package/lib-es/signOperation.js +89 -0
  274. package/lib-es/signOperation.js.map +1 -0
  275. package/lib-es/signer.d.ts +14 -0
  276. package/lib-es/signer.d.ts.map +1 -0
  277. package/lib-es/signer.js +2 -0
  278. package/lib-es/signer.js.map +1 -0
  279. package/lib-es/specs.d.ts +7 -0
  280. package/lib-es/specs.d.ts.map +1 -0
  281. package/lib-es/specs.js +95 -0
  282. package/lib-es/specs.js.map +1 -0
  283. package/lib-es/speculos-deviceActions.d.ts +4 -0
  284. package/lib-es/speculos-deviceActions.d.ts.map +1 -0
  285. package/lib-es/speculos-deviceActions.js +41 -0
  286. package/lib-es/speculos-deviceActions.js.map +1 -0
  287. package/lib-es/synchronization.d.ts +3 -0
  288. package/lib-es/synchronization.d.ts.map +1 -0
  289. package/lib-es/synchronization.js +69 -0
  290. package/lib-es/synchronization.js.map +1 -0
  291. package/lib-es/transaction.d.ts +15 -0
  292. package/lib-es/transaction.d.ts.map +1 -0
  293. package/lib-es/transaction.js +33 -0
  294. package/lib-es/transaction.js.map +1 -0
  295. package/lib-es/types/bridge.fixture.d.ts +5 -0
  296. package/lib-es/types/bridge.fixture.d.ts.map +1 -0
  297. package/lib-es/types/bridge.fixture.js +68 -0
  298. package/lib-es/types/bridge.fixture.js.map +1 -0
  299. package/lib-es/types/index.d.ts +46 -0
  300. package/lib-es/types/index.d.ts.map +1 -0
  301. package/lib-es/types/index.js +2 -0
  302. package/lib-es/types/index.js.map +1 -0
  303. package/package.json +85 -0
  304. package/src/__test__/api/index.unit.test.ts +151 -0
  305. package/src/__test__/unit/buildTransaction.test.ts +76 -0
  306. package/src/__test__/unit/getFeesForTransaction.unit.test.ts +76 -0
  307. package/src/__test__/unit/getTransactionStatus.unit.test.ts +172 -0
  308. package/src/__test__/unit/logic.unit.test.ts +145 -0
  309. package/src/__test__/unit/serializations.unit.test.ts +92 -0
  310. package/src/__test__/unit/transaction.unit.test.ts +144 -0
  311. package/src/account.ts +30 -0
  312. package/src/api/api-type.ts +65 -0
  313. package/src/api/index.ts +156 -0
  314. package/src/api/node.ts +115 -0
  315. package/src/bridge/index.ts +76 -0
  316. package/src/bridge.integration.test.ts +107 -0
  317. package/src/broadcast.ts +18 -0
  318. package/src/buildTransaction.ts +53 -0
  319. package/src/cli-transaction.ts +49 -0
  320. package/src/config.ts +29 -0
  321. package/src/constants.ts +14 -0
  322. package/src/createTransaction.ts +16 -0
  323. package/src/deviceTransactionConfig.ts +44 -0
  324. package/src/errors.ts +5 -0
  325. package/src/estimateMaxSpendable.ts +34 -0
  326. package/src/getFeesForTransaction.ts +46 -0
  327. package/src/getTransactionStatus.ts +139 -0
  328. package/src/hw-getAddress.ts +19 -0
  329. package/src/initAccount.ts +11 -0
  330. package/src/logic.ts +132 -0
  331. package/src/prepareTransaction.ts +24 -0
  332. package/src/serialization.ts +34 -0
  333. package/src/signOperation.ts +140 -0
  334. package/src/signer.ts +14 -0
  335. package/src/specs.ts +124 -0
  336. package/src/speculos-deviceActions.ts +46 -0
  337. package/src/synchronization.ts +70 -0
  338. package/src/transaction.ts +60 -0
  339. package/src/types/bridge.fixture.ts +75 -0
  340. package/src/types/index.ts +61 -0
  341. package/tsconfig.json +12 -0
@@ -0,0 +1,145 @@
1
+ import { BigNumber } from "bignumber.js";
2
+ import IconService from "icon-sdk-js";
3
+ import {
4
+ convertLoopToIcx,
5
+ convertICXtoLoop,
6
+ EXISTENTIAL_DEPOSIT,
7
+ MAX_AMOUNT_INPUT,
8
+ isValidAddress,
9
+ isSelfTransaction,
10
+ getNonce,
11
+ isTestnet,
12
+ getNid,
13
+ calculateAmount,
14
+ getMinimumBalance,
15
+ } from "../../logic";
16
+ import { getCryptoCurrencyById } from "@ledgerhq/cryptoassets";
17
+
18
+ import type { Account } from "@ledgerhq/types-live";
19
+ import type { CryptoCurrency } from "@ledgerhq/types-cryptoassets";
20
+ import { IconAccount, Transaction } from "../../types";
21
+ import { BERLIN_TESTNET_NID, MAINNET_NID } from "../../constants";
22
+
23
+ jest.mock("@ledgerhq/cryptoassets", () => ({
24
+ getCryptoCurrencyById: jest.fn(),
25
+ }));
26
+
27
+ const { IconAmount } = IconService;
28
+
29
+ describe("Icon Utils", () => {
30
+ describe("Conversion functions", () => {
31
+ it("should convert loop to ICX correctly", () => {
32
+ const value = "1000000000000000000";
33
+ const result = convertLoopToIcx(value);
34
+ expect(
35
+ result.isEqualTo(new BigNumber(IconAmount.fromLoop(value, IconAmount.Unit.ICX.toString()))),
36
+ ).toBe(true);
37
+ });
38
+
39
+ it("should convert ICX to loop correctly", () => {
40
+ const value = "1";
41
+ const result = convertICXtoLoop(value);
42
+ expect(
43
+ result.isEqualTo(new BigNumber(IconAmount.toLoop(value, IconAmount.Unit.ICX.toString()))),
44
+ ).toBe(true);
45
+ });
46
+ });
47
+
48
+ describe("Validation functions", () => {
49
+ it("should validate address correctly", () => {
50
+ const validAddress = "hx1234567890123456789012345678901234567890";
51
+ const invalidAddress = "hx12345";
52
+ expect(isValidAddress(validAddress)).toBe(true);
53
+ expect(isValidAddress(invalidAddress)).toBe(false);
54
+ });
55
+ });
56
+
57
+ describe("Transaction-related functions", () => {
58
+ it("should identify self transaction correctly", () => {
59
+ const account = { freshAddress: "hx1234567890123456789012345678901234567890" } as Account;
60
+ const transaction = {
61
+ recipient: "hx1234567890123456789012345678901234567890",
62
+ } as Transaction;
63
+ expect(isSelfTransaction(account, transaction)).toBe(true);
64
+ });
65
+
66
+ it("should get nonce correctly", () => {
67
+ const account: IconAccount = {
68
+ iconResources: { nonce: 5 },
69
+ pendingOperations: [{ transactionSequenceNumber: 4 }],
70
+ } as IconAccount;
71
+ expect(getNonce(account)).toBe(5);
72
+
73
+ account.pendingOperations = [{ transactionSequenceNumber: 6 }] as any;
74
+ expect(getNonce(account)).toBe(7);
75
+ });
76
+ });
77
+
78
+ describe("Network-related functions", () => {
79
+ it("should identify testnet correctly", () => {
80
+ (getCryptoCurrencyById as jest.Mock).mockReturnValue({ isTestnetFor: true });
81
+ const currency = { id: "icon" } as CryptoCurrency;
82
+ expect(isTestnet(currency)).toBe(true);
83
+ });
84
+
85
+ it("should return correct network ID", () => {
86
+ (getCryptoCurrencyById as jest.Mock).mockReturnValue({ isTestnetFor: true });
87
+ const currency = { id: "icon" } as CryptoCurrency;
88
+ expect(getNid(currency)).toBe(BERLIN_TESTNET_NID);
89
+
90
+ (getCryptoCurrencyById as jest.Mock).mockReturnValue({ isTestnetFor: false });
91
+ expect(getNid(currency)).toBe(MAINNET_NID);
92
+ });
93
+ });
94
+
95
+ describe("Amount calculation functions", () => {
96
+ it("should calculate correct amount if useAllAmount is true and mode is send", () => {
97
+ const account = {
98
+ spendableBalance: new BigNumber(1000),
99
+ } as IconAccount;
100
+ const transaction = {
101
+ useAllAmount: true,
102
+ mode: "send",
103
+ fees: new BigNumber(10),
104
+ } as Transaction;
105
+
106
+ const result = calculateAmount({ account, transaction });
107
+ expect(result.isEqualTo(new BigNumber(990))).toBe(true);
108
+ });
109
+
110
+ it("should calculate correct amount if useAllAmount is true and mode is not send", () => {
111
+ const account = {
112
+ spendableBalance: new BigNumber(1000),
113
+ } as IconAccount;
114
+ const transaction = {
115
+ useAllAmount: true,
116
+ mode: "other",
117
+ fees: new BigNumber(10),
118
+ } as Transaction;
119
+
120
+ const result = calculateAmount({ account, transaction });
121
+ expect(result.isEqualTo(new BigNumber(990))).toBe(true);
122
+ });
123
+
124
+ it("should limit amount to MAX_AMOUNT_INPUT", () => {
125
+ const account = {} as IconAccount;
126
+ const transaction = {
127
+ useAllAmount: false,
128
+ amount: new BigNumber(10000000000000000000000000000000000),
129
+ } as Transaction;
130
+
131
+ const result = calculateAmount({ account, transaction });
132
+ expect(result.isEqualTo(new BigNumber(MAX_AMOUNT_INPUT))).toBe(true);
133
+ });
134
+
135
+ it("should calculate minimum balance correctly", () => {
136
+ const account = {
137
+ balance: new BigNumber(1000),
138
+ spendableBalance: new BigNumber(900),
139
+ } as Account;
140
+
141
+ const result = getMinimumBalance(account);
142
+ expect(result.isEqualTo(EXISTENTIAL_DEPOSIT.minus(new BigNumber(100)))).toBe(true);
143
+ });
144
+ });
145
+ });
@@ -0,0 +1,92 @@
1
+ import { BigNumber } from "bignumber.js";
2
+
3
+ import type { Account, AccountRaw } from "@ledgerhq/types-live";
4
+ import { IconAccount, IconAccountRaw, IconResources, IconResourcesRaw } from "../../types";
5
+ import {
6
+ assignFromAccountRaw,
7
+ assignToAccountRaw,
8
+ fromIconResourcesRaw,
9
+ toIconResourcesRaw,
10
+ } from "../../serialization";
11
+
12
+ describe("Icon Resources Utilities", () => {
13
+ describe("toIconResourcesRaw", () => {
14
+ it("should convert IconResources to IconResourcesRaw", () => {
15
+ const resources: IconResources = {
16
+ nonce: 1,
17
+ votingPower: new BigNumber(1000),
18
+ totalDelegated: new BigNumber(2000),
19
+ };
20
+
21
+ const expectedRaw: IconResourcesRaw = {
22
+ nonce: 1,
23
+ votingPower: "1000",
24
+ totalDelegated: "2000",
25
+ };
26
+
27
+ expect(toIconResourcesRaw(resources)).toEqual(expectedRaw);
28
+ });
29
+ });
30
+
31
+ describe("fromIconResourcesRaw", () => {
32
+ it("should convert IconResourcesRaw to IconResources", () => {
33
+ const rawResources: IconResourcesRaw = {
34
+ nonce: 1,
35
+ votingPower: "1000",
36
+ totalDelegated: "2000",
37
+ };
38
+
39
+ const expectedResources: IconResources = {
40
+ nonce: 1,
41
+ votingPower: new BigNumber(1000),
42
+ totalDelegated: new BigNumber(2000),
43
+ };
44
+
45
+ expect(fromIconResourcesRaw(rawResources)).toEqual(expectedResources);
46
+ });
47
+ });
48
+
49
+ describe("assignToAccountRaw", () => {
50
+ it("should assign IconResources to AccountRaw", () => {
51
+ const account: IconAccount = {
52
+ iconResources: {
53
+ nonce: 1,
54
+ votingPower: new BigNumber(1000),
55
+ totalDelegated: new BigNumber(2000),
56
+ },
57
+ } as IconAccount;
58
+
59
+ const accountRaw: AccountRaw = {} as IconAccountRaw;
60
+
61
+ assignToAccountRaw(account as Account, accountRaw);
62
+
63
+ expect((accountRaw as IconAccountRaw).iconResources).toEqual({
64
+ nonce: 1,
65
+ votingPower: "1000",
66
+ totalDelegated: "2000",
67
+ });
68
+ });
69
+ });
70
+
71
+ describe("assignFromAccountRaw", () => {
72
+ it("should assign IconResourcesRaw to Account", () => {
73
+ const accountRaw = {
74
+ iconResources: {
75
+ nonce: 1,
76
+ votingPower: "1000",
77
+ totalDelegated: "2000",
78
+ },
79
+ } as any;
80
+
81
+ const account: Account = {} as IconAccount;
82
+
83
+ assignFromAccountRaw(accountRaw as AccountRaw, account);
84
+
85
+ expect((account as IconAccount).iconResources).toEqual({
86
+ nonce: 1,
87
+ votingPower: new BigNumber(1000),
88
+ totalDelegated: new BigNumber(2000),
89
+ });
90
+ });
91
+ });
92
+ });
@@ -0,0 +1,144 @@
1
+ import { BigNumber } from "bignumber.js";
2
+ import type { Account } from "@ledgerhq/types-live";
3
+ import { getAccountCurrency } from "@ledgerhq/coin-framework/account";
4
+ import { formatCurrencyUnit } from "@ledgerhq/coin-framework/currencies";
5
+ import { formatTransaction, fromTransactionRaw, toTransactionRaw } from "../../transaction";
6
+
7
+ jest.mock("@ledgerhq/coin-framework/account", () => ({
8
+ getAccountCurrency: jest.fn(),
9
+ }));
10
+
11
+ jest.mock("@ledgerhq/coin-framework/currencies", () => ({
12
+ formatCurrencyUnit: jest.fn(),
13
+ }));
14
+
15
+ describe("Transaction Utilities", () => {
16
+ describe("formatTransaction", () => {
17
+ it("should format a transaction correctly", () => {
18
+ const transaction = {
19
+ mode: "send",
20
+ amount: new BigNumber(1000),
21
+ recipient: "recipient-address",
22
+ useAllAmount: false,
23
+ } as any;
24
+ const account = {
25
+ currency: {
26
+ units: [{ code: "ICX", name: "ICX", magnitude: 18 }],
27
+ },
28
+ } as any;
29
+
30
+ (getAccountCurrency as jest.Mock).mockReturnValue(account.currency);
31
+ (formatCurrencyUnit as jest.Mock).mockReturnValue("1 ICX");
32
+
33
+ const formatted = formatTransaction(transaction, account);
34
+ expect(formatted).toBe("\nSEND 1 ICX\nTO recipient-address");
35
+ });
36
+
37
+ it("should handle useAllAmount correctly", () => {
38
+ const transaction = {
39
+ mode: "send",
40
+ amount: new BigNumber(0),
41
+ recipient: "recipient-address",
42
+ useAllAmount: true,
43
+ } as any;
44
+ const account = {
45
+ currency: {
46
+ units: [{ code: "ICX", name: "ICX", magnitude: 8 }],
47
+ },
48
+ };
49
+
50
+ (getAccountCurrency as jest.Mock).mockReturnValue(account.currency);
51
+
52
+ const formatted = formatTransaction(transaction, account as Account);
53
+ expect(formatted).toBe("\nSEND MAX\nTO recipient-address");
54
+ });
55
+ });
56
+
57
+ describe("fromTransactionRaw", () => {
58
+ it("should convert a raw transaction to a transaction", () => {
59
+ const rawTransaction = {
60
+ family: "ICX",
61
+ mode: "send",
62
+ amount: "1000",
63
+ recipient: "recipient-address",
64
+ fees: "10",
65
+ stepLimit: "1000",
66
+ } as any;
67
+
68
+ const transaction = fromTransactionRaw(rawTransaction);
69
+ expect(transaction).toEqual({
70
+ family: "ICX",
71
+ mode: "send",
72
+ amount: new BigNumber(1000),
73
+ recipient: "recipient-address",
74
+ fees: new BigNumber(10),
75
+ stepLimit: new BigNumber(1000),
76
+ });
77
+ });
78
+
79
+ it("should handle null and undefined fields", () => {
80
+ const rawTransaction = {
81
+ family: "ICX",
82
+ mode: "send",
83
+ amount: "1000",
84
+ recipient: "recipient-address",
85
+ fees: null,
86
+ stepLimit: undefined,
87
+ } as any;
88
+
89
+ const transaction = fromTransactionRaw(rawTransaction);
90
+ expect(transaction).toEqual({
91
+ family: "ICX",
92
+ mode: "send",
93
+ amount: new BigNumber(1000),
94
+ recipient: "recipient-address",
95
+ fees: null,
96
+ stepLimit: undefined,
97
+ });
98
+ });
99
+ });
100
+
101
+ describe("toTransactionRaw", () => {
102
+ it("should convert a transaction to a raw transaction", () => {
103
+ const transaction = {
104
+ family: "ICX",
105
+ mode: "send",
106
+ amount: new BigNumber(1000),
107
+ recipient: "recipient-address",
108
+ fees: new BigNumber(10),
109
+ stepLimit: new BigNumber(1000),
110
+ } as any;
111
+
112
+ const rawTransaction = toTransactionRaw(transaction);
113
+ expect(rawTransaction).toEqual({
114
+ family: "ICX",
115
+ mode: "send",
116
+ amount: "1000",
117
+ recipient: "recipient-address",
118
+ fees: "10",
119
+ stepLimit: "1000",
120
+ });
121
+ });
122
+
123
+ it("should handle null and undefined fields", () => {
124
+ const transaction = {
125
+ family: "ICX",
126
+ mode: "send",
127
+ amount: new BigNumber(1000),
128
+ recipient: "recipient-address",
129
+ fees: null,
130
+ stepLimit: undefined,
131
+ } as any;
132
+
133
+ const rawTransaction = toTransactionRaw(transaction);
134
+ expect(rawTransaction).toEqual({
135
+ family: "ICX",
136
+ mode: "send",
137
+ amount: "1000",
138
+ recipient: "recipient-address",
139
+ fees: null,
140
+ stepLimit: undefined,
141
+ });
142
+ });
143
+ });
144
+ });
package/src/account.ts ADDED
@@ -0,0 +1,30 @@
1
+ import { getAccountCurrency } from "@ledgerhq/coin-framework/account/index";
2
+ import { IconAccount } from "./types";
3
+ import { formatCurrencyUnit } from "@ledgerhq/coin-framework/currencies/index";
4
+
5
+ function formatAccountSpecifics(account: IconAccount): string {
6
+ const { iconResources } = account;
7
+ if (!iconResources) {
8
+ throw new Error("icon account expected");
9
+ }
10
+
11
+ const unit = getAccountCurrency(account).units[0];
12
+ const formatConfig = {
13
+ disableRounding: true,
14
+ alwaysShowSign: false,
15
+ showCode: true,
16
+ };
17
+
18
+ let str = " ";
19
+
20
+ str += formatCurrencyUnit(unit, account.spendableBalance, formatConfig) + " spendable. ";
21
+
22
+ if (iconResources.nonce) {
23
+ str += "\nonce : " + iconResources.nonce;
24
+ }
25
+
26
+ return str;
27
+ }
28
+ export default {
29
+ formatAccountSpecifics,
30
+ };
@@ -0,0 +1,65 @@
1
+ import BigNumber from "bignumber.js";
2
+
3
+ /**
4
+ * Icon Account
5
+ */
6
+ export type AccountType = {
7
+ address: string;
8
+ audit_tx_hash: string;
9
+ balance: number; //9.998855272082496
10
+ code_hash: string;
11
+ contract_type: string;
12
+ contract_updated_block: number; // 0
13
+ created_timestamp: number; // 0
14
+ deploy_tx_hash: string;
15
+ is_contract: boolean;
16
+ is_nft: boolean;
17
+ is_prep: boolean;
18
+ is_token: boolean;
19
+ log_count: number;
20
+ name: string;
21
+ owner: string;
22
+ status: string;
23
+ symbol: string;
24
+ token_standard: string;
25
+ token_transfer_count: number;
26
+ transaction_count: number;
27
+ transaction_internal_count: number;
28
+ type: string;
29
+ };
30
+
31
+ /**
32
+ * Icon TransactionType
33
+ */
34
+ export type IconTransactionType = {
35
+ block_number: number;
36
+ block_timestamp: number;
37
+ data: string;
38
+ from_address: string;
39
+ hash: string;
40
+ method: string;
41
+ status: string; // 010
42
+ to_address: string;
43
+ transaction_fee: string; // 0x470de4df82000"
44
+ transaction_type: number;
45
+ type: number;
46
+ value: string; //0x470de4df82000
47
+ value_decimal: number; // 4
48
+ };
49
+
50
+ /**
51
+ * Icon Delegation item Type
52
+ */
53
+ export type IconDelegationItemType = {
54
+ address: string;
55
+ value: string;
56
+ };
57
+
58
+ /**
59
+ * Icon DelegationType
60
+ */
61
+ export type IconDelegationType = {
62
+ delegations: IconDelegationItemType[];
63
+ totalDelegated: BigNumber;
64
+ votingPower: BigNumber;
65
+ };
@@ -0,0 +1,156 @@
1
+ import network from "@ledgerhq/live-network/network";
2
+ import { BigNumber } from "bignumber.js";
3
+ import type { Operation, OperationType } from "@ledgerhq/types-live";
4
+ import { encodeOperationId } from "@ledgerhq/coin-framework/operation";
5
+ import { CryptoCurrency } from "@ledgerhq/types-cryptoassets";
6
+ import { LIMIT } from "../constants";
7
+ import { isTestnet } from "../logic";
8
+ import { AccountType, IconTransactionType } from "./api-type";
9
+ import { log } from "@ledgerhq/logs";
10
+ import { IconOperation } from "../types/index";
11
+ import querystring from "querystring";
12
+ import { getCoinConfig } from "../config";
13
+
14
+ /**
15
+ * Returns Testnet API URL if the current network is testnet
16
+ *
17
+ * @param {network} network
18
+ */
19
+ function getApiUrl(network: CryptoCurrency): string {
20
+ const currencyConfig = getCoinConfig();
21
+ let apiUrl = currencyConfig.infra.indexer;
22
+ if (isTestnet(network)) {
23
+ apiUrl = currencyConfig.infra.indexer_testnet;
24
+ }
25
+ return apiUrl;
26
+ }
27
+
28
+ async function fetch(url: string) {
29
+ const { data } = await network({
30
+ method: "GET",
31
+ url,
32
+ });
33
+
34
+ if (data.Error) {
35
+ log("icon-error", data.Error, {
36
+ url,
37
+ });
38
+ throw new Error(data.Error);
39
+ }
40
+
41
+ return data;
42
+ }
43
+
44
+ export const getAccount = async (addr: string, network: CryptoCurrency): Promise<AccountType> => {
45
+ const data = await fetch(`${getApiUrl(network)}/addresses/details/${addr}`);
46
+ return data;
47
+ };
48
+
49
+ export const getCurrentBlockHeight = async (
50
+ network: CryptoCurrency,
51
+ ): Promise<number | undefined> => {
52
+ const data = await fetch(`${getApiUrl(network)}/blocks`);
53
+ return data?.[0].number;
54
+ };
55
+
56
+ /**
57
+ * Returns true if account is the signer
58
+ */
59
+ function isSender(transaction: IconTransactionType, addr: string): boolean {
60
+ return transaction.from_address === addr;
61
+ }
62
+
63
+ /**
64
+ * Map transaction to an Operation Type
65
+ */
66
+ function getOperationType(transaction: IconTransactionType, addr: string): OperationType {
67
+ return isSender(transaction, addr) ? "OUT" : "IN";
68
+ }
69
+
70
+ /**
71
+ * Map transaction to a correct Operation Value (affecting account balance)
72
+ */
73
+ function getOperationValue(transaction: IconTransactionType, addr: string): BigNumber {
74
+ if (isSender(transaction, addr)) {
75
+ return transaction.value
76
+ ? new BigNumber(transaction.value).plus(transaction.transaction_fee)
77
+ : new BigNumber(0);
78
+ } else {
79
+ return transaction.value ? new BigNumber(transaction.value) : new BigNumber(0);
80
+ }
81
+ }
82
+
83
+ /**
84
+ * Map the ICON history transaction to a Ledger Live Operation
85
+ */
86
+ function txToOperation(
87
+ accountId: string,
88
+ addr: string,
89
+ transaction: IconTransactionType,
90
+ ): IconOperation {
91
+ const type = getOperationType(transaction, addr);
92
+
93
+ return {
94
+ id: encodeOperationId(accountId, transaction.hash, type),
95
+ accountId,
96
+ fee: new BigNumber(transaction.transaction_fee),
97
+ value: getOperationValue(transaction, addr),
98
+ type,
99
+ hash: transaction.hash,
100
+ blockHash: null,
101
+ blockHeight: transaction.block_number,
102
+ date: new Date(transaction.block_timestamp / 1000),
103
+ senders: [transaction.from_address],
104
+ recipients: [transaction.to_address],
105
+ extra: {},
106
+ hasFailed: transaction.status !== "0x1",
107
+ };
108
+ }
109
+
110
+ /**
111
+ * Fetch operation list
112
+ */
113
+ export const getOperations = async (
114
+ accountId: string,
115
+ addr: string,
116
+ skip: number,
117
+ network: CryptoCurrency,
118
+ maxLength: number,
119
+ ): Promise<Operation[]> => {
120
+ return await fetchOperationList(accountId, addr, skip, network, maxLength);
121
+ };
122
+
123
+ export const fetchOperationList = async (
124
+ accountId: string,
125
+ addr: string,
126
+ skip: number,
127
+ network: CryptoCurrency,
128
+ maxLength: number,
129
+ prevOperations: IconOperation[] = [],
130
+ ): Promise<IconOperation[]> => {
131
+ const data = await getTxHistory(addr, skip, network);
132
+ const operations = data.map((transaction: IconTransactionType) =>
133
+ txToOperation(accountId, addr, transaction),
134
+ );
135
+
136
+ const mergedOp = [...prevOperations, ...operations];
137
+ if (operations.length < LIMIT || operations.length >= maxLength) {
138
+ return mergedOp;
139
+ }
140
+ return await fetchOperationList(accountId, addr, skip + LIMIT, network, maxLength, mergedOp);
141
+ };
142
+
143
+ export const getTxHistory = async (
144
+ addr: string,
145
+ skip: number,
146
+ network: CryptoCurrency,
147
+ limit: number = LIMIT,
148
+ ): Promise<IconTransactionType[]> => {
149
+ const query = querystring.stringify({
150
+ skip: skip,
151
+ limit: limit,
152
+ });
153
+
154
+ const data = await fetch(`${getApiUrl(network)}/transactions/address/${addr}?${query}`);
155
+ return data;
156
+ };