@ledgerhq/live-common 22.3.0-universal.0 → 23.0.0-next.1

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 (269) hide show
  1. package/lib/__tests__/test-helpers/setup.js +2 -0
  2. package/lib/__tests__/test-helpers/setup.js.map +1 -1
  3. package/lib/account/pending.d.ts +2 -1
  4. package/lib/account/pending.d.ts.map +1 -1
  5. package/lib/account/serialization.d.ts +3 -1
  6. package/lib/account/serialization.d.ts.map +1 -1
  7. package/lib/account/serialization.js +12 -3
  8. package/lib/account/serialization.js.map +1 -1
  9. package/lib/cross.d.ts.map +1 -1
  10. package/lib/cross.js +2 -1
  11. package/lib/cross.js.map +1 -1
  12. package/lib/derivation.d.ts +10 -1
  13. package/lib/derivation.d.ts.map +1 -1
  14. package/lib/derivation.js +28 -5
  15. package/lib/derivation.js.map +1 -1
  16. package/lib/env.d.ts +20 -5
  17. package/lib/env.d.ts.map +1 -1
  18. package/lib/env.js +16 -1
  19. package/lib/env.js.map +1 -1
  20. package/lib/env.react.d.ts +1 -1
  21. package/lib/env.react.d.ts.map +1 -1
  22. package/lib/errors.d.ts +1 -0
  23. package/lib/errors.d.ts.map +1 -1
  24. package/lib/errors.js +1 -0
  25. package/lib/errors.js.map +1 -1
  26. package/lib/families/algorand/js-synchronization.d.ts +2 -2
  27. package/lib/families/bitcoin/exchange.d.ts +1 -1
  28. package/lib/families/bitcoin/js-synchronisation.d.ts +2 -2
  29. package/lib/families/cardano/api/api-types.d.ts +38 -0
  30. package/lib/families/cardano/api/api-types.d.ts.map +1 -0
  31. package/lib/families/cardano/api/api-types.js +3 -0
  32. package/lib/families/cardano/api/api-types.js.map +1 -0
  33. package/lib/families/cardano/api/getNetworkInfo.d.ts +4 -0
  34. package/lib/families/cardano/api/getNetworkInfo.d.ts.map +1 -0
  35. package/lib/families/cardano/api/getNetworkInfo.js +83 -0
  36. package/lib/families/cardano/api/getNetworkInfo.js.map +1 -0
  37. package/lib/families/cardano/api/getTransactions.d.ts +10 -0
  38. package/lib/families/cardano/api/getTransactions.d.ts.map +1 -0
  39. package/lib/families/cardano/api/getTransactions.js +257 -0
  40. package/lib/families/cardano/api/getTransactions.js.map +1 -0
  41. package/lib/families/cardano/api/submitTransaction.d.ts +7 -0
  42. package/lib/families/cardano/api/submitTransaction.d.ts.map +1 -0
  43. package/lib/families/cardano/api/submitTransaction.js +69 -0
  44. package/lib/families/cardano/api/submitTransaction.js.map +1 -0
  45. package/lib/families/cardano/bridge/js.d.ts +8 -0
  46. package/lib/families/cardano/bridge/js.d.ts.map +1 -0
  47. package/lib/families/cardano/bridge/js.js +69 -0
  48. package/lib/families/cardano/bridge/js.js.map +1 -0
  49. package/lib/families/cardano/bridge.test.d.ts +2 -0
  50. package/lib/families/cardano/bridge.test.d.ts.map +1 -0
  51. package/lib/families/cardano/bridge.test.js +11 -0
  52. package/lib/families/cardano/bridge.test.js.map +1 -0
  53. package/lib/families/cardano/buildSubAccounts.d.ts +25 -0
  54. package/lib/families/cardano/buildSubAccounts.d.ts.map +1 -0
  55. package/lib/families/cardano/buildSubAccounts.js +176 -0
  56. package/lib/families/cardano/buildSubAccounts.js.map +1 -0
  57. package/lib/families/cardano/cli-transaction.d.ts +19 -0
  58. package/lib/families/cardano/cli-transaction.d.ts.map +1 -0
  59. package/lib/families/cardano/cli-transaction.js +70 -0
  60. package/lib/families/cardano/cli-transaction.js.map +1 -0
  61. package/lib/families/cardano/constants.d.ts +8 -0
  62. package/lib/families/cardano/constants.d.ts.map +1 -0
  63. package/lib/families/cardano/constants.js +12 -0
  64. package/lib/families/cardano/constants.js.map +1 -0
  65. package/lib/families/cardano/datasets/rawAccount.1.d.ts +3 -0
  66. package/lib/families/cardano/datasets/rawAccount.1.d.ts.map +1 -0
  67. package/lib/families/cardano/datasets/rawAccount.1.js +27 -0
  68. package/lib/families/cardano/datasets/rawAccount.1.js.map +1 -0
  69. package/lib/families/cardano/datasets/scanAccounts.d.ts +5 -0
  70. package/lib/families/cardano/datasets/scanAccounts.d.ts.map +1 -0
  71. package/lib/families/cardano/datasets/scanAccounts.js +10 -0
  72. package/lib/families/cardano/datasets/scanAccounts.js.map +1 -0
  73. package/lib/families/cardano/errors.d.ts +7 -0
  74. package/lib/families/cardano/errors.d.ts.map +1 -0
  75. package/lib/families/cardano/errors.js +9 -0
  76. package/lib/families/cardano/errors.js.map +1 -0
  77. package/lib/families/cardano/hw-getAddress.d.ts +4 -0
  78. package/lib/families/cardano/hw-getAddress.d.ts.map +1 -0
  79. package/lib/families/cardano/hw-getAddress.js +126 -0
  80. package/lib/families/cardano/hw-getAddress.js.map +1 -0
  81. package/lib/families/cardano/js-broadcast.d.ts +8 -0
  82. package/lib/families/cardano/js-broadcast.d.ts.map +1 -0
  83. package/lib/families/cardano/js-broadcast.js +65 -0
  84. package/lib/families/cardano/js-broadcast.js.map +1 -0
  85. package/lib/families/cardano/js-buildTransaction.d.ts +12 -0
  86. package/lib/families/cardano/js-buildTransaction.d.ts.map +1 -0
  87. package/lib/families/cardano/js-buildTransaction.js +253 -0
  88. package/lib/families/cardano/js-buildTransaction.js.map +1 -0
  89. package/lib/families/cardano/js-estimateMaxSpendable.d.ts +15 -0
  90. package/lib/families/cardano/js-estimateMaxSpendable.d.ts.map +1 -0
  91. package/lib/families/cardano/js-estimateMaxSpendable.js +99 -0
  92. package/lib/families/cardano/js-estimateMaxSpendable.js.map +1 -0
  93. package/lib/families/cardano/js-getFeesForTransaction.d.ts +15 -0
  94. package/lib/families/cardano/js-getFeesForTransaction.d.ts.map +1 -0
  95. package/lib/families/cardano/js-getFeesForTransaction.js +61 -0
  96. package/lib/families/cardano/js-getFeesForTransaction.js.map +1 -0
  97. package/lib/families/cardano/js-getTransactionStatus.d.ts +5 -0
  98. package/lib/families/cardano/js-getTransactionStatus.d.ts.map +1 -0
  99. package/lib/families/cardano/js-getTransactionStatus.js +156 -0
  100. package/lib/families/cardano/js-getTransactionStatus.js.map +1 -0
  101. package/lib/families/cardano/js-signOperation.d.ts +13 -0
  102. package/lib/families/cardano/js-signOperation.d.ts.map +1 -0
  103. package/lib/families/cardano/js-signOperation.js +274 -0
  104. package/lib/families/cardano/js-signOperation.js.map +1 -0
  105. package/lib/families/cardano/js-synchronisation.d.ts +11 -0
  106. package/lib/families/cardano/js-synchronisation.d.ts.map +1 -0
  107. package/lib/families/cardano/js-synchronisation.js +247 -0
  108. package/lib/families/cardano/js-synchronisation.js.map +1 -0
  109. package/lib/families/cardano/js-transaction.d.ts +23 -0
  110. package/lib/families/cardano/js-transaction.d.ts.map +1 -0
  111. package/lib/families/cardano/js-transaction.js +112 -0
  112. package/lib/families/cardano/js-transaction.js.map +1 -0
  113. package/lib/families/cardano/logic.d.ts +92 -0
  114. package/lib/families/cardano/logic.d.ts.map +1 -0
  115. package/lib/families/cardano/logic.js +378 -0
  116. package/lib/families/cardano/logic.js.map +1 -0
  117. package/lib/families/cardano/networks.d.ts +3 -0
  118. package/lib/families/cardano/networks.d.ts.map +1 -0
  119. package/lib/families/cardano/networks.js +34 -0
  120. package/lib/families/cardano/networks.js.map +1 -0
  121. package/lib/families/cardano/postSyncPatch.d.ts +7 -0
  122. package/lib/families/cardano/postSyncPatch.d.ts.map +1 -0
  123. package/lib/families/cardano/postSyncPatch.js +37 -0
  124. package/lib/families/cardano/postSyncPatch.js.map +1 -0
  125. package/lib/families/cardano/serialization.d.ts +4 -0
  126. package/lib/families/cardano/serialization.d.ts.map +1 -0
  127. package/lib/families/cardano/serialization.js +121 -0
  128. package/lib/families/cardano/serialization.js.map +1 -0
  129. package/lib/families/cardano/specs.d.ts +7 -0
  130. package/lib/families/cardano/specs.d.ts.map +1 -0
  131. package/lib/families/cardano/specs.js +83 -0
  132. package/lib/families/cardano/specs.js.map +1 -0
  133. package/lib/families/cardano/speculos-deviceActions.d.ts +7 -0
  134. package/lib/families/cardano/speculos-deviceActions.d.ts.map +1 -0
  135. package/lib/families/cardano/speculos-deviceActions.js +69 -0
  136. package/lib/families/cardano/speculos-deviceActions.js.map +1 -0
  137. package/lib/families/cardano/test-dataset.d.ts +5 -0
  138. package/lib/families/cardano/test-dataset.d.ts.map +1 -0
  139. package/lib/families/cardano/test-dataset.js +42 -0
  140. package/lib/families/cardano/test-dataset.js.map +1 -0
  141. package/lib/families/cardano/transaction.d.ts +12 -0
  142. package/lib/families/cardano/transaction.d.ts.map +1 -0
  143. package/lib/families/cardano/transaction.js +47 -0
  144. package/lib/families/cardano/transaction.js.map +1 -0
  145. package/lib/families/cardano/types.d.ts +152 -0
  146. package/lib/families/cardano/types.d.ts.map +1 -0
  147. package/lib/families/cardano/types.js +18 -0
  148. package/lib/families/cardano/types.js.map +1 -0
  149. package/lib/families/celo/js-synchronisation.d.ts +2 -2
  150. package/lib/families/cosmos/js-synchronisation.d.ts +2 -2
  151. package/lib/families/crypto_org/js-synchronisation.d.ts +2 -2
  152. package/lib/families/elrond/js-synchronisation.d.ts +2 -2
  153. package/lib/families/ethereum/hw-signMessage.d.ts.map +1 -1
  154. package/lib/families/ethereum/hw-signMessage.js +10 -4
  155. package/lib/families/ethereum/hw-signMessage.js.map +1 -1
  156. package/lib/families/filecoin/bridge/account.js +18 -14
  157. package/lib/families/filecoin/bridge/account.js.map +1 -1
  158. package/lib/families/filecoin/bridge/utils/types.d.ts +1 -1
  159. package/lib/families/filecoin/bridge/utils/types.d.ts.map +1 -1
  160. package/lib/families/filecoin/bridge/utils/utils.d.ts +1 -1
  161. package/lib/families/filecoin/bridge/utils/utils.d.ts.map +1 -1
  162. package/lib/families/filecoin/bridge/utils/utils.js +9 -8
  163. package/lib/families/filecoin/bridge/utils/utils.js.map +1 -1
  164. package/lib/families/filecoin/bridge.test.d.ts +2 -0
  165. package/lib/families/filecoin/bridge.test.d.ts.map +1 -0
  166. package/lib/families/filecoin/bridge.test.js +92 -0
  167. package/lib/families/filecoin/bridge.test.js.map +1 -0
  168. package/lib/families/filecoin/specs.d.ts.map +1 -1
  169. package/lib/families/filecoin/specs.js +1 -2
  170. package/lib/families/filecoin/specs.js.map +1 -1
  171. package/lib/families/filecoin/test-dataset.d.ts.map +1 -1
  172. package/lib/families/filecoin/test-dataset.js +8 -9
  173. package/lib/families/filecoin/test-dataset.js.map +1 -1
  174. package/lib/families/hedera/js-synchronisation.d.ts +2 -2
  175. package/lib/families/polkadot/js-synchronisation.d.ts +2 -2
  176. package/lib/families/ripple/js-synchronization.d.ts +2 -2
  177. package/lib/families/stellar/js-synchronization.d.ts +2 -2
  178. package/lib/generated/bridge/js.d.ts +4 -0
  179. package/lib/generated/bridge/js.d.ts.map +1 -1
  180. package/lib/generated/bridge/js.js +30 -28
  181. package/lib/generated/bridge/js.js.map +1 -1
  182. package/lib/generated/cli-transaction.d.ts +15 -0
  183. package/lib/generated/cli-transaction.d.ts.map +1 -1
  184. package/lib/generated/cli-transaction.js +28 -26
  185. package/lib/generated/cli-transaction.js.map +1 -1
  186. package/lib/generated/exchange.d.ts +1 -1
  187. package/lib/generated/hw-getAddress.d.ts +1 -0
  188. package/lib/generated/hw-getAddress.d.ts.map +1 -1
  189. package/lib/generated/hw-getAddress.js +30 -28
  190. package/lib/generated/hw-getAddress.js.map +1 -1
  191. package/lib/generated/specs.d.ts +3 -0
  192. package/lib/generated/specs.d.ts.map +1 -1
  193. package/lib/generated/specs.js +28 -26
  194. package/lib/generated/specs.js.map +1 -1
  195. package/lib/generated/speculos-deviceActions.d.ts +3 -0
  196. package/lib/generated/speculos-deviceActions.d.ts.map +1 -1
  197. package/lib/generated/speculos-deviceActions.js +28 -26
  198. package/lib/generated/speculos-deviceActions.js.map +1 -1
  199. package/lib/generated/test-dataset.d.ts +1 -0
  200. package/lib/generated/test-dataset.d.ts.map +1 -1
  201. package/lib/generated/test-dataset.js +26 -24
  202. package/lib/generated/test-dataset.js.map +1 -1
  203. package/lib/generated/transaction.d.ts +5 -0
  204. package/lib/generated/transaction.d.ts.map +1 -1
  205. package/lib/generated/transaction.js +30 -28
  206. package/lib/generated/transaction.js.map +1 -1
  207. package/lib/generated/types.d.ts +4 -2
  208. package/lib/generated/types.d.ts.map +1 -1
  209. package/lib/reconciliation.d.ts.map +1 -1
  210. package/lib/reconciliation.js +6 -0
  211. package/lib/reconciliation.js.map +1 -1
  212. package/lib/types/account.d.ts +3 -0
  213. package/lib/types/account.d.ts.map +1 -1
  214. package/package.json +5 -3
  215. package/src/__tests__/test-helpers/setup.ts +2 -0
  216. package/src/account/serialization.ts +15 -0
  217. package/src/cross.ts +2 -1
  218. package/src/derivation.ts +19 -2
  219. package/src/env.ts +16 -1
  220. package/src/errors.ts +1 -0
  221. package/src/families/cardano/__snapshots__/bridge.test.ts.snap +1667 -0
  222. package/src/families/cardano/api/api-types.ts +33 -0
  223. package/src/families/cardano/api/getNetworkInfo.ts +38 -0
  224. package/src/families/cardano/api/getTransactions.ts +240 -0
  225. package/src/families/cardano/api/submitTransaction.ts +27 -0
  226. package/src/families/cardano/bridge/js.ts +35 -0
  227. package/src/families/cardano/bridge.test.ts +6 -0
  228. package/src/families/cardano/buildSubAccounts.ts +182 -0
  229. package/src/families/cardano/cli-transaction.ts +78 -0
  230. package/src/families/cardano/constants.ts +12 -0
  231. package/src/families/cardano/datasets/rawAccount.1.ts +28 -0
  232. package/src/families/cardano/datasets/scanAccounts.ts +23 -0
  233. package/src/families/cardano/errors.ts +8 -0
  234. package/src/families/cardano/hw-getAddress.ts +61 -0
  235. package/src/families/cardano/js-broadcast.ts +25 -0
  236. package/src/families/cardano/js-buildTransaction.ts +293 -0
  237. package/src/families/cardano/js-estimateMaxSpendable.ts +60 -0
  238. package/src/families/cardano/js-getFeesForTransaction.ts +25 -0
  239. package/src/families/cardano/js-getTransactionStatus.ts +123 -0
  240. package/src/families/cardano/js-signOperation.ts +265 -0
  241. package/src/families/cardano/js-synchronisation.ts +255 -0
  242. package/src/families/cardano/js-transaction.ts +69 -0
  243. package/src/families/cardano/logic.ts +454 -0
  244. package/src/families/cardano/networks.ts +33 -0
  245. package/src/families/cardano/postSyncPatch.ts +45 -0
  246. package/src/families/cardano/serialization.ts +179 -0
  247. package/src/families/cardano/specs.ts +96 -0
  248. package/src/families/cardano/speculos-deviceActions.ts +66 -0
  249. package/src/families/cardano/test-dataset.ts +41 -0
  250. package/src/families/cardano/transaction.ts +55 -0
  251. package/src/families/cardano/types.ts +178 -0
  252. package/src/families/ethereum/hw-signMessage.ts +9 -5
  253. package/src/families/filecoin/__snapshots__/bridge.test.ts.snap +87 -0
  254. package/src/families/filecoin/bridge/account.ts +20 -20
  255. package/src/families/filecoin/bridge/utils/types.ts +1 -1
  256. package/src/families/filecoin/bridge/utils/utils.ts +8 -7
  257. package/src/families/filecoin/bridge.test.ts +39 -0
  258. package/src/families/filecoin/specs.ts +1 -2
  259. package/src/families/filecoin/test-dataset.ts +19 -15
  260. package/src/generated/bridge/js.ts +3 -0
  261. package/src/generated/cli-transaction.ts +3 -0
  262. package/src/generated/hw-getAddress.ts +3 -0
  263. package/src/generated/specs.ts +3 -0
  264. package/src/generated/speculos-deviceActions.ts +3 -0
  265. package/src/generated/test-dataset.ts +3 -0
  266. package/src/generated/transaction.ts +3 -0
  267. package/src/generated/types.ts +4 -0
  268. package/src/reconciliation.ts +10 -0
  269. package/src/types/account.ts +6 -0
