Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/silent-clubs-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@stakekit/widget": patch
---

feat: enhance account sorting logic in ledger connector
2 changes: 1 addition & 1 deletion packages/widget/src/domain/types/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ export const supportedLedgerFamiliesWithCurrency = {
family: "ethereum",
skChainName: EvmNetworks.Optimism,
},
"avalanche-c": {
avalanche_c_chain: {
currencyId: "avalanche_c_chain",
family: "ethereum",
skChainName: EvmNetworks.AvalancheC,
Expand Down
87 changes: 53 additions & 34 deletions packages/widget/src/providers/ledger/ledger-connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
WindowMessageTransport,
deserializeTransaction,
} from "@ledgerhq/wallet-api-client";
import { Networks } from "@stakekit/common";
import type {
Chain,
WalletDetailsParams,
Expand Down Expand Up @@ -117,28 +118,44 @@ const createLedgerLiveConnector = ({

ledgerAccounts = accounts;

const accountsWithChain = accounts.reduce(
(acc, next) => {
const family = ledgerCurrencies.get(next.currency);
const chainPriority = new Map<SupportedSKChains, number>([
[Networks.Polkadot, 1],
[Networks.AvalancheC, 2],
[Networks.Tron, 3],
[Networks.Binance, 4],
[Networks.Cronos, 5],
[Networks.Polygon, 6],
]);

if (!family) return acc;
const accountsWithChain = accounts
.reduce(
(acc, next) => {
const family = ledgerCurrencies.get(next.currency);

const itemMap = filteredSupportedLedgerFamiliesWithCurrency.get(
family as SupportedLedgerLiveFamilies
);
if (!family) return acc;

if (!family || !itemMap) return acc;
const itemMap = filteredSupportedLedgerFamiliesWithCurrency.get(
family as SupportedLedgerLiveFamilies
);

const chainItem = itemMap.get("*") || itemMap.get(next.currency);
if (!family || !itemMap) return acc;

if (chainItem) {
acc.push({ account: next, chainItem });
}
const chainItem = itemMap.get("*") || itemMap.get(next.currency);

return acc;
},
[] as { account: Account; chainItem: ChainItem }[]
);
if (chainItem) {
acc.push({ account: next, chainItem });
}

return acc;
},
[] as { account: Account; chainItem: ChainItem }[]
)
.sort((a, b) => {
const aPriority = chainPriority.get(a.chainItem.skChainName) || 999;
const bPriority = chainPriority.get(b.chainItem.skChainName) || 999;

return aPriority - bPriority;
});

if (!accountsWithChain.length) {
const defaultChain = Maybe.fromNullable(
Expand All @@ -161,8 +178,8 @@ const createLedgerLiveConnector = ({
};
}

const preferredAccount = Maybe.fromNullable(queryParams.accountId)
.chain((accId) =>
const preferredAccount = Maybe.fromNullable(queryParams.accountId).chain(
(accId) =>
Maybe.encase(() => {
if (accId.startsWith("js:")) {
const [, , , address] = accId.split(":");
Expand All @@ -172,24 +189,26 @@ const createLedgerLiveConnector = ({

return { type: "accountId", accountId: accId };
})
)
.extractNullable();

const accountWithChain = List.find((v) => {
if (v.chainItem.skChainName !== queryParams.network) {
return false;
}

if (preferredAccount) {
if (preferredAccount.type === "address") {
return v.account.address === preferredAccount.address;
}
);

return v.account.id === preferredAccount.accountId;
}
const accountWithChain = preferredAccount
.chain((pa) =>
List.find((v) => {
if (pa.type === "address") {
return v.account.address === pa.address;
}

return true;
}, accountsWithChain)
return v.account.id === pa.accountId;
}, accountsWithChain)
)
.altLazy(() =>
Maybe.fromNullable(queryParams.network).chain((network) =>
List.find(
(v) => v.chainItem.skChainName === network,
accountsWithChain
)
)
)
.altLazy(() => List.head(accountsWithChain))
.toEither(new Error("Account not found"))
.unsafeCoerce();
Expand Down