Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: properly check if metamask is installed #517

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { MainWalletBase } from '@cosmos-kit/core';

import { ChainCosmosExtensionMetamaskSnap } from './chain-wallet';
import { CosmosExtensionClient } from './client';
import { isMetamaskInstalled } from './utils';

export class CosmosMetamaskExtensionWallet extends MainWalletBase {
constructor(walletInfo: Wallet) {
Expand All @@ -12,7 +13,8 @@ export class CosmosMetamaskExtensionWallet extends MainWalletBase {
async initClient() {
this.initingClient();
try {
this.initClientDone(new CosmosExtensionClient());
const installed = await isMetamaskInstalled();
this.initClientDone(installed ? new CosmosExtensionClient() : undefined);
} catch (error) {
this.initClientError(error);
}
Expand Down
21 changes: 21 additions & 0 deletions wallets/cosmos-extension-metamask/src/extension/utils.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,38 @@
import { ClientNotExistError } from '@cosmos-kit/core';
import { CosmosSnap } from '@cosmsnap/snapper';

interface MetamaskWindow {
ethereum?: {
isMetaMask?: boolean;
};
}

interface SnapWindow {
cosmos?: CosmosSnap;
}

export const isMetamaskInstalled = async (): Promise<boolean> => {
if (typeof window === 'undefined') {
return false;
}

const ethereum = (window as MetamaskWindow).ethereum;
return Boolean(ethereum?.isMetaMask);
};

export const getSnapProviderFromExtension: () => Promise<
CosmosSnap | undefined
> = async () => {
if (typeof window === 'undefined') {
return void 0;
}

// Check if MetaMask is installed first
const hasMetaMask = await isMetamaskInstalled();
if (!hasMetaMask) {
throw ClientNotExistError;
}

const cosmos = (window as SnapWindow).cosmos;

if (cosmos) {
Expand Down
Loading