@ledgerhq/coin-evm 5.4.1 → 8.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (355) hide show
  1. package/.oxlintrc.json +1 -1
  2. package/CHANGELOG.md +234 -0
  3. package/docs/evm-family-integration-process/README.md +1 -1
  4. package/knip.json +2 -3
  5. package/lib/adapters/blockOperations.js +6 -6
  6. package/lib/adapters/blockOperations.js.map +1 -1
  7. package/lib/adapters/etherscan.js +23 -23
  8. package/lib/adapters/etherscan.js.map +1 -1
  9. package/lib/adapters/ledger.js +17 -17
  10. package/lib/adapters/ledger.js.map +1 -1
  11. package/lib/api/index.js +4 -4
  12. package/lib/api/index.js.map +1 -1
  13. package/lib/config.d.ts +21 -16
  14. package/lib/config.d.ts.map +1 -1
  15. package/lib/config.js +5 -25
  16. package/lib/config.js.map +1 -1
  17. package/lib/errors.d.ts +9 -0
  18. package/lib/errors.d.ts.map +1 -1
  19. package/lib/errors.js +15 -1
  20. package/lib/errors.js.map +1 -1
  21. package/lib/logic/broadcast.js +1 -1
  22. package/lib/logic/broadcast.js.map +1 -1
  23. package/lib/logic/common.d.ts +2 -1
  24. package/lib/logic/common.d.ts.map +1 -1
  25. package/lib/logic/common.js +4 -4
  26. package/lib/logic/common.js.map +1 -1
  27. package/lib/logic/craftTransaction.d.ts.map +1 -1
  28. package/lib/logic/craftTransaction.js +2 -2
  29. package/lib/logic/craftTransaction.js.map +1 -1
  30. package/lib/logic/estimateFees.d.ts.map +1 -1
  31. package/lib/logic/estimateFees.js +9 -9
  32. package/lib/logic/estimateFees.js.map +1 -1
  33. package/lib/logic/getBalance.d.ts.map +1 -1
  34. package/lib/logic/getBalance.js +50 -16
  35. package/lib/logic/getBalance.js.map +1 -1
  36. package/lib/logic/getBlock.d.ts.map +1 -1
  37. package/lib/logic/getBlock.js +19 -11
  38. package/lib/logic/getBlock.js.map +1 -1
  39. package/lib/logic/getBlockInfo.js +1 -1
  40. package/lib/logic/getBlockInfo.js.map +1 -1
  41. package/lib/logic/getNextSequence.d.ts.map +1 -1
  42. package/lib/logic/getNextSequence.js +1 -1
  43. package/lib/logic/getNextSequence.js.map +1 -1
  44. package/lib/logic/index.d.ts +2 -0
  45. package/lib/logic/index.d.ts.map +1 -1
  46. package/lib/logic/index.js +4 -0
  47. package/lib/logic/index.js.map +1 -1
  48. package/lib/logic/lastBlock.js +1 -1
  49. package/lib/logic/lastBlock.js.map +1 -1
  50. package/lib/logic/listOperations.d.ts.map +1 -1
  51. package/lib/logic/listOperations.js +10 -1
  52. package/lib/logic/listOperations.js.map +1 -1
  53. package/lib/logic/validateIntent.d.ts.map +1 -1
  54. package/lib/logic/validateIntent.js +12 -12
  55. package/lib/logic/validateIntent.js.map +1 -1
  56. package/lib/logic-public/index.d.ts +3 -0
  57. package/lib/logic-public/index.d.ts.map +1 -0
  58. package/lib/logic-public/index.js +19 -0
  59. package/lib/logic-public/index.js.map +1 -0
  60. package/lib/{utils.d.ts → logic-public/utils.d.ts} +0 -44
  61. package/lib/logic-public/utils.d.ts.map +1 -0
  62. package/lib/logic-public/utils.js +166 -0
  63. package/lib/logic-public/utils.js.map +1 -0
  64. package/lib/logic.d.ts +2 -1
  65. package/lib/logic.d.ts.map +1 -1
  66. package/lib/logic.js +3 -3
  67. package/lib/logic.js.map +1 -1
  68. package/lib/network/explorer/etherscan.d.ts +16 -3
  69. package/lib/network/explorer/etherscan.d.ts.map +1 -1
  70. package/lib/network/explorer/etherscan.js +61 -6
  71. package/lib/network/explorer/etherscan.js.map +1 -1
  72. package/lib/network/explorer/ledger.d.ts +50 -4
  73. package/lib/network/explorer/ledger.d.ts.map +1 -1
  74. package/lib/network/explorer/ledger.js +286 -37
  75. package/lib/network/explorer/ledger.js.map +1 -1
  76. package/lib/network/explorer/none.d.ts +1 -0
  77. package/lib/network/explorer/none.d.ts.map +1 -1
  78. package/lib/network/explorer/none.js +4 -1
  79. package/lib/network/explorer/none.js.map +1 -1
  80. package/lib/network/explorer/types.d.ts +15 -2
  81. package/lib/network/explorer/types.d.ts.map +1 -1
  82. package/lib/network/explorer/types.js.map +1 -1
  83. package/lib/network/index.d.ts +5 -0
  84. package/lib/network/index.d.ts.map +1 -0
  85. package/lib/network/index.js +23 -0
  86. package/lib/network/index.js.map +1 -0
  87. package/lib/network/node/explorerProxy.d.ts +27 -0
  88. package/lib/network/node/explorerProxy.d.ts.map +1 -0
  89. package/lib/network/node/explorerProxy.js +262 -0
  90. package/lib/network/node/explorerProxy.js.map +1 -0
  91. package/lib/network/node/index.d.ts +2 -1
  92. package/lib/network/node/index.d.ts.map +1 -1
  93. package/lib/network/node/index.js +11 -10
  94. package/lib/network/node/index.js.map +1 -1
  95. package/lib/network/node/ledger.js +7 -7
  96. package/lib/network/node/ledger.js.map +1 -1
  97. package/lib/network/node/rpc.common.d.ts +2 -2
  98. package/lib/network/node/rpc.common.js +19 -19
  99. package/lib/network/node/rpc.common.js.map +1 -1
  100. package/lib/network/node/withNodeSources.d.ts +21 -0
  101. package/lib/network/node/withNodeSources.d.ts.map +1 -0
  102. package/lib/network/node/withNodeSources.js +78 -0
  103. package/lib/network/node/withNodeSources.js.map +1 -0
  104. package/lib/nodeSources.d.ts +15 -0
  105. package/lib/nodeSources.d.ts.map +1 -0
  106. package/lib/nodeSources.js +33 -0
  107. package/lib/nodeSources.js.map +1 -0
  108. package/lib/staking/contracts.js +1 -1
  109. package/lib/staking/contracts.js.map +1 -1
  110. package/lib/staking/fetchers.js +1 -1
  111. package/lib/staking/fetchers.js.map +1 -1
  112. package/lib/staking/operations/sei.js +1 -1
  113. package/lib/staking/operations/sei.js.map +1 -1
  114. package/lib/staking/prepareIntents.js +3 -3
  115. package/lib/staking/prepareIntents.js.map +1 -1
  116. package/lib/staking/rewards.js +1 -1
  117. package/lib/staking/rewards.js.map +1 -1
  118. package/lib/staking/transactionData.js +2 -2
  119. package/lib/staking/transactionData.js.map +1 -1
  120. package/lib/staking/utils.d.ts +45 -0
  121. package/lib/staking/utils.d.ts.map +1 -0
  122. package/lib/{utils.js → staking/utils.js} +5 -164
  123. package/lib/staking/utils.js.map +1 -0
  124. package/lib/staking/validators/sei.js +1 -1
  125. package/lib/staking/validators/sei.js.map +1 -1
  126. package/lib/types/staking.d.ts +2 -2
  127. package/lib-es/adapters/blockOperations.js +1 -1
  128. package/lib-es/adapters/blockOperations.js.map +1 -1
  129. package/lib-es/adapters/etherscan.js +1 -1
  130. package/lib-es/adapters/etherscan.js.map +1 -1
  131. package/lib-es/adapters/ledger.js +1 -1
  132. package/lib-es/adapters/ledger.js.map +1 -1
  133. package/lib-es/api/index.js +2 -2
  134. package/lib-es/api/index.js.map +1 -1
  135. package/lib-es/config.d.ts +21 -16
  136. package/lib-es/config.d.ts.map +1 -1
  137. package/lib-es/config.js +1 -21
  138. package/lib-es/config.js.map +1 -1
  139. package/lib-es/errors.d.ts +9 -0
  140. package/lib-es/errors.d.ts.map +1 -1
  141. package/lib-es/errors.js +13 -0
  142. package/lib-es/errors.js.map +1 -1
  143. package/lib-es/logic/broadcast.js +1 -1
  144. package/lib-es/logic/broadcast.js.map +1 -1
  145. package/lib-es/logic/common.d.ts +2 -1
  146. package/lib-es/logic/common.d.ts.map +1 -1
  147. package/lib-es/logic/common.js +3 -3
  148. package/lib-es/logic/common.js.map +1 -1
  149. package/lib-es/logic/craftTransaction.d.ts.map +1 -1
  150. package/lib-es/logic/craftTransaction.js +2 -2
  151. package/lib-es/logic/craftTransaction.js.map +1 -1
  152. package/lib-es/logic/estimateFees.d.ts.map +1 -1
  153. package/lib-es/logic/estimateFees.js +6 -6
  154. package/lib-es/logic/estimateFees.js.map +1 -1
  155. package/lib-es/logic/getBalance.d.ts.map +1 -1
  156. package/lib-es/logic/getBalance.js +50 -16
  157. package/lib-es/logic/getBalance.js.map +1 -1
  158. package/lib-es/logic/getBlock.d.ts.map +1 -1
  159. package/lib-es/logic/getBlock.js +16 -8
  160. package/lib-es/logic/getBlock.js.map +1 -1
  161. package/lib-es/logic/getBlockInfo.js +1 -1
  162. package/lib-es/logic/getBlockInfo.js.map +1 -1
  163. package/lib-es/logic/getNextSequence.d.ts.map +1 -1
  164. package/lib-es/logic/getNextSequence.js +1 -1
  165. package/lib-es/logic/getNextSequence.js.map +1 -1
  166. package/lib-es/logic/index.d.ts +2 -0
  167. package/lib-es/logic/index.d.ts.map +1 -1
  168. package/lib-es/logic/index.js +4 -0
  169. package/lib-es/logic/index.js.map +1 -1
  170. package/lib-es/logic/lastBlock.js +1 -1
  171. package/lib-es/logic/lastBlock.js.map +1 -1
  172. package/lib-es/logic/listOperations.d.ts.map +1 -1
  173. package/lib-es/logic/listOperations.js +10 -1
  174. package/lib-es/logic/listOperations.js.map +1 -1
  175. package/lib-es/logic/validateIntent.d.ts.map +1 -1
  176. package/lib-es/logic/validateIntent.js +4 -4
  177. package/lib-es/logic/validateIntent.js.map +1 -1
  178. package/lib-es/logic-public/index.d.ts +3 -0
  179. package/lib-es/logic-public/index.d.ts.map +1 -0
  180. package/lib-es/logic-public/index.js +4 -0
  181. package/lib-es/logic-public/index.js.map +1 -0
  182. package/lib-es/{utils.d.ts → logic-public/utils.d.ts} +0 -44
  183. package/lib-es/logic-public/utils.d.ts.map +1 -0
  184. package/lib-es/logic-public/utils.js +150 -0
  185. package/lib-es/logic-public/utils.js.map +1 -0
  186. package/lib-es/logic.d.ts +2 -1
  187. package/lib-es/logic.d.ts.map +1 -1
  188. package/lib-es/logic.js +3 -3
  189. package/lib-es/logic.js.map +1 -1
  190. package/lib-es/network/explorer/etherscan.d.ts +16 -3
  191. package/lib-es/network/explorer/etherscan.d.ts.map +1 -1
  192. package/lib-es/network/explorer/etherscan.js +60 -5
  193. package/lib-es/network/explorer/etherscan.js.map +1 -1
  194. package/lib-es/network/explorer/ledger.d.ts +50 -4
  195. package/lib-es/network/explorer/ledger.d.ts.map +1 -1
  196. package/lib-es/network/explorer/ledger.js +283 -37
  197. package/lib-es/network/explorer/ledger.js.map +1 -1
  198. package/lib-es/network/explorer/none.d.ts +1 -0
  199. package/lib-es/network/explorer/none.d.ts.map +1 -1
  200. package/lib-es/network/explorer/none.js +2 -0
  201. package/lib-es/network/explorer/none.js.map +1 -1
  202. package/lib-es/network/explorer/types.d.ts +15 -2
  203. package/lib-es/network/explorer/types.d.ts.map +1 -1
  204. package/lib-es/network/explorer/types.js.map +1 -1
  205. package/lib-es/network/index.d.ts +5 -0
  206. package/lib-es/network/index.d.ts.map +1 -0
  207. package/lib-es/network/index.js +7 -0
  208. package/lib-es/network/index.js.map +1 -0
  209. package/lib-es/network/node/explorerProxy.d.ts +27 -0
  210. package/lib-es/network/node/explorerProxy.d.ts.map +1 -0
  211. package/lib-es/network/node/explorerProxy.js +254 -0
  212. package/lib-es/network/node/explorerProxy.js.map +1 -0
  213. package/lib-es/network/node/index.d.ts +2 -1
  214. package/lib-es/network/node/index.d.ts.map +1 -1
  215. package/lib-es/network/node/index.js +11 -10
  216. package/lib-es/network/node/index.js.map +1 -1
  217. package/lib-es/network/node/ledger.js +1 -1
  218. package/lib-es/network/node/ledger.js.map +1 -1
  219. package/lib-es/network/node/rpc.common.d.ts +2 -2
  220. package/lib-es/network/node/rpc.common.js +3 -3
  221. package/lib-es/network/node/rpc.common.js.map +1 -1
  222. package/lib-es/network/node/withNodeSources.d.ts +21 -0
  223. package/lib-es/network/node/withNodeSources.d.ts.map +1 -0
  224. package/lib-es/network/node/withNodeSources.js +74 -0
  225. package/lib-es/network/node/withNodeSources.js.map +1 -0
  226. package/lib-es/nodeSources.d.ts +15 -0
  227. package/lib-es/nodeSources.d.ts.map +1 -0
  228. package/lib-es/nodeSources.js +28 -0
  229. package/lib-es/nodeSources.js.map +1 -0
  230. package/lib-es/staking/contracts.js +1 -1
  231. package/lib-es/staking/contracts.js.map +1 -1
  232. package/lib-es/staking/fetchers.js +1 -1
  233. package/lib-es/staking/fetchers.js.map +1 -1
  234. package/lib-es/staking/operations/sei.js +1 -1
  235. package/lib-es/staking/operations/sei.js.map +1 -1
  236. package/lib-es/staking/prepareIntents.js +1 -1
  237. package/lib-es/staking/prepareIntents.js.map +1 -1
  238. package/lib-es/staking/rewards.js +1 -1
  239. package/lib-es/staking/rewards.js.map +1 -1
  240. package/lib-es/staking/transactionData.js +1 -1
  241. package/lib-es/staking/transactionData.js.map +1 -1
  242. package/lib-es/staking/utils.d.ts +45 -0
  243. package/lib-es/staking/utils.d.ts.map +1 -0
  244. package/lib-es/{utils.js → staking/utils.js} +1 -147
  245. package/lib-es/staking/utils.js.map +1 -0
  246. package/lib-es/staking/validators/sei.js +1 -1
  247. package/lib-es/staking/validators/sei.js.map +1 -1
  248. package/lib-es/types/staking.d.ts +2 -2
  249. package/package.json +75 -9
  250. package/src/adapters/blockOperations.ts +1 -1
  251. package/src/adapters/etherscan.ts +1 -1
  252. package/src/adapters/ledger.ts +1 -1
  253. package/src/api/getBlock.integ.test.ts +10 -4
  254. package/src/api/index.arbitrum.integ.test.ts +1 -0
  255. package/src/api/index.arc.integ.test.ts +1 -0
  256. package/src/api/index.avalanche_c_chain_fuji.integ.test.ts +1 -0
  257. package/src/api/index.cronos.integ.test.ts +1 -0
  258. package/src/api/index.flare.integ.test.ts +4 -2
  259. package/src/api/index.linea.integ.test.ts +4 -2
  260. package/src/api/index.monad_testnet.integ.test.ts +95 -4
  261. package/src/api/index.optimism.integ.test.ts +1 -0
  262. package/src/api/index.rsk.integ.test.ts +1 -0
  263. package/src/api/index.shape.integ.test.ts +4 -2
  264. package/src/api/index.somnia.integ.test.ts +1 -0
  265. package/src/api/index.ts +2 -2
  266. package/src/config.ts +22 -35
  267. package/src/errors.ts +13 -0
  268. package/src/fixtures/context.fixtures.ts +9 -4
  269. package/src/logic/broadcast.ts +1 -1
  270. package/src/logic/common.ts +4 -2
  271. package/src/logic/craftTransaction.ts +2 -1
  272. package/src/logic/estimateFees.ts +20 -6
  273. package/src/logic/getBalance.ts +72 -26
  274. package/src/logic/getBlock.ts +26 -14
  275. package/src/logic/getBlockInfo.ts +1 -1
  276. package/src/logic/getNextSequence.ts +4 -1
  277. package/src/logic/index.ts +4 -0
  278. package/src/logic/lastBlock.ts +1 -1
  279. package/src/logic/listOperations.ts +14 -1
  280. package/src/logic/tests/broadcast.integ.test.ts +53 -0
  281. package/src/logic/{broadcast.test.ts → tests/broadcast.test.ts} +7 -7
  282. package/src/logic/{broadcastSource.test.ts → tests/broadcastSource.test.ts} +6 -6
  283. package/src/logic/{combine.test.ts → tests/combine.test.ts} +1 -1
  284. package/src/logic/{common.test.ts → tests/common.test.ts} +45 -21
  285. package/src/logic/{computeGasLimit.test.ts → tests/computeGasLimit.test.ts} +2 -2
  286. package/src/logic/{craftTransaction.test.ts → tests/craftTransaction.test.ts} +7 -7
  287. package/src/logic/{craftTransactionData.test.ts → tests/craftTransactionData.test.ts} +4 -4
  288. package/src/logic/{estimateFees.test.ts → tests/estimateFees.test.ts} +12 -12
  289. package/src/logic/tests/getBalance.test.ts +602 -0
  290. package/src/logic/{getBlock.test.ts → tests/getBlock.test.ts} +182 -89
  291. package/src/logic/{getBlockInfo.test.ts → tests/getBlockInfo.test.ts} +8 -8
  292. package/src/logic/{getErc20Data.test.ts → tests/getErc20Data.test.ts} +1 -1
  293. package/src/logic/{getNextSequence.test.ts → tests/getNextSequence.test.ts} +7 -7
  294. package/src/logic/{getStakes.test.ts → tests/getStakes.test.ts} +13 -13
  295. package/src/logic/{internalTransactionsFetcher.test.ts → tests/internalTransactionsFetcher.test.ts} +8 -8
  296. package/src/logic/{lastBlock.test.ts → tests/lastBlock.test.ts} +7 -7
  297. package/src/logic/{listOperations.test.ts → tests/listOperations.test.ts} +125 -6
  298. package/src/logic/{validateAddress.test.ts → tests/validateAddress.test.ts} +1 -1
  299. package/src/logic/{validateIntent.test.ts → tests/validateIntent.test.ts} +80 -78
  300. package/src/logic/validateIntent.ts +7 -4
  301. package/src/logic-public/index.ts +18 -0
  302. package/src/{utils.test.ts → logic-public/tests/utils.test.ts} +1 -27
  303. package/src/logic-public/utils.ts +184 -0
  304. package/src/logic.test.ts +5 -59
  305. package/src/logic.ts +5 -3
  306. package/src/network/explorer/etherscan.test.ts +403 -195
  307. package/src/network/explorer/etherscan.ts +99 -10
  308. package/src/network/explorer/index.test.ts +27 -0
  309. package/src/network/explorer/ledger.test.ts +792 -62
  310. package/src/network/explorer/ledger.ts +426 -54
  311. package/src/network/explorer/none.ts +3 -0
  312. package/src/network/explorer/types.ts +29 -5
  313. package/src/network/index.ts +7 -0
  314. package/src/network/node/explorerProxy.test.ts +673 -0
  315. package/src/network/node/explorerProxy.ts +308 -0
  316. package/src/network/node/index.memoization.test.ts +22 -10
  317. package/src/network/node/index.test.ts +6 -3
  318. package/src/network/node/index.ts +11 -7
  319. package/src/network/node/ledger.ts +1 -1
  320. package/src/network/node/node.fixtures.ts +2 -1
  321. package/src/network/node/rpc.common.ts +3 -3
  322. package/src/network/node/rpc.test.ts +66 -105
  323. package/src/network/node/withNodeSources.test.ts +240 -0
  324. package/src/network/node/withNodeSources.ts +95 -0
  325. package/src/nodeSources.ts +38 -0
  326. package/src/staking/contracts.ts +1 -1
  327. package/src/staking/encoder.test.ts +1 -1
  328. package/src/staking/fetchers.ts +1 -1
  329. package/src/staking/operations/sei.ts +1 -1
  330. package/src/staking/prepareIntents.ts +1 -1
  331. package/src/staking/redelegations.test.ts +3 -1
  332. package/src/staking/rewards.test.ts +1 -1
  333. package/src/staking/rewards.ts +1 -1
  334. package/src/staking/transactionData.ts +1 -1
  335. package/src/staking/unbondings.test.ts +2 -1
  336. package/src/staking/utils.test.ts +25 -0
  337. package/src/{utils.ts → staking/utils.ts} +2 -182
  338. package/src/staking/validators/sei.ts +1 -1
  339. package/src/staking/validators.test.ts +1 -0
  340. package/src/types/staking.ts +2 -2
  341. package/lib/constants.d.ts +0 -2
  342. package/lib/constants.d.ts.map +0 -1
  343. package/lib/constants.js +0 -11
  344. package/lib/constants.js.map +0 -1
  345. package/lib/utils.d.ts.map +0 -1
  346. package/lib/utils.js.map +0 -1
  347. package/lib-es/constants.d.ts +0 -2
  348. package/lib-es/constants.d.ts.map +0 -1
  349. package/lib-es/constants.js +0 -7
  350. package/lib-es/constants.js.map +0 -1
  351. package/lib-es/utils.d.ts.map +0 -1
  352. package/lib-es/utils.js.map +0 -1
  353. package/src/constants.ts +0 -9
  354. package/src/logic/broadcast.integ.test.ts +0 -46
  355. package/src/logic/getBalance.test.ts +0 -362
