Skip to content

Commit 8bbee03

Browse files
authored
[SDK] Fix: Allow smart wallets for site linking (#6015)
1 parent b38604c commit 8bbee03

File tree

8 files changed

+76
-13
lines changed

8 files changed

+76
-13
lines changed

.changeset/khaki-singers-leave.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@thirdweb-dev/wagmi-adapter": patch
3+
"thirdweb": patch
4+
---
5+
6+
Fixes issue with smart wallets used on SiteLink and SiteEmbed

packages/thirdweb/src/react/web/ui/SiteEmbed.test.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from "vitest";
2-
import { render } from "../../../../test/src/react-render.js";
2+
import { render, waitFor } from "../../../../test/src/react-render.js";
33
import { TEST_CLIENT } from "../../../../test/src/test-clients.js";
44
import { SiteEmbed } from "./SiteEmbed.js";
55

@@ -21,4 +21,19 @@ describe("SiteEmbed", () => {
2121
render(<SiteEmbed src={testUrl} client={{} as any} />),
2222
).toThrow("The SiteEmbed client must have a clientId");
2323
});
24+
25+
it("uses inApp wallet when wallet is a smart wallet", async () => {
26+
const testUrl = "https://thirdweb.com/";
27+
const { container } = render(
28+
<SiteEmbed src={testUrl} client={TEST_CLIENT} />,
29+
{
30+
setConnectedWallet: true,
31+
walletId: "smart",
32+
},
33+
);
34+
35+
const iframe = container.querySelector("iframe");
36+
expect(iframe).toBeTruthy();
37+
await waitFor(() => expect(iframe?.src).toContain("walletId=inApp"));
38+
});
2439
});

