|
| 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