Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(llm/lld): ledger sync - improve account names module #7714

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/gentle-lions-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"ledger-live-desktop": minor
"live-mobile": minor
"@ledgerhq/live-wallet": minor
---

Ledger Sync - Improve account names module to avoid erasing custom names with the default ones when two instances are pushing around the same time
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function useGetMembers() {
const sdk = useTrustchainSdk();
const trustchain = useSelector(trustchainSelector);
const memberCredentials = useSelector(memberCredentialsSelector);
const errorHandler = useLifeCycle();
const { handleError } = useLifeCycle();

function fetchMembers() {
if (!memberCredentials) {
Expand Down Expand Up @@ -41,9 +41,9 @@ export function useGetMembers() {

useEffect(() => {
if (isErrorGetMembers) {
errorHandler.handleError(getMembersError);
handleError(getMembersError);
}
}, [errorHandler, getMembersError, isErrorGetMembers]);
}, [handleError, getMembersError, isErrorGetMembers]);

return {
isMembersLoading: isMembersLoading,
Expand Down
7 changes: 5 additions & 2 deletions apps/ledger-live-desktop/src/renderer/actions/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ import { Account, AccountUserData } from "@ledgerhq/types-live";
import { AccountComparator } from "@ledgerhq/live-wallet/ordering";
import { getKey } from "~/renderer/storage";
import { PasswordIncorrectError } from "@ledgerhq/errors";
import { getDefaultAccountName } from "@ledgerhq/live-wallet/accountName";

export const removeAccount = (payload: Account) => ({
type: "DB:REMOVE_ACCOUNT",
payload,
});

export const initAccounts = (data: [Account, AccountUserData][]) => {
const accounts = data.map(data => data[0]);
const accountsUserData = data.map(data => data[1]);
const accounts = data.map(([account]) => account);
const accountsUserData = data
.filter(([account, userData]) => userData.name !== getDefaultAccountName(account))
.map(([, userData]) => userData);
return {
type: "INIT_ACCOUNTS",
payload: {
Expand Down
5 changes: 4 additions & 1 deletion apps/ledger-live-mobile/src/actions/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
import { AccountsActionTypes } from "./types";
import logger from "../logger";
import { initAccounts } from "@ledgerhq/live-wallet/store";
import { getDefaultAccountName } from "@ledgerhq/live-wallet/accountName";

const version = 0; // FIXME this needs to come from user data

Expand All @@ -26,7 +27,9 @@ export const importStore = (rawAccounts: { active: { data: AccountRaw }[] }) =>
}
}
const accounts = tuples.map(([account]) => account);
const accountsUserData = tuples.map(([, userData]) => userData);
const accountsUserData = tuples
.filter(([account, userData]) => userData.name !== getDefaultAccountName(account))
.map(([, userData]) => userData);
return initAccounts(accounts, accountsUserData);
};
export const reorderAccounts = createAction<AccountsReorderPayload>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function useGetMembers() {
const trustchain = useSelector(trustchainSelector);
const memberCredentials = useSelector(memberCredentialsSelector);

const errorHandler = useLifeCycle();
const { handleError } = useLifeCycle();

function getMembers() {
if (!memberCredentials) {
Expand Down Expand Up @@ -42,9 +42,9 @@ export function useGetMembers() {

useEffect(() => {
if (memberHook.isError) {
errorHandler.handleError(memberHook.error);
handleError(memberHook.error);
}
}, [errorHandler, memberHook.error, memberHook.isError]);
}, [handleError, memberHook.error, memberHook.isError]);

return memberHook;
}
40 changes: 40 additions & 0 deletions libs/live-wallet/src/store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,46 @@ describe("Wallet store", () => {
expect(result.accountNames.get(POLKADOT_ACCOUNT)).toBe(POLKADOT_ACCOUNT_NAME);
expect(result.accountNames.get(NEW_POLKADOT_ACCOUNT)).toBe(NEW_POLKADOT_ACCOUNT_NAME);
});
it("should not save the default accounts names when adding an account", () => {
const editedNames = new Map();
editedNames.set(ETHEREUM_ACCOUNT, "Ethereum 1");
editedNames.set(POLKADOT_ACCOUNT, "Polkadot 1");
editedNames.set(NEW_POLKADOT_ACCOUNT, NEW_POLKADOT_ACCOUNT_NAME);
const result = handlers.ADD_ACCOUNTS(
{
...initialState,
accountNames: new Map(),
},
{
payload: {
allAccounts: [
{
id: ETHEREUM_ACCOUNT,
type: "Account",
currency: { name: "Ethereum" },
index: 0,
} as Account,
{
id: POLKADOT_ACCOUNT,
type: "Account",
currency: { name: "Polkadot" },
index: 0,
} as Account,
{
id: NEW_POLKADOT_ACCOUNT,
type: "Account",
currency: { name: "Polkadot" },
index: 1,
} as Account,
],
editedNames,
},
},
);
expect(result.accountNames.get(ETHEREUM_ACCOUNT)).toBe(undefined);
expect(result.accountNames.get(POLKADOT_ACCOUNT)).toBe(undefined);
expect(result.accountNames.get(NEW_POLKADOT_ACCOUNT)).toBe(NEW_POLKADOT_ACCOUNT_NAME);
});

it("can decompose an account raw", () => {
const account = genAccount("foo", {
Expand Down
13 changes: 7 additions & 6 deletions libs/live-wallet/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ export const handlers: WalletHandlers = {
const accountNames = new Map();
const starredAccountIds = new Set<string>();
accountsUserData.forEach(accountUserData => {
accountNames.set(accountUserData.id, accountUserData.name);
if (accountUserData.name) {
accountNames.set(accountUserData.id, accountUserData.name);
}
for (const starredId of accountUserData.starredIds) {
starredAccountIds.add(starredId);
}
Expand Down Expand Up @@ -119,11 +121,10 @@ export const handlers: WalletHandlers = {
ADD_ACCOUNTS: (state, { payload: { allAccounts, editedNames } }) => {
const accountNames = new Map(state.accountNames);
for (const account of allAccounts) {
const name =
editedNames.get(account.id) ||
accountNames.get(account.id) ||
getDefaultAccountName(account);
accountNames.set(account.id, name);
const name = editedNames.get(account.id) || accountNames.get(account.id);
if (name && name !== getDefaultAccountName(account)) {
accountNames.set(account.id, name);
}
}
return { ...state, accountNames };
},
Expand Down
Loading