@ledgerhq/coin-canton 0.10.0 → 0.11.0-nightly.20251126160702

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 (69) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/lib/bridge/acceptOffer.d.ts +8 -0
  3. package/lib/bridge/acceptOffer.d.ts.map +1 -0
  4. package/lib/bridge/acceptOffer.js +18 -0
  5. package/lib/bridge/acceptOffer.js.map +1 -0
  6. package/lib/bridge/index.d.ts.map +1 -1
  7. package/lib/bridge/index.js +3 -0
  8. package/lib/bridge/index.js.map +1 -1
  9. package/lib/bridge/serialization.d.ts +2 -0
  10. package/lib/bridge/serialization.d.ts.map +1 -1
  11. package/lib/bridge/serialization.js +4 -1
  12. package/lib/bridge/serialization.js.map +1 -1
  13. package/lib/bridge/sync.d.ts.map +1 -1
  14. package/lib/bridge/sync.js +14 -4
  15. package/lib/bridge/sync.js.map +1 -1
  16. package/lib/helpers.d.ts +10 -0
  17. package/lib/helpers.d.ts.map +1 -0
  18. package/lib/helpers.js +26 -0
  19. package/lib/helpers.js.map +1 -0
  20. package/lib/index.d.ts +2 -0
  21. package/lib/index.d.ts.map +1 -1
  22. package/lib/index.js +5 -1
  23. package/lib/index.js.map +1 -1
  24. package/lib/network/gateway.d.ts +18 -0
  25. package/lib/network/gateway.d.ts.map +1 -1
  26. package/lib/network/gateway.js +26 -6
  27. package/lib/network/gateway.js.map +1 -1
  28. package/lib/types/bridge.d.ts +4 -0
  29. package/lib/types/bridge.d.ts.map +1 -1
  30. package/lib-es/bridge/acceptOffer.d.ts +8 -0
  31. package/lib-es/bridge/acceptOffer.d.ts.map +1 -0
  32. package/lib-es/bridge/acceptOffer.js +14 -0
  33. package/lib-es/bridge/acceptOffer.js.map +1 -0
  34. package/lib-es/bridge/index.d.ts.map +1 -1
  35. package/lib-es/bridge/index.js +3 -0
  36. package/lib-es/bridge/index.js.map +1 -1
  37. package/lib-es/bridge/serialization.d.ts +2 -0
  38. package/lib-es/bridge/serialization.d.ts.map +1 -1
  39. package/lib-es/bridge/serialization.js +4 -2
  40. package/lib-es/bridge/serialization.js.map +1 -1
  41. package/lib-es/bridge/sync.d.ts.map +1 -1
  42. package/lib-es/bridge/sync.js +16 -6
  43. package/lib-es/bridge/sync.js.map +1 -1
  44. package/lib-es/helpers.d.ts +10 -0
  45. package/lib-es/helpers.d.ts.map +1 -0
  46. package/lib-es/helpers.js +22 -0
  47. package/lib-es/helpers.js.map +1 -0
  48. package/lib-es/index.d.ts +2 -0
  49. package/lib-es/index.d.ts.map +1 -1
  50. package/lib-es/index.js +2 -0
  51. package/lib-es/index.js.map +1 -1
  52. package/lib-es/network/gateway.d.ts +18 -0
  53. package/lib-es/network/gateway.d.ts.map +1 -1
  54. package/lib-es/network/gateway.js +22 -6
  55. package/lib-es/network/gateway.js.map +1 -1
  56. package/lib-es/types/bridge.d.ts +4 -0
  57. package/lib-es/types/bridge.d.ts.map +1 -1
  58. package/package.json +6 -6
  59. package/src/bridge/acceptOffer.test.ts +300 -0
  60. package/src/bridge/acceptOffer.ts +36 -0
  61. package/src/bridge/getTransactionStatus.test.ts +1 -0
  62. package/src/bridge/index.ts +3 -0
  63. package/src/bridge/serialization.ts +4 -2
  64. package/src/bridge/sync.ts +21 -6
  65. package/src/helpers.test.ts +361 -0
  66. package/src/helpers.ts +31 -0
  67. package/src/index.ts +2 -0
  68. package/src/network/gateway.ts +60 -6
  69. package/src/types/bridge.ts +15 -0
