feat: add `wallet_switchEthereumChain` permission and enforce it (behind feature flag)
Description
This PR introduces the wallet_switchEthereumChain permission (as an endowment type permission) behind a feature flag (CHAIN_PERMISSIONS), allowing us to remove the wallet_switchEthereum confirmation. Now instead of seeing this confirmation everytime wallet_switchEthereumChain is called for a given chain, users sees a confirmation to add a permission for the dapp to switch to this chain after which they will no longer see any confirmation at all when the dapp next attempts to switch to this chain.
Related issues
Fixes: https://github.com/MetaMask/MetaMask-planning/issues/2327
Manual testing steps
-
CHAIN_PERMISSIONS=1 yarn start - Go to a dapp (uniswap is good for this test) and connect the wallet
- Use the in dapp switch chain UI
- You should see a permission confirmation (instead of the old switch chain confirmation)
- The UI is not fully implemented, this will be completed by the
wallet-uxteam.
- The UI is not fully implemented, this will be completed by the
- Manually switch to another chain in the wallet
- Go back to the dapp and switch to the same chain you just added permissions for
- You should not see any confirmation but the network should change
Screenshots/Recordings
Before
https://github.com/MetaMask/metamask-extension/assets/34557516/6f6321ef-5fb5-48db-84a2-790cf178ea7f
After
https://github.com/MetaMask/metamask-extension/assets/34557516/c94c67be-f17d-4b9d-aec0-431ea89500f5
Pre-merge author checklist
- [ ] I’ve followed MetaMask Coding Standards.
- [ ] 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 format if applicable
- [ ] I’ve applied the right labels on the PR (see labeling guidelines). 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.
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.
Builds ready [89955a4]
- builds: chrome, firefox
- builds (beta): chrome
- builds (flask): chrome, firefox
- builds (MMI): chrome, firefox
- builds (test): chrome, firefox
- builds (test-flask): chrome, firefox
- build viz: Build System
- mv3: Background Module Init Stats
- mv3: UI Init Stats
- mv3: Module Load Stats
- mv3: Bundle Size Stats
- mv2: E2e Actions Stats
- code coverage: Report
- storybook: Storybook
- typescript migration: Dashboard
- all artifacts
Page Load Metrics (823 ± 631 ms)
| Platform | Page | Metric | Min (ms) | Max (ms) | Average (ms) | StandardDeviation (ms) | MarginOfError (ms) |
|---|---|---|---|---|---|---|---|
| Chrome | Home | firstPaint | 60 | 215 | 98 | 39 | 19 |
| domContentLoaded | 9 | 34 | 15 | 7 | 3 | ||
| load | 48 | 3227 | 823 | 1313 | 631 | ||
| domInteractive | 9 | 34 | 15 | 7 | 3 |
Bundle size diffs [🚨 Warning! Bundle size has increased!]
- background: -8.2 KiB (-0.25%)
- ui: 889 Bytes (0.01%)
- common: 12.77 KiB (0.21%)
Codecov Report
Attention: Patch coverage is 40.12739% with 188 lines in your changes are missing coverage. Please review.
Project coverage is 67.43%. Comparing base (
4233f9e) to head (2619ccb). Report is 30 commits behind head on develop.
:exclamation: Current head 2619ccb differs from pull request most recent head 80fbb98
Please upload reports for the commit 80fbb98 to get more accurate results.
Additional details and impacted files
@@ Coverage Diff @@
## develop #24415 +/- ##
===========================================
+ Coverage 67.41% 67.43% +0.02%
===========================================
Files 1288 1289 +1
Lines 50233 50340 +107
Branches 13013 13051 +38
===========================================
+ Hits 33863 33943 +80
- Misses 16370 16397 +27
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
Builds ready [cefcd71]
- builds: chrome, firefox
- builds (beta): chrome
- builds (flask): chrome, firefox
- builds (MMI): chrome, firefox
- builds (test): chrome, firefox
- builds (test-flask): chrome, firefox
- build viz: Build System
- mv3: Background Module Init Stats
- mv3: UI Init Stats
- mv3: Module Load Stats
- mv3: Bundle Size Stats
- mv2: E2e Actions Stats
- code coverage: Report
- storybook: Storybook
- typescript migration: Dashboard
- all artifacts
Page Load Metrics (727 ± 553 ms)
| Platform | Page | Metric | Min (ms) | Max (ms) | Average (ms) | StandardDeviation (ms) | MarginOfError (ms) |
|---|---|---|---|---|---|---|---|
| Chrome | Home | firstPaint | 58 | 129 | 83 | 17 | 8 |
| domContentLoaded | 8 | 31 | 12 | 5 | 2 | ||
| load | 47 | 2850 | 727 | 1152 | 553 | ||
| domInteractive | 8 | 31 | 12 | 5 | 2 |
Bundle size diffs [🚨 Warning! Bundle size has increased!]
- background: -8.2 KiB (-0.25%)
- ui: 889 Bytes (0.01%)
- common: 12.77 KiB (0.21%)
I think this comment got dismissed due to a rebase, so restating it (re: app/scripts/lib/rpc-method-middleware/handlers/switch-ethereum-chain.test.js):
Are there any test cases for when the chain permissions feature flag is enabled?
Are there any test cases for when the chain permissions feature flag is enabled?
Not but I will add them. I was waiting for more general validation of the approach before proceeding with the tests. Should've added that todo context in the PR description
Refactored Switch and Add chain handlers, still todo:
- [x] Add tests
- [x] Handle permission removal when networkConfiguration is deleted
Builds ready [4ab4c5e]
- builds: chrome, firefox
- builds (beta): chrome
- builds (flask): chrome, firefox
- builds (MMI): chrome, firefox
- builds (test): chrome, firefox
- builds (test-flask): chrome, firefox
- build viz: Build System
- mv3: Background Module Init Stats
- mv3: UI Init Stats
- mv3: Module Load Stats
- mv3: Bundle Size Stats
- mv2: E2e Actions Stats
- code coverage: Report
- storybook: Storybook
- typescript migration: Dashboard
- all artifacts
Page Load Metrics (1368 ± 565 ms)
| Platform | Page | Metric | Min (ms) | Max (ms) | Average (ms) | StandardDeviation (ms) | MarginOfError (ms) |
|---|---|---|---|---|---|---|---|
| Chrome | Home | firstPaint | 73 | 151 | 104 | 22 | 10 |
| domContentLoaded | 10 | 50 | 18 | 12 | 6 | ||
| load | 59 | 2794 | 1368 | 1177 | 565 | ||
| domInteractive | 10 | 50 | 18 | 12 | 6 |
Bundle size diffs [🚨 Warning! Bundle size has increased!]
- background: -7.82 KiB (-0.23%)
- ui: 856 Bytes (0.01%)
- common: 12.6 KiB (0.20%)
Builds ready [0b05c11]
- builds: chrome, firefox
- builds (beta): chrome
- builds (flask): chrome, firefox
- builds (MMI): chrome, firefox
- builds (test): chrome, firefox
- builds (test-flask): chrome, firefox
- build viz: Build System
- mv3: Background Module Init Stats
- mv3: UI Init Stats
- mv3: Module Load Stats
- mv3: Bundle Size Stats
- mv2: E2e Actions Stats
- code coverage: Report
- storybook: Storybook
- typescript migration: Dashboard
- all artifacts
Page Load Metrics (664 ± 494 ms)
| Platform | Page | Metric | Min (ms) | Max (ms) | Average (ms) | StandardDeviation (ms) | MarginOfError (ms) |
|---|---|---|---|---|---|---|---|
| Chrome | Home | firstPaint | 69 | 133 | 92 | 20 | 10 |
| domContentLoaded | 9 | 41 | 16 | 9 | 4 | ||
| load | 57 | 2628 | 664 | 1029 | 494 | ||
| domInteractive | 9 | 41 | 16 | 9 | 4 |
Bundle size diffs [🚨 Warning! Bundle size has increased!]
- background: -7.58 KiB (-0.23%)
- ui: 1012 Bytes (0.01%)
- common: 12.86 KiB (0.21%)
Builds ready [c1798c9]
- builds: chrome, firefox
- builds (beta): chrome
- builds (flask): chrome, firefox
- builds (MMI): chrome, firefox
- builds (test): chrome, firefox
- builds (test-flask): chrome, firefox
- build viz: Build System
- mv3: Background Module Init Stats
- mv3: UI Init Stats
- mv3: Module Load Stats
- mv3: Bundle Size Stats
- mv2: E2e Actions Stats
- code coverage: Report
- storybook: Storybook
- typescript migration: Dashboard
- all artifacts
Page Load Metrics (982 ± 541 ms)
| Platform | Page | Metric | Min (ms) | Max (ms) | Average (ms) | StandardDeviation (ms) | MarginOfError (ms) |
|---|---|---|---|---|---|---|---|
| Chrome | Home | firstPaint | 72 | 125 | 89 | 14 | 7 |
| domContentLoaded | 9 | 19 | 13 | 3 | 1 | ||
| load | 59 | 2640 | 982 | 1126 | 541 | ||
| domInteractive | 9 | 19 | 13 | 3 | 1 |
Bundle size diffs [🚨 Warning! Bundle size has increased!]
- background: -7.58 KiB (-0.23%)
- ui: 1012 Bytes (0.01%)
- common: 12.86 KiB (0.21%)
DRYing up the addEthereumChain and switchEthereumChain implementations. I recommend creating a module like lib/rpc-method-middleware/handlers/ethereum-chain-utils.js or something along those lines.
@rekmarks I've DRY'ed a bunch and made a shared module as you recommend!
Builds ready [df5964e]
- builds: chrome, firefox
- builds (beta): chrome
- builds (flask): chrome, firefox
- builds (MMI): chrome, firefox
- builds (test): chrome, firefox
- builds (test-flask): chrome, firefox
- build viz: Build System
- mv3: Background Module Init Stats
- mv3: UI Init Stats
- mv3: Module Load Stats
- mv3: Bundle Size Stats
- mv2: E2e Actions Stats
- code coverage: Report
- storybook: Storybook
- typescript migration: Dashboard
- all artifacts
Page Load Metrics (1308 ± 546 ms)
| Platform | Page | Metric | Min (ms) | Max (ms) | Average (ms) | StandardDeviation (ms) | MarginOfError (ms) |
|---|---|---|---|---|---|---|---|
| Chrome | Home | firstPaint | 64 | 145 | 97 | 25 | 12 |
| domContentLoaded | 9 | 69 | 16 | 13 | 6 | ||
| load | 52 | 2886 | 1308 | 1138 | 546 | ||
| domInteractive | 9 | 69 | 16 | 13 | 6 |
Bundle size diffs [🚨 Warning! Bundle size has increased!]
- background: -9.07 KiB (-0.27%)
- ui: 863 Bytes (0.01%)
- common: 12.86 KiB (0.21%)
Builds ready [4f5a98c]
- builds: chrome, firefox
- builds (beta): chrome
- builds (flask): chrome, firefox
- builds (MMI): chrome, firefox
- builds (test): chrome, firefox
- builds (test-flask): chrome, firefox
- build viz: Build System
- mv3: Background Module Init Stats
- mv3: UI Init Stats
- mv3: Module Load Stats
- mv3: Bundle Size Stats
- mv2: E2e Actions Stats
- code coverage: Report
- storybook: Storybook
- typescript migration: Dashboard
- all artifacts
Page Load Metrics (1242 ± 512 ms)
| Platform | Page | Metric | Min (ms) | Max (ms) | Average (ms) | StandardDeviation (ms) | MarginOfError (ms) |
|---|---|---|---|---|---|---|---|
| Chrome | Home | firstPaint | 69 | 168 | 91 | 22 | 11 |
| domContentLoaded | 9 | 72 | 18 | 15 | 7 | ||
| load | 56 | 2369 | 1242 | 1066 | 512 | ||
| domInteractive | 9 | 72 | 18 | 15 | 7 |
Bundle size diffs [🚨 Warning! Bundle size has increased!]
- background: -9.05 KiB (-0.27%)
- ui: 963 Bytes (0.01%)
- common: 12.76 KiB (0.21%)
Builds ready [2619ccb]
- builds: chrome, firefox
- builds (beta): chrome
- builds (flask): chrome, firefox
- builds (MMI): chrome, firefox
- builds (test): chrome, firefox
- builds (test-flask): chrome, firefox
- build viz: Build System
- mv3: Background Module Init Stats
- mv3: UI Init Stats
- mv3: Module Load Stats
- mv3: Bundle Size Stats
- mv2: E2e Actions Stats
- code coverage: Report
- storybook: Storybook
- typescript migration: Dashboard
- all artifacts
Page Load Metrics (920 ± 627 ms)
| Platform | Page | Metric | Min (ms) | Max (ms) | Average (ms) | StandardDeviation (ms) | MarginOfError (ms) |
|---|---|---|---|---|---|---|---|
| Chrome | Home | firstPaint | 69 | 177 | 108 | 33 | 16 |
| domContentLoaded | 9 | 61 | 23 | 16 | 8 | ||
| load | 57 | 3488 | 920 | 1306 | 627 | ||
| domInteractive | 9 | 61 | 23 | 16 | 8 |
Bundle size diffs [🚨 Warning! Bundle size has increased!]
- background: -9.06 KiB (-0.27%)
- ui: 963 Bytes (0.01%)
- common: 12.76 KiB (0.21%)
Builds ready [ec84f4f]
- builds: chrome, firefox
- builds (beta): chrome
- builds (flask): chrome, firefox
- builds (MMI): chrome, firefox
- builds (test): chrome, firefox
- builds (test-flask): chrome, firefox
- build viz: Build System
- mv3: Background Module Init Stats
- mv3: UI Init Stats
- mv3: Module Load Stats
- mv3: Bundle Size Stats
- mv2: E2e Actions Stats
- code coverage: Report
- storybook: Storybook
- typescript migration: Dashboard
- all artifacts
Page Load Metrics (401 ± 380 ms)
| Platform | Page | Metric | Min (ms) | Max (ms) | Average (ms) | StandardDeviation (ms) | MarginOfError (ms) |
|---|---|---|---|---|---|---|---|
| Chrome | Home | firstPaint | 64 | 134 | 88 | 19 | 9 |
| domContentLoaded | 9 | 41 | 16 | 10 | 5 | ||
| load | 51 | 2369 | 401 | 791 | 380 | ||
| domInteractive | 9 | 41 | 16 | 10 | 5 |
Bundle size diffs [🚨 Warning! Bundle size has increased!]
- background: -9.06 KiB (-0.27%)
- ui: 963 Bytes (0.01%)
- common: 12.74 KiB (0.21%)
Codecov Report
Attention: Patch coverage is 40.25559% with 187 lines in your changes are missing coverage. Please review.
Project coverage is 67.43%. Comparing base (
4233f9e) to head (ec84f4f). Report is 31 commits behind head on develop.
Additional details and impacted files
@@ Coverage Diff @@
## develop #24415 +/- ##
===========================================
+ Coverage 67.41% 67.43% +0.02%
===========================================
Files 1288 1289 +1
Lines 50233 50340 +107
Branches 13013 13050 +37
===========================================
+ Hits 33863 33943 +80
- Misses 16370 16397 +27
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.