chore(runway): cherry-pick fix: multichain account network permissions switch cp-7.57.0#21208
Conversation
…s switch cp-7.57.0 (#21043) <!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until the template has been completely filled out, and PR status checks have passed at least once. --> ## **Description** This PR fixes following problem: When a user changes network permissions for a connected dapp (by going to Permissions tab → "Use your enabled networks" → selecting different networks → Update), the dapp's provider doesn't get notified of the network change. Transactions continue to execute on the initial network instead of the newly permitted network. <!-- Write a short description of the changes included in this pull request, also include relevant motivation and context. Have in mind the following questions: 1. What is the reason for the change? 2. What is the improvement/solution? --> ## **Changelog** <!-- If this PR is not End-User-Facing and should not show up in the CHANGELOG, you can choose to either: 1. Write `CHANGELOG entry: null` 2. Label with `no-changelog` If this PR is End-User-Facing, please write a short User-Facing description in the past tense like: `CHANGELOG entry: Added a new tab for users to see their NFTs` `CHANGELOG entry: Fixed a bug that was causing some NFTs to flicker` (This helps the Release Engineer do their job more quickly and accurately) --> CHANGELOG entry: Fixed a bug preventing network switch when dapp network permissions changes ## **Related issues** Fixes: #20722 ## **Manual testing steps** ```gherkin Feature: Dapp network permissions change Scenario: user changes network permissions Given user connected to the dapp with network A When user click on Account Icon on top right corner → goes to Permissions tab → "Use your enabled networks" → selecting different networks → Update Then after clicking Connect, network of the dapp should change ``` ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> https://github.com/user-attachments/assets/b96c2502-777f-43e9-a029-f5d225650bd7 ### **Before** <!-- [screenshots/recordings] --> ### **After** <!-- [screenshots/recordings] --> ## **Pre-merge author checklist** - [x] I’ve followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile Coding Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [x] I've completed the PR template to the best of my ability - [x] I’ve included tests if applicable - [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [x] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots. <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Switches the active EVM network for a dapp when network permissions change and triggers a provider update notification. > > - **MultichainAccountPermissions** (`MultichainAccountPermissions.tsx`): > - Implements async `handleNetworksSelected` to switch the dapp's active EVM network when the current permitted network is removed and another EVM chain is selected. > - Uses `isPerDappSelectedNetworkEnabled`, `useNetworkInfo`, and `selectEvmChainId` to resolve current chain and per-dapp context. > - Finds target network via `networkConfigurations`, extracts `networkClientId`, calls `SelectedNetworkController.setNetworkClientIdForDomain`, and triggers `SelectedNetworkController.update` to notify the dapp. > - Adds CAIP helpers (`parseChainId`, `KnownCaipNamespace`, `hasProperty`) and types (`NetworkConfiguration`). > - **Tests** (`MultichainAccountPermissions.test.tsx`): > - Extends Engine and selectors mocks (Network/SelectedNetworkController, network configurations, per-dapp flag, network info). > - Adds test for selecting a new network (e.g., Sepolia) and confirming update. > - Retains and adjusts navigation and selection flow tests. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit ab6d187. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
|
CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes. |
| } | ||
| ).update((state: { activeDappNetwork: string | null }) => { | ||
| state.activeDappNetwork = networkClientId; | ||
| }); |
There was a problem hiding this comment.
Bug: Unsafe Type Assertion Causes Runtime Crashes
An unsafe type assertion on Engine.context.SelectedNetworkController assumes an update method with a specific signature. This lacks runtime verification and can lead to a crash if the method doesn't exist or differs from the assumed type, as suggested by test mocks.
| const currentEvmCaipChainId: CaipChainId = | ||
| isPerDappSelectedNetworkEnabled() | ||
| ? `eip155:${parseInt(networkInfo.chainId, 16)}` | ||
| : `eip155:${parseInt(currentEvmChainId, 16)}`; |
There was a problem hiding this comment.
Bug: Missing Null Checks in Network Chain ID Parsing
In handleNetworksSelected, parseInt is called on networkInfo.chainId and currentEvmChainId without null/undefined checks. This can cause runtime errors if values are missing, or produce malformed CAIP chain IDs like eip155:NaN, affecting network operations.
| ) { | ||
| const { rpcEndpoints, defaultRpcEndpointIndex } = | ||
| config as NetworkConfiguration; | ||
| const { networkClientId } = rpcEndpoints[defaultRpcEndpointIndex]; |
|
|
No release label on PR. Adding release label release-7.57.0 on PR, as PR was cherry-picked in branch 7.57.0. |



Description
This PR fixes following problem:
When a user changes network permissions for a connected dapp (by going
to Permissions tab → "Use your enabled networks" → selecting different
networks → Update), the dapp's provider doesn't get notified of the
network change. Transactions continue to execute on the initial network
instead of the newly permitted network.
Changelog
CHANGELOG entry: Fixed a bug preventing network switch when dapp network
permissions changes
Related issues
Fixes: #20722
Manual testing steps
Screenshots/Recordings
dapp-network-permissions.mov
Before
After
Pre-merge author checklist
Docs and MetaMask Mobile
Coding
Standards.
if applicable
guidelines).
Not required for external contributors.
Pre-merge reviewer checklist
app, test code being changed).
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
Note
Ensures per-dapp network switches (and provider notification) when updating permitted networks in Multichain Account Permissions, with supporting selectors/utilities and tests.
MultichainAccountPermissions.tsx)SelectedNetworkController.setNetworkClientIdForDomainandSelectedNetworkController.update.useNetworkInfo,selectEvmChainId,parseChainId, andKnownCaipNamespaceto switch when the originally permitted network is removed.networkConfigurationsto resolvenetworkClientId; guards withhasPropertyandNetworkConfigurationtypes.isPerDappSelectedNetworkEnabledand usesgetNetworkImageSourcefor avatars.MultichainAccountPermissions.test.tsx)SelectedNetworkController.update, network state,isPerDappSelectedNetworkEnabled, anduseNetworkInfo.Written by Cursor Bugbot for commit 8b3bfd6. This will update automatically on new commits. Configure here.
9f95100