From 1dd37b021eebf1e369ff2d4dd676b14a1881486e Mon Sep 17 00:00:00 2001 From: hamidra Date: Tue, 17 May 2022 21:10:35 -0700 Subject: [PATCH] add extension filtered account subscriptions (#1063) * add extension filtered account subscriptions * fix floating promise * add extensions filter as account option * Update packages/extension-dapp/src/bundle.ts Co-authored-by: Jaco * fix lint issue Co-authored-by: Jaco --- packages/extension-dapp/src/bundle.ts | 12 ++++++++---- packages/extension-inject/src/types.ts | 3 ++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/extension-dapp/src/bundle.ts b/packages/extension-dapp/src/bundle.ts index 72c132cf..45942b58 100644 --- a/packages/extension-dapp/src/bundle.ts +++ b/packages/extension-dapp/src/bundle.ts @@ -109,7 +109,7 @@ export function web3Enable (originName: string, compatInits: (() => Promise { +export async function web3Accounts ({ accountType, extensions, ss58Format }: Web3AccountsOptions = {}): Promise { if (!web3EnablePromise) { return throwError('web3Accounts'); } @@ -118,7 +118,9 @@ export async function web3Accounts ({ accountType, ss58Format }: Web3AccountsOpt const injected = await web3EnablePromise; const retrieved = await Promise.all( - injected.map(async ({ accounts, name: source }): Promise => { + injected.filter( + ({ name: source }) => !extensions || extensions.includes(source) + ).map(async ({ accounts, name: source }): Promise => { try { const list = await accounts.get(); @@ -141,7 +143,7 @@ export async function web3Accounts ({ accountType, ss58Format }: Web3AccountsOpt return accounts; } -export async function web3AccountsSubscribe (cb: (accounts: InjectedAccountWithMeta[]) => void | Promise, { ss58Format }: Web3AccountsOptions = {}): Promise { +export async function web3AccountsSubscribe (cb: (accounts: InjectedAccountWithMeta[]) => void | Promise, { extensions, ss58Format }: Web3AccountsOptions = {}): Promise { if (!web3EnablePromise) { return throwError('web3AccountsSubscribe'); } @@ -160,7 +162,9 @@ export async function web3AccountsSubscribe (cb: (accounts: InjectedAccountWithM ) ); - const unsubs = (await web3EnablePromise).map( + const unsubs = (await web3EnablePromise).filter( + ({ name: source }) => !extensions || extensions.includes(source) + ).map( ({ accounts: { subscribe }, name: source }): Unsubcall => subscribe((result): void => { accounts[source] = result; diff --git a/packages/extension-inject/src/types.ts b/packages/extension-inject/src/types.ts index 57a25ee2..ed82455d 100644 --- a/packages/extension-inject/src/types.ts +++ b/packages/extension-inject/src/types.ts @@ -113,5 +113,6 @@ export type InjectOptions = InjectedExtensionInfo; export interface Web3AccountsOptions { ss58Format?: number, - accountType?: KeypairType[] + accountType?: KeypairType[], + extensions?: string[] }