Wallet Integration Patterns

Production engineering guide for wallet integration patterns in Web3 and blockchain systems.

Wallet integration is the process of connecting a web or mobile application to a user’s cryptocurrency wallet, enabling actions like signing transactions, verifying identities, and managing assets. It matters when users must interact with blockchain applications—whether onboarding, making payments, participating in DAOs, or using NFTs—and the experience hinges on seamless wallet connection and transaction handling.

Connecting a Wallet to a Web Application

When a user visits a web app and clicks “Connect Wallet,” the application must guide the user through a series of steps: selecting a wallet provider, authenticating with the wallet, and establishing a session. The core pattern involves an initialization phase where the app requests a wallet connection via a cryptographic challenge.

The process begins with the app generating a challenge string and a callback_url and sending them to the wallet via a standard protocol. The most common approach is a wallet-init endpoint that returns a connection_token and a qr_code URL.

POST /api/wallet/init HTTP/1.1
Host: app.example.com
Content-Type: application/json

{
  "callback_url": "https://app.example.com/wallet/callback",
  "challenge": "a1b2c3d4e5f6",
  "networks": ["ethereum-mainnet", "polygon-mainnet"],
  "scopes": ["sign", "send", "verify"]
}

The response includes a connection_token and a qr_code payload:

{
  "connection_token": "ct-7a8b9c",
  "qr_code": "https://app.example.com/qr/ct-7a8b9c",
  "expires_at": "2025-04-05T12:00:00Z"
}

The app displays the QR code and waits for the user to scan it with their wallet. Once scanned, the wallet sends a POST to the callback_url with the connection_token and the user’s public key:

POST /wallet/callback HTTP/1.1
Host: app.example.com
Content-Type: application/json

{
  "connection_token": "ct-7a8b9c",
  "public_key": "0x742d35Cc6634C0532925a3b8D4C05C6d4351F7B8",
  "wallet_type": "metamask",
  "version": "12.1.0"
}

The app then registers the user and returns a session_token:

{
  "session_token": "st-9f8e7d6c",
  "user_id": 12345,
  "public_key": "0x742d35Cc6634C0532925a3b8D4C05C6d4351F7B8",
  "networks": ["ethereum-mainnet", "polygon-mainnet"]
}

Common Failure Modes

Signing a Transaction

After connection, the app needs to ask the user to sign a transaction. The pattern involves three phases: transaction preparation, submission, and verification.

The app prepares a transaction object with the required fields: to, value, data, chain_id, and gas_limit.

{
  "to": "0x742d35Cc6634C0532925a3b8D4C05C6d4351F7B8",
  "value": "1000000000000000000",
  "data": "0xa9059cbb000000000000000000000000742d35cc6634c0532925a3b8d4c05c6d4351f7b8",
  "chain_id": 1,
  "gas_limit": 21000,
  "nonce": 12
}

The app sends this to the /api/wallet/sign endpoint, which returns a transaction_id and a status:

POST /api/wallet/sign HTTP/1.1
Host: app.example.com
Content-Type: application/json
Authorization: Bearer st-9f8e7d6c

{
  "to": "0x742d35Cc6634C0532925a3b8D4C05C6d4351F7B8",
  "value": "1000000000000000000",
  "data": "0xa9059cbb000000000000000000000000742d35cc6634c0532925a3b8d4c05c6d4351f7b8",
  "chain_id": 1,
  "gas_limit": 21000,
  "nonce": 12
}
{
  "transaction_id": "tx-1a2b3c",
  "status": "pending",
  "signature": null,
  "request_url": "https://app.example.com/wallet/tx/tx-1a2b3c"
}

The app redirects the user to request_url where the wallet displays the transaction details. The user confirms the transaction, and the wallet calls a POST /wallet/transaction/confirm endpoint with the transaction_id and signature.

POST /wallet/transaction/confirm HTTP/1.1
Host: app.example.com
Content-Type: application/json

{
  "transaction_id": "tx-1a2b3c",
  "signature": "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"
}

The app updates the transaction status and returns a tx_hash and receipt:

