UNPKG

x0-react-sdk

Version:

React SDK for X0Pay Hyperlane token bridging with MetaMask and Safe wallet integration

60 lines (53 loc) 2.03 kB
import { ethers } from 'ethers'; import WalletConnectProvider from '@walletconnect/ethereum-provider'; import type { WalletConnectionResult, WalletConnectConfig } from '../types/wallet'; export async function connectWalletConnect( config: WalletConnectConfig ): Promise<WalletConnectionResult> { if (typeof window === 'undefined') { throw new Error('WalletConnect is only available in browser environment'); } try { // Initialize WalletConnect provider with minimal required config const provider = await WalletConnectProvider.init({ projectId: config.projectId, chains: config.chains, optionalChains: [1, 5, 137, 42161], // Use the same chains as optional showQrModal: config.showQrModal ?? true, metadata: config.metadata || { name: 'X0 SDK', description: 'X0Pay Token bridging SDK', url: 'https://x0.com', icons: ['https://x0.com/icon.png'] } }); // Enable the provider (this will show QR code or deep link) await provider.enable(); // Create ethers provider and signer const ethersProvider = new ethers.providers.Web3Provider(provider); const signer = ethersProvider.getSigner(); const address = await signer.getAddress(); const network = await ethersProvider.getNetwork(); const chainId = network.chainId.toString(); return { provider: ethersProvider, signer, address, chainId, walletType: 'walletconnect' }; } catch (error) { if (error instanceof Error) { throw new Error(`WalletConnect connection failed: ${error.message}`); } throw new Error('WalletConnect connection failed'); } } export function isWalletConnectAvailable(): boolean { return typeof window !== 'undefined'; } // Helper function to disconnect WalletConnect session export async function disconnectWalletConnect(): Promise<void> { // This would need to be called on the provider instance // Implementation depends on how you store the provider reference }