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: CAIP Multichain (New) #27782

Draft
wants to merge 414 commits into
base: caip25-permission-migration
Choose a base branch
from

Conversation

jiexi
Copy link
Contributor

@jiexi jiexi commented Oct 10, 2024

Description

This branch is the feature branch for CAIP-25, CAIP-27, CAIP stream bifurcation. TODO better PR desc.

Open in GitHub Codespaces

Related issues

Related: MetaMask/core#4813
Upstream: #27847

See: https://github.com/MetaMask/MetaMask-planning/issues/2360
See: MetaMask/test-dapp#324

Manual testing steps

yarn start:flask

Replace EXTENSION_ID with your local extension ID

// Setup
const EXTENSION_ID = 'nonfpcflonapegmnfeafnddgdniflbnk';
const extensionPort = chrome.runtime.connect(EXTENSION_ID)
extensionPort.onMessage.addListener((msg) => {
    // format wallet_notify events nicely so that we can read them more easily later
    if (msg.data.method === 'wallet_notify') {
        console.log('wallet_notify:', {
            scope: msg.data.params.scope,
            method: msg.data.params.notification.method,
            subscription: msg.data.params.notification.params.subscription,
            number: msg.data.params.notification.params.result.number
        })
        return;
    }
    console.log(msg.data)
})


// Initial Connection
extensionPort.postMessage({
    type: 'caip-x',
    data: {
        "jsonrpc": "2.0",
        method: 'wallet_createSession',
        params: {
            requiredScopes: {
            },
            optionalScopes: {
                'eip155:1': {
                    methods: [
                        'eth_blockNumber',
                        'eth_gasPrice',
                        'eth_getTransactionCount'
                    ],
                    notifications: [],
                    accounts: []
                }
            },
        },
    }
})


extensionPort.postMessage({
    type: 'caip-x',
    data: {
        "jsonrpc": "2.0",
        method: 'wallet_invokeMethod',
        params: {
            scope: 'eip155:1',
            request: {
                "method": "eth_blockNumber",
                "params": [],
            }
        }
    }
})

extensionPort.postMessage({
    type: 'caip-x',
    data: {
        "jsonrpc": "2.0",
        method: 'wallet_invokeMethod',
        params: {
            scope: 'eip155:1',
            request: {
                "method": "eth_gasPrice",
                "params": [],
            }
        }
    }
})

extensionPort.postMessage({
    type: 'caip-x',
    data: {
        "jsonrpc": "2.0",
        method: 'wallet_invokeMethod',
        params: {
            scope: 'eip155:1',
            request: {
                "method": "eth_getTransactionCount",
                "params": ["0x5bA08AF1bc30f17272178bDcACA1C74e94955cF4", "latest"],
            }
        }
    }
})

// Not very interesting, let’s do this on Sepolia where I have some tx

extensionPort.postMessage({
    type: 'caip-x',
    data: {
        "jsonrpc": "2.0",
        method: 'wallet_invokeMethod',
        params: {
            scope: 'eip155:11155111',
            request: {
                "method": "eth_getTransactionCount",
                "params": ["0x5bA08AF1bc30f17272178bDcACA1C74e94955cF4", "latest"],
            }
        }
    }
})


// Oh no, an error. Let’s fix that
extensionPort.postMessage({
    type: 'caip-x',
    data: {
        "jsonrpc": "2.0",
        method: 'wallet_createSession',
        params: {
            requiredScopes: {
            },
            optionalScopes: {
                'eip155:1': {
                    methods: [
                        'eth_blockNumber',
                        'eth_gasPrice',
                        'eth_getTransactionCount'
                    ],
                    notifications: [],
                    accounts: []
                },
                'eip155:11155111': {
                    methods: [
                        'eth_blockNumber',
                        'eth_gasPrice',
                        'eth_getTransactionCount'
                    ],
                    notifications: [],
                    accounts: []
                }
            },
        },
    }
})

