|
| 1 | +import { type CreateConnectorFn, createConnector } from "@wagmi/core"; |
| 2 | +import type { Prettify } from "@wagmi/core/chains"; |
| 3 | +import { createThirdwebClient, defineChain, getAddress } from "thirdweb"; |
| 4 | +import { |
| 5 | + EIP1193, |
| 6 | + type InAppWalletConnectionOptions, |
| 7 | + ecosystemWallet, |
| 8 | + inAppWallet as thirdwebInAppWallet, |
| 9 | +} from "thirdweb/wallets"; |
| 10 | +import type { InAppWalletCreationOptions } from "thirdweb/wallets/in-app"; |
| 11 | + |
| 12 | +export type InAppWalletParameters = Prettify< |
| 13 | + Omit<InAppWalletConnectionOptions, "client"> & |
| 14 | + InAppWalletCreationOptions & { |
| 15 | + clientId: string; |
| 16 | + ecosystemId?: `ecosystem.${string}`; |
| 17 | + } |
| 18 | +>; |
| 19 | + |
| 20 | +type Provider = EIP1193.EIP1193Provider | undefined; |
| 21 | +type Properties = Record<string, unknown>; |
| 22 | +type StorageItem = { "tw.lastChainId": number }; |
| 23 | + |
| 24 | +/** |
| 25 | + * Connect to an in-app wallet using the auth strategy of your choice. |
| 26 | + * @param args - Options for the in-app wallet connection. |
| 27 | + * @returns A wagmi connector. |
| 28 | + * @example |
| 29 | + * ```ts |
| 30 | + * import { http, createConfig } from "wagmi"; |
| 31 | + * import { inAppWalletConnector } from "@thirdweb-dev/wagmi-adapter"; |
| 32 | + * |
| 33 | + * export const config = createConfig({ |
| 34 | + * chains: [sepolia], |
| 35 | + * connectors: [ |
| 36 | + * inAppWalletConnector({ |
| 37 | + * clientId: "...", |
| 38 | + * strategy: "google", |
| 39 | + * }), |
| 40 | + * ], |
| 41 | + * transports: { |
| 42 | + * [sepolia.id]: http(), |
| 43 | + * }, |
| 44 | + * }); |
| 45 | + * ``` |
| 46 | + */ |
| 47 | +export function inAppWalletConnector( |
| 48 | + args: InAppWalletParameters, |
| 49 | +): CreateConnectorFn<Provider, Properties, StorageItem> { |
| 50 | + const client = createThirdwebClient({ clientId: args.clientId }); |
| 51 | + const wallet = args.ecosystemId |
| 52 | + ? ecosystemWallet(args.ecosystemId, { partnerId: args.partnerId }) |
| 53 | + : thirdwebInAppWallet(args); |
| 54 | + return createConnector((config) => ({ |
| 55 | + id: "in-app-wallet", |
| 56 | + name: "In-App wallet", |
| 57 | + type: "in-app", |
| 58 | + connect: async (params) => { |
| 59 | + const lastChainId = await config.storage?.getItem("tw.lastChainId"); |
| 60 | + const chain = defineChain(params?.chainId || lastChainId || 1); |
| 61 | + const options = { |
| 62 | + client, |
| 63 | + chain, |
| 64 | + ...args, |
| 65 | + } as unknown as InAppWalletConnectionOptions; |
| 66 | + const account = params?.isReconnecting |
| 67 | + ? await wallet.autoConnect({ |
| 68 | + client, |
| 69 | + chain, |
| 70 | + }) |
| 71 | + : await wallet.connect(options); |
| 72 | + await config.storage?.setItem("tw.lastChainId", chain.id); |
| 73 | + return { accounts: [getAddress(account.address)], chainId: chain.id }; |
| 74 | + }, |
| 75 | + disconnect: async () => { |
| 76 | + await wallet.disconnect(); |
| 77 | + }, |
| 78 | + getAccounts: async () => { |
| 79 | + const account = wallet.getAccount(); |
| 80 | + if (!account) { |
| 81 | + throw new Error("Wallet not connected"); |
| 82 | + } |
| 83 | + return [getAddress(account.address)]; |
| 84 | + }, |
| 85 | + getChainId: async () => { |
| 86 | + return wallet.getChain()?.id || 1; |
| 87 | + }, |
| 88 | + getProvider: async (params) => { |
| 89 | + return EIP1193.toProvider({ |
| 90 | + wallet, |
| 91 | + client, |
| 92 | + chain: wallet.getChain() || defineChain(params?.chainId || 1), |
| 93 | + }); |
| 94 | + }, |
| 95 | + isAuthorized: async () => !!wallet.getAccount(), |
| 96 | + switchChain: async (params) => { |
| 97 | + const chain = config.chains.find((x) => x.id === params.chainId); |
| 98 | + if (!chain) { |
| 99 | + throw new Error(`Chain ${params.chainId} not supported`); |
| 100 | + } |
| 101 | + await wallet.switchChain(defineChain(chain.id)); |
| 102 | + return chain; |
| 103 | + }, |
| 104 | + onAccountsChanged: () => { |
| 105 | + // no-op |
| 106 | + }, |
| 107 | + onChainChanged: () => { |
| 108 | + // no-op |
| 109 | + }, |
| 110 | + onDisconnect: () => { |
| 111 | + // no-op |
| 112 | + }, |
| 113 | + })); |
| 114 | +} |
0 commit comments