Skip to content

Commit fd17420

Browse files
Add thirdweb inAppWallet connector
1 parent a13aa8d commit fd17420

File tree

11 files changed

+1816
-55
lines changed

11 files changed

+1816
-55
lines changed

.changeset/many-spies-behave.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@wagmi/connectors": minor
3+
---
4+
5+
thirdweb in-app wallet connector

packages/connectors/package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,15 @@
3737
"peerDependencies": {
3838
"@wagmi/core": "workspace:*",
3939
"typescript": ">=5.0.4",
40+
"thirdweb": "^5.68.0",
4041
"viem": "2.x"
4142
},
4243
"peerDependenciesMeta": {
4344
"typescript": {
4445
"optional": true
46+
},
47+
"thirdweb": {
48+
"optional": true
4549
}
4650
},
4751
"dependencies": {
@@ -54,7 +58,8 @@
5458
},
5559
"devDependencies": {
5660
"@wagmi/core": "workspace:*",
57-
"msw": "^2.4.9"
61+
"msw": "^2.4.9",
62+
"thirdweb": "5.68.0"
5863
},
5964
"contributors": ["awkweb.eth <[email protected]>", "jxom.eth <[email protected]>"],
6065
"funding": "https://github.com/sponsors/wevm",

packages/connectors/src/exports/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ export {
2020
walletConnect,
2121
} from '../walletConnect.js'
2222

23+
export { inAppWallet } from '../thirdweb.js'
24+
2325
export { version } from '../version.js'
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { config } from '@wagmi/test'
2+
import { expect, test } from 'vitest'
3+
4+
import { createThirdwebClient } from 'thirdweb'
5+
import { inAppWallet } from './thirdweb.js'
6+
7+
/**
8+
* To manually test this connector:
9+
* 1. get a clientId from https://thirdweb.com
10+
* 3. add this connector to the playground
11+
*/
12+
test('setup', () => {
13+
const connectorFn = inAppWallet({
14+
client: createThirdwebClient({ clientId: 'testClientId' }),
15+
strategy: 'google',
16+
})
17+
const connector = config._internal.connectors.setup(connectorFn)
18+
expect(connector.name).toEqual('In-App wallet')
19+
})
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import { type CreateConnectorFn, createConnector } from '@wagmi/core'
2+
import { defineChain } from 'thirdweb'
3+
import {
4+
EIP1193,
5+
type InAppWalletConnectionOptions,
6+
ecosystemWallet,
7+
inAppWallet as thirdwebInAppWallet,
8+
} from 'thirdweb/wallets'
9+
import type { InAppWalletCreationOptions } from 'thirdweb/wallets/in-app'
10+
import type { Address } from 'viem/accounts'
11+
12+
/**
13+
* Connect to an in-app wallet using the auth strategy of your choice.
14+
* @param args - Options for the in-app wallet connection.
15+
* @returns A wagmi connector.
16+
* @example
17+
* ```ts
18+
* import { createThirdwebClient } from "thirdweb";
19+
* import { http, createConfig } from "wagmi";
20+
* import { inAppWallet } from "@wagmi/connectors";
21+
*
22+
* const client = createThirdwebClient({
23+
* clientId: "...",
24+
* })
25+
*
26+
* export const config = createConfig({
27+
* chains: [sepolia],
28+
* connectors: [
29+
* inAppWallet({
30+
* client,
31+
* strategy: "google",
32+
* }),
33+
* ],
34+
* transports: {
35+
* [sepolia.id]: http(),
36+
* },
37+
* });
38+
* ```
39+
*/
40+
export function inAppWallet(
41+
args: InAppWalletConnectionOptions &
42+
InAppWalletCreationOptions & { ecosystemId?: `ecosystem.${string}` },
43+
): CreateConnectorFn {
44+
const { client, ecosystemId } = args
45+
const wallet = ecosystemId
46+
? ecosystemWallet(ecosystemId, { partnerId: args.partnerId })
47+
: thirdwebInAppWallet(args)
48+
return createConnector((config) => ({
49+
id: 'in-app-wallet',
50+
name: 'In-App wallet',
51+
type: 'in-app',
52+
connect: async (params) => {
53+
const chain = defineChain(params?.chainId || 1)
54+
const account = params?.isReconnecting
55+
? await wallet.autoConnect({
56+
client,
57+
chain,
58+
})
59+
: await wallet.connect(args)
60+
return { accounts: [account.address as Address], chainId: chain.id }
61+
},
62+
disconnect: async () => {
63+
await wallet.disconnect()
64+
},
65+
getAccounts: async () => {
66+
const account = wallet.getAccount()
67+
if (!account) {
68+
throw new Error('Wallet not connected')
69+
}
70+
return [account.address as Address]
71+
},
72+
getChainId: async () => {
73+
return wallet.getChain()?.id || 1
74+
},
75+
getProvider: async (params) => {
76+
return EIP1193.toProvider({
77+
wallet,
78+
client,
79+
chain: wallet.getChain() || defineChain(params?.chainId || 1),
80+
})
81+
},
82+
isAuthorized: async () => true,
83+
switchChain: async (params) => {
84+
const chain = config.chains.find((x) => x.id === params.chainId)
85+
if (!chain) {
86+
throw new Error(`Chain ${params.chainId} not supported`)
87+
}
88+
await wallet.switchChain(defineChain(chain.id))
89+
return chain
90+
},
91+
onAccountsChanged: () => {
92+
// no-op
93+
},
94+
onChainChanged: () => {
95+
// no-op
96+
},
97+
onDisconnect: () => {
98+
// no-op
99+
},
100+
}))
101+
}

0 commit comments

Comments
 (0)