|
1 | 1 | import { encodesubstrateAddress } from './encodeSubstrateAddress'
|
2 | 2 | import { InjectedPolkadotAccount } from 'polkadot-api/pjs-signer'
|
3 | 3 |
|
4 |
| -export const encodeAccounts = ( |
5 |
| - accounts: InjectedPolkadotAccount[] | string[], |
| 4 | +export function encodeAccounts(accounts: string[], ss58Format: number): string[] |
| 5 | +export function encodeAccounts( |
| 6 | + accounts: InjectedPolkadotAccount[], |
6 | 7 | ss58Format: number
|
7 |
| -) => { |
8 |
| - return accounts |
| 8 | +): InjectedPolkadotAccount[] |
| 9 | +export function encodeAccounts(accounts: unknown[], ss58Format: number): unknown[] { |
| 10 | + if (!accounts || accounts.length === 0) return [] |
| 11 | + |
| 12 | + if (typeof accounts[0] === 'string') { |
| 13 | + return (accounts as string[]) |
| 14 | + .map((account) => encodesubstrateAddress(account, ss58Format)) |
| 15 | + .filter(Boolean) as string[] |
| 16 | + } |
| 17 | + |
| 18 | + return (accounts as InjectedPolkadotAccount[]) |
9 | 19 | .map((account) => {
|
10 |
| - const addressToEncode = typeof account === 'string' ? account : account.address |
| 20 | + const addressToEncode = account.address |
11 | 21 |
|
12 | 22 | const encodedAddress = encodesubstrateAddress(addressToEncode, ss58Format)
|
13 | 23 |
|
14 |
| - if (typeof account === 'string') { |
15 |
| - return encodedAddress |
| 24 | + if (!encodedAddress) { |
| 25 | + return null |
16 | 26 | }
|
17 | 27 |
|
18 |
| - return encodedAddress |
19 |
| - ? { |
20 |
| - ...account, |
21 |
| - address: encodedAddress |
22 |
| - } |
23 |
| - : null |
| 28 | + return { |
| 29 | + ...account, |
| 30 | + address: encodedAddress |
| 31 | + } as InjectedPolkadotAccount |
24 | 32 | })
|
25 |
| - .filter((acc) => !!acc) as InjectedPolkadotAccount[] |
| 33 | + .filter(Boolean) as InjectedPolkadotAccount[] |
26 | 34 | }
|
0 commit comments