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:
parent
00e73c43bf
commit
d7987805b6
13
CHANGELOG.md
13
CHANGELOG.md
@ -2,9 +2,22 @@
|
|||||||
|
|
||||||
## master
|
## 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:
|
Changes:
|
||||||
|
|
||||||
- Ensure that `EXTENSION_PREFIX` is always set as part of `@polkadot/extension-base`
|
- 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
|
## 0.44.6 Aug 21, 2022
|
||||||
|
|||||||
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
import type { InjectedAccount, InjectedAccountWithMeta, InjectedExtension, InjectedProviderWithMeta, InjectedWindow, ProviderList, Unsubcall, Web3AccountsOptions } from '@polkadot/extension-inject/types';
|
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 { decodeAddress, encodeAddress } from '@polkadot/util-crypto';
|
||||||
|
|
||||||
import { documentReadyPromise } from './util';
|
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
|
// have we found a properly constructed window.injectedWeb3
|
||||||
let isWeb3Injected = web3IsInjected();
|
let isWeb3Injected = web3IsInjected();
|
||||||
|
|
||||||
@ -126,22 +134,24 @@ export function web3Enable (originName: string, compatInits: (() => Promise<bool
|
|||||||
}
|
}
|
||||||
|
|
||||||
// retrieve all the accounts across all providers
|
// 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) {
|
if (!web3EnablePromise) {
|
||||||
return throwError('web3Accounts');
|
return throwError('web3Accounts');
|
||||||
}
|
}
|
||||||
|
|
||||||
const accounts: InjectedAccountWithMeta[] = [];
|
const accounts: InjectedAccountWithMeta[] = [];
|
||||||
const injected = await web3EnablePromise;
|
const sources = await web3EnablePromise;
|
||||||
|
|
||||||
const retrieved = await Promise.all(
|
const retrieved = await Promise.all(
|
||||||
injected
|
sources
|
||||||
.filter(({ name: source }) => !extensions || extensions.includes(source))
|
.filter(({ name: source }) =>
|
||||||
|
!extensions ||
|
||||||
|
extensions.includes(source)
|
||||||
|
)
|
||||||
.map(async ({ accounts, name: source }): Promise<InjectedAccountWithMeta[]> => {
|
.map(async ({ accounts, name: source }): Promise<InjectedAccountWithMeta[]> => {
|
||||||
try {
|
try {
|
||||||
const list = await accounts.get();
|
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) {
|
} catch (error) {
|
||||||
// cannot handle this one
|
// cannot handle this one
|
||||||
return [];
|
return [];
|
||||||
@ -158,7 +168,7 @@ export async function web3Accounts ({ accountType, extensions, ss58Format }: Web
|
|||||||
return accounts;
|
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) {
|
if (!web3EnablePromise) {
|
||||||
return throwError('web3AccountsSubscribe');
|
return throwError('web3AccountsSubscribe');
|
||||||
}
|
}
|
||||||
@ -170,20 +180,28 @@ export async function web3AccountsSubscribe (cb: (accounts: InjectedAccountWithM
|
|||||||
Object
|
Object
|
||||||
.entries(accounts)
|
.entries(accounts)
|
||||||
.reduce((result: InjectedAccountWithMeta[], [source, list]): InjectedAccountWithMeta[] => {
|
.reduce((result: InjectedAccountWithMeta[], [source, list]): InjectedAccountWithMeta[] => {
|
||||||
result.push(...mapAccounts(source, list, ss58Format));
|
result.push(...mapAccounts(source, filterAccounts(list, genesisHash, accountType), ss58Format));
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}, [])
|
}, [])
|
||||||
);
|
);
|
||||||
|
|
||||||
const unsubs = (await web3EnablePromise)
|
const sources = await web3EnablePromise;
|
||||||
.filter(({ name: source }) => !extensions || extensions.includes(source))
|
const unsubs = sources
|
||||||
|
.filter(({ name: source }) =>
|
||||||
|
!extensions ||
|
||||||
|
extensions.includes(source)
|
||||||
|
)
|
||||||
.map(({ accounts: { subscribe }, name: source }): Unsubcall =>
|
.map(({ accounts: { subscribe }, name: source }): Unsubcall =>
|
||||||
subscribe((result): void => {
|
subscribe((result): void => {
|
||||||
accounts[source] = result;
|
accounts[source] = result;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
triggerUpdate()?.catch(console.error);
|
const result = triggerUpdate();
|
||||||
|
|
||||||
|
if (result && isPromise(result)) {
|
||||||
|
result.catch(console.error);
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
}
|
}
|
||||||
@ -204,7 +222,7 @@ export async function web3FromSource (source: string): Promise<InjectedExtension
|
|||||||
}
|
}
|
||||||
|
|
||||||
const sources = await web3EnablePromise;
|
const sources = await web3EnablePromise;
|
||||||
const found = source && sources.find(({ name }): boolean => name === source);
|
const found = source && sources.find(({ name }) => name === source);
|
||||||
|
|
||||||
if (!found) {
|
if (!found) {
|
||||||
throw new Error(`web3FromSource: Unable to find an injected ${source}`);
|
throw new Error(`web3FromSource: Unable to find an injected ${source}`);
|
||||||
|
|||||||
@ -113,7 +113,8 @@ export type InjectedExtension = InjectedExtensionInfo & Injected;
|
|||||||
export type InjectOptions = InjectedExtensionInfo;
|
export type InjectOptions = InjectedExtensionInfo;
|
||||||
|
|
||||||
export interface Web3AccountsOptions {
|
export interface Web3AccountsOptions {
|
||||||
ss58Format?: number,
|
accountType?: KeypairType[];
|
||||||
accountType?: KeypairType[],
|
extensions?: string[];
|
||||||
extensions?: string[]
|
genesisHash?: string | null;
|
||||||
|
ss58Format?: number;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user