@@ -0,0 +1,184 @@
1
+ // SPDX-FileCopyrightText: © 2026 LEDGER SAS
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type {
5
+ StakingTransactionIntent,
6
+ TransactionIntent,
7
+ } from '@ledgerhq/coin-module-framework/api/types'
8
+ import { log } from '@ledgerhq/logs'
9
+ import BigNumber from 'bignumber.js'
10
+ import eip55 from 'eip55'
11
+
12
+ /**
13
+ * Some addresses returned by the explorers are not 40 characters hex addresses
14
+ * For example the explorers may return "0x0" as an address (for example for
15
+ * some events or contract interactions, like a contract creation transaction)
16
+ *
17
+ * This is not a valid EIP55 address and thus will fail when trying to encode it
18
+ * with a "Bad address" error.
19
+ * cf:
20
+ * https://github.com/cryptocoinjs/eip55/blob/v2.1.1/index.js#L5-L6
21
+ * https://github.com/cryptocoinjs/eip55/blob/v2.1.1/index.js#L63-L65
22
+ *
23
+ * Since we can't control what the explorer returns, and we don't want the app to crash
24
+ * in these cases, we simply ignore the address and return an empty string.
25
+ *
26
+ * For now this has only been observed on the from or to fields of an operation
27
+ * so we only use this function for these fields.
28
+ */
29
+ export const safeEncodeEIP55 = (addr: string): string => {
30
+ if (!addr || addr === '0x' || addr === '0x0') {
31
+ return ''
32
+ }
33
+
34
+ try {
35
+ return eip55.encode(addr)
36
+ } catch (e) {
37
+ log('EVM Family - utils.ts', 'Failed to eip55 encode address', {
38
+ address: addr,
39
+ error: e,
40
+ })
41
+
42
+ return addr
43
+ }
44
+ }
45
+
46
+ /**
47
+ * Helper adding when necessary a 0
48
+ * prefix if string length is odd
49
+ */
50
+ export const padHexString = (str: string): string => {
51
+ return str.length % 2 !== 0 ? '0' + str : str
52
+ }
53
+
54
+ /**
55
+ * arbitrary default value for a new transaction nonce
56
+ * the actual nonce will be set by `prepareForSignOperation`
57
+ */
58
+ export const DEFAULT_NONCE = -1
59
+
60
+ export const DEFAULT_GAS_LIMIT = new BigNumber(21000)
61
+
62
+ export function isEthAddress(address: string): boolean {
63
+ return /^(0x)?[0-9a-fA-F]{40}$/.test(address)
64
+ }
65
+
66
+ /**
67
+ * True when `value` is a 0x-prefixed, even-length hex string (the empty payload `"0x"` is valid).
68
+ * When `byteLength` is provided, also checks the payload is exactly that many bytes.
69
+ *
70
+ * Unlike {@link isEthAddress}, this requires the `0x` prefix, which is why it is used to validate
71
+ * untrusted `call` params rather than reusing the more lenient address matcher.
72
+ */
73
+ export function isHexString(value: string, byteLength?: number): boolean {
74
+ if (!/^0x([0-9a-fA-F]{2})*$/.test(value)) {
75
+ return false
76
+ }
77
+ return byteLength === undefined || (value.length - 2) / 2 === byteLength
78
+ }
79
+
80
+ /** Discriminant for smart contract call vs contract creation (deployment). */
81
+ export type ContractInteractionKind = 'SmartContractInteraction' | 'SmartContractDeployment'
82
+
83
+ /**
84
+ * True when `input` carries non-trivial calldata or init code (not empty / whitespace-only /
85
+ * bare hex prefix). Leading and trailing whitespace are ignored; `0x` / `0X` with no payload
86
+ * after trim are treated as empty.
87
+ */
88
+ export function isSmartContractInput(input: string | null | undefined): input is string {
89
+ if (input === null || input === undefined) {
90
+ return false
91
+ }
92
+ const trimmed = input.trim()
93
+ if (trimmed === '') {
94
+ return false
95
+ }
96
+ if (trimmed.length === 2 && trimmed.toLowerCase() === '0x') {
97
+ return false
98
+ }
99
+ return true
100
+ }
101
+
102
+ /**
103
+ * Builds flat `details` fields for coin-framework operation when a tx has smart contract input.
104
+ *
105
+ * `contractAddress` (when present) identifies the relevant contract: for
106
+ * `SmartContractInteraction` it is the called address (`to`); for
107
+ * `SmartContractDeployment` it is the created contract when
108
+ * `deployedContractAddress` is known (e.g. from a receipt), otherwise omitted.
109
+ */
110
+ export function buildSmartContractDetails(
111
+ to: string | undefined,
112
+ input: string | undefined,
113
+ deployedContractAddress?: string | undefined
114
+ ): Record<string, unknown> | undefined {
115
+ if (!isSmartContractInput(input)) {
116
+ return undefined
117
+ }
118
+ const trimmedInput = input.trim()
119
+ const encodedTo = to ? safeEncodeEIP55(to) : ''
120
+ const contractInteraction: ContractInteractionKind = encodedTo
121
+ ? 'SmartContractInteraction'
122
+ : 'SmartContractDeployment'
123
+ const contractPayload = /^0x/i.test(trimmedInput)
124
+ ? `0x${trimmedInput.slice(2)}`
125
+ : `0x${trimmedInput}`
126
+ const encodedDeployed = deployedContractAddress ? safeEncodeEIP55(deployedContractAddress) : ''
127
+ const contractAddress = encodedDeployed || encodedTo
128
+ return {
129
+ contractInteraction,
130
+ ...(contractAddress ? { contractAddress } : {}),
131
+ contractPayload,
132
+ }
133
+ }
134
+
135
+ /**
136
+ * Normalizes an Ethereum address to lowercase to avoid checksum validation issues.
137
+ *
138
+ * Some chains like RSK use EIP-1191 (chain-specific checksum) instead of EIP-55.
139
+ * When an address has a valid EIP-1191 checksum but not a valid EIP-55 checksum,
140
+ * ethers.js will throw "bad address checksum" error.
141
+ *
142
+ * Converting the address to lowercase bypasses checksum validation in ethers.js
143
+ * since lowercase addresses are treated as not checksummed.
144
+ *
145
+ * @see https://github.com/rsksmart/RSKIPs/blob/master/IPs/RSKIP60.md
146
+ */
147
+ export function normalizeAddress(address: string): string {
148
+ return address.toLowerCase()
149
+ }
150
+
151
+ /**
152
+ * Safely converts a value to BigInt, returning 0n if conversion fails
153
+ */
154
+ export const safeBigInt = (value: string | number | bigint): bigint => {
155
+ try {
156
+ return BigInt(value.toString())
157
+ } catch {
158
+ return 0n
159
+ }
160
+ }
161
+
162
+ export const unwrapProxy = (proxy: unknown): unknown => {
163
+ if (proxy && typeof proxy === 'object' && proxy.constructor.name === 'Proxy') {
164
+ return Object.fromEntries(Object.entries(proxy).map(([k, v]) => [k, unwrapProxy(v)]))
165
+ }
166
+ if (Array.isArray(proxy)) return proxy.map(unwrapProxy)
167
+ return proxy
168
+ }
169
+
170
+ /**
171
+ * Checks if a transaction intent is a staking intent
172
+ */
173
+ // NOTE: `valId`/`withdrawId` should eventually be promoted to first-class properties of
174
+ // `StakingTransactionIntent` in @ledgerhq/coin-module-framework. Kept as local augmentations for now.
175
+ export type EvmStakingIntent = StakingTransactionIntent & {
176
+ valId?: string
177
+ withdrawId?: string
178
+ txValue?: bigint
179
+ shares?: bigint
180
+ }
181
+
182
+ export function isStakingIntent(intent: TransactionIntent): intent is EvmStakingIntent {
183
+ return intent.intentType === 'staking'
184
+ }
package/src/logic.test.ts CHANGED
@@ -8,17 +8,11 @@ jest.mock('./network/node/index', () => ({
8
8
  ),
9
9
  }))
