@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,115 @@
1
+ import { BigNumber } from "bignumber.js";
2
+ import IconService from "icon-sdk-js";
3
+ import type { IcxTransaction, SignedTransaction } from "icon-sdk-js";
4
+ import { CryptoCurrency } from "@ledgerhq/types-cryptoassets";
5
+ import { isTestnet } from "../logic";
6
+ import { GOVERNANCE_SCORE_ADDRESS, IISS_SCORE_ADDRESS } from "../constants";
7
+ import { IconAccount } from "../types/index";
8
+ import { SignedOperation } from "@ledgerhq/types-live";
9
+ import { IconDelegationType } from "./api-type";
10
+ import { getCoinConfig } from "../config";
11
+
12
+ const { HttpProvider } = IconService;
13
+ const { IconBuilder } = IconService;
14
+
15
+ /**
16
+ * Returns Testnet RPC URL if the current currency is testnet
17
+ * @param {currency} currency
18
+ */
19
+ export function getRpcUrl(currency: CryptoCurrency): string {
20
+ const currencyConfig = getCoinConfig();
21
+ let rpcUrl = currencyConfig.infra.node_endpoint;
22
+ if (isTestnet(currency)) {
23
+ rpcUrl = currencyConfig.infra.node_testnet_endpoint;
24
+ }
25
+ return rpcUrl;
26
+ }
27
+
28
+ export function getDebugRpcUrl(currency: CryptoCurrency): string {
29
+ const currencyConfig = getCoinConfig();
30
+ let rpcUrl = currencyConfig.infra.debug_endpoint;
31
+ if (isTestnet(currency)) {
32
+ rpcUrl = currencyConfig.infra.debug_testnet_endpoint;
33
+ }
34
+ return rpcUrl;
35
+ }
36
+
37
+ /**
38
+ * Broadcast blob to blockchain
39
+ */
40
+ export const broadcastTransaction = async (
41
+ signedOperation: SignedOperation,
42
+ currency: CryptoCurrency,
43
+ ) => {
44
+ const { hash } = await submit(signedOperation, currency);
45
+ return { hash };
46
+ };
47
+
48
+ export const submit = async (signedOperation: SignedOperation, currency: CryptoCurrency) => {
49
+ const rpcURL = getRpcUrl(currency);
50
+
51
+ const httpProvider = new HttpProvider(rpcURL);
52
+ const iconService = new IconService(httpProvider);
53
+
54
+ const signedTransaction = {
55
+ getProperties: () => signedOperation.rawData,
56
+ getSignature: () => signedOperation.signature,
57
+ };
58
+
59
+ const response = await iconService
60
+ .sendTransaction(signedTransaction as SignedTransaction)
61
+ .execute();
62
+ return {
63
+ hash: response,
64
+ };
65
+ };
66
+
67
+ /**
68
+ * Obtain fees from blockchain
69
+ */
70
+ export const getFees = async (
71
+ unsigned: IcxTransaction,
72
+ account: IconAccount,
73
+ ): Promise<BigNumber> => {
74
+ const debugRpcUrl = getDebugRpcUrl(account.currency);
75
+ const httpProvider = new HttpProvider(debugRpcUrl);
76
+ const iconService = new IconService(httpProvider);
77
+ const res = await iconService.estimateStep(unsigned).execute();
78
+ return new BigNumber(res);
79
+ };
80
+
81
+ /**
82
+ * Get step price from governance contract
83
+ */
84
+ export const getStepPrice = async (account: IconAccount): Promise<BigNumber> => {
85
+ const rpcURL = getRpcUrl(account.currency);
86
+ const httpProvider = new HttpProvider(rpcURL);
87
+ const iconService = new IconService(httpProvider);
88
+ const txBuilder = new IconBuilder.CallBuilder();
89
+ const stepPriceTx = txBuilder.to(GOVERNANCE_SCORE_ADDRESS).method("getStepPrice").build();
90
+ const res = await iconService.call(stepPriceTx).execute();
91
+ return new BigNumber(res);
92
+ };
93
+
94
+ export const getDelegation = async (
95
+ address: string,
96
+ currency: CryptoCurrency,
97
+ ): Promise<IconDelegationType> => {
98
+ const rpcURL = getRpcUrl(currency);
99
+ const httpProvider = new HttpProvider(rpcURL);
100
+ const iconService = new IconService(httpProvider);
101
+ const delegationTx = new IconBuilder.CallBuilder()
102
+ .to(IISS_SCORE_ADDRESS)
103
+ .method("getDelegation")
104
+ .params({
105
+ address,
106
+ })
107
+ .build();
108
+
109
+ const res = await iconService.call(delegationTx).execute();
110
+ return {
111
+ delegations: res.delegations,
112
+ totalDelegated: new BigNumber(res.totalDelegated),
113
+ votingPower: new BigNumber(res.votingPower),
114
+ };
115
+ };
@@ -0,0 +1,76 @@
1
+ import getAddressWrapper from "@ledgerhq/coin-framework/bridge/getAddressWrapper";
2
+ import {
3
+ defaultUpdateTransaction,
4
+ makeAccountBridgeReceive,
5
+ makeScanAccounts,
6
+ makeSync,
7
+ } from "@ledgerhq/coin-framework/bridge/jsHelpers";
8
+ import { SignerContext } from "@ledgerhq/coin-framework/signer";
9
+ import { CoinConfig } from "@ledgerhq/coin-framework/config";
10
+
11
+ import type { AccountBridge, CurrencyBridge } from "@ledgerhq/types-live";
12
+ import resolver from "../hw-getAddress";
13
+ import { initAccount } from "../initAccount";
14
+ import { broadcast } from "../broadcast";
15
+ import { createTransaction } from "../createTransaction";
16
+ import { estimateMaxSpendable } from "../estimateMaxSpendable";
17
+ import { getTransactionStatus } from "../getTransactionStatus";
18
+ import { prepareTransaction } from "../prepareTransaction";
19
+ import { buildSignOperation } from "../signOperation";
20
+ import { getAccountShape } from "../synchronization";
21
+ import { assignFromAccountRaw, assignToAccountRaw } from "../serialization";
22
+ import type { Transaction } from "../types/index";
23
+ import { IconSigner } from "../signer";
24
+ import { IconCoinConfig, setCoinConfig } from "../config";
25
+
26
+ export function buildCurrencyBridge(signerContext: SignerContext<IconSigner>): CurrencyBridge {
27
+ const getAddress = resolver(signerContext);
28
+
29
+ const scanAccounts = makeScanAccounts({
30
+ getAccountShape,
31
+ getAddressFn: getAddress,
32
+ });
33
+
34
+ return {
35
+ preload: async () => Promise.resolve({}),
36
+ hydrate: () => {},
37
+ scanAccounts,
38
+ };
39
+ }
40
+
41
+ export function buildAccountBridge(
42
+ signerContext: SignerContext<IconSigner>,
43
+ ): AccountBridge<Transaction> {
44
+ const getAddress = resolver(signerContext);
45
+
46
+ const receive = makeAccountBridgeReceive(getAddressWrapper(getAddress));
47
+ const signOperation = buildSignOperation(signerContext);
48
+ const sync = makeSync({ getAccountShape });
49
+
50
+ return {
51
+ createTransaction,
52
+ updateTransaction: defaultUpdateTransaction,
53
+ prepareTransaction,
54
+ getTransactionStatus,
55
+ sync,
56
+ receive,
57
+ assignToAccountRaw,
58
+ assignFromAccountRaw,
59
+ initAccount,
60
+ signOperation,
61
+ broadcast,
62
+ estimateMaxSpendable,
63
+ };
64
+ }
65
+
66
+ export function createBridges(
67
+ signerContext: SignerContext<IconSigner>,
68
+ coinConfig: CoinConfig<IconCoinConfig>,
69
+ ) {
70
+ setCoinConfig(coinConfig);
71
+
72
+ return {
73
+ currencyBridge: buildCurrencyBridge(signerContext),
74
+ accountBridge: buildAccountBridge(signerContext),
75
+ };
76
+ }
@@ -0,0 +1,107 @@
1
+ import type { DatasetTest, CurrenciesData } from "@ledgerhq/types-live";
2
+ import type { Transaction } from "./types";
3
+ import { fromTransactionRaw } from "./transaction";
4
+ import BigNumber from "bignumber.js";
5
+ import {
6
+ InvalidAddressBecauseDestinationIsAlsoSource,
7
+ InvalidAddress,
8
+ NotEnoughBalance,
9
+ } from "@ledgerhq/errors";
10
+
11
+ const TEST_ADDRESS = "hxe52720d9125586e64c745bf3c2c1917dbb46f9ba";
12
+
13
+ const icon: CurrenciesData<Transaction> = {
14
+ scanAccounts: [
15
+ {
16
+ name: "icon seed 1",
17
+ apdus: `
18
+ => e00200010d038000002c8049435880000000
19
+ <= 410426dd3cdd69f3604e9c1d8f3021b20e133524f12894b9ef8e35d3f1f5244516d18b7bb0d1fee7b7c2972d5cde08190d66cf932d0addd5ac87f53198d050940ccb2a6878626232396531313463626434303939346136346462303137626363323233616661353135363966669fd5109b2b2077b50f18fee878efef03cef0cfb5c3256ea9e55f44fa0e9ae5289000
20
+ => e002000115058000002c80494358800000008000000080000000
21
+ <= 4104c1142d5b765d3026e801fd3946d9673051c7e3c3bd0eecd8ca27b6be079c9ee6b7b9f311b9eecb2995efd7e4a7ef9287a7f8878751d3227689c25a324270e8082a687863663465326339346235323631396637326161346461313333373061613663316333646164303064ff4e5e0cebe9462a469955997216658ce22953f79d272a6a1ecf3b3cdcb3549a9000
22
+ => e002000115058000002c80494358800000008000000080000001
23
+ <= 410429e40c4a50f96a32b622e218114c9204f66763c997dadb8c719a95d3a2d98fda3b00ccc9f3d491906d453fa2a63298817eb6af94c77a47f5eb4baa43cbb73a832a6878666330343962346365363063633539363834383764313830643963343236336536333138313733392ed566d1841e629d6f3040a23918ee84015fb60b99824d7dcadde65bcb01bb089000
24
+ => e002000115058000002c80494358800000008000000080000002
25
+ <= 41040310ab9742ae76a33cfb81fbb90f7d2c16af8dbe543cea73dc62dda4eb79eea4363cabad566329103dc8c6d1f29d6e005d608e35d97d80337e1ca5fd07f3da5b2a6878303139393332346162343038343733313565633339373264313738316239646363643136323061650ce87de91bb446401889398f585fc09a525bf7841dc254d94def7023b40523069000
26
+ `,
27
+ },
28
+ ],
29
+ accounts: [
30
+ {
31
+ FIXME_tests: ["balance is sum of ops"],
32
+ raw: {
33
+ id: `js:2:icon:${TEST_ADDRESS}:`,
34
+ seedIdentifier: `${TEST_ADDRESS}`,
35
+ name: "ICON 1",
36
+ derivationMode: "",
37
+ index: 0,
38
+ freshAddress: `${TEST_ADDRESS}`,
39
+ freshAddressPath: "44'/4801368'/0'/0'/0'",
40
+ blockHeight: 0,
41
+ operations: [],
42
+ pendingOperations: [],
43
+ currencyId: "icon",
44
+ lastSyncDate: "",
45
+ balance: "299569965",
46
+ },
47
+ transactions: [
48
+ {
49
+ name: "recipient and sender must not be the same",
50
+ transaction: fromTransactionRaw({
51
+ family: "icon",
52
+ recipient: `${TEST_ADDRESS}`,
53
+ amount: "100000000",
54
+ mode: "send",
55
+ fees: "0.00125",
56
+ }),
57
+ expectedStatus: {
58
+ amount: new BigNumber("100000000"),
59
+ errors: {
60
+ recipient: new InvalidAddressBecauseDestinationIsAlsoSource(),
61
+ },
62
+ warnings: {},
63
+ },
64
+ },
65
+ {
66
+ name: "Not a valid address",
67
+ transaction: fromTransactionRaw({
68
+ family: "icon",
69
+ recipient: "iconinv",
70
+ amount: "100000000",
71
+ mode: "send",
72
+ fees: null,
73
+ }),
74
+ expectedStatus: {
75
+ errors: {
76
+ recipient: new InvalidAddress(),
77
+ },
78
+ warnings: {},
79
+ },
80
+ },
81
+ {
82
+ name: "Not enough balance",
83
+ transaction: fromTransactionRaw({
84
+ family: "icon",
85
+ recipient: "hxedaf3b2027fbbc0a31f589299c0b34533cd8edac",
86
+ amount: "1000000000000000000000000",
87
+ mode: "send",
88
+ fees: null,
89
+ }),
90
+ expectedStatus: {
91
+ errors: {
92
+ amount: new NotEnoughBalance(),
93
+ },
94
+ warnings: {},
95
+ },
96
+ },
97
+ ],
98
+ },
99
+ ],
100
+ };
101
+
102
+ export const dataset: DatasetTest<Transaction> = {
103
+ implementations: ["js"],
104
+ currencies: {
105
+ icon,
106
+ },
107
+ };
@@ -0,0 +1,18 @@
1
+ import { Account, Operation, SignedOperation } from "@ledgerhq/types-live";
2
+ import { patchOperationWithHash } from "@ledgerhq/coin-framework/operation";
3
+
4
+ import { broadcastTransaction } from "./api/node";
5
+
6
+ /**
7
+ * Broadcast the signed transaction
8
+ */
9
+ export async function broadcast({
10
+ account,
11
+ signedOperation,
12
+ }: {
13
+ account: Account;
14
+ signedOperation: SignedOperation;
15
+ }): Promise<Operation> {
16
+ const { hash } = await broadcastTransaction(signedOperation, account.currency);
17
+ return patchOperationWithHash(signedOperation.operation, hash);
18
+ }
@@ -0,0 +1,53 @@
1
+ import type { IconAccount, Transaction } from "./types";
2
+ import IconService from "icon-sdk-js";
3
+ import type { IcxTransaction } from "icon-sdk-js";
4
+ import { getNid, getNonce } from "./logic";
5
+ import BigNumber from "bignumber.js";
6
+ import { RPC_VERSION } from "./constants";
7
+
8
+ const { IconBuilder, IconConverter } = IconService;
9
+
10
+ const buildTransferTransaction = (
11
+ account: IconAccount,
12
+ transaction: Transaction,
13
+ stepLimit?: BigNumber | undefined,
14
+ ): IcxTransaction => {
15
+ const address = account.freshAddress;
16
+ const icxTransactionBuilder = new IconBuilder.IcxTransactionBuilder();
17
+ const icxTransferData = icxTransactionBuilder
18
+ .from(address)
19
+ .to(transaction.recipient)
20
+ .value(IconConverter.toHexNumber(transaction.amount))
21
+ .nid(IconConverter.toHexNumber(getNid(account.currency)))
22
+ .nonce(IconConverter.toHexNumber(getNonce(account)))
23
+ .timestamp(IconConverter.toHexNumber(new Date().getTime() * 1000))
24
+ .version(IconConverter.toHexNumber(RPC_VERSION));
25
+ if (stepLimit) {
26
+ icxTransferData.stepLimit(IconConverter.toHexNumber(stepLimit));
27
+ }
28
+
29
+ return icxTransferData.build();
30
+ };
31
+
32
+ /**
33
+ *
34
+ * @param {Account} account
35
+ * @param {Transaction} transaction
36
+ */
37
+ export const buildTransaction = async (
38
+ account: IconAccount,
39
+ transaction: Transaction,
40
+ stepLimit?: BigNumber | undefined,
41
+ ) => {
42
+ let unsigned: IcxTransaction | undefined;
43
+ switch (transaction.mode) {
44
+ case "send":
45
+ unsigned = buildTransferTransaction(account, transaction, stepLimit);
46
+ break;
47
+ default:
48
+ throw new Error(`Unsupported transaction mode: ${transaction.mode}`);
49
+ }
50
+ return {
51
+ unsigned,
52
+ };
53
+ };
@@ -0,0 +1,49 @@
1
+ import type { AccountLike } from "@ledgerhq/types-live";
2
+ import invariant from "invariant";
3
+ import flatMap from "lodash/flatMap";
4
+ import type { IconAccount, Transaction } from "./types";
5
+
6
+ const options = [
7
+ {
8
+ name: "mode",
9
+ type: String,
10
+ desc: "mode of transaction: send, stake, unstake",
11
+ },
12
+ ];
13
+
14
+ function inferTransactions(
15
+ transactions: Array<{
16
+ account: AccountLike;
17
+ transaction: Transaction;
18
+ }>,
19
+ opts: Record<string, any>,
20
+ ): Transaction[] {
21
+ return flatMap(transactions, ({ transaction, account }) => {
22
+ invariant(
23
+ transaction.family === "icon",
24
+ "[cli-transaction] inferTransactions expects icon family transaction",
25
+ );
26
+
27
+ if (account.type === "Account") {
28
+ invariant((account as IconAccount).iconResources, "unactivated account");
29
+ }
30
+
31
+ if (account.type === "TokenAccount") {
32
+ const isDelisted = account.token.delisted === true;
33
+ invariant(!isDelisted, "token is delisted");
34
+ }
35
+
36
+ return {
37
+ ...transaction,
38
+ family: "icon",
39
+ mode: opts.mode || "send",
40
+ } as Transaction;
41
+ });
42
+ }
43
+
44
+ export default function makeCliTools() {
45
+ return {
46
+ options,
47
+ inferTransactions,
48
+ };
49
+ }
package/src/config.ts ADDED
@@ -0,0 +1,29 @@
1
+ import { CurrencyConfig, CoinConfig } from "@ledgerhq/coin-framework/config";
2
+ import { MissingCoinConfig } from "@ledgerhq/coin-framework/errors";
3
+
4
+ export type IconConfig = {
5
+ infra: {
6
+ indexer: string;
7
+ indexer_testnet: string;
8
+ node_endpoint: string;
9
+ node_testnet_endpoint: string;
10
+ debug_endpoint: string;
11
+ debug_testnet_endpoint: string;
12
+ };
13
+ };
14
+
15
+ export type IconCoinConfig = CurrencyConfig & IconConfig;
16
+
17
+ let coinConfig: CoinConfig<IconCoinConfig> | undefined;
18
+
19
+ export const setCoinConfig = (config: CoinConfig<IconCoinConfig>): void => {
20
+ coinConfig = config;
21
+ };
22
+
23
+ export const getCoinConfig = (): IconCoinConfig => {
24
+ if (!coinConfig) {
25
+ throw new MissingCoinConfig();
26
+ }
27
+
28
+ return coinConfig();
29
+ };
@@ -0,0 +1,14 @@
1
+ export const GOVERNANCE_SCORE_ADDRESS = "cx0000000000000000000000000000000000000001";
2
+ export const IISS_SCORE_ADDRESS = "cx0000000000000000000000000000000000000000";
3
+ export const LIMIT = 100;
4
+ export const BERLIN_TESTNET_NID = 7;
5
+ export const MAINNET_NID = 1;
6
+ export const I_SCORE_UNIT = 1000;
7
+ export const RPC_VERSION = 3;
8
+ export const DEFAULT_STEP_LIMIT = 200000;
9
+
10
+ export const PREP_TYPE = {
11
+ MAIN: "Main P-Rep",
12
+ SUB: "Sub P-Rep",
13
+ CANDIDATE: "Candidate",
14
+ };
@@ -0,0 +1,16 @@
1
+ import { BigNumber } from "bignumber.js";
2
+ import type { Transaction } from "./types";
3
+
4
+ /**
5
+ * Create an empty transaction
6
+ *
7
+ * @returns {Transaction}
8
+ */
9
+ export const createTransaction = (): Transaction => ({
10
+ family: "icon",
11
+ mode: "send",
12
+ amount: new BigNumber(0),
13
+ recipient: "",
14
+ useAllAmount: false,
15
+ fees: null,
16
+ });
@@ -0,0 +1,44 @@
1
+ import type { AccountLike, Account } from "@ledgerhq/types-live";
2
+ import type { Transaction, TransactionStatus } from "./types";
3
+ import type { CommonDeviceTransactionField as DeviceTransactionField } from "@ledgerhq/coin-framework/transaction/common";
4
+ import { getMainAccount } from "@ledgerhq/coin-framework/account/index";
5
+
6
+ export type ExtraDeviceTransactionField = {
7
+ type: "icon.fees";
8
+ label: string;
9
+ };
10
+
11
+ function getDeviceTransactionConfig({
12
+ transaction,
13
+ account,
14
+ parentAccount,
15
+ status: { amount },
16
+ }: {
17
+ account: AccountLike;
18
+ parentAccount?: Account;
19
+ transaction: Transaction;
20
+ status: TransactionStatus;
21
+ }): Array<DeviceTransactionField> {
22
+ const fields: Array<DeviceTransactionField> = [];
23
+ const mainAccount = getMainAccount(account, parentAccount);
24
+ const { mode } = transaction;
25
+
26
+ if (!amount.isZero()) {
27
+ fields.push({
28
+ type: "amount",
29
+ label: "Amount",
30
+ });
31
+ }
32
+
33
+ if (mode !== "send") {
34
+ fields.push({
35
+ type: "address",
36
+ label: "From Address",
37
+ address: mainAccount.freshAddress,
38
+ });
39
+ }
40
+
41
+ return fields;
42
+ }
43
+
44
+ export default getDeviceTransactionConfig;
package/src/errors.ts ADDED
@@ -0,0 +1,5 @@
1
+ import { createCustomErrorClass } from "@ledgerhq/errors";
2
+
3
+ export const IconAllFundsWarning = createCustomErrorClass("IconAllFundsWarning");
4
+ export const IconValidatorsRequired = createCustomErrorClass("IconValidatorsRequired");
5
+ export const IconDoMaxSendInstead = createCustomErrorClass("IconDoMaxSendInstead");
@@ -0,0 +1,34 @@
1
+ import { BigNumber } from "bignumber.js";
2
+ import type { AccountLike, Account } from "@ledgerhq/types-live";
3
+ import { getMainAccount } from "@ledgerhq/coin-framework/account/index";
4
+ import type { IconAccount, Transaction } from "./types";
5
+ import { createTransaction } from "./createTransaction";
6
+ import getEstimatedFees from "./getFeesForTransaction";
7
+ import { calculateAmount } from "./logic";
8
+
9
+ /**
10
+ * Returns the maximum possible amount for transaction
11
+ *
12
+ * @param {Object} param - the account, parentAccount and transaction
13
+ */
14
+ export const estimateMaxSpendable = async ({
15
+ account,
16
+ parentAccount,
17
+ transaction,
18
+ }: {
19
+ account: AccountLike;
20
+ parentAccount: Account | null | undefined;
21
+ transaction: Transaction | null | undefined;
22
+ }): Promise<BigNumber> => {
23
+ const acc = getMainAccount(account, parentAccount) as IconAccount;
24
+ const tx = { ...createTransaction(), ...transaction, useAllAmount: true };
25
+ const fees = await getEstimatedFees({
26
+ account: acc,
27
+ transaction: tx,
28
+ });
29
+
30
+ return calculateAmount({
31
+ account: acc,
32
+ transaction: { ...tx, fees },
33
+ });
34
+ };
@@ -0,0 +1,46 @@
1
+ import { BigNumber } from "bignumber.js";
2
+ import type { IconAccount, Transaction } from "./types";
3
+ import { getFees } from "./api/node";
4
+ import { buildTransaction } from "./buildTransaction";
5
+ import { getStepPrice } from "./api/node";
6
+ import { FEES_SAFETY_BUFFER, calculateAmount } from "./logic";
7
+ import { getAbandonSeedAddress } from "@ledgerhq/cryptoassets";
8
+
9
+ /**
10
+ * Fetch the transaction fees for a transaction
11
+ *
12
+ * @param {IconAccount} account
13
+ * @param {Transaction} transaction
14
+ */
15
+ const getEstimatedFees = async ({
16
+ account,
17
+ transaction,
18
+ }: {
19
+ account: IconAccount;
20
+ transaction: Transaction;
21
+ }): Promise<BigNumber> => {
22
+ const tx = {
23
+ ...transaction,
24
+ recipient: getAbandonSeedAddress(account.currency.id),
25
+ // Always use a fake recipient to estimate fees
26
+ amount: calculateAmount({
27
+ account,
28
+ transaction: {
29
+ ...transaction,
30
+ fees: new BigNumber(0),
31
+ },
32
+ }), // Remove fees if present since we are fetching fees
33
+ };
34
+ try {
35
+ const { unsigned } = await buildTransaction(account, tx);
36
+ const stepLimit = await getFees(unsigned, account);
37
+ transaction.stepLimit = stepLimit;
38
+ const stepPrice = await getStepPrice(account);
39
+ return stepLimit.multipliedBy(stepPrice);
40
+ } catch (_error) {
41
+ // Fix ME, the API of Icon throws an error when getting the fee with maximum balance
42
+ return FEES_SAFETY_BUFFER;
43
+ }
44
+ };
45
+
46
+ export default getEstimatedFees;