Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: validate caveat networks before requesting permissions for chain that is not added #29930

Closed
wants to merge 10 commits into from

Conversation

ffmcgee725
Copy link
Member

@ffmcgee725 ffmcgee725 commented Jan 27, 2025

await window.ethereum.request({
 "method": "wallet_requestPermissions",
 "params": [
  {
    
    "eth_accounts": {
        "caveats": [
            {
                "type": "restrictReturnedAccounts",
                "value": []
            }
        ]
    },
    "endowment:permitted-chains": {
        "caveats": [
            {
                "type": "restrictNetworkSwitching",
                "value": ["0xa86a"]
            }
        ]
    }
  }
],
});

Currently when running this request on main branch, the bad thing here being that we are requesting permitted chain that we don't have added on the extension, we get this error on UI (see image below).

Image

Desired behaviour is what we have on current latest build, where running the same exact request with the chain we don't have added, throws a proper error in the console and no prompt in the UI

{
    "code": -32603,
    "message": "endowment:permitted-chains error: Received unrecognized chainId: \"0xa86a\". Please try adding the network first via wallet_addEthereumChain.",
    "data": {
        "cause": {
            "stack": "Error: endowment:permitted-chains error: Received unrecognized chainId: \"0xa86a\". Please try adding the network first via wallet_addEthereumChain.\n  at chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/common-4.js:1:419383\n  at Array.forEach (<anonymous>)\n  at chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/common-4.js:1:419329\n  at Object.validator (chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/common-4.js:1:419532)\n  at j.validateCaveat (chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/common-2.js:1:10699)\n  at chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/common-2.js:1:9684\n  at Array.forEach (<anonymous>)\n  at j.validatePermission (chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/common-2.js:1:9660)\n  at j.validateRequestedPermissions (chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/common-2.js:1:12496)\n  at j.requestPermissions (chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/common-2.js:1:10822)\n  at requestPermissionsForOrigin (chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/background-0.js:1:243117)\n  at implementation (chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/common-2.js:1:30815)\n  at chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/background-7.js:1:173218\n  at chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/common-1.js:1:20623\n  at new Promise (<anonymous>)\n  at _.p (chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/common-1.js:1:20280)\n  at _.m (chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/common-1.js:1:20198)\n  at async _.d (chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/common-1.js:1:19999)\n  at async _.l (chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/common-1.js:1:19825)",
            "message": "endowment:permitted-chains error: Received unrecognized chainId: \"0xa86a\". Please try adding the network first via wallet_addEthereumChain."
        }
    }
}

This pull request fixes this issue.

Description

Open in GitHub Codespaces

Related issues

Fixes:

Manual testing steps

  1. Send the request shown on description
  2. If chain is not configured in wallet, exception should be thrown in console

Screenshots/Recordings

Before

Image

After

Screen.Recording.2025-01-27.at.20.39.12.mov

Pre-merge author checklist

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.

@metamaskbot
Copy link
Collaborator

Builds ready [6ec0db9]
Page Load Metrics (1660 ± 107 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint31021181574350168
domContentLoaded140721061636224107
load142521241660223107
domInteractive23115483316
backgroundConnect86826189
firstReactRender15103473115
getState45818178
initialActions01000
loadScripts9611561119318086
setupStore678172110
uiStartup162328261945314151
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 648 Bytes (0.01%)
  • ui: 0 Bytes (0.00%)
  • common: 0 Bytes (0.00%)

@metamaskbot
Copy link
Collaborator

Builds ready [c61fbe8]
Page Load Metrics (1586 ± 50 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint32618961455380182
domContentLoaded13971842156210752
load14051853158610550
domInteractive227334136
backgroundConnect76325168
firstReactRender1479372612
getState4539105
initialActions01000
loadScripts987133611318038
setupStore7441184
uiStartup16372166179812962
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 648 Bytes (0.01%)
  • ui: 0 Bytes (0.00%)
  • common: 0 Bytes (0.00%)

@ffmcgee725 ffmcgee725 marked this pull request as ready for review January 28, 2025 10:58
@metamaskbot
Copy link
Collaborator

Builds ready [b69e5d9]
Page Load Metrics (1714 ± 101 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint55122041630308148
domContentLoaded14772168168720297
load149822061714211101
domInteractive237338188
backgroundConnect86126168
firstReactRender15109412813
getState45217168
initialActions01000
loadScripts10351576122015374
setupStore785262612
uiStartup166830772000389187
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 648 Bytes (0.01%)
  • ui: 0 Bytes (0.00%)
  • common: 0 Bytes (0.00%)

@metamaskbot
Copy link
Collaborator

Builds ready [de15169]
Page Load Metrics (1710 ± 84 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint32121331637348167
domContentLoaded13762074168016177
load13862138171017584
domInteractive229339189
backgroundConnect864292210
firstReactRender1698453014
getState465182010
initialActions01000
loadScripts9531496122013866
setupStore890242713
uiStartup170024842026244117
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 648 Bytes (0.01%)
  • ui: 0 Bytes (0.00%)
  • common: 0 Bytes (0.00%)

@jiexi
Copy link
Contributor

jiexi commented Jan 28, 2025

Desired behaviour is what we have on current latest build, where running the same exact request with the chain we don't have added, throws a proper error in the console and no prompt in the UI

I believe the endowment:permitted-chains error: Received unrecognized chainId: error is occurring because the chainId in the original request is still component state and is making it into the approveChainIds result even though it does not show up in the checkboxes. IMO, it is the responsibility of the UI here to properly handle the case where it gets passed chainIds that the wallet does not have. I don't believe that we need the validation/filtering logic at the handler level. Thoughts, @adonesky1 ?

@metamaskbot
Copy link
Collaborator

Builds ready [d2db02b]
Page Load Metrics (1913 ± 90 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint32722171762499240
domContentLoaded15722203188818991
load15782218191318790
domInteractive26106452110
backgroundConnect985302211
firstReactRender16187524220
getState483312512
initialActions01000
loadScripts11051635137416278
setupStore880212211
uiStartup186830692267309149
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 648 Bytes (0.01%)
  • ui: 0 Bytes (0.00%)
  • common: 0 Bytes (0.00%)

@metamaskbot
Copy link
Collaborator

Builds ready [57d2779]
Page Load Metrics (1675 ± 69 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint30220011532428206
domContentLoaded14291930165213565
load14341998167514469
domInteractive269043199
backgroundConnect870252010
firstReactRender1695422613
getState45412136
initialActions00000
loadScripts10101446120212158
setupStore768202210
uiStartup16812246194417785
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 648 Bytes (0.01%)
  • ui: 0 Bytes (0.00%)
  • common: 0 Bytes (0.00%)

@ffmcgee725
Copy link
Member Author

the approach followed in the following PR will make this unnecessary. Closing this.

@ffmcgee725 ffmcgee725 closed this Jan 30, 2025
@github-actions github-actions bot locked and limited conversation to collaborators Jan 30, 2025
@ffmcgee725 ffmcgee725 deleted the jc/request-permissions-unavailable-chain branch January 30, 2025 16:18
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants