Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add thirdweb inAppWallet connector #4408

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/many-spies-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wagmi/connectors": minor
---

thirdweb in-app wallet connector
6 changes: 5 additions & 1 deletion packages/connectors/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
"peerDependenciesMeta": {
"typescript": {
"optional": true
},
"thirdweb": {
"optional": true
}
},
"dependencies": {
Expand All @@ -50,7 +53,8 @@
"@safe-global/safe-apps-provider": "0.18.4",
"@safe-global/safe-apps-sdk": "9.1.0",
"@walletconnect/ethereum-provider": "2.17.0",
"cbw-sdk": "npm:@coinbase/[email protected]"
"cbw-sdk": "npm:@coinbase/[email protected]",
"thirdweb": "5.68.0"
},
"devDependencies": {
"@wagmi/core": "workspace:*",
Expand Down
2 changes: 2 additions & 0 deletions packages/connectors/src/exports/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ export {
walletConnect,
} from '../walletConnect.js'

export { type InAppWalletParameters, inAppWallet } from '../thirdweb.js'

export { version } from '../version.js'
18 changes: 18 additions & 0 deletions packages/connectors/src/thirdweb.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { config } from '@wagmi/test'
import { expect, test } from 'vitest'

import { inAppWallet } from './thirdweb.js'

/**
* To manually test this connector:
* 1. get a clientId from https://thirdweb.com
* 3. add this connector to the playground
*/
test('setup', () => {
const connectorFn = inAppWallet({
clientId: 'testClientId',
strategy: 'google',
})
const connector = config._internal.connectors.setup(connectorFn)
expect(connector.name).toEqual('In-App wallet')
})
102 changes: 102 additions & 0 deletions packages/connectors/src/thirdweb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { type CreateConnectorFn, createConnector } from '@wagmi/core'
import { createThirdwebClient, defineChain } from 'thirdweb'
import {
EIP1193,
type InAppWalletConnectionOptions,
ecosystemWallet,
inAppWallet as thirdwebInAppWallet,
} from 'thirdweb/wallets'
import type { InAppWalletCreationOptions } from 'thirdweb/wallets/in-app'
import type { Prettify } from 'viem'
import type { Address } from 'viem/accounts'

export type InAppWalletParameters = Prettify<
Omit<InAppWalletConnectionOptions, 'client'> &
InAppWalletCreationOptions & {
clientId: string
ecosystemId?: `ecosystem.${string}`
}
>

/**
* Connect to an in-app wallet using the auth strategy of your choice.
* @param args - Options for the in-app wallet connection.
* @returns A wagmi connector.
* @example
* ```ts
* import { http, createConfig } from "wagmi";
* import { inAppWallet } from "@wagmi/connectors";
*
* export const config = createConfig({
* chains: [sepolia],
* connectors: [
* inAppWallet({
* clientId: "...",
* strategy: "google",
* }),
* ],
* transports: {
* [sepolia.id]: http(),
* },
* });
* ```
*/
export function inAppWallet(args: InAppWalletParameters): CreateConnectorFn {
const client = createThirdwebClient({ clientId: args.clientId })
const wallet = args.ecosystemId
? ecosystemWallet(args.ecosystemId, { partnerId: args.partnerId })
: thirdwebInAppWallet(args)
return createConnector((config) => ({
id: 'in-app-wallet',
name: 'In-App wallet',
type: 'in-app',
connect: async (params) => {
const chain = defineChain(params?.chainId || 1)
const account = params?.isReconnecting
? await wallet.autoConnect({
client,
chain,
})
: await wallet.connect(args)
return { accounts: [account.address as Address], chainId: chain.id }
},
disconnect: async () => {
await wallet.disconnect()
},
getAccounts: async () => {
const account = wallet.getAccount()
if (!account) {
throw new Error('Wallet not connected')
}
return [account.address as Address]
},
getChainId: async () => {
return wallet.getChain()?.id || 1
},
getProvider: async (params) => {
return EIP1193.toProvider({
wallet,
client,
chain: wallet.getChain() || defineChain(params?.chainId || 1),
})
},
isAuthorized: async () => true,
switchChain: async (params) => {
const chain = config.chains.find((x) => x.id === params.chainId)
if (!chain) {
throw new Error(`Chain ${params.chainId} not supported`)
}
await wallet.switchChain(defineChain(chain.id))
return chain
},
onAccountsChanged: () => {
// no-op
},
onChainChanged: () => {
// no-op
},
onDisconnect: () => {
// no-op
},
}))
}
Loading