Skip to content

Conversation

@EdouardBougon
Copy link
Contributor

@EdouardBougon EdouardBougon commented Dec 3, 2025

Description

After Solana, add the support for Tron account Change.

In parallel, a new PR is in progress to factorize this code and make it generic for all non-EVM chains: Solana, Tron, Bitcoin, etc.

Changelog

CHANGELOG entry: Add Tron Account Change detection support for multichain Api (#23639)

Related issues

Fixes:

Manual testing steps

You could try with our Tron test app here: https://metamask.github.io/test-dapp-tron/latest/

Feature: User connection
  Scenario: User connection on Mainnet
    Given the network Shasta Network is selected by default
    When user user select mainnet network, and then select MetaMask Wallet
    Then MetaMask connection screen popup

  Scenario: User connection on Shasta (2 step connection)
    Given the network Shasta Network is selected by default
    When user user select MetaMask Wallet
    Then MetaMask connection screen popup to connect. Once connected an other connection screen to accept to connect to Shasta.

  Scenario: User reload the page
    Given the user is already connected to a specific network and account 
    When user user reload the app
    Then the app should re-connect to Metamask automatically with the right account and network.

Feature: Chain Change
  Scenario: Ask permission for new chain
      Given the network Shasta Network is the only one user accepted 
      When user user switch to mainnet
      Then MetaMask connection screen popup to accept. Once connected the chain mainnet is selected.

  Scenario: Do not ask permission for chain
      Given the network Shasta Network AND mainnet are allowed  
      When user user switch to mainnet
      Then the chain mainnet is selected.

Feature: Account Change
  Scenario: User do not change if select a not allowed account
      Given the user allow only one account
      When user user switch to an other account
      Then nothing happens in the app.

  Scenario: User change if select an allowed account
      Given the user allow only 2 accounts
      When user user switch to the second account
      Then nothing the selected address change in the app

Feature: Disconnect
  Scenario: User disconnects from MM
      Given the user is connected
      When user remove the session from MM
      Then the app disconnects

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.

Note

Adds Tron accountChanged notifications to the Multichain provider, reusing a generalized notification helper, with new event subscriptions, tests, and dependency bumps.

  • Core (app/core/BackgroundBridge/BackgroundBridge.js)
    • Tron accountChanged support:
      • Add Tron-specific handlers: handleTronAccountChangedFromScopeChanges, handleTronAccountChangedFromSelectedAccountChanges, handleTronAccountChangedFromSelectedAccountGroupChanges, and getTronAccountFromSelectedAccountGroup.
      • Track last selected Tron account via lastSelectedTronAccountAddress and emit initial notifyTronAccountChangedForCurrentAccount() on provider setup.
      • Subscribe/unsubscribe to Tron-related Permission, Account, and AccountGroup events.
    • Generalization:
      • Replace _notifySolanaAccountChange with _notifyMultichainAccountChange(scope, value) and use for Solana and Tron.
      • Rename helper to getSolanaAccountFromSelectedAccountGroup() and update Solana flows accordingly.
  • Tests (app/core/BackgroundBridge/BackgroundBridge.test.js)
    • Add comprehensive Tron tests for permissions, selected account changes, and account group changes.
    • Minor Solana test assertion fix and alignment with generalized notifier.
  • Dependencies (package.json, yarn.lock)
    • Bump @metamask/chain-agnostic-permission to ^1.3.0.
    • Bump @metamask/multichain-api-client to ^0.10.1 and @metamask/multichain-api-middleware to 1.2.5.

Written by Cursor Bugbot for commit 3d95aa3. This will update automatically on new commits. Configure here.

@github-actions
Copy link
Contributor

github-actions bot commented Dec 3, 2025

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.

@metamaskbot metamaskbot added the team-wallet-standard Team responsible of the MetaMask Wallet Standard implementation label Dec 3, 2025
@github-actions github-actions bot added the size-L label Dec 3, 2025
@socket-security
Copy link

socket-security bot commented Dec 5, 2025

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Updatednpm/​@​metamask/​multichain-api-middleware@​1.2.4 ⏵ 1.2.5991007695100
Updatednpm/​@​metamask/​chain-agnostic-permission@​1.2.2 ⏵ 1.3.0100 +11007895 +1100
Updatednpm/​@​metamask/​multichain-api-client@​0.7.0 ⏵ 0.10.110010010096 -4100

View full report

@socket-security
Copy link

socket-security bot commented Dec 5, 2025

All alerts resolved. Learn more about Socket for GitHub.

This PR previously contained dependency changes with security issues that have been resolved, removed, or ignored.

Ignoring alerts on:

  • npm/@metamask/multichain-api-middleware@1.2.5

View full report

@EdouardBougon EdouardBougon marked this pull request as ready for review December 5, 2025 17:36
@EdouardBougon EdouardBougon requested a review from a team as a code owner December 5, 2025 17:36
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Bug: Missing unsubscribe calls for Tron event handlers

The onDisconnect method unsubscribes the Solana event handlers but does not unsubscribe the newly added Tron event handlers (handleTronAccountChangedFromScopeChanges, handleTronAccountChangedFromSelectedAccountChanges, handleTronAccountChangedFromSelectedAccountGroupChanges). These handlers are subscribed in setupCaipEventSubscriptions() but never cleaned up, causing a resource leak where the handlers continue responding to events after the bridge is disconnected.

app/core/BackgroundBridge/BackgroundBridge.js#L503-L521

// Enable multichain functionality for all connections except for WalletConnect and MMSDK v1.
if (!(this.isMMSDK && this.sdkVersion === 'v1') && !this.isWalletConnect) {
controllerMessenger.unsubscribe(
`${PermissionController.name}:stateChange`,
this.handleCaipSessionScopeChanges,
);
controllerMessenger.unsubscribe(
`${PermissionController.name}:stateChange`,
this.handleSolanaAccountChangedFromScopeChanges,
);
controllerMessenger.unsubscribe(
`${AccountsController.name}:selectedAccountChange`,
this.handleSolanaAccountChangedFromSelectedAccountChanges,
);
controllerMessenger.unsubscribe(
`${AccountTreeController.name}:selectedAccountGroupChange`,
this.handleSolanaAccountChangedFromSelectedAccountGroupChanges,
);
}

app/core/BackgroundBridge/BackgroundBridge.js#L961-L978

// wallet_notify for tron accountChanged when permission changes
controllerMessenger.subscribe(
`${PermissionController.name}:stateChange`,
this.handleTronAccountChangedFromScopeChanges,
getAuthorizedScopes(this.channelIdOrOrigin),
);
// wallet_notify for tron accountChanged when selected account changes
controllerMessenger.subscribe(
`${AccountsController.name}:selectedAccountChange`,
this.handleTronAccountChangedFromSelectedAccountChanges,
);
controllerMessenger.subscribe(
`${AccountTreeController.name}:selectedAccountGroupChange`,
this.handleTronAccountChangedFromSelectedAccountGroupChanges,
);

Fix in Cursor Fix in Web


@EdouardBougon EdouardBougon marked this pull request as draft December 5, 2025 17:50
@EdouardBougon EdouardBougon marked this pull request as ready for review December 8, 2025 16:47
@EdouardBougon EdouardBougon force-pushed the tron-account-change branch 4 times, most recently from 6c6fb83 to 2911f7a Compare December 9, 2025 20:12
@baptiste-marchand
Copy link
Contributor

@SocketSecurity ignore npm/@metamask/multichain-api-middleware@1.2.5

// have listeners ready for this Solana accountChanged event below.
this.notifySolanaAccountChangedForCurrentAccount();
///: BEGIN:ONLY_INCLUDE_IF(tron)
this.notifyTronAccountChangedForCurrentAccount();
Copy link
Member

Choose a reason for hiding this comment

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

We could include the same comment as above, for tron as well. Or even remove the above comment altogether since the method name is pretty self explanatory. This is not a blocking comment

);

