@ledgerhq/coin-sui 0.14.1-nightly.0 → 0.15.0-nightly.2

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 (57) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/CHANGELOG.md +13 -0
  3. package/lib/api/index.js +7 -2
  4. package/lib/api/index.js.map +1 -1
  5. package/lib/api/index.test.js +1 -1
  6. package/lib/api/index.test.js.map +1 -1
  7. package/lib/logic/craftTransaction.d.ts +1 -1
  8. package/lib/logic/craftTransaction.d.ts.map +1 -1
  9. package/lib/logic/craftTransaction.integration.test.js +38 -0
  10. package/lib/logic/craftTransaction.integration.test.js.map +1 -1
  11. package/lib/logic/craftTransaction.js +3 -4
  12. package/lib/logic/craftTransaction.js.map +1 -1
  13. package/lib/logic/craftTransaction.test.js +5 -5
  14. package/lib/logic/craftTransaction.test.js.map +1 -1
  15. package/lib/logic/estimateFees.integration.test.js +1 -1
  16. package/lib/network/index.d.ts +1 -1
  17. package/lib/network/sdk.d.ts +3 -2
  18. package/lib/network/sdk.d.ts.map +1 -1
  19. package/lib/network/sdk.integration.test.js +2 -2
  20. package/lib/network/sdk.integration.test.js.map +1 -1
  21. package/lib/network/sdk.js +10 -3
  22. package/lib/network/sdk.js.map +1 -1
  23. package/lib/types/model.d.ts +3 -0
  24. package/lib/types/model.d.ts.map +1 -1
  25. package/lib-es/api/index.js +7 -2
  26. package/lib-es/api/index.js.map +1 -1
  27. package/lib-es/api/index.test.js +1 -1
  28. package/lib-es/api/index.test.js.map +1 -1
  29. package/lib-es/logic/craftTransaction.d.ts +1 -1
  30. package/lib-es/logic/craftTransaction.d.ts.map +1 -1
  31. package/lib-es/logic/craftTransaction.integration.test.js +38 -0
  32. package/lib-es/logic/craftTransaction.integration.test.js.map +1 -1
  33. package/lib-es/logic/craftTransaction.js +3 -4
  34. package/lib-es/logic/craftTransaction.js.map +1 -1
  35. package/lib-es/logic/craftTransaction.test.js +5 -5
  36. package/lib-es/logic/craftTransaction.test.js.map +1 -1
  37. package/lib-es/logic/estimateFees.integration.test.js +1 -1
  38. package/lib-es/network/index.d.ts +1 -1
  39. package/lib-es/network/sdk.d.ts +3 -2
  40. package/lib-es/network/sdk.d.ts.map +1 -1
  41. package/lib-es/network/sdk.integration.test.js +2 -2
  42. package/lib-es/network/sdk.integration.test.js.map +1 -1
  43. package/lib-es/network/sdk.js +10 -3
  44. package/lib-es/network/sdk.js.map +1 -1
  45. package/lib-es/types/model.d.ts +3 -0
  46. package/lib-es/types/model.d.ts.map +1 -1
  47. package/package.json +4 -4
  48. package/src/api/index.test.ts +1 -1
  49. package/src/api/index.ts +7 -2
  50. package/src/logic/craftTransaction.integration.test.ts +48 -0
  51. package/src/logic/craftTransaction.test.ts +41 -25
  52. package/src/logic/craftTransaction.ts +25 -20
  53. package/src/logic/estimateFees.integration.test.ts +1 -1
  54. package/src/network/sdk.integration.test.ts +2 -2
  55. package/src/network/sdk.ts +22 -4
  56. package/src/types/model.ts +4 -0
  57. package/index.d.ts +0 -0
