@metamask-previews/network-enablement-controller 4.0.0-preview-009e026d → 4.0.0-preview-f8cdcfc2
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 +7 -0
- package/dist/NetworkEnablementController.cjs +188 -59
- package/dist/NetworkEnablementController.cjs.map +1 -1
- package/dist/NetworkEnablementController.d.cts +52 -2
- package/dist/NetworkEnablementController.d.cts.map +1 -1
- package/dist/NetworkEnablementController.d.mts +52 -2
- package/dist/NetworkEnablementController.d.mts.map +1 -1
- package/dist/NetworkEnablementController.mjs +188 -59
- package/dist/NetworkEnablementController.mjs.map +1 -1
- package/dist/index.cjs +5 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -1
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +3 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -0
- package/dist/index.mjs.map +1 -1
- package/dist/services/Slip44Service.cjs +176 -0
- package/dist/services/Slip44Service.cjs.map +1 -0
- package/dist/services/Slip44Service.d.cts +74 -0
- package/dist/services/Slip44Service.d.cts.map +1 -0
- package/dist/services/Slip44Service.d.mts +74 -0
- package/dist/services/Slip44Service.d.mts.map +1 -0
- package/dist/services/Slip44Service.mjs +169 -0
- package/dist/services/Slip44Service.mjs.map +1 -0
- package/dist/services/index.cjs +9 -0
- package/dist/services/index.cjs.map +1 -0
- package/dist/services/index.d.cts +6 -0
- package/dist/services/index.d.cts.map +1 -0
- package/dist/services/index.d.mts +6 -0
- package/dist/services/index.d.mts.map +1 -0
- package/dist/services/index.mjs +6 -0
- package/dist/services/index.mjs.map +1 -0
- package/package.json +2 -1
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- Add `nativeAssetIdentifiers` state property that maps CAIP-2 chain IDs to CAIP-19-like native asset identifiers (e.g., `eip155:1/slip44:60`) ([#7609](https://github.com/MetaMask/core/pull/7609))
|
|
13
|
+
- Add `Slip44Service` to look up SLIP-44 coin types by native currency symbol ([#7609](https://github.com/MetaMask/core/pull/7609))
|
|
14
|
+
- Add `@metamask/slip44` dependency for SLIP-44 coin type lookups ([#7609](https://github.com/MetaMask/core/pull/7609))
|
|
15
|
+
- Subscribe to `NetworkController:stateChange` to update `nativeAssetIdentifiers` when a network's native currency changes ([#7609](https://github.com/MetaMask/core/pull/7609))
|
|
16
|
+
|
|
10
17
|
### Changed
|
|
11
18
|
|
|
12
19
|
- Upgrade `@metamask/utils` from `^11.8.1` to `^11.9.0` ([#7511](https://github.com/MetaMask/core/pull/7511))
|
|
@@ -12,8 +12,19 @@ const controller_utils_1 = require("@metamask/controller-utils");
|
|
|
12
12
|
const keyring_api_1 = require("@metamask/keyring-api");
|
|
13
13
|
const utils_1 = require("@metamask/utils");
|
|
14
14
|
const constants_1 = require("./constants.cjs");
|
|
15
|
+
const services_1 = require("./services/index.cjs");
|
|
15
16
|
const utils_2 = require("./utils.cjs");
|
|
16
17
|
const controllerName = 'NetworkEnablementController';
|
|
18
|
+
/**
|
|
19
|
+
* Builds a native asset identifier in CAIP-19-like format.
|
|
20
|
+
*
|
|
21
|
+
* @param caipChainId - The CAIP-2 chain ID (e.g., 'eip155:1', 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp')
|
|
22
|
+
* @param slip44CoinType - The SLIP-44 coin type number
|
|
23
|
+
* @returns The native asset identifier string (e.g., 'eip155:1/slip44:60')
|
|
24
|
+
*/
|
|
25
|
+
function buildNativeAssetIdentifier(caipChainId, slip44CoinType) {
|
|
26
|
+
return `${caipChainId}/slip44:${slip44CoinType}`;
|
|
27
|
+
}
|
|
17
28
|
/**
|
|
18
29
|
* Gets the default state for the NetworkEnablementController.
|
|
19
30
|
*
|
|
@@ -47,6 +58,9 @@ const getDefaultNetworkEnablementControllerState = () => ({
|
|
|
47
58
|
[keyring_api_1.TrxScope.Shasta]: false,
|
|
48
59
|
},
|
|
49
60
|
},
|
|
61
|
+
// nativeAssetIdentifiers is initialized as empty and should be populated
|
|
62
|
+
// by the client using initNativeAssetIdentifiers() during controller init
|
|
63
|
+
nativeAssetIdentifiers: {},
|
|
50
64
|
});
|
|
51
65
|
// Metadata for the controller state
|
|
52
66
|
const metadata = {
|
|
@@ -56,6 +70,12 @@ const metadata = {
|
|
|
56
70
|
includeInDebugSnapshot: true,
|
|
57
71
|
usedInUi: true,
|
|
58
72
|
},
|
|
73
|
+
nativeAssetIdentifiers: {
|
|
74
|
+
includeInStateLogs: true,
|
|
75
|
+
persist: true,
|
|
76
|
+
includeInDebugSnapshot: true,
|
|
77
|
+
usedInUi: true,
|
|
78
|
+
},
|
|
59
79
|
};
|
|
60
80
|
/**
|
|
61
81
|
* Controller responsible for managing network enablement state across different blockchain networks.
|
|
@@ -85,8 +105,9 @@ class NetworkEnablementController extends base_controller_1.BaseController {
|
|
|
85
105
|
},
|
|
86
106
|
});
|
|
87
107
|
_NetworkEnablementController_instances.add(this);
|
|
88
|
-
messenger.subscribe('NetworkController:networkAdded', ({ chainId }) => {
|
|
89
|
-
|
|
108
|
+
messenger.subscribe('NetworkController:networkAdded', ({ chainId, nativeCurrency }) => {
|
|
109
|
+
// eslint-disable-next-line no-void
|
|
110
|
+
void __classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_onAddNetwork).call(this, chainId, nativeCurrency);
|
|
90
111
|
});
|
|
91
112
|
messenger.subscribe('NetworkController:networkRemoved', ({ chainId }) => {
|
|
92
113
|
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_removeNetworkEntry).call(this, chainId);
|
|
@@ -110,20 +131,20 @@ class NetworkEnablementController extends base_controller_1.BaseController {
|
|
|
110
131
|
*/
|
|
111
132
|
enableNetwork(chainId) {
|
|
112
133
|
const { namespace, storageKey } = (0, utils_2.deriveKeys)(chainId);
|
|
113
|
-
this.update((
|
|
134
|
+
this.update((state) => {
|
|
114
135
|
// disable all networks in all namespaces first
|
|
115
|
-
Object.keys(
|
|
116
|
-
Object.keys(
|
|
117
|
-
|
|
136
|
+
Object.keys(state.enabledNetworkMap).forEach((ns) => {
|
|
137
|
+
Object.keys(state.enabledNetworkMap[ns]).forEach((key) => {
|
|
138
|
+
state.enabledNetworkMap[ns][key] = false;
|
|
118
139
|
});
|
|
119
140
|
});
|
|
120
141
|
// if the namespace bucket does not exist, return
|
|
121
142
|
// new nemespace are added only when a new network is added
|
|
122
|
-
if (!
|
|
143
|
+
if (!state.enabledNetworkMap[namespace]) {
|
|
123
144
|
return;
|
|
124
145
|
}
|
|
125
146
|
// enable the network
|
|
126
|
-
|
|
147
|
+
state.enabledNetworkMap[namespace][storageKey] = true;
|
|
127
148
|
});
|
|
128
149
|
}
|
|
129
150
|
/**
|
|
@@ -148,17 +169,17 @@ class NetworkEnablementController extends base_controller_1.BaseController {
|
|
|
148
169
|
if (derivedNamespace !== namespace) {
|
|
149
170
|
throw new Error(`Chain ID ${chainId} belongs to namespace ${derivedNamespace}, but namespace ${namespace} was specified`);
|
|
150
171
|
}
|
|
151
|
-
this.update((
|
|
172
|
+
this.update((state) => {
|
|
152
173
|
// Ensure the namespace bucket exists
|
|
153
|
-
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this,
|
|
174
|
+
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this, state, namespace);
|
|
154
175
|
// Disable all networks in the specified namespace first
|
|
155
|
-
if (
|
|
156
|
-
Object.keys(
|
|
157
|
-
|
|
176
|
+
if (state.enabledNetworkMap[namespace]) {
|
|
177
|
+
Object.keys(state.enabledNetworkMap[namespace]).forEach((key) => {
|
|
178
|
+
state.enabledNetworkMap[namespace][key] = false;
|
|
158
179
|
});
|
|
159
180
|
}
|
|
160
181
|
// Enable the target network in the specified namespace
|
|
161
|
-
|
|
182
|
+
state.enabledNetworkMap[namespace][storageKey] = true;
|
|
162
183
|
});
|
|
163
184
|
}
|
|
164
185
|
/**
|
|
@@ -172,11 +193,11 @@ class NetworkEnablementController extends base_controller_1.BaseController {
|
|
|
172
193
|
* Popular networks that don't exist in NetworkController or MultichainNetworkController configurations will be skipped silently.
|
|
173
194
|
*/
|
|
174
195
|
enableAllPopularNetworks() {
|
|
175
|
-
this.update((
|
|
196
|
+
this.update((state) => {
|
|
176
197
|
// First disable all networks across all namespaces
|
|
177
|
-
Object.keys(
|
|
178
|
-
Object.keys(
|
|
179
|
-
|
|
198
|
+
Object.keys(state.enabledNetworkMap).forEach((ns) => {
|
|
199
|
+
Object.keys(state.enabledNetworkMap[ns]).forEach((key) => {
|
|
200
|
+
state.enabledNetworkMap[ns][key] = false;
|
|
180
201
|
});
|
|
181
202
|
});
|
|
182
203
|
// Get current network configurations to check if networks exist
|
|
@@ -188,35 +209,36 @@ class NetworkEnablementController extends base_controller_1.BaseController {
|
|
|
188
209
|
// Check if network exists in NetworkController configurations
|
|
189
210
|
if (networkControllerState.networkConfigurationsByChainId[chainId]) {
|
|
190
211
|
// Ensure namespace bucket exists
|
|
191
|
-
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this,
|
|
212
|
+
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this, state, namespace);
|
|
192
213
|
// Enable the network
|
|
193
|
-
|
|
214
|
+
state.enabledNetworkMap[namespace][storageKey] = true;
|
|
194
215
|
}
|
|
195
216
|
});
|
|
196
217
|
// Enable Solana mainnet if it exists in MultichainNetworkController configurations
|
|
197
218
|
const solanaKeys = (0, utils_2.deriveKeys)(keyring_api_1.SolScope.Mainnet);
|
|
198
219
|
if (multichainState.multichainNetworkConfigurationsByChainId[keyring_api_1.SolScope.Mainnet]) {
|
|
199
220
|
// Ensure namespace bucket exists
|
|
200
|
-
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this,
|
|
221
|
+
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this, state, solanaKeys.namespace);
|
|
201
222
|
// Enable Solana mainnet
|
|
202
|
-
|
|
223
|
+
state.enabledNetworkMap[solanaKeys.namespace][solanaKeys.storageKey] =
|
|
224
|
+
true;
|
|
203
225
|
}
|
|
204
226
|
// Enable Bitcoin mainnet if it exists in MultichainNetworkController configurations
|
|
205
227
|
const bitcoinKeys = (0, utils_2.deriveKeys)(keyring_api_1.BtcScope.Mainnet);
|
|
206
228
|
if (multichainState.multichainNetworkConfigurationsByChainId[keyring_api_1.BtcScope.Mainnet]) {
|
|
207
229
|
// Ensure namespace bucket exists
|
|
208
|
-
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this,
|
|
230
|
+
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this, state, bitcoinKeys.namespace);
|
|
209
231
|
// Enable Bitcoin mainnet
|
|
210
|
-
|
|
232
|
+
state.enabledNetworkMap[bitcoinKeys.namespace][bitcoinKeys.storageKey] =
|
|
211
233
|
true;
|
|
212
234
|
}
|
|
213
235
|
// Enable Tron mainnet if it exists in MultichainNetworkController configurations
|
|
214
236
|
const tronKeys = (0, utils_2.deriveKeys)(keyring_api_1.TrxScope.Mainnet);
|
|
215
237
|
if (multichainState.multichainNetworkConfigurationsByChainId[keyring_api_1.TrxScope.Mainnet]) {
|
|
216
238
|
// Ensure namespace bucket exists
|
|
217
|
-
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this,
|
|
239
|
+
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this, state, tronKeys.namespace);
|
|
218
240
|
// Enable Tron mainnet
|
|
219
|
-
|
|
241
|
+
state.enabledNetworkMap[tronKeys.namespace][tronKeys.storageKey] = true;
|
|
220
242
|
}
|
|
221
243
|
});
|
|
222
244
|
}
|
|
@@ -224,39 +246,124 @@ class NetworkEnablementController extends base_controller_1.BaseController {
|
|
|
224
246
|
* Initializes the network enablement state from network controller configurations.
|
|
225
247
|
*
|
|
226
248
|
* This method reads the current network configurations from both NetworkController
|
|
227
|
-
* and MultichainNetworkController and syncs the enabled network map accordingly.
|
|
249
|
+
* and MultichainNetworkController and syncs the enabled network map and nativeAssetIdentifiers accordingly.
|
|
228
250
|
* It ensures proper namespace buckets exist for all configured networks and only
|
|
229
251
|
* adds missing networks with a default value of false, preserving existing user settings.
|
|
230
252
|
*
|
|
231
253
|
* This method should be called after the NetworkController and MultichainNetworkController
|
|
232
254
|
* have been initialized and their configurations are available.
|
|
233
255
|
*/
|
|
234
|
-
init() {
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
256
|
+
async init() {
|
|
257
|
+
// Get network configurations from NetworkController (EVM networks)
|
|
258
|
+
const networkControllerState = this.messenger.call('NetworkController:getState');
|
|
259
|
+
// Get network configurations from MultichainNetworkController (all networks)
|
|
260
|
+
const multichainState = this.messenger.call('MultichainNetworkController:getState');
|
|
261
|
+
// Build nativeAssetIdentifiers for EVM networks using chainid.network
|
|
262
|
+
const evmNativeAssetUpdates = [];
|
|
263
|
+
for (const [chainId, config] of Object.entries(networkControllerState.networkConfigurationsByChainId)) {
|
|
264
|
+
const { caipChainId } = (0, utils_2.deriveKeys)(chainId);
|
|
265
|
+
// Skip if already in state
|
|
266
|
+
if (this.state.nativeAssetIdentifiers[caipChainId] !== undefined) {
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
// Parse hex chainId to number for chainid.network lookup
|
|
270
|
+
const numericChainId = parseInt(chainId, 16);
|
|
271
|
+
// EVM networks: use getSlip44ByChainId (chainid.network data)
|
|
272
|
+
// Default to 60 (Ethereum) if no specific mapping is found
|
|
273
|
+
const slip44CoinType = (await services_1.Slip44Service.getSlip44ByChainId(numericChainId, config.nativeCurrency)) ?? 60;
|
|
274
|
+
evmNativeAssetUpdates.push({
|
|
275
|
+
caipChainId,
|
|
276
|
+
identifier: buildNativeAssetIdentifier(caipChainId, slip44CoinType),
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
// Update state synchronously
|
|
280
|
+
this.update((state) => {
|
|
240
281
|
// Initialize namespace buckets for EVM networks from NetworkController
|
|
241
|
-
Object.
|
|
282
|
+
Object.entries(networkControllerState.networkConfigurationsByChainId).forEach(([chainId]) => {
|
|
283
|
+
var _a;
|
|
242
284
|
const { namespace, storageKey } = (0, utils_2.deriveKeys)(chainId);
|
|
243
|
-
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this,
|
|
285
|
+
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this, state, namespace);
|
|
244
286
|
// Only add network if it doesn't already exist in state (preserves user settings)
|
|
245
|
-
|
|
246
|
-
s.enabledNetworkMap[namespace][storageKey] = false;
|
|
247
|
-
}
|
|
287
|
+
(_a = state.enabledNetworkMap[namespace])[storageKey] ?? (_a[storageKey] = false);
|
|
248
288
|
});
|
|
289
|
+
// Apply nativeAssetIdentifier updates
|
|
290
|
+
for (const { caipChainId, identifier } of evmNativeAssetUpdates) {
|
|
291
|
+
state.nativeAssetIdentifiers[caipChainId] = identifier;
|
|
292
|
+
}
|
|
249
293
|
// Initialize namespace buckets for all networks from MultichainNetworkController
|
|
250
294
|
Object.keys(multichainState.multichainNetworkConfigurationsByChainId).forEach((chainId) => {
|
|
295
|
+
var _a;
|
|
251
296
|
const { namespace, storageKey } = (0, utils_2.deriveKeys)(chainId);
|
|
252
|
-
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this,
|
|
297
|
+
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this, state, namespace);
|
|
253
298
|
// Only add network if it doesn't already exist in state (preserves user settings)
|
|
254
|
-
|
|
255
|
-
s.enabledNetworkMap[namespace][storageKey] = false;
|
|
256
|
-
}
|
|
299
|
+
(_a = state.enabledNetworkMap[namespace])[storageKey] ?? (_a[storageKey] = false);
|
|
257
300
|
});
|
|
258
301
|
});
|
|
259
302
|
}
|
|
303
|
+
/**
|
|
304
|
+
* Initializes the native asset identifiers from network configurations.
|
|
305
|
+
* This method should be called from the client during controller initialization
|
|
306
|
+
* to populate the nativeAssetIdentifiers state based on actual network configurations.
|
|
307
|
+
*
|
|
308
|
+
* @param networks - Array of network configurations with chainId and nativeCurrency
|
|
309
|
+
* @example
|
|
310
|
+
* ```typescript
|
|
311
|
+
* const evmNetworks = Object.values(networkControllerState.networkConfigurationsByChainId)
|
|
312
|
+
* .map(config => ({
|
|
313
|
+
* chainId: toEvmCaipChainId(config.chainId),
|
|
314
|
+
* nativeCurrency: config.nativeCurrency,
|
|
315
|
+
* }));
|
|
316
|
+
*
|
|
317
|
+
* const multichainNetworks = Object.values(multichainState.multichainNetworkConfigurationsByChainId)
|
|
318
|
+
* .map(config => ({
|
|
319
|
+
* chainId: config.chainId,
|
|
320
|
+
* nativeCurrency: config.nativeCurrency,
|
|
321
|
+
* }));
|
|
322
|
+
*
|
|
323
|
+
* await controller.initNativeAssetIdentifiers([...evmNetworks, ...multichainNetworks]);
|
|
324
|
+
* ```
|
|
325
|
+
*/
|
|
326
|
+
async initNativeAssetIdentifiers(networks) {
|
|
327
|
+
// Process networks and collect updates
|
|
328
|
+
const updates = [];
|
|
329
|
+
for (const { chainId, nativeCurrency } of networks) {
|
|
330
|
+
// Check if nativeCurrency is already in CAIP-19 format (e.g., "bip122:.../slip44:0")
|
|
331
|
+
// Non-EVM networks from MultichainNetworkController use this format
|
|
332
|
+
if (nativeCurrency.includes('/slip44:')) {
|
|
333
|
+
updates.push({
|
|
334
|
+
chainId,
|
|
335
|
+
identifier: nativeCurrency,
|
|
336
|
+
});
|
|
337
|
+
continue;
|
|
338
|
+
}
|
|
339
|
+
// Extract namespace from CAIP-2 chainId
|
|
340
|
+
const [namespace, reference] = chainId.split(':');
|
|
341
|
+
let slip44CoinType;
|
|
342
|
+
if (namespace === 'eip155') {
|
|
343
|
+
// EVM networks: use getSlip44ByChainId (chainid.network data)
|
|
344
|
+
// Default to 60 (Ethereum) if no specific mapping is found
|
|
345
|
+
const numericChainId = parseInt(reference, 10);
|
|
346
|
+
slip44CoinType =
|
|
347
|
+
(await services_1.Slip44Service.getSlip44ByChainId(numericChainId, nativeCurrency)) ?? 60;
|
|
348
|
+
}
|
|
349
|
+
else {
|
|
350
|
+
// Non-EVM networks: use getSlip44BySymbol (@metamask/slip44 package)
|
|
351
|
+
slip44CoinType = services_1.Slip44Service.getSlip44BySymbol(nativeCurrency);
|
|
352
|
+
}
|
|
353
|
+
if (slip44CoinType !== undefined) {
|
|
354
|
+
updates.push({
|
|
355
|
+
chainId,
|
|
356
|
+
identifier: buildNativeAssetIdentifier(chainId, slip44CoinType),
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
// Apply all updates synchronously
|
|
361
|
+
this.update((state) => {
|
|
362
|
+
for (const { chainId, identifier } of updates) {
|
|
363
|
+
state.nativeAssetIdentifiers[chainId] = identifier;
|
|
364
|
+
}
|
|
365
|
+
});
|
|
366
|
+
}
|
|
260
367
|
/**
|
|
261
368
|
* Disables a network for the user.
|
|
262
369
|
*
|
|
@@ -274,8 +381,8 @@ class NetworkEnablementController extends base_controller_1.BaseController {
|
|
|
274
381
|
disableNetwork(chainId) {
|
|
275
382
|
const derivedKeys = (0, utils_2.deriveKeys)(chainId);
|
|
276
383
|
const { namespace, storageKey } = derivedKeys;
|
|
277
|
-
this.update((
|
|
278
|
-
|
|
384
|
+
this.update((state) => {
|
|
385
|
+
state.enabledNetworkMap[namespace][storageKey] = false;
|
|
279
386
|
});
|
|
280
387
|
}
|
|
281
388
|
/**
|
|
@@ -314,22 +421,42 @@ _NetworkEnablementController_instances = new WeakSet(), _NetworkEnablementContro
|
|
|
314
421
|
return enabledPopularNetworksCount > 1;
|
|
315
422
|
}, _NetworkEnablementController_removeNetworkEntry = function _NetworkEnablementController_removeNetworkEntry(chainId) {
|
|
316
423
|
const derivedKeys = (0, utils_2.deriveKeys)(chainId);
|
|
317
|
-
const { namespace, storageKey } = derivedKeys;
|
|
318
|
-
this.update((
|
|
424
|
+
const { namespace, storageKey, caipChainId } = derivedKeys;
|
|
425
|
+
this.update((state) => {
|
|
319
426
|
// fallback and enable ethereum mainnet
|
|
320
427
|
if ((0, utils_2.isOnlyNetworkEnabledInNamespace)(this.state, derivedKeys)) {
|
|
321
|
-
|
|
322
|
-
true;
|
|
428
|
+
state.enabledNetworkMap[namespace][controller_utils_1.ChainId[controller_utils_1.BuiltInNetworkName.Mainnet]] = true;
|
|
323
429
|
}
|
|
324
|
-
if (namespace in
|
|
325
|
-
delete
|
|
430
|
+
if (namespace in state.enabledNetworkMap) {
|
|
431
|
+
delete state.enabledNetworkMap[namespace][storageKey];
|
|
326
432
|
}
|
|
433
|
+
// Remove from nativeAssetIdentifiers as well
|
|
434
|
+
delete state.nativeAssetIdentifiers[caipChainId];
|
|
327
435
|
});
|
|
328
|
-
}, _NetworkEnablementController_onAddNetwork =
|
|
329
|
-
|
|
330
|
-
|
|
436
|
+
}, _NetworkEnablementController_onAddNetwork =
|
|
437
|
+
/**
|
|
438
|
+
* Handles the addition of a new EVM network to the controller.
|
|
439
|
+
*
|
|
440
|
+
* @param chainId - The chain ID to add (Hex format)
|
|
441
|
+
* @param nativeCurrency - The native currency symbol of the network (e.g., 'ETH')
|
|
442
|
+
*
|
|
443
|
+
* @description
|
|
444
|
+
* - If in popular networks mode (>2 popular networks enabled) AND adding a popular network:
|
|
445
|
+
* - Keep current selection (add but don't enable the new network)
|
|
446
|
+
* - Otherwise:
|
|
447
|
+
* - Switch to the newly added network (disable all others, enable this one)
|
|
448
|
+
* - Also updates the nativeAssetIdentifiers with the CAIP-19-like identifier
|
|
449
|
+
*/
|
|
450
|
+
async function _NetworkEnablementController_onAddNetwork(chainId, nativeCurrency) {
|
|
451
|
+
const { namespace, storageKey, reference, caipChainId } = (0, utils_2.deriveKeys)(chainId);
|
|
452
|
+
// Parse hex chainId to number for chainid.network lookup
|
|
453
|
+
const numericChainId = parseInt(reference, 16);
|
|
454
|
+
// EVM networks: use getSlip44ByChainId (chainid.network data)
|
|
455
|
+
// Default to 60 (Ethereum) if no specific mapping is found
|
|
456
|
+
const slip44CoinType = (await services_1.Slip44Service.getSlip44ByChainId(numericChainId, nativeCurrency)) ?? 60;
|
|
457
|
+
this.update((state) => {
|
|
331
458
|
// Ensure the namespace bucket exists
|
|
332
|
-
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this,
|
|
459
|
+
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this, state, namespace);
|
|
333
460
|
// Check if popular networks mode is active (>2 popular networks enabled)
|
|
334
461
|
const inPopularNetworksMode = __classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_isInPopularNetworksMode).call(this);
|
|
335
462
|
// Check if the network being added is a popular network
|
|
@@ -338,18 +465,20 @@ _NetworkEnablementController_instances = new WeakSet(), _NetworkEnablementContro
|
|
|
338
465
|
const shouldKeepCurrentSelection = inPopularNetworksMode && isAddedNetworkPopular;
|
|
339
466
|
if (shouldKeepCurrentSelection) {
|
|
340
467
|
// Add the popular network but don't enable it (keep current selection)
|
|
341
|
-
|
|
468
|
+
state.enabledNetworkMap[namespace][storageKey] = true;
|
|
342
469
|
}
|
|
343
470
|
else {
|
|
344
471
|
// Switch to the newly added network (disable all others, enable this one)
|
|
345
|
-
Object.keys(
|
|
346
|
-
Object.keys(
|
|
347
|
-
|
|
472
|
+
Object.keys(state.enabledNetworkMap).forEach((ns) => {
|
|
473
|
+
Object.keys(state.enabledNetworkMap[ns]).forEach((key) => {
|
|
474
|
+
state.enabledNetworkMap[ns][key] = false;
|
|
348
475
|
});
|
|
349
476
|
});
|
|
350
477
|
// Enable the newly added network
|
|
351
|
-
|
|
478
|
+
state.enabledNetworkMap[namespace][storageKey] = true;
|
|
352
479
|
}
|
|
480
|
+
// Update nativeAssetIdentifiers with the CAIP-19-like identifier
|
|
481
|
+
state.nativeAssetIdentifiers[caipChainId] = buildNativeAssetIdentifier(caipChainId, slip44CoinType);
|
|
353
482
|
});
|
|
354
483
|
};
|
|
355
484
|
//# sourceMappingURL=NetworkEnablementController.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NetworkEnablementController.cjs","sourceRoot":"","sources":["../src/NetworkEnablementController.ts"],"names":[],"mappings":";;;;;;;;;AAAA,+DAA2D;AAK3D,iEAAyE;AACzE,uDAAqE;AAWrE,2CAAqD;AAErD,+CAA+C;AAC/C,uCAIiB;AAEjB,MAAM,cAAc,GAAG,6BAA6B,CAAC;AA4ErD;;;;GAIG;AACH,MAAM,0CAA0C,GAC9C,GAAqC,EAAE,CAAC,CAAC;IACvC,iBAAiB,EAAE;QACjB,CAAC,0BAAkB,CAAC,MAAM,CAAC,EAAE;YAC3B,CAAC,0BAAO,CAAC,qCAAkB,CAAC,OAAO,CAAC,CAAC,EAAE,IAAI;YAC3C,CAAC,0BAAO,CAAC,qCAAkB,CAAC,YAAY,CAAC,CAAC,EAAE,IAAI;YAChD,CAAC,0BAAO,CAAC,qCAAkB,CAAC,WAAW,CAAC,CAAC,EAAE,IAAI;YAC/C,CAAC,0BAAO,CAAC,qCAAkB,CAAC,WAAW,CAAC,CAAC,EAAE,IAAI;YAC/C,CAAC,0BAAO,CAAC,qCAAkB,CAAC,UAAU,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,CAAC;aAAM,CAAC;YACN,0EAA0E;YAC1E,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,iBAAiB,CAAC,CAAC,OAAO,CAAC,CAAC,EAAE,EAAE,EAAE;gBAC9C,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,iBAAiB,CAAC,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;oBACnD,CAAC,CAAC,iBAAiB,CAAC,EAAE,CAAC,CAAC,GAAwB,CAAC,GAAG,KAAK,CAAC;gBAC5D,CAAC,CAAC,CAAC;YACL,CAAC,CAAC,CAAC;YACH,iCAAiC;YACjC,CAAC,CAAC,iBAAiB,CAAC,SAAS,CAAC,CAAC,UAAU,CAAC,GAAG,IAAI,CAAC;QACpD,CAAC;IACH,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import { BaseController } from '@metamask/base-controller';\nimport type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n} from '@metamask/base-controller';\nimport { BuiltInNetworkName, ChainId } from '@metamask/controller-utils';\nimport { BtcScope, SolScope, TrxScope } from '@metamask/keyring-api';\nimport type { Messenger } from '@metamask/messenger';\nimport type { MultichainNetworkControllerGetStateAction } from '@metamask/multichain-network-controller';\nimport type {\n NetworkControllerGetStateAction,\n NetworkControllerNetworkAddedEvent,\n NetworkControllerNetworkRemovedEvent,\n NetworkControllerStateChangeEvent,\n} from '@metamask/network-controller';\nimport type { TransactionControllerTransactionSubmittedEvent } from '@metamask/transaction-controller';\nimport type { CaipChainId, CaipNamespace, Hex } from '@metamask/utils';\nimport { KnownCaipNamespace } from '@metamask/utils';\n\nimport { POPULAR_NETWORKS } from './constants';\nimport {\n deriveKeys,\n isOnlyNetworkEnabledInNamespace,\n isPopularNetwork,\n} from './utils';\n\nconst controllerName = 'NetworkEnablementController';\n\n/**\n * Information about an ordered network.\n */\nexport type NetworksInfo = {\n /**\n * The network's chain id\n */\n networkId: CaipChainId;\n};\n\n/**\n * A map of enabled networks by CAIP namespace and chain ID.\n * For EIP-155 networks, the keys are Hex chain IDs.\n * For other networks, the keys are CAIP chain IDs.\n */\ntype EnabledMap = Record<CaipNamespace, Record<CaipChainId | Hex, boolean>>;\n\n// State shape for NetworkEnablementController\nexport type NetworkEnablementControllerState = {\n enabledNetworkMap: EnabledMap;\n};\n\nexport type NetworkEnablementControllerGetStateAction =\n ControllerGetStateAction<\n typeof controllerName,\n NetworkEnablementControllerState\n >;\n\nexport type NetworkEnablementControllerSetEnabledNetworksAction = {\n type: `${typeof controllerName}:enableNetwork`;\n handler: NetworkEnablementController['enableNetwork'];\n};\n\nexport type NetworkEnablementControllerDisableNetworkAction = {\n type: `${typeof controllerName}:disableNetwork`;\n handler: NetworkEnablementController['disableNetwork'];\n};\n\n/**\n * All actions that {@link NetworkEnablementController} calls internally.\n */\nexport type AllowedActions =\n | NetworkControllerGetStateAction\n | MultichainNetworkControllerGetStateAction;\n\nexport type NetworkEnablementControllerActions =\n | NetworkEnablementControllerGetStateAction\n | NetworkEnablementControllerSetEnabledNetworksAction\n | NetworkEnablementControllerDisableNetworkAction;\n\nexport type NetworkEnablementControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n NetworkEnablementControllerState\n >;\n\nexport type NetworkEnablementControllerEvents =\n NetworkEnablementControllerStateChangeEvent;\n\n/**\n * All events that {@link NetworkEnablementController} subscribes to internally.\n */\nexport type AllowedEvents =\n | NetworkControllerNetworkAddedEvent\n | NetworkControllerNetworkRemovedEvent\n | NetworkControllerStateChangeEvent\n | TransactionControllerTransactionSubmittedEvent;\n\nexport type NetworkEnablementControllerMessenger = Messenger<\n typeof controllerName,\n NetworkEnablementControllerActions | AllowedActions,\n NetworkEnablementControllerEvents | AllowedEvents\n>;\n\n/**\n * Gets the default state for the NetworkEnablementController.\n *\n * @returns The default state with pre-enabled networks.\n */\nconst getDefaultNetworkEnablementControllerState =\n (): NetworkEnablementControllerState => ({\n enabledNetworkMap: {\n [KnownCaipNamespace.Eip155]: {\n [ChainId[BuiltInNetworkName.Mainnet]]: true,\n [ChainId[BuiltInNetworkName.LineaMainnet]]: true,\n [ChainId[BuiltInNetworkName.BaseMainnet]]: true,\n [ChainId[BuiltInNetworkName.ArbitrumOne]]: true,\n [ChainId[BuiltInNetworkName.BscMainnet]]: true,\n [ChainId[BuiltInNetworkName.OptimismMainnet]]: true,\n [ChainId[BuiltInNetworkName.PolygonMainnet]]: true,\n [ChainId[BuiltInNetworkName.SeiMainnet]]: true,\n },\n [KnownCaipNamespace.Solana]: {\n [SolScope.Mainnet]: true,\n [SolScope.Testnet]: false,\n [SolScope.Devnet]: false,\n },\n [KnownCaipNamespace.Bip122]: {\n [BtcScope.Mainnet]: true,\n [BtcScope.Testnet]: false,\n [BtcScope.Signet]: false,\n },\n [KnownCaipNamespace.Tron]: {\n [TrxScope.Mainnet]: true,\n [TrxScope.Nile]: false,\n [TrxScope.Shasta]: false,\n },\n },\n });\n\n// Metadata for the controller state\nconst metadata = {\n enabledNetworkMap: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n};\n\n/**\n * Controller responsible for managing network enablement state across different blockchain networks.\n *\n * This controller tracks which networks are enabled/disabled for the user and provides methods\n * to toggle network states. It supports both EVM (EIP-155) and non-EVM networks like Solana.\n *\n * The controller maintains a map of enabled networks organized by namespace (e.g., 'eip155', 'solana')\n * and provides methods to query and modify network enablement states.\n */\nexport class NetworkEnablementController extends BaseController<\n typeof controllerName,\n NetworkEnablementControllerState,\n NetworkEnablementControllerMessenger\n> {\n /**\n * Creates a NetworkEnablementController instance.\n *\n * @param args - The arguments to this function.\n * @param args.messenger - Messenger used to communicate with BaseV2 controller.\n * @param args.state - Initial state to set on this controller.\n */\n constructor({\n messenger,\n state,\n }: {\n messenger: NetworkEnablementControllerMessenger;\n state?: Partial<NetworkEnablementControllerState>;\n }) {\n super({\n messenger,\n metadata,\n name: controllerName,\n state: {\n ...getDefaultNetworkEnablementControllerState(),\n ...state,\n },\n });\n\n messenger.subscribe('NetworkController:networkAdded', ({ chainId }) => {\n this.#onAddNetwork(chainId);\n });\n\n messenger.subscribe('NetworkController:networkRemoved', ({ chainId }) => {\n this.#removeNetworkEntry(chainId);\n });\n }\n\n /**\n * Enables or disables a network for the user.\n *\n * This method accepts either a Hex chain ID (for EVM networks) or a CAIP-2 chain ID\n * (for any blockchain network). The method will automatically convert Hex chain IDs\n * to CAIP-2 format internally. This dual parameter support allows for backward\n * compatibility with existing EVM chain ID formats while supporting newer\n * multi-chain standards.\n *\n * When enabling a non-popular network, this method will disable all other networks\n * to ensure only one network is active at a time (exclusive mode).\n *\n * @param chainId - The chain ID of the network to enable or disable. Can be either:\n * - A Hex string (e.g., '0x1' for Ethereum mainnet) for EVM networks\n * - A CAIP-2 chain ID (e.g., 'eip155:1' for Ethereum mainnet, 'solana:mainnet' for Solana)\n */\n enableNetwork(chainId: Hex | CaipChainId): void {\n const { namespace, storageKey } = deriveKeys(chainId);\n\n this.update((s) => {\n // disable all networks in all namespaces first\n Object.keys(s.enabledNetworkMap).forEach((ns) => {\n Object.keys(s.enabledNetworkMap[ns]).forEach((key) => {\n s.enabledNetworkMap[ns][key as CaipChainId | Hex] = false;\n });\n });\n\n // if the namespace bucket does not exist, return\n // new nemespace are added only when a new network is added\n if (!s.enabledNetworkMap[namespace]) {\n return;\n }\n\n // enable the network\n s.enabledNetworkMap[namespace][storageKey] = true;\n });\n }\n\n /**\n * Enables a network for the user within a specific namespace.\n *\n * This method accepts either a Hex chain ID (for EVM networks) or a CAIP-2 chain ID\n * (for any blockchain network) and enables it within the specified namespace.\n * The method validates that the chainId belongs to the specified namespace for safety.\n *\n * Before enabling the target network, this method disables all other networks\n * in the same namespace to ensure exclusive behavior within the namespace.\n *\n * @param chainId - The chain ID of the network to enable. Can be either:\n * - A Hex string (e.g., '0x1' for Ethereum mainnet) for EVM networks\n * - A CAIP-2 chain ID (e.g., 'eip155:1' for Ethereum mainnet, 'solana:mainnet' for Solana)\n * @param namespace - The CAIP namespace where the network should be enabled\n * @throws Error if the chainId's derived namespace doesn't match the provided namespace\n */\n enableNetworkInNamespace(\n chainId: Hex | CaipChainId,\n namespace: CaipNamespace,\n ): void {\n const { namespace: derivedNamespace, storageKey } = deriveKeys(chainId);\n\n // Validate that the derived namespace matches the provided namespace\n if (derivedNamespace !== namespace) {\n throw new Error(\n `Chain ID ${chainId} belongs to namespace ${derivedNamespace}, but namespace ${namespace} was specified`,\n );\n }\n\n this.update((s) => {\n // Ensure the namespace bucket exists\n this.#ensureNamespaceBucket(s, namespace);\n\n // Disable all networks in the specified namespace first\n if (s.enabledNetworkMap[namespace]) {\n Object.keys(s.enabledNetworkMap[namespace]).forEach((key) => {\n s.enabledNetworkMap[namespace][key as CaipChainId | Hex] = false;\n });\n }\n\n // Enable the target network in the specified namespace\n s.enabledNetworkMap[namespace][storageKey] = true;\n });\n }\n\n /**\n * Enables all popular networks and Solana mainnet.\n *\n * This method first disables all networks across all namespaces, then enables\n * all networks defined in POPULAR_NETWORKS (EVM networks), Solana mainnet, and\n * Bitcoin mainnet. This provides exclusive behavior - only popular networks will\n * be enabled after calling this method.\n *\n * Popular networks that don't exist in NetworkController or MultichainNetworkController configurations will be skipped silently.\n */\n enableAllPopularNetworks(): void {\n this.update((s) => {\n // First disable all networks across all namespaces\n Object.keys(s.enabledNetworkMap).forEach((ns) => {\n Object.keys(s.enabledNetworkMap[ns]).forEach((key) => {\n s.enabledNetworkMap[ns][key as CaipChainId | Hex] = false;\n });\n });\n\n // Get current network configurations to check if networks exist\n const networkControllerState = this.messenger.call(\n 'NetworkController:getState',\n );\n const multichainState = this.messenger.call(\n 'MultichainNetworkController:getState',\n );\n\n // Enable all popular EVM networks that exist in NetworkController configurations\n POPULAR_NETWORKS.forEach((chainId) => {\n const { namespace, storageKey } = deriveKeys(chainId as Hex);\n\n // Check if network exists in NetworkController configurations\n if (\n networkControllerState.networkConfigurationsByChainId[chainId as Hex]\n ) {\n // Ensure namespace bucket exists\n this.#ensureNamespaceBucket(s, namespace);\n // Enable the network\n s.enabledNetworkMap[namespace][storageKey] = true;\n }\n });\n\n // Enable Solana mainnet if it exists in MultichainNetworkController configurations\n const solanaKeys = deriveKeys(SolScope.Mainnet as CaipChainId);\n if (\n multichainState.multichainNetworkConfigurationsByChainId[\n SolScope.Mainnet\n ]\n ) {\n // Ensure namespace bucket exists\n this.#ensureNamespaceBucket(s, solanaKeys.namespace);\n // Enable Solana mainnet\n s.enabledNetworkMap[solanaKeys.namespace][solanaKeys.storageKey] = true;\n }\n\n // Enable Bitcoin mainnet if it exists in MultichainNetworkController configurations\n const bitcoinKeys = deriveKeys(BtcScope.Mainnet as CaipChainId);\n if (\n multichainState.multichainNetworkConfigurationsByChainId[\n BtcScope.Mainnet\n ]\n ) {\n // Ensure namespace bucket exists\n this.#ensureNamespaceBucket(s, bitcoinKeys.namespace);\n // Enable Bitcoin mainnet\n s.enabledNetworkMap[bitcoinKeys.namespace][bitcoinKeys.storageKey] =\n true;\n }\n\n // Enable Tron mainnet if it exists in MultichainNetworkController configurations\n const tronKeys = deriveKeys(TrxScope.Mainnet as CaipChainId);\n if (\n multichainState.multichainNetworkConfigurationsByChainId[\n TrxScope.Mainnet\n ]\n ) {\n // Ensure namespace bucket exists\n this.#ensureNamespaceBucket(s, tronKeys.namespace);\n // Enable Tron mainnet\n s.enabledNetworkMap[tronKeys.namespace][tronKeys.storageKey] = true;\n }\n });\n }\n\n /**\n * Initializes the network enablement state from network controller configurations.\n *\n * This method reads the current network configurations from both NetworkController\n * and MultichainNetworkController and syncs the enabled network map accordingly.\n * It ensures proper namespace buckets exist for all configured networks and only\n * adds missing networks with a default value of false, preserving existing user settings.\n *\n * This method should be called after the NetworkController and MultichainNetworkController\n * have been initialized and their configurations are available.\n */\n init(): void {\n this.update((s) => {\n // Get network configurations from NetworkController (EVM networks)\n const networkControllerState = this.messenger.call(\n 'NetworkController:getState',\n );\n\n // Get network configurations from MultichainNetworkController (all networks)\n const multichainState = this.messenger.call(\n 'MultichainNetworkController:getState',\n );\n\n // Initialize namespace buckets for EVM networks from NetworkController\n Object.keys(\n networkControllerState.networkConfigurationsByChainId,\n ).forEach((chainId) => {\n const { namespace, storageKey } = deriveKeys(chainId as Hex);\n this.#ensureNamespaceBucket(s, namespace);\n\n // Only add network if it doesn't already exist in state (preserves user settings)\n if (s.enabledNetworkMap[namespace][storageKey] === undefined) {\n s.enabledNetworkMap[namespace][storageKey] = false;\n }\n });\n\n // Initialize namespace buckets for all networks from MultichainNetworkController\n Object.keys(\n multichainState.multichainNetworkConfigurationsByChainId,\n ).forEach((chainId) => {\n const { namespace, storageKey } = deriveKeys(chainId as CaipChainId);\n this.#ensureNamespaceBucket(s, namespace);\n\n // Only add network if it doesn't already exist in state (preserves user settings)\n if (s.enabledNetworkMap[namespace][storageKey] === undefined) {\n s.enabledNetworkMap[namespace][storageKey] = false;\n }\n });\n });\n }\n\n /**\n * Disables a network for the user.\n *\n * This method accepts either a Hex chain ID (for EVM networks) or a CAIP-2 chain ID\n * (for any blockchain network). The method will automatically convert Hex chain IDs\n * to CAIP-2 format internally.\n *\n * Note: This method will prevent disabling the last remaining enabled network\n * to ensure at least one network is always available.\n *\n * @param chainId - The chain ID of the network to disable. Can be either:\n * - A Hex string (e.g., '0x1' for Ethereum mainnet) for EVM networks\n * - A CAIP-2 chain ID (e.g., 'eip155:1' for Ethereum mainnet, 'solana:mainnet' for Solana)\n */\n disableNetwork(chainId: Hex | CaipChainId): void {\n const derivedKeys = deriveKeys(chainId);\n const { namespace, storageKey } = derivedKeys;\n\n this.update((s) => {\n s.enabledNetworkMap[namespace][storageKey] = false;\n });\n }\n\n /**\n * Checks if a network is enabled.\n *\n * @param chainId - The chain ID of the network to check. Can be either:\n * - A Hex string (e.g., '0x1' for Ethereum mainnet) for EVM networks\n * - A CAIP-2 chain ID (e.g., 'eip155:1' for Ethereum mainnet, 'solana:mainnet' for Solana)\n * @returns True if the network is enabled, false otherwise\n */\n isNetworkEnabled(chainId: Hex | CaipChainId): boolean {\n const derivedKeys = deriveKeys(chainId);\n const { namespace, storageKey } = derivedKeys;\n return this.state.enabledNetworkMap[namespace]?.[storageKey] ?? false;\n }\n\n /**\n * Ensures that a namespace bucket exists in the state.\n *\n * This method creates the namespace entry in the enabledNetworkMap if it doesn't\n * already exist. This is used to prepare the state structure before adding\n * network entries.\n *\n * @param state - The current controller state\n * @param ns - The CAIP namespace to ensure exists\n */\n #ensureNamespaceBucket(\n state: NetworkEnablementControllerState,\n ns: CaipNamespace,\n ) {\n if (!state.enabledNetworkMap[ns]) {\n state.enabledNetworkMap[ns] = {};\n }\n }\n\n /**\n * Checks if popular networks mode is active (more than 2 popular networks enabled).\n *\n * This method counts how many networks defined in POPULAR_NETWORKS are currently\n * enabled in the state and returns true if more than 2 are enabled. It only checks\n * networks that actually exist in the NetworkController configurations.\n *\n * @returns True if more than 2 popular networks are enabled, false otherwise\n */\n #isInPopularNetworksMode(): boolean {\n // Get current network configurations to check which popular networks exist\n const networkControllerState = this.messenger.call(\n 'NetworkController:getState',\n );\n\n // Count how many popular networks are enabled\n const enabledPopularNetworksCount = POPULAR_NETWORKS.reduce(\n (count, chainId) => {\n // Only check networks that actually exist in NetworkController configurations\n if (\n !networkControllerState.networkConfigurationsByChainId[chainId as Hex]\n ) {\n return count; // Skip networks that don't exist\n }\n\n const { namespace, storageKey } = deriveKeys(chainId as Hex);\n const isEnabled = this.state.enabledNetworkMap[namespace]?.[storageKey];\n return isEnabled ? count + 1 : count;\n },\n 0,\n );\n\n // Return true if more than 2 popular networks are enabled\n return enabledPopularNetworksCount > 1;\n }\n\n /**\n * Removes a network entry from the state.\n *\n * This method is called when a network is removed from the system. It cleans up\n * the network entry and ensures that at least one network remains enabled.\n *\n * @param chainId - The chain ID to remove (Hex or CAIP-2 format)\n */\n #removeNetworkEntry(chainId: Hex | CaipChainId): void {\n const derivedKeys = deriveKeys(chainId);\n const { namespace, storageKey } = derivedKeys;\n\n this.update((s) => {\n // fallback and enable ethereum mainnet\n if (isOnlyNetworkEnabledInNamespace(this.state, derivedKeys)) {\n s.enabledNetworkMap[namespace][ChainId[BuiltInNetworkName.Mainnet]] =\n true;\n }\n\n if (namespace in s.enabledNetworkMap) {\n delete s.enabledNetworkMap[namespace][storageKey];\n }\n });\n }\n\n /**\n * Handles the addition of a new network to the controller.\n *\n * @param chainId - The chain ID to add (Hex or CAIP-2 format)\n *\n * @description\n * - If in popular networks mode (>2 popular networks enabled) AND adding a popular network:\n * - Keep current selection (add but don't enable the new network)\n * - Otherwise:\n * - Switch to the newly added network (disable all others, enable this one)\n */\n #onAddNetwork(chainId: Hex | CaipChainId): void {\n const { namespace, storageKey, reference } = deriveKeys(chainId);\n\n this.update((s) => {\n // Ensure the namespace bucket exists\n this.#ensureNamespaceBucket(s, namespace);\n\n // Check if popular networks mode is active (>2 popular networks enabled)\n const inPopularNetworksMode = this.#isInPopularNetworksMode();\n\n // Check if the network being added is a popular network\n const isAddedNetworkPopular = isPopularNetwork(reference);\n\n // Keep current selection only if in popular networks mode AND adding a popular network\n const shouldKeepCurrentSelection =\n inPopularNetworksMode && isAddedNetworkPopular;\n\n if (shouldKeepCurrentSelection) {\n // Add the popular network but don't enable it (keep current selection)\n s.enabledNetworkMap[namespace][storageKey] = true;\n } else {\n // Switch to the newly added network (disable all others, enable this one)\n Object.keys(s.enabledNetworkMap).forEach((ns) => {\n Object.keys(s.enabledNetworkMap[ns]).forEach((key) => {\n s.enabledNetworkMap[ns][key as CaipChainId | Hex] = false;\n });\n });\n // Enable the newly added network\n s.enabledNetworkMap[namespace][storageKey] = true;\n }\n });\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"NetworkEnablementController.cjs","sourceRoot":"","sources":["../src/NetworkEnablementController.ts"],"names":[],"mappings":";;;;;;;;;AAAA,+DAA2D;AAK3D,iEAAyE;AACzE,uDAAqE;AAWrE,2CAAqD;AAErD,+CAA+C;AAC/C,mDAA2C;AAC3C,uCAIiB;AAEjB,MAAM,cAAc,GAAG,6BAA6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iB,CAAC,SAAS,CAAC,CAAC,UAAU,CAAC,GAAG,IAAI,CAAC;QACxD,CAAC;QAED,iEAAiE;QACjE,KAAK,CAAC,sBAAsB,CAAC,WAAW,CAAC,GAAG,0BAA0B,CACpE,WAAW,EACX,cAAc,CACf,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import { BaseController } from '@metamask/base-controller';\nimport type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n} from '@metamask/base-controller';\nimport { BuiltInNetworkName, ChainId } from '@metamask/controller-utils';\nimport { BtcScope, SolScope, TrxScope } from '@metamask/keyring-api';\nimport type { Messenger } from '@metamask/messenger';\nimport type { MultichainNetworkControllerGetStateAction } from '@metamask/multichain-network-controller';\nimport type {\n NetworkControllerGetStateAction,\n NetworkControllerNetworkAddedEvent,\n NetworkControllerNetworkRemovedEvent,\n NetworkControllerStateChangeEvent,\n} from '@metamask/network-controller';\nimport type { TransactionControllerTransactionSubmittedEvent } from '@metamask/transaction-controller';\nimport type { CaipChainId, CaipNamespace, Hex } from '@metamask/utils';\nimport { KnownCaipNamespace } from '@metamask/utils';\n\nimport { POPULAR_NETWORKS } from './constants';\nimport { Slip44Service } from './services';\nimport {\n deriveKeys,\n isOnlyNetworkEnabledInNamespace,\n isPopularNetwork,\n} from './utils';\n\nconst controllerName = 'NetworkEnablementController';\n\n/**\n * Information about an ordered network.\n */\nexport type NetworksInfo = {\n /**\n * The network's chain id\n */\n networkId: CaipChainId;\n};\n\n/**\n * A map of enabled networks by CAIP namespace and chain ID.\n * For EIP-155 networks, the keys are Hex chain IDs.\n * For other networks, the keys are CAIP chain IDs.\n */\ntype EnabledMap = Record<CaipNamespace, Record<CaipChainId | Hex, boolean>>;\n\n/**\n * A native asset identifier in CAIP-19-like format.\n * Format: `{caip2ChainId}/slip44:{coinType}`\n *\n * @example\n * - `eip155:1/slip44:60` for Ethereum mainnet (ETH)\n * - `solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/slip44:501` for Solana mainnet (SOL)\n * - `bip122:000000000019d6689c085ae165831e93/slip44:0` for Bitcoin mainnet (BTC)\n */\nexport type NativeAssetIdentifier = `${CaipChainId}/slip44:${number}`;\n\n/**\n * A map of CAIP-2 chain IDs to their native asset identifiers.\n * Uses CAIP-19-like format to identify the native asset for each chain.\n *\n * @see https://github.com/satoshilabs/slips/blob/master/slip-0044.md\n */\nexport type NativeAssetIdentifiersMap = Record<\n CaipChainId,\n NativeAssetIdentifier\n>;\n\n// State shape for NetworkEnablementController\nexport type NetworkEnablementControllerState = {\n enabledNetworkMap: EnabledMap;\n nativeAssetIdentifiers: NativeAssetIdentifiersMap;\n};\n\nexport type NetworkEnablementControllerGetStateAction =\n ControllerGetStateAction<\n typeof controllerName,\n NetworkEnablementControllerState\n >;\n\nexport type NetworkEnablementControllerSetEnabledNetworksAction = {\n type: `${typeof controllerName}:enableNetwork`;\n handler: NetworkEnablementController['enableNetwork'];\n};\n\nexport type NetworkEnablementControllerDisableNetworkAction = {\n type: `${typeof controllerName}:disableNetwork`;\n handler: NetworkEnablementController['disableNetwork'];\n};\n\n/**\n * All actions that {@link NetworkEnablementController} calls internally.\n */\nexport type AllowedActions =\n | NetworkControllerGetStateAction\n | MultichainNetworkControllerGetStateAction;\n\nexport type NetworkEnablementControllerActions =\n | NetworkEnablementControllerGetStateAction\n | NetworkEnablementControllerSetEnabledNetworksAction\n | NetworkEnablementControllerDisableNetworkAction;\n\nexport type NetworkEnablementControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n NetworkEnablementControllerState\n >;\n\nexport type NetworkEnablementControllerEvents =\n NetworkEnablementControllerStateChangeEvent;\n\n/**\n * All events that {@link NetworkEnablementController} subscribes to internally.\n */\nexport type AllowedEvents =\n | NetworkControllerNetworkAddedEvent\n | NetworkControllerNetworkRemovedEvent\n | NetworkControllerStateChangeEvent\n | TransactionControllerTransactionSubmittedEvent;\n\nexport type NetworkEnablementControllerMessenger = Messenger<\n typeof controllerName,\n NetworkEnablementControllerActions | AllowedActions,\n NetworkEnablementControllerEvents | AllowedEvents\n>;\n\n/**\n * Builds a native asset identifier in CAIP-19-like format.\n *\n * @param caipChainId - The CAIP-2 chain ID (e.g., 'eip155:1', 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp')\n * @param slip44CoinType - The SLIP-44 coin type number\n * @returns The native asset identifier string (e.g., 'eip155:1/slip44:60')\n */\nfunction buildNativeAssetIdentifier(\n caipChainId: CaipChainId,\n slip44CoinType: number,\n): NativeAssetIdentifier {\n return `${caipChainId}/slip44:${slip44CoinType}`;\n}\n\n/**\n * Network configuration with chain ID and native currency symbol.\n * Used to initialize native asset identifiers.\n */\nexport type NetworkConfig = {\n chainId: CaipChainId;\n nativeCurrency: string;\n};\n\n/**\n * Gets the default state for the NetworkEnablementController.\n *\n * @returns The default state with pre-enabled networks.\n */\nconst getDefaultNetworkEnablementControllerState =\n (): NetworkEnablementControllerState => ({\n enabledNetworkMap: {\n [KnownCaipNamespace.Eip155]: {\n [ChainId[BuiltInNetworkName.Mainnet]]: true,\n [ChainId[BuiltInNetworkName.LineaMainnet]]: true,\n [ChainId[BuiltInNetworkName.BaseMainnet]]: true,\n [ChainId[BuiltInNetworkName.ArbitrumOne]]: true,\n [ChainId[BuiltInNetworkName.BscMainnet]]: true,\n [ChainId[BuiltInNetworkName.OptimismMainnet]]: true,\n [ChainId[BuiltInNetworkName.PolygonMainnet]]: true,\n [ChainId[BuiltInNetworkName.SeiMainnet]]: true,\n },\n [KnownCaipNamespace.Solana]: {\n [SolScope.Mainnet]: true,\n [SolScope.Testnet]: false,\n [SolScope.Devnet]: false,\n },\n [KnownCaipNamespace.Bip122]: {\n [BtcScope.Mainnet]: true,\n [BtcScope.Testnet]: false,\n [BtcScope.Signet]: false,\n },\n [KnownCaipNamespace.Tron]: {\n [TrxScope.Mainnet]: true,\n [TrxScope.Nile]: false,\n [TrxScope.Shasta]: false,\n },\n },\n // nativeAssetIdentifiers is initialized as empty and should be populated\n // by the client using initNativeAssetIdentifiers() during controller init\n nativeAssetIdentifiers: {},\n });\n\n// Metadata for the controller state\nconst metadata = {\n enabledNetworkMap: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n nativeAssetIdentifiers: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n};\n\n/**\n * Controller responsible for managing network enablement state across different blockchain networks.\n *\n * This controller tracks which networks are enabled/disabled for the user and provides methods\n * to toggle network states. It supports both EVM (EIP-155) and non-EVM networks like Solana.\n *\n * The controller maintains a map of enabled networks organized by namespace (e.g., 'eip155', 'solana')\n * and provides methods to query and modify network enablement states.\n */\nexport class NetworkEnablementController extends BaseController<\n typeof controllerName,\n NetworkEnablementControllerState,\n NetworkEnablementControllerMessenger\n> {\n /**\n * Creates a NetworkEnablementController instance.\n *\n * @param args - The arguments to this function.\n * @param args.messenger - Messenger used to communicate with BaseV2 controller.\n * @param args.state - Initial state to set on this controller.\n */\n constructor({\n messenger,\n state,\n }: {\n messenger: NetworkEnablementControllerMessenger;\n state?: Partial<NetworkEnablementControllerState>;\n }) {\n super({\n messenger,\n metadata,\n name: controllerName,\n state: {\n ...getDefaultNetworkEnablementControllerState(),\n ...state,\n },\n });\n\n messenger.subscribe(\n 'NetworkController:networkAdded',\n ({ chainId, nativeCurrency }) => {\n // eslint-disable-next-line no-void\n void this.#onAddNetwork(chainId, nativeCurrency);\n },\n );\n\n messenger.subscribe('NetworkController:networkRemoved', ({ chainId }) => {\n this.#removeNetworkEntry(chainId);\n });\n }\n\n /**\n * Enables or disables a network for the user.\n *\n * This method accepts either a Hex chain ID (for EVM networks) or a CAIP-2 chain ID\n * (for any blockchain network). The method will automatically convert Hex chain IDs\n * to CAIP-2 format internally. This dual parameter support allows for backward\n * compatibility with existing EVM chain ID formats while supporting newer\n * multi-chain standards.\n *\n * When enabling a non-popular network, this method will disable all other networks\n * to ensure only one network is active at a time (exclusive mode).\n *\n * @param chainId - The chain ID of the network to enable or disable. Can be either:\n * - A Hex string (e.g., '0x1' for Ethereum mainnet) for EVM networks\n * - A CAIP-2 chain ID (e.g., 'eip155:1' for Ethereum mainnet, 'solana:mainnet' for Solana)\n */\n enableNetwork(chainId: Hex | CaipChainId): void {\n const { namespace, storageKey } = deriveKeys(chainId);\n\n this.update((state) => {\n // disable all networks in all namespaces first\n Object.keys(state.enabledNetworkMap).forEach((ns) => {\n Object.keys(state.enabledNetworkMap[ns]).forEach((key) => {\n state.enabledNetworkMap[ns][key as CaipChainId | Hex] = false;\n });\n });\n\n // if the namespace bucket does not exist, return\n // new nemespace are added only when a new network is added\n if (!state.enabledNetworkMap[namespace]) {\n return;\n }\n\n // enable the network\n state.enabledNetworkMap[namespace][storageKey] = true;\n });\n }\n\n /**\n * Enables a network for the user within a specific namespace.\n *\n * This method accepts either a Hex chain ID (for EVM networks) or a CAIP-2 chain ID\n * (for any blockchain network) and enables it within the specified namespace.\n * The method validates that the chainId belongs to the specified namespace for safety.\n *\n * Before enabling the target network, this method disables all other networks\n * in the same namespace to ensure exclusive behavior within the namespace.\n *\n * @param chainId - The chain ID of the network to enable. Can be either:\n * - A Hex string (e.g., '0x1' for Ethereum mainnet) for EVM networks\n * - A CAIP-2 chain ID (e.g., 'eip155:1' for Ethereum mainnet, 'solana:mainnet' for Solana)\n * @param namespace - The CAIP namespace where the network should be enabled\n * @throws Error if the chainId's derived namespace doesn't match the provided namespace\n */\n enableNetworkInNamespace(\n chainId: Hex | CaipChainId,\n namespace: CaipNamespace,\n ): void {\n const { namespace: derivedNamespace, storageKey } = deriveKeys(chainId);\n\n // Validate that the derived namespace matches the provided namespace\n if (derivedNamespace !== namespace) {\n throw new Error(\n `Chain ID ${chainId} belongs to namespace ${derivedNamespace}, but namespace ${namespace} was specified`,\n );\n }\n\n this.update((state) => {\n // Ensure the namespace bucket exists\n this.#ensureNamespaceBucket(state, namespace);\n\n // Disable all networks in the specified namespace first\n if (state.enabledNetworkMap[namespace]) {\n Object.keys(state.enabledNetworkMap[namespace]).forEach((key) => {\n state.enabledNetworkMap[namespace][key as CaipChainId | Hex] = false;\n });\n }\n\n // Enable the target network in the specified namespace\n state.enabledNetworkMap[namespace][storageKey] = true;\n });\n }\n\n /**\n * Enables all popular networks and Solana mainnet.\n *\n * This method first disables all networks across all namespaces, then enables\n * all networks defined in POPULAR_NETWORKS (EVM networks), Solana mainnet, and\n * Bitcoin mainnet. This provides exclusive behavior - only popular networks will\n * be enabled after calling this method.\n *\n * Popular networks that don't exist in NetworkController or MultichainNetworkController configurations will be skipped silently.\n */\n enableAllPopularNetworks(): void {\n this.update((state) => {\n // First disable all networks across all namespaces\n Object.keys(state.enabledNetworkMap).forEach((ns) => {\n Object.keys(state.enabledNetworkMap[ns]).forEach((key) => {\n state.enabledNetworkMap[ns][key as CaipChainId | Hex] = false;\n });\n });\n\n // Get current network configurations to check if networks exist\n const networkControllerState = this.messenger.call(\n 'NetworkController:getState',\n );\n const multichainState = this.messenger.call(\n 'MultichainNetworkController:getState',\n );\n\n // Enable all popular EVM networks that exist in NetworkController configurations\n POPULAR_NETWORKS.forEach((chainId) => {\n const { namespace, storageKey } = deriveKeys(chainId as Hex);\n\n // Check if network exists in NetworkController configurations\n if (\n networkControllerState.networkConfigurationsByChainId[chainId as Hex]\n ) {\n // Ensure namespace bucket exists\n this.#ensureNamespaceBucket(state, namespace);\n // Enable the network\n state.enabledNetworkMap[namespace][storageKey] = true;\n }\n });\n\n // Enable Solana mainnet if it exists in MultichainNetworkController configurations\n const solanaKeys = deriveKeys(SolScope.Mainnet as CaipChainId);\n if (\n multichainState.multichainNetworkConfigurationsByChainId[\n SolScope.Mainnet\n ]\n ) {\n // Ensure namespace bucket exists\n this.#ensureNamespaceBucket(state, solanaKeys.namespace);\n // Enable Solana mainnet\n state.enabledNetworkMap[solanaKeys.namespace][solanaKeys.storageKey] =\n true;\n }\n\n // Enable Bitcoin mainnet if it exists in MultichainNetworkController configurations\n const bitcoinKeys = deriveKeys(BtcScope.Mainnet as CaipChainId);\n if (\n multichainState.multichainNetworkConfigurationsByChainId[\n BtcScope.Mainnet\n ]\n ) {\n // Ensure namespace bucket exists\n this.#ensureNamespaceBucket(state, bitcoinKeys.namespace);\n // Enable Bitcoin mainnet\n state.enabledNetworkMap[bitcoinKeys.namespace][bitcoinKeys.storageKey] =\n true;\n }\n\n // Enable Tron mainnet if it exists in MultichainNetworkController configurations\n const tronKeys = deriveKeys(TrxScope.Mainnet as CaipChainId);\n if (\n multichainState.multichainNetworkConfigurationsByChainId[\n TrxScope.Mainnet\n ]\n ) {\n // Ensure namespace bucket exists\n this.#ensureNamespaceBucket(state, tronKeys.namespace);\n // Enable Tron mainnet\n state.enabledNetworkMap[tronKeys.namespace][tronKeys.storageKey] = true;\n }\n });\n }\n\n /**\n * Initializes the network enablement state from network controller configurations.\n *\n * This method reads the current network configurations from both NetworkController\n * and MultichainNetworkController and syncs the enabled network map and nativeAssetIdentifiers accordingly.\n * It ensures proper namespace buckets exist for all configured networks and only\n * adds missing networks with a default value of false, preserving existing user settings.\n *\n * This method should be called after the NetworkController and MultichainNetworkController\n * have been initialized and their configurations are available.\n */\n async init(): Promise<void> {\n // Get network configurations from NetworkController (EVM networks)\n const networkControllerState = this.messenger.call(\n 'NetworkController:getState',\n );\n\n // Get network configurations from MultichainNetworkController (all networks)\n const multichainState = this.messenger.call(\n 'MultichainNetworkController:getState',\n );\n\n // Build nativeAssetIdentifiers for EVM networks using chainid.network\n const evmNativeAssetUpdates: {\n caipChainId: CaipChainId;\n identifier: NativeAssetIdentifier;\n }[] = [];\n\n for (const [chainId, config] of Object.entries(\n networkControllerState.networkConfigurationsByChainId,\n )) {\n const { caipChainId } = deriveKeys(chainId as Hex);\n\n // Skip if already in state\n if (this.state.nativeAssetIdentifiers[caipChainId] !== undefined) {\n continue;\n }\n\n // Parse hex chainId to number for chainid.network lookup\n const numericChainId = parseInt(chainId, 16);\n\n // EVM networks: use getSlip44ByChainId (chainid.network data)\n // Default to 60 (Ethereum) if no specific mapping is found\n const slip44CoinType =\n (await Slip44Service.getSlip44ByChainId(\n numericChainId,\n config.nativeCurrency,\n )) ?? 60;\n\n evmNativeAssetUpdates.push({\n caipChainId,\n identifier: buildNativeAssetIdentifier(caipChainId, slip44CoinType),\n });\n }\n\n // Update state synchronously\n this.update((state) => {\n // Initialize namespace buckets for EVM networks from NetworkController\n Object.entries(\n networkControllerState.networkConfigurationsByChainId,\n ).forEach(([chainId]) => {\n const { namespace, storageKey } = deriveKeys(chainId as Hex);\n this.#ensureNamespaceBucket(state, namespace);\n\n // Only add network if it doesn't already exist in state (preserves user settings)\n state.enabledNetworkMap[namespace][storageKey] ??= false;\n });\n\n // Apply nativeAssetIdentifier updates\n for (const { caipChainId, identifier } of evmNativeAssetUpdates) {\n state.nativeAssetIdentifiers[caipChainId] = identifier;\n }\n\n // Initialize namespace buckets for all networks from MultichainNetworkController\n Object.keys(\n multichainState.multichainNetworkConfigurationsByChainId,\n ).forEach((chainId) => {\n const { namespace, storageKey } = deriveKeys(chainId as CaipChainId);\n this.#ensureNamespaceBucket(state, namespace);\n\n // Only add network if it doesn't already exist in state (preserves user settings)\n state.enabledNetworkMap[namespace][storageKey] ??= false;\n });\n });\n }\n\n /**\n * Initializes the native asset identifiers from network configurations.\n * This method should be called from the client during controller initialization\n * to populate the nativeAssetIdentifiers state based on actual network configurations.\n *\n * @param networks - Array of network configurations with chainId and nativeCurrency\n * @example\n * ```typescript\n * const evmNetworks = Object.values(networkControllerState.networkConfigurationsByChainId)\n * .map(config => ({\n * chainId: toEvmCaipChainId(config.chainId),\n * nativeCurrency: config.nativeCurrency,\n * }));\n *\n * const multichainNetworks = Object.values(multichainState.multichainNetworkConfigurationsByChainId)\n * .map(config => ({\n * chainId: config.chainId,\n * nativeCurrency: config.nativeCurrency,\n * }));\n *\n * await controller.initNativeAssetIdentifiers([...evmNetworks, ...multichainNetworks]);\n * ```\n */\n async initNativeAssetIdentifiers(networks: NetworkConfig[]): Promise<void> {\n // Process networks and collect updates\n const updates: {\n chainId: CaipChainId;\n identifier: NativeAssetIdentifier;\n }[] = [];\n\n for (const { chainId, nativeCurrency } of networks) {\n // Check if nativeCurrency is already in CAIP-19 format (e.g., \"bip122:.../slip44:0\")\n // Non-EVM networks from MultichainNetworkController use this format\n if (nativeCurrency.includes('/slip44:')) {\n updates.push({\n chainId,\n identifier: nativeCurrency as NativeAssetIdentifier,\n });\n continue;\n }\n\n // Extract namespace from CAIP-2 chainId\n const [namespace, reference] = chainId.split(':');\n let slip44CoinType: number | undefined;\n\n if (namespace === 'eip155') {\n // EVM networks: use getSlip44ByChainId (chainid.network data)\n // Default to 60 (Ethereum) if no specific mapping is found\n const numericChainId = parseInt(reference, 10);\n slip44CoinType =\n (await Slip44Service.getSlip44ByChainId(\n numericChainId,\n nativeCurrency,\n )) ?? 60;\n } else {\n // Non-EVM networks: use getSlip44BySymbol (@metamask/slip44 package)\n slip44CoinType = Slip44Service.getSlip44BySymbol(nativeCurrency);\n }\n\n if (slip44CoinType !== undefined) {\n updates.push({\n chainId,\n identifier: buildNativeAssetIdentifier(chainId, slip44CoinType),\n });\n }\n }\n\n // Apply all updates synchronously\n this.update((state) => {\n for (const { chainId, identifier } of updates) {\n state.nativeAssetIdentifiers[chainId] = identifier;\n }\n });\n }\n\n /**\n * Disables a network for the user.\n *\n * This method accepts either a Hex chain ID (for EVM networks) or a CAIP-2 chain ID\n * (for any blockchain network). The method will automatically convert Hex chain IDs\n * to CAIP-2 format internally.\n *\n * Note: This method will prevent disabling the last remaining enabled network\n * to ensure at least one network is always available.\n *\n * @param chainId - The chain ID of the network to disable. Can be either:\n * - A Hex string (e.g., '0x1' for Ethereum mainnet) for EVM networks\n * - A CAIP-2 chain ID (e.g., 'eip155:1' for Ethereum mainnet, 'solana:mainnet' for Solana)\n */\n disableNetwork(chainId: Hex | CaipChainId): void {\n const derivedKeys = deriveKeys(chainId);\n const { namespace, storageKey } = derivedKeys;\n\n this.update((state) => {\n state.enabledNetworkMap[namespace][storageKey] = false;\n });\n }\n\n /**\n * Checks if a network is enabled.\n *\n * @param chainId - The chain ID of the network to check. Can be either:\n * - A Hex string (e.g., '0x1' for Ethereum mainnet) for EVM networks\n * - A CAIP-2 chain ID (e.g., 'eip155:1' for Ethereum mainnet, 'solana:mainnet' for Solana)\n * @returns True if the network is enabled, false otherwise\n */\n isNetworkEnabled(chainId: Hex | CaipChainId): boolean {\n const derivedKeys = deriveKeys(chainId);\n const { namespace, storageKey } = derivedKeys;\n return this.state.enabledNetworkMap[namespace]?.[storageKey] ?? false;\n }\n\n /**\n * Ensures that a namespace bucket exists in the state.\n *\n * This method creates the namespace entry in the enabledNetworkMap if it doesn't\n * already exist. This is used to prepare the state structure before adding\n * network entries.\n *\n * @param state - The current controller state\n * @param ns - The CAIP namespace to ensure exists\n */\n #ensureNamespaceBucket(\n state: NetworkEnablementControllerState,\n ns: CaipNamespace,\n ): void {\n if (!state.enabledNetworkMap[ns]) {\n state.enabledNetworkMap[ns] = {};\n }\n }\n\n /**\n * Checks if popular networks mode is active (more than 2 popular networks enabled).\n *\n * This method counts how many networks defined in POPULAR_NETWORKS are currently\n * enabled in the state and returns true if more than 2 are enabled. It only checks\n * networks that actually exist in the NetworkController configurations.\n *\n * @returns True if more than 2 popular networks are enabled, false otherwise\n */\n #isInPopularNetworksMode(): boolean {\n // Get current network configurations to check which popular networks exist\n const networkControllerState = this.messenger.call(\n 'NetworkController:getState',\n );\n\n // Count how many popular networks are enabled\n const enabledPopularNetworksCount = POPULAR_NETWORKS.reduce(\n (count, chainId) => {\n // Only check networks that actually exist in NetworkController configurations\n if (\n !networkControllerState.networkConfigurationsByChainId[chainId as Hex]\n ) {\n return count; // Skip networks that don't exist\n }\n\n const { namespace, storageKey } = deriveKeys(chainId as Hex);\n const isEnabled = this.state.enabledNetworkMap[namespace]?.[storageKey];\n return isEnabled ? count + 1 : count;\n },\n 0,\n );\n\n // Return true if more than 2 popular networks are enabled\n return enabledPopularNetworksCount > 1;\n }\n\n /**\n * Removes a network entry from the state.\n *\n * This method is called when a network is removed from the system. It cleans up\n * the network entry from both enabledNetworkMap and nativeAssetIdentifiers, and ensures that\n * at least one network remains enabled.\n *\n * @param chainId - The chain ID to remove (Hex or CAIP-2 format)\n */\n #removeNetworkEntry(chainId: Hex | CaipChainId): void {\n const derivedKeys = deriveKeys(chainId);\n const { namespace, storageKey, caipChainId } = derivedKeys;\n\n this.update((state) => {\n // fallback and enable ethereum mainnet\n if (isOnlyNetworkEnabledInNamespace(this.state, derivedKeys)) {\n state.enabledNetworkMap[namespace][\n ChainId[BuiltInNetworkName.Mainnet]\n ] = true;\n }\n\n if (namespace in state.enabledNetworkMap) {\n delete state.enabledNetworkMap[namespace][storageKey];\n }\n\n // Remove from nativeAssetIdentifiers as well\n delete state.nativeAssetIdentifiers[caipChainId];\n });\n }\n\n /**\n * Handles the addition of a new EVM network to the controller.\n *\n * @param chainId - The chain ID to add (Hex format)\n * @param nativeCurrency - The native currency symbol of the network (e.g., 'ETH')\n *\n * @description\n * - If in popular networks mode (>2 popular networks enabled) AND adding a popular network:\n * - Keep current selection (add but don't enable the new network)\n * - Otherwise:\n * - Switch to the newly added network (disable all others, enable this one)\n * - Also updates the nativeAssetIdentifiers with the CAIP-19-like identifier\n */\n async #onAddNetwork(chainId: Hex, nativeCurrency: string): Promise<void> {\n const { namespace, storageKey, reference, caipChainId } =\n deriveKeys(chainId);\n\n // Parse hex chainId to number for chainid.network lookup\n const numericChainId = parseInt(reference, 16);\n\n // EVM networks: use getSlip44ByChainId (chainid.network data)\n // Default to 60 (Ethereum) if no specific mapping is found\n const slip44CoinType =\n (await Slip44Service.getSlip44ByChainId(\n numericChainId,\n nativeCurrency,\n )) ?? 60;\n\n this.update((state) => {\n // Ensure the namespace bucket exists\n this.#ensureNamespaceBucket(state, namespace);\n\n // Check if popular networks mode is active (>2 popular networks enabled)\n const inPopularNetworksMode = this.#isInPopularNetworksMode();\n\n // Check if the network being added is a popular network\n const isAddedNetworkPopular = isPopularNetwork(reference);\n\n // Keep current selection only if in popular networks mode AND adding a popular network\n const shouldKeepCurrentSelection =\n inPopularNetworksMode && isAddedNetworkPopular;\n\n if (shouldKeepCurrentSelection) {\n // Add the popular network but don't enable it (keep current selection)\n state.enabledNetworkMap[namespace][storageKey] = true;\n } else {\n // Switch to the newly added network (disable all others, enable this one)\n Object.keys(state.enabledNetworkMap).forEach((ns) => {\n Object.keys(state.enabledNetworkMap[ns]).forEach((key) => {\n state.enabledNetworkMap[ns][key as CaipChainId | Hex] = false;\n });\n });\n // Enable the newly added network\n state.enabledNetworkMap[namespace][storageKey] = true;\n }\n\n // Update nativeAssetIdentifiers with the CAIP-19-like identifier\n state.nativeAssetIdentifiers[caipChainId] = buildNativeAssetIdentifier(\n caipChainId,\n slip44CoinType,\n );\n });\n }\n}\n"]}
|