x0-react-sdk
Version:
React SDK for X0Pay Hyperlane token bridging with MetaMask and Safe wallet integration
67 lines (55 loc) • 1.9 kB
text/typescript
import { ethers } from 'ethers';
import type {
WalletType,
WalletConnectionResult,
WalletConnectionOptions,
WalletConnectConfig
} from '../types/wallet';
import { connectMetaMaskWeb } from './metamask';
import { connectWalletConnect } from './walletconnect';
import { getRecommendedWalletType } from '../utils/platform';
export async function connectWallet(
options: WalletConnectionOptions = {}
): Promise<WalletConnectionResult> {
const {
walletType,
walletConnectConfig,
autoConnect = false
} = options;
// Determine which wallet type to use
const targetWalletType = walletType || getRecommendedWalletType();
try {
switch (targetWalletType) {
case 'metamask':
return await connectMetaMaskWeb();
case 'walletconnect':
if (!walletConnectConfig) {
throw new Error('WalletConnect configuration is required. Please provide projectId and chains.');
}
return await connectWalletConnect(walletConnectConfig);
default:
throw new Error(`Unsupported wallet type: ${targetWalletType}`);
}
} catch (error) {
if (error instanceof Error) {
throw new Error(`Wallet connection failed: ${error.message}`);
}
throw new Error('Wallet connection failed');
}
}
// Helper function to get available wallet types
export function getAvailableWalletTypes(): WalletType[] {
const available: WalletType[] = [];
// Check MetaMask availability
if (typeof window !== 'undefined' && window.ethereum?.isMetaMask) {
available.push('metamask');
}
// WalletConnect is always available in browser
if (typeof window !== 'undefined') {
available.push('walletconnect');
}
return available;
}
// Export individual connectors for advanced usage
export { connectMetaMaskWeb } from './metamask';
export { connectWalletConnect } from './walletconnect';