// And try again
extensionPort.postMessage({
    type: 'caip-x',
    data: {
        "jsonrpc": "2.0",
        method: 'wallet_invokeMethod',
        params: {
            scope: 'eip155:11155111',
            request: {
                "method": "eth_getTransactionCount",
                "params": ["0x5bA08AF1bc30f17272178bDcACA1C74e94955cF4", "latest"],
            }
        }
    }
})

// What if I want even more chains and methods and subscriptions and preselected accounts?
extensionPort.postMessage({
    type: 'caip-x',
    data: {
        "jsonrpc": "2.0",
        method: 'wallet_createSession',
        params: {
            requiredScopes: {
            },
            optionalScopes: {
                'eip155:1': {
                    methods: [
                        'eth_blockNumber',
                        'eth_gasPrice',
                        'eth_getTransactionCount',
                        'eth_sendTransaction', 
                        'eth_subscribe'
                    ],
                    notifications: ['eth_subscription'],
                    accounts: []
                },
                'eip155:11155111': {
                    methods: [
                        'eth_blockNumber',
                        'eth_gasPrice',
                        'eth_getTransactionCount',
                        'eth_sendTransaction', 
                        'eth_subscribe'
                    ],
                    notifications: ['eth_subscription'],
                    accounts: ['eip155:11155111:0x5bA08AF1bc30f17272178bDcACA1C74e94955cF4', 'eip155:11155111:0xEe166a3eec4796DeC6A1D314e7485a52bBe68e4d']
                },
                'eip155:59144': {
                    methods: [
                        'eth_estimateGas',
                    ],
                    notifications: [],
                    accounts: []
                },
            },
        },
    }
})










// What if I want to deny some requested chains and add extras?
// Rerun above, Remove Linea, add Linea Sepolia. Show how the added chains get the full permission set


// My dapp that just loaded / I forgot what my permissions look like / I want to check what my permissions currently look like.
extensionPort.postMessage({
    type: 'caip-x',
    data: {
        "jsonrpc": "2.0",
        method: 'wallet_getSession',
        params:{}
    }
})

// What if the user modifies the permission outside of my dapp? How will I know without checking wallet_getSession frequently?
// Modify permission in page. Check for wallet_sessionChanged event


// What if I want to disconnect from the session entirely?
extensionPort.postMessage({
    type: 'caip-x',
    data: {
        "jsonrpc": "2.0",
        method: 'wallet_revokeSession',
        params:{}
    }
})


// Let’s send some transactions! On sepolia and linea sepolia

extensionPort.postMessage({
    type: 'caip-x',
    data: {
        "jsonrpc": "2.0",
        method: 'wallet_createSession',
        params: {
            requiredScopes: {
            },
            optionalScopes: {
                'eip155:1': {
                    methods: [
                        'eth_sendTransaction', 
                        'eth_subscribe'
                    ],
                    notifications: ['eth_subscription'],
                    accounts: []
                },
                'eip155:11155111': {
                    methods: [
                        'eth_sendTransaction', 
                        'eth_subscribe'
                    ],
                    notifications: ['eth_subscription'],
                    accounts: ['eip155:11155111:0x5bA08AF1bc30f17272178bDcACA1C74e94955cF4', 'eip155:11155111:0xEe166a3eec4796DeC6A1D314e7485a52bBe68e4d']
                },
                'eip155:59141': {
                    methods: [
                        'eth_sendTransaction',
                    ],
                    notifications: [],
                    accounts: []
                },
            },
        },
    }
})


extensionPort.postMessage({
    type: 'caip-x',
    data: {
        "jsonrpc": "2.0",
        method: 'wallet_invokeMethod',
        params: {
            scope: 'eip155:11155111',
            request: {
                "method": "eth_sendTransaction",
                 "params": [
                  {
                    from: "0x5bA08AF1bc30f17272178bDcACA1C74e94955cF4",
                    to: "0x0000000000000000000000000000000000000000",
                    gas: "0x76c0",
                    value: "0x2bdbb64bc09000",
                    data: "0x",
                  }
                ],
            }
        }
    }
})


