Basic Usage

Basic Usage

Examples of how to use XO Connect in different scenarios.

Basic Connection

Connect with XO wallet and get user accounts:

connect.ts
import { XOConnectProvider } from 'xo-connect';

// No configuration required for basic operations
const provider = new XOConnectProvider();

// Request connection
const accounts = await provider.request({
  method: 'eth_requestAccounts',
  params: []
});

console.log('Connected:', accounts[0]);

Sign Messages

personal-sign.ts
// Simple message signing
const message = "Hello, XO Connect!";
const accounts = await provider.request({ method: 'eth_accounts' });

const signature = await provider.request({
  method: 'personal_sign',
  params: [message, accounts[0]]
});

console.log('Signature:', signature);

Send Transactions

send-tx.ts
// Send a transaction
const txHash = await provider.request({
  method: 'eth_sendTransaction',
  params: [{
    to: '0x742d35Cc6634C0532925a3b844Bc9e7595f42bE7',
    value: '0x2386F26FC10000', // 0.01 ETH in wei (hex)
    data: '0x'
  }]
});

console.log('Transaction hash:', txHash);

Switch Network

switch-chain.ts
// Switch to Polygon
await provider.request({
  method: 'wallet_switchEthereumChain',
  params: [{ chainId: '0x89' }]
});

// Verify the change
const chainId = await provider.request({
  method: 'eth_chainId'
});

console.log('Current chain:', chainId); // "0x89"

Listen to Events

events.ts
// Listen for network changes
provider.on('chainChanged', (chainId) => {
  console.log('Network changed to:', chainId);
  // Reload page or update state
});

// Listen for account changes
provider.on('accountsChanged', (accounts) => {
  console.log('Accounts updated:', accounts);
  if (accounts.length === 0) {
    console.log('Wallet disconnected');
  }
});

// Listen for connection
provider.on('connect', ({ chainId }) => {
  console.log('Connected on chain:', chainId);
});

ethers.js Integration

ethers-integration.ts
import { ethers } from 'ethers';
import { XOConnectProvider } from 'xo-connect';

// Create XO provider (RPC optional for signing/sending)
const xoProvider = new XOConnectProvider();

// Create ethers provider
const provider = new ethers.providers.Web3Provider(xoProvider);
const signer = provider.getSigner();

// Get address
const address = await signer.getAddress();
console.log('My address:', address);

// Sign message (no RPC needed)
const signature = await signer.signMessage("Hello XOConnect!");
console.log('Signature:', signature);

// Send transaction (no RPC needed)
const tx = await signer.sendTransaction({
  to: '0x742d35Cc6634C0532925a3b844Bc9e7595f42bE7',
  value: ethers.utils.parseEther('0.01')
});
console.log('Tx:', tx.hash);

// NOTE: For getBalance, estimateGas, etc., you need RPC:
// const xoProvider = new XOConnectProvider({
//   rpcs: { "0x1": "https://eth-mainnet.rpc.url" }
// });

Access Client Info

client-info.ts
import { XOConnect } from 'xo-connect';

// Get connected client info
const client = await XOConnect.getClient();

if (client) {
  console.log('Alias:', client.alias);
  console.log('ID:', client._id);
  console.log('Image:', client.image);

  // List available currencies
  client.currencies.forEach(currency => {
    console.log(`- ${currency.id}: ${currency.address}`);
  });

  // Find address on Ethereum mainnet
  const ethCurrency = client.currencies.find(
    c => c.id === "ethereum.mainnet.native.eth"
  );
  console.log('ETH Address:', ethCurrency?.address);
}