@@ -0,0 +1,300 @@
1
+ import { SignerContext } from "@ledgerhq/coin-framework/signer";
2
+ import type { Account } from "@ledgerhq/types-live";
3
+ import type { CryptoCurrency } from "@ledgerhq/types-cryptoassets";
4
+ import { buildTransferInstruction } from "./acceptOffer";
5
+ import * as gateway from "../network/gateway";
6
+ import * as signTransactionModule from "../common-logic/transaction/sign";
7
+ import type { CantonSigner, CantonSignature } from "../types/signer";
8
+ import type { PrepareTransferResponse } from "../network/gateway";
9
+
10
+ jest.mock("../network/gateway");
11
+ jest.mock("../common-logic/transaction/sign");
12
+
13
+ const mockedGateway = gateway as jest.Mocked<typeof gateway>;
14
+ const mockedSignTransaction = signTransactionModule as jest.Mocked<typeof signTransactionModule>;
15
+
16
+ describe("acceptOffer", () => {
17
+ const mockCurrency = {
18
+ id: "canton_network",
19
+ } as unknown as CryptoCurrency;
20
+
21
+ const mockAccount = {
22
+ id: "test-account-id",
23
+ freshAddressPath: "44'/6767'/0'/0'/0'",
24
+ } as unknown as Account;
25
+
26
+ const mockPartyId = "test-party-id";
27
+ const mockContractId = "test-contract-id";
28
+ const mockDeviceId = "test-device-id";
29
+
30
+ const mockPreparedTransaction: PrepareTransferResponse = {
31
+ hash: "test-hash",
32
+ json: {
33
+ transaction: {
34
+ version: "2.1",
35
+ roots: ["0"],
36
+ nodes: [
37
+ {
38
+ nodeId: "0",
39
+ v1: {
40
+ create: {
41
+ lfVersion: "2.1",
42
+ contractId: mockContractId,
43
+ packageName: "test-package",
44
+ templateId: {
45
+ packageId: "test-package-id",
46
+ moduleName: "TestModule",
47
+ entityName: "TestEntity",
48
+ },
49
+ argument: {
50
+ record: {
51
+ recordId: {
52
+ packageId: "test-package-id",
53
+ moduleName: "TestModule",
54
+ entityName: "TestEntity",
55
+ },
56
+ fields: [],
57
+ },
58
+ },
59
+ },
60
+ },
61
+ },
62
+ ],
63
+ },
64
+ metadata: {
65
+ submitterInfo: {
66
+ actAs: [mockPartyId],
67
+ commandId: "test-command-id",
68
+ },
69
+ synchronizerId: "test-synchronizer-id",
70
+ transactionUuid: "test-transaction-uuid",
71
+ submissionTime: "1234567890",
72
+ inputContracts: [],
73
+ },
74
+ },
75
+ serialized: "serialized-transaction",
76
+ };
77
+
78
+ const mockSignature: CantonSignature = {
79
+ signature: "test-signature",
80
+ };
81
+
82
+ const mockSigner: CantonSigner = {
83
+ getAddress: jest.fn(),
84
+ signTransaction: jest.fn(),
85
+ } as unknown as CantonSigner;
86
+
87
+ const mockSignerContext: SignerContext<CantonSigner> = jest.fn(
88
+ async (deviceId: string, callback: (signer: CantonSigner) => Promise<CantonSignature>) => {
89
+ return callback(mockSigner);
90
+ },
91
+ ) as unknown as SignerContext<CantonSigner>;
92
+
93
+ beforeEach(() => {
94
+ jest.clearAllMocks();
95
+ mockedGateway.prepareTransferInstruction.mockResolvedValue(mockPreparedTransaction);
96
+ mockedSignTransaction.signTransaction.mockResolvedValue(mockSignature);
97
+ mockedGateway.submitTransferInstruction.mockResolvedValue({ update_id: "test-update-id" });
98
+ });
99
+
100
+ describe("buildTransferInstruction", () => {
101
+ it("should accept transfer instruction without reason", async () => {
102
+ // GIVEN
103
+ const transferInstruction = buildTransferInstruction(mockSignerContext);
104
+
105
+ // WHEN
106
+ await transferInstruction(
107
+ mockCurrency,
108
+ mockDeviceId,
109
+ mockAccount,
110
+ mockPartyId,
111
+ mockContractId,
112
+ "accept-transfer-instruction",
113
+ );
114
+
115
+ // THEN
116
+ expect(mockedGateway.prepareTransferInstruction).toHaveBeenCalledWith(
117
+ mockCurrency,
118
+ mockPartyId,
119
+ {
120
+ type: "accept-transfer-instruction",
121
+ contract_id: mockContractId,
122
+ },
123
+ );
124
+ expect(mockedSignTransaction.signTransaction).toHaveBeenCalledWith(
125
+ mockSigner,
126
+ mockAccount.freshAddressPath,
127
+ mockPreparedTransaction,
128
+ );
129
+ expect(mockedGateway.submitTransferInstruction).toHaveBeenCalledWith(
130
+ mockCurrency,
131
+ mockPartyId,
132
+ mockPreparedTransaction.serialized,
133
+ mockSignature.signature,
134
+ );
135
+ expect(mockSignerContext).toHaveBeenCalledWith(mockDeviceId, expect.any(Function));
136
+ });
137
+
138
+ it("should accept transfer instruction with reason", async () => {
139
+ // GIVEN
140
+ const reason = "test-reason";
141
+ const transferInstruction = buildTransferInstruction(mockSignerContext);
142
+
143
+ // WHEN
144
+ await transferInstruction(
145
+ mockCurrency,
146
+ mockDeviceId,
147
+ mockAccount,
148
+ mockPartyId,
149
+ mockContractId,
150
+ "accept-transfer-instruction",
151
+ reason,
152
+ );
153
+
154
+ // THEN
155
+ expect(mockedGateway.prepareTransferInstruction).toHaveBeenCalledWith(
156
+ mockCurrency,
157
+ mockPartyId,
158
+ {
159
+ type: "accept-transfer-instruction",
160
+ contract_id: mockContractId,
161
+ reason,
162
+ },
163
+ );
164
+ expect(mockedSignTransaction.signTransaction).toHaveBeenCalledWith(
165
+ mockSigner,
166
+ mockAccount.freshAddressPath,
167
+ mockPreparedTransaction,
168
+ );
169
+ expect(mockedGateway.submitTransferInstruction).toHaveBeenCalledWith(
170
+ mockCurrency,
171
+ mockPartyId,
172
+ mockPreparedTransaction.serialized,
173
+ mockSignature.signature,
174
+ );
175
+ });
176
+
177
+ it("should reject transfer instruction", async () => {
178
+ // GIVEN
179
+ const transferInstruction = buildTransferInstruction(mockSignerContext);
180
+
181
+ // WHEN
182
+ await transferInstruction(
183
+ mockCurrency,
184
+ mockDeviceId,
185
+ mockAccount,
186
+ mockPartyId,
187
+ mockContractId,
188
+ "reject-transfer-instruction",
189
+ );
190
+
191
+ // THEN
192
+ expect(mockedGateway.prepareTransferInstruction).toHaveBeenCalledWith(
193
+ mockCurrency,
194
+ mockPartyId,
195
+ {
196
+ type: "reject-transfer-instruction",
197
+ contract_id: mockContractId,
198
+ },
199
+ );
200
+ expect(mockedSignTransaction.signTransaction).toHaveBeenCalled();
201
+ expect(mockedGateway.submitTransferInstruction).toHaveBeenCalled();
202
+ });
203
+
204
+ it("should withdraw transfer instruction", async () => {
205
+ // GIVEN
206
+ const transferInstruction = buildTransferInstruction(mockSignerContext);
207
+
208
+ // WHEN
209
+ await transferInstruction(
210
+ mockCurrency,
211
+ mockDeviceId,
212
+ mockAccount,
213
+ mockPartyId,
214
+ mockContractId,
215
+ "withdraw-transfer-instruction",
216
+ );
217
+
218
+ // THEN
219
+ expect(mockedGateway.prepareTransferInstruction).toHaveBeenCalledWith(
220
+ mockCurrency,
221
+ mockPartyId,
222
+ {
223
+ type: "withdraw-transfer-instruction",
224
+ contract_id: mockContractId,
225
+ },
226
+ );
227
+ expect(mockedSignTransaction.signTransaction).toHaveBeenCalled();
228
+ expect(mockedGateway.submitTransferInstruction).toHaveBeenCalled();
229
+ });
230
+
231
+ it("should handle errors from prepareTransferInstruction", async () => {
232
+ // GIVEN
233
+ const error = new Error("Prepare failed");
234
+ mockedGateway.prepareTransferInstruction.mockRejectedValue(error);
235
+ const transferInstruction = buildTransferInstruction(mockSignerContext);
236
+
237
+ // WHEN & THEN
238
+ await expect(
239
+ transferInstruction(
240
+ mockCurrency,
241
+ mockDeviceId,
242
+ mockAccount,
243
+ mockPartyId,
244
+ mockContractId,
245
+ "accept-transfer-instruction",
246
+ ),
247
+ ).rejects.toThrow("Prepare failed");
248
+
249
+ expect(mockedGateway.prepareTransferInstruction).toHaveBeenCalled();
250
+ expect(mockedSignTransaction.signTransaction).not.toHaveBeenCalled();
251
+ expect(mockedGateway.submitTransferInstruction).not.toHaveBeenCalled();
252
+ });
253
+
254
+ it("should handle errors from signTransaction", async () => {
255
+ // GIVEN
256
+ const error = new Error("Sign failed");
257
+ mockedSignTransaction.signTransaction.mockRejectedValue(error);
258
+ const transferInstruction = buildTransferInstruction(mockSignerContext);
259
+
260
+ // WHEN & THEN
261
+ await expect(
262
+ transferInstruction(
263
+ mockCurrency,
264
+ mockDeviceId,
265
+ mockAccount,
266
+ mockPartyId,
267
+ mockContractId,
268
+ "accept-transfer-instruction",
269
+ ),
270
+ ).rejects.toThrow("Sign failed");
271
+
272
+ expect(mockedGateway.prepareTransferInstruction).toHaveBeenCalled();
273
+ expect(mockedSignTransaction.signTransaction).toHaveBeenCalled();
274
+ expect(mockedGateway.submitTransferInstruction).not.toHaveBeenCalled();
275
+ });
276
+
277
+ it("should handle errors from submitTransferInstruction", async () => {
278
+ // GIVEN
279
+ const error = new Error("Submit failed");
280
+ mockedGateway.submitTransferInstruction.mockRejectedValue(error);
281
+ const transferInstruction = buildTransferInstruction(mockSignerContext);
282
+
283
+ // WHEN & THEN
284
+ await expect(
285
+ transferInstruction(
286
+ mockCurrency,
287
+ mockDeviceId,
288
+ mockAccount,
289
+ mockPartyId,
290
+ mockContractId,
291
+ "accept-transfer-instruction",
292
+ ),
293
+ ).rejects.toThrow("Submit failed");
294
+
295
+ expect(mockedGateway.prepareTransferInstruction).toHaveBeenCalled();
296
+ expect(mockedSignTransaction.signTransaction).toHaveBeenCalled();
297
+ expect(mockedGateway.submitTransferInstruction).toHaveBeenCalled();
298
+ });
299
+ });
300
+ });
@@ -0,0 +1,36 @@
1
+ import { SignerContext } from "@ledgerhq/coin-framework/signer";
2
+ import type { Account } from "@ledgerhq/types-live";
3
+ import type { CryptoCurrency } from "@ledgerhq/types-cryptoassets";
4
+
5
+ import { prepareTransferInstruction, submitTransferInstruction } from "../network/gateway";
6
+ import { signTransaction } from "../common-logic/transaction/sign";
7
+ import type { CantonSigner } from "../types";
8
+
9
+ type TransferInstructionType =
10
+ | "accept-transfer-instruction"
11
+ | "reject-transfer-instruction"
12
+ | "withdraw-transfer-instruction";
13
+
14
+ export const buildTransferInstruction =
15
+ (signerContext: SignerContext<CantonSigner>) =>
16
+ async (
17
+ currency: CryptoCurrency,
18
+ deviceId: string,
19
+ account: Account,
20
+ partyId: string,
21
+ contractId: string,
22
+ type: TransferInstructionType,
23
+ reason?: string,
24
+ ) => {
25
+ const preparedTransaction = await prepareTransferInstruction(currency, partyId, {
26
+ type,
27
+ contract_id: contractId,
28
+ ...(reason && { reason }),
29
+ });
30
+
31
+ const { signature } = await signerContext(deviceId, async signer => {
32
+ return await signTransaction(signer, account.freshAddressPath, preparedTransaction);
33
+ });
34
+
35
+ await submitTransferInstruction(currency, partyId, preparedTransaction.serialized, signature);
36
+ };
@@ -311,6 +311,7 @@ describe("getTransactionStatus", () => {
311
311
  const accountWithManyUtxos = {
312
312
  ...mockAccount,
313
313
  cantonResources: {
314
+ pendingTransferProposals: [],
314
315
  instrumentUtxoCounts: {
315
316
  Amulet: TO_MANY_UTXOS_WARNING_COUNT + 1,
316
317
  },
@@ -21,6 +21,7 @@ import { buildSignOperation } from "./signOperation";
21
21
  import { makeGetAccountShape } from "./sync";
22
22
  import { updateTransaction } from "./updateTransaction";
23
23
  import { buildOnboardAccount, buildAuthorizePreapproval } from "./onboard";
24
+ import { buildTransferInstruction } from "./acceptOffer";
24
25
  import { assignToAccountRaw, assignFromAccountRaw } from "./serialization";
25
26
 
26
27
  export function createBridges(
@@ -39,6 +40,7 @@ export function createBridges(
39
40
 
40
41
  const onboardAccount = buildOnboardAccount(signerContext);
41
42
  const authorizePreapproval = buildAuthorizePreapproval(signerContext);
43
+ const transferInstruction = buildTransferInstruction(signerContext);
42
44
 
43
45
  const currencyBridge: CantonCurrencyBridge = {
44
46
  preload: () => Promise.resolve({}),
@@ -46,6 +48,7 @@ export function createBridges(
46
48
  scanAccounts,
47
49
  onboardAccount,
48
50
  authorizePreapproval,
51
+ transferInstruction,
49
52
  };
50
53
 
51
54
  const signOperation = buildSignOperation(signerContext);
@@ -6,7 +6,7 @@ import {
6
6
  type CantonResourcesRaw,
7
7
  } from "../types";
8
8
 
9
- function isCantonAccount(account: Account): account is CantonAccount {
9
+ export function isCantonAccount(account: Account): account is CantonAccount {
10
10
  return "cantonResources" in account;
11
11
  }
12
12
 
@@ -15,15 +15,17 @@ function isCantonAccountRaw(accountRaw: AccountRaw): accountRaw is CantonAccount
15
15
  }
16
16
 
17
17
  function toResourcesRaw(r: CantonResources): CantonResourcesRaw {
18
- const { instrumentUtxoCounts } = r;
18
+ const { instrumentUtxoCounts, pendingTransferProposals } = r;
19
19
  return {
20
20
  instrumentUtxoCounts,
21
+ pendingTransferProposals,
21
22
  };
22
23
  }
23
24
 
24
25
  function fromResourcesRaw(r: CantonResourcesRaw): CantonResources {
25
26
  return {
26
27
  instrumentUtxoCounts: r.instrumentUtxoCounts,
28
+ pendingTransferProposals: r.pendingTransferProposals,
27
29
  };
28
30
  }
29
31
 
@@ -4,12 +4,18 @@ import { encodeAccountId } from "@ledgerhq/coin-framework/account/index";
4
4
  import { GetAccountShape, mergeOps } from "@ledgerhq/coin-framework/bridge/jsHelpers";
5
5
  import { encodeOperationId } from "@ledgerhq/coin-framework/operation";
6
6
  import { SignerContext } from "@ledgerhq/coin-framework/signer";
7
- import { getLedgerEnd, getOperations, type OperationInfo } from "../network/gateway";
7
+ import {
8
+ getLedgerEnd,
9
+ getOperations,
10
+ type OperationInfo,
11
+ getPendingTransferProposals,
12
+ } from "../network/gateway";
8
13
  import { getBalance, type CantonBalance } from "../common-logic/account/getBalance";
9
14
  import coinConfig from "../config";
10
15
  import resolver from "../signer";
11
16
  import { CantonAccount, CantonSigner } from "../types";
12
- import { isAccountOnboarded, isCantonCoinPreapproved } from "./onboard";
17
+ import { isAccountOnboarded } from "./onboard";
18
+ import { isCantonAccountEmpty } from "../helpers";
13
19
 
14
20
  const txInfoToOperationAdapter =
15
21
  (accountId: string, partyId: string) =>
@@ -107,6 +113,9 @@ export function makeGetAccountShape(
107
113
 
108
114
  const { nativeInstrumentId } = coinConfig.getCoinConfig(currency);
109
115
  const balances = xpubOrAddress ? await getBalance(currency, xpubOrAddress) : [];
116
+ const pendingTransferProposals = xpubOrAddress
117
+ ? await getPendingTransferProposals(currency, xpubOrAddress)
118
+ : [];
110
119
 
111
120
  const balancesData = (balances || []).reduce(
112
121
  (acc, balance) => {
@@ -141,10 +150,15 @@ export function makeGetAccountShape(
141
150
  operations = mergeOps(oldOperations, newOperations);
142
151
  }
143
152
 
144
- const isPreapproved = xpubOrAddress
145
- ? await isCantonCoinPreapproved(currency, xpubOrAddress)
146
- : false;
147
- const used = isPreapproved && totalBalance.gt(0);
153
+ const used = !isCantonAccountEmpty({
154
+ operationsCount: operations.length,
155
+ balance: totalBalance,
156
+ subAccounts: initialAccount?.subAccounts ?? [],
157
+ cantonResources: {
158
+ instrumentUtxoCounts,
159
+ pendingTransferProposals,
160
+ },
161
+ });
148
162
 
149
163
  const blockHeight = await getLedgerEnd(currency);
150
164
 
@@ -169,6 +183,7 @@ export function makeGetAccountShape(
169
183
  used,
170
184
  cantonResources: {
171
185
  instrumentUtxoCounts,
186
+ pendingTransferProposals,
172
187
  },
173
188
  };
174
189