@ledgerhq/coin-cardano 0.1.0-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 (377) hide show
  1. package/.eslintrc.js +20 -0
  2. package/.turbo/turbo-build.log +4 -0
  3. package/.unimportedrc.json +35 -0
  4. package/CHANGELOG.md +19 -0
  5. package/LICENSE.txt +21 -0
  6. package/jest.config.js +8 -0
  7. package/lib/account.d.ts +7 -0
  8. package/lib/account.d.ts.map +1 -0
  9. package/lib/account.js +41 -0
  10. package/lib/account.js.map +1 -0
  11. package/lib/address.unit.test.d.ts +2 -0
  12. package/lib/address.unit.test.d.ts.map +1 -0
  13. package/lib/address.unit.test.js +21 -0
  14. package/lib/address.unit.test.js.map +1 -0
  15. package/lib/api/api-types.d.ts +106 -0
  16. package/lib/api/api-types.d.ts.map +1 -0
  17. package/lib/api/api-types.js +9 -0
  18. package/lib/api/api-types.js.map +1 -0
  19. package/lib/api/getDelegationInfo.d.ts +4 -0
  20. package/lib/api/getDelegationInfo.d.ts.map +1 -0
  21. package/lib/api/getDelegationInfo.js +48 -0
  22. package/lib/api/getDelegationInfo.js.map +1 -0
  23. package/lib/api/getNetworkInfo.d.ts +5 -0
  24. package/lib/api/getNetworkInfo.d.ts.map +1 -0
  25. package/lib/api/getNetworkInfo.js +46 -0
  26. package/lib/api/getNetworkInfo.js.map +1 -0
  27. package/lib/api/getPools.d.ts +5 -0
  28. package/lib/api/getPools.d.ts.map +1 -0
  29. package/lib/api/getPools.js +45 -0
  30. package/lib/api/getPools.js.map +1 -0
  31. package/lib/api/getTransactions.d.ts +10 -0
  32. package/lib/api/getTransactions.d.ts.map +1 -0
  33. package/lib/api/getTransactions.js +139 -0
  34. package/lib/api/getTransactions.js.map +1 -0
  35. package/lib/api/submitTransaction.d.ts +7 -0
  36. package/lib/api/submitTransaction.d.ts.map +1 -0
  37. package/lib/api/submitTransaction.js +34 -0
  38. package/lib/api/submitTransaction.js.map +1 -0
  39. package/lib/bridge/index.d.ts +11 -0
  40. package/lib/bridge/index.d.ts.map +1 -0
  41. package/lib/bridge/index.js +70 -0
  42. package/lib/bridge/index.js.map +1 -0
  43. package/lib/bridge.integration.test.d.ts +4 -0
  44. package/lib/bridge.integration.test.d.ts.map +1 -0
  45. package/lib/bridge.integration.test.js +113 -0
  46. package/lib/bridge.integration.test.js.map +1 -0
  47. package/lib/broadcast.d.ts +8 -0
  48. package/lib/broadcast.d.ts.map +1 -0
  49. package/lib/broadcast.js +28 -0
  50. package/lib/broadcast.js.map +1 -0
  51. package/lib/buildOptimisticOperation.d.ts +4 -0
  52. package/lib/buildOptimisticOperation.d.ts.map +1 -0
  53. package/lib/buildOptimisticOperation.js +125 -0
  54. package/lib/buildOptimisticOperation.js.map +1 -0
  55. package/lib/buildSubAccounts.d.ts +26 -0
  56. package/lib/buildSubAccounts.d.ts.map +1 -0
  57. package/lib/buildSubAccounts.js +124 -0
  58. package/lib/buildSubAccounts.js.map +1 -0
  59. package/lib/buildTransaction.d.ts +11 -0
  60. package/lib/buildTransaction.d.ts.map +1 -0
  61. package/lib/buildTransaction.js +342 -0
  62. package/lib/buildTransaction.js.map +1 -0
  63. package/lib/cli-transaction.d.ts +18 -0
  64. package/lib/cli-transaction.d.ts.map +1 -0
  65. package/lib/cli-transaction.js +66 -0
  66. package/lib/cli-transaction.js.map +1 -0
  67. package/lib/constants.d.ts +8 -0
  68. package/lib/constants.d.ts.map +1 -0
  69. package/lib/constants.js +12 -0
  70. package/lib/constants.js.map +1 -0
  71. package/lib/createTransaction.d.ts +9 -0
  72. package/lib/createTransaction.d.ts.map +1 -0
  73. package/lib/createTransaction.js +23 -0
  74. package/lib/createTransaction.js.map +1 -0
  75. package/lib/datasets/rawAccount.1.d.ts +3 -0
  76. package/lib/datasets/rawAccount.1.d.ts.map +1 -0
  77. package/lib/datasets/rawAccount.1.js +20 -0
  78. package/lib/datasets/rawAccount.1.js.map +1 -0
  79. package/lib/datasets/scanAccounts.d.ts +5 -0
  80. package/lib/datasets/scanAccounts.d.ts.map +1 -0
  81. package/lib/datasets/scanAccounts.js +27 -0
  82. package/lib/datasets/scanAccounts.js.map +1 -0
  83. package/lib/deviceTransactionConfig.d.ts +11 -0
  84. package/lib/deviceTransactionConfig.d.ts.map +1 -0
  85. package/lib/deviceTransactionConfig.js +106 -0
  86. package/lib/deviceTransactionConfig.js.map +1 -0
  87. package/lib/errors.d.ts +22 -0
  88. package/lib/errors.d.ts.map +1 -0
  89. package/lib/errors.js +18 -0
  90. package/lib/errors.js.map +1 -0
  91. package/lib/estimateMaxSpendable.d.ts +9 -0
  92. package/lib/estimateMaxSpendable.d.ts.map +1 -0
  93. package/lib/estimateMaxSpendable.js +53 -0
  94. package/lib/estimateMaxSpendable.js.map +1 -0
  95. package/lib/getTransactionStatus.d.ts +4 -0
  96. package/lib/getTransactionStatus.d.ts.map +1 -0
  97. package/lib/getTransactionStatus.js +214 -0
  98. package/lib/getTransactionStatus.js.map +1 -0
  99. package/lib/getTransactionStatus.unit.test.d.ts +2 -0
  100. package/lib/getTransactionStatus.unit.test.d.ts.map +1 -0
  101. package/lib/getTransactionStatus.unit.test.js +54 -0
  102. package/lib/getTransactionStatus.unit.test.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 +37 -0
  106. package/lib/hw-getAddress.js.map +1 -0
  107. package/lib/logic.d.ts +88 -0
  108. package/lib/logic.d.ts.map +1 -0
  109. package/lib/logic.js +264 -0
  110. package/lib/logic.js.map +1 -0
  111. package/lib/logic.unit.test.d.ts +2 -0
  112. package/lib/logic.unit.test.d.ts.map +1 -0
  113. package/lib/logic.unit.test.js +40 -0
  114. package/lib/logic.unit.test.js.map +1 -0
  115. package/lib/networks.d.ts +3 -0
  116. package/lib/networks.d.ts.map +1 -0
  117. package/lib/networks.js +36 -0
  118. package/lib/networks.js.map +1 -0
  119. package/lib/postSyncPatch.d.ts +6 -0
  120. package/lib/postSyncPatch.d.ts.map +1 -0
  121. package/lib/postSyncPatch.js +35 -0
  122. package/lib/postSyncPatch.js.map +1 -0
  123. package/lib/prepareTransaction.d.ts +10 -0
  124. package/lib/prepareTransaction.d.ts.map +1 -0
  125. package/lib/prepareTransaction.js +44 -0
  126. package/lib/prepareTransaction.js.map +1 -0
  127. package/lib/serialization.d.ts +7 -0
  128. package/lib/serialization.d.ts.map +1 -0
  129. package/lib/serialization.js +146 -0
  130. package/lib/serialization.js.map +1 -0
  131. package/lib/signOperation.d.ts +9 -0
  132. package/lib/signOperation.d.ts.map +1 -0
  133. package/lib/signOperation.js +71 -0
  134. package/lib/signOperation.js.map +1 -0
  135. package/lib/signer.d.ts +92 -0
  136. package/lib/signer.d.ts.map +1 -0
  137. package/lib/signer.js +3 -0
  138. package/lib/signer.js.map +1 -0
  139. package/lib/specs.d.ts +7 -0
  140. package/lib/specs.d.ts.map +1 -0
  141. package/lib/specs.js +218 -0
  142. package/lib/specs.js.map +1 -0
  143. package/lib/speculos-deviceActions.d.ts +4 -0
  144. package/lib/speculos-deviceActions.d.ts.map +1 -0
  145. package/lib/speculos-deviceActions.js +128 -0
  146. package/lib/speculos-deviceActions.js.map +1 -0
  147. package/lib/synchronisation.d.ts +6 -0
  148. package/lib/synchronisation.d.ts.map +1 -0
  149. package/lib/synchronisation.js +237 -0
  150. package/lib/synchronisation.js.map +1 -0
  151. package/lib/synchronisation.unit.test.d.ts +2 -0
  152. package/lib/synchronisation.unit.test.d.ts.map +1 -0
  153. package/lib/synchronisation.unit.test.js +129 -0
  154. package/lib/synchronisation.unit.test.js.map +1 -0
  155. package/lib/transaction.d.ts +15 -0
  156. package/lib/transaction.d.ts.map +1 -0
  157. package/lib/transaction.js +43 -0
  158. package/lib/transaction.js.map +1 -0
  159. package/lib/types.d.ts +188 -0
  160. package/lib/types.d.ts.map +1 -0
  161. package/lib/types.js +18 -0
  162. package/lib/types.js.map +1 -0
  163. package/lib/typhonSerializer.d.ts +17 -0
  164. package/lib/typhonSerializer.d.ts.map +1 -0
  165. package/lib/typhonSerializer.js +213 -0
  166. package/lib/typhonSerializer.js.map +1 -0
  167. package/lib/utils.d.ts +2 -0
  168. package/lib/utils.d.ts.map +1 -0
  169. package/lib/utils.js +8 -0
  170. package/lib/utils.js.map +1 -0
  171. package/lib-es/account.d.ts +7 -0
  172. package/lib-es/account.d.ts.map +1 -0
  173. package/lib-es/account.js +34 -0
  174. package/lib-es/account.js.map +1 -0
  175. package/lib-es/address.unit.test.d.ts +2 -0
  176. package/lib-es/address.unit.test.d.ts.map +1 -0
  177. package/lib-es/address.unit.test.js +19 -0
  178. package/lib-es/address.unit.test.js.map +1 -0
  179. package/lib-es/api/api-types.d.ts +106 -0
  180. package/lib-es/api/api-types.d.ts.map +1 -0
  181. package/lib-es/api/api-types.js +6 -0
  182. package/lib-es/api/api-types.js.map +1 -0
  183. package/lib-es/api/getDelegationInfo.d.ts +4 -0
  184. package/lib-es/api/getDelegationInfo.d.ts.map +1 -0
  185. package/lib-es/api/getDelegationInfo.js +41 -0
  186. package/lib-es/api/getDelegationInfo.js.map +1 -0
  187. package/lib-es/api/getNetworkInfo.d.ts +5 -0
  188. package/lib-es/api/getNetworkInfo.d.ts.map +1 -0
  189. package/lib-es/api/getNetworkInfo.js +39 -0
  190. package/lib-es/api/getNetworkInfo.js.map +1 -0
  191. package/lib-es/api/getPools.d.ts +5 -0
  192. package/lib-es/api/getPools.d.ts.map +1 -0
  193. package/lib-es/api/getPools.js +37 -0
  194. package/lib-es/api/getPools.js.map +1 -0
  195. package/lib-es/api/getTransactions.d.ts +10 -0
  196. package/lib-es/api/getTransactions.d.ts.map +1 -0
  197. package/lib-es/api/getTransactions.js +132 -0
  198. package/lib-es/api/getTransactions.js.map +1 -0
  199. package/lib-es/api/submitTransaction.d.ts +7 -0
  200. package/lib-es/api/submitTransaction.d.ts.map +1 -0
  201. package/lib-es/api/submitTransaction.js +27 -0
  202. package/lib-es/api/submitTransaction.js.map +1 -0
  203. package/lib-es/bridge/index.d.ts +11 -0
  204. package/lib-es/bridge/index.d.ts.map +1 -0
  205. package/lib-es/bridge/index.js +61 -0
  206. package/lib-es/bridge/index.js.map +1 -0
  207. package/lib-es/bridge.integration.test.d.ts +4 -0
  208. package/lib-es/bridge.integration.test.d.ts.map +1 -0
  209. package/lib-es/bridge.integration.test.js +107 -0
  210. package/lib-es/bridge.integration.test.js.map +1 -0
  211. package/lib-es/broadcast.d.ts +8 -0
  212. package/lib-es/broadcast.d.ts.map +1 -0
  213. package/lib-es/broadcast.js +24 -0
  214. package/lib-es/broadcast.js.map +1 -0
  215. package/lib-es/buildOptimisticOperation.d.ts +4 -0
  216. package/lib-es/buildOptimisticOperation.d.ts.map +1 -0
  217. package/lib-es/buildOptimisticOperation.js +118 -0
  218. package/lib-es/buildOptimisticOperation.js.map +1 -0
  219. package/lib-es/buildSubAccounts.d.ts +26 -0
  220. package/lib-es/buildSubAccounts.d.ts.map +1 -0
  221. package/lib-es/buildSubAccounts.js +114 -0
  222. package/lib-es/buildSubAccounts.js.map +1 -0
  223. package/lib-es/buildTransaction.d.ts +11 -0
  224. package/lib-es/buildTransaction.d.ts.map +1 -0
  225. package/lib-es/buildTransaction.js +335 -0
  226. package/lib-es/buildTransaction.js.map +1 -0
  227. package/lib-es/cli-transaction.d.ts +18 -0
  228. package/lib-es/cli-transaction.d.ts.map +1 -0
  229. package/lib-es/cli-transaction.js +60 -0
  230. package/lib-es/cli-transaction.js.map +1 -0
  231. package/lib-es/constants.d.ts +8 -0
  232. package/lib-es/constants.d.ts.map +1 -0
  233. package/lib-es/constants.js +9 -0
  234. package/lib-es/constants.js.map +1 -0
  235. package/lib-es/createTransaction.d.ts +9 -0
  236. package/lib-es/createTransaction.d.ts.map +1 -0
  237. package/lib-es/createTransaction.js +16 -0
  238. package/lib-es/createTransaction.js.map +1 -0
  239. package/lib-es/datasets/rawAccount.1.d.ts +3 -0
  240. package/lib-es/datasets/rawAccount.1.d.ts.map +1 -0
  241. package/lib-es/datasets/rawAccount.1.js +17 -0
  242. package/lib-es/datasets/rawAccount.1.js.map +1 -0
  243. package/lib-es/datasets/scanAccounts.d.ts +5 -0
  244. package/lib-es/datasets/scanAccounts.d.ts.map +1 -0
  245. package/lib-es/datasets/scanAccounts.js +24 -0
  246. package/lib-es/datasets/scanAccounts.js.map +1 -0
  247. package/lib-es/deviceTransactionConfig.d.ts +11 -0
  248. package/lib-es/deviceTransactionConfig.d.ts.map +1 -0
  249. package/lib-es/deviceTransactionConfig.js +101 -0
  250. package/lib-es/deviceTransactionConfig.js.map +1 -0
  251. package/lib-es/errors.d.ts +22 -0
  252. package/lib-es/errors.d.ts.map +1 -0
  253. package/lib-es/errors.js +15 -0
  254. package/lib-es/errors.js.map +1 -0
  255. package/lib-es/estimateMaxSpendable.d.ts +9 -0
  256. package/lib-es/estimateMaxSpendable.d.ts.map +1 -0
  257. package/lib-es/estimateMaxSpendable.js +49 -0
  258. package/lib-es/estimateMaxSpendable.js.map +1 -0
  259. package/lib-es/getTransactionStatus.d.ts +4 -0
  260. package/lib-es/getTransactionStatus.d.ts.map +1 -0
  261. package/lib-es/getTransactionStatus.js +210 -0
  262. package/lib-es/getTransactionStatus.js.map +1 -0
  263. package/lib-es/getTransactionStatus.unit.test.d.ts +2 -0
  264. package/lib-es/getTransactionStatus.unit.test.d.ts.map +1 -0
  265. package/lib-es/getTransactionStatus.unit.test.js +49 -0
  266. package/lib-es/getTransactionStatus.unit.test.js.map +1 -0
  267. package/lib-es/hw-getAddress.d.ts +6 -0
  268. package/lib-es/hw-getAddress.d.ts.map +1 -0
  269. package/lib-es/hw-getAddress.js +35 -0
  270. package/lib-es/hw-getAddress.js.map +1 -0
  271. package/lib-es/logic.d.ts +88 -0
  272. package/lib-es/logic.d.ts.map +1 -0
  273. package/lib-es/logic.js +236 -0
  274. package/lib-es/logic.js.map +1 -0
  275. package/lib-es/logic.unit.test.d.ts +2 -0
  276. package/lib-es/logic.unit.test.d.ts.map +1 -0
  277. package/lib-es/logic.unit.test.js +35 -0
  278. package/lib-es/logic.unit.test.js.map +1 -0
  279. package/lib-es/networks.d.ts +3 -0
  280. package/lib-es/networks.d.ts.map +1 -0
  281. package/lib-es/networks.js +32 -0
  282. package/lib-es/networks.js.map +1 -0
  283. package/lib-es/postSyncPatch.d.ts +6 -0
  284. package/lib-es/postSyncPatch.d.ts.map +1 -0
  285. package/lib-es/postSyncPatch.js +31 -0
  286. package/lib-es/postSyncPatch.js.map +1 -0
  287. package/lib-es/prepareTransaction.d.ts +10 -0
  288. package/lib-es/prepareTransaction.d.ts.map +1 -0
  289. package/lib-es/prepareTransaction.js +40 -0
  290. package/lib-es/prepareTransaction.js.map +1 -0
  291. package/lib-es/serialization.d.ts +7 -0
  292. package/lib-es/serialization.d.ts.map +1 -0
  293. package/lib-es/serialization.js +139 -0
  294. package/lib-es/serialization.js.map +1 -0
  295. package/lib-es/signOperation.d.ts +9 -0
  296. package/lib-es/signOperation.d.ts.map +1 -0
  297. package/lib-es/signOperation.js +64 -0
  298. package/lib-es/signOperation.js.map +1 -0
  299. package/lib-es/signer.d.ts +92 -0
  300. package/lib-es/signer.d.ts.map +1 -0
  301. package/lib-es/signer.js +2 -0
  302. package/lib-es/signer.js.map +1 -0
  303. package/lib-es/specs.d.ts +7 -0
  304. package/lib-es/specs.d.ts.map +1 -0
  305. package/lib-es/specs.js +213 -0
  306. package/lib-es/specs.js.map +1 -0
  307. package/lib-es/speculos-deviceActions.d.ts +4 -0
  308. package/lib-es/speculos-deviceActions.d.ts.map +1 -0
  309. package/lib-es/speculos-deviceActions.js +125 -0
  310. package/lib-es/speculos-deviceActions.js.map +1 -0
  311. package/lib-es/synchronisation.d.ts +6 -0
  312. package/lib-es/synchronisation.d.ts.map +1 -0
  313. package/lib-es/synchronisation.js +230 -0
  314. package/lib-es/synchronisation.js.map +1 -0
  315. package/lib-es/synchronisation.unit.test.d.ts +2 -0
  316. package/lib-es/synchronisation.unit.test.d.ts.map +1 -0
  317. package/lib-es/synchronisation.unit.test.js +124 -0
  318. package/lib-es/synchronisation.unit.test.js.map +1 -0
  319. package/lib-es/transaction.d.ts +15 -0
  320. package/lib-es/transaction.d.ts.map +1 -0
  321. package/lib-es/transaction.js +37 -0
  322. package/lib-es/transaction.js.map +1 -0
  323. package/lib-es/types.d.ts +188 -0
  324. package/lib-es/types.d.ts.map +1 -0
  325. package/lib-es/types.js +15 -0
  326. package/lib-es/types.js.map +1 -0
  327. package/lib-es/typhonSerializer.d.ts +17 -0
  328. package/lib-es/typhonSerializer.d.ts.map +1 -0
  329. package/lib-es/typhonSerializer.js +207 -0
  330. package/lib-es/typhonSerializer.js.map +1 -0
  331. package/lib-es/utils.d.ts +2 -0
  332. package/lib-es/utils.d.ts.map +1 -0
  333. package/lib-es/utils.js +5 -0
  334. package/lib-es/utils.js.map +1 -0
  335. package/package.json +91 -0
  336. package/src/account.ts +39 -0
  337. package/src/address.unit.test.ts +32 -0
  338. package/src/api/api-types.ts +115 -0
  339. package/src/api/getDelegationInfo.ts +39 -0
  340. package/src/api/getNetworkInfo.ts +34 -0
  341. package/src/api/getPools.ts +35 -0
  342. package/src/api/getTransactions.ts +218 -0
  343. package/src/api/submitTransaction.ts +24 -0
  344. package/src/bridge/index.ts +69 -0
  345. package/src/bridge.integration.test.ts +113 -0
  346. package/src/broadcast.ts +21 -0
  347. package/src/buildOptimisticOperation.ts +181 -0
  348. package/src/buildSubAccounts.ts +170 -0
  349. package/src/buildTransaction.ts +435 -0
  350. package/src/cli-transaction.ts +82 -0
  351. package/src/constants.ts +10 -0
  352. package/src/createTransaction.ts +18 -0
  353. package/src/datasets/rawAccount.1.ts +19 -0
  354. package/src/datasets/scanAccounts.ts +23 -0
  355. package/src/deviceTransactionConfig.ts +128 -0
  356. package/src/errors.ts +18 -0
  357. package/src/estimateMaxSpendable.ts +61 -0
  358. package/src/getTransactionStatus.ts +251 -0
  359. package/src/getTransactionStatus.unit.test.ts +45 -0
  360. package/src/hw-getAddress.ts +37 -0
  361. package/src/logic.ts +330 -0
  362. package/src/logic.unit.test.ts +39 -0
  363. package/src/networks.ts +33 -0
  364. package/src/postSyncPatch.ts +42 -0
  365. package/src/prepareTransaction.ts +41 -0
  366. package/src/serialization.ts +219 -0
  367. package/src/signOperation.ts +82 -0
  368. package/src/signer.ts +98 -0
  369. package/src/specs.ts +253 -0
  370. package/src/speculos-deviceActions.ts +132 -0
  371. package/src/synchronisation.ts +347 -0
  372. package/src/synchronisation.unit.test.ts +137 -0
  373. package/src/transaction.ts +66 -0
  374. package/src/types.ts +227 -0
  375. package/src/typhonSerializer.ts +259 -0
  376. package/src/utils.ts +4 -0
  377. package/tsconfig.json +12 -0
