Skip to content

Commit a346111

Browse files
authored
Headless WalletConnect Integration (#3181)
1 parent 51e7ada commit a346111

33 files changed

+2591
-966
lines changed

.changeset/cool-coats-march.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
"thirdweb": minor
3+
---
4+
5+
Adds headless functions for creating and managing a WalletConnect session with a connected wallet
6+
7+
### `createWalletConnectClient`
8+
9+
```ts
10+
import { createWalletConnectClient } from "thirdweb/wallets/wallet-connect";
11+
12+
createWalletConnectClient({
13+
client: client,
14+
wallet: wallet,
15+
onConnect: (session: any) => {
16+
alert("Connected");
17+
},
18+
onDisconnect: (session: any) => {
19+
alert("Disconnected");
20+
},
21+
});
22+
```
23+
24+
### `createWalletConnectSession`
25+
26+
```ts
27+
import { createWalletConnectSession } from "thirdweb/wallets/wallet-connect";
28+
29+
createWalletConnectSession({
30+
walletConnectClient: wcClient,
31+
uri: "wc:...",
32+
});
33+
```
34+
35+
### `getWalletConnectSessions`
36+
37+
```ts
38+
import {
39+
getWalletConnectSession,
40+
type WalletConnectSession,
41+
} from "thirdweb/wallets/wallet-connect";
42+
43+
const sessions: WalletConnectSession[] = await getWalletConnectSessions();
44+
```
45+
46+
### `disconnectWalletConnectClient`
47+
48+
```ts
49+
import { disconnectWalletConnectClient } from "thirdweb/wallets/wallet-connect";
50+
51+
disconnectWalletConnectClient({ session, walletConnectClient: wcClient });
52+
```

packages/thirdweb/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@
189189
"@radix-ui/react-tooltip": "1.0.7",
190190
"@tanstack/react-query": "5.29.2",
191191
"@walletconnect/ethereum-provider": "2.12.2",
192+
"@walletconnect/sign-client": "^2.13.1",
192193
"abitype": "1.0.0",
193194
"fast-text-encoding": "^1.0.6",
194195
"fuse.js": "7.0.0",
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export {
2+
createWalletConnectClient,
3+
createWalletConnectSession,
4+
disconnectWalletConnectSession,
5+
getActiveWalletConnectSessions,
6+
} from "../../wallets/wallet-connect/consumer/index.js";
7+
export type {
8+
WalletConnectClient,
9+
WalletConnectSession,
10+
} from "../../wallets/wallet-connect/consumer/types.js";

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ export function createWallet<const ID extends WalletId>(
150150
}
151151

152152
if (options && "client" in options) {
153-
const { autoConnectWC } = await import("./wallet-connect/index.js");
153+
const { autoConnectWC } = await import(
154+
"./wallet-connect/controller.js"
155+
);
154156

155157
const [
156158
connectedAccount,
@@ -179,7 +181,9 @@ export function createWallet<const ID extends WalletId>(
179181
},
180182
connect: async (options) => {
181183
async function wcConnect(wcOptions: WCConnectOptions) {
182-
const { connectWC } = await import("./wallet-connect/index.js");
184+
const { connectWC } = await import(
185+
"./wallet-connect/controller.js"
186+
);
183187

184188
const [
185189
connectedAccount,

packages/thirdweb/src/wallets/eip5792/get-calls-status.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { isInAppWallet } from "../in-app/core/wallet/index.js";
44
import { getInjectedProvider } from "../injected/index.js";
55
import type { Wallet } from "../interfaces/wallet.js";
66
import { isSmartWallet } from "../smart/index.js";
7-
import { isWalletConnect } from "../wallet-connect/index.js";
7+
import { isWalletConnect } from "../wallet-connect/controller.js";
88
import type { GetCallsStatusResponse, WalletSendCallsId } from "./types.js";
99

1010
export type GetCallsStatusOptions = {

packages/thirdweb/src/wallets/eip5792/get-capabilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { isInAppWallet } from "../in-app/core/wallet/index.js";
44
import { getInjectedProvider } from "../injected/index.js";
55
import type { Wallet } from "../interfaces/wallet.js";
66
import { isSmartWallet } from "../smart/index.js";
7-
import { isWalletConnect } from "../wallet-connect/index.js";
7+
import { isWalletConnect } from "../wallet-connect/controller.js";
88
import type { WalletId } from "../wallet-types.js";
99
import type { WalletCapabilities, WalletCapabilitiesRecord } from "./types.js";
1010

packages/thirdweb/src/wallets/eip5792/send-calls.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { isInAppWallet } from "../in-app/core/wallet/index.js";
1313
import { getInjectedProvider } from "../injected/index.js";
1414
import type { Wallet } from "../interfaces/wallet.js";
1515
import { isSmartWallet } from "../smart/index.js";
16-
import { isWalletConnect } from "../wallet-connect/index.js";
16+
import { isWalletConnect } from "../wallet-connect/controller.js";
1717
import type { WalletId } from "../wallet-types.js";
1818
import type {
1919
EIP5792Call,

packages/thirdweb/src/wallets/eip5792/show-calls-status.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { isInAppWallet } from "../in-app/core/wallet/index.js";
33
import { getInjectedProvider } from "../injected/index.js";
44
import type { Wallet } from "../interfaces/wallet.js";
55
import { isSmartWallet } from "../smart/index.js";
6-
import { isWalletConnect } from "../wallet-connect/index.js";
6+
import { isWalletConnect } from "../wallet-connect/controller.js";
77
import type { WalletSendCallsId } from "./types.js";
88

99
export type ShowCallsStatusOptions = {

packages/thirdweb/src/wallets/in-app/core/constants/settings.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ export const DEVICE_SHARE_LOCAL_STORAGE_NAME = (
6060
userId: string,
6161
) => `${DEVICE_SHARE_LOCAL_STORAGE_PREFIX}-${clientId}-${userId}`;
6262

63+
/**
64+
* @internal
65+
*/
66+
export const WALLET_CONNECT_SESSIONS_LOCAL_STORAGE_NAME = (clientId: string) =>
67+
`walletConnectSessions-${clientId}`;
68+
6369
/**
6470
* @internal
6571
*/

packages/thirdweb/src/wallets/in-app/web/utils/Storage/LocalStorage.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
AUTH_TOKEN_LOCAL_STORAGE_NAME,
33
DEVICE_SHARE_LOCAL_STORAGE_NAME,
44
PASSKEY_CREDENTIAL_ID_LOCAL_STORAGE_NAME,
5+
WALLET_CONNECT_SESSIONS_LOCAL_STORAGE_NAME,
56
WALLET_USER_ID_LOCAL_STORAGE_NAME,
67
} from "../../../core/constants/settings.js";
78

@@ -44,6 +45,25 @@ export class LocalStorage {
4445
return false;
4546
}
4647

48+
/**
49+
* @internal
50+
*/
51+
async getWalletConnectSessions(): Promise<string | null> {
52+
return this.getItem(
53+
WALLET_CONNECT_SESSIONS_LOCAL_STORAGE_NAME(this.clientId),
54+
);
55+
}
56+
57+
/**
58+
* @internal
59+
*/
60+
async saveWalletConnectSessions(stringifiedSessions: string): Promise<void> {
61+
await this.setItem(
62+
WALLET_CONNECT_SESSIONS_LOCAL_STORAGE_NAME(this.clientId),
63+
stringifiedSessions,
64+
);
65+
}
66+
4767
/**
4868
* @internal
4969
*/

0 commit comments

Comments
 (0)