Extend filtering on web3{Accounts, AccountsSubscribe} (#1186)

* Extend filtering on web3{Accounts, AccountsSubscribe}

* Adjust formatting

* Align naming

* CHANGELOG

* CHANGELOG

* CHANGELOG
This commit is contained in:
Jaco 2022-12-25 09:55:00 +02:00 committed by GitHub
parent 00e73c43bf
commit d7987805b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 16 deletions

View File

@ -2,9 +2,22 @@
## master
Contributed:
- Fix naming of `getAllMetadata` message (Thanks to https://github.com/Nick-1979)
- Add more zh translations (Thanks to https://github.com/chendatony31)
- Typo fix (Thanks to https://github.com/Nick-1979)
- Re-add QR signing support (Thanks to https://github.com/Tbaut)
- Adjust flow of auth management screens (Thanks to https://github.com/Tbaut)
- Add links to connected accounts in header (Thanks to https://github.com/Tbaut)
Changes:
- Ensure that `EXTENSION_PREFIX` is always set as part of `@polkadot/extension-base`
- Allow for `genesisHash` filter to both `web3{Accounts, AccountsSubscribe}`
- Allow for transparent extension `ping` (as available)
- Support for new privacy-preserving `connect(<source>)` interfaces (non-default)
- Always set metadata before signing (fixes for ETH-compat chains)
## 0.44.6 Aug 21, 2022

View File

@ -3,7 +3,7 @@
import type { InjectedAccount, InjectedAccountWithMeta, InjectedExtension, InjectedProviderWithMeta, InjectedWindow, ProviderList, Unsubcall, Web3AccountsOptions } from '@polkadot/extension-inject/types';
import { objectSpread, u8aEq } from '@polkadot/util';
import { isPromise, objectSpread, u8aEq } from '@polkadot/util';
import { decodeAddress, encodeAddress } from '@polkadot/util-crypto';
import { documentReadyPromise } from './util';
@ -42,6 +42,14 @@ function mapAccounts (source: string, list: InjectedAccount[], ss58Format?: numb
}));
}
// internal helper to filter accounts
function filterAccounts (list: InjectedAccount[], genesisHash?: string | null, type?: string[]): InjectedAccount[] {
return list.filter((a) =>
(!a.type || !type || type.includes(a.type)) &&
(!a.genesisHash || !genesisHash || a.genesisHash === genesisHash)
);
}
// have we found a properly constructed window.injectedWeb3
let isWeb3Injected = web3IsInjected();
@ -126,22 +134,24 @@ export function web3Enable (originName: string, compatInits: (() => Promise<bool
}
// retrieve all the accounts across all providers
export async function web3Accounts ({ accountType, extensions, ss58Format }: Web3AccountsOptions = {}): Promise<InjectedAccountWithMeta[]> {
export async function web3Accounts ({ accountType, extensions, genesisHash, ss58Format }: Web3AccountsOptions = {}): Promise<InjectedAccountWithMeta[]> {
if (!web3EnablePromise) {
return throwError('web3Accounts');
}
const accounts: InjectedAccountWithMeta[] = [];
const injected = await web3EnablePromise;
const sources = await web3EnablePromise;
const retrieved = await Promise.all(
injected
.filter(({ name: source }) => !extensions || extensions.includes(source))
sources
.filter(({ name: source }) =>
!extensions ||
extensions.includes(source)
)
.map(async ({ accounts, name: source }): Promise<InjectedAccountWithMeta[]> => {
try {
const list = await accounts.get();
return mapAccounts(source, list.filter(({ type }) => type && accountType ? accountType.includes(type) : true), ss58Format);
return mapAccounts(source, filterAccounts(list, genesisHash, accountType), ss58Format);
} catch (error) {
// cannot handle this one
return [];
@ -158,7 +168,7 @@ export async function web3Accounts ({ accountType, extensions, ss58Format }: Web
return accounts;
}
export async function web3AccountsSubscribe (cb: (accounts: InjectedAccountWithMeta[]) => void | Promise<void>, { extensions, ss58Format }: Web3AccountsOptions = {}): Promise<Unsubcall> {
export async function web3AccountsSubscribe (cb: (accounts: InjectedAccountWithMeta[]) => void | Promise<void>, { accountType, extensions, genesisHash, ss58Format }: Web3AccountsOptions = {}): Promise<Unsubcall> {
if (!web3EnablePromise) {
return throwError('web3AccountsSubscribe');
}
@ -170,20 +180,28 @@ export async function web3AccountsSubscribe (cb: (accounts: InjectedAccountWithM
Object
.entries(accounts)
.reduce((result: InjectedAccountWithMeta[], [source, list]): InjectedAccountWithMeta[] => {
result.push(...mapAccounts(source, list, ss58Format));
result.push(...mapAccounts(source, filterAccounts(list, genesisHash, accountType), ss58Format));
return result;
}, [])
);
const unsubs = (await web3EnablePromise)
.filter(({ name: source }) => !extensions || extensions.includes(source))
const sources = await web3EnablePromise;
const unsubs = sources
.filter(({ name: source }) =>
!extensions ||
extensions.includes(source)
)
.map(({ accounts: { subscribe }, name: source }): Unsubcall =>
subscribe((result): void => {
accounts[source] = result;
try {
triggerUpdate()?.catch(console.error);
const result = triggerUpdate();
if (result && isPromise(result)) {
result.catch(console.error);
}
} catch (error) {
console.error(error);
}
@ -204,7 +222,7 @@ export async function web3FromSource (source: string): Promise<InjectedExtension
}
const sources = await web3EnablePromise;
const found = source && sources.find(({ name }): boolean => name === source);
const found = source && sources.find(({ name }) => name === source);
if (!found) {
throw new Error(`web3FromSource: Unable to find an injected ${source}`);

View File

@ -113,7 +113,8 @@ export type InjectedExtension = InjectedExtensionInfo & Injected;
export type InjectOptions = InjectedExtensionInfo;
export interface Web3AccountsOptions {
ss58Format?: number,
accountType?: KeypairType[],
extensions?: string[]
accountType?: KeypairType[];
extensions?: string[];
genesisHash?: string | null;
ss58Format?: number;
}