Wallet provider and controller lifecycle
The wallet provider and controller form the application boundary for wallet use in React. They expose one connection, the discovered wallet set, the selected and connected wallet, authorized signing features, and transaction operations as a consistent state view. The page extends the broader Wallet adapter integration model and complements the wallet UI components. It relies on the package's wallet plugin dependency for wallet integration.
Provider boundary
The provider boundary turns wallet-client state into values that React components can read. The connection provider owns the RPC connection, while the wallet provider owns discovery, selection, authorization, and wallet actions.
Connection context
The connection context gives one @solana/web3.js Connection to a component subtree. It creates the connection from an RPC endpoint and optional configuration, so transaction-facing components use the same RPC settings until either input changes. useConnection() fails fast when the context is absent.
References: packages/…/ConnectionProvider.tsx• L7-43
Wallet context
The wallet context gives components an immutable view of wallet state and a stable set of actions. The view includes discovered wallets, selection, connection status, account identity, signer access, supported transaction versions, and only the signing features that the current wallet authorizes.
The provider creates the browser wallet client after React commits. Before that point, including on the server, it exposes a safe pending view. Its action functions keep the same identity and forward to the current controller, so child effects can request selection or connection during the first client render. A configuration change disposes the old controller and starts a new wallet lifecycle.
References: packages/…/WalletProvider.tsx• L19-29 packages/…/WalletProvider.tsx• L31-78 packages/…/WalletProvider.tsx• L83-130
Wallet lifecycle
The controller is the state bridge between Wallet Standard wallets and the context shape used by applications. It listens to wallet-client changes, maps wallet identities into adapter-compatible views, and publishes a new snapshot when selection or wallet state changes.
Discovery and selection
Wallet discovery starts when the provider creates the controller in the browser. The controller filters the discovered set through provider options and presents each wallet once by name. Selection is separate from connection: an application can select a wallet before asking it to connect, and the selected wallet is the target for connection and sign-in requests.
Selection has three useful states:
undefinedfollows the active or restored wallet when no explicit choice exists.- A wallet name requests that wallet and fails if it is not available yet.
nullexplicitly clears the selection.
This separation lets a picker show the available wallet before authorization. It also prevents a stale choice from silently becoming a different wallet.
References: packages/…/wallet-controller.ts• L168-237 packages/…/wallet-controller.ts• L516-573 packages/…/types.ts• L64-74
Connection and restoration
Connection asks the wallet client to authorize the selected wallet and then exposes its active account and signer through the next snapshot. When no explicit wallet is selected, the controller waits for an automatic or restored connection before it prompts again. Disconnect clears the explicit selection and asks the wallet client to end the active connection.
The provider passes autoConnect, storage, and storageKey into the wallet client. Storage therefore supports the wallet client's restoration path rather than becoming part of the React view. The browser-only setup keeps server rendering free of wallet discovery and lets hydration activate discovery without replacing server-rendered content.
The lifecycle is coordinated across the provider, controller, wallet client, and context snapshot as shown below.
flowchart TD
A[React WalletProvider] --> B[Create wallet controller after commit]
B --> C[Discover and filter wallets]
C --> D[Select wallet]
D --> E{Restore active connection?}
E -->|yes| F[Expose active account and signer]
E -->|no| G[Connect selected wallet]
G --> F
F --> H[Publish immutable wallet snapshot]
H --> I[Enable authorized wallet actions]
I --> J[Disconnect or change provider configuration]
J --> K[Dispose old controller]
References: packages/…/WalletProvider.tsx• L83-117 packages/…/wallet-controller.ts• L238-267 packages/…/wallet-controller.ts• L575-592 packages/…/provider-test.tsx• L113-165
Authorization capabilities
The active account determines which wallet actions are available. The snapshot exposes transaction signing only when the connected wallet has a transaction-modifying signer. It exposes message signing only when the connected account advertises that feature. Sign-in uses the selected wallet and can be available before connection, while offchain sign-in requires the selected wallet to advertise Sign In With Solana version 1.1.0.
The controller converts wallet failures into package errors and sends them to the optional error callback. A superseded connect or sign-in request remains distinguishable from a real wallet failure, so a newer request can replace an older one without reporting a false error.
References: packages/…/wallet-controller.ts• L133-141 packages/…/wallet-controller.ts• L269-363 packages/…/wallet-controller.ts• L516-569 packages/…/provider-test.tsx• L185-288
Persistent wallet state
Wallet state has two persistence boundaries. The wallet provider passes storage settings to the wallet client for connection restoration, while useLocalStorage gives unrelated React state a small JSON persistence helper. Neither boundary should be treated as the source of truth for the live wallet snapshot; the controller remains the source of truth after it is mounted.
Wallet client storage
The wallet provider accepts a storage object and an optional storage key as part of controller configuration. The wallet client uses these settings to read a prior wallet connection in the browser. Initial storage reads do not occur during server rendering, and storage failure must not prevent the provider from exposing its pending or live state.
References: packages/…/WalletProvider.tsx• L84-117 packages/…/provider-test.tsx• L113-165
React state storage
useLocalStorage reads a JSON value for a key and writes later state changes back to that key. It avoids replacing stored data with a fallback during mount, removes the key when the value is null, and keeps in-memory state usable when browser storage is missing or full. When the key changes, it adopts the new key's value and prevents an old setter from writing to the new key.
References: packages/…/useLocalStorage.ts• L10-65 packages/…/storage-test.tsx• L6-83
Transaction-facing wallet state
The controller translates a connected Wallet Standard account into the transaction operations expected by @solana/web3.js applications. It checks the account's advertised transaction versions and signing features before it asks the wallet to sign or submit data.
Transaction submission
sendTransaction requires an active connected wallet and a signer. It uses the connection supplied by the caller for RPC work, while the controller's configured chain identifies the wallet chain. A public Solana endpoint that clearly names a different cluster is rejected before submission.
The path supports both wallet-side sign-and-send and local signing followed by RPC submission. It prepares legacy transaction lifetime data when needed, applies additional local signers first, and checks that caller signatures survive wallet signing. The result is a transaction signature, while wallet or configuration failures are reported through the normal wallet error path.
References: packages/…/wallet-controller.ts• L143-166 packages/…/wallet-controller.ts• L400-515 packages/…/transactions.ts• L22-83 packages/…/transactions.ts• L117-147
Sign-in verification
Sign In With Solana produces a signed message tied to sign-in input and an account. verifySignIn accepts either the plain sign-in message or a version 1 offchain message, then checks that the signed content matches the input, names the expected account, and has a valid signature from that account. A caller can use this check after the wallet provider returns sign-in output instead of trusting the wallet response alone.
References: packages/…/sign-in.ts• L18-57 packages/…/sign-in-test.ts• L71-141
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the pending context shape aligned with the live
WalletContextStateshape. Server rendering and first client effects depend on both views exposing the same action contract. - Keep selection semantics aligned with restoration and disconnect behavior.
undefined, a wallet name, andnullhave different meanings. - Keep capability checks aligned with the features advertised by the connected account. A function that appears in the snapshot must be callable for that same account.
- Keep the configured wallet chain aligned with connection endpoints used for submission. A mismatch can reject transactions before the wallet sees them.
- Keep transaction conversion rules aligned for legacy and versioned transactions. Wallet signing must not remove caller-provided signatures or lifetime data.
- Keep sign-in verification aligned with every sign-in output format that the controller accepts, including plain and version 1 offchain messages.
- Keep the
@solana/kit-plugin-walletdependency compatible with the provider and controller wallet lifecycle when its version changes.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
A component calls useConnection() or useWallet() outside its provider | The component cannot obtain a connection or wallet state | Render the component below the matching provider |
| A connect request has no selected wallet and no restored active wallet | Connection fails before a wallet prompt | Select a discovered wallet before calling connect() |
| The selected wallet disappears or is filtered out | The selected wallet is not ready and the live wallet set can become empty | Re-read the wallet list and choose an available wallet |
| A connected account does not advertise the requested transaction version or signing feature | The operation fails before wallet signing or submission | Check the snapshot capability and supported-version fields |
| The RPC endpoint clearly names a cluster different from the configured wallet chain | Transaction submission is rejected as a configuration error | Use a connection for the configured chain |
| A wallet changes a transaction that already has caller signatures | Submission fails because the caller's authorized message or signatures were not preserved | Let the wallet sign without altering caller-signed transaction data |
| Sign-in output names another account or changes the requested message | Sign-in verification returns false | Verify the output against the original input and expected account |
| Browser storage is unavailable or full | Persistent restoration or unrelated saved state is lost, but live in-memory state can continue | Treat storage as optional and rely on the current provider state |
Key files
| File | Why to read it |
|---|---|
| packages/…/WalletProvider.tsx | Shows the React lifecycle, pending server view, controller ownership, and context publication |
| packages/…/ConnectionProvider.tsx | Defines the shared RPC connection boundary used by wallet actions and application code |
| packages/…/wallet-controller.ts | Defines discovery, selection, connection, capability checks, error reporting, and transaction-facing state |
| packages/…/types.ts | Defines the public wallet snapshot and the action contract consumed by React components and neutral callers |
| packages/…/useLocalStorage.ts | Defines the separate JSON persistence helper and its storage-failure behavior |
| packages/…/transactions.ts | Defines transaction conversion and signature-preservation rules at the wallet boundary |
| packages/…/sign-in.ts | Defines trust checks for plain and offchain sign-in output |
| packages/…/provider-test.tsx | Demonstrates provider activation, restoration, capability exposure, and cleanup behavior |
| packages/…/package.json | Defines the wallet plugin dependency that supports the package's provider and controller integration |