Skip to content

Commit

Permalink
fix: Added installed and mobile check for near snap (#938)
Browse files Browse the repository at this point in the history
* added isInstall method for near-snap

* added isMobile method for near-snap

* fixed check for nightly

* removed default wallet from documentation
  • Loading branch information
erditkurteshiSQA authored Sep 21, 2023
1 parent 638f36f commit 59ecaf3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
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

0 comments on commit 59ecaf3

Please sign in to comment.