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: issue where provider engine needs to rerun after permissions are granted to ensure correct proxies are assigned #23525

Merged
merged 36 commits into from
Apr 15, 2024

Conversation

adonesky1
Copy link
Contributor

@adonesky1 adonesky1 commented Mar 15, 2024

Description

When the "Selected networks for each site" toggle is enabled, the wallet API should use an origin-specific chain for all interactions. However this doesn't work correctly if the connection status has changed since the last refresh. If you grant permissions to a site or revoke them (i.e. connect to or disconnect from a site), the site does not work as expected until after the site is refreshed.

Expected behavior
The wallet API should always use the dapp-selected chain for sites with permissions, and should use the globally-selected chain for sites without permissions. This should apply even if the permissions have changed since the page loaded.

Solution

This PR introduces changes made to @metamask/selected-network-controller to resolve the issue described above:

Previously the SelectedNetworkController only constructed proxies for domains that had permissions. Other domains have no associated proxy and the getProviderAndBlockTracker method would throw an error. This was problematic because we grab the network client for an origin a single time when constructing an RPC pipeline for that origin in the MetaMask extension. We don't re-create the RPC pipeline when permissions change. That means that the pipeline is setup with the wrong network client and cannot be updated. The following changes ensure seamlessly proxying calls during sessions where a dapp connects/disconnects and provides a path for clients to prune inactive proxies.

This domainProxyMap param now passed into SelectedNetworkController allows the client to handle pruning inactive proxies while the SelectedNetworkController handles adding entries - it can't handle removal, as it doesn't know which connections are active.

The extension passes in an instance of a newly created WeakStringMap class which allows for garbage collection of its data once no longer referenced.

Related issues

Fixes: #23509

Manual testing steps

The problem presented itself in two scenarios: when granting site permissions, and when revoking all site permissions.

When granting permissions:

  • Enable the "Selected networks for each site" toggle
  • Open a site with no permissions in one tab, and the fullscreen wallet UI in another
  • If the site was open already, hard-refresh before proceeding
  • Call eth_requestAccounts on the site, asking for the eth_accounts permissions
  • Approve the request
  • Call eth_chainId from the site, and see that it matches the wallet selected chain
  • Switch the selected chain in the fullscreen UI
  • Call eth_chainId from the site, and see that it is still on the same chain the wallet was connected to before switching

When revoking permissions:

  • Redo all the same steps for When granting permissions:
  • Open the browser-action popup, click on the three-vertical-dots menu, click "Connected sites", then disconnect from the site
  • Call eth_chainId from the site. See that the sites connected chain is now the same as the "globally selected chain" in the wallet

Screenshots/Recordings

Before

This recording shows both reproduction use cases (granting permissions, then revoking)

queue-selected-chain-bug-2024-03-14_19.49.19.webm

After

Screen.Recording.2024-04-08.at.11.33.40.AM.mov

Pre-merge author checklist

  • I’ve followed MetaMask Coding Standards.
  • I've clearly explained what problem this PR is solving and how it is solved.
  • I've linked related issues
  • I've included manual testing steps
  • I've included screenshots/recordings if applicable
  • [x]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.
  • I’ve properly set the pull request status:
    • In case it's not yet "ready for review", I've set it to "draft".
    • In case it's "ready for review", I've changed it from "draft" to "non-draft".

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

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 force-pushed the ad/fix-selected-network-controller-proxies-bug branch from d1d17de to 8c41fcd Compare March 15, 2024 16:27
@adonesky1 adonesky1 force-pushed the ad/fix-selected-network-controller-proxies-bug branch from dd8e36a to 389e639 Compare March 27, 2024 21:11
Copy link

socket-security bot commented Mar 27, 2024

@adonesky1
Copy link
Contributor Author

@metamaskbot update-policies

@metamaskbot
Copy link
Collaborator

Policies updated

Copy link

socket-security bot commented Mar 28, 2024

👍 Dependency issues cleared. Learn more about Socket for GitHub ↗︎

This PR previously contained dependency changes with security issues that have been resolved, removed, or ignored.

View full report↗︎

@adonesky1 adonesky1 force-pushed the ad/fix-selected-network-controller-proxies-bug branch from 0e89e61 to 265e11f Compare March 28, 2024 14:02
@adonesky1
Copy link
Contributor Author

@metamaskbot update-policies

@metamaskbot
Copy link
Collaborator

Policies updated