packages/thirdweb/src/react/web/ui/SiteEmbed.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ export function SiteEmbed({
5353
} = useQuery({
5454
queryKey: ["site-embed", walletId, src, client.clientId, ecosystem],
5555
enabled:
56-
activeWallet && (isEcosystemWallet(activeWallet) || walletId === "inApp"),
56+
activeWallet &&
57+
(isEcosystemWallet(activeWallet) ||
58+
walletId === "inApp" ||
59+
walletId === "smart"),
5760
queryFn: async () => {
5861
const storage = new ClientScopedStorage({
5962
storage: webLocalStorage,
@@ -70,7 +73,7 @@ export function SiteEmbed({
7073

7174
const url = new URL(src);
7275
if (walletId) {
73-
url.searchParams.set("walletId", walletId);
76+
url.searchParams.set("walletId", walletId === "smart" ? "inApp" : walletId);
7477
}
7578
if (authProvider) {
7679
url.searchParams.set("authProvider", authProvider);

packages/thirdweb/src/react/web/ui/SiteLink.test.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,21 @@ describe("SiteLink", () => {
4545
expect(anchor).toBeTruthy();
4646
await waitFor(() => expect(anchor?.href).toContain("walletId="));
4747
});
48+
49+
it("uses inApp wallet when wallet is a smart wallet", async () => {
50+
const testUrl = "https://example.com/";
51+
const { container } = render(
52+
<SiteLink href={testUrl} client={TEST_CLIENT}>
53+
Test Link
54+
</SiteLink>,
55+
{
56+
setConnectedWallet: true,
57+
walletId: "smart",
58+
},
59+
);
60+
61+
const anchor = container.querySelector("a");
62+
expect(anchor).toBeTruthy();
63+
await waitFor(() => expect(anchor?.href).toContain("walletId=inApp"));
64+
});
4865
});

packages/thirdweb/src/react/web/ui/SiteLink.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ export function SiteLink({
5353
} = useQuery({
5454
queryKey: ["site-link", walletId, href, client.clientId, ecosystem],
5555
enabled:
56-
activeWallet && (isEcosystemWallet(activeWallet) || walletId === "inApp"),
56+
activeWallet &&
57+
(isEcosystemWallet(activeWallet) ||
58+
walletId === "inApp" ||
59+
walletId === "smart"),
5760
queryFn: async () => {
5861
const storage = new ClientScopedStorage({
5962
storage: webLocalStorage,
@@ -70,7 +73,7 @@ export function SiteLink({
7073

7174
const url = new URL(href);
7275
if (walletId) {
73-
url.searchParams.set("walletId", walletId);
76+
url.searchParams.set("walletId", walletId === "smart" ? "inApp" : walletId);
7477
}
7578
if (authProvider) {
7679
url.searchParams.set("authProvider", authProvider);

packages/thirdweb/test/src/SetConnectedWallet.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ import { useEffect, useRef } from "react";
22
import { createWalletAdapter } from "../../src/adapters/wallet-adapter.js";
33
import { ethereum } from "../../src/chains/chain-definitions/ethereum.js";
44
import { useConnect } from "../../src/react/core/hooks/wallets/useConnect.js";
5+
import type { WalletId } from "../../src/wallets/wallet-types.js";
6+
import type { Wallet } from "../../src/wallets/interfaces/wallet.js";
57
import { TEST_CLIENT } from "./test-clients.js";
68
import { TEST_ACCOUNT_A } from "./test-wallets.js";
79

8-
export const SetConnectedWallet = () => {
10+
export const SetConnectedWallet = (props: { id?: WalletId }) => {
11+
const { id } = props;
912
const connectStarted = useRef(false);
1013
const { connect } = useConnect();
1114

@@ -21,9 +24,13 @@ export const SetConnectedWallet = () => {
2124
adaptedAccount: TEST_ACCOUNT_A,
2225
client: TEST_CLIENT,
2326
chain: ethereum,
24-
onDisconnect: () => {},
25-
switchChain: () => {},
26-
});
27+
onDisconnect: () => { },
28+
switchChain: () => { },
29+
}) as Wallet;
30+
31+
if (id) {
32+
wallet.id = id;
33+
}
2734

2835
connect(wallet);
2936
}, []);

packages/thirdweb/test/src/react-render.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { RenderOptions } from "@testing-library/react";
44
import type { ReactElement } from "react";
55
import { ThirdwebProvider } from "../../src/react/web/providers/thirdweb-provider.js";
66
import { SetConnectedWallet } from "./SetConnectedWallet.js";
7+
import type { WalletId } from "src/wallets/wallet-types.js";
78

89
const Providers = ({ children }: { children: React.ReactNode }) => {
910
return <ThirdwebProvider>{children}</ThirdwebProvider>;
@@ -13,11 +14,14 @@ const customRender = (
1314
ui: ReactElement,
1415
options?: Omit<RenderOptions, "wrapper"> & {
1516
setConnectedWallet?: boolean;
17+
walletId?: WalletId;
1618
},
1719
) => {
1820
return render(
1921
<div>
20-
{options?.setConnectedWallet ? <SetConnectedWallet /> : null}
22+
{options?.setConnectedWallet ? (
23+
<SetConnectedWallet id={options.walletId} />
24+
) : null}
2125
{ui}
2226
</div>,
2327
{ wrapper: Providers, ...options },

packages/wagmi-adapter/src/connector.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { type CreateConnectorFn, createConnector } from "@wagmi/core";
22
import type { Prettify } from "@wagmi/core/chains";
33
import { type ThirdwebClient, defineChain, getAddress } from "thirdweb";
4-
import { autoConnect } from "thirdweb/wallets";
54
import {
65
EIP1193,
76
type InAppWalletConnectionOptions,
@@ -55,7 +54,7 @@ type StorageItem = { "tw.lastChainId": number };
5554
* inAppWalletConnector({
5655
* client,
5756
* // optional: turn on smart accounts
58-
* smartAccounts: {
57+
* smartAccount: {
5958
* sponsorGas: true,
6059
* chain: thirdwebChain(sepolia)
6160
* }
@@ -95,10 +94,18 @@ export function inAppWalletConnector(
9594
connect: async (params) => {
9695
const lastChainId = await config.storage?.getItem("tw.lastChainId");
9796
if (params?.isReconnecting) {
98-
const account = await wallet.autoConnect({
97+
const { autoConnect } = await import("thirdweb/wallets");
98+
await autoConnect({
9999
client,
100100
chain: defineChain(lastChainId || 1),
101+
wallets: [wallet],
101102
});
103+
104+
const account = wallet.getAccount();
105+
if (!account) {
106+
throw new Error("Wallet failed to reconnect");
107+
}
108+
102109
return {
103110
accounts: [getAddress(account.address)],
104111
chainId: lastChainId || 1,
@@ -137,6 +144,7 @@ export function inAppWalletConnector(
137144
const lastChainId = await config.storage?.getItem("tw.lastChainId");
138145
const chain = defineChain(params?.chainId || lastChainId || 1);
139146
if (!wallet.getAccount()) {
147+
const { autoConnect } = await import("thirdweb/wallets");
140148
await autoConnect({
141149
client,
142150
chain,

0 commit comments

Comments
 (0)