Skip to content

Commit

Permalink
fix: live wallet store to export account names and starred accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
KVNLS committed Jan 7, 2025
1 parent 11be2e9 commit 1b4cbc0
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 7 deletions.
7 changes: 7 additions & 0 deletions .changeset/stale-panthers-smash.md
Original file line number Diff line number Diff line change
@@ -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
49 changes: 49 additions & 0 deletions libs/live-wallet/src/store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ describe("Wallet store", () => {
const exportedState = {
walletSyncState: { data: {}, version: 42 },
nonImportedAccountInfos: [],
accountNames: [],
starredAccountIds: [],
};

it("allows partial wallet state", () => {
Expand All @@ -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", () => {
Expand All @@ -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);
});
});
26 changes: 19 additions & 7 deletions libs/live-wallet/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export type WalletState = {
export type ExportedWalletState = {
walletSyncState: WSState;
nonImportedAccountInfos: NonImportedAccountInfo[];
accountNames: Array<[string, string]>;
starredAccountIds: Array<string>;
};

export const initialState: WalletState = {
Expand Down Expand Up @@ -61,7 +63,7 @@ export type HandlersPayloads = {
data: DistantState | null;
version: number;
};
IMPORT_WALLET_SYNC: Partial<ExportedWalletState>;
IMPORT_WALLET_SYNC: Partial<WalletState>;
SET_NON_IMPORTED_ACCOUNTS: NonImportedAccountInfo[];
};

Expand Down Expand Up @@ -169,7 +171,11 @@ export const initAccounts = (accounts: Account[], accountsUserData: AccountUserD
*/
export const importWalletState = (payload: Partial<ExportedWalletState>) => ({
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) => ({
Expand Down Expand Up @@ -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
);
};

Expand Down

0 comments on commit 1b4cbc0

Please sign in to comment.