@adonesky1 adonesky1 force-pushed the ad/fix-selected-network-controller-proxies-bug branch 3 times, most recently from 1d15a0c to e39883f Compare April 4, 2024 19:01
@adonesky1 adonesky1 force-pushed the ad/fix-selected-network-controller-proxies-bug branch from e39883f to c12992a Compare April 8, 2024 17:58
@adonesky1
Copy link
Contributor Author

@metamaskbot update-policies

@metamaskbot
Copy link
Collaborator

No policy changes

@adonesky1 adonesky1 force-pushed the ad/fix-selected-network-controller-proxies-bug branch from c12992a to 90ee937 Compare April 8, 2024 20:15
@adonesky1
Copy link
Contributor Author

@metamaskbot update-policies

@metamaskbot
Copy link
Collaborator

No policy changes

@adonesky1 adonesky1 marked this pull request as ready for review April 8, 2024 21:30
@adonesky1 adonesky1 requested review from a team as code owners April 8, 2024 21:30
@adonesky1 adonesky1 added team-wallet-api-platform area-multichain amon-hen-v1 Represents blocking issues for the release of Amon Hen labels Apr 8, 2024
@metamaskbot
Copy link
Collaborator

Builds ready [a45f7e8]
Page Load Metrics (580 ± 419 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint643281145928
domContentLoaded106024136
load532212580873419
domInteractive106024136
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 12.83 KiB (0.44%)
  • ui: 0 Bytes (0.00%)
  • common: 326 Bytes (0.01%)

@@ -109,15 +109,13 @@ describe('Request Queuing for Multiple Dapps and Txs on different networks.', fu

// Dapp one send tx
await driver.switchToWindowWithUrl(DAPP_URL);
await driver.executeScript(`window.location.reload()`);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

its no longer required to reload the page after connecting to ensure that the proxies are correctly synced

@metamaskbot
Copy link
Collaborator

Builds ready [292a4fe]
Page Load Metrics (726 ± 542 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint653201297134
domContentLoaded11112282311
load5231657261129542
domInteractive10112272311
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 13.49 KiB (0.39%)
  • ui: 0 Bytes (0.00%)
  • common: 326 Bytes (0.01%)

jiexi
jiexi previously approved these changes Apr 12, 2024
jiexi
jiexi previously approved these changes Apr 12, 2024
@metamaskbot
Copy link
Collaborator

Builds ready [2577090]
Page Load Metrics (1281 ± 573 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint693791497436
domContentLoaded1099382512
load55311512811194573
domInteractive1099382512
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 13.49 KiB (0.39%)
  • ui: 0 Bytes (0.00%)
  • common: 326 Bytes (0.01%)

@metamaskbot
Copy link
Collaborator

Builds ready [61586c9]
Page Load Metrics (1318 ± 648 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint813771598541
domContentLoaded14145343014
load59330813181350648
domInteractive14145343014
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 13.49 KiB (0.39%)
  • ui: 0 Bytes (0.00%)
  • common: 326 Bytes (0.01%)

Gudahtt
Gudahtt previously approved these changes Apr 15, 2024
Copy link
Member

@Gudahtt Gudahtt left a comment

Choose a reason for hiding this comment

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

LGTM! One more pending suggestion (re: improving types), but overall this looks great

@metamaskbot
Copy link
Collaborator

Builds ready [9592db0]
Page Load Metrics (1286 ± 641 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint763811397134
domContentLoaded11138352813
load63312512861335641
domInteractive11138352813
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 13.49 KiB (0.39%)
  • ui: 0 Bytes (0.00%)
  • common: 326 Bytes (0.01%)

Co-authored-by: Mark Stacey <markjstacey@gmail.com>
@adonesky1 adonesky1 merged commit b1b8c08 into develop Apr 15, 2024
66 of 68 checks passed
@adonesky1 adonesky1 deleted the ad/fix-selected-network-controller-proxies-bug branch April 15, 2024 20:05
@github-actions github-actions bot locked and limited conversation to collaborators Apr 15, 2024
@metamaskbot
Copy link
Collaborator

Builds ready [77f8100]
Page Load Metrics (954 ± 571 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint7341815010048
domContentLoaded96824168
load6028309541189571
domInteractive96824168
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 13.49 KiB (0.39%)
  • ui: 0 Bytes (0.00%)
  • common: 326 Bytes (0.01%)

@metamaskbot metamaskbot added the release-11.16.0 Issue or pull request that will be included in release 11.16.0 label Apr 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
amon-hen-v1 Represents blocking issues for the release of Amon Hen area-multichain release-11.16.0 Issue or pull request that will be included in release 11.16.0 team-wallet-api-platform
Projects
None yet
6 participants