Skip to content

Commit 4bc9929

Browse files
committed
feat: add support for accountChanged for Tron network
1 parent ff6d9df commit 4bc9929

File tree

3 files changed

+379
-3
lines changed

3 files changed

+379
-3
lines changed

app/scripts/metamask-controller.js

Lines changed: 214 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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,193 @@ 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+
) {
2037+
lastSelectedTronAccountAddress = account.address;
2038+
2039+
const originsWithTronAccountChangedNotifications =
2040+
getOriginsWithSessionProperty(
2041+
this.permissionController.state,
2042+
KnownSessionProperties.TronAccountChangedNotifications,
2043+
);
2044+
2045+
// returns a map of origins to permitted tron accounts
2046+
const tronAccounts = getPermittedAccountsForScopesByOrigin(
2047+
this.permissionController.state,
2048+
[
2049+
MultichainNetworks.TRON,
2050+
MultichainNetworks.TRON_SHASTA,
2051+
MultichainNetworks.TRON_NILE,
2052+
],
2053+
);
2054+
2055+
if (tronAccounts.size > 0) {
2056+
for (const [origin, accounts] of tronAccounts.entries()) {
2057+
const parsedTronAddresses = accounts.map((caipAccountId) => {
2058+
const { address } = parseCaipAccountId(caipAccountId);
2059+
return address;
2060+
});
2061+
2062+
if (
2063+
parsedTronAddresses.includes(account.address) &&
2064+
originsWithTronAccountChangedNotifications[origin]
2065+
) {
2066+
this._notifyTronAccountChange(origin, [account.address]);
2067+
}
2068+
}
2069+
}
2070+
}
2071+
},
2072+
);
2073+
2074+
// wallet_notify for tron accountChanged when selected account group changes
2075+
this.controllerMessenger.subscribe(
2076+
`${this.accountTreeController.name}:selectedAccountGroupChange`,
2077+
(groupId) => {
2078+
// TODO: Move this logic to the SnapKeyring directly.
2079+
// Forward selected accounts to the Snap keyring, so each Snaps can fetch those accounts.
2080+
// eslint-disable-next-line no-void
2081+
void this.forwardSelectedAccountGroupToSnapKeyring(
2082+
this.getSnapKeyringIfAvailable(),
2083+
groupId,
2084+
);
2085+
2086+
const [account] =
2087+
this.accountTreeController.getAccountsFromSelectedAccountGroup({
2088+
scopes: [TrxScope.Mainnet],
2089+
});
2090+
if (
2091+
account &&
2092+
account.type === TrxAccountType.Eoa &&
2093+
account.address !== lastSelectedTronAccountAddress
2094+
) {
2095+
lastSelectedTronAccountAddress = account.address;
2096+
2097+
const originsWithTronAccountChangedNotifications =
2098+
getOriginsWithSessionProperty(
2099+
this.permissionController.state,
2100+
KnownSessionProperties.TronAccountChangedNotifications,
2101+
);
2102+
2103+
// returns a map of origins to permitted tron accounts
2104+
const tronAccounts = getPermittedAccountsForScopesByOrigin(
2105+
this.permissionController.state,
2106+
[
2107+
MultichainNetworks.TRON,
2108+
MultichainNetworks.TRON_SHASTA,
2109+
MultichainNetworks.TRON_NILE,
2110+
],
2111+
);
2112+
2113+
if (tronAccounts.size > 0) {
2114+
for (const [origin, accounts] of tronAccounts.entries()) {
2115+
const parsedTronAddresses = accounts.map((caipAccountId) => {
2116+
const { address } = parseCaipAccountId(caipAccountId);
2117+
return address;
2118+
});
2119+
2120+
if (
2121+
parsedTronAddresses.includes(account.address) &&
2122+
originsWithTronAccountChangedNotifications[origin]
2123+
) {
2124+
this._notifyTronAccountChange(origin, [account.address]);
2125+
}
2126+
}
2127+
}
2128+
}
2129+
},
2130+
);
2131+
19362132
// TODO: Move this logic to the SnapKeyring directly.
19372133
// Forward selected accounts to the Snap keyring, so each Snaps can fetch those accounts.
19382134
this.controllerMessenger.subscribe(
@@ -8630,6 +8826,23 @@ export default class MetamaskController extends EventEmitter {
86308826
);
86318827
}
86328828

8829+
async _notifyTronAccountChange(origin, accountAddressArray) {
8830+
this.notifyConnections(
8831+
origin,
8832+
{
8833+
method: MultichainApiNotifications.walletNotify,
8834+
params: {
8835+
scope: MultichainNetworks.TRON,
8836+
notification: {
8837+
method: NOTIFICATION_NAMES.accountsChanged,
8838+
params: accountAddressArray,
8839+
},
8840+
},
8841+
},
8842+
API_TYPE.CAIP_MULTICHAIN,
8843+
);
8844+
}
8845+
86338846
async _notifyChainChange() {
86348847
this.notifyAllConnections(
86358848
async (origin) => ({

0 commit comments

Comments
 (0)