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 Multichain API to Flask #27782

Open
wants to merge 969 commits into
base: main
Choose a base branch
from
Open

Conversation

jiexi
Copy link
Contributor

@jiexi jiexi commented Oct 10, 2024

Description

This branch adds support for the Multichain API to the Flask build of the Extension.

The existing API (via injected provider) should be completely unchanged.

(Very Briefly) What is the MetaMask Multichain API

  • Concurrent connection to any number of chains (no network switching)
  • Unified entry point for all chain ecosystems (EVM, BTC, Solana, Cosmos, Polkadot etc)
  • Accessible (on extension for chromium based browsers) via externally_connectable. Not accessible via an injected global like window.ethereum

Key Documents/Standards

mip = MetaMask Improvement Proposal

  • MIP-5 (Overview of the Multichain API)
    • CAIP-25 (new connection request API)
    • CAIP-27 (new request API, envelope with target scope/chainId included)
  • MIP-6 (Overview of how the Multichain API’s EVM support diverges from the 1193 injected provider)

Manual testing steps

yarn start:flask

Then

(RECOMMENDED) Use the Multichain Test Dapp

OR

Form requests manually

Open in GitHub Codespaces

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

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.

@jiexi
Copy link
Contributor Author

jiexi commented Oct 14, 2024

@metamaskbot update-policies

@metamaskbot
Copy link
Collaborator

Policies updated.
👀 Please review the diff for suspicious new powers.

🧠 Learn how: https://lavamoat.github.io/guides/policy-diff/#what-to-look-for-when-reviewing-a-policy-diff

Copy link

socket-security bot commented Oct 14, 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/encodeurl@2.0.0 🚫
New author npm/send@0.19.0 🚫
New author npm/express@4.21.2 🚫

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/foo@1.0.0 or ignore all packages with @SocketSecurity ignore-all

  • @SocketSecurity ignore npm/encodeurl@2.0.0
  • @SocketSecurity ignore npm/send@0.19.0
  • @SocketSecurity ignore npm/express@4.21.2

@shanejonas
Copy link
Contributor

this PR needs the patches from here: /~https://github.com/MetaMask/metamask-extension/pull/27847/files#r1801195961

@jiexi jiexi changed the base branch from caip-multichain to caip25-permission-migration October 15, 2024 16:18
@jiexi jiexi changed the title Multichain: migrate to core package feat: CAIP Multichain Oct 15, 2024
@jiexi
Copy link
Contributor Author

jiexi commented Oct 15, 2024

REMINDER: check the original feature branch PR for unresolved comments

@jiexi jiexi changed the title feat: CAIP Multichain feat: CAIP Multichain (New) Oct 15, 2024
@jiexi
Copy link
Contributor Author

jiexi commented Oct 15, 2024

TODO: Convert BARAD_DUR flag into flask feature flag

Done here #29003

@shanejonas shanejonas mentioned this pull request Oct 17, 2024
7 tasks
adonesky1 pushed a commit that referenced this pull request Oct 17, 2024
<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->

## **Description**

<!--
Write a short description of the changes included in this pull request,
also include relevant motivation and context. Have in mind the following
questions:
1. What is the reason for the change?
2. What is the improvement/solution?
-->

