Skip to content

Commit 97fc114

Browse files
committed
refactor code
1 parent 6a2b20b commit 97fc114

File tree

6 files changed

+15
-13
lines changed

6 files changed

+15
-13
lines changed

packages/modal/src/ui/components/ConnectWallet/ConnectWallet.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,7 @@ function ConnectWallet(props: ConnectWalletProps) {
153153
const isInjectedConnectorAndSingleChainNamespace = button.hasInjectedWallet && button.chainNamespaces?.length === 1;
154154
// if doesn't have wallet connect & doesn't have install links, must be a custom connector
155155
const isCustomConnector = !button.hasInjectedWallet && !button.hasWalletConnect && !button.hasInstallLinks;
156-
// for non-injected Metamask, handle it as a discoverable wallet to display QR code instead of treating as custom connector
157-
if (isInjectedConnectorAndSingleChainNamespace || (button.name !== WALLET_CONNECTORS.METAMASK && isCustomConnector)) {
156+
if (isInjectedConnectorAndSingleChainNamespace || isCustomConnector) {
158157
return handleExternalWalletClick({ connector: button.name });
159158
}
160159

packages/modal/src/ui/components/Root/Root.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,12 +303,12 @@ function Root(props: RootProps) {
303303
}, [] as ExternalButton[]);
304304

305305
// if metamask connector is not injected, use the registry button instead to display QR code
306-
const metamaskCustomConnectorIdx = installedConnectors.findIndex((button) => button.name === WALLET_CONNECTORS.METAMASK);
306+
const metamaskCustomConnectorIdx = installedConnectors.findIndex((x) => x.name === WALLET_CONNECTORS.METAMASK && !x.hasInjectedWallet);
307307
if (metamaskCustomConnectorIdx !== -1) {
308308
const metamaskCustomConnector = installedConnectors[metamaskCustomConnectorIdx];
309309
let metamaskRegistryButton = allButtons.find((button) => button.name === WALLET_CONNECTORS.METAMASK);
310310
if (!metamaskRegistryButton) {
311-
// if metamask is not in the registry, use the default metamask registry item
311+
// use the default metamask registry item if it's not in the registry
312312
metamaskRegistryButton = generateWalletButtons({
313313
[WALLET_CONNECTORS.METAMASK]: DEFAULT_METAMASK_WALLET_REGISTRY_ITEM,
314314
})[0];

packages/modal/src/ui/components/Widget/Widget.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,14 @@ function Widget(props: WidgetProps) {
173173
// TODO: maybe move this inside root
174174
if (!modalState.modalVisibility) return;
175175
if (typeof modalState.externalWalletsConfig === "object") {
176+
// auto connect to WC if not injected to generate QR code URI for mobile connection
176177
const wcAvailable = (modalState.externalWalletsConfig[WALLET_CONNECTORS.WALLET_CONNECT_V2]?.showOnModal || false) !== false;
177-
const mmAvailable = !modalState.externalWalletsConfig[WALLET_CONNECTORS.METAMASK]?.isInjected;
178178
if (wcAvailable && !modalState.walletConnectUri && typeof handleExternalWalletClick === "function") {
179179
handleExternalWalletClick({ connector: WALLET_CONNECTORS.WALLET_CONNECT_V2 });
180180
}
181+
182+
// auto connect to MetaMask if not injected to generate QR code URI for mobile connection
183+
const mmAvailable = !modalState.externalWalletsConfig[WALLET_CONNECTORS.METAMASK]?.isInjected;
181184
if (mmAvailable && !modalState.metamaskConnectUri && typeof handleExternalWalletClick === "function") {
182185
handleExternalWalletClick({ connector: WALLET_CONNECTORS.METAMASK });
183186
}

packages/no-modal/src/base/connector/baseConnector.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ export abstract class BaseConnector<T> extends SafeEventEmitter<ConnectorEvents>
4848
public abstract get provider(): IProvider | null;
4949

5050
checkConnectionRequirements(): void {
51-
// we reconnect without killing existing wallet connect session on calling connect again.
51+
// we reconnect without killing existing Wallet Connect or Metamask Connect session on calling connect again.
5252
if (this.name === WALLET_CONNECTORS.WALLET_CONNECT_V2 && this.status === CONNECTOR_STATUS.CONNECTING) return;
53+
if (this.name === WALLET_CONNECTORS.METAMASK && !this.isInjected && this.status === CONNECTOR_STATUS.CONNECTING) return;
5354

5455
if (this.status === CONNECTOR_STATUS.CONNECTING) throw WalletInitializationError.notReady("Already connecting");
5556
if (this.status === CONNECTOR_STATUS.CONNECTED) throw WalletLoginError.connectionError("Already connected");

packages/no-modal/src/connectors/metamask-connector/metamaskConnector.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,10 @@ class MetaMaskConnector extends BaseEvmConnector<void> {
8080
};
8181

8282
// initialize the MetaMask SDK
83-
const metamaskOptions = deepmerge(this.metamaskOptions || { headless: true }, { dappMetadata: appMetadata }); // TODO: only use headless for Modal SDK
83+
const metamaskOptions = deepmerge(this.metamaskOptions || {}, { dappMetadata: appMetadata });
8484
this.metamaskSDK = new MetaMaskSDK(metamaskOptions);
85-
// Note: in case there is an existing SDK instance in memory (window.mmsdk exists), it won't initialize the new SDK instance and return the existing instance instead of undefined
85+
// Work around: in case there is an existing SDK instance in memory (window.mmsdk exists), it won't initialize the new SDK instance again
86+
// and return the existing instance instead of undefined (this is an assumption, not sure if it's a bug or feature of the MetaMask SDK)
8687
const initResult = await this.metamaskSDK.init();
8788
if (initResult) {
8889
this.metamaskSDK = initResult;
@@ -110,11 +111,8 @@ class MetaMaskConnector extends BaseEvmConnector<void> {
110111
if (this.status !== CONNECTOR_STATUS.CONNECTING) {
111112
this.status = CONNECTOR_STATUS.CONNECTING;
112113
this.emit(CONNECTOR_EVENTS.CONNECTING, { connector: WALLET_CONNECTORS.METAMASK });
113-
if (!this.metamaskSDK.isInitialized()) {
114-
await this.metamaskSDK.init();
115-
}
116-
// when metamask is not injected and headless is true, broadcast the uri to the login modal
117114
if (!this.metamaskSDK.isExtensionActive() && this.metamaskOptions.headless) {
115+
// when metamask is not injected and headless is true, broadcast the uri to the login modal
118116
this.metamaskSDK.getProvider().on("display_uri", (uri) => {
119117
this.updateConnectorData({ uri } as MetaMaskConnectorData);
120118
});

packages/no-modal/src/noModal.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ export class Web3AuthNoModal extends SafeEventEmitter<Web3AuthNoModalEvents> imp
370370
}
371371

372372
protected async loadConnectors({ projectConfig, modalMode }: { projectConfig: ProjectConfig; modalMode?: boolean }) {
373-
// always add auth and metaMask connector
373+
// always add auth connector
374374
const connectorFns = [...(this.coreOptions.connectors || []), authConnector()];
375375
const config = {
376376
projectConfig,
@@ -412,6 +412,7 @@ export class Web3AuthNoModal extends SafeEventEmitter<Web3AuthNoModalEvents> imp
412412
}
413413

414414
// it's safe to add it here as if there is a MetaMask injected provider, this won't override it
415+
// only set headless to true if modal SDK is used, otherwise just use the modal from native Metamask SDK
415416
connectorFns.push(metaMaskConnector(modalMode ? { headless: true } : undefined));
416417

417418
// add WalletConnectV2 connector if external wallets are enabled

0 commit comments

Comments
 (0)