@@ -32,6 +32,7 @@ describe("craftTransaction", () => {
32
32
 
33
33
  expect(result).toBeDefined();
34
34
  expect(result.unsigned).toBeInstanceOf(Uint8Array);
35
+ expect(result.objects).toBeUndefined();
35
36
 
36
37
  const resultCoinTypes = await extractCoinTypeFromUnsignedTx(result.unsigned);
37
38
  expect(resultCoinTypes).toEqual(expect.arrayContaining([expect.stringContaining("sui")]));
@@ -56,6 +57,53 @@ describe("craftTransaction", () => {
56
57
 
57
58
  expect(result).toBeDefined();
58
59
  expect(result.unsigned).toBeInstanceOf(Uint8Array);
60
+ expect(result.objects).toBeUndefined();
61
+
62
+ const resultCoinTypes = await extractCoinTypeFromUnsignedTx(result.unsigned);
63
+ expect(resultCoinTypes).toEqual(expect.arrayContaining([expect.stringContaining("usdt")]));
64
+ }, 15000);
65
+
66
+ it("should craft a native SUI send transaction, returning serialized objects when requested", async () => {
67
+ const transactionIntent: TransactionIntent = {
68
+ sender: SENDER,
69
+ recipient: RECIPIENT,
70
+ amount: BigInt(1000),
71
+ type: "send",
72
+ asset: { type: "native" },
73
+ };
74
+
75
+ const result = await craftTransaction(transactionIntent, true);
76
+
77
+ expect(result).toBeDefined();
78
+ expect(result.unsigned).toBeInstanceOf(Uint8Array);
79
+ expect(result.objects).toBeDefined();
80
+ expect(result.objects?.length).toBeGreaterThan(0);
81
+
82
+ const resultCoinTypes = await extractCoinTypeFromUnsignedTx(result.unsigned);
83
+ expect(resultCoinTypes).toEqual(expect.arrayContaining([expect.stringContaining("sui")]));
84
+ }, 15000);
85
+
86
+ it("should craft a token send transaction, returning serialized objects when requested", async () => {
87
+ const coinType =
88
+ "0x375f70cf2ae4c00bf37117d0c85a2c71545e6ee05c4a5c7d282cd66a4504b068::usdt::USDT";
89
+
90
+ const transactionIntent: TransactionIntent = {
91
+ sender: SENDER,
92
+ recipient: RECIPIENT,
93
+ amount: BigInt(1000),
94
+ type: "send",
95
+ asset: {
96
+ type: "token",
97
+ assetReference: coinType,
98
+ },
99
+ };
100
+
101
+ const result = await craftTransaction(transactionIntent, true);
102
+
103
+ expect(result).toBeDefined();
104
+ expect(result.unsigned).toBeInstanceOf(Uint8Array);
105
+ expect(result.objects).toBeDefined();
106
+ expect(result.objects?.length).toBeGreaterThan(0);
59
107
 
60
108
  const resultCoinTypes = await extractCoinTypeFromUnsignedTx(result.unsigned);
61
109
  expect(resultCoinTypes).toEqual(expect.arrayContaining([expect.stringContaining("usdt")]));
@@ -10,7 +10,7 @@ describe("craftTransaction", () => {
10
10
 
11
11
  beforeEach(() => {
12
12
  jest.clearAllMocks();
13
- mockCreateTransaction.mockResolvedValue(mockUnsignedTx);
13
+ mockCreateTransaction.mockResolvedValue({ unsigned: mockUnsignedTx });
14
14
  });
15
15
 
16
16
  it("should create a transaction with correct parameters", async () => {
@@ -27,12 +27,16 @@ describe("craftTransaction", () => {
27
27
  asset: { type: "native" },
28
28
  });
29
29
 
30
- expect(mockCreateTransaction).toHaveBeenCalledWith(sender, {
31
- amount: new BigNumber(amount.toString()),
32
- recipient,
33
- mode: type,
34
- coinType: "0x2::sui::SUI",
35
- });
30
+ expect(mockCreateTransaction).toHaveBeenCalledWith(
31
+ sender,
32
+ {
33
+ amount: new BigNumber(amount.toString()),
34
+ recipient,
35
+ mode: type,
36
+ coinType: "0x2::sui::SUI",
37
+ },
38
+ false,
39
+ );
36
40
  expect(result).toEqual({ unsigned: mockUnsignedTx });
37
41
  });
38
42
 
@@ -50,12 +54,16 @@ describe("craftTransaction", () => {
50
54
  asset: { type: "native" },
51
55
  });
52
56
 
53
- expect(mockCreateTransaction).toHaveBeenCalledWith(sender, {
54
- amount: new BigNumber(amount.toString()),
55
- recipient,
56
- mode: type,
57
- coinType: "0x2::sui::SUI",
58
- });
57
+ expect(mockCreateTransaction).toHaveBeenCalledWith(
58
+ sender,
59
+ {
60
+ amount: new BigNumber(amount.toString()),
61
+ recipient,
62
+ mode: type,
63
+ coinType: "0x2::sui::SUI",
64
+ },
65
+ false,
66
+ );
59
67
  expect(result).toEqual({ unsigned: mockUnsignedTx });
60
68
  });
61
69
 
@@ -73,12 +81,16 @@ describe("craftTransaction", () => {
73
81
  asset: { type: "native" },
74
82
  });
75
83
 
76
- expect(mockCreateTransaction).toHaveBeenCalledWith(sender, {
77
- amount: new BigNumber(amount.toString()),
78
- recipient,
79
- mode: type,
80
- coinType: "0x2::sui::SUI",
81
- });
84
+ expect(mockCreateTransaction).toHaveBeenCalledWith(
85
+ sender,
86
+ {
87
+ amount: new BigNumber(amount.toString()),
88
+ recipient,
89
+ mode: type,
90
+ coinType: "0x2::sui::SUI",
91
+ },
92
+ false,
93
+ );
82
94
  expect(result).toEqual({ unsigned: mockUnsignedTx });
83
95
  });
84
96
 
@@ -96,12 +108,16 @@ describe("craftTransaction", () => {
96
108
  asset: { type: "native" },
97
109
  });
98
110
 
99
- expect(mockCreateTransaction).toHaveBeenCalledWith(sender, {
100
- amount: new BigNumber(amount.toString()),
101
- recipient,
102
- mode: type,
103
- coinType: "0x2::sui::SUI",
104
- });
111
+ expect(mockCreateTransaction).toHaveBeenCalledWith(
112
+ sender,
113
+ {
114
+ amount: new BigNumber(amount.toString()),
115
+ recipient,
116
+ mode: type,
117
+ coinType: "0x2::sui::SUI",
118
+ },
119
+ false,
120
+ );
105
121
  expect(result).toEqual({ unsigned: mockUnsignedTx });
106
122
  });
