👛Wallet Connectors

Complete guide for integrating wallet connections in your Somnia applications.

Overview

The Somnia Agent Kit provides wallet connectors for seamless integration with popular Web3 wallets:

  • 🦊 MetaMask: Browser extension wallet

  • 🔗 WalletConnect: Multi-wallet support

  • 🔐 Private Key: Direct wallet access

  • 📱 Mobile Wallets: Support for mobile apps

Installation

Wallet connectors are included in the main package:

npm install somnia-agent-kit

MetaMask Connector

Initialize MetaMask

import { SomniaAgentKit, SOMNIA_NETWORKS } from 'somnia-agent-kit';

// Initialize SDK
const kit = new SomniaAgentKit({
  network: SOMNIA_NETWORKS.testnet,
  contracts: {
    agentRegistry: process.env.AGENT_REGISTRY_ADDRESS!,
    agentManager: process.env.AGENT_MANAGER_ADDRESS!,
    agentExecutor: process.env.AGENT_EXECUTOR_ADDRESS!,
    agentVault: process.env.AGENT_VAULT_ADDRESS!,
  },
});

await kit.initialize();

// Get MetaMask connector (recommended)
const metamask = kit.getMetaMaskConnector();

// Check if MetaMask is installed
if (!await metamask.isAvailable()) {
  console.log('❌ MetaMask not installed');
  console.log('Install from: https://metamask.io');
  return;
}

console.log('✅ MetaMask detected!');

Connect Wallet

Get Current Account

Get Balance

Switch Network

Add Network

Sign Message

Send Transaction

Listen to Events

Complete Example: MetaMask Integration

React Integration

MetaMask Hook

React Component

Private Key Wallet

Initialize with Private Key

Create New Wallet

Import from Mnemonic

Best Practices

1. Check Wallet Availability

2. Handle Connection Errors

3. Listen for Disconnection

4. Validate Network

5. Never Expose Private Keys

6. Handle Network Changes

7. Show User Feedback

Security Considerations

Private Key Storage

Transaction Validation

User Confirmation

See Also

Last updated