10
10
 
11
- import { getCoinConfig } from './config'
12
- import {
13
- createMockEvmCoinConfig,
14
- createMockEvmConfigInfo,
15
- mkExternalNode,
16
- mkLedgerNode,
17
- } from './fixtures/context.fixtures'
11
+ import { createMockEvmConfigInfo, mkExternalNode, testLogger } from './fixtures/context.fixtures'
18
12
  import { getAdditionalLayer2Fees } from './logic'
13
+ import { padHexString, safeEncodeEIP55 } from './logic-public'
19
14
  import { getNodeApi } from './network/node/index'
20
15
  import { mockNodeApi } from './network/node/node.fixtures'
21
- import { padHexString, safeEncodeEIP55 } from './utils'
22
16
 
23
17
  const mockGetNodeApi = jest.mocked(getNodeApi)
24
18
  const nodeApiMock = mockNodeApi()
@@ -27,54 +21,6 @@ const {
27
21
  getScrollAdditionalFees: mockGetScrollAdditionalFees,
28
22
  } = nodeApiMock
29
23
 
30
- jest.mock('./config')
31
- const mockGetConfig = jest.mocked(getCoinConfig)
32
-
33
- mockGetConfig.mockImplementation((currencyId) => {
34
- switch (currencyId) {
35
- case 'ethereum':
36
- return createMockEvmCoinConfig({
37
- chainId: 1,
38
- node: mkLedgerNode({ explorerId: 'eth' }),
39
- explorer: { type: 'ledger', explorerId: 'eth' },
40
- })
41
- case 'matic':
42
- return createMockEvmCoinConfig({
43
- chainId: 137,
44
- node: mkLedgerNode({ explorerId: 'matic' }),
45
- explorer: { type: 'ledger', explorerId: 'matic' },
46
- })
47
- case 'optimism':
48
- return createMockEvmCoinConfig({ chainId: 10, node: mkExternalNode({ uri: 'optimism_uri' }) })
49
- case 'scroll':
50
- return createMockEvmCoinConfig({
51
- chainId: 534352,
52
- node: mkExternalNode({ uri: 'scroll_uri' }),
53
- })
54
- case 'polygon':
55
- return createMockEvmCoinConfig({
56
- chainId: 137,
57
- node: mkLedgerNode({ explorerId: 'polygon' }),
58
- })
59
- case 'bsc':
60
- return createMockEvmCoinConfig({ chainId: 56, node: mkLedgerNode({ explorerId: 'bsc' }) })
61
- case 'anything':
62
- return createMockEvmCoinConfig({
63
- chainId: 1,
64
- node: mkExternalNode({ uri: 'anything' }),
65
- explorer: { type: 'etherscan', uri: 'anything' },
66
- })
67
- case 'somethingelse':
68
- return createMockEvmCoinConfig({
69
- chainId: 1,
70
- node: mkLedgerNode({ explorerId: 'somethingelse' }),
71
- explorer: { type: 'blockscout', uri: 'somethingelse' },
72
- })
73
- default:
74
- throw new Error(`unexpected currencyId: ${currencyId}`)
75
- }
76
- })
77
-
78
24
  describe('EVM Family', () => {
79
25
  describe('logic.ts', () => {
80
26
  describe('getAdditionalLayer2Fees', () => {
@@ -85,15 +31,15 @@ describe('EVM Family', () => {
85
31
 
86
32
  it('should try to get additionalFees for a valid layer 2', async () => {
87
33
  const cfg = createMockEvmConfigInfo({ chainId: 1, node: mkExternalNode() })
88
- await getAdditionalLayer2Fees(cfg, 'optimism', '')
34
+ await getAdditionalLayer2Fees(cfg, 'optimism', '', testLogger)
89
35
  expect(mockGetOptimismAdditionalFees).toHaveBeenCalled()
90
- await getAdditionalLayer2Fees(cfg, 'scroll', '')
36
+ await getAdditionalLayer2Fees(cfg, 'scroll', '', testLogger)
91
37
  expect(mockGetScrollAdditionalFees).toHaveBeenCalled()
92
38
  })
93
39
 
94
40
  it('should not try to get additionalFees for an invalid layer 2', async () => {
95
41
  const cfg = createMockEvmConfigInfo({ chainId: 1, node: mkExternalNode() })
96
- await getAdditionalLayer2Fees(cfg, 'ethereum', '')
42
+ await getAdditionalLayer2Fees(cfg, 'ethereum', '', testLogger)
97
43
  expect(mockGetOptimismAdditionalFees).not.toHaveBeenCalled()
98
44
  expect(mockGetScrollAdditionalFees).not.toHaveBeenCalled()
99
45
  })
package/src/logic.ts CHANGED
@@ -2,6 +2,7 @@
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
4
  import type { EvmConfigInfo } from './config'
5
+ import type { Logger } from '@ledgerhq/coin-module-framework/config'
5
6
  import type BigNumber from 'bignumber.js'
6
7
  import { getNodeApi } from './network/node/index'
7
8
 
@@ -12,7 +13,8 @@ import { getNodeApi } from './network/node/index'
12
13
  export const getAdditionalLayer2Fees = async (
13
14
  config: EvmConfigInfo,
14
15
  currencyId: string,
15
- transaction: string
16
+ transaction: string,
17
+ logger: Logger
16
18
  ): Promise<BigNumber | undefined> => {
17
19
  switch (currencyId) {
18
20
  case 'optimism':
@@ -21,12 +23,12 @@ export const getAdditionalLayer2Fees = async (
21
23
  case 'blast_sepolia':
22
24
  case 'base':
23
25
  case 'base_sepolia': {
24
- const nodeApi = getNodeApi(config, currencyId)
26
+ const nodeApi = getNodeApi(config, currencyId, logger)
25
27
  const additionalFees = await nodeApi.getOptimismAdditionalFees(currencyId, transaction)
26
28
  return additionalFees
27
29
  }
28
30
  case 'scroll': {
29
- const nodeApi = getNodeApi(config, currencyId)
31
+ const nodeApi = getNodeApi(config, currencyId, logger)
30
32
  const additionalFees = await nodeApi.getScrollAdditionalFees(currencyId, transaction)
31
33
  return additionalFees
32
34
  }