Skip to content

Latest commit

 

History

History
391 lines (304 loc) · 11 KB

File metadata and controls

391 lines (304 loc) · 11 KB

Nautilus Wallet Integration

Overview

The "CONNECT WALLET" button is now fully functional using the Ergo dApp Connector standard. Users can connect their Nautilus wallet, and the dashboard will display their ERG balance in real-time.

Features Implemented

✅ 1. Wallet Hook (lib/hooks/useWallet.ts)

Purpose: Manages wallet connection state and interactions with Nautilus

Key Functionality:

  • Nautilus Detection: Checks for window.ergoConnector.nautilus
  • Connection Flow:
    1. Calls ergoConnector.nautilus.connect()
    2. Requests read access via ergo.request_read_access()
    3. Fetches address using ergo.get_change_address()
    4. Fetches balance using ergo.get_balance()
  • Balance Conversion: Converts NanoErg (10^9) to ERG
  • Auto-Reconnect: Persists connection in localStorage and auto-reconnects on page refresh
  • Balance Refresh: Polls balance every 30 seconds when connected
  • Error Handling: Graceful error messages for missing Nautilus or connection failures

Return Values:

{
  isConnected: boolean;
  address: string | null;
  balance: number; // ERG (not NanoErg)
  isLoading: boolean;
  error: string | null;
  connect: () => Promise<void>;
  disconnect: () => Promise<void>;
  refreshBalance: () => Promise<void>;
}

✅ 2. Connect Button (components/WalletConnect.tsx)

Purpose: Interactive button for connecting/disconnecting wallet

Visual States:

