Skip to content

fix(signer): sanitize chain state on reconnect to prevent stale address#345

Open
mehmetkr-31 wants to merge 1 commit into
base:masterfrom
mehmetkr-31:fix/reconnect-sanitize-chain-state
Open

fix(signer): sanitize chain state on reconnect to prevent stale address#345
mehmetkr-31 wants to merge 1 commit into
base:masterfrom
mehmetkr-31:fix/reconnect-sanitize-chain-state

Conversation

@mehmetkr-31

Copy link
Copy Markdown

Summary

Fixes #214 — incorrect or missing public address after reconnecting signer.

Root Cause

When Signer is reconstructed after cleanup() (e.g., page reload + reconnect):

  1. The constructor reads account.chain from the store.
  2. If the store was cleared by cleanup(), account.chain is undefined.
  3. The fallback params.metadata.appChainIds?.[0] ?? 1 could be stale, missing, or default to chain id 1 (Ethereum mainnet) even when the user was on Base (8453).
  4. This caused this.accounts to remain empty until the next handshake, and handleGetCapabilitiesRequest to throw unauthorized.

Fix

Prefer the chains array already registered in the store before falling back to constructor metadata:

Constructor:

this.chain = account.chain ?? chains?.[0] ?? {
  id: params.metadata.appChainIds?.[0] ?? 1,
};

cleanup():

const chains = this.storeInstance.getState().chains;
this.chain = chains?.[0] ?? {
  id: metadata?.appChainIds?.[0] ?? 1,
};

This ensures the correct chain context (e.g., Base mainnet 8453) is preserved across reconnect cycles instead of silently falling back to Ethereum mainnet.

Test Plan

  • yarn workspace @base-org/account test --run → 999/999 pass

Related Issues

When a Signer is reconstructed after cleanup (e.g., reconnect), the
constructor reads account.chain from the store. If the store was cleared,
account.chain is undefined and the fallback used params.metadata.appChainIds
which could be stale or missing, defaulting to chain id 1 (mainnet).

This caused two problems:
1. Incorrect public address returned after reconnect on Base (chain id 8453)
2. handleGetCapabilitiesRequest failing because accounts array was empty
   until the next successful handshake

The fix prefers the chains array already registered in the store before
falling back to metadata, ensuring the correct chain context is preserved
across reconnect cycles.
@cb-heimdall

Copy link
Copy Markdown
Collaborator

🟡 Heimdall Review Status

Requirement Status More Info
Reviews 🟡 0/1
Denominator calculation
Show calculation
1 if user is bot 0
1 if user is external 0
2 if repo is sensitive 0
From .codeflow.yml 1
Additional review requirements
Show calculation
Max 0
0
From CODEOWNERS 0
Global minimum 0
Max 1
1
1 if commit is unverified 0
Sum 1

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.

Bug: Account SDK returns incorrect or missing public address after reconnecting signer

2 participants