x0-react-sdk
Version:
React SDK for X0Pay Hyperlane token bridging with MetaMask and Safe wallet integration
52 lines • 2.05 kB
JavaScript
import { ethers } from 'ethers';
import WalletConnectProvider from '@walletconnect/ethereum-provider';
export async function connectWalletConnect(config) {
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],
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() {
return typeof window !== 'undefined';
}
// Helper function to disconnect WalletConnect session
export async function disconnectWalletConnect() {
// This would need to be called on the provider instance
// Implementation depends on how you store the provider reference
}
//# sourceMappingURL=walletconnect.js.map