extensionPort.postMessage({
    type: 'caip-x',
    data: {
        "jsonrpc": "2.0",
        method: 'wallet_invokeMethod',
        params: {
            scope: 'eip155:59141',
            request: {
                "method": "eth_sendTransaction",
                 "params": [
                  {
                    from: "0x5bA08AF1bc30f17272178bDcACA1C74e94955cF4",
                    to: "0x0000000000000000000000000000000000000000",
                    gas: "0x76c0",
                    value: "0x2bdbb64bc09000",
                    data: "0x",
                  }
                ],
            }
        }
    }
})

// You can even be receive eth subscriptions events on multiple chains at the same time, previously impossible.
extensionPort.postMessage({
    type: 'caip-x',
    data: {
        "jsonrpc": "2.0",
        method: 'wallet_invokeMethod',
        params: {
            scope: 'eip155:1',
            request: {
                "method": "eth_subscribe",
                 "params": [
                     "newHeads"
                ],
            }
        }
    }
})



extensionPort.postMessage({
    type: 'caip-x',
    data: {
        "jsonrpc": "2.0",
        method: 'wallet_invokeMethod',
        params: {
            scope: 'eip155:11155111',
            request: {
                "method": "eth_subscribe",
                 "params": [
                     "newHeads"
                ],
            }
        }
    }
})

Locked Wallet Behavior

  • wallet_getSession returns full session with all accounts
  • wallet_revokeSession works as usual
  • wallet_createSession prompts for unlock
  • wallet_invokeMethod on signature methods fails with method or accounts not authorized
  • wallet_invokeMethod on non-signature methods work as usual
  • wallet_sessionChanged not being fired when locked and revokeSession is called (TODO need to fix this). Is being fired when unlocked and revokeSession is called.

Wallet Onboarding Behavior (fresh install, not setup yet)

  • wallet_createSession causes wallet setup screen to be shown instead
  • all other methods work as expected
    • wallet_getSession returns no session since there is none
    • wallet_invokeMethod fails for everything since there is no active session

Screenshots/Recordings

Before

After

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.

jiexi and others added 30 commits August 2, 2024 07:47
<!--
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**

* Add `isMultichainOrigin` flag to the CAIP-25 permission
* Unsure if this should have been added as a separate caveat or not
though
* Consider CAIP-25 permission inapplicable in the multichain flow if
`isMultichainOrigin` is false for the existing authorization
* Allow all previously implicit permissions in the EIP-1193 flow when
there is a no CAIP-25 permission, or the CAIP-25 permission has
`isMultichainOrigin` as false
* Enforce the CAIP-25 permission in the EIP-1193 flow when the CAIP-25
permission has `isMultichainOrigin` true
* Set `isMultichainOrigin` true when CAIP-25 permission is granted as
part of the multichain flow via `provider_authorize`
* Set `isMultichainOrigin` false when a CAIP-25 permission is granted
(not updated) as part of `eth_requestAccounts` or
`wallet_requestPermissions` in the EIP-1193 flow

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

## **Related issues**

See: MetaMask/MetaMask-planning#2922
See: MetaMask/MetaMask-planning#2862

## **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.

---------

Co-authored-by: MetaMask Bot <[email protected]>
<!--
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**
Added multichain api notifications
<!--
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/25869?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/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.

---------

Co-authored-by: Jiexi Luan <[email protected]>
Co-authored-by: Alex <[email protected]>
Co-authored-by: MetaMask Bot <[email protected]>
# Conflicts:
#	lavamoat/browserify/beta/policy.json
#	lavamoat/browserify/flask/policy.json
#	lavamoat/browserify/main/policy.json
#	lavamoat/browserify/mmi/policy.json
This reverts commit bbbf8e5.
@metamaskbot
Copy link
Collaborator