[![Open in GitHub
Codespaces](/~https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/27940?quickstart=1)

## **Related issues**

Fixes:
#27782 (comment)

## **Manual testing steps**

1. Go to this page...
2.
3.

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**

<!-- [screenshots/recordings] -->

### **After**

<!-- [screenshots/recordings] -->

## **Pre-merge author checklist**

- [ ] I've followed [MetaMask Contributor
Docs](/~https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](/~https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md).
- [ ] 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](https://jsdoc.app/) format
if applicable
- [ ] I’ve applied the right labels on the PR (see [labeling
guidelines](/~https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)).
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.
@jiexi
Copy link
Contributor Author

jiexi commented Oct 17, 2024

@metamaskbot update-policies

@metamaskbot
Copy link
Collaborator

Policies updated.
👀 Please review the diff for suspicious new powers.

🧠 Learn how: https://lavamoat.github.io/guides/policy-diff/#what-to-look-for-when-reviewing-a-policy-diff

@metamaskbot
Copy link
Collaborator

❌ Multichain API Spec Test Failed. View the report here.

Copy link

socket-security bot commented Oct 17, 2024

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

Package New capabilities Transitives Size Publisher
npm/@metamask/api-specs@0.10.150.10.17 None 0 298 kB danfinlay, gudahtt, kumavis, ...6 more
npm/@metamask/multichain@2.1.02.2.0 None 0 522 kB metamaskbot
npm/@metamask/test-dapp-multichain@0.6.0 None 0 1.69 MB metamaskbot
npm/@open-rpc/test-coverage@2.2.22.2.4 Transitive: environment, eval +5 1.33 MB belfordz
npm/cookie@0.5.00.7.1 None 0 23.3 kB blakeembrey
npm/crc-32@1.2.01.2.2 None 0 31 kB sheetjs
npm/encodeurl@2.0.0 None 0 6.98 kB blakeembrey
npm/express@4.18.34.21.2 None +1 230 kB jonchurch
npm/finalhandler@1.2.01.3.1 None 0 19 kB wesleytodd
npm/merge-descriptors@1.0.11.0.3 None 0 5.08 kB sindresorhus
npm/send@0.18.00.19.0 None 0 50.2 kB ulisesgascon
npm/serve-static@1.15.01.16.2 None 0 25.4 kB wesleytodd

🚮 Removed packages: npm/exit-on-epipe@1.0.1, npm/jsonschema@1.2.2, npm/printj@1.1.2

View full report↗︎

Have feedback? Participate in our User Experience Survey 📊

@metamaskbot
Copy link
Collaborator

❌ Multichain API Spec Test Failed. View the report here.

Gudahtt added a commit that referenced this pull request Feb 24, 2025
The permissions confirmation page currently ignores the
`requestedChainIds` prop when the connection is confirmed. This hasn't
resulted in a bug because this page is only used for snap permissions.
Permission requests for `eth_account` or `endowment:permitted-chains`
are handled by the "ChooseAccount" or "ConnectPage" components (the
former if a snap is also requested alongside, the latter otherwise).

This PR fixes the problem regardless, as it's confusing for the
component to have this prop but to ignore it when processing the
confirmation.

This was extracted from #27782
Gudahtt added a commit that referenced this pull request Feb 24, 2025
The permissions confirmation page currently ignores the
`requestedChainIds` prop when the connection is confirmed. This hasn't
resulted in a bug because this page is only used for snap permissions.
Permission requests for `eth_account` or `endowment:permitted-chains`
are handled by the "ChooseAccount" or "ConnectPage" components (the
former if a snap is also requested alongside, the latter otherwise).

This PR fixes the problem regardless, as it's confusing for the
component to have this prop but to ignore it when processing the
confirmation.

This was extracted from #27782
Gudahtt added a commit that referenced this pull request Feb 24, 2025
The E2E test setup function (`withFixtures`) has been updated to pass
the extension ID to E2E tests. This will be useful in the near future
for testing the new multichain API, which is exposed over
`externally_connectable` and requires the extension ID to use.

This was extracted from #27782
We appear to be adding an account to the `wallet` scope


![Image](/~https://github.com/user-attachments/assets/8e7d47a7-1e0c-4184-b05c-0dff629f6a30)

Resulting in this failing CI for our [Multichain Flask
PR](#27782)


https://app.circleci.com/pipelines/github/MetaMask/metamask-extension/125517/workflows/abdfdb4f-da74-4cc2-9c2f-cf001817f358/jobs/4559282

we should add it to the `wallet:eip155` scope (as currently done), but
not just wallet.

The fix involves refactoring [core](/~https://github.com/MetaMask/core)
`Multichain` package so that creating the `scopeObjects` for each entry,
we make sure that in `wallet` scope string, the accounts property is not
populated.

<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->

## **Description**

<!--
Write a short description of the changes included in this pull request,
also include relevant motivation and context. Have in mind the following
questions:
1. What is the reason for the change?
2. What is the improvement/solution?
-->

[![Open in GitHub
Codespaces](/~https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/30495?quickstart=1)

## **Related issues**

Fixes:

## **Manual testing steps**

1. Go to this page...
2.
3.

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**

<!-- [screenshots/recordings] -->

### **After**

<!-- [screenshots/recordings] -->

## **Pre-merge author checklist**

- [ ] I've followed [MetaMask Contributor
Docs](/~https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](/~https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [ ] 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](https://jsdoc.app/) format
if applicable
- [ ] I’ve applied the right labels on the PR (see [labeling
guidelines](/~https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
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.
@metamaskbot
Copy link
Collaborator

❌ Multichain API Spec Test Failed. View the report here.

github-merge-queue bot pushed a commit that referenced this pull request Feb 25, 2025
## **Description**

The E2E test setup function (`withFixtures`) has been updated to pass
the extension ID to E2E tests. This will be useful in the near future
for testing the new multichain API, which is exposed over
`externally_connectable` and requires the extension ID to use.

[![Open in GitHub
Codespaces](/~https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/30539?quickstart=1)

## **Related issues**

This was extracted from #27782

## **Manual testing steps**

N/A, this isn't used yet so there is nothing to manually test.

## **Screenshots/Recordings**

N/A

## **Pre-merge author checklist**

- [x] I've followed [MetaMask Contributor
Docs](/~https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](/~https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I’ve included tests if applicable
- [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I’ve applied the right labels on the PR (see [labeling
guidelines](/~https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
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.
Gudahtt added a commit that referenced this pull request Feb 25, 2025
The permissions confirmation page currently ignores the
`requestedChainIds` prop when the connection is confirmed. This hasn't
resulted in a bug because this page is only used for snap permissions.
Permission requests for `eth_account` or `endowment:permitted-chains`
are handled by the "ChooseAccount" or "ConnectPage" components (the
former if a snap is also requested alongside, the latter otherwise).

This PR fixes the problem regardless, as it's confusing for the
component to have this prop but to ignore it when processing the
confirmation.

This was extracted from #27782
*
* @returns {Map<string, Caip25Authorization>} The current origin:authorization map.
*/
export const getAuthorizedScopesByOrigin = createDeepEqualSelector(
Copy link
Contributor

Choose a reason for hiding this comment

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

Given the discussion above, are we okay with using createSelector here? @FrederikBolding

Suggested change
export const getAuthorizedScopesByOrigin = createDeepEqualSelector(
export const getAuthorizedScopesByOrigin = createSelector(

/**
* Get the permitted chains for each subject, keyed by origin.
* The values of the returned map are immutable values from the
* PermissionController state.
*
* @returns {Map<string, string[]>} The current origin:chainIds[] map.
*/
export const getPermittedChainsByOrigin = createSelector(
export const getPermittedChainsByOrigin = createDeepEqualSelector(
Copy link
Contributor

Choose a reason for hiding this comment

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

And similar here:

Suggested change
export const getPermittedChainsByOrigin = createDeepEqualSelector(
export const getPermittedChainsByOrigin = createSelector(

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.

Finally got some time to read over wallet_createSession. I really think we should revisit some of the naming in @metamask/multichain, but I tried to offer suggestions here that would help a bit.

Next I will take a closer look at the e2e tests.

console.log('MetaMask CAIP stream', err),
);
pipeline(portStream, caipStream, portStream, (err: Error) => {
caipStream.substream.destroy();
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this related to other changes in this PR? Or is this a bug we found along the way?

Copy link
Contributor

Choose a reason for hiding this comment

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

I suspect this is unrelated. I will try submitting this as a separate PR.

github-merge-queue bot pushed a commit that referenced this pull request Feb 27, 2025
…30443)

## **Description**

The permissions confirmation page currently ignores the
`requestedChainIds` prop when the connection is confirmed. This hasn't
resulted in a bug because this page is only used for snap permissions.
Permission requests for `eth_account` or `endowment:permitted-chains`
are handled by the "ChooseAccount" or "ConnectPage" components (the
former if a snap is also requested alongside, the latter otherwise).

This PR fixes the problem regardless, as it's confusing for the
component to have this prop but to ignore it when processing the
confirmation.

This was extracted from #27782

[![Open in GitHub
Codespaces](/~https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/30443?quickstart=1)

## **Related issues**

See this comment for some additional context:
/~https://github.com/MetaMask/metamask-extension/pull/27782/files#r1936463248

## **Manual testing steps**

N/A, the impacted functionality is unreachable.

## **Screenshots/Recordings**

N/A

## **Pre-merge author checklist**

- [x] I've followed [MetaMask Contributor
Docs](/~https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](/~https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I’ve included tests if applicable
- [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I’ve applied the right labels on the PR (see [labeling
guidelines](/~https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
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.
@mcmire
Copy link
Contributor

mcmire commented Feb 27, 2025

I have tested wallet_createSession, wallet_getSession, and wallet_revokeSession with the Multichain Test Dapp and it seems to still work.

@metamaskbot
Copy link
Collaborator

❌ Multichain API Spec Test Failed. View the report here.

delete transaction.unevaluatedProperties;
}

const chainIdMethod = openrpcDocument.methods.find(
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Can we break up this function? It seems like it could become unmaintainable pretty fast.

const parsedResult = await testDapp.getSession();

const sessionScope = parsedResult.sessionScopes[DEFAULT_SCOPE];
const expectedSessionScope = getExpectedSessionScope(DEFAULT_SCOPE, [
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 not understanding how this test works out of the box when we didn't create a session first. And how is the default scope eip155:1337? Is this test reliant on other tests to run first? It seems that we should at least be calling initCreateSessionScopes first in this test.

Copy link
Contributor

@mcmire mcmire Feb 27, 2025

Choose a reason for hiding this comment

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

Oh, maybe this works because of the caveat set in withPermissionControllerConnectedToMultichainTestDapp?

Copy link
Contributor

@mcmire mcmire Feb 27, 2025

Choose a reason for hiding this comment

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

Nit: It'd be nice if we could split this up into multiple files, to make this look like other e2e tests. I imagine that these tests don't follow the same shape and so we were forced to do it this way? If so, makes sense, it's just a bit difficult to read these tests in this form.

extensionId,
),
reporters: ['console-streaming'],
skip: [
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Some of these methods are already listed in ignoreMethods, can we reuse that variable?

@mcmire
Copy link
Contributor

mcmire commented Feb 27, 2025

@metamaskbot update-policies

@mcmire
Copy link
Contributor

mcmire commented Feb 27, 2025

The multichain e2e test "Generate params from examples and expect results to match wallet_swapAsset example" is currently failing, perhaps due to invalid params or something. I will take a look at this tomorrow.

mcmire and others added 2 commits February 27, 2025 16:13
Co-authored-by: Mark Stacey <markjstacey@gmail.com>
@metamaskbot
Copy link
Collaborator

Policies updated.
👀 Please review the diff for suspicious new powers.

🧠 Learn how: https://lavamoat.github.io/guides/policy-diff/#what-to-look-for-when-reviewing-a-policy-diff

@mcmire
Copy link
Contributor

mcmire commented Feb 28, 2025

If we can get MetaMask/api-specs#289 merged, it should fix the failing e2e spec.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.