|
| 1 | +# Wallet Integration with Mesh.js |
| 2 | + |
| 3 | +This document describes the wallet integration implemented in the Cardano Dashboard using Mesh.js. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The wallet integration allows users to connect their Cardano wallets (like Eternl, Nami, Flint, etc.) to the dashboard using the CIP-30 standard. Users can view their wallet information, balance, and network details. |
| 8 | + |
| 9 | +## Features |
| 10 | + |
| 11 | +- **Automatic Wallet Detection**: Automatically detects available wallets on the user's device |
| 12 | +- **Wallet Connection**: Connect to any CIP-30 compatible wallet |
| 13 | +- **Persistent Connections**: Wallet connections are remembered across browser sessions |
| 14 | +- **Wallet Information Display**: Shows wallet name, icon, balance, and network |
| 15 | +- **Error Handling**: Comprehensive error handling with user-friendly messages |
| 16 | +- **Responsive Design**: Works on both desktop and mobile devices |
| 17 | + |
| 18 | +## Technical Implementation |
| 19 | + |
| 20 | +### Components |
| 21 | + |
| 22 | +1. **WalletContext** (`src/contexts/WalletContext.tsx`) |
| 23 | + - Manages wallet state and connections |
| 24 | + - Handles wallet discovery and connection logic |
| 25 | + - Provides wallet information to the rest of the application |
| 26 | + |
| 27 | +2. **WalletConnect** (`src/components/WalletConnect/WalletConnect.tsx`) |
| 28 | + - UI component for wallet connection |
| 29 | + - Dropdown menu for wallet selection |
| 30 | + - Displays connected wallet information |
| 31 | + |
| 32 | +### Key Features |
| 33 | + |
| 34 | +- **Wallet Discovery**: Uses `BrowserWallet.getAvailableWallets()` to find available wallets |
| 35 | +- **Connection Management**: Uses `BrowserWallet.enable(walletName)` to connect |
| 36 | +- **Balance Retrieval**: Fetches ADA balance using `wallet.getLovelace()` |
| 37 | +- **Network Detection**: Gets network ID (0 for testnet, 1 for mainnet) |
| 38 | +- **Address Management**: Retrieves wallet addresses for display |
| 39 | + |
| 40 | +### State Management |
| 41 | + |
| 42 | +The wallet context manages: |
| 43 | +- Available wallets list |
| 44 | +- Currently connected wallet |
| 45 | +- Connection status (connecting, disconnecting) |
| 46 | +- Error states |
| 47 | +- Loading states |
| 48 | + |
| 49 | +### Persistence |
| 50 | + |
| 51 | +Wallet connections are stored in localStorage with a 1-hour expiration to ensure security while maintaining user convenience. |
| 52 | + |
| 53 | +## Usage |
| 54 | + |
| 55 | +### For Users |
| 56 | + |
| 57 | +1. Click the "Connect Wallet" button in the header |
| 58 | +2. Select your preferred wallet from the dropdown |
| 59 | +3. Approve the connection in your wallet |
| 60 | +4. View your wallet information and balance |
| 61 | +5. Use the disconnect button to disconnect when done |
| 62 | + |
| 63 | +### For Developers |
| 64 | + |
| 65 | +The wallet context provides several hooks and functions: |
| 66 | + |
| 67 | +```typescript |
| 68 | +import { useWallet } from '@/contexts/WalletContext'; |
| 69 | + |
| 70 | +function MyComponent() { |
| 71 | + const { |
| 72 | + availableWallets, |
| 73 | + connectedWallet, |
| 74 | + isConnecting, |
| 75 | + connectWallet, |
| 76 | + disconnectWallet, |
| 77 | + error |
| 78 | + } = useWallet(); |
| 79 | + |
| 80 | + // Use wallet data and functions |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +## Supported Wallets |
| 85 | + |
| 86 | +Any wallet that implements the CIP-30 standard is supported, including: |
| 87 | +- Eternl |
| 88 | +- Nami |
| 89 | +- Flint |
| 90 | +- Yoroi |
| 91 | +- Daedalus |
| 92 | +- And many others |
| 93 | + |
| 94 | +## Security Considerations |
| 95 | + |
| 96 | +- Wallet connections expire after 1 hour |
| 97 | +- No private keys are ever stored or transmitted |
| 98 | +- All wallet interactions use the secure CIP-30 protocol |
| 99 | +- Users must explicitly approve connections in their wallet |
| 100 | + |
| 101 | +## Error Handling |
| 102 | + |
| 103 | +The system handles various error scenarios: |
| 104 | +- No wallets available |
| 105 | +- Connection failures |
| 106 | +- Network issues |
| 107 | +- Wallet permission denials |
| 108 | + |
| 109 | +Errors are displayed as user-friendly toast messages and automatically clear after 5 seconds. |
| 110 | + |
| 111 | +## Future Enhancements |
| 112 | + |
| 113 | +Potential future improvements: |
| 114 | +- Transaction signing capabilities |
| 115 | +- Asset management |
| 116 | +- Staking information |
| 117 | +- DRep functionality |
| 118 | +- Multi-wallet support |
| 119 | +- Wallet switching without reconnection |
| 120 | + |
| 121 | +## Dependencies |
| 122 | + |
| 123 | +- `@meshsdk/core`: Core wallet functionality |
| 124 | +- `@meshsdk/react`: React integration utilities |
| 125 | +- React Context API for state management |
| 126 | +- CSS Modules for styling |
| 127 | + |
| 128 | +## Browser Compatibility |
| 129 | + |
| 130 | +Requires a modern browser with: |
| 131 | +- ES6+ support |
| 132 | +- LocalStorage support |
| 133 | +- Modern CSS features |
| 134 | +- Web Crypto API support (for wallet operations) |
0 commit comments