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.
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"]
}
callback_url is not reachable: the wallet sends the POST but the app never receives it. The app must poll for the callback using GET /api/wallet/callback?token=ct-7a8b9c.challenge is not verified: the app expects the wallet to include the challenge in the POST, but it’s missing. The app logs ChallengeMismatchError with expected: "a1b2c3d4e5f6", actual: "b2c3d4e5f6a1".networks field is malformed: an array of strings, but some entries are missing or have typos like "ethereume-mainnet". The app must validate using a known list of network identifiers.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
}
}
chain_id is not validated: the wallet signs on chain_id: 1, but the app expects chain_id: 5 (Goerli). The transaction is valid but not processed.signature is not in r,s,v format: the app expects a single 65-byte signature, but the wallet sends a 64-byte r,s array. The app logs InvalidSignatureFormatError.nonce is reused: two transactions with the same nonce are submitted. The app must detect and resolve conflicts using POST /wallet/transaction/conflict.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.
network field is missing from the response: the app defaults to ethereum-mainnet, but a transaction appears on polygon-mainnet. The app must dynamically add new networks to the user’s state.balance is a string in wei: the app expects a DECIMAL, but the value is "1234500000000000000000" and is not converted to 1234.5 ETH. The app logs BalanceFormatError.transactions array is missing a tx_hash: the app cannot link the transaction to a tx_hash, and the tx_hash field is null in the wallet_states table. The app must periodically clean up orphaned transactions.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.
reason field is not standardized: the app expects "user_action", "timeout", or "network_error", but the wallet sends "useraction" or "neterr". The app must normalize the reason using a lookup table.timestamp is in ISO 8601 but not Zulu: the app parses "2025-04-05T12:05:00" as local time, but the wallet sends "2025-04-05T12:05:00+02:00". The app logs TimestampTimezoneMismatchError.disconnect request is not idempotent: the app calls it twice, but the wallet processes both. The app must track request_id and deduplicate using POST /api/wallet/disconnect?request_id=12345.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.
challenge string is not properly encoded: the app expects UTF-8, but the wallet sends a challenge in ISO-8859-1. The app logs ChallengeEncodingMismatchError.signature is not deterministic: two signatures for the same challenge differ in r and s values. The app logs NonDeterministicSignatureError.callback_url is not called: the wallet signs but never calls callback_url. The app must retry the callback every 5 minutes for 15 minutes.POST /wallet/init, QR code, callback_url, connection_token, public_key.POST /wallet/sign, transaction_id, request_url, signature, tx_hash, receipt.GET /wallet/state, last_synced_at, network, balance, transactions.POST /wallet/disconnect, reason, timestamp, request_id.POST /wallet/verify, challenge, callback_url, signature, verified.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.
We use cookies for analytics (Google Analytics) and advertising (Google AdSense) to improve your experience and support free content. Privacy Policy