@metamask/assets-controllers 109.4.0 → 110.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +54 -1
- package/LICENSE +1 -0
- package/dist/AssetsContractController.cjs +1 -0
- package/dist/AssetsContractController.cjs.map +1 -1
- package/dist/AssetsContractController.d.cts +1 -0
- package/dist/AssetsContractController.d.cts.map +1 -1
- package/dist/AssetsContractController.d.mts +1 -0
- package/dist/AssetsContractController.d.mts.map +1 -1
- package/dist/AssetsContractController.mjs +1 -0
- package/dist/AssetsContractController.mjs.map +1 -1
- package/dist/DeFiPositionsController/DeFiPositionsControllerV2-method-action-types.cjs +7 -0
- package/dist/DeFiPositionsController/DeFiPositionsControllerV2-method-action-types.cjs.map +1 -0
- package/dist/DeFiPositionsController/DeFiPositionsControllerV2-method-action-types.d.cts +28 -0
- package/dist/DeFiPositionsController/DeFiPositionsControllerV2-method-action-types.d.cts.map +1 -0
- package/dist/DeFiPositionsController/DeFiPositionsControllerV2-method-action-types.d.mts +28 -0
- package/dist/DeFiPositionsController/DeFiPositionsControllerV2-method-action-types.d.mts.map +1 -0
- package/dist/DeFiPositionsController/DeFiPositionsControllerV2-method-action-types.mjs +6 -0
- package/dist/DeFiPositionsController/DeFiPositionsControllerV2-method-action-types.mjs.map +1 -0
- package/dist/DeFiPositionsController/DeFiPositionsControllerV2.cjs +128 -0
- package/dist/DeFiPositionsController/DeFiPositionsControllerV2.cjs.map +1 -0
- package/dist/DeFiPositionsController/DeFiPositionsControllerV2.d.cts +89 -0
- package/dist/DeFiPositionsController/DeFiPositionsControllerV2.d.cts.map +1 -0
- package/dist/DeFiPositionsController/DeFiPositionsControllerV2.d.mts +89 -0
- package/dist/DeFiPositionsController/DeFiPositionsControllerV2.d.mts.map +1 -0
- package/dist/DeFiPositionsController/DeFiPositionsControllerV2.mjs +123 -0
- package/dist/DeFiPositionsController/DeFiPositionsControllerV2.mjs.map +1 -0
- package/dist/DeFiPositionsController/build-defi-balances-query.cjs +73 -0
- package/dist/DeFiPositionsController/build-defi-balances-query.cjs.map +1 -0
- package/dist/DeFiPositionsController/build-defi-balances-query.d.cts +34 -0
- package/dist/DeFiPositionsController/build-defi-balances-query.d.cts.map +1 -0
- package/dist/DeFiPositionsController/build-defi-balances-query.d.mts +34 -0
- package/dist/DeFiPositionsController/build-defi-balances-query.d.mts.map +1 -0
- package/dist/DeFiPositionsController/build-defi-balances-query.mjs +69 -0
- package/dist/DeFiPositionsController/build-defi-balances-query.mjs.map +1 -0
- package/dist/DeFiPositionsController/group-defi-positions-v6.cjs +235 -0
- package/dist/DeFiPositionsController/group-defi-positions-v6.cjs.map +1 -0
- package/dist/DeFiPositionsController/group-defi-positions-v6.d.cts +117 -0
- package/dist/DeFiPositionsController/group-defi-positions-v6.d.cts.map +1 -0
- package/dist/DeFiPositionsController/group-defi-positions-v6.d.mts +117 -0
- package/dist/DeFiPositionsController/group-defi-positions-v6.d.mts.map +1 -0
- package/dist/DeFiPositionsController/group-defi-positions-v6.mjs +231 -0
- package/dist/DeFiPositionsController/group-defi-positions-v6.mjs.map +1 -0
- package/dist/DeFiPositionsController/merge-positions-for-accounts.cjs +71 -0
- package/dist/DeFiPositionsController/merge-positions-for-accounts.cjs.map +1 -0
- package/dist/DeFiPositionsController/merge-positions-for-accounts.d.cts +16 -0
- package/dist/DeFiPositionsController/merge-positions-for-accounts.d.cts.map +1 -0
- package/dist/DeFiPositionsController/merge-positions-for-accounts.d.mts +16 -0
- package/dist/DeFiPositionsController/merge-positions-for-accounts.d.mts.map +1 -0
- package/dist/DeFiPositionsController/merge-positions-for-accounts.mjs +67 -0
- package/dist/DeFiPositionsController/merge-positions-for-accounts.mjs.map +1 -0
- package/dist/MultichainAssetsController/MultichainAssetsController.cjs +52 -29
- package/dist/MultichainAssetsController/MultichainAssetsController.cjs.map +1 -1
- package/dist/MultichainAssetsController/MultichainAssetsController.d.cts +3 -17
- package/dist/MultichainAssetsController/MultichainAssetsController.d.cts.map +1 -1
- package/dist/MultichainAssetsController/MultichainAssetsController.d.mts +3 -17
- package/dist/MultichainAssetsController/MultichainAssetsController.d.mts.map +1 -1
- package/dist/MultichainAssetsController/MultichainAssetsController.mjs +52 -29
- package/dist/MultichainAssetsController/MultichainAssetsController.mjs.map +1 -1
- package/dist/MultichainBalancesController/MultichainBalancesController.cjs +16 -62
- package/dist/MultichainBalancesController/MultichainBalancesController.cjs.map +1 -1
- package/dist/MultichainBalancesController/MultichainBalancesController.d.cts +5 -3
- package/dist/MultichainBalancesController/MultichainBalancesController.d.cts.map +1 -1
- package/dist/MultichainBalancesController/MultichainBalancesController.d.mts +5 -3
- package/dist/MultichainBalancesController/MultichainBalancesController.d.mts.map +1 -1
- package/dist/MultichainBalancesController/MultichainBalancesController.mjs +16 -62
- package/dist/MultichainBalancesController/MultichainBalancesController.mjs.map +1 -1
- package/dist/MultichainBalancesController/index.cjs.map +1 -1
- package/dist/MultichainBalancesController/index.d.cts +1 -1
- package/dist/MultichainBalancesController/index.d.cts.map +1 -1
- package/dist/MultichainBalancesController/index.d.mts +1 -1
- package/dist/MultichainBalancesController/index.d.mts.map +1 -1
- package/dist/MultichainBalancesController/index.mjs.map +1 -1
- package/dist/TokenDetectionController.cjs +34 -3
- package/dist/TokenDetectionController.cjs.map +1 -1
- package/dist/TokenDetectionController.d.cts +3 -1
- package/dist/TokenDetectionController.d.cts.map +1 -1
- package/dist/TokenDetectionController.d.mts +3 -1
- package/dist/TokenDetectionController.d.mts.map +1 -1
- package/dist/TokenDetectionController.mjs +34 -3
- package/dist/TokenDetectionController.mjs.map +1 -1
- package/dist/assetsUtil.cjs +1 -0
- package/dist/assetsUtil.cjs.map +1 -1
- package/dist/assetsUtil.d.cts +2 -1
- package/dist/assetsUtil.d.cts.map +1 -1
- package/dist/assetsUtil.d.mts +2 -1
- package/dist/assetsUtil.d.mts.map +1 -1
- package/dist/assetsUtil.mjs +1 -0
- package/dist/assetsUtil.mjs.map +1 -1
- package/dist/constants.cjs +2 -1
- package/dist/constants.cjs.map +1 -1
- package/dist/constants.d.cts.map +1 -1
- package/dist/constants.d.mts.map +1 -1
- package/dist/constants.mjs +2 -1
- package/dist/constants.mjs.map +1 -1
- package/dist/index.cjs +10 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +7 -1
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +7 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +4 -1
- package/dist/index.mjs.map +1 -1
- package/dist/selectors/token-selectors.cjs +0 -3
- package/dist/selectors/token-selectors.cjs.map +1 -1
- package/dist/selectors/token-selectors.d.cts +25 -10
- package/dist/selectors/token-selectors.d.cts.map +1 -1
- package/dist/selectors/token-selectors.d.mts +25 -10
- package/dist/selectors/token-selectors.d.mts.map +1 -1
- package/dist/selectors/token-selectors.mjs +0 -3
- package/dist/selectors/token-selectors.mjs.map +1 -1
- package/dist/token-prices-service/codefi-v2.cjs +42 -22
- package/dist/token-prices-service/codefi-v2.cjs.map +1 -1
- package/dist/token-prices-service/codefi-v2.d.cts +25 -4
- package/dist/token-prices-service/codefi-v2.d.cts.map +1 -1
- package/dist/token-prices-service/codefi-v2.d.mts +25 -4
- package/dist/token-prices-service/codefi-v2.d.mts.map +1 -1
- package/dist/token-prices-service/codefi-v2.mjs +40 -21
- package/dist/token-prices-service/codefi-v2.mjs.map +1 -1
- package/dist/token-prices-service/index.cjs +2 -1
- package/dist/token-prices-service/index.cjs.map +1 -1
- package/dist/token-prices-service/index.d.cts +1 -1
- package/dist/token-prices-service/index.d.cts.map +1 -1
- package/dist/token-prices-service/index.d.mts +1 -1
- package/dist/token-prices-service/index.d.mts.map +1 -1
- package/dist/token-prices-service/index.mjs +1 -1
- package/dist/token-prices-service/index.mjs.map +1 -1
- package/dist/token-service.cjs +87 -8
- package/dist/token-service.cjs.map +1 -1
- package/dist/token-service.d.cts +4 -0
- package/dist/token-service.d.cts.map +1 -1
- package/dist/token-service.d.mts +4 -0
- package/dist/token-service.d.mts.map +1 -1
- package/dist/token-service.mjs +85 -7
- package/dist/token-service.mjs.map +1 -1
- package/package.json +15 -13
- package/dist/MultichainBalancesController/account-asset-info.cjs +0 -118
- package/dist/MultichainBalancesController/account-asset-info.cjs.map +0 -1
- package/dist/MultichainBalancesController/account-asset-info.d.cts +0 -82
- package/dist/MultichainBalancesController/account-asset-info.d.cts.map +0 -1
- package/dist/MultichainBalancesController/account-asset-info.d.mts +0 -82
- package/dist/MultichainBalancesController/account-asset-info.d.mts.map +0 -1
- package/dist/MultichainBalancesController/account-asset-info.mjs +0 -110
- package/dist/MultichainBalancesController/account-asset-info.mjs.map +0 -1
|
@@ -9,7 +9,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
9
9
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
|
-
var _MultichainAssetsController_instances, _MultichainAssetsController_snaps, _MultichainAssetsController_controllerOperationMutex, _MultichainAssetsController_handleAccountAssetListUpdatedEvent, _MultichainAssetsController_handleOnAccountAddedEvent, _MultichainAssetsController_isAssetIgnored, _MultichainAssetsController_handleAccountAssetListUpdated, _MultichainAssetsController_isNonEvmAccount, _MultichainAssetsController_handleOnAccountAdded, _MultichainAssetsController_handleOnAccountRemovedEvent, _MultichainAssetsController_refreshAssetsMetadata, _MultichainAssetsController_updateAssetsMetadata, _MultichainAssetsController_getAssetSnaps, _MultichainAssetsController_getAssetSnapFor, _MultichainAssetsController_getAllSnaps, _MultichainAssetsController_getSnapsPermissions, _MultichainAssetsController_getAssetsMetadataFrom, _MultichainAssetsController_groupTokenAssetsByChain, _MultichainAssetsController_runBatchedBulkTokenScans, _MultichainAssetsController_filterBlockaidSpamTokensOnAdd, _MultichainAssetsController_findMaliciousTokensAmong, _MultichainAssetsController_getAssetsList, _MultichainAssetsController_getClient, _MultichainAssetsController_assertControllerMutexIsLocked, _MultichainAssetsController_withControllerLock;
|
|
12
|
+
var _MultichainAssetsController_instances, _MultichainAssetsController_snaps, _MultichainAssetsController_controllerOperationMutex, _MultichainAssetsController_isDeprecated, _MultichainAssetsController_enforceDisabledState, _MultichainAssetsController_handleAccountAssetListUpdatedEvent, _MultichainAssetsController_handleOnAccountAddedEvent, _MultichainAssetsController_isAssetIgnored, _MultichainAssetsController_handleAccountAssetListUpdated, _MultichainAssetsController_isNonEvmAccount, _MultichainAssetsController_handleOnAccountAdded, _MultichainAssetsController_handleOnAccountRemovedEvent, _MultichainAssetsController_refreshAssetsMetadata, _MultichainAssetsController_updateAssetsMetadata, _MultichainAssetsController_getAssetSnaps, _MultichainAssetsController_getAssetSnapFor, _MultichainAssetsController_getAllSnaps, _MultichainAssetsController_getSnapsPermissions, _MultichainAssetsController_getAssetsMetadataFrom, _MultichainAssetsController_groupTokenAssetsByChain, _MultichainAssetsController_runBatchedBulkTokenScans, _MultichainAssetsController_filterBlockaidSpamTokensOnAdd, _MultichainAssetsController_findMaliciousTokensAmong, _MultichainAssetsController_getAssetsList, _MultichainAssetsController_getClient, _MultichainAssetsController_assertControllerMutexIsLocked, _MultichainAssetsController_withControllerLock;
|
|
13
13
|
import { isEvmAccountType } from "@metamask/keyring-api";
|
|
14
14
|
import { KeyringClient } from "@metamask/keyring-snap-client";
|
|
15
15
|
import { TokenScanResultType } from "@metamask/phishing-controller";
|
|
@@ -70,7 +70,7 @@ const BLOCKAID_BULK_TOKEN_SCAN_BATCH_SIZE = 100;
|
|
|
70
70
|
*/
|
|
71
71
|
const DEFAULT_BLOCKAID_TOKEN_RESCAN_INTERVAL_MS = 24 * 60 * 60 * 1000;
|
|
72
72
|
export class MultichainAssetsController extends StaticIntervalPollingController() {
|
|
73
|
-
constructor({ messenger, state = {}, blockaidTokenRescanInterval = DEFAULT_BLOCKAID_TOKEN_RESCAN_INTERVAL_MS, }) {
|
|
73
|
+
constructor({ messenger, state = {}, blockaidTokenRescanInterval = DEFAULT_BLOCKAID_TOKEN_RESCAN_INTERVAL_MS, isDeprecated = () => false, }) {
|
|
74
74
|
super({
|
|
75
75
|
messenger,
|
|
76
76
|
name: controllerName,
|
|
@@ -84,8 +84,13 @@ export class MultichainAssetsController extends StaticIntervalPollingController(
|
|
|
84
84
|
// Mapping of CAIP-2 Chain ID to Asset Snaps.
|
|
85
85
|
_MultichainAssetsController_snaps.set(this, void 0);
|
|
86
86
|
_MultichainAssetsController_controllerOperationMutex.set(this, new Mutex());
|
|
87
|
+
_MultichainAssetsController_isDeprecated.set(this, void 0);
|
|
87
88
|
__classPrivateFieldSet(this, _MultichainAssetsController_snaps, {}, "f");
|
|
88
|
-
|
|
89
|
+
__classPrivateFieldSet(this, _MultichainAssetsController_isDeprecated, isDeprecated, "f");
|
|
90
|
+
if (__classPrivateFieldGet(this, _MultichainAssetsController_isDeprecated, "f").call(this)) {
|
|
91
|
+
__classPrivateFieldGet(this, _MultichainAssetsController_instances, "m", _MultichainAssetsController_enforceDisabledState).call(this);
|
|
92
|
+
}
|
|
93
|
+
else if (blockaidTokenRescanInterval > 0) {
|
|
89
94
|
this.setIntervalLength(blockaidTokenRescanInterval);
|
|
90
95
|
this.startPolling(null);
|
|
91
96
|
}
|
|
@@ -101,6 +106,10 @@ export class MultichainAssetsController extends StaticIntervalPollingController(
|
|
|
101
106
|
messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
|
|
102
107
|
}
|
|
103
108
|
async _executePoll(_input) {
|
|
109
|
+
if (__classPrivateFieldGet(this, _MultichainAssetsController_isDeprecated, "f").call(this)) {
|
|
110
|
+
__classPrivateFieldGet(this, _MultichainAssetsController_instances, "m", _MultichainAssetsController_enforceDisabledState).call(this);
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
104
113
|
await __classPrivateFieldGet(this, _MultichainAssetsController_instances, "m", _MultichainAssetsController_withControllerLock).call(this, async () => {
|
|
105
114
|
const assetsByAccount = {};
|
|
106
115
|
for (const [accountId, assets] of Object.entries(this.state.accountsAssets)) {
|
|
@@ -150,6 +159,10 @@ export class MultichainAssetsController extends StaticIntervalPollingController(
|
|
|
150
159
|
* @param accountId - The account ID to ignore assets for.
|
|
151
160
|
*/
|
|
152
161
|
ignoreAssets(assetsToIgnore, accountId) {
|
|
162
|
+
if (__classPrivateFieldGet(this, _MultichainAssetsController_isDeprecated, "f").call(this)) {
|
|
163
|
+
__classPrivateFieldGet(this, _MultichainAssetsController_instances, "m", _MultichainAssetsController_enforceDisabledState).call(this);
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
153
166
|
this.update((state) => {
|
|
154
167
|
if (state.accountsAssets[accountId]) {
|
|
155
168
|
state.accountsAssets[accountId] = state.accountsAssets[accountId].filter((asset) => !assetsToIgnore.includes(asset));
|
|
@@ -171,6 +184,10 @@ export class MultichainAssetsController extends StaticIntervalPollingController(
|
|
|
171
184
|
* @throws Error if assets are from different chains.
|
|
172
185
|
*/
|
|
173
186
|
async addAssets(assetIds, accountId) {
|
|
187
|
+
if (__classPrivateFieldGet(this, _MultichainAssetsController_isDeprecated, "f").call(this)) {
|
|
188
|
+
__classPrivateFieldGet(this, _MultichainAssetsController_instances, "m", _MultichainAssetsController_enforceDisabledState).call(this);
|
|
189
|
+
return [];
|
|
190
|
+
}
|
|
174
191
|
if (assetIds.length === 0) {
|
|
175
192
|
return this.state.accountsAssets[accountId] || [];
|
|
176
193
|
}
|
|
@@ -219,14 +236,33 @@ export class MultichainAssetsController extends StaticIntervalPollingController(
|
|
|
219
236
|
});
|
|
220
237
|
}
|
|
221
238
|
}
|
|
222
|
-
_MultichainAssetsController_snaps = new WeakMap(), _MultichainAssetsController_controllerOperationMutex = new WeakMap(), _MultichainAssetsController_instances = new WeakSet(),
|
|
239
|
+
_MultichainAssetsController_snaps = new WeakMap(), _MultichainAssetsController_controllerOperationMutex = new WeakMap(), _MultichainAssetsController_isDeprecated = new WeakMap(), _MultichainAssetsController_instances = new WeakSet(), _MultichainAssetsController_enforceDisabledState = function _MultichainAssetsController_enforceDisabledState() {
|
|
240
|
+
if (Object.keys(this.state.accountsAssets).length === 0 &&
|
|
241
|
+
Object.keys(this.state.assetsMetadata).length === 0 &&
|
|
242
|
+
Object.keys(this.state.allIgnoredAssets).length === 0) {
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
this.update((state) => {
|
|
246
|
+
state.accountsAssets = {};
|
|
247
|
+
state.assetsMetadata = {};
|
|
248
|
+
state.allIgnoredAssets = {};
|
|
249
|
+
});
|
|
250
|
+
}, _MultichainAssetsController_handleAccountAssetListUpdatedEvent =
|
|
223
251
|
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
224
252
|
async function _MultichainAssetsController_handleAccountAssetListUpdatedEvent(event) {
|
|
225
|
-
|
|
253
|
+
if (__classPrivateFieldGet(this, _MultichainAssetsController_isDeprecated, "f").call(this)) {
|
|
254
|
+
__classPrivateFieldGet(this, _MultichainAssetsController_instances, "m", _MultichainAssetsController_enforceDisabledState).call(this);
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
await __classPrivateFieldGet(this, _MultichainAssetsController_instances, "m", _MultichainAssetsController_withControllerLock).call(this, async () => __classPrivateFieldGet(this, _MultichainAssetsController_instances, "m", _MultichainAssetsController_handleAccountAssetListUpdated).call(this, event));
|
|
226
258
|
}, _MultichainAssetsController_handleOnAccountAddedEvent =
|
|
227
259
|
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
228
260
|
async function _MultichainAssetsController_handleOnAccountAddedEvent(account) {
|
|
229
|
-
|
|
261
|
+
if (__classPrivateFieldGet(this, _MultichainAssetsController_isDeprecated, "f").call(this)) {
|
|
262
|
+
__classPrivateFieldGet(this, _MultichainAssetsController_instances, "m", _MultichainAssetsController_enforceDisabledState).call(this);
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
await __classPrivateFieldGet(this, _MultichainAssetsController_instances, "m", _MultichainAssetsController_withControllerLock).call(this, async () => __classPrivateFieldGet(this, _MultichainAssetsController_instances, "m", _MultichainAssetsController_handleOnAccountAdded).call(this, account));
|
|
230
266
|
}, _MultichainAssetsController_isAssetIgnored = function _MultichainAssetsController_isAssetIgnored(asset, accountId) {
|
|
231
267
|
return this.state.allIgnoredAssets[accountId]?.includes(asset) ?? false;
|
|
232
268
|
}, _MultichainAssetsController_handleAccountAssetListUpdated =
|
|
@@ -239,8 +275,7 @@ async function _MultichainAssetsController_handleOnAccountAddedEvent(account) {
|
|
|
239
275
|
async function _MultichainAssetsController_handleAccountAssetListUpdated(event) {
|
|
240
276
|
__classPrivateFieldGet(this, _MultichainAssetsController_instances, "m", _MultichainAssetsController_assertControllerMutexIsLocked).call(this);
|
|
241
277
|
const assetsForMetadataRefresh = new Set([]);
|
|
242
|
-
const
|
|
243
|
-
const accountsAndAssetsToPublish = {};
|
|
278
|
+
const accountsAndAssetsToUpdate = {};
|
|
244
279
|
for (const [accountId, { added, removed }] of Object.entries(event.assets)) {
|
|
245
280
|
if (added.length > 0 || removed.length > 0) {
|
|
246
281
|
const existing = this.state.accountsAssets[accountId] || [];
|
|
@@ -249,29 +284,15 @@ async function _MultichainAssetsController_handleAccountAssetListUpdated(event)
|
|
|
249
284
|
const preFilteredToBeAddedAssets = added.filter((asset) => !existing.includes(asset) &&
|
|
250
285
|
isCaipAssetType(asset) &&
|
|
251
286
|
!__classPrivateFieldGet(this, _MultichainAssetsController_instances, "m", _MultichainAssetsController_isAssetIgnored).call(this, asset, accountId));
|
|
252
|
-
const refreshedAssets = added.filter((asset) => existing.includes(asset) &&
|
|
253
|
-
isCaipAssetType(asset) &&
|
|
254
|
-
!__classPrivateFieldGet(this, _MultichainAssetsController_instances, "m", _MultichainAssetsController_isAssetIgnored).call(this, asset, accountId));
|
|
255
287
|
// Filter out tokens that cannot be verified or are flagged malicious
|
|
256
288
|
const filteredToBeAddedAssets = await __classPrivateFieldGet(this, _MultichainAssetsController_instances, "m", _MultichainAssetsController_filterBlockaidSpamTokensOnAdd).call(this, preFilteredToBeAddedAssets);
|
|
257
289
|
// In case accountsAndAssetsToUpdate event is fired with "removed" assets that don't exist, we don't want to remove them
|
|
258
290
|
const filteredToBeRemovedAssets = removed.filter((asset) => existing.includes(asset) && isCaipAssetType(asset));
|
|
259
291
|
if (filteredToBeAddedAssets.length > 0 ||
|
|
260
292
|
filteredToBeRemovedAssets.length > 0) {
|
|
261
|
-
|
|
262
|
-
added: filteredToBeAddedAssets,
|
|
263
|
-
removed: filteredToBeRemovedAssets,
|
|
264
|
-
};
|
|
265
|
-
}
|
|
266
|
-
if (filteredToBeAddedAssets.length > 0 ||
|
|
267
|
-
filteredToBeRemovedAssets.length > 0 ||
|
|
268
|
-
refreshedAssets.length > 0) {
|
|
269
|
-
accountsAndAssetsToPublish[accountId] = {
|
|
293
|
+
accountsAndAssetsToUpdate[accountId] = {
|
|
270
294
|
added: filteredToBeAddedAssets,
|
|
271
295
|
removed: filteredToBeRemovedAssets,
|
|
272
|
-
...(refreshedAssets.length > 0
|
|
273
|
-
? { refreshed: refreshedAssets }
|
|
274
|
-
: {}),
|
|
275
296
|
};
|
|
276
297
|
}
|
|
277
298
|
for (const asset of existing) {
|
|
@@ -286,7 +307,7 @@ async function _MultichainAssetsController_handleAccountAssetListUpdated(event)
|
|
|
286
307
|
}
|
|
287
308
|
}
|
|
288
309
|
this.update((state) => {
|
|
289
|
-
for (const [accountId, { added, removed }] of Object.entries(
|
|
310
|
+
for (const [accountId, { added, removed }] of Object.entries(accountsAndAssetsToUpdate)) {
|
|
290
311
|
const assets = new Set([
|
|
291
312
|
...(state.accountsAssets[accountId] || []),
|
|
292
313
|
...added,
|
|
@@ -299,11 +320,9 @@ async function _MultichainAssetsController_handleAccountAssetListUpdated(event)
|
|
|
299
320
|
});
|
|
300
321
|
// Trigger fetching metadata for new assets
|
|
301
322
|
await __classPrivateFieldGet(this, _MultichainAssetsController_instances, "m", _MultichainAssetsController_refreshAssetsMetadata).call(this, Array.from(assetsForMetadataRefresh));
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
});
|
|
306
|
-
}
|
|
323
|
+
this.messenger.publish(`${controllerName}:accountAssetListUpdated`, {
|
|
324
|
+
assets: accountsAndAssetsToUpdate,
|
|
325
|
+
});
|
|
307
326
|
}, _MultichainAssetsController_isNonEvmAccount = function _MultichainAssetsController_isNonEvmAccount(account) {
|
|
308
327
|
return (!isEvmAccountType(account.type) &&
|
|
309
328
|
// Non-EVM accounts are backed by a Snap for now
|
|
@@ -347,6 +366,10 @@ async function _MultichainAssetsController_handleOnAccountAdded(account) {
|
|
|
347
366
|
* @param accountId - The new account id being removed.
|
|
348
367
|
*/
|
|
349
368
|
async function _MultichainAssetsController_handleOnAccountRemovedEvent(accountId) {
|
|
369
|
+
if (__classPrivateFieldGet(this, _MultichainAssetsController_isDeprecated, "f").call(this)) {
|
|
370
|
+
__classPrivateFieldGet(this, _MultichainAssetsController_instances, "m", _MultichainAssetsController_enforceDisabledState).call(this);
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
350
373
|
this.update((state) => {
|
|
351
374
|
if (state.accountsAssets[accountId]) {
|
|
352
375
|
delete state.accountsAssets[accountId];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MultichainAssetsController.mjs","sourceRoot":"","sources":["../../src/MultichainAssetsController/MultichainAssetsController.ts"],"names":[],"mappings":";;;;;;;;;;;;AAWA,OAAO,EAAE,gBAAgB,EAAE,8BAA8B;AAOzD,OAAO,EAAE,aAAa,EAAE,sCAAsC;AAW9D,OAAO,EAAE,mBAAmB,EAAE,sCAAsC;AACpE,OAAO,EAAE,+BAA+B,EAAE,qCAAqC;AAM/E,OAAO,EAAE,WAAW,EAAE,8BAA8B;AACpD,OAAO,EAAE,eAAe,EAAE,kBAAkB,EAAE,wBAAwB;AAItE,OAAO,EAAE,KAAK,EAAE,oBAAoB;AAGpC,OAAO,EAAE,iBAAiB,EAAE,oBA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,EAAE,WAAW,CAAC,gBAAgB;YACrC,OAAO;SACR,CAAC,CAAkB;KACvB,CAAC,CAAC;AACL,CAAC;IASC,IAAI,CAAC,uBAAA,IAAI,4DAA0B,CAAC,QAAQ,EAAE,EAAE,CAAC;QAC/C,MAAM,IAAI,KAAK,CACb,2DAA2D,CAC5D,CAAC;IACJ,CAAC;AACH,CAAC;AAED;;;;;;;;;;;GAWG;AACH,KAAK,yDACH,QAA2C;IAE3C,OAAO,QAAQ,CAAC,uBAAA,IAAI,4DAA0B,EAAE,QAAQ,CAAC,CAAC;AAC5D,CAAC;AAGH;;;;;;;;GAQG;AACH,KAAK,UAAU,QAAQ,CACrB,KAAY,EACZ,QAA2C;IAE3C,MAAM,WAAW,GAAG,MAAM,KAAK,CAAC,OAAO,EAAE,CAAC;IAE1C,IAAI,CAAC;QACH,OAAO,MAAM,QAAQ,CAAC,EAAE,WAAW,EAAE,CAAC,CAAC;IACzC,CAAC;YAAS,CAAC;QACT,WAAW,EAAE,CAAC;IAChB,CAAC;AACH,CAAC","sourcesContent":["import type {\n AccountsControllerAccountAddedEvent,\n AccountsControllerAccountAssetListUpdatedEvent,\n AccountsControllerAccountRemovedEvent,\n AccountsControllerListMultichainAccountsAction,\n} from '@metamask/accounts-controller';\nimport type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { isEvmAccountType } from '@metamask/keyring-api';\nimport type {\n AccountAssetListUpdatedEventPayload,\n CaipAssetType,\n CaipAssetTypeOrId,\n} from '@metamask/keyring-api';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport { KeyringClient } from '@metamask/keyring-snap-client';\nimport type { Messenger } from '@metamask/messenger';\nimport type {\n GetPermissions,\n PermissionConstraint,\n SubjectPermissions,\n} from '@metamask/permission-controller';\nimport type {\n BulkTokenScanResponse,\n PhishingControllerBulkScanTokensAction,\n} from '@metamask/phishing-controller';\nimport { TokenScanResultType } from '@metamask/phishing-controller';\nimport { StaticIntervalPollingController } from '@metamask/polling-controller';\nimport type {\n SnapControllerGetRunnableSnapsAction,\n SnapControllerHandleRequestAction,\n} from '@metamask/snaps-controllers';\nimport type { FungibleAssetMetadata, Snap, SnapId } from '@metamask/snaps-sdk';\nimport { HandlerType } from '@metamask/snaps-utils';\nimport { isCaipAssetType, parseCaipAssetType } from '@metamask/utils';\nimport type { CaipChainId } from '@metamask/utils';\nimport type { Json, JsonRpcRequest } from '@metamask/utils';\nimport type { MutexInterface } from 'async-mutex';\nimport { Mutex } from 'async-mutex';\n\nimport type { MultichainAssetsControllerMethodActions } from './MultichainAssetsController-method-action-types';\nimport { getChainIdsCaveat } from './utils';\n\nconst controllerName = 'MultichainAssetsController';\n\nexport type MultichainAssetsControllerState = {\n assetsMetadata: {\n [asset: CaipAssetType]: FungibleAssetMetadata;\n };\n accountsAssets: { [account: string]: CaipAssetType[] };\n allIgnoredAssets: { [account: string]: CaipAssetType[] };\n};\n\n// Represents the response of the asset snap's onAssetLookup handler\nexport type AssetMetadataResponse = {\n assets: {\n [asset: CaipAssetType]: FungibleAssetMetadata;\n };\n};\n\n/**\n * Per-account asset list delta published by {@link MultichainAssetsController}.\n */\nexport type MultichainAssetsControllerAccountAssetListDelta = {\n added: CaipAssetType[];\n removed: CaipAssetType[];\n /**\n * Snap-reported adds that are already tracked in MAC state. Downstream\n * controllers should refresh balances and enrichment for these assets.\n */\n refreshed?: CaipAssetType[];\n};\n\nexport type MultichainAssetsControllerAccountAssetListUpdatedPayload = {\n assets: Record<string, MultichainAssetsControllerAccountAssetListDelta>;\n};\n\nexport type MultichainAssetsControllerAccountAssetListUpdatedEvent = {\n type: `${typeof controllerName}:accountAssetListUpdated`;\n payload: [MultichainAssetsControllerAccountAssetListUpdatedPayload];\n};\n\n/**\n * Constructs the default {@link MultichainAssetsController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link MultichainAssetsController} state.\n */\nexport function getDefaultMultichainAssetsControllerState(): MultichainAssetsControllerState {\n return { accountsAssets: {}, assetsMetadata: {}, allIgnoredAssets: {} };\n}\n\n/**\n * Returns the state of the {@link MultichainAssetsController}.\n */\nexport type MultichainAssetsControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n MultichainAssetsControllerState\n>;\n\n/**\n * Event emitted when the state of the {@link MultichainAssetsController} changes.\n */\nexport type MultichainAssetsControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n MultichainAssetsControllerState\n >;\n\n/**\n * Actions exposed by the {@link MultichainAssetsController}.\n */\nexport type MultichainAssetsControllerActions =\n | MultichainAssetsControllerGetStateAction\n | MultichainAssetsControllerMethodActions;\n\n/**\n * Events emitted by {@link MultichainAssetsController}.\n */\nexport type MultichainAssetsControllerEvents =\n | MultichainAssetsControllerStateChangeEvent\n | MultichainAssetsControllerAccountAssetListUpdatedEvent;\n\n/**\n * A function executed within a mutually exclusive lock, with\n * a mutex releaser in its option bag.\n *\n * @param releaseLock - A function to release the lock.\n */\ntype MutuallyExclusiveCallback<Result> = ({\n releaseLock,\n}: {\n releaseLock: MutexInterface.Releaser;\n}) => Promise<Result>;\n\n/**\n * Actions that this controller is allowed to call.\n */\ntype AllowedActions =\n | SnapControllerGetRunnableSnapsAction\n | SnapControllerHandleRequestAction\n | GetPermissions\n | AccountsControllerListMultichainAccountsAction\n | PhishingControllerBulkScanTokensAction;\n\n/**\n * Events that this controller is allowed to subscribe.\n */\ntype AllowedEvents =\n | AccountsControllerAccountAddedEvent\n | AccountsControllerAccountRemovedEvent\n | AccountsControllerAccountAssetListUpdatedEvent;\n\n/**\n * Messenger type for the MultichainAssetsController.\n */\nexport type MultichainAssetsControllerMessenger = Messenger<\n typeof controllerName,\n MultichainAssetsControllerActions | AllowedActions,\n MultichainAssetsControllerEvents | AllowedEvents\n>;\n\n/**\n * {@link MultichainAssetsController}'s metadata.\n *\n * This allows us to choose if fields of the state should be persisted or not\n * using the `persist` flag; and if they can be sent to Sentry or not, using\n * the `anonymous` flag.\n */\nconst assetsControllerMetadata: StateMetadata<MultichainAssetsControllerState> =\n {\n assetsMetadata: {\n includeInStateLogs: false,\n persist: true,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n accountsAssets: {\n includeInStateLogs: false,\n persist: true,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n allIgnoredAssets: {\n includeInStateLogs: false,\n persist: true,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n };\n\nconst MESSENGER_EXPOSED_METHODS = [\n 'getAssetMetadata',\n 'ignoreAssets',\n 'addAssets',\n] as const;\n\n/** Phishing API allows at most this many token addresses per bulk scan request. */\nconst BLOCKAID_BULK_TOKEN_SCAN_BATCH_SIZE = 100;\n\n/**\n * Default interval for re-scanning stored SPL (`token:`) assets with Blockaid.\n * Once per day limits API load while still catching tokens reclassified after add.\n */\nconst DEFAULT_BLOCKAID_TOKEN_RESCAN_INTERVAL_MS = 24 * 60 * 60 * 1000;\n\ntype ChainTokenEntry = { asset: CaipAssetType; address: string };\n\ntype BulkTokenScanBatchOutcome =\n | {\n status: 'fulfilled';\n response: BulkTokenScanResponse;\n entries: ChainTokenEntry[];\n }\n | { status: 'rejected'; entries: ChainTokenEntry[] };\n\nexport class MultichainAssetsController extends StaticIntervalPollingController<null>()<\n typeof controllerName,\n MultichainAssetsControllerState,\n MultichainAssetsControllerMessenger\n> {\n // Mapping of CAIP-2 Chain ID to Asset Snaps.\n #snaps: Record<CaipChainId, Snap[]>;\n\n readonly #controllerOperationMutex = new Mutex();\n\n constructor({\n messenger,\n state = {},\n blockaidTokenRescanInterval = DEFAULT_BLOCKAID_TOKEN_RESCAN_INTERVAL_MS,\n }: {\n messenger: MultichainAssetsControllerMessenger;\n state?: Partial<MultichainAssetsControllerState>;\n /** Blockaid re-scan interval (ms); default daily. `0` disables. */\n blockaidTokenRescanInterval?: number;\n }) {\n super({\n messenger,\n name: controllerName,\n metadata: assetsControllerMetadata,\n state: {\n ...getDefaultMultichainAssetsControllerState(),\n ...state,\n },\n });\n\n this.#snaps = {};\n\n if (blockaidTokenRescanInterval > 0) {\n this.setIntervalLength(blockaidTokenRescanInterval);\n this.startPolling(null);\n }\n\n this.messenger.subscribe(\n 'AccountsController:accountAdded',\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n async (account) => await this.#handleOnAccountAddedEvent(account),\n );\n this.messenger.subscribe(\n 'AccountsController:accountRemoved',\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n async (account) => await this.#handleOnAccountRemovedEvent(account),\n );\n this.messenger.subscribe(\n 'AccountsController:accountAssetListUpdated',\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n async (event) => await this.#handleAccountAssetListUpdatedEvent(event),\n );\n\n messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);\n }\n\n async _executePoll(_input: null): Promise<void> {\n await this.#withControllerLock(async () => {\n const assetsByAccount: Record<\n string,\n { added: CaipAssetType[]; removed: CaipAssetType[] }\n > = {};\n\n for (const [accountId, assets] of Object.entries(\n this.state.accountsAssets,\n )) {\n const splTokens = assets.filter((asset) => {\n if (!isCaipAssetType(asset)) {\n return false;\n }\n try {\n return parseCaipAssetType(asset).assetNamespace === 'token';\n } catch {\n return false;\n }\n });\n\n if (splTokens.length === 0) {\n continue;\n }\n\n const malicious = await this.#findMaliciousTokensAmong(splTokens);\n if (malicious.length > 0) {\n this.ignoreAssets(malicious, accountId);\n assetsByAccount[accountId] = {\n added: [],\n removed: malicious,\n };\n }\n }\n\n if (Object.keys(assetsByAccount).length > 0) {\n this.messenger.publish(`${controllerName}:accountAssetListUpdated`, {\n assets: assetsByAccount,\n });\n }\n });\n }\n\n // eslint-disable-next-line @typescript-eslint/explicit-function-return-type\n async #handleAccountAssetListUpdatedEvent(\n event: AccountAssetListUpdatedEventPayload,\n ) {\n return this.#withControllerLock(async () =>\n this.#handleAccountAssetListUpdated(event),\n );\n }\n\n // eslint-disable-next-line @typescript-eslint/explicit-function-return-type\n async #handleOnAccountAddedEvent(account: InternalAccount) {\n return this.#withControllerLock(async () =>\n this.#handleOnAccountAdded(account),\n );\n }\n\n /**\n * Returns the metadata for the given asset\n *\n * @param asset - The asset to get metadata for\n * @returns The metadata for the asset or undefined if not found.\n */\n getAssetMetadata(asset: CaipAssetType): FungibleAssetMetadata | undefined {\n return this.state.assetsMetadata[asset];\n }\n\n /**\n * Ignores a batch of assets for a specific account.\n *\n * @param assetsToIgnore - Array of asset IDs to ignore.\n * @param accountId - The account ID to ignore assets for.\n */\n ignoreAssets(assetsToIgnore: CaipAssetType[], accountId: string): void {\n this.update((state) => {\n if (state.accountsAssets[accountId]) {\n state.accountsAssets[accountId] = state.accountsAssets[\n accountId\n ].filter((asset) => !assetsToIgnore.includes(asset));\n }\n\n if (!state.allIgnoredAssets[accountId]) {\n state.allIgnoredAssets[accountId] = [];\n }\n\n const newIgnoredAssets = assetsToIgnore.filter(\n (asset) => !state.allIgnoredAssets[accountId].includes(asset),\n );\n state.allIgnoredAssets[accountId].push(...newIgnoredAssets);\n });\n }\n\n /**\n * Adds multiple assets to the stored asset list for a specific account.\n * All assets must belong to the same chain.\n *\n * @param assetIds - Array of CAIP asset IDs to add (must be from same chain).\n * @param accountId - The account ID to add the assets to.\n * @returns The updated asset list for the account.\n * @throws Error if assets are from different chains.\n */\n async addAssets(\n assetIds: CaipAssetType[],\n accountId: string,\n ): Promise<CaipAssetType[]> {\n if (assetIds.length === 0) {\n return this.state.accountsAssets[accountId] || [];\n }\n\n // Validate that all assets are from the same chain\n const chainIds = new Set(\n assetIds.map((assetId) => parseCaipAssetType(assetId).chainId),\n );\n if (chainIds.size > 1) {\n throw new Error(\n `All assets must belong to the same chain. Found assets from chains: ${Array.from(chainIds).join(', ')}`,\n );\n }\n\n return this.#withControllerLock(async () => {\n // Refresh metadata for all assets\n await this.#refreshAssetsMetadata(assetIds);\n\n const addedAssets: CaipAssetType[] = [];\n\n this.update((state) => {\n // Initialize account assets if it doesn't exist\n if (!state.accountsAssets[accountId]) {\n state.accountsAssets[accountId] = [];\n }\n\n // Add assets if they don't already exist\n for (const assetId of assetIds) {\n if (!state.accountsAssets[accountId].includes(assetId)) {\n state.accountsAssets[accountId].push(assetId);\n addedAssets.push(assetId);\n }\n }\n\n // Remove from ignored list if they exist there (inline logic like EVM)\n if (state.allIgnoredAssets[accountId]) {\n state.allIgnoredAssets[accountId] = state.allIgnoredAssets[\n accountId\n ].filter((asset) => !assetIds.includes(asset));\n\n // Clean up empty arrays\n if (state.allIgnoredAssets[accountId].length === 0) {\n delete state.allIgnoredAssets[accountId];\n }\n }\n });\n\n // Publish event to notify other controllers (balances, rates) about the new assets\n if (addedAssets.length > 0) {\n this.messenger.publish(`${controllerName}:accountAssetListUpdated`, {\n assets: {\n [accountId]: {\n added: addedAssets,\n removed: [],\n },\n },\n });\n }\n\n return this.state.accountsAssets[accountId] || [];\n });\n }\n\n /**\n * Checks if an asset is ignored for a specific account.\n *\n * @param asset - The asset ID to check.\n * @param accountId - The account ID to check for.\n * @returns True if the asset is ignored, false otherwise.\n */\n #isAssetIgnored(asset: CaipAssetType, accountId: string): boolean {\n return this.state.allIgnoredAssets[accountId]?.includes(asset) ?? false;\n }\n\n /**\n * Function to update the assets list for an account\n *\n * @param event - The list of assets to update\n */\n // eslint-disable-next-line @typescript-eslint/explicit-function-return-type\n async #handleAccountAssetListUpdated(\n event: AccountAssetListUpdatedEventPayload,\n ) {\n this.#assertControllerMutexIsLocked();\n\n const assetsForMetadataRefresh = new Set<CaipAssetType>([]);\n const accountsAndAssetsForState: AccountAssetListUpdatedEventPayload['assets'] =\n {};\n const accountsAndAssetsToPublish: MultichainAssetsControllerAccountAssetListUpdatedPayload['assets'] =\n {};\n for (const [accountId, { added, removed }] of Object.entries(\n event.assets,\n )) {\n if (added.length > 0 || removed.length > 0) {\n const existing = this.state.accountsAssets[accountId] || [];\n\n // In case accountsAndAssetsToUpdate event is fired with \"added\" assets that already exist, we don't want to add them again\n // Also filter out ignored assets\n const preFilteredToBeAddedAssets = added.filter(\n (asset) =>\n !existing.includes(asset) &&\n isCaipAssetType(asset) &&\n !this.#isAssetIgnored(asset, accountId),\n );\n\n const refreshedAssets = added.filter(\n (asset): asset is CaipAssetType =>\n existing.includes(asset) &&\n isCaipAssetType(asset) &&\n !this.#isAssetIgnored(asset, accountId),\n );\n\n // Filter out tokens that cannot be verified or are flagged malicious\n const filteredToBeAddedAssets =\n await this.#filterBlockaidSpamTokensOnAdd(preFilteredToBeAddedAssets);\n\n // In case accountsAndAssetsToUpdate event is fired with \"removed\" assets that don't exist, we don't want to remove them\n const filteredToBeRemovedAssets = removed.filter(\n (asset) => existing.includes(asset) && isCaipAssetType(asset),\n );\n\n if (\n filteredToBeAddedAssets.length > 0 ||\n filteredToBeRemovedAssets.length > 0\n ) {\n accountsAndAssetsForState[accountId] = {\n added: filteredToBeAddedAssets,\n removed: filteredToBeRemovedAssets,\n };\n }\n\n if (\n filteredToBeAddedAssets.length > 0 ||\n filteredToBeRemovedAssets.length > 0 ||\n refreshedAssets.length > 0\n ) {\n accountsAndAssetsToPublish[accountId] = {\n added: filteredToBeAddedAssets,\n removed: filteredToBeRemovedAssets,\n ...(refreshedAssets.length > 0\n ? { refreshed: refreshedAssets }\n : {}),\n };\n }\n\n for (const asset of existing) {\n assetsForMetadataRefresh.add(asset);\n }\n for (const asset of filteredToBeAddedAssets) {\n assetsForMetadataRefresh.add(asset);\n }\n for (const asset of filteredToBeRemovedAssets) {\n assetsForMetadataRefresh.delete(asset);\n }\n }\n }\n\n this.update((state) => {\n for (const [accountId, { added, removed }] of Object.entries(\n accountsAndAssetsForState,\n )) {\n const assets = new Set([\n ...(state.accountsAssets[accountId] || []),\n ...added,\n ]);\n for (const asset of removed) {\n assets.delete(asset);\n }\n\n state.accountsAssets[accountId] = Array.from(assets);\n }\n });\n\n // Trigger fetching metadata for new assets\n await this.#refreshAssetsMetadata(Array.from(assetsForMetadataRefresh));\n\n if (Object.keys(accountsAndAssetsToPublish).length > 0) {\n this.messenger.publish(`${controllerName}:accountAssetListUpdated`, {\n assets: accountsAndAssetsToPublish,\n });\n }\n }\n\n /**\n * Checks for non-EVM accounts.\n *\n * @param account - The new account to be checked.\n * @returns True if the account is a non-EVM account, false otherwise.\n */\n #isNonEvmAccount(account: InternalAccount): boolean {\n return (\n !isEvmAccountType(account.type) &&\n // Non-EVM accounts are backed by a Snap for now\n account.metadata.snap !== undefined\n );\n }\n\n /**\n * Handles changes when a new account has been added.\n *\n * @param account - The new account being added.\n */\n async #handleOnAccountAdded(account: InternalAccount): Promise<void> {\n if (!this.#isNonEvmAccount(account)) {\n // Nothing to do here for EVM accounts\n return;\n }\n this.#assertControllerMutexIsLocked();\n\n // Get assets list\n if (account.metadata.snap) {\n const allAssets = await this.#getAssetsList(\n account.id,\n account.metadata.snap.id,\n );\n const caipAssets = allAssets.filter(isCaipAssetType);\n const filteredCaip =\n await this.#filterBlockaidSpamTokensOnAdd(caipAssets);\n const filteredCaipSet = new Set(filteredCaip);\n const assets = allAssets.filter(\n (asset) => !isCaipAssetType(asset) || filteredCaipSet.has(asset),\n );\n await this.#refreshAssetsMetadata(assets);\n this.update((state) => {\n state.accountsAssets[account.id] = assets;\n });\n this.messenger.publish(`${controllerName}:accountAssetListUpdated`, {\n assets: {\n [account.id]: {\n added: assets,\n removed: [],\n },\n },\n });\n }\n }\n\n /**\n * Handles changes when a new account has been removed.\n *\n * @param accountId - The new account id being removed.\n */\n async #handleOnAccountRemovedEvent(accountId: string): Promise<void> {\n this.update((state) => {\n if (state.accountsAssets[accountId]) {\n delete state.accountsAssets[accountId];\n }\n if (state.allIgnoredAssets[accountId]) {\n delete state.allIgnoredAssets[accountId];\n }\n // TODO: We are not deleting the assetsMetadata because we will soon make this controller extends StaticIntervalPollingController\n // and update all assetsMetadata once a day.\n });\n }\n\n /**\n * Refreshes the assets snaps and metadata for the given list of assets\n *\n * @param assets - The assets to refresh\n */\n // eslint-disable-next-line @typescript-eslint/explicit-function-return-type\n async #refreshAssetsMetadata(assets: CaipAssetType[]) {\n this.#assertControllerMutexIsLocked();\n\n const assetsWithoutMetadata: CaipAssetType[] = assets.filter(\n (asset) => !this.state.assetsMetadata[asset],\n );\n\n // Call the snap to get the metadata\n if (assetsWithoutMetadata.length > 0) {\n // Check if for every asset in assetsWithoutMetadata there is a snap in snaps by chainId else call getAssetSnaps\n if (\n !assetsWithoutMetadata.every((asset: CaipAssetType) => {\n const { chainId } = parseCaipAssetType(asset);\n return Boolean(this.#getAssetSnapFor(chainId));\n })\n ) {\n this.#snaps = this.#getAssetSnaps();\n }\n await this.#updateAssetsMetadata(assetsWithoutMetadata);\n }\n }\n\n /**\n * Updates the assets metadata for the given list of assets\n *\n * @param assets - The assets to update\n */\n // eslint-disable-next-line @typescript-eslint/explicit-function-return-type\n async #updateAssetsMetadata(assets: CaipAssetType[]) {\n // Creates a mapping of scope to their respective assets list.\n const assetsByScope: Record<CaipChainId, CaipAssetType[]> = {};\n for (const asset of assets) {\n const { chainId } = parseCaipAssetType(asset);\n if (!assetsByScope[chainId]) {\n assetsByScope[chainId] = [];\n }\n assetsByScope[chainId].push(asset);\n }\n\n let newMetadata: Record<CaipAssetType, FungibleAssetMetadata> = {};\n for (const chainId of Object.keys(assetsByScope) as CaipChainId[]) {\n const assetsForChain = assetsByScope[chainId];\n // Now fetch metadata from the associated asset Snaps:\n const snap = this.#getAssetSnapFor(chainId);\n if (snap) {\n const metadata = await this.#getAssetsMetadataFrom(\n assetsForChain,\n snap.id,\n );\n newMetadata = {\n ...newMetadata,\n ...(metadata?.assets ?? {}),\n };\n }\n }\n this.update((state) => {\n state.assetsMetadata = {\n ...this.state.assetsMetadata,\n ...newMetadata,\n };\n });\n }\n\n /**\n * Creates a mapping of CAIP-2 Chain ID to Asset Snaps.\n *\n * @returns A mapping of CAIP-2 Chain ID to Asset Snaps.\n */\n #getAssetSnaps(): Record<CaipChainId, Snap[]> {\n const snaps: Record<CaipChainId, Snap[]> = {};\n const allSnaps = this.#getAllSnaps();\n const allPermissions = allSnaps.map((snap) =>\n this.#getSnapsPermissions(snap.id),\n );\n\n for (const [index, permission] of allPermissions.entries()) {\n let scopes;\n for (const singlePermissionConstraint of Object.values(permission)) {\n scopes = getChainIdsCaveat(singlePermissionConstraint);\n if (!scopes) {\n continue;\n }\n for (const scope of scopes as CaipChainId[]) {\n if (!snaps[scope]) {\n snaps[scope] = [];\n }\n snaps[scope].push(allSnaps[index]);\n }\n }\n }\n return snaps;\n }\n\n /**\n * Returns the first asset snap for the given scope\n *\n * @param scope - The scope to get the asset snap for\n * @returns The asset snap for the given scope\n */\n #getAssetSnapFor(scope: CaipChainId): Snap | undefined {\n const allSnaps = this.#snaps[scope];\n // Pick only the first one, we ignore the other Snaps if there are multiple candidates for now.\n return allSnaps?.[0]; // Will be undefined if there's no Snaps candidate for this scope.\n }\n\n /**\n * Returns all the asset snaps\n *\n * @returns All the asset snaps\n */\n #getAllSnaps(): Snap[] {\n return this.messenger.call('SnapController:getRunnableSnaps');\n }\n\n /**\n * Returns the permissions for the given origin\n *\n * @param origin - The origin to get the permissions for\n * @returns The permissions for the given origin\n */\n #getSnapsPermissions(\n origin: string,\n ): SubjectPermissions<PermissionConstraint> {\n return this.messenger.call(\n 'PermissionController:getPermissions',\n origin,\n ) as SubjectPermissions<PermissionConstraint>;\n }\n\n /**\n * Returns the metadata for the given assets\n *\n * @param assets - The assets to get metadata for\n * @param snapId - The snap ID to get metadata from\n * @returns The metadata for the assets\n */\n async #getAssetsMetadataFrom(\n assets: CaipAssetType[],\n snapId: string,\n ): Promise<AssetMetadataResponse | undefined> {\n try {\n return (await this.messenger.call('SnapController:handleRequest', {\n snapId: snapId as SnapId,\n origin: 'metamask',\n handler: HandlerType.OnAssetsLookup,\n request: {\n jsonrpc: '2.0',\n method: 'onAssetLookup',\n params: {\n assets,\n },\n },\n })) as Promise<AssetMetadataResponse>;\n } catch (error) {\n // Ignore\n console.error(error);\n return undefined;\n }\n }\n\n /**\n * Groups `token:` CAIP assets by chain namespace for bulk scan.\n *\n * @param assets - CAIP assets to inspect.\n * @returns Map of chain namespace to token entries.\n */\n #groupTokenAssetsByChain(\n assets: CaipAssetType[],\n ): Record<string, ChainTokenEntry[]> {\n const tokensByChain: Record<string, ChainTokenEntry[]> = {};\n\n for (const asset of assets) {\n const { assetNamespace, assetReference, chain } =\n parseCaipAssetType(asset);\n\n if (assetNamespace === 'token') {\n const chainName = chain.namespace;\n if (!tokensByChain[chainName]) {\n tokensByChain[chainName] = [];\n }\n tokensByChain[chainName].push({ asset, address: assetReference });\n }\n }\n\n return tokensByChain;\n }\n\n async #runBatchedBulkTokenScans(\n chainName: string,\n tokenEntries: ChainTokenEntry[],\n ): Promise<BulkTokenScanBatchOutcome[]> {\n const batches: ChainTokenEntry[][] = [];\n for (\n let i = 0;\n i < tokenEntries.length;\n i += BLOCKAID_BULK_TOKEN_SCAN_BATCH_SIZE\n ) {\n batches.push(\n tokenEntries.slice(i, i + BLOCKAID_BULK_TOKEN_SCAN_BATCH_SIZE),\n );\n }\n\n const batchResults = await Promise.allSettled(\n batches.map((batch) =>\n this.messenger.call('PhishingController:bulkScanTokens', {\n chainId: chainName,\n tokens: batch.map((entry) => entry.address),\n }),\n ),\n );\n\n return batches.map((entries, index) => {\n const result = batchResults[index];\n if (result.status === 'fulfilled') {\n return {\n status: 'fulfilled' as const,\n response: result.value,\n entries,\n };\n }\n return { status: 'rejected' as const, entries };\n });\n }\n\n /**\n * Fail-open Blockaid filter for newly detected `token:` assets (native/other namespaces unchanged).\n *\n * @param assets - CAIP assets to filter.\n * @returns Filtered list, original order preserved.\n */\n async #filterBlockaidSpamTokensOnAdd(\n assets: CaipAssetType[],\n ): Promise<CaipAssetType[]> {\n const tokensByChain = this.#groupTokenAssetsByChain(assets);\n\n if (Object.keys(tokensByChain).length === 0) {\n return [...assets];\n }\n\n const rejectedAssets = new Set<CaipAssetType>();\n\n for (const [chainName, tokenEntries] of Object.entries(tokensByChain)) {\n const batchOutcomes = await this.#runBatchedBulkTokenScans(\n chainName,\n tokenEntries,\n );\n\n for (const outcome of batchOutcomes) {\n if (outcome.status === 'rejected') {\n // Fail-open: if API fails, allow all tokens in this batch through\n continue;\n }\n for (const entry of outcome.entries) {\n const scanned = outcome.response[entry.address];\n // Reject only if we have a definitive malicious result\n if (\n scanned?.result_type &&\n scanned.result_type === TokenScanResultType.Malicious\n ) {\n rejectedAssets.add(entry.asset);\n }\n }\n }\n }\n\n return assets.filter((asset) => !rejectedAssets.has(asset));\n }\n\n /**\n * SPL `token:` assets in state that Blockaid marks malicious (failed batches skipped).\n *\n * @param assets - CAIP `token:` assets to scan.\n * @returns Subset marked malicious.\n */\n async #findMaliciousTokensAmong(\n assets: CaipAssetType[],\n ): Promise<CaipAssetType[]> {\n const tokensByChain = this.#groupTokenAssetsByChain(assets);\n\n const maliciousAssets: CaipAssetType[] = [];\n\n for (const [chainName, tokenEntries] of Object.entries(tokensByChain)) {\n const batchOutcomes = await this.#runBatchedBulkTokenScans(\n chainName,\n tokenEntries,\n );\n\n for (const outcome of batchOutcomes) {\n if (outcome.status === 'rejected') {\n continue;\n }\n for (const entry of outcome.entries) {\n if (\n outcome.response[entry.address]?.result_type ===\n TokenScanResultType.Malicious\n ) {\n maliciousAssets.push(entry.asset);\n }\n }\n }\n }\n\n return maliciousAssets;\n }\n\n /**\n * Get assets list for an account\n *\n * @param accountId - AccountId to get assets for\n * @param snapId - Snap ID for the account\n * @returns list of assets\n */\n async #getAssetsList(\n accountId: string,\n snapId: string,\n ): Promise<CaipAssetTypeOrId[]> {\n return await this.#getClient(snapId).listAccountAssets(accountId);\n }\n\n /**\n * Gets a `KeyringClient` for a Snap.\n *\n * @param snapId - ID of the Snap to get the client for.\n * @returns A `KeyringClient` for the Snap.\n */\n #getClient(snapId: string): KeyringClient {\n return new KeyringClient({\n send: async (request: JsonRpcRequest) =>\n (await this.messenger.call('SnapController:handleRequest', {\n snapId: snapId as SnapId,\n origin: 'metamask',\n handler: HandlerType.OnKeyringRequest,\n request,\n })) as Promise<Json>,\n });\n }\n\n /**\n * Assert that the controller mutex is locked.\n *\n * @throws If the controller mutex is not locked.\n */\n // eslint-disable-next-line @typescript-eslint/explicit-function-return-type\n #assertControllerMutexIsLocked() {\n if (!this.#controllerOperationMutex.isLocked()) {\n throw new Error(\n 'MultichainAssetsControllerError - Attempt to update state',\n );\n }\n }\n\n /**\n * Lock the controller mutex before executing the given function,\n * and release it after the function is resolved or after an\n * error is thrown.\n *\n * This wrapper ensures that each mutable operation that interacts with the\n * controller and that changes its state is executed in a mutually exclusive way,\n * preventing unsafe concurrent access that could lead to unpredictable behavior.\n *\n * @param callback - The function to execute while the controller mutex is locked.\n * @returns The result of the function.\n */\n async #withControllerLock<Result>(\n callback: MutuallyExclusiveCallback<Result>,\n ): Promise<Result> {\n return withLock(this.#controllerOperationMutex, callback);\n }\n}\n\n/**\n * Lock the given mutex before executing the given function,\n * and release it after the function is resolved or after an\n * error is thrown.\n *\n * @param mutex - The mutex to lock.\n * @param callback - The function to execute while the mutex is locked.\n * @returns The result of the function.\n */\nasync function withLock<Result>(\n mutex: Mutex,\n callback: MutuallyExclusiveCallback<Result>,\n): Promise<Result> {\n const releaseLock = await mutex.acquire();\n\n try {\n return await callback({ releaseLock });\n } finally {\n releaseLock();\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"MultichainAssetsController.mjs","sourceRoot":"","sources":["../../src/MultichainAssetsController/MultichainAssetsController.ts"],"names":[],"mappings":";;;;;;;;;;;;AAWA,OAAO,EAAE,gBAAgB,EAAE,8BAA8B;AAOzD,OAAO,EAAE,aAAa,EAAE,sCAAsC;AAW9D,OAAO,EAAE,mBAAmB,EAAE,sCAAsC;AACpE,OAAO,EAAE,+BAA+B,EAAE,qCAAqC;AAM/E,OAAO,EAAE,WAAW,EAAE,8BAA8B;AACpD,OAAO,EAAE,eAAe,EAAE,kBAAkB,EAAE,wBAAwB;AAItE,OAAO,EAAE,KAAK,EAAE,oBAAoB;AAGpC,OAAO,EAAE,iBAAiB,EAA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C;AAC5D,CAAC;AAGH;;;;;;;;GAQG;AACH,KAAK,UAAU,QAAQ,CACrB,KAAY,EACZ,QAA2C;IAE3C,MAAM,WAAW,GAAG,MAAM,KAAK,CAAC,OAAO,EAAE,CAAC;IAE1C,IAAI,CAAC;QACH,OAAO,MAAM,QAAQ,CAAC,EAAE,WAAW,EAAE,CAAC,CAAC;IACzC,CAAC;YAAS,CAAC;QACT,WAAW,EAAE,CAAC;IAChB,CAAC;AACH,CAAC","sourcesContent":["import type {\n AccountsControllerAccountAddedEvent,\n AccountsControllerAccountAssetListUpdatedEvent,\n AccountsControllerAccountRemovedEvent,\n AccountsControllerListMultichainAccountsAction,\n} from '@metamask/accounts-controller';\nimport type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { isEvmAccountType } from '@metamask/keyring-api';\nimport type {\n AccountAssetListUpdatedEventPayload,\n CaipAssetType,\n CaipAssetTypeOrId,\n} from '@metamask/keyring-api';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport { KeyringClient } from '@metamask/keyring-snap-client';\nimport type { Messenger } from '@metamask/messenger';\nimport type {\n GetPermissions,\n PermissionConstraint,\n SubjectPermissions,\n} from '@metamask/permission-controller';\nimport type {\n BulkTokenScanResponse,\n PhishingControllerBulkScanTokensAction,\n} from '@metamask/phishing-controller';\nimport { TokenScanResultType } from '@metamask/phishing-controller';\nimport { StaticIntervalPollingController } from '@metamask/polling-controller';\nimport type {\n SnapControllerGetRunnableSnapsAction,\n SnapControllerHandleRequestAction,\n} from '@metamask/snaps-controllers';\nimport type { FungibleAssetMetadata, Snap, SnapId } from '@metamask/snaps-sdk';\nimport { HandlerType } from '@metamask/snaps-utils';\nimport { isCaipAssetType, parseCaipAssetType } from '@metamask/utils';\nimport type { CaipChainId } from '@metamask/utils';\nimport type { Json, JsonRpcRequest } from '@metamask/utils';\nimport type { MutexInterface } from 'async-mutex';\nimport { Mutex } from 'async-mutex';\n\nimport type { MultichainAssetsControllerMethodActions } from './MultichainAssetsController-method-action-types';\nimport { getChainIdsCaveat } from './utils';\n\nconst controllerName = 'MultichainAssetsController';\n\nexport type MultichainAssetsControllerState = {\n assetsMetadata: {\n [asset: CaipAssetType]: FungibleAssetMetadata;\n };\n accountsAssets: { [account: string]: CaipAssetType[] };\n allIgnoredAssets: { [account: string]: CaipAssetType[] };\n};\n\n// Represents the response of the asset snap's onAssetLookup handler\nexport type AssetMetadataResponse = {\n assets: {\n [asset: CaipAssetType]: FungibleAssetMetadata;\n };\n};\n\nexport type MultichainAssetsControllerAccountAssetListUpdatedEvent = {\n type: `${typeof controllerName}:accountAssetListUpdated`;\n payload: AccountsControllerAccountAssetListUpdatedEvent['payload'];\n};\n\n/**\n * Constructs the default {@link MultichainAssetsController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link MultichainAssetsController} state.\n */\nexport function getDefaultMultichainAssetsControllerState(): MultichainAssetsControllerState {\n return { accountsAssets: {}, assetsMetadata: {}, allIgnoredAssets: {} };\n}\n\n/**\n * Returns the state of the {@link MultichainAssetsController}.\n */\nexport type MultichainAssetsControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n MultichainAssetsControllerState\n>;\n\n/**\n * Event emitted when the state of the {@link MultichainAssetsController} changes.\n */\nexport type MultichainAssetsControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n MultichainAssetsControllerState\n >;\n\n/**\n * Actions exposed by the {@link MultichainAssetsController}.\n */\nexport type MultichainAssetsControllerActions =\n | MultichainAssetsControllerGetStateAction\n | MultichainAssetsControllerMethodActions;\n\n/**\n * Events emitted by {@link MultichainAssetsController}.\n */\nexport type MultichainAssetsControllerEvents =\n | MultichainAssetsControllerStateChangeEvent\n | MultichainAssetsControllerAccountAssetListUpdatedEvent;\n\n/**\n * A function executed within a mutually exclusive lock, with\n * a mutex releaser in its option bag.\n *\n * @param releaseLock - A function to release the lock.\n */\ntype MutuallyExclusiveCallback<Result> = ({\n releaseLock,\n}: {\n releaseLock: MutexInterface.Releaser;\n}) => Promise<Result>;\n\n/**\n * Actions that this controller is allowed to call.\n */\ntype AllowedActions =\n | SnapControllerGetRunnableSnapsAction\n | SnapControllerHandleRequestAction\n | GetPermissions\n | AccountsControllerListMultichainAccountsAction\n | PhishingControllerBulkScanTokensAction;\n\n/**\n * Events that this controller is allowed to subscribe.\n */\ntype AllowedEvents =\n | AccountsControllerAccountAddedEvent\n | AccountsControllerAccountRemovedEvent\n | AccountsControllerAccountAssetListUpdatedEvent;\n\n/**\n * Messenger type for the MultichainAssetsController.\n */\nexport type MultichainAssetsControllerMessenger = Messenger<\n typeof controllerName,\n MultichainAssetsControllerActions | AllowedActions,\n MultichainAssetsControllerEvents | AllowedEvents\n>;\n\n/**\n * {@link MultichainAssetsController}'s metadata.\n *\n * This allows us to choose if fields of the state should be persisted or not\n * using the `persist` flag; and if they can be sent to Sentry or not, using\n * the `anonymous` flag.\n */\nconst assetsControllerMetadata: StateMetadata<MultichainAssetsControllerState> =\n {\n assetsMetadata: {\n includeInStateLogs: false,\n persist: true,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n accountsAssets: {\n includeInStateLogs: false,\n persist: true,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n allIgnoredAssets: {\n includeInStateLogs: false,\n persist: true,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n };\n\nconst MESSENGER_EXPOSED_METHODS = [\n 'getAssetMetadata',\n 'ignoreAssets',\n 'addAssets',\n] as const;\n\n/** Phishing API allows at most this many token addresses per bulk scan request. */\nconst BLOCKAID_BULK_TOKEN_SCAN_BATCH_SIZE = 100;\n\n/**\n * Default interval for re-scanning stored SPL (`token:`) assets with Blockaid.\n * Once per day limits API load while still catching tokens reclassified after add.\n */\nconst DEFAULT_BLOCKAID_TOKEN_RESCAN_INTERVAL_MS = 24 * 60 * 60 * 1000;\n\ntype ChainTokenEntry = { asset: CaipAssetType; address: string };\n\ntype BulkTokenScanBatchOutcome =\n | {\n status: 'fulfilled';\n response: BulkTokenScanResponse;\n entries: ChainTokenEntry[];\n }\n | { status: 'rejected'; entries: ChainTokenEntry[] };\n\nexport class MultichainAssetsController extends StaticIntervalPollingController<null>()<\n typeof controllerName,\n MultichainAssetsControllerState,\n MultichainAssetsControllerMessenger\n> {\n // Mapping of CAIP-2 Chain ID to Asset Snaps.\n #snaps: Record<CaipChainId, Snap[]>;\n\n readonly #controllerOperationMutex = new Mutex();\n\n readonly #isDeprecated: () => boolean;\n\n constructor({\n messenger,\n state = {},\n blockaidTokenRescanInterval = DEFAULT_BLOCKAID_TOKEN_RESCAN_INTERVAL_MS,\n isDeprecated = (): boolean => false,\n }: {\n messenger: MultichainAssetsControllerMessenger;\n state?: Partial<MultichainAssetsControllerState>;\n /** Blockaid re-scan interval (ms); default daily. `0` disables. */\n blockaidTokenRescanInterval?: number;\n isDeprecated?: () => boolean;\n }) {\n super({\n messenger,\n name: controllerName,\n metadata: assetsControllerMetadata,\n state: {\n ...getDefaultMultichainAssetsControllerState(),\n ...state,\n },\n });\n\n this.#snaps = {};\n this.#isDeprecated = isDeprecated;\n\n if (this.#isDeprecated()) {\n this.#enforceDisabledState();\n } else if (blockaidTokenRescanInterval > 0) {\n this.setIntervalLength(blockaidTokenRescanInterval);\n this.startPolling(null);\n }\n\n this.messenger.subscribe(\n 'AccountsController:accountAdded',\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n async (account) => await this.#handleOnAccountAddedEvent(account),\n );\n this.messenger.subscribe(\n 'AccountsController:accountRemoved',\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n async (account) => await this.#handleOnAccountRemovedEvent(account),\n );\n this.messenger.subscribe(\n 'AccountsController:accountAssetListUpdated',\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n async (event) => await this.#handleAccountAssetListUpdatedEvent(event),\n );\n\n messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);\n }\n\n /**\n * Clears all persisted `accountsAssets`, `assetsMetadata`, and\n * `allIgnoredAssets` so that no stale asset data remains in state.\n */\n #enforceDisabledState(): void {\n if (\n Object.keys(this.state.accountsAssets).length === 0 &&\n Object.keys(this.state.assetsMetadata).length === 0 &&\n Object.keys(this.state.allIgnoredAssets).length === 0\n ) {\n return;\n }\n this.update((state) => {\n state.accountsAssets = {};\n state.assetsMetadata = {};\n state.allIgnoredAssets = {};\n });\n }\n\n async _executePoll(_input: null): Promise<void> {\n if (this.#isDeprecated()) {\n this.#enforceDisabledState();\n return;\n }\n await this.#withControllerLock(async () => {\n const assetsByAccount: Record<\n string,\n { added: CaipAssetType[]; removed: CaipAssetType[] }\n > = {};\n\n for (const [accountId, assets] of Object.entries(\n this.state.accountsAssets,\n )) {\n const splTokens = assets.filter((asset) => {\n if (!isCaipAssetType(asset)) {\n return false;\n }\n try {\n return parseCaipAssetType(asset).assetNamespace === 'token';\n } catch {\n return false;\n }\n });\n\n if (splTokens.length === 0) {\n continue;\n }\n\n const malicious = await this.#findMaliciousTokensAmong(splTokens);\n if (malicious.length > 0) {\n this.ignoreAssets(malicious, accountId);\n assetsByAccount[accountId] = {\n added: [],\n removed: malicious,\n };\n }\n }\n\n if (Object.keys(assetsByAccount).length > 0) {\n this.messenger.publish(`${controllerName}:accountAssetListUpdated`, {\n assets: assetsByAccount,\n });\n }\n });\n }\n\n // eslint-disable-next-line @typescript-eslint/explicit-function-return-type\n async #handleAccountAssetListUpdatedEvent(\n event: AccountAssetListUpdatedEventPayload,\n ) {\n if (this.#isDeprecated()) {\n this.#enforceDisabledState();\n return;\n }\n await this.#withControllerLock(async () =>\n this.#handleAccountAssetListUpdated(event),\n );\n }\n\n // eslint-disable-next-line @typescript-eslint/explicit-function-return-type\n async #handleOnAccountAddedEvent(account: InternalAccount) {\n if (this.#isDeprecated()) {\n this.#enforceDisabledState();\n return;\n }\n await this.#withControllerLock(async () =>\n this.#handleOnAccountAdded(account),\n );\n }\n\n /**\n * Returns the metadata for the given asset\n *\n * @param asset - The asset to get metadata for\n * @returns The metadata for the asset or undefined if not found.\n */\n getAssetMetadata(asset: CaipAssetType): FungibleAssetMetadata | undefined {\n return this.state.assetsMetadata[asset];\n }\n\n /**\n * Ignores a batch of assets for a specific account.\n *\n * @param assetsToIgnore - Array of asset IDs to ignore.\n * @param accountId - The account ID to ignore assets for.\n */\n ignoreAssets(assetsToIgnore: CaipAssetType[], accountId: string): void {\n if (this.#isDeprecated()) {\n this.#enforceDisabledState();\n return;\n }\n this.update((state) => {\n if (state.accountsAssets[accountId]) {\n state.accountsAssets[accountId] = state.accountsAssets[\n accountId\n ].filter((asset) => !assetsToIgnore.includes(asset));\n }\n\n if (!state.allIgnoredAssets[accountId]) {\n state.allIgnoredAssets[accountId] = [];\n }\n\n const newIgnoredAssets = assetsToIgnore.filter(\n (asset) => !state.allIgnoredAssets[accountId].includes(asset),\n );\n state.allIgnoredAssets[accountId].push(...newIgnoredAssets);\n });\n }\n\n /**\n * Adds multiple assets to the stored asset list for a specific account.\n * All assets must belong to the same chain.\n *\n * @param assetIds - Array of CAIP asset IDs to add (must be from same chain).\n * @param accountId - The account ID to add the assets to.\n * @returns The updated asset list for the account.\n * @throws Error if assets are from different chains.\n */\n async addAssets(\n assetIds: CaipAssetType[],\n accountId: string,\n ): Promise<CaipAssetType[]> {\n if (this.#isDeprecated()) {\n this.#enforceDisabledState();\n return [];\n }\n\n if (assetIds.length === 0) {\n return this.state.accountsAssets[accountId] || [];\n }\n\n // Validate that all assets are from the same chain\n const chainIds = new Set(\n assetIds.map((assetId) => parseCaipAssetType(assetId).chainId),\n );\n if (chainIds.size > 1) {\n throw new Error(\n `All assets must belong to the same chain. Found assets from chains: ${Array.from(chainIds).join(', ')}`,\n );\n }\n\n return this.#withControllerLock(async () => {\n // Refresh metadata for all assets\n await this.#refreshAssetsMetadata(assetIds);\n\n const addedAssets: CaipAssetType[] = [];\n\n this.update((state) => {\n // Initialize account assets if it doesn't exist\n if (!state.accountsAssets[accountId]) {\n state.accountsAssets[accountId] = [];\n }\n\n // Add assets if they don't already exist\n for (const assetId of assetIds) {\n if (!state.accountsAssets[accountId].includes(assetId)) {\n state.accountsAssets[accountId].push(assetId);\n addedAssets.push(assetId);\n }\n }\n\n // Remove from ignored list if they exist there (inline logic like EVM)\n if (state.allIgnoredAssets[accountId]) {\n state.allIgnoredAssets[accountId] = state.allIgnoredAssets[\n accountId\n ].filter((asset) => !assetIds.includes(asset));\n\n // Clean up empty arrays\n if (state.allIgnoredAssets[accountId].length === 0) {\n delete state.allIgnoredAssets[accountId];\n }\n }\n });\n\n // Publish event to notify other controllers (balances, rates) about the new assets\n if (addedAssets.length > 0) {\n this.messenger.publish(`${controllerName}:accountAssetListUpdated`, {\n assets: {\n [accountId]: {\n added: addedAssets,\n removed: [],\n },\n },\n });\n }\n\n return this.state.accountsAssets[accountId] || [];\n });\n }\n\n /**\n * Checks if an asset is ignored for a specific account.\n *\n * @param asset - The asset ID to check.\n * @param accountId - The account ID to check for.\n * @returns True if the asset is ignored, false otherwise.\n */\n #isAssetIgnored(asset: CaipAssetType, accountId: string): boolean {\n return this.state.allIgnoredAssets[accountId]?.includes(asset) ?? false;\n }\n\n /**\n * Function to update the assets list for an account\n *\n * @param event - The list of assets to update\n */\n // eslint-disable-next-line @typescript-eslint/explicit-function-return-type\n async #handleAccountAssetListUpdated(\n event: AccountAssetListUpdatedEventPayload,\n ) {\n this.#assertControllerMutexIsLocked();\n\n const assetsForMetadataRefresh = new Set<CaipAssetType>([]);\n const accountsAndAssetsToUpdate: AccountAssetListUpdatedEventPayload['assets'] =\n {};\n for (const [accountId, { added, removed }] of Object.entries(\n event.assets,\n )) {\n if (added.length > 0 || removed.length > 0) {\n const existing = this.state.accountsAssets[accountId] || [];\n\n // In case accountsAndAssetsToUpdate event is fired with \"added\" assets that already exist, we don't want to add them again\n // Also filter out ignored assets\n const preFilteredToBeAddedAssets = added.filter(\n (asset) =>\n !existing.includes(asset) &&\n isCaipAssetType(asset) &&\n !this.#isAssetIgnored(asset, accountId),\n );\n\n // Filter out tokens that cannot be verified or are flagged malicious\n const filteredToBeAddedAssets =\n await this.#filterBlockaidSpamTokensOnAdd(preFilteredToBeAddedAssets);\n\n // In case accountsAndAssetsToUpdate event is fired with \"removed\" assets that don't exist, we don't want to remove them\n const filteredToBeRemovedAssets = removed.filter(\n (asset) => existing.includes(asset) && isCaipAssetType(asset),\n );\n\n if (\n filteredToBeAddedAssets.length > 0 ||\n filteredToBeRemovedAssets.length > 0\n ) {\n accountsAndAssetsToUpdate[accountId] = {\n added: filteredToBeAddedAssets,\n removed: filteredToBeRemovedAssets,\n };\n }\n\n for (const asset of existing) {\n assetsForMetadataRefresh.add(asset);\n }\n for (const asset of filteredToBeAddedAssets) {\n assetsForMetadataRefresh.add(asset);\n }\n for (const asset of filteredToBeRemovedAssets) {\n assetsForMetadataRefresh.delete(asset);\n }\n }\n }\n\n this.update((state) => {\n for (const [accountId, { added, removed }] of Object.entries(\n accountsAndAssetsToUpdate,\n )) {\n const assets = new Set([\n ...(state.accountsAssets[accountId] || []),\n ...added,\n ]);\n for (const asset of removed) {\n assets.delete(asset);\n }\n\n state.accountsAssets[accountId] = Array.from(assets);\n }\n });\n\n // Trigger fetching metadata for new assets\n await this.#refreshAssetsMetadata(Array.from(assetsForMetadataRefresh));\n\n this.messenger.publish(`${controllerName}:accountAssetListUpdated`, {\n assets: accountsAndAssetsToUpdate,\n });\n }\n\n /**\n * Checks for non-EVM accounts.\n *\n * @param account - The new account to be checked.\n * @returns True if the account is a non-EVM account, false otherwise.\n */\n #isNonEvmAccount(account: InternalAccount): boolean {\n return (\n !isEvmAccountType(account.type) &&\n // Non-EVM accounts are backed by a Snap for now\n account.metadata.snap !== undefined\n );\n }\n\n /**\n * Handles changes when a new account has been added.\n *\n * @param account - The new account being added.\n */\n async #handleOnAccountAdded(account: InternalAccount): Promise<void> {\n if (!this.#isNonEvmAccount(account)) {\n // Nothing to do here for EVM accounts\n return;\n }\n this.#assertControllerMutexIsLocked();\n\n // Get assets list\n if (account.metadata.snap) {\n const allAssets = await this.#getAssetsList(\n account.id,\n account.metadata.snap.id,\n );\n const caipAssets = allAssets.filter(isCaipAssetType);\n const filteredCaip =\n await this.#filterBlockaidSpamTokensOnAdd(caipAssets);\n const filteredCaipSet = new Set(filteredCaip);\n const assets = allAssets.filter(\n (asset) => !isCaipAssetType(asset) || filteredCaipSet.has(asset),\n );\n await this.#refreshAssetsMetadata(assets);\n this.update((state) => {\n state.accountsAssets[account.id] = assets;\n });\n this.messenger.publish(`${controllerName}:accountAssetListUpdated`, {\n assets: {\n [account.id]: {\n added: assets,\n removed: [],\n },\n },\n });\n }\n }\n\n /**\n * Handles changes when a new account has been removed.\n *\n * @param accountId - The new account id being removed.\n */\n async #handleOnAccountRemovedEvent(accountId: string): Promise<void> {\n if (this.#isDeprecated()) {\n this.#enforceDisabledState();\n return;\n }\n this.update((state) => {\n if (state.accountsAssets[accountId]) {\n delete state.accountsAssets[accountId];\n }\n if (state.allIgnoredAssets[accountId]) {\n delete state.allIgnoredAssets[accountId];\n }\n // TODO: We are not deleting the assetsMetadata because we will soon make this controller extends StaticIntervalPollingController\n // and update all assetsMetadata once a day.\n });\n }\n\n /**\n * Refreshes the assets snaps and metadata for the given list of assets\n *\n * @param assets - The assets to refresh\n */\n // eslint-disable-next-line @typescript-eslint/explicit-function-return-type\n async #refreshAssetsMetadata(assets: CaipAssetType[]) {\n this.#assertControllerMutexIsLocked();\n\n const assetsWithoutMetadata: CaipAssetType[] = assets.filter(\n (asset) => !this.state.assetsMetadata[asset],\n );\n\n // Call the snap to get the metadata\n if (assetsWithoutMetadata.length > 0) {\n // Check if for every asset in assetsWithoutMetadata there is a snap in snaps by chainId else call getAssetSnaps\n if (\n !assetsWithoutMetadata.every((asset: CaipAssetType) => {\n const { chainId } = parseCaipAssetType(asset);\n return Boolean(this.#getAssetSnapFor(chainId));\n })\n ) {\n this.#snaps = this.#getAssetSnaps();\n }\n await this.#updateAssetsMetadata(assetsWithoutMetadata);\n }\n }\n\n /**\n * Updates the assets metadata for the given list of assets\n *\n * @param assets - The assets to update\n */\n // eslint-disable-next-line @typescript-eslint/explicit-function-return-type\n async #updateAssetsMetadata(assets: CaipAssetType[]) {\n // Creates a mapping of scope to their respective assets list.\n const assetsByScope: Record<CaipChainId, CaipAssetType[]> = {};\n for (const asset of assets) {\n const { chainId } = parseCaipAssetType(asset);\n if (!assetsByScope[chainId]) {\n assetsByScope[chainId] = [];\n }\n assetsByScope[chainId].push(asset);\n }\n\n let newMetadata: Record<CaipAssetType, FungibleAssetMetadata> = {};\n for (const chainId of Object.keys(assetsByScope) as CaipChainId[]) {\n const assetsForChain = assetsByScope[chainId];\n // Now fetch metadata from the associated asset Snaps:\n const snap = this.#getAssetSnapFor(chainId);\n if (snap) {\n const metadata = await this.#getAssetsMetadataFrom(\n assetsForChain,\n snap.id,\n );\n newMetadata = {\n ...newMetadata,\n ...(metadata?.assets ?? {}),\n };\n }\n }\n this.update((state) => {\n state.assetsMetadata = {\n ...this.state.assetsMetadata,\n ...newMetadata,\n };\n });\n }\n\n /**\n * Creates a mapping of CAIP-2 Chain ID to Asset Snaps.\n *\n * @returns A mapping of CAIP-2 Chain ID to Asset Snaps.\n */\n #getAssetSnaps(): Record<CaipChainId, Snap[]> {\n const snaps: Record<CaipChainId, Snap[]> = {};\n const allSnaps = this.#getAllSnaps();\n const allPermissions = allSnaps.map((snap) =>\n this.#getSnapsPermissions(snap.id),\n );\n\n for (const [index, permission] of allPermissions.entries()) {\n let scopes;\n for (const singlePermissionConstraint of Object.values(permission)) {\n scopes = getChainIdsCaveat(singlePermissionConstraint);\n if (!scopes) {\n continue;\n }\n for (const scope of scopes as CaipChainId[]) {\n if (!snaps[scope]) {\n snaps[scope] = [];\n }\n snaps[scope].push(allSnaps[index]);\n }\n }\n }\n return snaps;\n }\n\n /**\n * Returns the first asset snap for the given scope\n *\n * @param scope - The scope to get the asset snap for\n * @returns The asset snap for the given scope\n */\n #getAssetSnapFor(scope: CaipChainId): Snap | undefined {\n const allSnaps = this.#snaps[scope];\n // Pick only the first one, we ignore the other Snaps if there are multiple candidates for now.\n return allSnaps?.[0]; // Will be undefined if there's no Snaps candidate for this scope.\n }\n\n /**\n * Returns all the asset snaps\n *\n * @returns All the asset snaps\n */\n #getAllSnaps(): Snap[] {\n return this.messenger.call('SnapController:getRunnableSnaps');\n }\n\n /**\n * Returns the permissions for the given origin\n *\n * @param origin - The origin to get the permissions for\n * @returns The permissions for the given origin\n */\n #getSnapsPermissions(\n origin: string,\n ): SubjectPermissions<PermissionConstraint> {\n return this.messenger.call(\n 'PermissionController:getPermissions',\n origin,\n ) as SubjectPermissions<PermissionConstraint>;\n }\n\n /**\n * Returns the metadata for the given assets\n *\n * @param assets - The assets to get metadata for\n * @param snapId - The snap ID to get metadata from\n * @returns The metadata for the assets\n */\n async #getAssetsMetadataFrom(\n assets: CaipAssetType[],\n snapId: string,\n ): Promise<AssetMetadataResponse | undefined> {\n try {\n return (await this.messenger.call('SnapController:handleRequest', {\n snapId: snapId as SnapId,\n origin: 'metamask',\n handler: HandlerType.OnAssetsLookup,\n request: {\n jsonrpc: '2.0',\n method: 'onAssetLookup',\n params: {\n assets,\n },\n },\n })) as Promise<AssetMetadataResponse>;\n } catch (error) {\n // Ignore\n console.error(error);\n return undefined;\n }\n }\n\n /**\n * Groups `token:` CAIP assets by chain namespace for bulk scan.\n *\n * @param assets - CAIP assets to inspect.\n * @returns Map of chain namespace to token entries.\n */\n #groupTokenAssetsByChain(\n assets: CaipAssetType[],\n ): Record<string, ChainTokenEntry[]> {\n const tokensByChain: Record<string, ChainTokenEntry[]> = {};\n\n for (const asset of assets) {\n const { assetNamespace, assetReference, chain } =\n parseCaipAssetType(asset);\n\n if (assetNamespace === 'token') {\n const chainName = chain.namespace;\n if (!tokensByChain[chainName]) {\n tokensByChain[chainName] = [];\n }\n tokensByChain[chainName].push({ asset, address: assetReference });\n }\n }\n\n return tokensByChain;\n }\n\n async #runBatchedBulkTokenScans(\n chainName: string,\n tokenEntries: ChainTokenEntry[],\n ): Promise<BulkTokenScanBatchOutcome[]> {\n const batches: ChainTokenEntry[][] = [];\n for (\n let i = 0;\n i < tokenEntries.length;\n i += BLOCKAID_BULK_TOKEN_SCAN_BATCH_SIZE\n ) {\n batches.push(\n tokenEntries.slice(i, i + BLOCKAID_BULK_TOKEN_SCAN_BATCH_SIZE),\n );\n }\n\n const batchResults = await Promise.allSettled(\n batches.map((batch) =>\n this.messenger.call('PhishingController:bulkScanTokens', {\n chainId: chainName,\n tokens: batch.map((entry) => entry.address),\n }),\n ),\n );\n\n return batches.map((entries, index) => {\n const result = batchResults[index];\n if (result.status === 'fulfilled') {\n return {\n status: 'fulfilled' as const,\n response: result.value,\n entries,\n };\n }\n return { status: 'rejected' as const, entries };\n });\n }\n\n /**\n * Fail-open Blockaid filter for newly detected `token:` assets (native/other namespaces unchanged).\n *\n * @param assets - CAIP assets to filter.\n * @returns Filtered list, original order preserved.\n */\n async #filterBlockaidSpamTokensOnAdd(\n assets: CaipAssetType[],\n ): Promise<CaipAssetType[]> {\n const tokensByChain = this.#groupTokenAssetsByChain(assets);\n\n if (Object.keys(tokensByChain).length === 0) {\n return [...assets];\n }\n\n const rejectedAssets = new Set<CaipAssetType>();\n\n for (const [chainName, tokenEntries] of Object.entries(tokensByChain)) {\n const batchOutcomes = await this.#runBatchedBulkTokenScans(\n chainName,\n tokenEntries,\n );\n\n for (const outcome of batchOutcomes) {\n if (outcome.status === 'rejected') {\n // Fail-open: if API fails, allow all tokens in this batch through\n continue;\n }\n for (const entry of outcome.entries) {\n const scanned = outcome.response[entry.address];\n // Reject only if we have a definitive malicious result\n if (\n scanned?.result_type &&\n scanned.result_type === TokenScanResultType.Malicious\n ) {\n rejectedAssets.add(entry.asset);\n }\n }\n }\n }\n\n return assets.filter((asset) => !rejectedAssets.has(asset));\n }\n\n /**\n * SPL `token:` assets in state that Blockaid marks malicious (failed batches skipped).\n *\n * @param assets - CAIP `token:` assets to scan.\n * @returns Subset marked malicious.\n */\n async #findMaliciousTokensAmong(\n assets: CaipAssetType[],\n ): Promise<CaipAssetType[]> {\n const tokensByChain = this.#groupTokenAssetsByChain(assets);\n\n const maliciousAssets: CaipAssetType[] = [];\n\n for (const [chainName, tokenEntries] of Object.entries(tokensByChain)) {\n const batchOutcomes = await this.#runBatchedBulkTokenScans(\n chainName,\n tokenEntries,\n );\n\n for (const outcome of batchOutcomes) {\n if (outcome.status === 'rejected') {\n continue;\n }\n for (const entry of outcome.entries) {\n if (\n outcome.response[entry.address]?.result_type ===\n TokenScanResultType.Malicious\n ) {\n maliciousAssets.push(entry.asset);\n }\n }\n }\n }\n\n return maliciousAssets;\n }\n\n /**\n * Get assets list for an account\n *\n * @param accountId - AccountId to get assets for\n * @param snapId - Snap ID for the account\n * @returns list of assets\n */\n async #getAssetsList(\n accountId: string,\n snapId: string,\n ): Promise<CaipAssetTypeOrId[]> {\n return await this.#getClient(snapId).listAccountAssets(accountId);\n }\n\n /**\n * Gets a `KeyringClient` for a Snap.\n *\n * @param snapId - ID of the Snap to get the client for.\n * @returns A `KeyringClient` for the Snap.\n */\n #getClient(snapId: string): KeyringClient {\n return new KeyringClient({\n send: async (request: JsonRpcRequest) =>\n (await this.messenger.call('SnapController:handleRequest', {\n snapId: snapId as SnapId,\n origin: 'metamask',\n handler: HandlerType.OnKeyringRequest,\n request,\n })) as Promise<Json>,\n });\n }\n\n /**\n * Assert that the controller mutex is locked.\n *\n * @throws If the controller mutex is not locked.\n */\n // eslint-disable-next-line @typescript-eslint/explicit-function-return-type\n #assertControllerMutexIsLocked() {\n if (!this.#controllerOperationMutex.isLocked()) {\n throw new Error(\n 'MultichainAssetsControllerError - Attempt to update state',\n );\n }\n }\n\n /**\n * Lock the controller mutex before executing the given function,\n * and release it after the function is resolved or after an\n * error is thrown.\n *\n * This wrapper ensures that each mutable operation that interacts with the\n * controller and that changes its state is executed in a mutually exclusive way,\n * preventing unsafe concurrent access that could lead to unpredictable behavior.\n *\n * @param callback - The function to execute while the controller mutex is locked.\n * @returns The result of the function.\n */\n async #withControllerLock<Result>(\n callback: MutuallyExclusiveCallback<Result>,\n ): Promise<Result> {\n return withLock(this.#controllerOperationMutex, callback);\n }\n}\n\n/**\n * Lock the given mutex before executing the given function,\n * and release it after the function is resolved or after an\n * error is thrown.\n *\n * @param mutex - The mutex to lock.\n * @param callback - The function to execute while the mutex is locked.\n * @returns The result of the function.\n */\nasync function withLock<Result>(\n mutex: Mutex,\n callback: MutuallyExclusiveCallback<Result>,\n): Promise<Result> {\n const releaseLock = await mutex.acquire();\n\n try {\n return await callback({ releaseLock });\n } finally {\n releaseLock();\n }\n}\n"]}
|
|
@@ -10,14 +10,13 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
10
10
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
11
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
12
|
};
|
|
13
|
-
var _MultichainBalancesController_instances, _MultichainBalancesController_isDeprecated, _MultichainBalancesController_enforceDisabledState, _MultichainBalancesController_handleOnAccountAssetListUpdated, _MultichainBalancesController_updateBalance, _MultichainBalancesController_listMultichainAccounts, _MultichainBalancesController_listAccounts, _MultichainBalancesController_listAccountAssets, _MultichainBalancesController_getAccount, _MultichainBalancesController_isNonEvmAccount, _MultichainBalancesController_handleOnAccountBalancesUpdated,
|
|
13
|
+
var _MultichainBalancesController_instances, _MultichainBalancesController_isDeprecated, _MultichainBalancesController_enforceDisabledState, _MultichainBalancesController_handleOnAccountAssetListUpdated, _MultichainBalancesController_updateBalance, _MultichainBalancesController_listMultichainAccounts, _MultichainBalancesController_listAccounts, _MultichainBalancesController_listAccountAssets, _MultichainBalancesController_getAccount, _MultichainBalancesController_isNonEvmAccount, _MultichainBalancesController_handleOnAccountBalancesUpdated, _MultichainBalancesController_handleOnAccountRemoved, _MultichainBalancesController_getBalances, _MultichainBalancesController_getClient;
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.MultichainBalancesController = exports.getDefaultMultichainBalancesControllerState = void 0;
|
|
16
16
|
const base_controller_1 = require("@metamask/base-controller");
|
|
17
17
|
const keyring_api_1 = require("@metamask/keyring-api");
|
|
18
18
|
const keyring_snap_client_1 = require("@metamask/keyring-snap-client");
|
|
19
19
|
const snaps_utils_1 = require("@metamask/snaps-utils");
|
|
20
|
-
const account_asset_info_1 = require("./account-asset-info.cjs");
|
|
21
20
|
const controllerName = 'MultichainBalancesController';
|
|
22
21
|
/**
|
|
23
22
|
* Constructs the default {@link MultichainBalancesController} state. This allows
|
|
@@ -93,11 +92,10 @@ class MultichainBalancesController extends base_controller_1.BaseController {
|
|
|
93
92
|
this.messenger.subscribe('MultichainAssetsController:accountAssetListUpdated',
|
|
94
93
|
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
95
94
|
async ({ assets }) => {
|
|
96
|
-
const updatedAccountAssets = Object.entries(assets).map(([accountId, { added, removed
|
|
95
|
+
const updatedAccountAssets = Object.entries(assets).map(([accountId, { added, removed }]) => ({
|
|
97
96
|
accountId,
|
|
98
97
|
added: [...added],
|
|
99
98
|
removed: [...removed],
|
|
100
|
-
refreshed: refreshed ? [...refreshed] : [],
|
|
101
99
|
}));
|
|
102
100
|
await __classPrivateFieldGet(this, _MultichainBalancesController_instances, "m", _MultichainBalancesController_handleOnAccountAssetListUpdated).call(this, updatedAccountAssets);
|
|
103
101
|
});
|
|
@@ -126,7 +124,7 @@ _MultichainBalancesController_isDeprecated = new WeakMap(), _MultichainBalancesC
|
|
|
126
124
|
*
|
|
127
125
|
* The event payload is treated as a delta:
|
|
128
126
|
* - balances for `removed` assets are deleted so stale entries cannot remain
|
|
129
|
-
* - balances for `added`
|
|
127
|
+
* - balances for `added` assets are fetched from the snap and merged in
|
|
130
128
|
* - if an added asset is not returned by the snap, a zero placeholder is stored
|
|
131
129
|
* so the asset can still be represented in state
|
|
132
130
|
*
|
|
@@ -142,27 +140,21 @@ async function _MultichainBalancesController_handleOnAccountAssetListUpdated(acc
|
|
|
142
140
|
return;
|
|
143
141
|
}
|
|
144
142
|
const balancesToAdd = {};
|
|
145
|
-
for (const { accountId, added
|
|
146
|
-
|
|
147
|
-
if (assetsToFetch.length === 0) {
|
|
143
|
+
for (const { accountId, added } of accounts) {
|
|
144
|
+
if (added.length === 0) {
|
|
148
145
|
continue;
|
|
149
146
|
}
|
|
150
147
|
const account = __classPrivateFieldGet(this, _MultichainBalancesController_instances, "m", _MultichainBalancesController_getAccount).call(this, accountId);
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
148
|
+
if (account.metadata.snap) {
|
|
149
|
+
const accountBalance = await __classPrivateFieldGet(this, _MultichainBalancesController_instances, "m", _MultichainBalancesController_getBalances).call(this, account.id, account.metadata.snap.id, added);
|
|
150
|
+
balancesToAdd[accountId] = accountBalance;
|
|
154
151
|
}
|
|
155
|
-
const accountBalance = await __classPrivateFieldGet(this, _MultichainBalancesController_instances, "m", _MultichainBalancesController_getBalances).call(this, account.id, snapId, assetsToFetch);
|
|
156
|
-
const chainId = account.scopes[0];
|
|
157
|
-
const enrichment = chainId
|
|
158
|
-
? await __classPrivateFieldGet(this, _MultichainBalancesController_instances, "m", _MultichainBalancesController_fetchAccountAssetInfo).call(this, accountId, chainId, snapId, assetsToFetch).catch(() => undefined)
|
|
159
|
-
: undefined;
|
|
160
|
-
balancesToAdd[accountId] = (0, account_asset_info_1.buildBalanceRowsWithAccountAssetInfo)(assetsToFetch, accountBalance, enrichment);
|
|
161
152
|
}
|
|
162
153
|
this.update((state) => {
|
|
163
154
|
for (const { accountId, added, removed } of accounts) {
|
|
155
|
+
const accountBalances = state.balances[accountId] ?? {};
|
|
164
156
|
const addedBalances = balancesToAdd[accountId] ?? {};
|
|
165
|
-
state.balances[accountId] =
|
|
157
|
+
state.balances[accountId] = accountBalances;
|
|
166
158
|
// Remove balances for assets that disappeared from the account asset list
|
|
167
159
|
// so stale entries cannot remain in state.
|
|
168
160
|
for (const assetId of removed) {
|
|
@@ -170,13 +162,7 @@ async function _MultichainBalancesController_handleOnAccountAssetListUpdated(acc
|
|
|
170
162
|
}
|
|
171
163
|
// Merge the balances returned by the snap for the newly added assets.
|
|
172
164
|
for (const [assetId, balance] of Object.entries(addedBalances)) {
|
|
173
|
-
state.balances[accountId][assetId] =
|
|
174
|
-
...(state.balances[accountId][assetId] ?? {
|
|
175
|
-
amount: '0',
|
|
176
|
-
unit: '',
|
|
177
|
-
}),
|
|
178
|
-
...balance,
|
|
179
|
-
};
|
|
165
|
+
state.balances[accountId][assetId] = balance;
|
|
180
166
|
}
|
|
181
167
|
// If the asset list was updated but the snap did not return a balance for
|
|
182
168
|
// one of the added assets, keep the asset visible with an explicit zero.
|
|
@@ -209,7 +195,7 @@ async function _MultichainBalancesController_updateBalance(accountId, assets) {
|
|
|
209
195
|
if (account.metadata.snap) {
|
|
210
196
|
const accountBalance = await __classPrivateFieldGet(this, _MultichainBalancesController_instances, "m", _MultichainBalancesController_getBalances).call(this, account.id, account.metadata.snap.id, assets);
|
|
211
197
|
this.update((state) => {
|
|
212
|
-
state.balances[accountId] =
|
|
198
|
+
state.balances[accountId] = accountBalance;
|
|
213
199
|
});
|
|
214
200
|
}
|
|
215
201
|
}
|
|
@@ -244,33 +230,11 @@ async function _MultichainBalancesController_updateBalance(accountId, assets) {
|
|
|
244
230
|
return;
|
|
245
231
|
}
|
|
246
232
|
this.update((state) => {
|
|
247
|
-
|
|
248
|
-
if (
|
|
249
|
-
|
|
233
|
+
Object.entries(balanceUpdate.balances).forEach(([accountId, assetBalances]) => {
|
|
234
|
+
if (accountId in state.balances) {
|
|
235
|
+
Object.assign(state.balances[accountId], assetBalances);
|
|
250
236
|
}
|
|
251
|
-
|
|
252
|
-
}
|
|
253
|
-
});
|
|
254
|
-
}, _MultichainBalancesController_fetchAccountAssetInfo =
|
|
255
|
-
/**
|
|
256
|
-
* Fetches snap account-asset enrichment for the given assets.
|
|
257
|
-
*
|
|
258
|
-
* @param accountId - Account id.
|
|
259
|
-
* @param chainId - CAIP-2 chain id for enrichment.
|
|
260
|
-
* @param snapId - Wallet snap id.
|
|
261
|
-
* @param assetIds - Assets to enrich.
|
|
262
|
-
* @returns Per-asset enrichment fields, or undefined when unavailable.
|
|
263
|
-
*/
|
|
264
|
-
async function _MultichainBalancesController_fetchAccountAssetInfo(accountId, chainId, snapId, assetIds) {
|
|
265
|
-
const enrichmentAssets = (0, account_asset_info_1.filterAssetsForAccountAssetEnrichment)(assetIds, chainId);
|
|
266
|
-
if (enrichmentAssets.length === 0) {
|
|
267
|
-
return undefined;
|
|
268
|
-
}
|
|
269
|
-
return await (0, account_asset_info_1.fetchAccountAssetInfoFromSnap)((params) => this.messenger.call('SnapController:handleRequest', params), {
|
|
270
|
-
accountId,
|
|
271
|
-
snapId,
|
|
272
|
-
chainId,
|
|
273
|
-
assets: enrichmentAssets,
|
|
237
|
+
});
|
|
274
238
|
});
|
|
275
239
|
}, _MultichainBalancesController_handleOnAccountRemoved =
|
|
276
240
|
/**
|
|
@@ -309,14 +273,4 @@ async function _MultichainBalancesController_getBalances(accountId, snapId, asse
|
|
|
309
273
|
})),
|
|
310
274
|
});
|
|
311
275
|
};
|
|
312
|
-
function mergeAccountBalances(previous, incoming) {
|
|
313
|
-
const merged = { ...previous };
|
|
314
|
-
for (const [assetId, balance] of Object.entries(incoming)) {
|
|
315
|
-
merged[assetId] = {
|
|
316
|
-
...(merged[assetId] ?? { amount: '0', unit: '' }),
|
|
317
|
-
...balance,
|
|
318
|
-
};
|
|
319
|
-
}
|
|
320
|
-
return merged;
|
|
321
|
-
}
|
|
322
276
|
//# sourceMappingURL=MultichainBalancesController.cjs.map
|