Skip to content

Commit

Permalink
Merge pull request #180 from logion-network/feature/extension-detection
Browse files Browse the repository at this point in the history
Make extension detection more robust.
  • Loading branch information
benoitdevos authored Sep 4, 2023
2 parents 2e269c7 + e754c1b commit 01cc851
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/extension/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@logion/extension",
"version": "0.5.21-1",
"version": "0.5.21-4",
"description": "logion SDK for Polkadot JS extension",
"main": "dist/index.js",
"packageManager": "[email protected]",
Expand Down
15 changes: 11 additions & 4 deletions packages/extension/src/Extension.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { web3AccountsSubscribe, web3Enable, isWeb3Injected, web3Accounts } from '@polkadot/extension-dapp';
import { web3AccountsSubscribe, web3Enable, web3EnablePromise, web3Accounts } from '@polkadot/extension-dapp';
import initMetaMask from "@polkadot/extension-compat-metamask/bundle";
import { InjectedAccountWithMeta, InjectedExtension } from '@polkadot/extension-inject/types';

export function isExtensionAvailable(): boolean {
return isWeb3Injected;
export async function isExtensionAvailable(appName: string): Promise<boolean> {
const extensions = await web3Enable(appName);
return extensions.length > 0;
}

export type InjectedAccount = InjectedAccountWithMeta;
Expand All @@ -13,7 +14,13 @@ export type InjectedAccountsConsumer = (accounts: InjectedAccount[]) => void;
export type InjectedAccountsConsumerRegister = (consumer: InjectedAccountsConsumer) => void;

export async function enableExtensions(appName: string): Promise<InjectedAccountsConsumerRegister> {
await web3Enable(appName);
const extensions =
web3EnablePromise === null ?
await web3Enable(appName) :
await web3EnablePromise;
if (extensions === null || extensions.length === 0) {
throw new Error("Failed to detect any web3 extensions");
}
return consumer => web3AccountsSubscribe(consumer, { extensions: [ "polkadot-js" ] });
}

Expand Down

0 comments on commit 01cc851

Please sign in to comment.