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(snaps): Add support for custom network per Snap #26389

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

Mrtenz
Copy link
Member

@Mrtenz Mrtenz commented Aug 13, 2024

Description

Like with websites, we want to support a custom network per Snap rather than relying on the globally selected network. For Snaps, the permission for the network to switch to is automatically granted.

Blocked by:

Todo

  • Revert execution environment URL.
  • Remove preview version for selected-network-controller.

Open in GitHub Codespaces

Manual testing steps

  1. Build the extension with CHAIN_PERMISSIONS=true.
  2. Use wallet_switchEthereumChain in a Snap.
  3. The Snap should now use the new network, without any permission requests.

Screenshots/Recordings

Before

After

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 metamaskbot added the INVALID-PR-TEMPLATE PR's body doesn't match template label Aug 13, 2024
Copy link

socket-security bot commented Aug 13, 2024

New and updated dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher
npm/@babel/[email protected] 🔁 npm/@babel/[email protected] None 0 10.7 kB nicolo-ribaudo
npm/@metamask/[email protected] 🔁 npm/@metamask/[email protected] None 0 0 B
npm/@types/[email protected] None 0 1.63 kB types

View full report↗︎

Copy link

socket-security bot commented Aug 13, 2024

🚨 Potential security issues detected. Learn more about Socket for GitHub ↗︎

To accept the risk, merge this PR and you will not be notified again.

Alert Package NoteSourceCI
Deprecated npm/@types/[email protected]
  • Reason: This is a stub types definition. nanoid provides its own type definitions, so you do not need this installed.
⚠︎

View full report↗︎

Next steps

What is a deprecated package?

The maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.

Research the state of the package and determine if there are non-deprecated versions that can be used, or if it should be replaced with a new, supported solution.

Take a deeper look at the dependency

Take a moment to review the security alert above. Review the linked package source code to understand the potential risk. Ensure the package is not malicious before proceeding. If you're unsure how to proceed, reach out to your security team or ask the Socket team for help at support [AT] socket [DOT] dev.

Remove the package

If you happen to install a dependency that Socket reports as Known Malware you should immediately remove it and select a different dependency. For other alert types, you may may wish to investigate alternative packages or consider if there are other ways to mitigate the specific risk posed by the dependency.

Mark a package as acceptable risk

To ignore an alert, reply with a comment starting with @SocketSecurity ignore followed by a space separated list of ecosystem/package-name@version specifiers. e.g. @SocketSecurity ignore npm/[email protected] or ignore all packages with @SocketSecurity ignore-all

@metamaskbot metamaskbot added the team-snaps-platform Snaps Platform team label Aug 13, 2024
@Mrtenz Mrtenz changed the title Add support for custom network per Snap feat(snaps): Add support for custom network per Snap Aug 14, 2024
@Mrtenz Mrtenz force-pushed the mrtenz/snap-networks branch from 38edd7a to 7f2494b Compare August 15, 2024 08:29
Copy link

@@ -26,7 +26,7 @@ buildTypes:
- SEGMENT_WRITE_KEY_REF: SEGMENT_PROD_WRITE_KEY
- ALLOW_LOCAL_SNAPS: false
- REQUIRE_SNAPS_ALLOWLIST: true
- IFRAME_EXECUTION_ENVIRONMENT_URL: https://execution.metamask.io/iframe/6.5.0/index.html
- IFRAME_EXECUTION_ENVIRONMENT_URL: http://localhost:6363
Copy link
Member

Choose a reason for hiding this comment

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

Should be reverted before merge

package.json Outdated
@@ -350,7 +350,7 @@
"@metamask/rpc-errors": "^6.2.1",
"@metamask/safe-event-emitter": "^3.1.1",
"@metamask/scure-bip39": "^2.0.3",
"@metamask/selected-network-controller": "^15.0.2",
"@metamask/selected-network-controller": "npm:@metamask-previews/selected-network-controller@17.0.0-preview-55328a6",
Copy link
Member

Choose a reason for hiding this comment

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

Should be reverted before merge

Copy link
Contributor

This PR has been automatically marked as stale because it has not had recent activity in the last 60 days. It will be closed in 14 days. Thank you for your contributions.

@github-actions github-actions bot added the stale issues and PRs marked as stale label Oct 28, 2024
Copy link
Contributor

This PR was closed because there has been no follow up activity in the last 14 days. Thank you for your contributions.

@github-actions github-actions bot closed this Nov 11, 2024
@Mrtenz Mrtenz reopened this Jan 15, 2025
@Mrtenz Mrtenz removed the stale issues and PRs marked as stale label Jan 15, 2025
@Mrtenz Mrtenz force-pushed the mrtenz/snap-networks branch from 6453f80 to cf763b1 Compare January 17, 2025 12:42
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.

@Mrtenz Mrtenz changed the base branch from develop to main January 17, 2025 12:42
@FrederikBolding
Copy link
Member

@adonesky1 or @jiexi should review this PR too

@FrederikBolding FrederikBolding requested a review from jiexi January 17, 2025 13:32
@Mrtenz Mrtenz force-pushed the mrtenz/snap-networks branch from 0110271 to b74b5a8 Compare January 21, 2025 13:33
@metamaskbot
Copy link
Collaborator

Builds ready [b74b5a8]
Page Load Metrics (1927 ± 202 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint30630261663678326
domContentLoaded150529691899411197
load152230321927421202
domInteractive25188513617
backgroundConnect781272512
firstReactRender1575462311
getState55214147
initialActions01000
loadScripts107819871395291140
setupStore65114136
uiStartup169832082208492236
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 54 Bytes (0.00%)
  • ui: 0 Bytes (0.00%)
  • common: 0 Bytes (0.00%)

SelectedNetworkController: selectedNetworkControllerState,
} = state;
if (!networkControllerState || !isObject(networkControllerState)) {
log.warn(
Copy link
Contributor

Choose a reason for hiding this comment

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

🤔 where do these logs go? I'm more familiar with the pattern of sending these logs to sentry via:
global.sentry?.captureException(...). Like here for example.

Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure. I copied this from another migration, but I see now that the Sentry pattern is used in others. I'll swap it out for that.

>((accumulator, [key, subject]) => {
const permissionKeys = Object.keys(subject.permissions);
const needsMigration =
permissionKeys.includes(SnapEndowments.EthereumProvider) &&
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm assuming this permission can only be granted to Snaps? Otherwise we could be overwriting permissions for non-snaps subjects?

Copy link
Member Author

@Mrtenz Mrtenz Jan 22, 2025

Choose a reason for hiding this comment

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

@@ -201,13 +204,13 @@ export async function switchChain(
if (!ethChainIds.includes(chainId)) {
await requestPermittedChainsPermissionIncrementalForOrigin({
chainId,
autoApprove: isAddFlow,
autoApprove: isAddFlow || isSnapId(origin),
Copy link
Contributor

@jiexi jiexi Jan 21, 2025

Choose a reason for hiding this comment

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

i have a PR here that replaces the isAddFlow param with one named autoApprove. Perhaps this isSnapId check can happen outside the switchChain helper that way we don't need to pass origin in?

Copy link
Member Author

Choose a reason for hiding this comment

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

Makes sense. I didn't want to use isAddFlow since it's not an add flow, but autoApprove sounds good.

Copy link
Contributor

Choose a reason for hiding this comment

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

i managed to not link the PR lol. Here it is

#29837

@Mrtenz Mrtenz force-pushed the mrtenz/snap-networks branch from 5b07c03 to 49ac07d Compare January 22, 2025 11:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
INVALID-PR-TEMPLATE PR's body doesn't match template team-snaps-platform Snaps Platform team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants