From d7987805b6625e079756649aa6a57779003c4976 Mon Sep 17 00:00:00 2001 From: Jaco Date: Sun, 25 Dec 2022 09:55:00 +0200 Subject: [PATCH] Extend filtering on web3{Accounts, AccountsSubscribe} (#1186) * Extend filtering on web3{Accounts, AccountsSubscribe} * Adjust formatting * Align naming * CHANGELOG * CHANGELOG * CHANGELOG --- CHANGELOG.md | 13 ++++++++ packages/extension-dapp/src/bundle.ts | 44 ++++++++++++++++++-------- packages/extension-inject/src/types.ts | 7 ++-- 3 files changed, 48 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 923a1025..3f69123d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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()` interfaces (non-default) +- Always set metadata before signing (fixes for ETH-compat chains) ## 0.44.6 Aug 21, 2022 diff --git a/packages/extension-dapp/src/bundle.ts b/packages/extension-dapp/src/bundle.ts index bfedc245..a6e1a16b 100644 --- a/packages/extension-dapp/src/bundle.ts +++ b/packages/extension-dapp/src/bundle.ts @@ -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 { +export async function web3Accounts ({ accountType, extensions, genesisHash, ss58Format }: Web3AccountsOptions = {}): Promise { 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 => { 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, { extensions, ss58Format }: Web3AccountsOptions = {}): Promise { +export async function web3AccountsSubscribe (cb: (accounts: InjectedAccountWithMeta[]) => void | Promise, { accountType, extensions, genesisHash, ss58Format }: Web3AccountsOptions = {}): Promise { 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 name === source); + const found = source && sources.find(({ name }) => name === source); if (!found) { throw new Error(`web3FromSource: Unable to find an injected ${source}`); diff --git a/packages/extension-inject/src/types.ts b/packages/extension-inject/src/types.ts index d37d19b6..f42679c7 100644 --- a/packages/extension-inject/src/types.ts +++ b/packages/extension-inject/src/types.ts @@ -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; }