Skip to content

fix: handle wallet account switch correctly#166

Open
0xEdouardEth wants to merge 2 commits intosolana-foundation:mainfrom
0xEdouardEth:fix/wallet-account-change
Open

fix: handle wallet account switch correctly#166
0xEdouardEth wants to merge 2 commits intosolana-foundation:mainfrom
0xEdouardEth:fix/wallet-account-change

Conversation

@0xEdouardEth
Copy link
Copy Markdown

Summary

When a user switches accounts in their wallet, the app failed to reflect the change properly:

  • The client store was not updated with the new account, so the UI kept showing the stale address
  • The wallet session held a static snapshot of the account instead of a live reference, causing signing operations to use the wrong account

This PR fixes both issues:

  • Store update on account change: the onAccountsChanged listener now updates the store with the new account (and disconnects gracefully when no accounts remain)
  • Session state consolidation: mutable session variables are grouped into a single sessionState object with a getter, so all signing operations always read the latest account

Test plan

  • Added unit test: store account updates when wallet emits a new account
  • Added unit test: wallet disconnects when no accounts are reported
  • Manual test: connect wallet, switch account in wallet extension, verify UI reflects new address and transactions sign with the correct account

## Summary
  - When a user switches accounts in their wallet (e.g. Phantom), `onAccountsChanged` was already
    handling the empty-accounts case (disconnect) but silently ignored the non-empty case; leaving
    the store with a stale session reference so React components (address display, balance, etc.)
    would not re-render with the new account.
  - Added the missing `else` branch in `connectWallet` to call `updateState` with the new
    `accounts[0]` when the wallet reports a different account, keeping the store in sync.
  - Added two unit tests covering both cases: account switch updates the store, and empty accounts
    array triggers a full disconnect.
The wallet standard session had two independent mutable variables
(currentAccount and sessionAccount) updated by account change
listeners. This scattered mutation made it easy for future changes
to update one but forget the other, leading to inconsistent state
where signing operations could reference a stale account.

This consolidates both into a single sessionState object, making
the mutation surface explicit and co-located. The session's account
property is now a getter that always reads from sessionState, so
consumers always see the latest account after a wallet-side switch
instead of a stale snapshot captured at session creation time.
@vercel
Copy link
Copy Markdown

vercel Bot commented May 4, 2026

@0xEdouardEth is attempting to deploy a commit to the Solana Foundation Team on Vercel.

A member of the Team first needs to authorize it.

@0xEdouardEth 0xEdouardEth force-pushed the fix/wallet-account-change branch 2 times, most recently from f129370 to b5b2d88 Compare May 4, 2026 15:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant