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 6 commits into
base: develop
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 removed dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher
npm/@metamask-previews/[email protected] Transitive: environment, network +8 2.12 MB metamaskbot
npm/@types/[email protected] None 0 5.52 kB types

🚮 Removed packages: npm/@metamask/[email protected]

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
New author npm/@metamask/[email protected] 🚫

View full report↗︎

Next steps

What is new author?

A new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.

Scrutinize new collaborator additions to packages because they now have the ability to publish code into your dependency tree. Packages should avoid frequent or unnecessary additions or changes to publishing rights.

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
Copy link

sonarcloud bot commented Aug 15, 2024

@@ -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

@@ -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

@@ -214,7 +216,11 @@ export async function switchChain(
permissionedChainIds === undefined ||
!permissionedChainIds.includes(chainId)
) {
await requestPermittedChainsPermission([chainId]);
if (isSnapId(origin)) {
Copy link
Member

Choose a reason for hiding this comment

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

Can we test this functionality?

Comment on lines +5511 to +5521
requestData: {
approvedChainIds: chainIds,
},
approvedPermissions: {
[PermissionNames.permittedChains]: {
caveats: [
CaveatFactories[CaveatTypes.restrictNetworkSwitching](
chainIds,
),
],
},
Copy link
Member

Choose a reason for hiding this comment

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

Seems a bit strange that we have to specify the chain IDs twice 🤔 Seems like the permission specification strictly uses requestData

export const version = 126;

/**
* This migration adds the `permittedChains` permission to all Snaps that have
Copy link
Member

@FrederikBolding FrederikBolding Aug 29, 2024

Choose a reason for hiding this comment

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

Suggested change
* This migration adds the `permittedChains` permission to all Snaps that have
* This migration adds the `endowment:permittedChains` permission to all Snaps that have

As well as others 😄

permissionKeys.includes(SnapEndowments.EthereumProvider) &&
!permissionKeys.includes(PermissionNames.permittedChains);
if (!needsMigration) {
return accumulator;
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't this delete any subjects that don't need the migration?

expect(newStorage.meta).toStrictEqual({ version });
});

it('adds the network endowment to Snaps with the `endowment:ethereum-provider` permission', async () => {
Copy link
Member

@FrederikBolding FrederikBolding Aug 29, 2024

Choose a reason for hiding this comment

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

We should test that it doesn't touch Snaps that don't have the permission

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.

3 participants