Skip to content

Commit

Permalink
fix: bug when requested accounts/chainIds do not match wallet state (#…
Browse files Browse the repository at this point in the history
…29988)

## **Description**

Fix bug when requested accounts/chainIds do not match wallet state

[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/29988?quickstart=1)

## **Manual testing steps**
See videos below:
```
await window.ethereum.request({
 "method": "wallet_requestPermissions",
 "params": [
  {
    
    "eth_accounts": {
        "caveats": [
            {
                "type": "restrictReturnedAccounts",
                "value": []
            }
        ]
    },
    "endowment:permitted-chains": {
        "caveats": [
            {
                "type": "restrictNetworkSwitching",
                "value": ['0xbf04'] // any chainId not in the wallet
            }
        ]
    }
  }
],
});
```

```
await window.ethereum.request({
 "method": "wallet_requestPermissions",
 "params": [
  {
    
    "eth_accounts": {
        "caveats": [
            {
                "type": "restrictReturnedAccounts",
                "value": ["0x549f7D54F0a7ef67707c55dc51123586a2c6654e"] // any address not in the wallet
            }
        ]
    },
    "endowment:permitted-chains": {
        "caveats": [
            {
                "type": "restrictNetworkSwitching",
                "value": []
            }
        ]
    }
  }
],
});
```

## **Screenshots/Recordings**

### **Before**


https://github.com/user-attachments/assets/21f40a75-0513-4105-8f8a-f87faf2a583c


https://github.com/user-attachments/assets/d8e71869-5181-4751-a274-8e39cc8ec567


### **After**


https://github.com/user-attachments/assets/4f33ca41-657b-4902-b998-70c8c222484a


https://github.com/user-attachments/assets/d8059e25-2772-4aef-8d26-e6b6325788ac


https://github.com/user-attachments/assets/a67517c7-498f-485a-a547-59d330d9f0a8


## **Pre-merge author checklist**

- [ ] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [ ] 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
- [ ] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/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.

---------

Co-authored-by: ffmcgee <[email protected]>
Co-authored-by: ffmcgee <[email protected]>
Co-authored-by: ffmcgee <[email protected]>
  • Loading branch information
4 people authored Jan 30, 2025
1 parent 5f76d25 commit 538bbc9
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions ui/pages/permissions-connect/connect-page/connect-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { isEvmAccountType } from '@metamask/keyring-api';
import { NetworkConfiguration } from '@metamask/network-controller';
import { getEthAccounts, getPermittedEthChainIds } from '@metamask/multichain';
import { Hex } from '@metamask/utils';
import { isEqualCaseInsensitive } from '@metamask/controller-utils';
import { useI18nContext } from '../../../hooks/useI18nContext';
import {
getSelectedInternalAccount,
Expand Down Expand Up @@ -94,12 +95,18 @@ export const ConnectPage: React.FC<ConnectPageProps> = ({
);

const selectedNetworksList = selectedTestNetwork
? [...nonTestNetworks, selectedTestNetwork]
: nonTestNetworks;
? [...nonTestNetworks, selectedTestNetwork].map(({ chainId }) => chainId)
: nonTestNetworks.map(({ chainId }) => chainId);

const supportedRequestedChainIds = requestedChainIds.filter((chainId) =>
selectedNetworksList.includes(chainId),
);

const defaultSelectedChainIds =
requestedChainIds.length > 0
? requestedChainIds
: selectedNetworksList.map(({ chainId }) => chainId);
supportedRequestedChainIds.length > 0
? supportedRequestedChainIds
: selectedNetworksList;

const [selectedChainIds, setSelectedChainIds] = useState(
defaultSelectedChainIds,
);
Expand All @@ -111,12 +118,18 @@ export const ConnectPage: React.FC<ConnectPageProps> = ({
);
}, [accounts]);

const supportedRequestedAccounts = requestedAccounts.filter((account) =>
evmAccounts.find(({ address }) => isEqualCaseInsensitive(address, account)),
);

const currentAccount = useSelector(getSelectedInternalAccount);
const currentAccountAddress = isEvmAccountType(currentAccount.type)
? [currentAccount.address]
: []; // We do not support non-EVM accounts connections
const defaultAccountsAddresses =
requestedAccounts.length > 0 ? requestedAccounts : currentAccountAddress;
supportedRequestedAccounts.length > 0
? supportedRequestedAccounts
: currentAccountAddress;
const [selectedAccountAddresses, setSelectedAccountAddresses] = useState(
defaultAccountsAddresses,
);
Expand Down

0 comments on commit 538bbc9

Please sign in to comment.