@ledgerhq/live-common 23.0.0-next.0 → 23.0.0-next.3

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 (249) 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 +4 -0
  13. package/lib/derivation.d.ts.map +1 -1
  14. package/lib/derivation.js +18 -2
  15. package/lib/derivation.js.map +1 -1
  16. package/lib/env.d.ts +15 -5
  17. package/lib/env.d.ts.map +1 -1
  18. package/lib/env.js +10 -0
  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 +371 -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/cosmos/test-dataset.d.ts.map +1 -1
  152. package/lib/families/cosmos/test-dataset.js +1 -1
  153. package/lib/families/cosmos/test-dataset.js.map +1 -1
  154. package/lib/families/crypto_org/js-synchronisation.d.ts +2 -2
  155. package/lib/families/elrond/js-synchronisation.d.ts +2 -2
  156. package/lib/families/hedera/js-synchronisation.d.ts +2 -2
  157. package/lib/families/polkadot/js-synchronisation.d.ts +2 -2
  158. package/lib/families/ripple/js-synchronization.d.ts +2 -2
  159. package/lib/families/stellar/js-synchronization.d.ts +2 -2
  160. package/lib/generated/bridge/js.d.ts +4 -0
  161. package/lib/generated/bridge/js.d.ts.map +1 -1
  162. package/lib/generated/bridge/js.js +30 -28
  163. package/lib/generated/bridge/js.js.map +1 -1
  164. package/lib/generated/cli-transaction.d.ts +15 -0
  165. package/lib/generated/cli-transaction.d.ts.map +1 -1
  166. package/lib/generated/cli-transaction.js +28 -26
  167. package/lib/generated/cli-transaction.js.map +1 -1
  168. package/lib/generated/exchange.d.ts +1 -1
  169. package/lib/generated/hw-getAddress.d.ts +1 -0
  170. package/lib/generated/hw-getAddress.d.ts.map +1 -1
  171. package/lib/generated/hw-getAddress.js +30 -28
  172. package/lib/generated/hw-getAddress.js.map +1 -1
  173. package/lib/generated/specs.d.ts +3 -0
  174. package/lib/generated/specs.d.ts.map +1 -1
  175. package/lib/generated/specs.js +28 -26
  176. package/lib/generated/specs.js.map +1 -1
  177. package/lib/generated/speculos-deviceActions.d.ts +3 -0
  178. package/lib/generated/speculos-deviceActions.d.ts.map +1 -1
  179. package/lib/generated/speculos-deviceActions.js +28 -26
  180. package/lib/generated/speculos-deviceActions.js.map +1 -1
  181. package/lib/generated/test-dataset.d.ts +1 -0
  182. package/lib/generated/test-dataset.d.ts.map +1 -1
  183. package/lib/generated/test-dataset.js +26 -24
  184. package/lib/generated/test-dataset.js.map +1 -1
  185. package/lib/generated/transaction.d.ts +5 -0
  186. package/lib/generated/transaction.d.ts.map +1 -1
  187. package/lib/generated/transaction.js +30 -28
  188. package/lib/generated/transaction.js.map +1 -1
  189. package/lib/generated/types.d.ts +4 -2
  190. package/lib/generated/types.d.ts.map +1 -1
  191. package/lib/hw/actions/startExchange.d.ts.map +1 -1
  192. package/lib/hw/actions/startExchange.js +3 -2
  193. package/lib/hw/actions/startExchange.js.map +1 -1
  194. package/lib/reconciliation.d.ts.map +1 -1
  195. package/lib/reconciliation.js +6 -0
  196. package/lib/reconciliation.js.map +1 -1
  197. package/lib/types/account.d.ts +3 -0
  198. package/lib/types/account.d.ts.map +1 -1
  199. package/package.json +4 -1
  200. package/src/__tests__/test-helpers/setup.ts +2 -0
  201. package/src/account/serialization.ts +15 -0
  202. package/src/cross.ts +2 -1
  203. package/src/derivation.ts +10 -0
  204. package/src/env.ts +10 -0
  205. package/src/errors.ts +1 -0
  206. package/src/families/cardano/__snapshots__/bridge.test.ts.snap +1667 -0
  207. package/src/families/cardano/api/api-types.ts +33 -0
  208. package/src/families/cardano/api/getNetworkInfo.ts +38 -0
  209. package/src/families/cardano/api/getTransactions.ts +240 -0
  210. package/src/families/cardano/api/submitTransaction.ts +27 -0
  211. package/src/families/cardano/bridge/js.ts +35 -0
  212. package/src/families/cardano/bridge.test.ts +6 -0
  213. package/src/families/cardano/buildSubAccounts.ts +182 -0
  214. package/src/families/cardano/cli-transaction.ts +78 -0
  215. package/src/families/cardano/constants.ts +12 -0
  216. package/src/families/cardano/datasets/rawAccount.1.ts +28 -0
  217. package/src/families/cardano/datasets/scanAccounts.ts +23 -0
  218. package/src/families/cardano/errors.ts +8 -0
  219. package/src/families/cardano/hw-getAddress.ts +61 -0
  220. package/src/families/cardano/js-broadcast.ts +25 -0
  221. package/src/families/cardano/js-buildTransaction.ts +293 -0
  222. package/src/families/cardano/js-estimateMaxSpendable.ts +60 -0
  223. package/src/families/cardano/js-getFeesForTransaction.ts +25 -0
  224. package/src/families/cardano/js-getTransactionStatus.ts +123 -0
  225. package/src/families/cardano/js-signOperation.ts +265 -0
  226. package/src/families/cardano/js-synchronisation.ts +255 -0
  227. package/src/families/cardano/js-transaction.ts +69 -0
  228. package/src/families/cardano/logic.ts +450 -0
  229. package/src/families/cardano/networks.ts +33 -0
  230. package/src/families/cardano/postSyncPatch.ts +45 -0
  231. package/src/families/cardano/serialization.ts +179 -0
  232. package/src/families/cardano/specs.ts +96 -0
  233. package/src/families/cardano/speculos-deviceActions.ts +66 -0
  234. package/src/families/cardano/test-dataset.ts +41 -0
  235. package/src/families/cardano/transaction.ts +55 -0
  236. package/src/families/cardano/types.ts +178 -0
  237. package/src/families/cosmos/__snapshots__/bridge.test.ts.snap +134 -6
  238. package/src/families/cosmos/test-dataset.ts +2 -0
  239. package/src/generated/bridge/js.ts +3 -0
  240. package/src/generated/cli-transaction.ts +3 -0
  241. package/src/generated/hw-getAddress.ts +3 -0
  242. package/src/generated/specs.ts +3 -0
  243. package/src/generated/speculos-deviceActions.ts +3 -0
  244. package/src/generated/test-dataset.ts +3 -0
  245. package/src/generated/transaction.ts +3 -0
  246. package/src/generated/types.ts +4 -0
  247. package/src/hw/actions/startExchange.ts +3 -2
  248. package/src/reconciliation.ts +10 -0
  249. package/src/types/account.ts +6 -0
