@@ -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,196 @@ 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+ console . log ( 'TRON !!!!!!!! Permission state changed:' , { currentValue, previousValue } ) ;
1950+ const origins = uniq ( [ ...previousValue . keys ( ) , ...currentValue . keys ( ) ] ) ;
1951+ origins . forEach ( ( origin ) => {
1952+ const previousCaveatValue = previousValue . get ( origin ) ;
1953+ const currentCaveatValue = currentValue . get ( origin ) ;
1954+
1955+ const previousTronAccountChangedNotificationsEnabled = Boolean (
1956+ previousCaveatValue ?. sessionProperties ?. [
1957+ KnownSessionProperties . TronAccountChangedNotifications
1958+ ] ,
1959+ ) ;
1960+ const currentTronAccountChangedNotificationsEnabled = Boolean (
1961+ currentCaveatValue ?. sessionProperties ?. [
1962+ KnownSessionProperties . TronAccountChangedNotifications
1963+ ] ,
1964+ ) ;
1965+
1966+ if (
1967+ ! previousTronAccountChangedNotificationsEnabled &&
1968+ ! currentTronAccountChangedNotificationsEnabled
1969+ ) {
1970+ return ;
1971+ }
1972+
1973+ const previousTronCaipAccountIds = previousCaveatValue
1974+ ? getPermittedAccountsForScopes ( previousCaveatValue , [
1975+ MultichainNetworks . TRON ,
1976+ MultichainNetworks . TRON_SHASTA ,
1977+ MultichainNetworks . TRON_NILE ,
1978+ ] )
1979+ : [ ] ;
1980+ const previousNonUniqueTronHexAccountAddresses =
1981+ previousTronCaipAccountIds . map ( ( caipAccountId ) => {
1982+ const { address } = parseCaipAccountId ( caipAccountId ) ;
1983+ return address ;
1984+ } ) ;
1985+ const previousTronHexAccountAddresses = uniq (
1986+ previousNonUniqueTronHexAccountAddresses ,
1987+ ) ;
1988+ const [ previousSelectedTronAccountAddress ] =
1989+ this . sortMultichainAccountsByLastSelected (
1990+ previousTronHexAccountAddresses ,
1991+ ) ;
1992+
1993+ const currentTronCaipAccountIds = currentCaveatValue
1994+ ? getPermittedAccountsForScopes ( currentCaveatValue , [
1995+ MultichainNetworks . TRON ,
1996+ MultichainNetworks . TRON_SHASTA ,
1997+ MultichainNetworks . TRON_NILE ,
1998+ ] )
1999+ : [ ] ;
2000+ const currentNonUniqueTronHexAccountAddresses =
2001+ currentTronCaipAccountIds . map ( ( caipAccountId ) => {
2002+ const { address } = parseCaipAccountId ( caipAccountId ) ;
2003+ return address ;
2004+ } ) ;
2005+ const currentTronHexAccountAddresses = uniq (
2006+ currentNonUniqueTronHexAccountAddresses ,
2007+ ) ;
2008+ const [ currentSelectedTronAccountAddress ] =
2009+ this . sortMultichainAccountsByLastSelected (
2010+ currentTronHexAccountAddresses ,
2011+ ) ;
2012+
2013+ if (
2014+ previousSelectedTronAccountAddress !==
2015+ currentSelectedTronAccountAddress
2016+ ) {
2017+ this . _notifyTronAccountChange (
2018+ origin ,
2019+ currentSelectedTronAccountAddress
2020+ ? [ currentSelectedTronAccountAddress ]
2021+ : [ ] ,
2022+ ) ;
2023+ }
2024+ } ) ;
2025+ } ,
2026+ getAuthorizedScopesByOrigin ,
2027+ ) ;
2028+
2029+ // TODO: To be removed when state 2 is fully transitioned.
2030+ // wallet_notify for tron accountChanged when selected account changes
2031+ this . controllerMessenger . subscribe (
2032+ `${ this . accountsController . name } :selectedAccountChange` ,
2033+ async ( account ) => {
2034+ console . log ( 'TRON !!!!!!!! Selected account changed:' , account ) ;
2035+ if (
2036+ account . type === TrxAccountType . Eoa &&
2037+ account . address !== lastSelectedTronAccountAddress ) {
2038+ lastSelectedTronAccountAddress = account . address ;
2039+
2040+ const originsWithTronAccountChangedNotifications =
2041+ getOriginsWithSessionProperty (
2042+ this . permissionController . state ,
2043+ KnownSessionProperties . TronAccountChangedNotifications ,
2044+ ) ;
2045+
2046+ // returns a map of origins to permitted tron accounts
2047+ const tronAccounts = getPermittedAccountsForScopesByOrigin (
2048+ this . permissionController . state ,
2049+ [
2050+ MultichainNetworks . TRON ,
2051+ MultichainNetworks . TRON_SHASTA ,
2052+ MultichainNetworks . TRON_NILE ,
2053+ ] ,
2054+ ) ;
2055+
2056+ if ( tronAccounts . size > 0 ) {
2057+ for ( const [ origin , accounts ] of tronAccounts . entries ( ) ) {
2058+ const parsedTronAddresses = accounts . map ( ( caipAccountId ) => {
2059+ const { address } = parseCaipAccountId ( caipAccountId ) ;
2060+ return address ;
2061+ } ) ;
2062+
2063+ if (
2064+ parsedTronAddresses . includes ( account . address ) &&
2065+ originsWithTronAccountChangedNotifications [ origin ]
2066+ ) {
2067+ this . _notifyTronAccountChange ( origin , [ account . address ] ) ;
2068+ }
2069+ }
2070+ }
2071+ }
2072+ } ,
2073+ ) ;
2074+
2075+ // wallet_notify for tron accountChanged when selected account group changes
2076+ this . controllerMessenger . subscribe (
2077+ `${ this . accountTreeController . name } :selectedAccountGroupChange` ,
2078+ ( groupId ) => {
2079+ console . log ( 'TRON !!!!!!!! Selected account group changed:' , groupId ) ;
2080+ // TODO: Move this logic to the SnapKeyring directly.
2081+ // Forward selected accounts to the Snap keyring, so each Snaps can fetch those accounts.
2082+ // eslint-disable-next-line no-void
2083+ void this . forwardSelectedAccountGroupToSnapKeyring (
2084+ this . getSnapKeyringIfAvailable ( ) ,
2085+ groupId ,
2086+ ) ;
2087+
2088+ const [ account ] =
2089+ this . accountTreeController . getAccountsFromSelectedAccountGroup ( {
2090+ scopes : [ TrxScope . Mainnet ] ,
2091+ } ) ;
2092+ console . log ( 'TRON !!!!!!!! Selected account group changed:' , groupId , account ) ;
2093+ if (
2094+ account &&
2095+ account . type === TrxAccountType . Eoa &&
2096+ account . address !== lastSelectedTronAccountAddress
2097+ ) {
2098+ lastSelectedTronAccountAddress = account . address ;
2099+
2100+ const originsWithTronAccountChangedNotifications =
2101+ getOriginsWithSessionProperty (
2102+ this . permissionController . state ,
2103+ KnownSessionProperties . TronAccountChangedNotifications ,
2104+ ) ;
2105+
2106+ // returns a map of origins to permitted tron accounts
2107+ const tronAccounts = getPermittedAccountsForScopesByOrigin (
2108+ this . permissionController . state ,
2109+ [
2110+ MultichainNetworks . TRON ,
2111+ MultichainNetworks . TRON_SHASTA ,
2112+ MultichainNetworks . TRON_NILE ,
2113+ ] ,
2114+ ) ;
2115+
2116+ if ( tronAccounts . size > 0 ) {
2117+ for ( const [ origin , accounts ] of tronAccounts . entries ( ) ) {
2118+ const parsedTronAddresses = accounts . map ( ( caipAccountId ) => {
2119+ const { address } = parseCaipAccountId ( caipAccountId ) ;
2120+ return address ;
2121+ } ) ;
2122+
2123+ if (
2124+ parsedTronAddresses . includes ( account . address ) &&
2125+ originsWithTronAccountChangedNotifications [ origin ]
2126+ ) {
2127+ this . _notifyTronAccountChange ( origin , [ account . address ] ) ;
2128+ }
2129+ }
2130+ }
2131+ }
2132+ } ,
2133+ ) ;
2134+
19362135 // TODO: Move this logic to the SnapKeyring directly.
19372136 // Forward selected accounts to the Snap keyring, so each Snaps can fetch those accounts.
19382137 this . controllerMessenger . subscribe (
@@ -8630,6 +8829,24 @@ export default class MetamaskController extends EventEmitter {
86308829 ) ;
86318830 }
86328831
8832+ async _notifyTronAccountChange ( origin , accountAddressArray ) {
8833+ console . log ( 'TRON !!!!!!!! _notifyTronAccountChange' , origin , accountAddressArray ) ;
8834+ this . notifyConnections (
8835+ origin ,
8836+ {
8837+ method : MultichainApiNotifications . walletNotify ,
8838+ params : {
8839+ scope : MultichainNetworks . TRON ,
8840+ notification : {
8841+ method : NOTIFICATION_NAMES . accountsChanged ,
8842+ params : accountAddressArray ,
8843+ } ,
8844+ } ,
8845+ } ,
8846+ API_TYPE . CAIP_MULTICHAIN ,
8847+ ) ;
8848+ }
8849+
86338850 async _notifyChainChange ( ) {
86348851 this . notifyAllConnections (
86358852 async ( origin ) => ( {
0 commit comments