{
  "status": "confirmed",
  "tx_hash": "0x1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b1c2d3e4f5a6b7c8d9e0f1a2b3",
  "receipt": {
    "block_hash": "0x3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b1c2d3e4f5a6b7c8d9e0f1a2b3c4d",
    "block_number": 12345678,
    "gas_used": 21000,
    "cumulative_gas_used": 21000
  }
}

Key Pitfalls

Wallet State Management

A connected wallet must be kept in sync with the blockchain. The app must handle changes like balance updates, new transactions, and contract events.

The app polls the wallet for state updates every 30 seconds using GET /api/wallet/state?session_token=st-9f8e7d6c.

{
  "public_key": "0x742d35Cc6634C0532925a3b8D4C05C6d4351F7B8",
  "networks": {
    "ethereum-mainnet": {
      "balance": "1234500000000000000000",
      "last_synced_at": "2025-04-05T12:00:00Z",
      "transactions": [
        {
          "tx_hash": "0x1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b1c2d3e4f5a6b7c8d9e0f1a2b3",
          "from": "0x742d35Cc6634C0532925a3b8D4C05C6d4351F7B8",
          "to": "0x1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b1c2d3e4f5a6b7c8d9e0f1a2b3",
          "value": "500000000000000000",
          "status": "confirmed"
        }
      ]
    }
  }
}

The app stores the state in a wallet_states table:

CREATE TABLE wallet_states (
  session_token VARCHAR(64) PRIMARY KEY,
  public_key VARCHAR(42) NOT NULL,
  network VARCHAR(32) NOT NULL,
  balance DECIMAL(38,0) NOT NULL,
  last_synced_at TIMESTAMPTZ NOT NULL,
  updated_at TIMESTAMPTZ DEFAULT NOW(),
  FOREIGN KEY (session_token) REFERENCES users(session_token)
);

The app uses last_synced_at to fetch only new data since that time.

Critical Edge Cases

Handling Wallet Disconnection and Reconnection

When a user disconnects a wallet, the app must invalidate the session, update the state, and notify the user.

The app calls POST /api/wallet/disconnect with the session_token:

POST /api/wallet/disconnect HTTP/1.1
Host: app.example.com
Content-Type: application/json
Authorization: Bearer st-9f8e7d6c

{
  "reason": "user_action",
  "timestamp": "2025-04-05T12:05:00Z"
}

The wallet responds with a success status and updates the wallet_states table:

{
  "success": true,
  "disconnected_at": "2025-04-05T12:05:00Z"
}

The app then invalidates the session token by setting expires_at in the sessions table and sends a WebSocket event to all connected clients.

Silent Failures

Verifying User Identity

To prove a user owns a wallet, the app issues a challenge that the user signs and submits.

The app generates a challenge string and sends it via POST /api/wallet/verify:

POST /api/wallet/verify HTTP/1.1
Host: app.example.com
Content-Type: application/json
Authorization: Bearer st-9f8e7d6c

{
  "challenge": "verify-1234567890abcdef",
  "callback_url": "https://app.example.com/verify/callback"
}

The wallet displays the challenge and waits for the user to sign it. The app polls GET /api/wallet/verify/status?challenge=verify-1234567890abcdef until the user confirms.

When confirmed, the wallet calls callback_url with the challenge, signature, and public_key:

POST /verify/callback HTTP/1.1
Host: app.example.com
Content-Type: application/json

{
  "challenge": "verify-1234567890abcdef",
  "signature": "0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
  "public_key": "0x742d35Cc6634C0532925a3b8D4C05C6d4351F7B8"
}

The app verifies the signature using the public_key and challenge and returns a verified status.

Subtle Failures

Summary of Core Patterns

Each pattern relies on consistent use of session_token, standardized chain_id values, and robust error handling via status, error, and timestamp fields. The success of wallet integration depends not on perfect execution, but on handling the silent failures that arise when components are out of sync.

This page was rewritten on 18 October 2026. It replaced a templated version that was not specific to its own title. How these pages are written and checked. If anything here is wrong, tell us at [email protected] and we will correct it.