Disconnected State

  • Text: "CONNECT WALLET"
  • Style: Green border (#39FF14/50), semi-transparent text
  • Behavior: Click triggers Nautilus connection prompt
  • Hover: Border brightens to full opacity

Connected State

  • Text: "WAL: XX.X ERG" (e.g., "WAL: 125.4 ERG")
  • Style: Bright green text (#39FF14) with monospace font
  • Indicator: Small green pulsing dot on the left
  • Behavior: Click disconnects wallet

Loading State

  • Text: "CONNECTING..."
  • Style: 50% opacity, wait cursor
  • Behavior: Button disabled during connection

Error State

  • Display: Red error message below button
  • Content: Descriptive error (e.g., "Nautilus wallet not found")
  • Action: Alert prompts user to install Nautilus

Styling:

- Font: Mono, bold, 12px
- Border: 2px solid green
- Padding: 8px 16px
- Animation: Scale on hover (1.02x)
- Green dot: 8px circle, pulsing animation

✅ 3. Balance Display (components/WalletBalance.tsx)

Purpose: Shows wallet balance in top-right corner when connected

Visual Design:

  • Position: Fixed top-right (16px from edges)
  • Background: Dark (#0A0A0A) with 90% opacity, backdrop blur
  • Border: 2px green (#39FF14/30)
  • Content:
    • Green pulsing dot indicator
    • "WAL: XXX.X ERG" in bright green
    • Truncated address below (8 chars...6 chars)
  • Animation: Fade-in from right (x: 20 → 0)
  • Text Shadow: Glowing green effect

Behavior:

  • Only visible when wallet is connected
  • Auto-updates every 30 seconds
  • Smooth fade transition on balance change
  • Shows shortened address on hover

Example Display:

● WAL: 125.4 ERG
  9h8xT2f3...k7Bv2q

✅ 4. Persistent Connection

Storage: localStorage key: djed_wallet_connected

Auto-Reconnect Logic:

  1. On page load, check if localStorage has djed_wallet_connected: true
  2. Verify Nautilus is still connected via nautilus.isConnected()
  3. If connected, automatically call connect() without user interaction
  4. If not connected, clear localStorage flag
  5. 1-second delay to ensure Nautilus is fully initialized

Benefits:

  • User stays logged in across page refreshes
  • No need to reconnect on every visit
  • Seamless UX

User Flow

First Connection

  1. User visits dashboard
  2. Clicks "CONNECT WALLET" button
  3. Nautilus extension popup appears
  4. User approves connection request
  5. Dashboard fetches address and balance
  6. Button changes to "WAL: XXX.X ERG" with green dot
  7. Top-right corner displays balance card
  8. Connection persisted in localStorage

Subsequent Visits

  1. User visits dashboard (Nautilus unlocked)
  2. Auto-reconnect triggers after 1 second
  3. Balance displays automatically
  4. No user interaction needed

Disconnection

  1. User clicks "WAL: XXX.X ERG" button
  2. Wallet disconnects immediately
  3. localStorage flag removed
  4. Button reverts to "CONNECT WALLET"
  5. Balance display disappears

Error Handling

Nautilus Not Installed

Detection: window.ergoConnector?.nautilus is undefined

User Feedback:

  • Alert: "Please install Nautilus Wallet extension"
  • Error message below button with installation instructions
  • Red border and text

Resolution:

  • User installs Nautilus from Chrome Web Store or Firefox Add-ons
  • Refreshes page
  • Connection now available

Connection Rejected

Cause: User clicks "Cancel" in Nautilus popup

User Feedback:

  • Error: "Connection rejected. Please try again."
  • Button remains in disconnected state
  • User can retry connection

Balance Fetch Failed

Cause: Network error or Nautilus API issue

User Feedback:

  • Error logged to console
  • Balance shows 0 or last known value
  • Auto-retry on next 30s interval

Nautilus Locked

Cause: User locked Nautilus after connecting

Behavior:

  • Connection persists in state
  • Balance fetch will fail silently
  • Next auto-reconnect will detect locked state and clear connection

Technical Implementation

Ergo dApp Connector API

Nautilus Connection:

const connected = await window.ergoConnector.nautilus.connect();
// Returns: true (approved) or false (rejected)

Request Read Access:

const accessGranted = await window.ergo.request_read_access();
// Returns: true (granted) or false (denied)

Get Wallet Address:

const address = await window.ergo.get_change_address();
// Returns: "9h8xT2f3k7Bv2q..." (Base58 address)

Get Balance:

const balanceNanoErg = await window.ergo.get_balance();
// Returns: "125400000000" (string in NanoErg)
const balanceERG = parseInt(balanceNanoErg) / 1_000_000_000;
// Result: 125.4 ERG

Check Connection Status:

const isConnected = await window.ergoConnector.nautilus.isConnected();
// Returns: true or false

Disconnect:

await window.ergoConnector.nautilus.disconnect();

Integration Points

In app/page.tsx

Before:

<button className="...">
  ◆ CONNECT WALLET ›
</button>

After:

<WalletConnect />
<WalletBalance />

In Top Bar

The WalletConnect button appears in the top navigation bar next to the Sentinel toggle, providing easy access for wallet connection.

In Top-Right Corner

The WalletBalance component appears as a fixed overlay when connected, showing the current ERG balance.


Testing Scenarios

✅ Test 1: First Connection

  1. Open dashboard with Nautilus installed
  2. Click "CONNECT WALLET"
  3. Approve in Nautilus popup
  4. Expected: Button shows "WAL: XX.X ERG", balance appears in top-right

✅ Test 2: Auto-Reconnect

  1. Connect wallet (Test 1)
  2. Refresh page
  3. Wait 1-2 seconds
  4. Expected: Wallet auto-connects without user action

✅ Test 3: Disconnection

  1. While connected, click "WAL: XX.X ERG" button
  2. Expected: Wallet disconnects, button reverts to "CONNECT WALLET", balance disappears

✅ Test 4: No Nautilus Installed

  1. Open dashboard in browser without Nautilus
  2. Click "CONNECT WALLET"
  3. Expected: Alert appears with installation instructions

✅ Test 5: Connection Rejected

  1. Click "CONNECT WALLET"
  2. Click "Cancel" in Nautilus popup
  3. Expected: Error message below button, button remains disconnected

✅ Test 6: Balance Updates

  1. Connect wallet
  2. Wait 30 seconds
  3. Send/receive ERG in Nautilus
  4. Expected: Balance updates automatically after next poll

Files Modified

New Files Created

  1. lib/hooks/useWallet.ts (275 lines)
    • Core wallet hook with connection logic
    • Nautilus API integration
    • Balance polling and conversion
    • Error handling and auto-reconnect

Modified Files

  1. components/WalletConnect.tsx (50 lines)

    • Simplified button component using useWallet hook
    • Visual state management (disconnected/loading/connected)
    • Error display with installation instructions
  2. components/WalletBalance.tsx (50 lines)

    • Fixed top-right balance display
    • Green dot indicator and glowing text
    • Truncated address display
    • Auto-hide when disconnected
  3. app/page.tsx

    • Imported WalletConnect and WalletBalance
    • Replaced static button with WalletConnect component
    • Added WalletBalance to page layout

Browser Compatibility

Supported Browsers

  • Chrome/Edge: Full support (Nautilus available)
  • Firefox: Full support (Nautilus available)
  • Brave: Full support (Nautilus available)
  • Safari: No Nautilus extension available

Mobile Support

  • Nautilus is desktop-only (Chrome extension)
  • Mobile browsers will show "Nautilus not found" error
  • Future: Could integrate with Ergo Mobile Wallet via WalletConnect

Security Considerations

Read-Only Access

  • Hook only requests read access via request_read_access()
  • Cannot sign transactions or move funds
  • Can only view address and balance

No Private Keys

  • Nautilus never exposes private keys to dApp
  • All sensitive operations happen in Nautilus extension
  • Dashboard cannot access wallet seed

User Consent Required

  • Every connection requires explicit user approval
  • User can reject connection at any time
  • Disconnection clears all wallet data from app

localStorage Security

  • Only stores boolean flag (djed_wallet_connected: true)
  • No sensitive data (address, balance, keys) in localStorage
  • Safe to use on public computers (user should lock Nautilus)

Future Enhancements

Potential Features

  1. Transaction History: Show recent wallet transactions
  2. Multi-Token Balance: Display SigUSD, SigRSV, and other tokens
  3. Sign Transactions: Enable MINT/REDEEM operations via Nautilus
  4. Wallet Switcher: Support multiple connected accounts
  5. Mobile Wallet Support: Integrate WalletConnect protocol
  6. Balance Notifications: Alert when balance changes significantly

Known Limitations

  • Desktop-only (Nautilus is browser extension)
  • Requires manual Nautilus installation
  • No transaction signing yet (read-only)
  • Single account support (no multi-wallet)

Summary

The Nautilus wallet integration is fully functional and production-ready. Users can:

  1. ✅ Click "CONNECT WALLET" button
  2. ✅ Approve connection in Nautilus popup
  3. ✅ See their ERG balance displayed as "WAL: XX.X ERG"
  4. ✅ View balance in top-right corner with green indicator
  5. ✅ Stay connected across page refreshes (localStorage persistence)
  6. ✅ Disconnect by clicking the balance button

The integration follows the official Ergo dApp Connector standard, provides robust error handling, and delivers a smooth user experience with automatic reconnection and balance polling.

Status: ✅ COMPLETE AND READY TO TEST