Skip to content

Commit 052892b

Browse files
authored
[v5] Fix WC session request (#2585)
1 parent 3b12c5c commit 052892b

File tree

10 files changed

+73
-62
lines changed

10 files changed

+73
-62
lines changed

.changeset/gorgeous-dolphins-raise.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Fix wallet not opened on WC sessions request event on page reload on mobile

packages/thirdweb/src/react/core/utils/handleWCSessionRequest.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

packages/thirdweb/src/react/web/ui/components/DragNDrop.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { StyledDiv } from "../design-system/elements.js";
88
import { Container } from "./basic.js";
99
import { Text } from "./text.js";
1010
import type { IconFC } from "../ConnectWallet/icons/types.js";
11-
import { isMobile } from "../../../core/utils/isMobile.js";
11+
import { isMobile } from "../../../../utils/web/isMobile.js";
1212

1313
/**
1414
* @internal

packages/thirdweb/src/react/web/wallets/shared/GetStartedScreen.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { useState } from "react";
2-
import { isMobile } from "../../../core/utils/isMobile.js";
32
import { QRCode } from "../../ui/components/QRCode.js";
43
import { Spacer } from "../../ui/components/Spacer.js";
54
import { Container, ModalHeader } from "../../ui/components/basic.js";
@@ -10,12 +9,13 @@ import { AppleIcon } from "../../ui/ConnectWallet/icons/AppleIcon.js";
109
import { ChromeIcon } from "../../ui/ConnectWallet/icons/ChromeIcon.js";
1110
import { PlayStoreIcon } from "../../ui/ConnectWallet/icons/PlayStoreIcon.js";
1211
import { Text } from "../../ui/components/text.js";
13-
import { openWindow } from "../../../core/utils/openWindow.js";
1412
import type { InjectedWalletLocale } from "../injected/locale/types.js";
1513
import type { WalletInfo } from "../../../../wallets/wallet-info.js";
1614
import { WalletImage } from "../../ui/components/WalletImage.js";
1715
import type { WalletId } from "../../../../wallets/wallet-types.js";
1816
import type { Wallet } from "../../../../wallets/interfaces/wallet.js";
17+
import { isMobile } from "../../../../utils/web/isMobile.js";
18+
import { openWindow } from "../../../../utils/web/openWindow.js";
1919

2020
/**
2121
* @internal

packages/thirdweb/src/react/web/wallets/shared/WalletConnectConnection.tsx

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import { ScanScreen } from "./ScanScreen.js";
22
import { useState, useRef, useEffect, useCallback } from "react";
3-
import { isAndroid, isIOS, isMobile } from "../../../core/utils/isMobile.js";
4-
import { handelWCSessionRequest } from "../../../core/utils/handleWCSessionRequest.js";
53
import { ConnectingScreen } from "./ConnectingScreen.js";
6-
import { openWindow } from "../../../core/utils/openWindow.js";
74
import { useWalletConnectionCtx } from "../../../core/hooks/others/useWalletConnectionCtx.js";
85
import type { InjectedWalletLocale } from "../injected/locale/types.js";
96
import type { Wallet } from "../../../../wallets/interfaces/wallet.js";
107
import type { WalletInfo } from "../../../../wallets/wallet-info.js";
118
import type { WCSupportedWalletIds } from "../../../../wallets/__generated__/wallet-ids.js";
9+
import { isMobile, isAndroid, isIOS } from "../../../../utils/web/isMobile.js";
10+
import { openWindow } from "../../../../utils/web/openWindow.js";
1211

1312
/**
1413
* QR Scan UI for connecting a specific wallet on desktop.
@@ -31,16 +30,6 @@ export const WalletConnectConnection: React.FC<{
3130
const connect = useCallback(() => {
3231
setErrorConnecting(false);
3332

34-
const platformUris = {
35-
ios: walletInfo.mobile.native || "",
36-
android: walletInfo.mobile.universal || "",
37-
other: walletInfo.mobile.universal || "",
38-
};
39-
40-
const onSessionRequestSent = isMobile()
41-
? () => handelWCSessionRequest(platformUris)
42-
: undefined;
43-
4433
wallet
4534
.connect({
4635
chain,
@@ -49,6 +38,12 @@ export const WalletConnectConnection: React.FC<{
4938
projectId: walletConnect?.projectId,
5039
showQrModal: false,
5140
onDisplayUri(uri) {
41+
const platformUris = {
42+
ios: walletInfo.mobile.native || "",
43+
android: walletInfo.mobile.universal || "",
44+
other: walletInfo.mobile.universal || "",
45+
};
46+
5247
setQrCodeUri(uri);
5348
if (isMobile()) {
5449
if (isAndroid()) {
@@ -66,7 +61,6 @@ export const WalletConnectConnection: React.FC<{
6661
}
6762
}
6863
},
69-
onSessionRequestSent,
7064
optionalChains: chains,
7165
},
7266
})

packages/thirdweb/src/react/core/utils/isMobile.ts renamed to packages/thirdweb/src/utils/web/isMobile.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { detectOS } from "../../../utils/detect-platform.js";
1+
import { detectOS } from "../detect-platform.js";
22

33
/**
44
* @internal

packages/thirdweb/src/wallets/create-wallet.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import type {
66
import type { Account, Wallet } from "./interfaces/wallet.js";
77
import type { Chain } from "../chains/types.js";
88
import { injectedProvider } from "./injected/mipdStore.js";
9-
import type { InjectedSupportedWalletIds } from "./__generated__/wallet-ids.js";
9+
import type {
10+
InjectedSupportedWalletIds,
11+
WCSupportedWalletIds,
12+
} from "./__generated__/wallet-ids.js";
1013

1114
import { createWalletEmitter } from "./wallet-emitter.js";
1215
import { trackConnect } from "../analytics/track.js";
@@ -131,7 +134,11 @@ export function createWallet<const ID extends WalletId>(
131134
connectedChain,
132135
doDisconnect,
133136
doSwitchChain,
134-
] = await autoConnectWC(options, emitter);
137+
] = await autoConnectWC(
138+
options,
139+
emitter,
140+
wallet.id as WCSupportedWalletIds,
141+
);
135142
// set the states
136143
account = connectedAccount;
137144
chain = connectedChain;
@@ -157,7 +164,11 @@ export function createWallet<const ID extends WalletId>(
157164
connectedChain,
158165
doDisconnect,
159166
doSwitchChain,
160-
] = await connectWC(options, emitter);
167+
] = await connectWC(
168+
options,
169+
emitter,
170+
wallet.id as WCSupportedWalletIds,
171+
);
161172
// set the states
162173
account = connectedAccount;
163174
chain = connectedChain;

packages/thirdweb/src/wallets/wallet-connect/index.ts

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ import { getDefaultAppMetadata } from "../utils/defaultDappMetadata.js";
2525
import type { WCSupportedWalletIds } from "../__generated__/wallet-ids.js";
2626
import type { DisconnectFn, SwitchChainFn } from "../types.js";
2727
import type { WalletEmitter } from "../wallet-emitter.js";
28+
import { isAndroid, isIOS, isMobile } from "../../utils/web/isMobile.js";
29+
import { openWindow } from "../../utils/web/openWindow.js";
30+
import { getWalletInfo } from "../__generated__/getWalletInfo.js";
2831

2932
type WCProvider = InstanceType<typeof EthereumProvider>;
3033

@@ -42,8 +45,9 @@ const defaultShowQrModal = true;
4245
export async function connectWC(
4346
options: WCConnectOptions,
4447
emitter: WalletEmitter<WCSupportedWalletIds>,
48+
walletId: WCSupportedWalletIds,
4549
): Promise<ReturnType<typeof onConnect>> {
46-
const provider = await initProvider(options);
50+
const provider = await initProvider(options, walletId);
4751
const wcOptions = options.walletConnect;
4852

4953
const targetChain = options?.chain || ethereum;
@@ -54,22 +58,12 @@ export async function connectWC(
5458
client: options.client,
5559
});
5660

57-
const { onDisplayUri, onSessionRequestSent } = wcOptions || {};
61+
const { onDisplayUri } = wcOptions || {};
5862

59-
if (onDisplayUri || onSessionRequestSent) {
63+
if (onDisplayUri) {
6064
if (onDisplayUri) {
6165
provider.events.addListener("display_uri", onDisplayUri);
6266
}
63-
64-
if (onSessionRequestSent) {
65-
provider.signer.client.on("session_request_sent", onSessionRequestSent);
66-
provider.events.addListener("disconnect", () => {
67-
provider.signer.client.off(
68-
"session_request_sent",
69-
onSessionRequestSent,
70-
);
71-
});
72-
}
7367
}
7468

7569
// If there no active session, or the chain is stale, force connect.
@@ -106,6 +100,7 @@ export async function connectWC(
106100
export async function autoConnectWC(
107101
options: WCAutoConnectOptions,
108102
emitter: WalletEmitter<WCSupportedWalletIds>,
103+
walletId: WCSupportedWalletIds,
109104
): Promise<ReturnType<typeof onConnect>> {
110105
const provider = await initProvider(
111106
options.savedConnectParams
@@ -121,6 +116,7 @@ export async function autoConnectWC(
121116
client: options.client,
122117
walletConnect: {},
123118
},
119+
walletId,
124120
);
125121

126122
const address = provider.accounts[0];
@@ -153,7 +149,11 @@ export async function autoConnectWC(
153149

154150
// Connection utils -----------------------------------------------------------------------------------------------
155151

156-
async function initProvider(options: WCConnectOptions) {
152+
async function initProvider(
153+
options: WCConnectOptions,
154+
walletId: WCSupportedWalletIds,
155+
) {
156+
const walletInfo = await getWalletInfo(walletId);
157157
const wcOptions = options.walletConnect;
158158
const { EthereumProvider, OPTIONAL_EVENTS, OPTIONAL_METHODS } = await import(
159159
"@walletconnect/ethereum-provider"
@@ -192,6 +192,34 @@ async function initProvider(options: WCConnectOptions) {
192192
disableProviderPing: true,
193193
});
194194

195+
function handleSessionRequest() {
196+
if (typeof window === "undefined") {
197+
return;
198+
}
199+
200+
if (!isMobile()) {
201+
return;
202+
}
203+
204+
const preferUniversal =
205+
walletInfo.mobile.universal || walletInfo.mobile.native || "";
206+
const preferNative =
207+
walletInfo.mobile.native || walletInfo.mobile.universal || "";
208+
209+
if (isAndroid()) {
210+
openWindow(preferUniversal);
211+
} else if (isIOS()) {
212+
openWindow(preferNative);
213+
} else {
214+
openWindow(preferUniversal);
215+
}
216+
}
217+
218+
provider.signer.client.on("session_request_sent", handleSessionRequest);
219+
provider.events.addListener("disconnect", () => {
220+
provider.signer.client.off("session_request_sent", handleSessionRequest);
221+
});
222+
195223
provider.events.setMaxListeners(Infinity);
196224

197225
return provider;

packages/thirdweb/src/wallets/wallet-connect/types.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,6 @@ export type WCConnectOptions = {
131131
* ```
132132
*/
133133
onDisplayUri?: (uri: string) => void;
134-
/**
135-
* Callback to be called to listen to the `session_request_sent` event emitted by WalletConnect's `EthereumProvider`.
136-
* This can be used to
137-
*/
138-
onSessionRequestSent?: () => void;
139134

140135
/**
141136
* Your project’s unique identifier that can be obtained at https://cloud.walletconnect.com/

0 commit comments

Comments
 (0)