@ledgerhq/live-common 25.0.0-next.4 → 25.0.0-next.5

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 (62) hide show
  1. package/lib/apps/support.d.ts.map +1 -1
  2. package/lib/apps/support.js +2 -1
  3. package/lib/apps/support.js.map +1 -1
  4. package/lib/families/cardano/bridge.integration.test.d.ts.map +1 -1
  5. package/lib/families/cardano/bridge.integration.test.js +36 -2
  6. package/lib/families/cardano/bridge.integration.test.js.map +1 -1
  7. package/lib/families/cardano/buildSubAccounts.d.ts.map +1 -1
  8. package/lib/families/cardano/buildSubAccounts.js +5 -3
  9. package/lib/families/cardano/buildSubAccounts.js.map +1 -1
  10. package/lib/families/cardano/deviceTransactionConfig.d.ts +11 -0
  11. package/lib/families/cardano/deviceTransactionConfig.d.ts.map +1 -0
  12. package/lib/families/cardano/deviceTransactionConfig.js +91 -0
  13. package/lib/families/cardano/deviceTransactionConfig.js.map +1 -0
  14. package/lib/families/cardano/errors.d.ts +4 -0
  15. package/lib/families/cardano/errors.d.ts.map +1 -1
  16. package/lib/families/cardano/errors.js +5 -1
  17. package/lib/families/cardano/errors.js.map +1 -1
  18. package/lib/families/cardano/js-estimateMaxSpendable.d.ts.map +1 -1
  19. package/lib/families/cardano/js-estimateMaxSpendable.js +3 -2
  20. package/lib/families/cardano/js-estimateMaxSpendable.js.map +1 -1
  21. package/lib/families/cardano/js-getTransactionStatus.d.ts.map +1 -1
  22. package/lib/families/cardano/js-getTransactionStatus.js +6 -5
  23. package/lib/families/cardano/js-getTransactionStatus.js.map +1 -1
  24. package/lib/families/cardano/js-signOperation.d.ts.map +1 -1
  25. package/lib/families/cardano/js-signOperation.js +10 -5
  26. package/lib/families/cardano/js-signOperation.js.map +1 -1
  27. package/lib/families/cardano/js-synchronisation.d.ts.map +1 -1
  28. package/lib/families/cardano/js-synchronisation.js +126 -98
  29. package/lib/families/cardano/js-synchronisation.js.map +1 -1
  30. package/lib/families/cardano/js-transaction.js +1 -1
  31. package/lib/families/cardano/js-transaction.js.map +1 -1
  32. package/lib/families/cardano/logic.d.ts +1 -0
  33. package/lib/families/cardano/logic.d.ts.map +1 -1
  34. package/lib/families/cardano/logic.js +11 -1
  35. package/lib/families/cardano/logic.js.map +1 -1
  36. package/lib/families/cardano/specs.d.ts.map +1 -1
  37. package/lib/families/cardano/specs.js +39 -1
  38. package/lib/families/cardano/specs.js.map +1 -1
  39. package/lib/families/cardano/speculos-deviceActions.d.ts.map +1 -1
  40. package/lib/families/cardano/speculos-deviceActions.js +15 -0
  41. package/lib/families/cardano/speculos-deviceActions.js.map +1 -1
  42. package/lib/generated/deviceTransactionConfig.d.ts +2 -0
  43. package/lib/generated/deviceTransactionConfig.d.ts.map +1 -1
  44. package/lib/generated/deviceTransactionConfig.js +28 -26
  45. package/lib/generated/deviceTransactionConfig.js.map +1 -1
  46. package/package.json +3 -3
  47. package/src/apps/support.ts +1 -0
  48. package/src/currencies/__snapshots__/sortByMarketcap.test.ts.snap +101 -0
  49. package/src/families/cardano/__snapshots__/bridge.integration.test.ts.snap +41 -1
  50. package/src/families/cardano/bridge.integration.test.ts +38 -0
  51. package/src/families/cardano/buildSubAccounts.ts +5 -3
  52. package/src/families/cardano/deviceTransactionConfig.ts +109 -0
  53. package/src/families/cardano/errors.ts +7 -0
  54. package/src/families/cardano/js-estimateMaxSpendable.ts +4 -1
  55. package/src/families/cardano/js-getTransactionStatus.ts +7 -7
  56. package/src/families/cardano/js-signOperation.ts +10 -5
  57. package/src/families/cardano/js-synchronisation.ts +52 -21
  58. package/src/families/cardano/js-transaction.ts +1 -1
  59. package/src/families/cardano/logic.ts +10 -0
  60. package/src/families/cardano/specs.ts +53 -1
  61. package/src/families/cardano/speculos-deviceActions.ts +15 -0
  62. package/src/generated/deviceTransactionConfig.ts +3 -0
