← All work← Casebook 1 · Web3 engineering

PRACTICE

WalletConnect: Pairing, Sessions, and Callback Lifecycles

Scanning a QR code to connect a wallet fails in more ways than you'd expect: closing the popup doesn't cancel an in-flight request, and a refresh can break the connection. Sorting out the lifecycles of pairing, sessions, and callbacks.

Why closing a dialog does not end a request

Pages, sessions, and requests have different lifetimes. A page presents an interaction, a session represents a connection, and a request owns one operation’s outcome. Correlating them explicitly prevents late results from being delivered to the wrong caller.

Scanning, web navigation, connection approval, native signing pages, and asynchronous broadcasting have different lifecycles. Closing a dialog does not always end a request, and a page reload should not leave Provider registration disconnected from the page. Duplicate pairing or late callbacks can cause repeated completion, incorrect cancellation, or delivery of a result to the wrong request.

Implementation and tradeoffs

Distinguish connection sources and requests at entry

Route dedicated connection URIs into pairing, checking invalid and duplicate inputs rather than mixing them into generic deep-link dispatch. Protect approval and dismissal callbacks so the operation completes only once. Coordinate SDK fixes with application-layer presentation.

Keep Provider registration aligned with the page and chain

Extract a shared injection flow and restore the Provider and connection compatibility capabilities after script cleanup. Handle reloads, chain changes, and page origin within the same handoff, rather than addressing only the connection state at initial load.

Correlate callbacks with their sessions and requests

Associate responses using session and request identifiers. Accept late broadcast results after page dismissal within a bounded process, and treat a timeout explicitly as an unknown outcome. Handle cancellation, original Provider errors, and unparseable JSON-RPC responses separately. A malformed response is not a valid signing completion.

Testing the mechanism

Interleave pairing, approval, page dismissal, and broadcast completion. Check that each request ends once, Provider registration survives reloads and chain changes, and late results cannot reach another caller.

ENGINEERING SCENARIO

Key engineering scenarios

Illustrative scenarios · not real product interfaces

The same connection entry point fires twice

Complete each pairing attempt only once

Scanning and navigation may deliver the same URI repeatedly. Approval and dialog dismissal may also complete in different orders.

Handling principleIdentify duplicate pairing attempts and protect the completion callback against repeated execution.

The page reloads or the chain changes

Restore Provider registration

After page scripts are cleared or changed, the corresponding Provider and connection capabilities need to be registered again. A connected indicator alone is insufficient.

Handling principleRegister again in the correct page and verify the connection context.

A result arrives after the signing page closes

Separate page lifecycle from request outcome

Broadcasting may still be in progress when the page closes. Closing the UI does not establish the final request state, and the response still belongs to its original session.

Handling principleAccept late results within a bounded lifecycle and verify session and request identifiers.

  1. Identify the connection entry point and pairing request
  2. Associate page and session context
  3. Open signing and manage callbacks
  4. Return a final or unknown outcome to the originating request

Expand a scenario to explore its state and how it is handled.

← All work