Skip to content

Commit

Permalink
fix(connect-kit): MetaMask connections on mobile browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
runjuu committed Jul 18, 2023
1 parent 44d8ef9 commit 91cfdcd
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 15 deletions.
5 changes: 4 additions & 1 deletion packages/connect-kit/src/create-wagmi-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ export function createWagmiConfig({
}),
walletConnectV2ProjectId
? new WalletConnectConnector({
options: { projectId: walletConnectV2ProjectId },
options: {
showQrModal: false,
projectId: walletConnectV2ProjectId,
},
})
: null,
]);
Expand Down
9 changes: 9 additions & 0 deletions packages/connect-kit/src/utils/get-wallet-connect-uri.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { WalletConnectConnector } from "wagmi/connectors/walletConnect";

export async function getWalletConnectUri(connector: WalletConnectConnector) {
const provider = await connector.getProvider();

return new Promise<string>((resolve) =>
provider.once("display_uri", resolve),
);
}
1 change: 1 addition & 0 deletions packages/connect-kit/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./store";
export * from "./detect-browser";
export * from "./get-wallet-connect-uri";
export * from "./images";
export * from "./check-is-wallet-connect-connector";
export * from "./is-coinbase-wallet-installed";
Expand Down
10 changes: 7 additions & 3 deletions packages/connect-kit/src/wallets/use-default-wallet-connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@ import { useRefCallback } from "@crossbell/util-hooks";

import { checkIsWalletConnectConnector } from "../utils";
import { connectorStore } from "./connectors/store";
import { WalletConnectConnector } from "./connectors";

export function useDefaultWalletConnect() {
const { connectAsync, connectors } = useConnect();
const connector = React.useMemo(
const _connector = React.useMemo(
() => connectors.find(checkIsWalletConnectConnector),
[connectors],
);

const openDefaultWalletConnect = useRefCallback(async () => {
if (connector) {
if (_connector) {
try {
const connector = new WalletConnectConnector({
options: { ..._connector.options, showQrModal: true },
});
const preferredChainId = connector?.chains[0]?.id;
const result = await connectAsync({
chainId: preferredChainId,
Expand Down Expand Up @@ -44,7 +48,7 @@ export function useDefaultWalletConnect() {
});

return {
canOpenWalletConnect: !!connector,
canOpenWalletConnect: !!_connector,
openDefaultWalletConnect,
};
}
8 changes: 4 additions & 4 deletions packages/connect-kit/src/wallets/wallets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import compact from "lodash.compact";
import type { MetaMaskConnector } from "wagmi/connectors/metaMask";
import type { CoinbaseWalletConnector } from "wagmi/connectors/coinbaseWallet";
import type { InjectedConnector } from "wagmi/connectors/injected";
import type { WalletConnectLegacyConnector } from "wagmi/connectors/walletConnectLegacy";
import type { WalletConnectConnector } from "wagmi/connectors/walletConnect";

import type { OKXConnector } from "../connectors";
import { metaMaskWallet } from "./meta-mask-wallet";
Expand All @@ -18,15 +18,15 @@ enum KnownConnector {
okx = "okx",
coinbaseWallet = "coinbaseWallet",
injected = "injected",
walletConnectLegacy = "walletConnectLegacy",
walletConnect = "walletConnect",
}

type ConnectorMap = Partial<{
[KnownConnector.okx]: OKXConnector;
[KnownConnector.metaMask]: MetaMaskConnector;
[KnownConnector.coinbaseWallet]: CoinbaseWalletConnector;
[KnownConnector.injected]: InjectedConnector;
[KnownConnector.walletConnectLegacy]: WalletConnectLegacyConnector;
[KnownConnector.walletConnect]: WalletConnectConnector;
}>;

export function useWalletConnectors() {
Expand All @@ -40,7 +40,7 @@ export function useWalletConnectors() {
() =>
compact([
okxWallet(connectorMap.okx),
metaMaskWallet(connectorMap.metaMask, connectorMap.walletConnectLegacy),
metaMaskWallet(connectorMap.metaMask, connectorMap.walletConnect),
coinbaseWallet(connectorMap.coinbaseWallet),
braveWallet(connectorMap.injected),
]),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import type { MetaMaskConnector } from "wagmi/connectors/metaMask";
import type { WalletConnectLegacyConnector } from "wagmi/connectors/walletConnectLegacy";
import type { WalletConnectConnector } from "wagmi/connectors/walletConnect";
import React from "react";

import { MetamaskIcon } from "../../../components";
import { isAndroid, isMobile } from "../../../utils";
import {
isAndroid,
isMobile,
isIOS,
getWalletConnectUri,
} from "../../../utils";
import { Wallet } from "../../types";
import styles from "../coinbase-wallet/index.module.css";

Expand Down Expand Up @@ -35,7 +40,7 @@ function isMetaMask(ethereum: NonNullable<(typeof window)["ethereum"]>) {

export const metaMaskWallet = (
metaMask?: MetaMaskConnector,
walletConnectLegacy?: WalletConnectLegacyConnector,
walletConnect?: WalletConnectConnector,
): Wallet | null => {
if (!metaMask) return null;

Expand All @@ -61,15 +66,16 @@ export const metaMaskWallet = (
</span>
),
createConnector: () => {
if (shouldUseWalletConnect && walletConnectLegacy) {
if (shouldUseWalletConnect && walletConnect) {
return {
connector: walletConnectLegacy,
connector: walletConnect,
async qrCode() {
const { uri } = ((await walletConnectLegacy.getProvider()) as any)
?.connector;
const uri = await getWalletConnectUri(walletConnect);

return isAndroid()
? uri
: isIOS()
? `metamask://wc?uri=${encodeURIComponent(uri)}`
: `https://metamask.app.link/wc?uri=${encodeURIComponent(uri)}`;
},
};
Expand Down

1 comment on commit 91cfdcd

@vercel
Copy link

@vercel vercel bot commented on 91cfdcd Jul 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

crossbell-universe – ./

crossbell-dev.vercel.app
crossbell-universe-git-main-crossbell.vercel.app
crossbell-universe-crossbell.vercel.app

Please sign in to comment.