Skip to content

Commit

Permalink
feat: format addresses according to their chain's SS58 format (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
tien authored Sep 26, 2024
1 parent f682874 commit 6370689
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/friendly-otters-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@reactive-dot/core": minor
---

Added support for formatting addresses based on their chain's SS58 format.
30 changes: 25 additions & 5 deletions packages/core/src/actions/get-accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { MaybeAsync, PolkadotAccount } from "../types.js";
import { toObservable } from "../utils.js";
import type { Wallet } from "../wallets/wallet.js";
import type { ChainSpecData } from "@polkadot-api/substrate-client";
import { AccountId } from "polkadot-api";
import { combineLatest, of } from "rxjs";
import { map, switchMap } from "rxjs/operators";

Expand All @@ -15,6 +16,14 @@ export function getAccounts(
return of([]);
}

const maybeSs58Format = chainSpec?.properties["ss58Format"];

const ss58Format =
typeof maybeSs58Format === "number" ? maybeSs58Format : undefined;

const ss58AccountId =
ss58Format === undefined ? undefined : AccountId(ss58Format);

return combineLatest(
wallets.map((wallet) =>
wallet.accounts$.pipe(
Expand All @@ -31,11 +40,22 @@ export function getAccounts(
chainSpec === undefined
? (accounts) => accounts
: (accounts) =>
accounts.filter(
(account) =>
!account.genesisHash ||
chainSpec.genesisHash.includes(account.genesisHash),
),
accounts
.filter(
(account) =>
!account.genesisHash ||
chainSpec.genesisHash.includes(account.genesisHash),
)
.map((account) =>
ss58AccountId === undefined
? account
: {
...account,
address: ss58AccountId.dec(
ss58AccountId.enc(account.address),
),
},
),
),
);
}),
Expand Down

0 comments on commit 6370689

Please sign in to comment.