Skip to content

Commit

Permalink
feat: sort Ledger accounts by derivation path
Browse files Browse the repository at this point in the history
  • Loading branch information
tien committed Oct 6, 2024
1 parent 962c468 commit 25414fe
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilly-pugs-poke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@reactive-dot/wallet-ledger": patch
---

Sort Ledger accounts by derivation path.
62 changes: 32 additions & 30 deletions packages/wallet-ledger/src/ledger-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,37 +29,39 @@ export class LedgerWallet extends LocalWallet<LedgerAccount, "accounts"> {

readonly accounts$ = this.#ledgerAccounts$.pipe(
map((accounts) =>
accounts.map(
(account): PolkadotSignerAccount => ({
id: account.id,
...(account.name === undefined ? {} : { name: account.name }),
polkadotSigner: ({ tokenSymbol, tokenDecimals }) => ({
publicKey: account.publicKey,
signTx: async (...args) => {
await this.#assertMatchingAccount(account);

const ledgerSigner = await this.#getOrCreateLedgerSigner();
const polkadotSigner = await ledgerSigner.getPolkadotSigner(
{ tokenSymbol, decimals: tokenDecimals },
account.path,
);

return polkadotSigner.signTx(...args);
},
signBytes: async (...args) => {
await this.#assertMatchingAccount(account);

const ledgerSigner = await this.#getOrCreateLedgerSigner();
const polkadotSigner = await ledgerSigner.getPolkadotSigner(
{ tokenSymbol, decimals: tokenDecimals },
account.path,
);

return polkadotSigner.signBytes(...args);
},
accounts
.toSorted((a, b) => a.path - b.path)
.map(
(account): PolkadotSignerAccount => ({
id: account.id,
...(account.name === undefined ? {} : { name: account.name }),
polkadotSigner: ({ tokenSymbol, tokenDecimals }) => ({
publicKey: account.publicKey,
signTx: async (...args) => {
await this.#assertMatchingAccount(account);

const ledgerSigner = await this.#getOrCreateLedgerSigner();
const polkadotSigner = await ledgerSigner.getPolkadotSigner(
{ tokenSymbol, decimals: tokenDecimals },
account.path,
);

return polkadotSigner.signTx(...args);
},
signBytes: async (...args) => {
await this.#assertMatchingAccount(account);

const ledgerSigner = await this.#getOrCreateLedgerSigner();
const polkadotSigner = await ledgerSigner.getPolkadotSigner(
{ tokenSymbol, decimals: tokenDecimals },
account.path,
);

return polkadotSigner.signBytes(...args);
},
}),
}),
}),
),
),
),
);

Expand Down

0 comments on commit 25414fe

Please sign in to comment.