107
123
 
@@ -4,28 +4,33 @@ import type { SuiTransactionMode, CoreTransaction } from "../types";
4
4
  import suiAPI from "../network";
5
5
  import { DEFAULT_COIN_TYPE } from "../network/sdk";
6
6
 
7
- export async function craftTransaction({
8
- amount,
9
- asset,
10
- recipient,
11
- sender,
12
- type,
13
- ...extra
14
- }: TransactionIntent & {
15
- useAllAmount?: boolean;
16
- stakedSuiId?: string;
17
- }): Promise<CoreTransaction> {
7
+ export async function craftTransaction(
8
+ {
9
+ amount,
10
+ asset,
11
+ recipient,
12
+ sender,
13
+ type,
14
+ ...extra
15
+ }: TransactionIntent & {
16
+ useAllAmount?: boolean;
17
+ stakedSuiId?: string;
18
+ },
19
+ withObjects: boolean = false,
20
+ ): Promise<CoreTransaction> {
18
21
  let coinType = DEFAULT_COIN_TYPE;
19
22
  if (asset.type === "token" && asset.assetReference) {
20
23
  coinType = asset.assetReference;
21
24
  }
22
- const unsigned = await suiAPI.createTransaction(sender, {
23
- amount: BigNumber(amount.toString()),
24
- coinType,
25
- mode: type as SuiTransactionMode,
26
- recipient,
27
- ...extra,
28
- });
29
-
30
- return { unsigned };
25
+ return suiAPI.createTransaction(
26
+ sender,
27
+ {
28
+ amount: BigNumber(amount.toString()),
29
+ coinType,
30
+ mode: type as SuiTransactionMode,
31
+ recipient,
32
+ ...extra,
33
+ },
34
+ withObjects,
35
+ );
31
36
  }
@@ -3,7 +3,7 @@ import { getFullnodeUrl } from "@mysten/sui/client";
3
3
  import coinConfig from "../config";
4
4
  import { estimateFees } from "./estimateFees";
5
5
 
6
- const SENDER = "0x6e143fe0a8ca010a86580dafac44298e5b1b7d73efc345356a59a15f0d7824f0";
6
+ const SENDER = "0xad79719ac7edb44f6e253f1f771e8291e281a6aaf1e4789b52bf85336f525e8e";
7
7
  const RECIPIENT = "0x33444cf803c690db96527cec67e3c9ab512596f4ba2d4eace43f0b4f716e0164";
8
8
 
9
9
  describe("estimateFees", () => {
@@ -155,14 +155,14 @@ describe("SUI SDK Integration tests", () => {
155
155
  recipient: "0x33444cf803c690db96527cec67e3c9ab512596f4ba2d4eace43f0b4f716e0164",
156
156
  errors: {},
157
157
  };
158
- const tx = await createTransaction(address, transaction);
158
+ const { unsigned: tx } = await createTransaction(address, transaction);
159
159
  expect(tx).toBeInstanceOf(Uint8Array);
160
160
  });
161
161
  });
