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

feat: add wallet_switchEthereumChain permission and enforce it (behind feature flag) #24415

Merged
merged 96 commits into from
May 24, 2024

Conversation

adonesky1
Copy link
Contributor

@adonesky1 adonesky1 commented May 7, 2024

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.

Open in GitHub Codespaces

Related issues

Fixes: https://github.com/MetaMask/MetaMask-planning/issues/2327

Manual testing steps

  1. CHAIN_PERMISSIONS=1 yarn start
  2. Go to a dapp (uniswap is good for this test) and connect the wallet
  3. Use the in dapp switch chain UI
  4. 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-ux team.
  5. Manually switch to another chain in the wallet
  6. Go back to the dapp and switch to the same chain you just added permissions for
  7. You should not see any confirmation but the network should change

Screenshots/Recordings

Before

Screen.Recording.2024-05-14.at.2.57.09.PM.mov

After

Screen.Recording.2024-05-14.at.3.01.13.PM.mov

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.

Copy link
Contributor

github-actions bot commented May 7, 2024

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.

@adonesky1 adonesky1 changed the title Zb/switch as perm poc chainid Add wallet_switchEthereumChain permission May 7, 2024
app/scripts/controllers/permissions/specifications.js Outdated Show resolved Hide resolved
app/scripts/controllers/permissions/specifications.js Outdated Show resolved Hide resolved
shared/constants/methods-tags.ts Outdated Show resolved Hide resolved
@metamaskbot
Copy link
Collaborator

Builds ready [df5964e]
Page Load Metrics (1308 ± 546 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint64145972512
domContentLoaded96916136
load52288613081138546
domInteractive96916136
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%)

@metamaskbot
Copy link
Collaborator

Builds ready [4f5a98c]
Page Load Metrics (1242 ± 512 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint69168912211
domContentLoaded97218157
load56236912421066512
domInteractive97218157
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%)

Copy link
Contributor

@mcmire mcmire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tested this out locally and it seems to work as advertised.

Heads up that I have a PR open to group network configurations by chain ID. I've left some notes on how this code will change once that PR is merged. (Don't worry, it will be a while, as we are waiting for UX, but I thought you might want to know.)

Only piece of feedback that we could consider changing now is the networkConfigurationId argument in switchChain.

import { PermissionNames } from '../../../controllers/permissions';
import { getValidUrl } from '../../util';

export function findExistingNetwork(chainId, findNetworkConfigurationBy) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again this will change following MetaMask/core#4286 — Infura networks will be integrated into network configurations and it will be possible to look it up by calling getNetworkConfigurationByChainId on the network controller. So you will be able to simplify this to:

export function findExistingNetwork(chainId, getNetworkConfigurationByChainId) {
  return getNetworkConfigurationByChainId(chainId);
}

or even just inline this function into wherever it's being used.

}),
);
}
const networkClientIdToSwitchTo =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heads up that this will change following MetaMask/core#4286, and you'll probably want something like:

const networkConfigurationForRequestedChainId =
  getNetworkConfigurationByChainId(chainId);

const networkClientIdToSwitchTo = networkConfigurationForRequestedChainId 
 
 .rpcEndpointsByUrl[networkConfigurationForRequestedChainId.defaultRpcEndpointUrl]
  .networkClientId;

'nativeCurrency',
]),
const currentChainIdForDomain = getCurrentChainIdForDomain(origin);
const currentNetworkConfiguration = findExistingNetwork(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heads up that this will change following MetaMask/core#4286, and you'll probably want something like:

const currentNetworkConfiguration = getNetworkConfigurationByChainId(chainIdForDomain);

(and similar for below)

if (currentChainId === _chainId && currentRpcUrl === firstValidRPCUrl) {
return end();
}
if (!existingNetwork || existingNetwork.rpcUrl !== firstValidRPCUrl) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will also change after MetaMask/core#4286. Network configurations will have multiple RPC endpoints and also a default RPC endpoint to use. I'm not sure exactly how this will look but you may be able to simplify this.

fromNetworkConfiguration: currentNetworkConfiguration,
};
} else {
networkConfigurationId = existingNetwork.id ?? existingNetwork.type;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also following MetaMask/core#4286, the network configuration won't have an id property; rather, RPC endpoints will have an associated networkClientId.

);
}

removeNetworkConfiguration(networkConfigurationId) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole function will probably change following MetaMask/core#4286. There will only ever be one network configuration associated with a chain. removeNetworkConfiguration will be replaced with removeNetwork and take a chainId rather than a networkConfigurationId. So we may simply be able to say:

removeNetwork(chainId) {
  this.removeAllChainIdPermissions(chainId);
  this.networkController.removeNetwork(chainId);
}

@metamaskbot
Copy link
Collaborator

Builds ready [2619ccb]
Page Load Metrics (920 ± 627 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint691771083316
domContentLoaded96123168
load5734889201306627
domInteractive96123168
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%)

@adonesky1 adonesky1 requested review from mcmire and rekmarks May 23, 2024 22:33
Copy link
Member

@rekmarks rekmarks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@metamaskbot
Copy link
Collaborator

Builds ready [ec84f4f]
Page Load Metrics (401 ± 380 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint6413488199
domContentLoaded94116105
load512369401791380
domInteractive94116105
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-commenter
Copy link

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.

Files Patch % Lines
...method-middleware/handlers/ethereum-chain-utils.js 57.35% 58 Missing ⚠️
app/scripts/metamask-controller.js 4.44% 43 Missing ⚠️
.../scripts/controllers/permissions/specifications.js 13.16% 33 Missing ⚠️
...c-method-middleware/handlers/add-ethereum-chain.js 61.70% 18 Missing ⚠️
...e-container/permission-page-container.component.js 0.00% 14 Missing ⚠️
...scripts/controllers/permissions/caveat-mutators.js 8.33% 11 Missing ⚠️
...ethod-middleware/handlers/switch-ethereum-chain.js 55.00% 9 Missing ⚠️
ui/helpers/utils/permission.js 0.00% 1 Missing ⚠️
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     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@mcmire mcmire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@adonesky1 adonesky1 merged commit 671c997 into develop May 24, 2024
72 checks passed
@adonesky1 adonesky1 deleted the zb/switch-as-perm-poc-chainid branch May 24, 2024 15:49
@github-actions github-actions bot locked and limited conversation to collaborators May 24, 2024
@metamaskbot metamaskbot added the release-11.18.0 Issue or pull request that will be included in release 11.18.0 label May 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
release-11.18.0 Issue or pull request that will be included in release 11.18.0 team-wallet-api-platform
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants