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: Added installed and mobile check for near snap #938

Merged
merged 5 commits into from
Sep 21, 2023
Merged
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
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ yarn add \
@near-wallet-selector/ledger \
@near-wallet-selector/wallet-connect \
@near-wallet-selector/nightly-connect \
@near-wallet-selector/default-wallets \
@near-wallet-selector/coin98-wallet \
@near-wallet-selector/opto-wallet \
@near-wallet-selector/neth \
Expand All @@ -90,7 +89,6 @@ npm install \
@near-wallet-selector/ledger \
@near-wallet-selector/wallet-connect \
@near-wallet-selector/nightly-connect \
@near-wallet-selector/default-wallets \
@near-wallet-selector/coin98-wallet \
@near-wallet-selector/opto-wallet \
@near-wallet-selector/neth \
Expand Down
34 changes: 33 additions & 1 deletion packages/near-snap/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,46 @@
import { isMobile } from "is-mobile";
import type { WalletModuleFactory } from "@near-wallet-selector/core";
import { initNearSnap } from "./selector";
import icon from "./icon";

export { icon };

declare global {
interface Window {
ethereum: {
chainId: string;
// eslint-disable-next-line
request: any;
};
}
}

const isInstalled = async (): Promise<boolean> => {
try {
const provider = window.ethereum;
const clientVersion = await provider?.request({
method: "web3_clientVersion",
});

const isFlaskDetected = (clientVersion as Array<string>)?.includes("flask");
return Boolean(provider && isFlaskDetected);
} catch {
return false;
}
};

export function setupNearSnap({
deprecated = false,
iconUrl = icon,
} = {}): WalletModuleFactory {
return async () => {
const mobile = isMobile();
if (mobile) {
return null;
}

const installed = await isInstalled();

return {
id: "near-snap",
type: "injected",
Expand All @@ -19,7 +51,7 @@ export function setupNearSnap({
downloadUrl: "https://near-snap.surge.sh",
iconUrl,
deprecated,
available: true,
available: installed,
},
};
};
Expand Down
2 changes: 1 addition & 1 deletion packages/nightly/src/lib/nightly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ const Nightly: WalletBehaviourFactory<InjectedWallet> = async ({
state,
});

if (_state.wallet.isConnected) {
if (!_state.wallet.isConnected) {
await _state.wallet.connect();
}

Expand Down