162
162
 
163
163
  describe("paymentInfo", () => {
164
164
  test("paymentInfo should return gas budget and fees", async () => {
165
- const sender = "0x6e143fe0a8ca010a86580dafac44298e5b1b7d73efc345356a59a15f0d7824f0";
165
+ const sender = "0xad79719ac7edb44f6e253f1f771e8291e281a6aaf1e4789b52bf85336f525e8e";
166
166
  const fakeTransaction = {
167
167
  mode: "send" as const,
168
168
  family: "sui" as const,
@@ -34,13 +34,19 @@ import uniqBy from "lodash/unionBy";
34
34
  import { encodeOperationId } from "@ledgerhq/coin-framework/operation";
35
35
  import { log } from "@ledgerhq/logs";
36
36
  import { makeLRUCache, minutes } from "@ledgerhq/live-network/cache";
37
- import type { Transaction as TransactionType, SuiValidator, CreateExtrinsicArg } from "../types";
37
+ import type {
38
+ Transaction as TransactionType,
39
+ SuiValidator,
40
+ CreateExtrinsicArg,
41
+ CoreTransaction,
42
+ } from "../types";
38
43
  import { ensureAddressFormat } from "../utils";
39
44
  import coinConfig from "../config";
40
45
  import { getEnv } from "@ledgerhq/live-env";
41
46
  import { SUI_SYSTEM_STATE_OBJECT_ID } from "@mysten/sui/utils";
42
47
  import { getCurrentSuiPreloadData } from "../bridge/preload";
43
48
  import { ONE_SUI } from "../constants";
49
+ import { getInputObjects } from "@mysten/signers/ledger";
44
50
 
45
51
  const apiMap: Record<string, SuiClient> = {};
46
52
  type AsyncApiFunction<T> = (api: SuiClient) => Promise<T>;
@@ -732,10 +738,15 @@ export const getCoinsForAmount = async (
732
738
  *
733
739
  * @param address - The sender's address
734
740
  * @param transaction - The transaction details including recipient, amount, and coin type
741
+ * @param withObjects - Return serialized input objects used in the transaction
735
742
  * @returns Promise<TransactionBlock> - A built transaction block ready for execution
736
743
  *
737
744
  */
738
- export const createTransaction = async (address: string, transaction: CreateExtrinsicArg) =>
745
+ export const createTransaction = async (
746
+ address: string,
747
+ transaction: CreateExtrinsicArg,
748
+ withObjects: boolean = false,
749
+ ): Promise<CoreTransaction> =>
739
750
  withApi(async api => {
740
751
  const tx = new Transaction();
741
752
  tx.setSender(ensureAddressFormat(address));
@@ -800,7 +811,14 @@ export const createTransaction = async (address: string, transaction: CreateExtr
800
811
  }
801
812
  }
802
813
 
803
- return tx.build({ client: api });
814
+ const serialized = await tx.build({ client: api });
815
+
816
+ if (withObjects) {
817
+ const { bcsObjects } = await getInputObjects(tx, api);
818
+ return { unsigned: serialized, objects: bcsObjects as Uint8Array[] };
819
+ }
820
+
821
+ return { unsigned: serialized };
804
822
  });
805
823
 
806
824
  /**
@@ -808,7 +826,7 @@ export const createTransaction = async (address: string, transaction: CreateExtr
808
826
  */
809
827
  export const paymentInfo = async (sender: string, fakeTransaction: TransactionType) =>
810
828
  withApi(async api => {
811
- const txb = await createTransaction(sender, fakeTransaction);
829
+ const { unsigned: txb } = await createTransaction(sender, fakeTransaction);
812
830
  const dryRunTxResponse = await api.dryRunTransactionBlock({ transactionBlock: txb });
813
831
  const fees = getTotalGasUsed(dryRunTxResponse.effects);
814
832
 
@@ -3,5 +3,9 @@ export type SuiOperationMode = "send";
3
3
  export type AccountInfoResponse = Record<string, string>;
4
4
 
5
5
  export type CoreTransaction = {
6
+ /** The transaction in a serialized format, ready to be signed. */
6
7
  unsigned: Uint8Array;
8
+
9
+ /** The input objects referenced in the transaction, in serialized form.. */
10
+ objects?: Uint8Array[];
7
11
  };
package/index.d.ts DELETED
File without changes