@@ -0,0 +1,347 @@
1
+ import uniqBy from "lodash/uniqBy";
2
+ import BigNumber from "bignumber.js";
3
+ import {
4
+ AccountShapeInfo,
5
+ GetAccountShape,
6
+ mergeOps,
7
+ } from "@ledgerhq/coin-framework/bridge/jsHelpers";
8
+ import { utils as TyphonUtils } from "@stricahq/typhonjs";
9
+ import { SignerContext } from "@ledgerhq/coin-framework/signer";
10
+ import { listTokensForCryptoCurrency } from "@ledgerhq/cryptoassets";
11
+ import { encodeOperationId } from "@ledgerhq/coin-framework/operation";
12
+ import { encodeAccountId } from "@ledgerhq/coin-framework/account/index";
13
+ import { calculateMinUtxoAmount } from "@stricahq/typhonjs/dist/utils/utils";
14
+ import { formatCurrencyUnit } from "@ledgerhq/coin-framework/currencies/index";
15
+ import { inferSubOperations } from "@ledgerhq/coin-framework/serialization/index";
16
+ import type { Operation, OperationType, TokenAccount } from "@ledgerhq/types-live";
17
+ import { getDelegationInfo } from "./api/getDelegationInfo";
18
+ import { APITransaction, HashType } from "./api/api-types";
19
+ import { getTransactions } from "./api/getTransactions";
20
+ import { getNetworkInfo } from "./api/getNetworkInfo";
21
+ import { buildSubAccounts } from "./buildSubAccounts";
22
+ import { getNetworkParameters } from "./networks";
23
+ import { CardanoSigner } from "./signer";
24
+ import {
25
+ CardanoAccount,
26
+ CardanoOperation,
27
+ CardanoOperationExtra,
28
+ CardanoOutput,
29
+ PaymentCredential,
30
+ ProtocolParams,
31
+ StakeCredential,
32
+ } from "./types";
33
+ import {
34
+ getAccountChange,
35
+ getAccountStakeCredential,
36
+ getBaseAddress,
37
+ getBipPathString,
38
+ getMemoFromTx,
39
+ getOperationType,
40
+ isHexString,
41
+ mergeTokens,
42
+ } from "./logic";
43
+
44
+ export const makeGetAccountShape =
45
+ (signerContext: SignerContext<CardanoSigner>): GetAccountShape<CardanoAccount> =>
46
+ async (info, { blacklistedTokenIds }) => {
47
+ const {
48
+ currency,
49
+ index: accountIndex,
50
+ derivationPath,
51
+ derivationMode,
52
+ initialAccount,
53
+ deviceId,
54
+ } = info;
55
+ // In case we get a full derivation path
56
+ const rootPath = derivationPath.split("/", 2).join("/");
57
+ const accountPath = `${rootPath}/${accountIndex}'`;
58
+
59
+ const paramXpub = initialAccount?.xpub;
60
+ let xpub;
61
+ if (!paramXpub) {
62
+ if (deviceId === undefined || deviceId === null) {
63
+ // deviceId not provided
64
+ throw new Error("deviceId required to generate the xpub");
65
+ }
66
+ const extendedPubKeyRes = await signerContext(deviceId, signer =>
67
+ signer.getPublicKey(accountPath),
68
+ );
69
+ xpub = `${extendedPubKeyRes.publicKeyHex}${extendedPubKeyRes.chainCodeHex}`;
70
+ } else {
71
+ xpub = paramXpub;
72
+ }
73
+ const accountId = encodeAccountId({
74
+ type: "js",
75
+ version: "2",
76
+ currencyId: currency.id,
77
+ xpubOrAddress: xpub,
78
+ derivationMode,
79
+ });
80
+
81
+ // when new tokens are added / blacklist changes, we need to sync again because we need to go through all operations again
82
+ const syncHash =
83
+ JSON.stringify(blacklistedTokenIds || []) +
84
+ "_" +
85
+ listTokensForCryptoCurrency(currency, {
86
+ withDelisted: true,
87
+ }).length;
88
+ const outdatedSyncHash = initialAccount?.syncHash !== syncHash;
89
+
90
+ const requiredConfirmations = 90;
91
+
92
+ const oldOperations = initialAccount?.operations || [];
93
+ const lastBlockHeight = oldOperations.length ? (oldOperations[0].blockHeight || 0) + 1 : 0;
94
+
95
+ const syncFromBlockHeight = outdatedSyncHash
96
+ ? 0
97
+ : lastBlockHeight > requiredConfirmations
98
+ ? lastBlockHeight - requiredConfirmations
99
+ : 0;
100
+
101
+ const {
102
+ transactions: newTransactions,
103
+ blockHeight,
104
+ externalCredentials,
105
+ internalCredentials,
106
+ } = await getTransactions(xpub, accountIndex, initialAccount, syncFromBlockHeight, currency);
107
+
108
+ const accountCredentialsMap = [...externalCredentials, ...internalCredentials].reduce(
109
+ (finalMap, cred) => {
110
+ finalMap[cred.key] = cred;
111
+ return finalMap;
112
+ },
113
+ {} as Record<string, PaymentCredential>,
114
+ );
115
+
116
+ const stableOperationsByIds: Record<string, Operation> = {};
117
+ (initialAccount?.operations || []).forEach(o => {
118
+ if ((o.blockHeight as number) < syncFromBlockHeight) {
119
+ stableOperationsByIds[o.hash] = o;
120
+ }
121
+ });
122
+
123
+ const stableUtxos = (initialAccount?.cardanoResources?.utxos || []).filter(
124
+ u => stableOperationsByIds[u.hash],
125
+ );
126
+
127
+ const utxos = prepareUtxos(newTransactions, stableUtxos, accountCredentialsMap);
128
+ const utxosSum = utxos.reduce((total, u) => total.plus(u.amount), new BigNumber(0));
129
+ const tokenBalance = mergeTokens(utxos.map(u => u.tokens).flat());
130
+ const subAccounts = buildSubAccounts({
131
+ initialAccount,
132
+ parentAccountId: accountId,
133
+ parentCurrency: currency,
134
+ newTransactions,
135
+ tokens: tokenBalance,
136
+ accountCredentialsMap,
137
+ }).filter(a => !blacklistedTokenIds?.includes(a.token.id));
138
+
139
+ const stakeCredential = getAccountStakeCredential(xpub, accountIndex);
140
+ const networkParams = getNetworkParameters(currency.id);
141
+ const freshAddresses = externalCredentials
142
+ .filter(c => !c.isUsed)
143
+ .map(c => ({
144
+ derivationPath: getBipPathString(c.path),
145
+ address: getBaseAddress({
146
+ networkId: networkParams.networkId,
147
+ paymentCred: c,
148
+ stakeCred: stakeCredential,
149
+ }).getBech32(),
150
+ }));
151
+ const cardanoNetworkInfo = await getNetworkInfo(initialAccount, currency);
152
+ const delegationInfo = await getDelegationInfo(currency, stakeCredential.key);
153
+
154
+ const totalBalance = delegationInfo?.rewards ? utxosSum.plus(delegationInfo.rewards) : utxosSum;
155
+
156
+ const minAdaBalanceForTokens = tokenBalance.length
157
+ ? calculateMinUtxoAmount(
158
+ tokenBalance,
159
+ new BigNumber(cardanoNetworkInfo.protocolParams.lovelacePerUtxoWord),
160
+ false,
161
+ )
162
+ : new BigNumber(0);
163
+
164
+ const newOperations = newTransactions.map(t =>
165
+ mapTxToAccountOperation(
166
+ t,
167
+ accountId,
168
+ accountCredentialsMap,
169
+ stakeCredential,
170
+ subAccounts,
171
+ info,
172
+ cardanoNetworkInfo.protocolParams,
173
+ ),
174
+ );
175
+
176
+ const operations = mergeOps(Object.values(stableOperationsByIds), newOperations);
177
+
178
+ return {
179
+ id: accountId,
180
+ xpub,
181
+ balance: totalBalance,
182
+ spendableBalance: utxosSum.minus(minAdaBalanceForTokens),
183
+ operations: operations,
184
+ syncHash,
185
+ subAccounts,
186
+ freshAddress: freshAddresses[0].address,
187
+ freshAddressPath: freshAddresses[0].derivationPath,
188
+ blockHeight,
189
+ cardanoResources: {
190
+ utxos,
191
+ externalCredentials,
192
+ internalCredentials,
193
+ delegation: delegationInfo,
194
+ protocolParams: cardanoNetworkInfo.protocolParams,
195
+ },
196
+ };
197
+ };
198
+
199
+ function mapTxToAccountOperation(
200
+ tx: APITransaction,
201
+ accountId: string,
202
+ accountCredentialsMap: Record<string, PaymentCredential>,
203
+ stakeCredential: StakeCredential,
204
+ subAccounts: Array<TokenAccount>,
205
+ accountShapeInfo: AccountShapeInfo,
206
+ protocolParams: ProtocolParams,
207
+ ): CardanoOperation {
208
+ const accountChange = getAccountChange(tx, accountCredentialsMap);
209
+
210
+ const subOperations = inferSubOperations(tx.hash, subAccounts);
211
+ const memo = getMemoFromTx(tx);
212
+ const extra: CardanoOperationExtra = {};
213
+ if (memo) {
214
+ extra.memo = memo;
215
+ }
216
+
217
+ let operationValue = accountChange.ada;
218
+
219
+ if (tx.certificate.stakeRegistrations.length) {
220
+ const walletRegistration = tx.certificate.stakeRegistrations.find(
221
+ c =>
222
+ c.stakeCredential.type === HashType.ADDRESS &&
223
+ c.stakeCredential.key === stakeCredential.key,
224
+ );
225
+ if (walletRegistration) {
226
+ extra.deposit = formatCurrencyUnit(
227
+ accountShapeInfo.currency.units[0],
228
+ new BigNumber(protocolParams.stakeKeyDeposit),
229
+ {
230
+ showCode: true,
231
+ disableRounding: true,
232
+ },
233
+ );
234
+ }
235
+ }
236
+
237
+ if (tx.certificate.stakeDeRegistrations.length) {
238
+ const walletDeRegistration = tx.certificate.stakeDeRegistrations.find(
239
+ c =>
240
+ c.stakeCredential.type === HashType.ADDRESS &&
241
+ c.stakeCredential.key === stakeCredential.key,
242
+ );
243
+ if (walletDeRegistration) {
244
+ operationValue = operationValue.minus(protocolParams.stakeKeyDeposit);
245
+ extra.refund = formatCurrencyUnit(
246
+ accountShapeInfo.currency.units[0],
247
+ new BigNumber(protocolParams.stakeKeyDeposit),
248
+ {
249
+ showCode: true,
250
+ disableRounding: true,
251
+ },
252
+ );
253
+ }
254
+ }
255
+
256
+ if (tx.withdrawals && tx.withdrawals.length) {
257
+ const walletWithdraw = tx.withdrawals.find(
258
+ w =>
259
+ w.stakeCredential.type === HashType.ADDRESS &&
260
+ w.stakeCredential.key === stakeCredential.key,
261
+ );
262
+ if (walletWithdraw) {
263
+ operationValue = operationValue.minus(walletWithdraw.amount);
264
+ extra.rewards = formatCurrencyUnit(
265
+ accountShapeInfo.currency.units[0],
266
+ new BigNumber(walletWithdraw.amount),
267
+ {
268
+ showCode: true,
269
+ disableRounding: true,
270
+ },
271
+ );
272
+ }
273
+ }
274
+
275
+ const mainOperationType: OperationType = tx.certificate.stakeDelegations.length
276
+ ? "DELEGATE"
277
+ : tx.certificate.stakeDeRegistrations.length
278
+ ? "UNDELEGATE"
279
+ : getOperationType({
280
+ valueChange: operationValue,
281
+ fees: new BigNumber(tx.fees),
282
+ });
283
+
284
+ return {
285
+ accountId,
286
+ id: encodeOperationId(accountId, tx.hash, mainOperationType),
287
+ hash: tx.hash,
288
+ type: mainOperationType,
289
+ fee: new BigNumber(tx.fees),
290
+ value: operationValue.absoluteValue(),
291
+ senders: tx.inputs.map(i =>
292
+ isHexString(i.address) ? TyphonUtils.getAddressFromHex(i.address).getBech32() : i.address,
293
+ ),
294
+ recipients: tx.outputs.map(o =>
295
+ isHexString(o.address) ? TyphonUtils.getAddressFromHex(o.address).getBech32() : o.address,
296
+ ),
297
+ subOperations,
298
+ blockHeight: tx.blockHeight,
299
+ date: new Date(tx.timestamp),
300
+ extra: extra,
301
+ blockHash: undefined,
302
+ };
303
+ }
304
+
305
+ function prepareUtxos(
306
+ newTransactions: Array<APITransaction>,
307
+ stableUtxos: Array<CardanoOutput>,
308
+ accountCredentialsMap: Record<string, PaymentCredential>,
309
+ ): Array<CardanoOutput> {
310
+ const newUtxos: Array<CardanoOutput> = [];
311
+ // spentUtxoKey = txId#index
312
+ const spentUtxoKeys: Set<string> = new Set();
313
+
314
+ newTransactions.forEach(t => {
315
+ t.inputs.forEach(i => {
316
+ const cred = accountCredentialsMap[i.paymentKey];
317
+ if (cred) spentUtxoKeys.add(`${i.txId}#${i.index}`);
318
+ });
319
+
320
+ t.outputs.forEach((o, outputIndex) => {
321
+ const cred = accountCredentialsMap[o.paymentKey];
322
+ if (cred) {
323
+ newUtxos.push({
324
+ hash: t.hash,
325
+ index: outputIndex,
326
+ address: o.address,
327
+ amount: new BigNumber(o.value),
328
+ tokens: o.tokens.map(token => ({
329
+ assetName: token.assetName,
330
+ policyId: token.policyId,
331
+ amount: new BigNumber(token.value),
332
+ })),
333
+ paymentCredential: {
334
+ key: o.paymentKey,
335
+ path: cred.path,
336
+ },
337
+ });
338
+ }
339
+ });
340
+ });
341
+
342
+ const utxos = uniqBy([...stableUtxos, ...newUtxos], u => `${u.hash}#${u.index}`).filter(
343
+ u => !spentUtxoKeys.has(`${u.hash}#${u.index}`),
344
+ );
345
+
346
+ return utxos;
347
+ }
@@ -0,0 +1,137 @@
1
+ import BigNumber from "bignumber.js";
2
+ import type { SignerContext } from "@ledgerhq/coin-framework/signer";
3
+ import { AccountShapeInfo, GetAccountShape } from "@ledgerhq/coin-framework/bridge/jsHelpers";
4
+ import { BipPath, CardanoAccount, CardanoDelegation, PaymentCredential } from "./types";
5
+ import { getDelegationInfo } from "./api/getDelegationInfo";
6
+ import { makeGetAccountShape } from "./synchronisation";
7
+ import { getTransactions } from "./api/getTransactions";
8
+ import { buildSubAccounts } from "./buildSubAccounts";
9
+ import { getNetworkInfo } from "./api/getNetworkInfo";
10
+ import { APINetworkInfo } from "./api/api-types";
11
+ import { CardanoSigner } from "./signer";
12
+
13
+ jest.mock("./buildSubAccounts");
14
+ jest.mock("./api/getTransactions");
15
+ jest.mock("./api/getNetworkInfo");
16
+ jest.mock("./api/getDelegationInfo");
17
+
18
+ describe("makeGetAccountShape", () => {
19
+ let signerContext: SignerContext<CardanoSigner>;
20
+ let shape: GetAccountShape<CardanoAccount>;
21
+ let accountShapeInfo: AccountShapeInfo<CardanoAccount>;
22
+ let getTransactionsMock: jest.MaybeMockedDeep<typeof getTransactions>;
23
+
24
+ beforeEach(() => {
25
+ const pubKeyMock = {
26
+ chainCodeHex: "chainCodeHex",
27
+ publicKeyHex: "publicKeyHex",
28
+ };
29
+ const fakeSigner: CardanoSigner = {
30
+ getAddress: jest.fn(),
31
+ sign: jest.fn(),
32
+ getPublicKey: jest.fn().mockResolvedValue(pubKeyMock),
33
+ };
34
+ signerContext = <T>(_: string, fn: (signer: CardanoSigner) => Promise<T>) => fn(fakeSigner);
35
+ shape = makeGetAccountShape(signerContext);
36
+ accountShapeInfo = {
37
+ currency: { id: "cardano" } as any,
38
+ address: "add",
39
+ index: 0,
40
+ initialAccount: {
41
+ cardanoResources: {
42
+ utxos: [
43
+ {
44
+ hash: "hash",
45
+ index: 0,
46
+ address: "addr",
47
+ amount: new BigNumber(1),
48
+ tokens: [],
49
+ paymentCredential: {
50
+ key: "key",
51
+ path: {
52
+ purpose: 1852,
53
+ coin: 1815,
54
+ account: 0,
55
+ chain: 0,
56
+ index: 0,
57
+ },
58
+ },
59
+ },
60
+ ],
61
+ },
62
+ } as unknown as CardanoAccount,
63
+ derivationPath: "",
64
+ derivationMode: "cardano",
65
+ deviceId: "id",
66
+ };
67
+ const buildSubAccountsMock = jest.mocked(buildSubAccounts);
68
+ buildSubAccountsMock.mockReturnValue([]);
69
+ getTransactionsMock = jest.mocked(getTransactions);
70
+ const getNetworkInfoMock = jest.mocked(getNetworkInfo);
71
+ getNetworkInfoMock.mockReturnValue(
72
+ Promise.resolve({ protocolParams: { lovelacePerUtxoWord: "1" } } as APINetworkInfo),
73
+ );
74
+ });
75
+
76
+ afterEach(() => {
77
+ jest.resetAllMocks();
78
+ });
79
+
80
+ describe("balance", () => {
81
+ it("should return 0 balance when there is no utxos", async () => {
82
+ getTransactionsMock.mockReturnValue(
83
+ Promise.resolve({
84
+ transactions: [],
85
+ externalCredentials: [
86
+ { path: { index: 0 } as BipPath, networkId: "id", isUsed: false, key: "" },
87
+ ],
88
+ internalCredentials: [],
89
+ blockHeight: 0,
90
+ }),
91
+ );
92
+ const result = await shape(accountShapeInfo, { paginationConfig: {} });
93
+ expect(result.balance).toEqual(new BigNumber(0));
94
+ });
95
+
96
+ it("should return the sum of utxos with delegation reward value", async () => {
97
+ getTransactionsMock.mockReturnValue(
98
+ Promise.resolve({
99
+ transactions: [],
100
+ externalCredentials: [
101
+ { path: { index: 0 } as BipPath, networkId: "id", isUsed: false, key: "" },
102
+ ],
103
+ internalCredentials: [
104
+ {
105
+ isUsed: true,
106
+ key: "cred",
107
+ } as PaymentCredential,
108
+ ],
109
+ blockHeight: 0,
110
+ }),
111
+ );
112
+ const getDelegationInfoMock = jest.mocked(getDelegationInfo);
113
+ getDelegationInfoMock.mockReturnValue(
114
+ Promise.resolve({ rewards: new BigNumber(42) } as CardanoDelegation),
115
+ );
116
+ const result = await shape(accountShapeInfo, { paginationConfig: {} });
117
+ expect(result.balance).toEqual(new BigNumber(42));
118
+ });
119
+ });
120
+
121
+ describe("spendableBalance", () => {
122
+ it("should return 0 spendable balance when there is no utxos", async () => {
123
+ const getTransactionsMock = jest.mocked(getTransactions);
124
+ getTransactionsMock.mockReturnValue(
125
+ Promise.resolve({
126
+ transactions: [],
127
+ externalCredentials: [{ path: "p", networkId: "id" }],
128
+ internalCredentials: [],
129
+ } as any),
130
+ );
131
+ const result = await shape(accountShapeInfo, { paginationConfig: {} });
132
+ expect(result.spendableBalance).toEqual(new BigNumber(0));
133
+ });
134
+
135
+ // it("should return the sum of utxos minus ada minimum for tokens", async () => {});
136
+ });
137
+ });
@@ -0,0 +1,66 @@
1
+ import type { Transaction, TransactionRaw } from "./types";
2
+ import { BigNumber } from "bignumber.js";
3
+ import { formatTransactionStatus } from "@ledgerhq/coin-framework/formatters";
4
+ import {
5
+ fromTransactionCommonRaw,
6
+ fromTransactionStatusRawCommon as fromTransactionStatusRaw,
7
+ toTransactionCommonRaw,
8
+ toTransactionStatusRawCommon as toTransactionStatusRaw,
9
+ } from "@ledgerhq/coin-framework/serialization";
10
+ import { getAccountCurrency } from "@ledgerhq/coin-framework/account/index";
11
+ import { formatCurrencyUnit } from "@ledgerhq/coin-framework/currencies/index";
12
+ import type { Account } from "@ledgerhq/types-live";
13
+
14
+ export const formatTransaction = (
15
+ { mode, amount, recipient, useAllAmount, subAccountId }: Transaction,
16
+ mainAccount: Account,
17
+ ): string => {
18
+ const account =
19
+ (subAccountId && (mainAccount.subAccounts || []).find(a => a.id === subAccountId)) ||
20
+ mainAccount;
21
+ return `
22
+ ${mode.toUpperCase()} ${
23
+ useAllAmount
24
+ ? "MAX"
25
+ : amount.isZero()
26
+ ? ""
27
+ : " " +
28
+ formatCurrencyUnit(getAccountCurrency(account).units[0], amount, {
29
+ showCode: true,
30
+ disableRounding: true,
31
+ })
32
+ }${recipient ? `\nTO ${recipient}` : ""}`;
33
+ };
34
+
35
+ export const fromTransactionRaw = (tr: TransactionRaw): Transaction => {
36
+ const common = fromTransactionCommonRaw(tr);
37
+ return {
38
+ ...common,
39
+ family: tr.family,
40
+ mode: tr.mode,
41
+ fees: tr.fees ? new BigNumber(tr.fees) : undefined,
42
+ memo: tr.memo,
43
+ poolId: tr.poolId,
44
+ };
45
+ };
46
+
47
+ export const toTransactionRaw = (t: Transaction): TransactionRaw => {
48
+ const common = toTransactionCommonRaw(t);
49
+ return {
50
+ ...common,
51
+ family: t.family,
52
+ mode: t.mode,
53
+ fees: t.fees?.toString() || undefined,
54
+ memo: t.memo,
55
+ poolId: t.poolId,
56
+ };
57
+ };
58
+
59
+ export default {
60
+ formatTransaction,
61
+ fromTransactionRaw,
62
+ toTransactionRaw,
63
+ fromTransactionStatusRaw,
64
+ toTransactionStatusRaw,
65
+ formatTransactionStatus,
66
+ };