@@ -21,6 +21,7 @@ import {
21
21
  getMemoFromTx,
22
22
  getOperationType,
23
23
  isHexString,
24
+ mergeTokens,
24
25
  } from "./logic";
25
26
  import { encodeOperationId } from "../../operation";
26
27
  import { getNetworkParameters } from "./networks";
@@ -28,6 +29,9 @@ import { getNetworkInfo } from "./api/getNetworkInfo";
28
29
  import uniqBy from "lodash/uniqBy";
29
30
  import postSyncPatch from "./postSyncPatch";
30
31
  import { getTransactions } from "./api/getTransactions";
32
+ import { buildSubAccounts } from "./buildSubAccounts";
33
+ import { calculateMinUtxoAmount } from "@stricahq/typhonjs/dist/utils/utils";
34
+ import { listTokensForCryptoCurrency } from "../../currencies";
31
35
 
32
36
  function mapTxToAccountOperation(
33
37
  tx: APITransaction,
@@ -43,6 +47,10 @@ function mapTxToAccountOperation(
43
47
 
44
48
  const subOperations = inferSubOperations(tx.hash, subAccounts);
45
49
  const memo = getMemoFromTx(tx);
50
+ const extra = {};
51
+ if (memo) {
52
+ extra["memo"] = memo;
53
+ }
46
54
 
47
55
  return {
48
56
  accountId,
@@ -64,14 +72,12 @@ function mapTxToAccountOperation(
64
72
  subOperations,
65
73
  blockHeight: tx.blockHeight,
66
74
  date: new Date(tx.timestamp),
67
- extra: {
68
- memo,
69
- },
75
+ extra: extra,
70
76
  blockHash: undefined,
71
77
  };
72
78
  }
73
79
 
74
- function syncUtxos(
80
+ function prepareUtxos(
75
81
  newTransactions: Array<APITransaction>,
76
82
  stableUtxos: Array<CardanoOutput>,
77
83
  accountCredentialsMap: Record<string, PaymentCredential>
@@ -116,7 +122,10 @@ function syncUtxos(
116
122
  return utxos;
117
123
  }
118
124
 
119
- export const getAccountShape: GetAccountShape = async (info) => {
125
+ export const getAccountShape: GetAccountShape = async (
126
+ info,
127
+ { blacklistedTokenIds }
128
+ ) => {
120
129
  const {
121
130
  transport,
122
131
  currency,
@@ -152,16 +161,27 @@ export const getAccountShape: GetAccountShape = async (info) => {
152
161
  derivationMode,
153
162
  });
154
163
 
164
+ // when new tokens are added / blacklist changes, we need to sync again because we need to go through all operations again
165
+ const syncHash =
166
+ JSON.stringify(blacklistedTokenIds || []) +
167
+ "_" +
168
+ listTokensForCryptoCurrency(currency, {
169
+ withDelisted: true,
170
+ }).length;
171
+ const outdatedSyncHash = initialAccount?.syncHash !== syncHash;
172
+
155
173
  const requiredConfirmations = 90;
156
174
 
157
175
  const oldOperations = initialAccount?.operations || [];
158
176
  const lastBlockHeight = oldOperations.length
159
177
  ? (oldOperations[0].blockHeight || 0) + 1
160
178
  : 0;
161
- const syncFromBlockHeight =
162
- lastBlockHeight > requiredConfirmations
163
- ? lastBlockHeight - requiredConfirmations
164
- : 0;
179
+
180
+ const syncFromBlockHeight = outdatedSyncHash
181
+ ? 0
182
+ : lastBlockHeight > requiredConfirmations
183
+ ? lastBlockHeight - requiredConfirmations
184
+ : 0;
165
185
 
166
186
  const {
167
187
  transactions: newTransactions,
@@ -195,21 +215,24 @@ export const getAccountShape: GetAccountShape = async (info) => {
195
215
  (u) => stableOperationsByIds[u.hash]
196
216
  );
197
217
 
198
- const utxos = syncUtxos(newTransactions, stableUtxos, accountCredentialsMap);
218
+ const utxos = prepareUtxos(
219
+ newTransactions,
220
+ stableUtxos,
221
+ accountCredentialsMap
222
+ );
199
223
  const accountBalance = utxos.reduce(
200
224
  (total, u) => total.plus(u.amount),
201
225
  new BigNumber(0)
202
226
  );
203
- // const tokenBalance = mergeTokens(utxos.map((u) => u.tokens).flat());
204
- // const subAccounts = buildSubAccounts({
205
- // initialAccount,
206
- // parentAccountId: accountId,
207
- // parentCurrency: currency,
208
- // newTransactions,
209
- // tokens: tokenBalance,
210
- // accountCredentialsMap,
211
- // });
212
- const subAccounts = [];
227
+ const tokenBalance = mergeTokens(utxos.map((u) => u.tokens).flat());
228
+ const subAccounts = buildSubAccounts({
229
+ initialAccount,
230
+ parentAccountId: accountId,
231
+ parentCurrency: currency,
232
+ newTransactions,
233
+ tokens: tokenBalance,
234
+ accountCredentialsMap,
235
+ });
213
236
 
214
237
  const newOperations = newTransactions.map((t) =>
215
238
  mapTxToAccountOperation(t, accountId, accountCredentialsMap, subAccounts)
@@ -233,13 +256,21 @@ export const getAccountShape: GetAccountShape = async (info) => {
233
256
  }).getBech32(),
234
257
  }));
235
258
  const cardanoNetworkInfo = await getNetworkInfo(initialAccount, currency);
259
+ const minAdaBalanceForTokens = tokenBalance.length
260
+ ? calculateMinUtxoAmount(
261
+ tokenBalance,
262
+ new BigNumber(cardanoNetworkInfo.protocolParams.lovelacePerUtxoWord),
263
+ false
264
+ )
265
+ : new BigNumber(0);
236
266
 
237
267
  return {
238
268
  id: accountId,
239
269
  xpub,
240
270
  balance: accountBalance,
241
- spendableBalance: accountBalance,
271
+ spendableBalance: accountBalance.minus(minAdaBalanceForTokens),
242
272
  operations: operations,
273
+ syncHash,
243
274
  subAccounts,
244
275
  freshAddresses,
245
276
  freshAddress: freshAddresses[0].address,
@@ -47,7 +47,7 @@ export const prepareTransaction = async (
47
47
  try {
48
48
  transaction = await buildTransaction(a, t);
49
49
  } catch (error) {
50
- return { ...t, fees: new BigNumber(0), amount: new BigNumber(0) };
50
+ return { ...t, fees: new BigNumber(0), amount: t.amount };
51
51
  }
52
52
 
53
53
  const transactionFees = transaction.getFee();
@@ -448,3 +448,13 @@ export function isHexString(value: string): boolean {
448
448
  const regExp = /^[0-9a-fA-F]+$/;
449
449
  return regExp.test(value);
450
450
  }
451
+
452
+ export function decodeTokenName(assetName: string): string {
453
+ if (assetName.length > 0) {
454
+ const bytes = [...Buffer.from(assetName, "hex")];
455
+ if (bytes.filter((byte) => byte <= 32 || byte >= 127).length === 0) {
456
+ return String.fromCharCode(...bytes);
457
+ }
458
+ }
459
+ return assetName;
460
+ }
@@ -9,10 +9,15 @@ import invariant from "invariant";
9
9
  import { utils as TyphonUtils } from "@stricahq/typhonjs";
10
10
  import { mergeTokens } from "./logic";
11
11
  import { parseCurrencyUnit } from "../../currencies";
12
+ import { SubAccount } from "../../types";
12
13
 
13
14
  const currency = getCryptoCurrencyById("cardano");
14
15
  const minBalanceRequired = parseCurrencyUnit(currency.units[0], "2.2");
15
16
  const minBalanceRequiredForMaxSend = parseCurrencyUnit(currency.units[0], "1");
17
+ const minSpendableRequiredForTokenTx = parseCurrencyUnit(
18
+ currency.units[0],
19
+ "3"
20
+ );
16
21
 
17
22
  const cardano: AppSpec<Transaction> = {
18
23
  name: "cardano",
@@ -34,7 +39,12 @@ const cardano: AppSpec<Transaction> = {
34
39
 
35
40
  const updates = [
36
41
  { recipient },
37
- { amount: new BigNumber(account.balance.dividedBy(2)).dp(0, 1) },
42
+ {
43
+ amount: new BigNumber(account.balance.dividedBy(2)).dp(
44
+ 0,
45
+ BigNumber.ROUND_CEIL
46
+ ),
47
+ },
38
48
  { memo: "LedgerLiveBot" },
39
49
  ];
40
50
 
@@ -90,6 +100,48 @@ const cardano: AppSpec<Transaction> = {
90
100
  );
91
101
  },
92
102
  },
103
+ {
104
+ name: "move ~10% token",
105
+ maxRun: 1,
106
+ transaction: ({ account, siblings, bridge, maxSpendable }) => {
107
+ invariant(
108
+ maxSpendable.gte(minSpendableRequiredForTokenTx),
109
+ "balance is too low"
110
+ );
111
+ const sibling = pickSiblings(siblings, 3);
112
+ const recipient = sibling.freshAddress;
113
+ const transaction = bridge.createTransaction(account);
114
+
115
+ const subAccount = account.subAccounts?.find((subAccount) =>
116
+ subAccount.balance.gt(1)
117
+ ) as SubAccount;
118
+ invariant(subAccount, "No token account with balance");
119
+
120
+ const updates = [
121
+ { subAccountId: subAccount.id },
122
+ { recipient },
123
+ {
124
+ amount: new BigNumber(subAccount.balance.dividedBy(10)).dp(
125
+ 0,
126
+ BigNumber.ROUND_CEIL
127
+ ),
128
+ },
129
+ ];
130
+
131
+ return {
132
+ transaction,
133
+ updates,
134
+ };
135
+ },
136
+ test: ({ operation, transaction }): void => {
137
+ expect(operation.subOperations).toBeTruthy();
138
+ expect(operation.subOperations?.length).toEqual(1);
139
+
140
+ const subOperation =
141
+ operation.subOperations && operation.subOperations[0];
142
+ expect(subOperation?.value).toEqual(transaction.amount);
143
+ },
144
+ },
93
145
  ],
94
146
  };
95
147
 
@@ -39,6 +39,21 @@ const acceptTransaction: DeviceAction<Transaction, any> = deviceActionFlow({
39
39
  expectedValue: ({ account, status }) =>
40
40
  expectedValue(account.unit, status.amount),
41
41
  },
42
+ {
43
+ title: "Asset fingerprint",
44
+ button: "LRlr",
45
+ },
46
+ {
47
+ title: "Token amount",
48
+ button: "LRlr",
49
+ },
50
+ {
51
+ title: "Confirm",
52
+ },
53
+ {
54
+ title: "output?",
55
+ button: "Rr",
56
+ },
42
57
  {
43
58
  title: "Transaction fee",
44
59
  button: "LRlr",
@@ -2,6 +2,8 @@ import algorand from "../families/algorand/deviceTransactionConfig";
2
2
 
3
3
  import bitcoin from "../families/bitcoin/deviceTransactionConfig";
4
4
 
5
+ import cardano from "../families/cardano/deviceTransactionConfig";
6
+
5
7
  import celo from "../families/celo/deviceTransactionConfig";
6
8
 
7
9
  import cosmos from "../families/cosmos/deviceTransactionConfig";
@@ -32,6 +34,7 @@ import tron from "../families/tron/deviceTransactionConfig";
32
34
  export default {
33
35
  algorand,
34
36
  bitcoin,
37
+ cardano,
35
38
  celo,
36
39
  cosmos,
37
40
  crypto_org,