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