|
| 1 | +import { toPrivyWalletConnector } from "@privy-io/cross-app-connect/rainbow-kit"; |
| 2 | +import type { CreateConnectorFn } from "@wagmi/core"; |
| 3 | +import { createEmitter } from "@wagmi/core/internal"; |
| 4 | +import { fromProvider } from "../../adapters/eip1193/from-eip1193.js"; |
| 5 | +import { apechain } from "../../chains/chain-definitions/apechain.js"; |
| 6 | +import { GLYPH_APP_ID, glyphConnectorDetails, STAGING_GLYPH_APP_ID } from "../../wallets/glyph/constants.js"; |
| 7 | +import type { Wallet } from "../../wallets/interfaces/wallet.js"; |
| 8 | +import type { Chain, EIP1193Provider } from "viem"; |
| 9 | +import { abstract, apeChain, arbitrum, arbitrumSepolia, base, baseSepolia, curtis, mainnet, optimism, optimismSepolia, polygon, sepolia } from "viem/chains"; |
| 10 | + |
| 11 | +/** |
| 12 | + * Create a wagmi connector for the Glyph Global Wallet. |
| 13 | + * |
| 14 | + * Adapted from wagmi injected connector as a reference implementation: |
| 15 | + * https://github.com/wevm/wagmi/blob/main/packages/core/src/connectors/injected.ts#L94 |
| 16 | + * |
| 17 | + * @example |
| 18 | + * import { createConfig, http } from "wagmi"; |
| 19 | + * import { apeChain } from "wagmi/chains"; |
| 20 | + * import { glyphWalletConnector } from "@use-glyph/sdk-react" |
| 21 | + * |
| 22 | + * export const wagmiConfig = createConfig({ |
| 23 | + * chains: [apeChain], |
| 24 | + * transports: { |
| 25 | + * [apeChain.id]: http(), |
| 26 | + * }, |
| 27 | + * connectors: [glyphWalletConnector()], |
| 28 | + * ssr: true, |
| 29 | + * }); |
| 30 | + */ |
| 31 | +function glyphWalletConnector(options?: { |
| 32 | + useStagingTenant?: boolean |
| 33 | +}): CreateConnectorFn { |
| 34 | + const { useStagingTenant } = options ?? {}; |
| 35 | + |
| 36 | + return (params) => { |
| 37 | + const connector = toPrivyWalletConnector({ |
| 38 | + iconUrl: glyphConnectorDetails.iconUrl, |
| 39 | + id: useStagingTenant ? STAGING_GLYPH_APP_ID : GLYPH_APP_ID, |
| 40 | + name: glyphConnectorDetails.name |
| 41 | + })(params); |
| 42 | + |
| 43 | + const getGlyphProvider = async (parameters?: { chainId?: number | undefined } | undefined) => { |
| 44 | + const chainId = parameters?.chainId ?? params.chains?.[0]?.id ?? apechain.id; |
| 45 | + |
| 46 | + const provider = await connector.getProvider({ |
| 47 | + chainId |
| 48 | + }); |
| 49 | + |
| 50 | + return provider; |
| 51 | + }; |
| 52 | + |
| 53 | + const glyphConnector = { |
| 54 | + ...connector, |
| 55 | + getProvider: getGlyphProvider, |
| 56 | + type: glyphConnectorDetails.type, |
| 57 | + id: glyphConnectorDetails.id |
| 58 | + }; |
| 59 | + return glyphConnector; |
| 60 | + }; |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Create a thirdweb wallet for Glyph Global Wallet |
| 65 | + * |
| 66 | + * @returns A wallet instance wrapping Glyph Global Wallet to be used with the thirdweb Connect SDK |
| 67 | + * |
| 68 | + * @example |
| 69 | + * ```tsx |
| 70 | + * import { createThirdwebClient } from "thirdweb"; |
| 71 | + * import { glyphWalletTW } from "@use-glyph/sdk-react" |
| 72 | + * |
| 73 | + * const client = createThirdwebClient({ clientId }); |
| 74 | + * |
| 75 | + * <ConnectButton client={client} wallets=[glyphWalletTW()]> |
| 76 | + * ``` |
| 77 | + */ |
| 78 | +const glyphWalletTW = (chains?: [Chain, ...Chain[]]): Wallet => { |
| 79 | + const connector = glyphWalletConnector()({ |
| 80 | + chains: chains ?? [apeChain, curtis, mainnet, base, arbitrum, polygon, optimism, abstract, sepolia, baseSepolia, optimismSepolia, arbitrumSepolia], |
| 81 | + emitter: createEmitter("io.useglyph") |
| 82 | + }); |
| 83 | + return fromProvider({ |
| 84 | + provider: connector.getProvider as ( |
| 85 | + parameters?: { chainId?: number | undefined } | undefined |
| 86 | + ) => Promise<EIP1193Provider>, |
| 87 | + walletId: "io.useglyph" |
| 88 | + }); |
| 89 | +}; |
| 90 | + |
| 91 | +export { glyphWalletTW }; |
0 commit comments