@@ -0,0 +1,450 @@
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 BigNumber from "bignumber.js";
33
+ import { getNetworkParameters } from "./networks";
34
+ import { CryptoCurrency, OperationType } from "../../types";
35
+ import groupBy from "lodash/groupBy";
36
+ import { APITransaction } from "./api/api-types";
37
+ import { getCryptoCurrencyById } from "@ledgerhq/cryptoassets";
38
+ import ShelleyTypeAddress from "@stricahq/typhonjs/dist/address/ShelleyTypeAddress";
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
+ const cardanoAddress = TyphonUtils.getAddressFromBech32(address);
156
+ if (cardanoAddress instanceof ShelleyTypeAddress) {
157
+ const addressNetworkId = Number(
158
+ cardanoAddress.getHex().toLowerCase().charAt(1)
159
+ );
160
+ if (addressNetworkId !== networkId) {
161
+ return false;
162
+ }
163
+ }
164
+ } catch (error) {
165
+ return false;
166
+ }
167
+ return true;
168
+ };
169
+
170
+ export const getAbsoluteSlot = function (
171
+ networkName: string,
172
+ time: Date
173
+ ): number {
174
+ const networkParams = getNetworkParameters(networkName);
175
+ const byronChainEndSlots =
176
+ networkParams.shelleyStartEpoch * networkParams.byronSlotsPerEpoch;
177
+ const byronChainEndTime =
178
+ byronChainEndSlots * networkParams.byronSlotDuration;
179
+
180
+ const shelleyChainTime =
181
+ time.getTime() - networkParams.chainStartTime - byronChainEndTime;
182
+ const shelleyChainSlots = Math.floor(
183
+ shelleyChainTime / networkParams.shelleySlotDuration
184
+ );
185
+ return byronChainEndSlots + shelleyChainSlots;
186
+ };
187
+
188
+ /**
189
+ * Returns the time to live for transaction
190
+ *
191
+ * @returns {number}
192
+ */
193
+ export function getTTL(networkName: string): number {
194
+ return getAbsoluteSlot(networkName, new Date()) + TTL_GAP;
195
+ }
196
+
197
+ export function getEpoch(networkName: string, time: Date): number {
198
+ const networkParams = getNetworkParameters(networkName);
199
+ const chainTime = time.getTime() - networkParams.chainStartTime;
200
+ const epoch = Math.floor(
201
+ chainTime /
202
+ (networkParams.shelleySlotsPerEpoch * networkParams.shelleySlotDuration)
203
+ );
204
+ return epoch;
205
+ }
206
+
207
+ export function mergeTokens(
208
+ tokens: Array<TyphonTypes.Token>
209
+ ): Array<TyphonTypes.Token> {
210
+ return Object.values(
211
+ groupBy(tokens, (t) => `${t.policyId}${t.assetName}`)
212
+ ).map((similarTokens) => ({
213
+ policyId: similarTokens[0].policyId,
214
+ assetName: similarTokens[0].assetName,
215
+ amount: similarTokens.reduce(
216
+ (total, token) => total.plus(token.amount),
217
+ new BigNumber(0)
218
+ ),
219
+ }));
220
+ }
221
+
222
+ /**
223
+ * @param { Array<TyphonTypes.Token> } b
224
+ * @param { Array<TyphonTypes.Token> } a
225
+ * @returns a - b
226
+ */
227
+ export function getTokenDiff(
228
+ a: Array<TyphonTypes.Token>,
229
+ b: Array<TyphonTypes.Token>
230
+ ): Array<TyphonTypes.Token> {
231
+ return mergeTokens(
232
+ a.concat(b.map((t) => ({ ...t, amount: t.amount.negated() })))
233
+ ).filter((t) => !t.amount.eq(0));
234
+ }
235
+
236
+ /**
237
+ * returns the formatted transactionOutput for ledger cardano app
238
+ *
239
+ * @param output
240
+ * @param accountIndex
241
+ * @returns {TxOutput}
242
+ */
243
+ export function prepareLedgerOutput(
244
+ output: TyphonTypes.Output,
245
+ accountIndex: number
246
+ ): TxOutput {
247
+ const isByronAddress = output.address instanceof TyphonAddress.ByronAddress;
248
+ let isDeviceOwnedAddress = false;
249
+ let destination: TxOutputDestination;
250
+
251
+ if (!isByronAddress) {
252
+ const address = output.address as TyphonTypes.ShelleyAddress;
253
+ isDeviceOwnedAddress =
254
+ address.paymentCredential &&
255
+ address.paymentCredential.type === TyphonTypes.HashType.ADDRESS &&
256
+ address.paymentCredential.bipPath !== undefined;
257
+ }
258
+
259
+ if (isDeviceOwnedAddress) {
260
+ const address = output.address as TyphonAddress.BaseAddress;
261
+
262
+ const paymentKeyPath = (
263
+ address.paymentCredential as TyphonTypes.HashCredential
264
+ ).bipPath as TyphonTypes.BipPath;
265
+ const stakingKeyPath = (
266
+ address.stakeCredential as TyphonTypes.HashCredential
267
+ ).bipPath as TyphonTypes.BipPath;
268
+
269
+ const paymentKeyPathString = getBipPathString({
270
+ account: accountIndex,
271
+ chain: paymentKeyPath.chain,
272
+ index: paymentKeyPath.index,
273
+ });
274
+ const stakingKeyPathString = getBipPathString({
275
+ account: accountIndex,
276
+ chain: stakingKeyPath.chain,
277
+ index: stakingKeyPath.index,
278
+ });
279
+
280
+ destination = {
281
+ type: TxOutputDestinationType.DEVICE_OWNED,
282
+ params: {
283
+ type: AddressType.BASE_PAYMENT_KEY_STAKE_KEY,
284
+ params: {
285
+ spendingPath: str_to_path(paymentKeyPathString),
286
+ stakingPath: str_to_path(stakingKeyPathString),
287
+ },
288
+ },
289
+ };
290
+ } else {
291
+ const address = output.address;
292
+ destination = {
293
+ type: TxOutputDestinationType.THIRD_PARTY,
294
+ params: {
295
+ addressHex: address.getHex(),
296
+ },
297
+ };
298
+ }
299
+
300
+ const tokenBundle: Array<AssetGroup> = Object.values(
301
+ groupBy(output.tokens, ({ policyId }) => policyId)
302
+ ).map((tokens) => ({
303
+ policyIdHex: tokens[0].policyId,
304
+ tokens: tokens.map((token) => ({
305
+ assetNameHex: token.assetName,
306
+ amount: token.amount.toString(),
307
+ })),
308
+ }));
309
+
310
+ return {
311
+ amount: output.amount.toString(),
312
+ destination,
313
+ tokenBundle,
314
+ };
315
+ }
316
+
317
+ /**
318
+ * returns the formatted transactionInput for ledger cardano app
319
+ *
320
+ * @param {TyphonTypes.Input} input
321
+ * @param {number} accountIndex
322
+ * @returns {TxInput}
323
+ */
324
+ export function prepareLedgerInput(
325
+ input: TyphonTypes.Input,
326
+ accountIndex: number
327
+ ): TxInput {
328
+ const paymentKeyPath =
329
+ input.address.paymentCredential.type === TyphonTypes.HashType.ADDRESS
330
+ ? input.address.paymentCredential.bipPath
331
+ : undefined;
332
+ return {
333
+ txHashHex: input.txId,
334
+ outputIndex: input.index,
335
+ path: paymentKeyPath
336
+ ? str_to_path(
337
+ getBipPathString({
338
+ account: accountIndex,
339
+ chain: paymentKeyPath.chain,
340
+ index: paymentKeyPath.index,
341
+ })
342
+ )
343
+ : null,
344
+ };
345
+ }
346
+
347
+ export function getAccountStakeCredential(
348
+ xpub: string,
349
+ index: number
350
+ ): StakeCredential {
351
+ const accountXPubKey = getExtendedPublicKeyFromHex(xpub);
352
+ const keyPath = getCredentialKey(
353
+ accountXPubKey,
354
+ getBipPath({
355
+ account: index,
356
+ chain: StakeChain.stake,
357
+ index: STAKING_ADDRESS_INDEX,
358
+ })
359
+ );
360
+ return {
361
+ key: keyPath.key,
362
+ path: keyPath.path,
363
+ };
364
+ }
365
+
366
+ export function getOperationType({
367
+ valueChange,
368
+ fees,
369
+ }: {
370
+ valueChange: BigNumber;
371
+ fees: BigNumber;
372
+ }): OperationType {
373
+ return valueChange.isNegative()
374
+ ? valueChange.absoluteValue().eq(fees)
375
+ ? "FEES"
376
+ : "OUT"
377
+ : valueChange.isPositive()
378
+ ? "IN"
379
+ : "NONE";
380
+ }
381
+
382
+ export function isTestnet(currency: CryptoCurrency): boolean {
383
+ return getCryptoCurrencyById(currency.id).isTestnetFor ? true : false;
384
+ }
385
+
386
+ export function getAccountChange(
387
+ t: APITransaction,
388
+ accountCredentialsMap: Record<string, PaymentCredential>
389
+ ): { ada: BigNumber; tokens: Array<Token> } {
390
+ let accountInputAda = new BigNumber(0);
391
+ const accountInputTokens: Array<Token> = [];
392
+ t.inputs.forEach((i) => {
393
+ if (accountCredentialsMap[i.paymentKey]) {
394
+ accountInputAda = accountInputAda.plus(i.value);
395
+ accountInputTokens.push(
396
+ ...i.tokens.map((t) => ({
397
+ assetName: t.assetName,
398
+ policyId: t.policyId,
399
+ amount: new BigNumber(t.value),
400
+ }))
401
+ );
402
+ }
403
+ });
404
+
405
+ let accountOutputAda = new BigNumber(0);
406
+ const accountOutputTokens: Array<Token> = [];
407
+ t.outputs.forEach((o) => {
408
+ if (accountCredentialsMap[o.paymentKey]) {
409
+ accountOutputAda = accountOutputAda.plus(o.value);
410
+ accountOutputTokens.push(
411
+ ...o.tokens.map((t) => ({
412
+ assetName: t.assetName,
413
+ policyId: t.policyId,
414
+ amount: new BigNumber(t.value),
415
+ }))
416
+ );
417
+ }
418
+ });
419
+
420
+ return {
421
+ ada: accountOutputAda.minus(accountInputAda),
422
+ tokens: getTokenDiff(accountOutputTokens, accountInputTokens),
423
+ };
424
+ }
425
+
426
+ export function getMemoFromTx(tx: APITransaction): string | undefined {
427
+ let memo;
428
+ const metadataValue = tx.metadata?.data.find(
429
+ (m) => m.label === MEMO_LABEL.toString()
430
+ );
431
+ if (metadataValue) {
432
+ try {
433
+ const parsedValue = JSON.parse(metadataValue.value);
434
+ if (
435
+ parsedValue.msg &&
436
+ Array.isArray(parsedValue.msg) &&
437
+ parsedValue.msg.length
438
+ ) {
439
+ memo = parsedValue.msg.join(", ");
440
+ }
441
+ // eslint-disable-next-line no-empty
442
+ } catch (e) {}
443
+ }
444
+ return memo;
445
+ }
446
+
447
+ export function isHexString(value: string): boolean {
448
+ const regExp = /^[0-9a-fA-F]+$/;
449
+ return regExp.test(value);
450
+ }
@@ -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
+ }