Builds ready [038e490]
Page Load Metrics (1753 ± 101 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint34923171684373179
domContentLoaded15162260172520397
load152423211753210101
domInteractive22158383215
backgroundConnect874292211
firstReactRender158723168
getState702901294723
initialActions01000
loadScripts11331791132119091
setupStore612821
uiStartup175225572041219105
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 30.81 KiB (0.59%)
  • ui: 590 Bytes (0.01%)
  • common: 459.06 KiB (5.70%)

@metamaskbot
Copy link
Collaborator

Builds ready [cd4011e]
Page Load Metrics (2011 ± 67 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint25924341927409196
domContentLoaded17512413196914771
load17702437201114067
domInteractive255736105
backgroundConnect1198422813
firstReactRender1696362713
getState1113141666933
initialActions01000
loadScripts13581906152312158
setupStore6181031
uiStartup205929712410249119
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 30.81 KiB (0.59%)
  • ui: 590 Bytes (0.01%)
  • common: 459.2 KiB (5.70%)

@metamaskbot
Copy link
Collaborator

Builds ready [db816ce]
Page Load Metrics (1707 ± 61 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint36921811645324156
domContentLoaded14782082167912861
load14962095170712661
domInteractive246032115
backgroundConnect880352110
firstReactRender149021168
getState733021334220
initialActions01000
loadScripts11131637128311153
setupStore623842
uiStartup17132397198217484
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 30.81 KiB (0.59%)
  • ui: 590 Bytes (0.01%)
  • common: 459.2 KiB (5.70%)

@metamaskbot
Copy link
Collaborator

Builds ready [bf32e15]
Page Load Metrics (1590 ± 74 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint12741800159814670
domContentLoaded12651786155615775
load12721802159015374
domInteractive21147372813
backgroundConnect1082362512
firstReactRender1699552914
getState490172612
initialActions01000
loadScripts8881373112114368
setupStore690142010
uiStartup14422070185615474
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 30.81 KiB (0.59%)
  • ui: 590 Bytes (0.01%)
  • common: 459.2 KiB (5.70%)

ffmcgee725 and others added 3 commits December 13, 2024 10:35
Work for integrating
[CAIP294](https://github.com/ffmcgee725/caip294-diagram/blob/main/instructions.md)

feat: programatically send extensionId in metamask_getProviderState

For testing this, we make a call json rpc call to
`metamask_getProviderState` and check if response data contains the
`extensionId`

<!--
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/29111?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.
<!--
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**

Remove `BARAD_DUR` env var and move into Flask code fence

[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/29003?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.
@adonesky1 adonesky1 force-pushed the jl/caip-multichain-migrate-core branch from 96f372c to d522c25 Compare December 13, 2024 15:32
@metamaskbot
Copy link
Collaborator

Builds ready [d522c25]
Page Load Metrics (1535 ± 53 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint13251793154412258
domContentLoaded13181731151611555
load13291737153511053
domInteractive24213705928
backgroundConnect97026199
firstReactRender1691402613
getState45113157
initialActions01000
loadScripts9031337111210952
setupStore66418199
uiStartup14732385179720297
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 13.39 KiB (0.24%)
  • ui: 590 Bytes (0.01%)
  • common: 459.07 KiB (5.73%)

@adonesky1 adonesky1 force-pushed the jl/caip-multichain-migrate-core branch from d522c25 to 82aab64 Compare December 13, 2024 20:58
@metamaskbot
Copy link
Collaborator

Builds ready [886ec3c]
Page Load Metrics (1521 ± 115 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint130624661517241116
domContentLoaded129724581497240115
load131524671521239115
domInteractive237934168
backgroundConnect106725199
firstReactRender1479412613
getState48510
initialActions01000
loadScripts9101601106515373
setupStore669182110
uiStartup148626891711273131
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 13.39 KiB (0.25%)
  • ui: 590 Bytes (0.01%)
  • common: 459.08 KiB (5.70%)

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.

5 participants