MetaMask / MetaMask/metamask-mobile

WalletConnect: chainId mismatch error when calling signTypedData after switchChain

Open
#25,194 1 comment 0 reactions 0 assignees View on GitHub
external-contributor INVALID-ISSUE-TEMPLATE
Dominant language
TypeScript
Stars
3k
Forks
1.7k
Avg merge
1d 14h
Merged PRs (30d)
669

Description

### What happened?

When connecting to a dApp via WalletConnect and calling `wallet_switchEthereumChain` immediately followed by `eth_signTypedData_v4`, MetaMask shows an error:

```
Active chainId is 0xa4b1 but received 0x89
```

The wallet's internal state doesn't appear to be fully updated after the network switch, causing a chainId mismatch when the subsequent signing request arrives.

### Steps to reproduce

1. Clone and run the reproduction app:
```bash
git clone https://github.com/enesozturk/metamask-switchchain-issue.git
cd metamask-switchchain-issue
pnpm install
cp .env.example .env.local
# Add your Reown project ID to .env.local
pnpm dev
```
2. Open http://localhost:3000 in your desktop browser
3. Click **Connect Wallet**
4. Scan the WalletConnect QR code with MetaMask mobile
5. Once connected, select a network (e.g., Polygon)
6. Click **Sign**
7. Observe the error in MetaMask: "Active chainId is 0xa4b1 but received 0x89"
8. If no error on first attempt, select a **different network** and click Sign again

### Expected behavior

After `wallet_switchEthereumChain` resolves successfully, the subsequent `eth_signTypedData_v4` request should use the newly switched chainId without any mismatch error.

### Screenshots/Recordings

https://github.com/user-attachments/assets/06d594db-dea1-4f03-b37f-293427ade48f

### Error messages or log output

```
Active chainId is 0xa4b1 but received 0x89
```

### What is the wallet setup used?

- [ ] Hot wallet
- [x] Hardware wallet
- [ ] Wallet with snaps
- [ ] Wallet with imported tokens
- [ ] Wallet with connected dapp
- [ ] Key vault
- [ ] Other:

### Version

7.61.6 (latest from App Store/Play Store)

### Build number

_Latest available_

### Device

iPhone 14 Pro

### Operating system

iOS

### Additional context

**Reproduction repo:** https://github.com/enesozturk/metamask-switchchain-issue

The issue occurs when using WalletConnect to connect MetaMask mobile to a dApp. The dApp uses:
- AppKit (Reown) for wallet connection
- Wagmi v3 `useSwitchChain` and `useSignTypedData` hooks

The sequence that triggers the bug:
```typescript
// Step 1: Switch network
await switchChainAsync({ chainId: selectedNetworkId })

// Step 2: Sign typed data (immediately after)
await signTypedDataAsync(typedData)
```

This issue does **not** reproduce with other wallets - only MetaMask mobile via WalletConnect.

### Severity

High - This blocks users from signing typed data on networks different from their currently active network, which is a common flow for multi-chain dApps.

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the flow in the linked metamask-switchchain-issue app, then trace the wallet_switchEthereumChain handling and the subsequent eth_signTypedData_v4 request in the mobile WalletConnect path. Done means signing immediately after a successful switch uses the newly selected chainId without a mismatch error, including after switching between different networks.

Written by the indexing model from the issue text.

Assessment

Tech stack
ios, react-native, typescript
Domain
blockchain, mobile
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.