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

Multichain: Fix Subscriptions #27682

Merged
merged 34 commits into from
Oct 10, 2024

Conversation

jiexi
Copy link
Contributor

@jiexi jiexi commented Oct 7, 2024

Description

On CAIP connection via externally_connectable:

  • subscription and middleware for permitted chains are now instantiated on connection open
    • i.e. if you restart your wallet, you should be able to make eth_subscribe calls for previously permitted dapps without having to call wallet_createSession first
  • subscription and middleware and are cleaned up on connection clos
    • i.e. you should not get any eth_subscriptions after refreshing the page and reconnecting
  • fix CAIP stream pipeline close handler not firing
    • whoops...
  • subscription and middleware are now also key'ed by tabId
    • i.e. subscriptions are isolated to the tabs they are started in

Open in GitHub Codespaces

Related issues

Fixes: https://github.com/MetaMask/MetaMask-planning/issues/3451

Manual testing steps

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)
})

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_subscribe",
                 "params": [
                     "newHeads"
                ],
            }
        }
    }
})

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.

Copy link
Contributor

github-actions bot commented Oct 7, 2024

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 jiexi changed the title Jl/caip multichain/fix eth subscriptions Multichain: Instantiate subscription middleware on caip connection Oct 7, 2024
@jiexi jiexi changed the title Multichain: Instantiate subscription middleware on caip connection Multichain: Seperate subscriptions by tabId. Properly end Oct 8, 2024
@jiexi jiexi changed the title Multichain: Seperate subscriptions by tabId. Properly end Multichain: Fix Subscriptions Oct 8, 2024
@shanejonas shanejonas force-pushed the jl/caip-multichain/fix-eth-subscriptions branch from 00fe7a0 to d2ca213 Compare October 9, 2024 20:59
@jiexi
Copy link
Contributor Author

jiexi commented Oct 9, 2024

TODO make unsubscribe() and removeMiddleware() private

@jiexi jiexi marked this pull request as ready for review October 10, 2024 19:24
@jiexi jiexi requested a review from a team as a code owner October 10, 2024 19:24
@jiexi jiexi merged commit 0f9ba64 into caip-multichain Oct 10, 2024
9 of 16 checks passed
@jiexi jiexi deleted the jl/caip-multichain/fix-eth-subscriptions branch October 10, 2024 19:24
@github-actions github-actions bot locked and limited conversation to collaborators Oct 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants