@ledgerhq/live-common 34.52.0-nightly.2 → 34.52.0-nightly.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.
- package/lib/dada-client/hooks/useAssetsData.d.ts +4 -10
- package/lib/dada-client/hooks/useAssetsData.d.ts.map +1 -1
- package/lib/dada-client/hooks/useAssetsData.js +2 -1
- package/lib/dada-client/hooks/useAssetsData.js.map +1 -1
- package/lib/dada-client/state-manager/api.d.ts.map +1 -1
- package/lib/dada-client/state-manager/api.js +3 -0
- package/lib/dada-client/state-manager/api.js.map +1 -1
- package/lib/dada-client/state-manager/types.d.ts +1 -0
- package/lib/dada-client/state-manager/types.d.ts.map +1 -1
- package/lib/e2e/deviceInteraction/TouchDeviceSimulator.d.ts.map +1 -1
- package/lib/e2e/deviceInteraction/TouchDeviceSimulator.js +13 -5
- package/lib/e2e/deviceInteraction/TouchDeviceSimulator.js.map +1 -1
- package/lib/e2e/speculos.d.ts.map +1 -1
- package/lib/e2e/speculos.js +2 -1
- package/lib/e2e/speculos.js.map +1 -1
- package/lib/generated/deviceTransactionConfig.d.ts +1 -1
- package/lib/hooks/useDeviceTransactionConfig.d.ts +19 -0
- package/lib/hooks/useDeviceTransactionConfig.d.ts.map +1 -0
- package/lib/hooks/useDeviceTransactionConfig.js +45 -0
- package/lib/hooks/useDeviceTransactionConfig.js.map +1 -0
- package/lib/transaction/deviceTransactionConfig.d.ts +1 -1
- package/lib/transaction/deviceTransactionConfig.d.ts.map +1 -1
- package/lib/transaction/deviceTransactionConfig.js +2 -2
- package/lib/transaction/deviceTransactionConfig.js.map +1 -1
- package/lib-es/dada-client/hooks/useAssetsData.d.ts +4 -10
- package/lib-es/dada-client/hooks/useAssetsData.d.ts.map +1 -1
- package/lib-es/dada-client/hooks/useAssetsData.js +2 -1
- package/lib-es/dada-client/hooks/useAssetsData.js.map +1 -1
- package/lib-es/dada-client/state-manager/api.d.ts.map +1 -1
- package/lib-es/dada-client/state-manager/api.js +3 -0
- package/lib-es/dada-client/state-manager/api.js.map +1 -1
- package/lib-es/dada-client/state-manager/types.d.ts +1 -0
- package/lib-es/dada-client/state-manager/types.d.ts.map +1 -1
- package/lib-es/e2e/deviceInteraction/TouchDeviceSimulator.d.ts.map +1 -1
- package/lib-es/e2e/deviceInteraction/TouchDeviceSimulator.js +13 -5
- package/lib-es/e2e/deviceInteraction/TouchDeviceSimulator.js.map +1 -1
- package/lib-es/e2e/speculos.d.ts.map +1 -1
- package/lib-es/e2e/speculos.js +2 -1
- package/lib-es/e2e/speculos.js.map +1 -1
- package/lib-es/generated/deviceTransactionConfig.d.ts +1 -1
- package/lib-es/hooks/useDeviceTransactionConfig.d.ts +19 -0
- package/lib-es/hooks/useDeviceTransactionConfig.d.ts.map +1 -0
- package/lib-es/hooks/useDeviceTransactionConfig.js +41 -0
- package/lib-es/hooks/useDeviceTransactionConfig.js.map +1 -0
- package/lib-es/transaction/deviceTransactionConfig.d.ts +1 -1
- package/lib-es/transaction/deviceTransactionConfig.d.ts.map +1 -1
- package/lib-es/transaction/deviceTransactionConfig.js +2 -2
- package/lib-es/transaction/deviceTransactionConfig.js.map +1 -1
- package/package.json +48 -48
- package/src/account/serialization.test.ts +1 -0
- package/src/dada-client/hooks/useAssetsData.ts +4 -8
- package/src/dada-client/state-manager/api.ts +3 -0
- package/src/dada-client/state-manager/types.ts +1 -0
- package/src/e2e/deviceInteraction/TouchDeviceSimulator.ts +12 -5
- package/src/e2e/speculos.ts +6 -1
- package/src/families/bitcoin/satstack.test.ts +1 -0
- package/src/hooks/useDeviceTransactionConfig.test.tsx +200 -0
- package/src/hooks/useDeviceTransactionConfig.ts +65 -0
- package/src/transaction/deviceTransactionConfig.ts +3 -3
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jest-environment jsdom
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { TextDecoder, TextEncoder } from "util";
|
|
6
|
+
|
|
7
|
+
// Polyfill for TextDecoder/TextEncoder required by Cardano dependencies
|
|
8
|
+
global.TextDecoder = TextDecoder as any;
|
|
9
|
+
global.TextEncoder = TextEncoder as any;
|
|
10
|
+
|
|
11
|
+
// Mock the deviceTransactionConfig module before importing anything else
|
|
12
|
+
jest.mock("../transaction/deviceTransactionConfig", () => ({
|
|
13
|
+
getDeviceTransactionConfig: jest.fn(),
|
|
14
|
+
}));
|
|
15
|
+
|
|
16
|
+
import { renderHook, waitFor } from "@testing-library/react";
|
|
17
|
+
import { useDeviceTransactionConfig } from "./useDeviceTransactionConfig";
|
|
18
|
+
import { getDeviceTransactionConfig } from "../transaction/deviceTransactionConfig";
|
|
19
|
+
import { Account } from "@ledgerhq/types-live";
|
|
20
|
+
import { Transaction, TransactionStatus } from "../generated/types";
|
|
21
|
+
import BigNumber from "bignumber.js";
|
|
22
|
+
import { getCryptoCurrencyById } from "../currencies/index";
|
|
23
|
+
|
|
24
|
+
const mockGetDeviceTransactionConfig = getDeviceTransactionConfig as jest.MockedFunction<
|
|
25
|
+
typeof getDeviceTransactionConfig
|
|
26
|
+
>;
|
|
27
|
+
|
|
28
|
+
const btc = getCryptoCurrencyById("bitcoin");
|
|
29
|
+
|
|
30
|
+
describe("useDeviceTransactionConfig", () => {
|
|
31
|
+
const mockAccount: Account = {
|
|
32
|
+
type: "Account",
|
|
33
|
+
id: "test-account-id",
|
|
34
|
+
seedIdentifier: "seed-id",
|
|
35
|
+
derivationMode: "" as const,
|
|
36
|
+
index: 0,
|
|
37
|
+
freshAddress: "test-address",
|
|
38
|
+
freshAddressPath: "44'/0'/0'/0/0",
|
|
39
|
+
used: true,
|
|
40
|
+
balance: new BigNumber(1000000),
|
|
41
|
+
spendableBalance: new BigNumber(1000000),
|
|
42
|
+
creationDate: new Date(),
|
|
43
|
+
blockHeight: 100,
|
|
44
|
+
currency: btc,
|
|
45
|
+
operationsCount: 0,
|
|
46
|
+
operations: [],
|
|
47
|
+
pendingOperations: [],
|
|
48
|
+
lastSyncDate: new Date(),
|
|
49
|
+
balanceHistoryCache: {
|
|
50
|
+
HOUR: { latestDate: null, balances: [] },
|
|
51
|
+
DAY: { latestDate: null, balances: [] },
|
|
52
|
+
WEEK: { latestDate: null, balances: [] },
|
|
53
|
+
},
|
|
54
|
+
swapHistory: [],
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const mockTransaction: Transaction = {
|
|
58
|
+
family: "bitcoin" as any,
|
|
59
|
+
amount: new BigNumber(100),
|
|
60
|
+
recipient: "test-recipient",
|
|
61
|
+
useAllAmount: false,
|
|
62
|
+
} as Transaction;
|
|
63
|
+
|
|
64
|
+
const mockStatus: TransactionStatus = {
|
|
65
|
+
errors: {},
|
|
66
|
+
warnings: {},
|
|
67
|
+
estimatedFees: new BigNumber(10),
|
|
68
|
+
amount: new BigNumber(100),
|
|
69
|
+
totalSpent: new BigNumber(110),
|
|
70
|
+
} as TransactionStatus;
|
|
71
|
+
|
|
72
|
+
beforeEach(() => {
|
|
73
|
+
jest.clearAllMocks();
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("should load device transaction config fields successfully", async () => {
|
|
77
|
+
const mockFields = [
|
|
78
|
+
{ type: "amount", label: "Amount" },
|
|
79
|
+
{ type: "fees", label: "Fees" },
|
|
80
|
+
];
|
|
81
|
+
|
|
82
|
+
mockGetDeviceTransactionConfig.mockResolvedValue(mockFields as any);
|
|
83
|
+
|
|
84
|
+
const { result } = renderHook(() =>
|
|
85
|
+
useDeviceTransactionConfig({
|
|
86
|
+
account: mockAccount,
|
|
87
|
+
parentAccount: null,
|
|
88
|
+
transaction: mockTransaction,
|
|
89
|
+
status: mockStatus,
|
|
90
|
+
}),
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
// Initially loading
|
|
94
|
+
expect(result.current.loading).toBe(true);
|
|
95
|
+
expect(result.current.fields).toEqual([]);
|
|
96
|
+
|
|
97
|
+
// Wait for async operation
|
|
98
|
+
await waitFor(() => {
|
|
99
|
+
expect(result.current.loading).toBe(false);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
expect(result.current.fields).toEqual(mockFields);
|
|
103
|
+
expect(mockGetDeviceTransactionConfig).toHaveBeenCalledWith({
|
|
104
|
+
account: mockAccount,
|
|
105
|
+
parentAccount: null,
|
|
106
|
+
transaction: mockTransaction,
|
|
107
|
+
status: mockStatus,
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("should handle errors gracefully", async () => {
|
|
112
|
+
const consoleErrorSpy = jest.spyOn(console, "error").mockImplementation();
|
|
113
|
+
mockGetDeviceTransactionConfig.mockRejectedValue(new Error("Test error"));
|
|
114
|
+
|
|
115
|
+
const { result } = renderHook(() =>
|
|
116
|
+
useDeviceTransactionConfig({
|
|
117
|
+
account: mockAccount,
|
|
118
|
+
parentAccount: null,
|
|
119
|
+
transaction: mockTransaction,
|
|
120
|
+
status: mockStatus,
|
|
121
|
+
}),
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
expect(result.current.loading).toBe(true);
|
|
125
|
+
|
|
126
|
+
await waitFor(() => {
|
|
127
|
+
expect(result.current.loading).toBe(false);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
expect(result.current.fields).toEqual([]);
|
|
131
|
+
expect(consoleErrorSpy).toHaveBeenCalledWith(
|
|
132
|
+
"Failed to load device transaction config:",
|
|
133
|
+
expect.any(Error),
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
consoleErrorSpy.mockRestore();
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("should reload fields when dependencies change", async () => {
|
|
140
|
+
const mockFields1 = [{ type: "amount", label: "Amount 1" }];
|
|
141
|
+
const mockFields2 = [{ type: "amount", label: "Amount 2" }];
|
|
142
|
+
|
|
143
|
+
mockGetDeviceTransactionConfig
|
|
144
|
+
.mockResolvedValueOnce(mockFields1 as any)
|
|
145
|
+
.mockResolvedValueOnce(mockFields2 as any);
|
|
146
|
+
|
|
147
|
+
const { result, rerender } = renderHook(
|
|
148
|
+
({ transaction }) =>
|
|
149
|
+
useDeviceTransactionConfig({
|
|
150
|
+
account: mockAccount,
|
|
151
|
+
parentAccount: null,
|
|
152
|
+
transaction,
|
|
153
|
+
status: mockStatus,
|
|
154
|
+
}),
|
|
155
|
+
{
|
|
156
|
+
initialProps: { transaction: mockTransaction },
|
|
157
|
+
},
|
|
158
|
+
);
|
|
159
|
+
|
|
160
|
+
await waitFor(() => {
|
|
161
|
+
expect(result.current.loading).toBe(false);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
expect(result.current.fields).toEqual(mockFields1);
|
|
165
|
+
|
|
166
|
+
// Change transaction
|
|
167
|
+
const newTransaction = {
|
|
168
|
+
...mockTransaction,
|
|
169
|
+
amount: new BigNumber(200),
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
rerender({ transaction: newTransaction });
|
|
173
|
+
|
|
174
|
+
await waitFor(() => {
|
|
175
|
+
expect(result.current.loading).toBe(false);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
expect(result.current.fields).toEqual(mockFields2);
|
|
179
|
+
expect(mockGetDeviceTransactionConfig).toHaveBeenCalledTimes(2);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it("should cleanup on unmount", async () => {
|
|
183
|
+
const mockFields = [{ type: "amount", label: "Amount" }];
|
|
184
|
+
mockGetDeviceTransactionConfig.mockResolvedValue(mockFields as any);
|
|
185
|
+
|
|
186
|
+
const { unmount } = renderHook(() =>
|
|
187
|
+
useDeviceTransactionConfig({
|
|
188
|
+
account: mockAccount,
|
|
189
|
+
parentAccount: null,
|
|
190
|
+
transaction: mockTransaction,
|
|
191
|
+
status: mockStatus,
|
|
192
|
+
}),
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
unmount();
|
|
196
|
+
|
|
197
|
+
// Should not throw any errors
|
|
198
|
+
expect(mockGetDeviceTransactionConfig).toHaveBeenCalled();
|
|
199
|
+
});
|
|
200
|
+
});
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { useState, useEffect } from "react";
|
|
2
|
+
import { Account, AccountLike } from "@ledgerhq/types-live";
|
|
3
|
+
import { Transaction, TransactionStatus } from "../generated/types";
|
|
4
|
+
import {
|
|
5
|
+
getDeviceTransactionConfig,
|
|
6
|
+
DeviceTransactionField,
|
|
7
|
+
} from "../transaction/deviceTransactionConfig";
|
|
8
|
+
|
|
9
|
+
type UseDeviceTransactionConfigParams = {
|
|
10
|
+
account: AccountLike;
|
|
11
|
+
parentAccount: Account | null | undefined;
|
|
12
|
+
transaction: Transaction;
|
|
13
|
+
status: TransactionStatus;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Hook to fetch device transaction configuration fields asynchronously.
|
|
18
|
+
* This anticipates the future async nature of crypto assets store operations.
|
|
19
|
+
*/
|
|
20
|
+
export function useDeviceTransactionConfig({
|
|
21
|
+
account,
|
|
22
|
+
parentAccount,
|
|
23
|
+
transaction,
|
|
24
|
+
status,
|
|
25
|
+
}: UseDeviceTransactionConfigParams): {
|
|
26
|
+
fields: DeviceTransactionField[];
|
|
27
|
+
loading: boolean;
|
|
28
|
+
} {
|
|
29
|
+
const [fields, setFields] = useState<DeviceTransactionField[]>([]);
|
|
30
|
+
const [loading, setLoading] = useState(true);
|
|
31
|
+
|
|
32
|
+
useEffect(() => {
|
|
33
|
+
let mounted = true;
|
|
34
|
+
|
|
35
|
+
async function loadFields() {
|
|
36
|
+
try {
|
|
37
|
+
setLoading(true);
|
|
38
|
+
const result = await getDeviceTransactionConfig({
|
|
39
|
+
account,
|
|
40
|
+
parentAccount,
|
|
41
|
+
transaction,
|
|
42
|
+
status,
|
|
43
|
+
});
|
|
44
|
+
if (mounted) {
|
|
45
|
+
setFields(result);
|
|
46
|
+
setLoading(false);
|
|
47
|
+
}
|
|
48
|
+
} catch (error) {
|
|
49
|
+
console.error("Failed to load device transaction config:", error);
|
|
50
|
+
if (mounted) {
|
|
51
|
+
setFields([]);
|
|
52
|
+
setLoading(false);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
loadFields();
|
|
58
|
+
|
|
59
|
+
return () => {
|
|
60
|
+
mounted = false;
|
|
61
|
+
};
|
|
62
|
+
}, [account, parentAccount, transaction, status]);
|
|
63
|
+
|
|
64
|
+
return { fields, loading };
|
|
65
|
+
}
|
|
@@ -6,14 +6,14 @@ import { getMainAccount } from "../account";
|
|
|
6
6
|
import type { Account, AccountLike } from "@ledgerhq/types-live";
|
|
7
7
|
|
|
8
8
|
export type DeviceTransactionField = CommonDeviceTransactionField | ExtraDeviceTransactionField;
|
|
9
|
-
export function getDeviceTransactionConfig(arg: {
|
|
9
|
+
export async function getDeviceTransactionConfig(arg: {
|
|
10
10
|
account: AccountLike;
|
|
11
11
|
parentAccount: Account | null | undefined;
|
|
12
12
|
transaction: Transaction;
|
|
13
13
|
status: TransactionStatus;
|
|
14
|
-
}): Array<DeviceTransactionField
|
|
14
|
+
}): Promise<Array<DeviceTransactionField>> {
|
|
15
15
|
const mainAccount = getMainAccount(arg.account, arg.parentAccount);
|
|
16
16
|
const f = perFamily[mainAccount.currency.family];
|
|
17
17
|
if (!f) return [];
|
|
18
|
-
return f(arg);
|
|
18
|
+
return await f(arg);
|
|
19
19
|
}
|