@@ -1628,8 +1628,9 @@ export default class MetamaskController extends EventEmitter {
16281628 setupControllerEventSubscriptions ( ) {
16291629 let lastSelectedAddress ;
16301630 let lastSelectedSolanaAccountAddress ;
1631+ let lastSelectedTronAccountAddress ;
16311632
1632- // this throws if there is no solana account... perhaps we should handle this better at the controller level
1633+ // this throws if there is no solana or Tron account... perhaps we should handle this better at the controller level
16331634 try {
16341635 lastSelectedSolanaAccountAddress =
16351636 this . accountsController . getSelectedMultichainAccount (
@@ -1638,6 +1639,14 @@ export default class MetamaskController extends EventEmitter {
16381639 } catch {
16391640 // noop
16401641 }
1642+ try {
1643+ lastSelectedTronAccountAddress =
1644+ this . accountsController . getSelectedMultichainAccount (
1645+ MultichainNetworks . TRON ,
1646+ ) ?. address ;
1647+ } catch {
1648+ // noop
1649+ }
16411650
16421651 this . controllerMessenger . subscribe (
16431652 'PreferencesController:stateChange' ,
@@ -1933,6 +1942,192 @@ export default class MetamaskController extends EventEmitter {
19331942 } ,
19341943 ) ;
19351944
1945+ // wallet_notify for tron accountChanged when permission changes
1946+ this . controllerMessenger . subscribe (
1947+ `${ this . permissionController . name } :stateChange` ,
1948+ async ( currentValue , previousValue ) => {
1949+ const origins = uniq ( [ ...previousValue . keys ( ) , ...currentValue . keys ( ) ] ) ;
1950+ origins . forEach ( ( origin ) => {
1951+ const previousCaveatValue = previousValue . get ( origin ) ;
1952+ const currentCaveatValue = currentValue . get ( origin ) ;
1953+
1954+ const previousTronAccountChangedNotificationsEnabled = Boolean (
1955+ previousCaveatValue ?. sessionProperties ?. [
1956+ KnownSessionProperties . TronAccountChangedNotifications
1957+ ] ,
1958+ ) ;
1959+ const currentTronAccountChangedNotificationsEnabled = Boolean (
1960+ currentCaveatValue ?. sessionProperties ?. [
1961+ KnownSessionProperties . TronAccountChangedNotifications
1962+ ] ,
1963+ ) ;
1964+
1965+ if (
1966+ ! previousTronAccountChangedNotificationsEnabled &&
1967+ ! currentTronAccountChangedNotificationsEnabled
1968+ ) {
1969+ return ;
1970+ }
1971+
1972+ const previousTronCaipAccountIds = previousCaveatValue
1973+ ? getPermittedAccountsForScopes ( previousCaveatValue , [
1974+ MultichainNetworks . TRON ,
1975+ MultichainNetworks . TRON_SHASTA ,
1976+ MultichainNetworks . TRON_NILE ,
1977+ ] )
1978+ : [ ] ;
1979+ const previousNonUniqueTronHexAccountAddresses =
1980+ previousTronCaipAccountIds . map ( ( caipAccountId ) => {
1981+ const { address } = parseCaipAccountId ( caipAccountId ) ;
1982+ return address ;
1983+ } ) ;
1984+ const previousTronHexAccountAddresses = uniq (
1985+ previousNonUniqueTronHexAccountAddresses ,
1986+ ) ;
1987+ const [ previousSelectedTronAccountAddress ] =
1988+ this . sortMultichainAccountsByLastSelected (
1989+ previousTronHexAccountAddresses ,
1990+ ) ;
1991+
1992+ const currentTronCaipAccountIds = currentCaveatValue
1993+ ? getPermittedAccountsForScopes ( currentCaveatValue , [
1994+ MultichainNetworks . TRON ,
1995+ MultichainNetworks . TRON_SHASTA ,
1996+ MultichainNetworks . TRON_NILE ,
1997+ ] )
1998+ : [ ] ;
1999+ const currentNonUniqueTronHexAccountAddresses =
2000+ currentTronCaipAccountIds . map ( ( caipAccountId ) => {
2001+ const { address } = parseCaipAccountId ( caipAccountId ) ;
2002+ return address ;
2003+ } ) ;
2004+ const currentTronHexAccountAddresses = uniq (
2005+ currentNonUniqueTronHexAccountAddresses ,
2006+ ) ;
2007+ const [ currentSelectedTronAccountAddress ] =
2008+ this . sortMultichainAccountsByLastSelected (
2009+ currentTronHexAccountAddresses ,
2010+ ) ;
2011+
2012+ if (
2013+ previousSelectedTronAccountAddress !==
2014+ currentSelectedTronAccountAddress
2015+ ) {
2016+ this . _notifyTronAccountChange (
2017+ origin ,
2018+ currentSelectedTronAccountAddress
2019+ ? [ currentSelectedTronAccountAddress ]
2020+ : [ ] ,
2021+ ) ;
2022+ }
2023+ } ) ;
2024+ } ,
2025+ getAuthorizedScopesByOrigin ,
2026+ ) ;
2027+
2028+ // TODO: To be removed when state 2 is fully transitioned.
2029+ // wallet_notify for tron accountChanged when selected account changes
2030+ this . controllerMessenger . subscribe (
2031+ `${ this . accountsController . name } :selectedAccountChange` ,
2032+ async ( account ) => {
2033+ if (
2034+ account . type === TrxAccountType . Eoa &&
2035+ account . address !== lastSelectedTronAccountAddress ) {
2036+ lastSelectedTronAccountAddress = account . address ;
2037+
2038+ const originsWithTronAccountChangedNotifications =
2039+ getOriginsWithSessionProperty (
2040+ this . permissionController . state ,
2041+ KnownSessionProperties . TronAccountChangedNotifications ,
2042+ ) ;
2043+
2044+ // returns a map of origins to permitted tron accounts
2045+ const tronAccounts = getPermittedAccountsForScopesByOrigin (
2046+ this . permissionController . state ,
2047+ [
2048+ MultichainNetworks . TRON ,
2049+ MultichainNetworks . TRON_SHASTA ,
2050+ MultichainNetworks . TRON_NILE ,
2051+ ] ,
2052+ ) ;
2053+
2054+ if ( tronAccounts . size > 0 ) {
2055+ for ( const [ origin , accounts ] of tronAccounts . entries ( ) ) {
2056+ const parsedTronAddresses = accounts . map ( ( caipAccountId ) => {
2057+ const { address } = parseCaipAccountId ( caipAccountId ) ;
2058+ return address ;
2059+ } ) ;
2060+
2061+ if (
2062+ parsedTronAddresses . includes ( account . address ) &&
2063+ originsWithTronAccountChangedNotifications [ origin ]
2064+ ) {
2065+ this . _notifyTronAccountChange ( origin , [ account . address ] ) ;
2066+ }
2067+ }
2068+ }
2069+ }
2070+ } ,
2071+ ) ;
2072+
2073+ // wallet_notify for tron accountChanged when selected account group changes
2074+ this . controllerMessenger . subscribe (
2075+ `${ this . accountTreeController . name } :selectedAccountGroupChange` ,
2076+ ( groupId ) => {
2077+ // TODO: Move this logic to the SnapKeyring directly.
2078+ // Forward selected accounts to the Snap keyring, so each Snaps can fetch those accounts.
2079+ // eslint-disable-next-line no-void
2080+ void this . forwardSelectedAccountGroupToSnapKeyring (
2081+ this . getSnapKeyringIfAvailable ( ) ,
2082+ groupId ,
2083+ ) ;
2084+
2085+ const [ account ] =
2086+ this . accountTreeController . getAccountsFromSelectedAccountGroup ( {
2087+ scopes : [ TrxScope . Mainnet ] ,
2088+ } ) ;
2089+ if (
2090+ account &&
2091+ account . type === TrxAccountType . Eoa &&
2092+ account . address !== lastSelectedTronAccountAddress
2093+ ) {
2094+ lastSelectedTronAccountAddress = account . address ;
2095+
2096+ const originsWithTronAccountChangedNotifications =
2097+ getOriginsWithSessionProperty (
2098+ this . permissionController . state ,
2099+ KnownSessionProperties . TronAccountChangedNotifications ,
2100+ ) ;
2101+
2102+ // returns a map of origins to permitted tron accounts
2103+ const tronAccounts = getPermittedAccountsForScopesByOrigin (
2104+ this . permissionController . state ,
2105+ [
2106+ MultichainNetworks . TRON ,
2107+ MultichainNetworks . TRON_SHASTA ,
2108+ MultichainNetworks . TRON_NILE ,
2109+ ] ,
2110+ ) ;
2111+
2112+ if ( tronAccounts . size > 0 ) {
2113+ for ( const [ origin , accounts ] of tronAccounts . entries ( ) ) {
2114+ const parsedTronAddresses = accounts . map ( ( caipAccountId ) => {
2115+ const { address } = parseCaipAccountId ( caipAccountId ) ;
2116+ return address ;
2117+ } ) ;
2118+
2119+ if (
2120+ parsedTronAddresses . includes ( account . address ) &&
2121+ originsWithTronAccountChangedNotifications [ origin ]
2122+ ) {
2123+ this . _notifyTronAccountChange ( origin , [ account . address ] ) ;
2124+ }
2125+ }
2126+ }
2127+ }
2128+ } ,
2129+ ) ;
2130+
19362131 // TODO: Move this logic to the SnapKeyring directly.
19372132 // Forward selected accounts to the Snap keyring, so each Snaps can fetch those accounts.
19382133 this . controllerMessenger . subscribe (
@@ -8630,6 +8825,23 @@ export default class MetamaskController extends EventEmitter {
86308825 ) ;
86318826 }
86328827
8828+ async _notifyTronAccountChange ( origin , accountAddressArray ) {
8829+ this . notifyConnections (
8830+ origin ,
8831+ {
8832+ method : MultichainApiNotifications . walletNotify ,
8833+ params : {
8834+ scope : MultichainNetworks . TRON ,
8835+ notification : {
8836+ method : NOTIFICATION_NAMES . accountsChanged ,
8837+ params : accountAddressArray ,
8838+ } ,
8839+ } ,
8840+ } ,
8841+ API_TYPE . CAIP_MULTICHAIN ,
8842+ ) ;
8843+ }
8844+
86338845 async _notifyChainChange ( ) {
86348846 this . notifyAllConnections (
86358847 async ( origin ) => ( {
0 commit comments