From 1b4cbc072087c02c945a8472c809bfb019788333 Mon Sep 17 00:00:00 2001 From: Kevin Le Seigle Date: Tue, 7 Jan 2025 15:20:17 +0100 Subject: [PATCH] fix: live wallet store to export account names and starred accounts --- .changeset/stale-panthers-smash.md | 7 +++++ libs/live-wallet/src/store.test.ts | 49 ++++++++++++++++++++++++++++++ libs/live-wallet/src/store.ts | 26 +++++++++++----- 3 files changed, 75 insertions(+), 7 deletions(-) create mode 100644 .changeset/stale-panthers-smash.md diff --git a/.changeset/stale-panthers-smash.md b/.changeset/stale-panthers-smash.md new file mode 100644 index 000000000000..e5e938252acf --- /dev/null +++ b/.changeset/stale-panthers-smash.md @@ -0,0 +1,7 @@ +--- +"live-mobile": minor +"@ledgerhq/live-wallet": minor +"@ledgerhq/actions-root": minor +--- + +Fix live-wallet to store accountNames and starredAccounts in storage diff --git a/libs/live-wallet/src/store.test.ts b/libs/live-wallet/src/store.test.ts index 6d8f05fbec56..d371298321af 100644 --- a/libs/live-wallet/src/store.test.ts +++ b/libs/live-wallet/src/store.test.ts @@ -225,6 +225,8 @@ describe("Wallet store", () => { const exportedState = { walletSyncState: { data: {}, version: 42 }, nonImportedAccountInfos: [], + accountNames: [], + starredAccountIds: [], }; it("allows partial wallet state", () => { @@ -242,9 +244,44 @@ describe("Wallet store", () => { expect(walletSyncStateSelector(result)).toEqual({ data: {}, version: 42 }); }); + it("can import the wallet state with renamed accounts and starred accounts", () => { + const result = handlers.IMPORT_WALLET_SYNC( + initialState, + importWalletState({ + ...exportedState, + accountNames: [[ETHEREUM_ACCOUNT, "New name"]], + starredAccountIds: [ETHEREUM_ACCOUNT], + }), + ); + expect(walletSyncStateSelector(result)).toEqual({ data: {}, version: 42 }); + expect(accountNameSelector(result, { accountId: ETHEREUM_ACCOUNT })).toBe("New name"); + expect(isStarredAccountSelector(result, { accountId: ETHEREUM_ACCOUNT })).toBe(true); + }); + it("can export the wallet state", () => { const result = handlers.IMPORT_WALLET_SYNC(initialState, importWalletState(exportedState)); expect(exportWalletState(result)).toEqual(exportedState); + + //Check that the exported state includes rename of accounts. + const resultRename = handlers.SET_ACCOUNT_NAME( + result, + setAccountName(ETHEREUM_ACCOUNT, "New name"), + ); + expect(exportWalletState(resultRename)).toEqual({ + ...exportedState, + accountNames: [[ETHEREUM_ACCOUNT, "New name"]], + }); + + //Check that the exported state includes starred accounts. + const resultStarred = handlers.SET_ACCOUNT_STARRED( + resultRename, + setAccountStarred(ETHEREUM_ACCOUNT, true), + ); + expect(exportWalletState(resultStarred)).toEqual({ + ...exportedState, + accountNames: [[ETHEREUM_ACCOUNT, "New name"]], + starredAccountIds: [ETHEREUM_ACCOUNT], + }); }); it("walletStateExportShouldDiffer", () => { @@ -253,5 +290,17 @@ describe("Wallet store", () => { expect(walletStateExportShouldDiffer(initialState, result)).toBe(true); expect(walletStateExportShouldDiffer(initialState, initialState)).toBe(false); expect(walletStateExportShouldDiffer(result, result)).toBe(false); + + const resultRename = handlers.SET_ACCOUNT_NAME( + initialState, + setAccountName(ETHEREUM_ACCOUNT, "New name"), + ); + expect(walletStateExportShouldDiffer(initialState, resultRename)).toBe(true); + + const resultStar = handlers.SET_ACCOUNT_STARRED( + initialState, + setAccountStarred(ETHEREUM_ACCOUNT, true), + ); + expect(walletStateExportShouldDiffer(initialState, resultStar)).toBe(true); }); }); diff --git a/libs/live-wallet/src/store.ts b/libs/live-wallet/src/store.ts index 6bf85e56bd24..1adcefb7396e 100644 --- a/libs/live-wallet/src/store.ts +++ b/libs/live-wallet/src/store.ts @@ -31,6 +31,8 @@ export type WalletState = { export type ExportedWalletState = { walletSyncState: WSState; nonImportedAccountInfos: NonImportedAccountInfo[]; + accountNames: Array<[string, string]>; + starredAccountIds: Array; }; export const initialState: WalletState = { @@ -61,7 +63,7 @@ export type HandlersPayloads = { data: DistantState | null; version: number; }; - IMPORT_WALLET_SYNC: Partial; + IMPORT_WALLET_SYNC: Partial; SET_NON_IMPORTED_ACCOUNTS: NonImportedAccountInfo[]; }; @@ -169,7 +171,11 @@ export const initAccounts = (accounts: Account[], accountsUserData: AccountUserD */ export const importWalletState = (payload: Partial) => ({ type: "IMPORT_WALLET_SYNC", - payload, + payload: { + ...payload, + accountNames: new Map(payload.accountNames), + starredAccountIds: new Set(payload.starredAccountIds), + }, }); export const walletSyncUpdate = (data: DistantState | null, version: number) => ({ @@ -243,15 +249,21 @@ export const accountRawToAccountUserData = (raw: AccountRaw): AccountUserData => /** * call this selector to save the store state */ -export const exportWalletState = (state: WalletState): ExportedWalletState => ({ - walletSyncState: state.walletSyncState, - nonImportedAccountInfos: state.nonImportedAccountInfos, -}); +export const exportWalletState = (state: WalletState): ExportedWalletState => { + return { + walletSyncState: state.walletSyncState, + nonImportedAccountInfos: state.nonImportedAccountInfos, + accountNames: Array.from(state.accountNames), + starredAccountIds: Array.from(state.starredAccountIds), + }; +}; export const walletStateExportShouldDiffer = (a: WalletState, b: WalletState): boolean => { return ( a.walletSyncState !== b.walletSyncState || - a.nonImportedAccountInfos !== b.nonImportedAccountInfos + a.nonImportedAccountInfos !== b.nonImportedAccountInfos || + a.accountNames !== b.accountNames || + a.starredAccountIds !== b.starredAccountIds ); };