///: BEGIN:ONLY_INCLUDE_IF(tron)
// wallet_notify for tron accountChanged when permission changes
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// wallet_notify for tron accountChanged when permission changes
// wallet_notify for Tron accountChanged when permission changes

getAuthorizedScopes(this.channelIdOrOrigin),
);

// wallet_notify for tron accountChanged when selected account changes
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// wallet_notify for tron accountChanged when selected account changes
// wallet_notify for Tron accountChanged when selected account changes

@wenfix
Copy link
Contributor

wenfix commented Dec 15, 2025

Hey @EdouardBougon, been trying to test this but I'm unable to select mainnet. I only see Tron in the wallet but when I try to connect to Shasta it seems to work. Is there any additional configuration required that I might be missing? Video below, using physical Android device.

unable-to-select-mainnet.mov

@wenfix
Copy link
Contributor

wenfix commented Dec 15, 2025

Can't seem to get the app to register the account change, as the address never changes. Again, not sure if I'm missing something here. Clip below:

accountChanged-tron.mov

adonesky1
adonesky1 previously approved these changes Dec 15, 2025
Copy link
Contributor

@adonesky1 adonesky1 left a comment

Choose a reason for hiding this comment

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

Tested and works, also tested Solana and EVM account changed events still working. Code looks good. We'll want to refactor later but not blocking for now