@@ -0,0 +1,454 @@
1
+ import {
2
+ CARDANO_COIN_TYPE,
3
+ CARDANO_PURPOSE,
4
+ MEMO_LABEL,
5
+ STAKING_ADDRESS_INDEX,
6
+ TTL_GAP,
7
+ } from "./constants";
8
+
9
+ import {
10
+ utils as TyphonUtils,
11
+ types as TyphonTypes,
12
+ address as TyphonAddress,
13
+ } from "@stricahq/typhonjs";
14
+ import {
15
+ AddressType,
16
+ AssetGroup,
17
+ TxInput,
18
+ TxOutput,
19
+ TxOutputDestination,
20
+ TxOutputDestinationType,
21
+ } from "@cardano-foundation/ledgerjs-hw-app-cardano";
22
+ import { str_to_path } from "@cardano-foundation/ledgerjs-hw-app-cardano/dist/utils";
23
+ import {
24
+ BipPath,
25
+ PaymentChain,
26
+ PaymentCredential,
27
+ StakeChain,
28
+ StakeCredential,
29
+ Token,
30
+ } from "./types";
31
+ import { Bip32PublicKey } from "@stricahq/bip32ed25519";
32
+ import bs58 from "bs58";
33
+ import BigNumber from "bignumber.js";
34
+ import { getNetworkParameters } from "./networks";
35
+ import { CryptoCurrency, OperationType } from "../../types";
36
+ import groupBy from "lodash/groupBy";
37
+ import { APITransaction } from "./api/api-types";
38
+ import { getCryptoCurrencyById } from "@ledgerhq/cryptoassets";
39
+
40
+ /**
41
+ * returns BipPath object with account, chain and index field for cardano
42
+ *
43
+ * @param {string} path
44
+ */
45
+ export function getBipPathFromString(path: string): BipPath {
46
+ const regEx = new RegExp(
47
+ `^${CARDANO_PURPOSE}'/${CARDANO_COIN_TYPE}'/(\\d*)'/([012])/(\\d*)`
48
+ );
49
+ const result = path.match(regEx);
50
+ if (result == null) {
51
+ throw new Error("Invalid derivation path");
52
+ }
53
+ return getBipPath({
54
+ account: parseInt(result[1]),
55
+ chain: parseInt(result[2]),
56
+ index: parseInt(result[3]),
57
+ });
58
+ }
59
+
60
+ /**
61
+ * returns complete bipPath with purpose, coin, account, chain and index for cardano
62
+ */
63
+ export function getBipPath({
64
+ account,
65
+ chain,
66
+ index,
67
+ }: {
68
+ account: number;
69
+ chain: PaymentChain | StakeChain;
70
+ index: number;
71
+ }): BipPath {
72
+ return {
73
+ purpose: CARDANO_PURPOSE,
74
+ coin: CARDANO_COIN_TYPE,
75
+ account,
76
+ chain,
77
+ index,
78
+ };
79
+ }
80
+
81
+ /**
82
+ * returns bipPathString from account, chain and index for cardano
83
+ */
84
+ export function getBipPathString({
85
+ account,
86
+ chain,
87
+ index,
88
+ }: {
89
+ account: number;
90
+ chain: number;
91
+ index: number;
92
+ }): string {
93
+ return `${CARDANO_PURPOSE}'/${CARDANO_COIN_TYPE}'/${account}'/${chain}/${index}`;
94
+ }
95
+
96
+ export function getExtendedPublicKeyFromHex(keyHex: string): Bip32PublicKey {
97
+ return Bip32PublicKey.fromBytes(Buffer.from(keyHex, "hex"));
98
+ }
99
+
100
+ export function getCredentialKey(
101
+ accountKey: Bip32PublicKey,
102
+ path: BipPath
103
+ ): { key: string; path: BipPath } {
104
+ const keyBytes = accountKey
105
+ .derive(path.chain)
106
+ .derive(path.index)
107
+ .toPublicKey()
108
+ .hash();
109
+ const pubKeyHex = keyBytes.toString("hex");
110
+ return {
111
+ key: pubKeyHex,
112
+ path,
113
+ };
114
+ }
115
+
116
+ /**
117
+ * returns cardano base address by paymentKey and stakeKey
118
+ */
119
+ export function getBaseAddress({
120
+ networkId,
121
+ paymentCred,
122
+ stakeCred,
123
+ }: {
124
+ networkId: number;
125
+ paymentCred: PaymentCredential;
126
+ stakeCred: StakeCredential;
127
+ }): TyphonAddress.BaseAddress {
128
+ const paymentCredential: TyphonTypes.HashCredential = {
129
+ hash: paymentCred.key,
130
+ type: TyphonTypes.HashType.ADDRESS,
131
+ bipPath: paymentCred.path,
132
+ };
133
+
134
+ const stakeCredential: TyphonTypes.HashCredential = {
135
+ hash: stakeCred.key,
136
+ type: TyphonTypes.HashType.ADDRESS,
137
+ bipPath: stakeCred.path,
138
+ };
139
+ return new TyphonAddress.BaseAddress(
140
+ networkId,
141
+ paymentCredential,
142
+ stakeCredential
143
+ );
144
+ }
145
+
146
+ /**
147
+ * Returns true if address is a valid
148
+ *
149
+ * @param {string} address
150
+ */
151
+ export const isValidAddress = (address: string, networkId: number): boolean => {
152
+ if (!address) return false;
153
+
154
+ try {
155
+ // check if it is byron address
156
+ const addr = bs58.decode(address).toString("hex");
157
+ if (addr[0] !== "8") {
158
+ return false;
159
+ }
160
+ } catch (error) {
161
+ try {
162
+ const hexAddress = TyphonUtils.decodeBech32(address);
163
+ const addressNetworkId = Number(hexAddress.value.toLowerCase().charAt(1));
164
+ if (addressNetworkId !== networkId) {
165
+ return false;
166
+ }
167
+ } catch (error) {
168
+ return false;
169
+ }
170
+ }
171
+ return true;
172
+ };
173
+
174
+ export const getAbsoluteSlot = function (
175
+ networkName: string,
176
+ time: Date
177
+ ): number {
178
+ const networkParams = getNetworkParameters(networkName);
179
+ const byronChainEndSlots =
180
+ networkParams.shelleyStartEpoch * networkParams.byronSlotsPerEpoch;
181
+ const byronChainEndTime =
182
+ byronChainEndSlots * networkParams.byronSlotDuration;
183
+
184
+ const shelleyChainTime =
185
+ time.getTime() - networkParams.chainStartTime - byronChainEndTime;
186
+ const shelleyChainSlots = Math.floor(
187
+ shelleyChainTime / networkParams.shelleySlotDuration
188
+ );
189
+ return byronChainEndSlots + shelleyChainSlots;
190
+ };
191
+
192
+ /**
193
+ * Returns the time to live for transaction
194
+ *
195
+ * @returns {number}
196
+ */
197
+ export function getTTL(networkName: string): number {
198
+ return getAbsoluteSlot(networkName, new Date()) + TTL_GAP;
199
+ }
200
+
201
+ export function getEpoch(networkName: string, time: Date): number {
202
+ const networkParams = getNetworkParameters(networkName);
203
+ const chainTime = time.getTime() - networkParams.chainStartTime;
204
+ const epoch = Math.floor(
205
+ chainTime /
206
+ (networkParams.shelleySlotsPerEpoch * networkParams.shelleySlotDuration)
207
+ );
208
+ return epoch;
209
+ }
210
+
211
+ export function mergeTokens(
212
+ tokens: Array<TyphonTypes.Token>
213
+ ): Array<TyphonTypes.Token> {
214
+ return Object.values(
215
+ groupBy(tokens, (t) => `${t.policyId}${t.assetName}`)
216
+ ).map((similarTokens) => ({
217
+ policyId: similarTokens[0].policyId,
218
+ assetName: similarTokens[0].assetName,
219
+ amount: similarTokens.reduce(
220
+ (total, token) => total.plus(token.amount),
221
+ new BigNumber(0)
222
+ ),
223
+ }));
224
+ }
225
+
226
+ /**
227
+ * @param { Array<TyphonTypes.Token> } b
228
+ * @param { Array<TyphonTypes.Token> } a
229
+ * @returns a - b
230
+ */
231
+ export function getTokenDiff(
232
+ a: Array<TyphonTypes.Token>,
233
+ b: Array<TyphonTypes.Token>
234
+ ): Array<TyphonTypes.Token> {
235
+ return mergeTokens(
236
+ a.concat(b.map((t) => ({ ...t, amount: t.amount.negated() })))
237
+ ).filter((t) => !t.amount.eq(0));
238
+ }
239
+
240
+ /**
241
+ * returns the formatted transactionOutput for ledger cardano app
242
+ *
243
+ * @param output
244
+ * @param accountIndex
245
+ * @returns {TxOutput}
246
+ */
247
+ export function prepareLedgerOutput(
248
+ output: TyphonTypes.Output,
249
+ accountIndex: number
250
+ ): TxOutput {
251
+ const isByronAddress = output.address instanceof TyphonAddress.ByronAddress;
252
+ let isDeviceOwnedAddress = false;
253
+ let destination: TxOutputDestination;
254
+
255
+ if (!isByronAddress) {
256
+ const address = output.address as TyphonTypes.ShelleyAddress;
257
+ isDeviceOwnedAddress =
258
+ address.paymentCredential &&
259
+ address.paymentCredential.type === TyphonTypes.HashType.ADDRESS &&
260
+ address.paymentCredential.bipPath !== undefined;
261
+ }
262
+
263
+ if (isDeviceOwnedAddress) {
264
+ const address = output.address as TyphonAddress.BaseAddress;
265
+
266
+ const paymentKeyPath = (
267
+ address.paymentCredential as TyphonTypes.HashCredential
268
+ ).bipPath as TyphonTypes.BipPath;
269
+ const stakingKeyPath = (
270
+ address.stakeCredential as TyphonTypes.HashCredential
271
+ ).bipPath as TyphonTypes.BipPath;
272
+
273
+ const paymentKeyPathString = getBipPathString({
274
+ account: accountIndex,
275
+ chain: paymentKeyPath.chain,
276
+ index: paymentKeyPath.index,
277
+ });
278
+ const stakingKeyPathString = getBipPathString({
279
+ account: accountIndex,
280
+ chain: stakingKeyPath.chain,
281
+ index: stakingKeyPath.index,
282
+ });
283
+
284
+ destination = {
285
+ type: TxOutputDestinationType.DEVICE_OWNED,
286
+ params: {
287
+ type: AddressType.BASE_PAYMENT_KEY_STAKE_KEY,
288
+ params: {
289
+ spendingPath: str_to_path(paymentKeyPathString),
290
+ stakingPath: str_to_path(stakingKeyPathString),
291
+ },
292
+ },
293
+ };
294
+ } else {
295
+ const address = output.address;
296
+ destination = {
297
+ type: TxOutputDestinationType.THIRD_PARTY,
298
+ params: {
299
+ addressHex: address.getHex(),
300
+ },
301
+ };
302
+ }
303
+
304
+ const tokenBundle: Array<AssetGroup> = Object.values(
305
+ groupBy(output.tokens, ({ policyId }) => policyId)
306
+ ).map((tokens) => ({
307
+ policyIdHex: tokens[0].policyId,
308
+ tokens: tokens.map((token) => ({
309
+ assetNameHex: token.assetName,
310
+ amount: token.amount.toString(),
311
+ })),
312
+ }));
313
+
314
+ return {
315
+ amount: output.amount.toString(),
316
+ destination,
317
+ tokenBundle,
318
+ };
319
+ }
320
+
321
+ /**
322
+ * returns the formatted transactionInput for ledger cardano app
323
+ *
324
+ * @param {TyphonTypes.Input} input
325
+ * @param {number} accountIndex
326
+ * @returns {TxInput}
327
+ */
328
+ export function prepareLedgerInput(
329
+ input: TyphonTypes.Input,
330
+ accountIndex: number
331
+ ): TxInput {
332
+ const paymentKeyPath =
333
+ input.address.paymentCredential.type === TyphonTypes.HashType.ADDRESS
334
+ ? input.address.paymentCredential.bipPath
335
+ : undefined;
336
+ return {
337
+ txHashHex: input.txId,
338
+ outputIndex: input.index,
339
+ path: paymentKeyPath
340
+ ? str_to_path(
341
+ getBipPathString({
342
+ account: accountIndex,
343
+ chain: paymentKeyPath.chain,
344
+ index: paymentKeyPath.index,
345
+ })
346
+ )
347
+ : null,
348
+ };
349
+ }
350
+
351
+ export function getAccountStakeCredential(
352
+ xpub: string,
353
+ index: number
354
+ ): StakeCredential {
355
+ const accountXPubKey = getExtendedPublicKeyFromHex(xpub);
356
+ const keyPath = getCredentialKey(
357
+ accountXPubKey,
358
+ getBipPath({
359
+ account: index,
360
+ chain: StakeChain.stake,
361
+ index: STAKING_ADDRESS_INDEX,
362
+ })
363
+ );
364
+ return {
365
+ key: keyPath.key,
366
+ path: keyPath.path,
367
+ };
368
+ }
369
+
370
+ export function getOperationType({
371
+ valueChange,
372
+ fees,
373
+ }: {
374
+ valueChange: BigNumber;
375
+ fees: BigNumber;
376
+ }): OperationType {
377
+ return valueChange.isNegative()
378
+ ? valueChange.absoluteValue().eq(fees)
379
+ ? "FEES"
380
+ : "OUT"
381
+ : valueChange.isPositive()
382
+ ? "IN"
383
+ : "NONE";
384
+ }
385
+
386
+ export function isTestnet(currency: CryptoCurrency): boolean {
387
+ return getCryptoCurrencyById(currency.id).isTestnetFor ? true : false;
388
+ }
389
+
390
+ export function getAccountChange(
391
+ t: APITransaction,
392
+ accountCredentialsMap: Record<string, PaymentCredential>
393
+ ): { ada: BigNumber; tokens: Array<Token> } {
394
+ let accountInputAda = new BigNumber(0);
395
+ const accountInputTokens: Array<Token> = [];
396
+ t.inputs.forEach((i) => {
397
+ if (accountCredentialsMap[i.paymentKey]) {
398
+ accountInputAda = accountInputAda.plus(i.value);
399
+ accountInputTokens.push(
400
+ ...i.tokens.map((t) => ({
401
+ assetName: t.assetName,
402
+ policyId: t.policyId,
403
+ amount: new BigNumber(t.value),
404
+ }))
405
+ );
406
+ }
407
+ });
408
+
409
+ let accountOutputAda = new BigNumber(0);
410
+ const accountOutputTokens: Array<Token> = [];
411
+ t.outputs.forEach((o) => {
412
+ if (accountCredentialsMap[o.paymentKey]) {
413
+ accountOutputAda = accountOutputAda.plus(o.value);
414
+ accountOutputTokens.push(
415
+ ...o.tokens.map((t) => ({
416
+ assetName: t.assetName,
417
+ policyId: t.policyId,
418
+ amount: new BigNumber(t.value),
419
+ }))
420
+ );
421
+ }
422
+ });
423
+
424
+ return {
425
+ ada: accountOutputAda.minus(accountInputAda),
426
+ tokens: getTokenDiff(accountOutputTokens, accountInputTokens),
427
+ };
428
+ }
429
+
430
+ export function getMemoFromTx(tx: APITransaction): string | undefined {
431
+ let memo;
432
+ const metadataValue = tx.metadata?.data.find(
433
+ (m) => m.label === MEMO_LABEL.toString()
434
+ );
435
+ if (metadataValue) {
436
+ try {
437
+ const parsedValue = JSON.parse(metadataValue.value);
438
+ if (
439
+ parsedValue.msg &&
440
+ Array.isArray(parsedValue.msg) &&
441
+ parsedValue.msg.length
442
+ ) {
443
+ memo = parsedValue.msg.join(", ");
444
+ }
445
+ // eslint-disable-next-line no-empty
446
+ } catch (e) {}
447
+ }
448
+ return memo;
449
+ }
450
+
451
+ export function isHexString(value: string): boolean {
452
+ const regExp = /^[0-9a-fA-F]+$/;
453
+ return regExp.test(value);
454
+ }
@@ -0,0 +1,33 @@
1
+ import type { CardanoLikeNetworkParameters } from "./types";
2
+
3
+ export const getNetworkParameters = (
4
+ networkName: string
5
+ ): CardanoLikeNetworkParameters => {
6
+ if (networkName === "cardano") {
7
+ return {
8
+ identifier: "cardano",
9
+ networkId: 1,
10
+ chainStartTime: 1506203091000,
11
+ byronSlotDuration: 20000,
12
+ byronSlotsPerEpoch: 21600,
13
+ shelleyStartEpoch: 208,
14
+ shelleySlotDuration: 1000,
15
+ shelleySlotsPerEpoch: 432000,
16
+ addressPrefix: "addr",
17
+ };
18
+ } else if (networkName === "cardano_testnet") {
19
+ return {
20
+ identifier: "cardano_testnet",
21
+ networkId: 0,
22
+ chainStartTime: 1563999616000,
23
+ byronSlotDuration: 20000,
24
+ byronSlotsPerEpoch: 21600,
25
+ shelleyStartEpoch: 74,
26
+ shelleySlotDuration: 1000,
27
+ shelleySlotsPerEpoch: 432000,
28
+ addressPrefix: "addr_test",
29
+ };
30
+ }
31
+
32
+ throw new Error("No network parameters set for " + networkName);
33
+ };
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Code reference taken from src/families/ethereum/postSyncPatch.ts
3
+ */
4
+
5
+ import type { AccountLike, Account, Operation } from "../../types";
6
+
7
+ // we need to preserve ADA pendingOperations because there is no mempool to do this
8
+ // we assume we need to preserve until:
9
+ // - that txid appears in operations
10
+ const postSyncPatchGen = <T extends AccountLike>(
11
+ initial: T,
12
+ synced: T,
13
+ parentPendingOperation?: Operation[]
14
+ ): T => {
15
+ const pendingOperations = initial.pendingOperations || [];
16
+ if (pendingOperations.length === 0) return synced;
17
+ const { operations } = synced;
18
+ synced.pendingOperations = pendingOperations.filter(
19
+ (op) =>
20
+ (!parentPendingOperation || // a child pending parent need to disappear if parent ada op disappear
21
+ parentPendingOperation.some((o) => o.hash === op.hash)) &&
22
+ !operations.some((o) => o.hash === op.hash) // after retain logic, we need operation to appear
23
+ );
24
+ return synced;
25
+ };
26
+
27
+ const postSyncPatch = (initial: Account, synced: Account): Account => {
28
+ const acc = postSyncPatchGen(initial, synced);
29
+ const { subAccounts, pendingOperations } = acc;
30
+ const initialSubAccounts = initial.subAccounts;
31
+
32
+ if (subAccounts && initialSubAccounts) {
33
+ acc.subAccounts = subAccounts.map((subAccount) => {
34
+ const initialSubAccount = initialSubAccounts.find(
35
+ (a) => a.id === subAccount.id
36
+ );
37
+ if (!initialSubAccount) return subAccount;
38
+ return postSyncPatchGen(initialSubAccount, subAccount, pendingOperations);
39
+ });
40
+ }
41
+
42
+ return acc;
43
+ };
44
+
45
+ export default postSyncPatch;
@@ -0,0 +1,179 @@
1
+ import { BigNumber } from "bignumber.js";
2
+ import type {
3
+ BipPath,
4
+ BipPathRaw,
5
+ CardanoOutput,
6
+ CardanoOutputRaw,
7
+ CardanoResources,
8
+ CardanoResourcesRaw,
9
+ PaymentCredential,
10
+ PaymentCredentialRaw,
11
+ ProtocolParams,
12
+ ProtocolParamsRaw,
13
+ Token,
14
+ TokenRaw,
15
+ } from "./types";
16
+
17
+ function toTokenRaw({ assetName, policyId, amount }: Token): TokenRaw {
18
+ return {
19
+ assetName,
20
+ policyId,
21
+ amount: amount.toString(),
22
+ };
23
+ }
24
+
25
+ function fromTokenRaw({ assetName, policyId, amount }: TokenRaw): Token {
26
+ return {
27
+ assetName,
28
+ policyId,
29
+ amount: new BigNumber(amount),
30
+ };
31
+ }
32
+
33
+ function toBipPathRaw({
34
+ purpose,
35
+ coin,
36
+ account,
37
+ chain,
38
+ index,
39
+ }: BipPath): BipPathRaw {
40
+ return {
41
+ purpose,
42
+ coin,
43
+ account,
44
+ chain,
45
+ index,
46
+ };
47
+ }
48
+
49
+ function fromBipPathRaw({
50
+ purpose,
51
+ coin,
52
+ account,
53
+ chain,
54
+ index,
55
+ }: BipPathRaw): BipPath {
56
+ return {
57
+ purpose,
58
+ coin,
59
+ account,
60
+ chain,
61
+ index,
62
+ };
63
+ }
64
+
65
+ function toPaymentCredentialRaw(r: PaymentCredential): PaymentCredentialRaw {
66
+ return {
67
+ isUsed: r.isUsed,
68
+ key: r.key,
69
+ path: toBipPathRaw(r.path),
70
+ };
71
+ }
72
+
73
+ function fromPaymentCredentialRaw(r: PaymentCredentialRaw): PaymentCredential {
74
+ return {
75
+ isUsed: r.isUsed,
76
+ key: r.key,
77
+ path: fromBipPathRaw(r.path),
78
+ };
79
+ }
80
+
81
+ function toCardanoOutputRaw({
82
+ hash,
83
+ index,
84
+ address,
85
+ amount,
86
+ tokens,
87
+ paymentCredential,
88
+ }: CardanoOutput): CardanoOutputRaw {
89
+ return {
90
+ hash,
91
+ index,
92
+ address,
93
+ amount: amount.toString(),
94
+ tokens: tokens.map(toTokenRaw),
95
+ paymentCredential,
96
+ };
97
+ }
98
+
99
+ function fromCardanoOutputRaw({
100
+ hash,
101
+ index,
102
+ address,
103
+ amount,
104
+ tokens,
105
+ paymentCredential,
106
+ }: CardanoOutputRaw): CardanoOutput {
107
+ return {
108
+ hash,
109
+ index,
110
+ address,
111
+ amount: new BigNumber(amount),
112
+ tokens: tokens.map(fromTokenRaw),
113
+ paymentCredential,
114
+ };
115
+ }
116
+
117
+ function toProtocolParamsRaw({
118
+ minFeeA,
119
+ minFeeB,
120
+ stakeKeyDeposit,
121
+ lovelacePerUtxoWord,
122
+ collateralPercent,
123
+ priceSteps,
124
+ priceMem,
125
+ languageView,
126
+ }: ProtocolParams): ProtocolParamsRaw {
127
+ return {
128
+ minFeeA,
129
+ minFeeB,
130
+ stakeKeyDeposit,
131
+ lovelacePerUtxoWord,
132
+ collateralPercent,
133
+ priceSteps,
134
+ priceMem,
135
+ languageView,
136
+ };
137
+ }
138
+
139
+ function fromProtocolParamsRaw({
140
+ minFeeA,
141
+ minFeeB,
142
+ stakeKeyDeposit,
143
+ lovelacePerUtxoWord,
144
+ collateralPercent,
145
+ priceSteps,
146
+ priceMem,
147
+ languageView,
148
+ }: ProtocolParamsRaw): ProtocolParams {
149
+ return {
150
+ minFeeA,
151
+ minFeeB,
152
+ stakeKeyDeposit,
153
+ lovelacePerUtxoWord,
154
+ collateralPercent,
155
+ priceSteps,
156
+ priceMem,
157
+ languageView,
158
+ };
159
+ }
160
+
161
+ export function toCardanoResourceRaw(r: CardanoResources): CardanoResourcesRaw {
162
+ return {
163
+ internalCredentials: r.internalCredentials.map(toPaymentCredentialRaw),
164
+ externalCredentials: r.externalCredentials.map(toPaymentCredentialRaw),
165
+ utxos: r.utxos.map(toCardanoOutputRaw),
166
+ protocolParams: toProtocolParamsRaw(r.protocolParams),
167
+ };
168
+ }
169
+
170
+ export function fromCardanoResourceRaw(
171
+ r: CardanoResourcesRaw
172
+ ): CardanoResources {
173
+ return {
174
+ internalCredentials: r.internalCredentials.map(fromPaymentCredentialRaw),
175
+ externalCredentials: r.externalCredentials.map(fromPaymentCredentialRaw),
176
+ utxos: r.utxos.map(fromCardanoOutputRaw),
177
+ protocolParams: fromProtocolParamsRaw(r.protocolParams),
178
+ };
179
+ }