Skip to content

Commit

Permalink
temp+fix: validate chain before send (#12048)
Browse files Browse the repository at this point in the history
<!--
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 validates the chain id before handling the deeplink for making
an ethereum send.

## **Related issues**

Fixes: [11966](#11966)

## **Manual testing steps**

1. Open the deeplink
https://metamask.app.link/send/0x2990079bcdEe240329a520d2444386FC119da21a@56?value=3e18
2. Ensure that BNB is not added as a chain on the wallet
3. You should see an error "Unable to find network with chain id"

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

https://github.com/user-attachments/assets/c790fcdc-7737-4718-b5b5-f117151ea0a2

## **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
- [ ] I’ve included tests if applicable
- [ ] 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.
  • Loading branch information
runway-github[bot] authored Oct 28, 2024
2 parents a9f908a + d1a58ab commit a2176ce
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
10 changes: 5 additions & 5 deletions app/core/DeeplinkManager/Handlers/switchNetwork.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ describe('switchNetwork', () => {
);
});

it('should not dispatch an alert for an invalid switchToChainId', () => {
const switchToChainId = 'invalid_chain_id' as `${number}` | undefined;
it('should throw an error for an invalid switchToChainId', () => {
const switchToChainId = '56' as `${number}` | undefined;
mockHandleNetworkSwitch.mockReturnValue(undefined);

switchNetwork({ deeplinkManager, switchToChainId });

expect(deeplinkManager.dispatch).not.toHaveBeenCalled();
expect(() => switchNetwork({ deeplinkManager, switchToChainId })).toThrow(
`Unable to find network with chain id ${switchToChainId}`,
);
});

it('should not dispatch an alert when switchNetwork returns undefined', () => {
Expand Down
17 changes: 13 additions & 4 deletions app/core/DeeplinkManager/Handlers/switchNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { handleNetworkSwitch } from '../../../util/networks';
import DevLogger from '../../SDKConnect/utils/DevLogger';
import DeeplinkManager from '../DeeplinkManager';

import { selectChainId } from '../../../selectors/networkController';
import { store } from '../../../store';
import { toHex } from '@metamask/controller-utils';

function switchNetwork({
deeplinkManager,
switchToChainId,
Expand All @@ -15,11 +19,16 @@ function switchNetwork({
typeof switchToChainId === 'number' ||
typeof switchToChainId === 'string'
) {
const chainId = String(switchToChainId);

const networkName = handleNetworkSwitch(chainId);
const newChainId = String(switchToChainId);
const networkName = handleNetworkSwitch(newChainId);

if (!networkName) return;
if (!networkName) {
const activeChainId = selectChainId(store.getState());
if (activeChainId === toHex(newChainId)) {
return;
}
throw new Error(`Unable to find network with chain id ${newChainId}`);
}

deeplinkManager.dispatch(
showAlert({
Expand Down

0 comments on commit a2176ce

Please sign in to comment.