@github-actions
Copy link
Contributor

🔍 Smart E2E Test Selection

  • Selected E2E tags: SmokeCore, SmokeAccounts, SmokeMultiChainPermissions, SmokeNetworkExpansion, SmokeWalletPlatform, SmokeMultiChainAPI
  • Risk Level: high
  • AI Confidence: 85%
click to see 🤖 AI reasoning details

This PR introduces Tron blockchain support in the BackgroundBridge, which is a critical core component that handles communication between the app and dApps (browser, SDK, WalletConnect). The changes include:

  1. BackgroundBridge.js (Critical - in app/core/): Adds Tron account change notification handlers mirroring existing Solana functionality. This affects how the app communicates account changes to connected dApps across multiple chains.

  2. Package updates: Updates to @metamask/chain-agnostic-permission (1.2.2→1.3.0), @metamask/multichain-api-client (0.7.0→0.10.1), and @metamask/multichain-api-middleware (1.2.4→1.2.5). These packages are used extensively across permission-related components (AccountConnect, PermissionApproval, MultichainAccountConnect, etc.).

The BackgroundBridge is imported by 14 files including BrowserTab, SDKConnect, WalletConnect, and DeeplinkProtocol - all critical paths for dApp interactions.

Selected tags rationale:

  • SmokeCore: BackgroundBridge is core infrastructure for app-dApp communication
  • SmokeAccounts: Changes affect account selection and notification across chains
  • SmokeMultiChainPermissions: Permission handling is directly affected by package updates and new Tron scope handling
  • SmokeNetworkExpansion: Adding Tron support is network expansion functionality
  • SmokeWalletPlatform: Core wallet functionality for account/network switching
  • SmokeMultiChainAPI: Multichain API packages are updated and the changes add new multichain notification capabilities

The Tron code is feature-flagged (ONLY_INCLUDE_IF(tron)), but the package updates and refactoring of _notifySolanaAccountChange to _notifyMultichainAccountChange affect existing functionality.

View GitHub Actions results

@sonarqubecloud
Copy link

@EdouardBougon EdouardBougon added this pull request to the merge queue Dec 16, 2025
Merged via the queue into main with commit d6aa74e Dec 16, 2025
158 of 161 checks passed
@EdouardBougon EdouardBougon deleted the tron-account-change branch December 16, 2025 16:33
@github-actions github-actions bot locked and limited conversation to collaborators Dec 16, 2025
@metamaskbot metamaskbot added the release-7.62.0 Issue or pull request that will be included in release 7.62.0 label Dec 16, 2025
@EdouardBougon EdouardBougon added release-7.61.1 Issue or pull request that will be included in release 7.61.1 and removed release-7.62.0 Issue or pull request that will be included in release 7.62.0 labels Dec 16, 2025
@EdouardBougon EdouardBougon changed the title feat: add support for accountChanged for Tron network feat: add support for accountChanged for Tron network cp-7.61.1 Dec 17, 2025
@EdouardBougon EdouardBougon changed the title feat: add support for accountChanged for Tron network cp-7.61.1 feat: add support for accountChanged for Tron network cp-7.61.2 Dec 19, 2025
@EdouardBougon EdouardBougon removed the release-7.61.1 Issue or pull request that will be included in release 7.61.1 label Dec 19, 2025
@EdouardBougon EdouardBougon changed the title feat: add support for accountChanged for Tron network cp-7.61.2 feat: add support for accountChanged for Tron network Dec 19, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

size-L team-wallet-standard Team responsible of the MetaMask Wallet Standard implementation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants