@metamask-previews/network-enablement-controller 6.0.5-preview-8bfa290fb → 7.0.0-preview-0a30e47

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.
Files changed (85) hide show
  1. package/CHANGELOG.md +16 -2
  2. package/dist/{NetworkEnablementController-method-action-types.d.mts → NetworkEnablementController-method-action-types.d.ts} +2 -2
  3. package/dist/NetworkEnablementController-method-action-types.d.ts.map +1 -0
  4. package/dist/{NetworkEnablementController-method-action-types.mjs → NetworkEnablementController-method-action-types.js} +1 -1
  5. package/dist/NetworkEnablementController-method-action-types.js.map +1 -0
  6. package/dist/{NetworkEnablementController.d.cts → NetworkEnablementController.d.ts} +10 -10
  7. package/dist/NetworkEnablementController.d.ts.map +1 -0
  8. package/dist/{NetworkEnablementController.mjs → NetworkEnablementController.js} +167 -131
  9. package/dist/NetworkEnablementController.js.map +1 -0
  10. package/dist/constants.d.ts +3 -0
  11. package/dist/constants.d.ts.map +1 -0
  12. package/dist/{constants.mjs → constants.js} +1 -1
  13. package/dist/constants.js.map +1 -0
  14. package/dist/{index.d.cts → index.d.ts} +6 -6
  15. package/dist/index.d.ts.map +1 -0
  16. package/dist/index.js +4 -0
  17. package/dist/index.js.map +1 -0
  18. package/dist/{selectors.d.cts → selectors.d.ts} +18 -30
  19. package/dist/selectors.d.ts.map +1 -0
  20. package/dist/{selectors.mjs → selectors.js} +4 -4
  21. package/dist/selectors.js.map +1 -0
  22. package/dist/services/{Slip44Service.d.cts → Slip44Service.d.ts} +1 -1
  23. package/dist/services/Slip44Service.d.ts.map +1 -0
  24. package/dist/services/Slip44Service.js +167 -0
  25. package/dist/services/Slip44Service.js.map +1 -0
  26. package/dist/services/{index.d.cts → index.d.ts} +2 -2
  27. package/dist/services/index.d.ts.map +1 -0
  28. package/dist/services/{index.mjs → index.js} +2 -2
  29. package/dist/services/index.js.map +1 -0
  30. package/dist/{utils.d.mts → utils.d.ts} +3 -3
  31. package/dist/utils.d.ts.map +1 -0
  32. package/dist/{utils.mjs → utils.js} +4 -4
  33. package/dist/utils.js.map +1 -0
  34. package/package.json +19 -23
  35. package/dist/NetworkEnablementController-method-action-types.cjs +0 -7
  36. package/dist/NetworkEnablementController-method-action-types.cjs.map +0 -1
  37. package/dist/NetworkEnablementController-method-action-types.d.cts +0 -185
  38. package/dist/NetworkEnablementController-method-action-types.d.cts.map +0 -1
  39. package/dist/NetworkEnablementController-method-action-types.d.mts.map +0 -1
  40. package/dist/NetworkEnablementController-method-action-types.mjs.map +0 -1
  41. package/dist/NetworkEnablementController.cjs +0 -590
  42. package/dist/NetworkEnablementController.cjs.map +0 -1
  43. package/dist/NetworkEnablementController.d.cts.map +0 -1
  44. package/dist/NetworkEnablementController.d.mts +0 -234
  45. package/dist/NetworkEnablementController.d.mts.map +0 -1
  46. package/dist/NetworkEnablementController.mjs.map +0 -1
  47. package/dist/constants.cjs +0 -22
  48. package/dist/constants.cjs.map +0 -1
  49. package/dist/constants.d.cts +0 -3
  50. package/dist/constants.d.cts.map +0 -1
  51. package/dist/constants.d.mts +0 -3
  52. package/dist/constants.d.mts.map +0 -1
  53. package/dist/constants.mjs.map +0 -1
  54. package/dist/index.cjs +0 -18
  55. package/dist/index.cjs.map +0 -1
  56. package/dist/index.d.cts.map +0 -1
  57. package/dist/index.d.mts +0 -6
  58. package/dist/index.d.mts.map +0 -1
  59. package/dist/index.mjs +0 -4
  60. package/dist/index.mjs.map +0 -1
  61. package/dist/selectors.cjs +0 -87
  62. package/dist/selectors.cjs.map +0 -1
  63. package/dist/selectors.d.cts.map +0 -1
  64. package/dist/selectors.d.mts +0 -280
  65. package/dist/selectors.d.mts.map +0 -1
  66. package/dist/selectors.mjs.map +0 -1
  67. package/dist/services/Slip44Service.cjs +0 -182
  68. package/dist/services/Slip44Service.cjs.map +0 -1
  69. package/dist/services/Slip44Service.d.cts.map +0 -1
  70. package/dist/services/Slip44Service.d.mts +0 -63
  71. package/dist/services/Slip44Service.d.mts.map +0 -1
  72. package/dist/services/Slip44Service.mjs +0 -175
  73. package/dist/services/Slip44Service.mjs.map +0 -1
  74. package/dist/services/index.cjs +0 -11
  75. package/dist/services/index.cjs.map +0 -1
  76. package/dist/services/index.d.cts.map +0 -1
  77. package/dist/services/index.d.mts +0 -5
  78. package/dist/services/index.d.mts.map +0 -1
  79. package/dist/services/index.mjs.map +0 -1
  80. package/dist/utils.cjs +0 -62
  81. package/dist/utils.cjs.map +0 -1
  82. package/dist/utils.d.cts +0 -36
  83. package/dist/utils.d.cts.map +0 -1
  84. package/dist/utils.d.mts.map +0 -1
  85. package/dist/utils.mjs.map +0 -1
@@ -1,590 +0,0 @@
1
- "use strict";
2
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
3
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
4
- 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");
5
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
6
- };
7
- var _NetworkEnablementController_instances, _NetworkEnablementController_ensureNamespaceBucket, _NetworkEnablementController_isInPopularNetworksMode, _NetworkEnablementController_removeNetworkEntry, _NetworkEnablementController_onAddNetwork, _NetworkEnablementController_getRegistryPopularEvmChainIds, _NetworkEnablementController_getPopularEvmChainIds;
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.NetworkEnablementController = void 0;
10
- const base_controller_1 = require("@metamask/base-controller");
11
- const controller_utils_1 = require("@metamask/controller-utils");
12
- const keyring_api_1 = require("@metamask/keyring-api");
13
- const multichain_network_controller_1 = require("@metamask/multichain-network-controller");
14
- const utils_1 = require("@metamask/utils");
15
- const constants_js_1 = require("./constants.cjs");
16
- const index_js_1 = require("./services/index.cjs");
17
- const utils_js_1 = require("./utils.cjs");
18
- const controllerName = 'NetworkEnablementController';
19
- const MESSENGER_EXPOSED_METHODS = [
20
- 'init',
21
- 'initNativeAssetIdentifiers',
22
- 'enableNetwork',
23
- 'disableNetwork',
24
- 'enableNetworkInNamespace',
25
- 'enableAllPopularNetworks',
26
- 'isNetworkEnabled',
27
- 'listPopularNetworks',
28
- 'listPopularEvmNetworks',
29
- 'listPopularMultichainNetworks',
30
- 'restoreEnabledNetworkMap',
31
- ];
32
- /**
33
- * Builds a native asset identifier in CAIP-19-like format.
34
- *
35
- * @param caipChainId - The CAIP-2 chain ID (e.g., 'eip155:1', 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp')
36
- * @param slip44CoinType - The SLIP-44 coin type number
37
- * @returns The native asset identifier string (e.g., 'eip155:1/slip44:60')
38
- */
39
- function buildNativeAssetIdentifier(caipChainId, slip44CoinType) {
40
- return `${caipChainId}/slip44:${slip44CoinType}`;
41
- }
42
- /**
43
- * Gets the default state for the NetworkEnablementController.
44
- *
45
- * @returns The default state with pre-enabled networks.
46
- */
47
- const getDefaultNetworkEnablementControllerState = () => ({
48
- enabledNetworkMap: {
49
- [utils_1.KnownCaipNamespace.Eip155]: {
50
- [controller_utils_1.ChainId[controller_utils_1.BuiltInNetworkName.Mainnet]]: true,
51
- [controller_utils_1.ChainId[controller_utils_1.BuiltInNetworkName.LineaMainnet]]: true,
52
- [controller_utils_1.ChainId[controller_utils_1.BuiltInNetworkName.BaseMainnet]]: true,
53
- [controller_utils_1.ChainId[controller_utils_1.BuiltInNetworkName.ArbitrumOne]]: true,
54
- [controller_utils_1.ChainId[controller_utils_1.BuiltInNetworkName.BscMainnet]]: true,
55
- [controller_utils_1.ChainId[controller_utils_1.BuiltInNetworkName.OptimismMainnet]]: true,
56
- [controller_utils_1.ChainId[controller_utils_1.BuiltInNetworkName.PolygonMainnet]]: true,
57
- [controller_utils_1.ChainId[controller_utils_1.BuiltInNetworkName.SeiMainnet]]: true,
58
- [controller_utils_1.ChainId[controller_utils_1.BuiltInNetworkName.MonadMainnet]]: true,
59
- },
60
- [utils_1.KnownCaipNamespace.Solana]: {
61
- [keyring_api_1.SolScope.Mainnet]: true,
62
- [keyring_api_1.SolScope.Testnet]: false,
63
- [keyring_api_1.SolScope.Devnet]: false,
64
- },
65
- [utils_1.KnownCaipNamespace.Bip122]: {
66
- [keyring_api_1.BtcScope.Mainnet]: true,
67
- [keyring_api_1.BtcScope.Testnet]: false,
68
- [keyring_api_1.BtcScope.Signet]: false,
69
- },
70
- [utils_1.KnownCaipNamespace.Tron]: {
71
- [keyring_api_1.TrxScope.Mainnet]: true,
72
- [keyring_api_1.TrxScope.Nile]: false,
73
- [keyring_api_1.TrxScope.Shasta]: false,
74
- },
75
- [utils_1.KnownCaipNamespace.Stellar]: {
76
- [keyring_api_1.XlmScope.Pubnet]: true,
77
- [keyring_api_1.XlmScope.Testnet]: false,
78
- },
79
- },
80
- // nativeAssetIdentifiers is initialized as empty and should be populated
81
- // by the client using initNativeAssetIdentifiers() during controller init
82
- nativeAssetIdentifiers: {},
83
- });
84
- // Metadata for the controller state
85
- const metadata = {
86
- enabledNetworkMap: {
87
- includeInStateLogs: true,
88
- persist: true,
89
- includeInDebugSnapshot: true,
90
- usedInUi: true,
91
- },
92
- nativeAssetIdentifiers: {
93
- includeInStateLogs: true,
94
- persist: true,
95
- includeInDebugSnapshot: true,
96
- usedInUi: true,
97
- },
98
- };
99
- /**
100
- * Controller responsible for managing network enablement state across different blockchain networks.
101
- *
102
- * This controller tracks which networks are enabled/disabled for the user and provides methods
103
- * to toggle network states. It supports both EVM (EIP-155) and non-EVM networks like Solana.
104
- *
105
- * The controller maintains a map of enabled networks organized by namespace (e.g., 'eip155', 'solana')
106
- * and provides methods to query and modify network enablement states.
107
- */
108
- class NetworkEnablementController extends base_controller_1.BaseController {
109
- /**
110
- * Creates a NetworkEnablementController instance.
111
- *
112
- * @param args - The arguments to this function.
113
- * @param args.messenger - Messenger used to communicate with BaseV2 controller.
114
- * @param args.state - Initial state to set on this controller.
115
- */
116
- constructor({ messenger, state, }) {
117
- super({
118
- messenger,
119
- metadata,
120
- name: controllerName,
121
- state: {
122
- ...getDefaultNetworkEnablementControllerState(),
123
- ...state,
124
- },
125
- });
126
- _NetworkEnablementController_instances.add(this);
127
- messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
128
- messenger.subscribe('NetworkController:networkAdded', ({ chainId }) => {
129
- // eslint-disable-next-line no-void
130
- void __classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_onAddNetwork).call(this, chainId);
131
- });
132
- messenger.subscribe('NetworkController:networkRemoved', ({ chainId }) => {
133
- __classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_removeNetworkEntry).call(this, chainId);
134
- });
135
- }
136
- /**
137
- * Enables or disables a network for the user.
138
- *
139
- * This method accepts either a Hex chain ID (for EVM networks) or a CAIP-2 chain ID
140
- * (for any blockchain network). The method will automatically convert Hex chain IDs
141
- * to CAIP-2 format internally. This dual parameter support allows for backward
142
- * compatibility with existing EVM chain ID formats while supporting newer
143
- * multi-chain standards.
144
- *
145
- * When enabling a non-popular network, this method will disable all other networks
146
- * to ensure only one network is active at a time (exclusive mode).
147
- *
148
- * @param chainId - The chain ID of the network to enable or disable. Can be either:
149
- * - A Hex string (e.g., '0x1' for Ethereum mainnet) for EVM networks
150
- * - A CAIP-2 chain ID (e.g., 'eip155:1' for Ethereum mainnet, 'solana:mainnet' for Solana)
151
- */
152
- enableNetwork(chainId) {
153
- const { namespace, storageKey } = (0, utils_js_1.deriveKeys)(chainId);
154
- this.update((state) => {
155
- // disable all networks in all namespaces first
156
- Object.keys(state.enabledNetworkMap).forEach((ns) => {
157
- Object.keys(state.enabledNetworkMap[ns]).forEach((key) => {
158
- state.enabledNetworkMap[ns][key] = false;
159
- });
160
- });
161
- // if the namespace bucket does not exist, return
162
- // new nemespace are added only when a new network is added
163
- if (!state.enabledNetworkMap[namespace]) {
164
- return;
165
- }
166
- // enable the network
167
- state.enabledNetworkMap[namespace][storageKey] = true;
168
- });
169
- }
170
- /**
171
- * Enables a network for the user within a specific namespace.
172
- *
173
- * This method accepts either a Hex chain ID (for EVM networks) or a CAIP-2 chain ID
174
- * (for any blockchain network) and enables it within the specified namespace.
175
- * The method validates that the chainId belongs to the specified namespace for safety.
176
- *
177
- * Before enabling the target network, this method disables all other networks
178
- * in the same namespace to ensure exclusive behavior within the namespace.
179
- *
180
- * @param chainId - The chain ID of the network to enable. Can be either:
181
- * - A Hex string (e.g., '0x1' for Ethereum mainnet) for EVM networks
182
- * - A CAIP-2 chain ID (e.g., 'eip155:1' for Ethereum mainnet, 'solana:mainnet' for Solana)
183
- * @param namespace - The CAIP namespace where the network should be enabled
184
- * @throws Error if the chainId's derived namespace doesn't match the provided namespace
185
- */
186
- enableNetworkInNamespace(chainId, namespace) {
187
- const { namespace: derivedNamespace, storageKey } = (0, utils_js_1.deriveKeys)(chainId);
188
- // Validate that the derived namespace matches the provided namespace
189
- if (derivedNamespace !== namespace) {
190
- throw new Error(`Chain ID ${chainId} belongs to namespace ${derivedNamespace}, but namespace ${namespace} was specified`);
191
- }
192
- this.update((state) => {
193
- // Ensure the namespace bucket exists
194
- __classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this, state, namespace);
195
- // Disable all networks in the specified namespace first
196
- if (state.enabledNetworkMap[namespace]) {
197
- Object.keys(state.enabledNetworkMap[namespace]).forEach((key) => {
198
- state.enabledNetworkMap[namespace][key] = false;
199
- });
200
- }
201
- // Enable the target network in the specified namespace
202
- state.enabledNetworkMap[namespace][storageKey] = true;
203
- });
204
- }
205
- /**
206
- * Enables all popular networks and Solana mainnet.
207
- *
208
- * This method first disables all networks across all namespaces, then enables
209
- * all networks defined in POPULAR_NETWORKS (EVM networks), Solana mainnet, and
210
- * Bitcoin mainnet. This provides exclusive behavior - only popular networks will
211
- * be enabled after calling this method.
212
- *
213
- * Popular networks that don't exist in NetworkController or MultichainNetworkController configurations will be skipped silently.
214
- */
215
- enableAllPopularNetworks() {
216
- this.update((state) => {
217
- // First disable all networks across all namespaces
218
- Object.keys(state.enabledNetworkMap).forEach((ns) => {
219
- Object.keys(state.enabledNetworkMap[ns]).forEach((key) => {
220
- state.enabledNetworkMap[ns][key] = false;
221
- });
222
- });
223
- // Get current network configurations to check if networks exist
224
- const networkControllerState = this.messenger.call('NetworkController:getState');
225
- const multichainState = this.messenger.call('MultichainNetworkController:getState');
226
- // Enable all popular EVM networks that exist in NetworkController configurations
227
- __classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_getPopularEvmChainIds).call(this).forEach((chainId) => {
228
- const { namespace, storageKey } = (0, utils_js_1.deriveKeys)(chainId);
229
- // Check if network exists in NetworkController configurations
230
- if (networkControllerState.networkConfigurationsByChainId[chainId]) {
231
- // Ensure namespace bucket exists
232
- __classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this, state, namespace);
233
- // Enable the network
234
- state.enabledNetworkMap[namespace][storageKey] = true;
235
- }
236
- });
237
- // Enable Solana mainnet if it exists in MultichainNetworkController configurations
238
- const solanaKeys = (0, utils_js_1.deriveKeys)(keyring_api_1.SolScope.Mainnet);
239
- if (multichainState.multichainNetworkConfigurationsByChainId[keyring_api_1.SolScope.Mainnet]) {
240
- // Ensure namespace bucket exists
241
- __classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this, state, solanaKeys.namespace);
242
- // Enable Solana mainnet
243
- state.enabledNetworkMap[solanaKeys.namespace][solanaKeys.storageKey] =
244
- true;
245
- }
246
- // Enable Bitcoin mainnet if it exists in MultichainNetworkController configurations
247
- const bitcoinKeys = (0, utils_js_1.deriveKeys)(keyring_api_1.BtcScope.Mainnet);
248
- if (multichainState.multichainNetworkConfigurationsByChainId[keyring_api_1.BtcScope.Mainnet]) {
249
- // Ensure namespace bucket exists
250
- __classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this, state, bitcoinKeys.namespace);
251
- // Enable Bitcoin mainnet
252
- state.enabledNetworkMap[bitcoinKeys.namespace][bitcoinKeys.storageKey] =
253
- true;
254
- }
255
- // Enable Tron mainnet if it exists in MultichainNetworkController configurations
256
- const tronKeys = (0, utils_js_1.deriveKeys)(keyring_api_1.TrxScope.Mainnet);
257
- if (multichainState.multichainNetworkConfigurationsByChainId[keyring_api_1.TrxScope.Mainnet]) {
258
- // Ensure namespace bucket exists
259
- __classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this, state, tronKeys.namespace);
260
- // Enable Tron mainnet
261
- state.enabledNetworkMap[tronKeys.namespace][tronKeys.storageKey] = true;
262
- }
263
- // Enable Stellar mainnet if it exists in MultichainNetworkController configurations
264
- const stellarKeys = (0, utils_js_1.deriveKeys)(keyring_api_1.XlmScope.Pubnet);
265
- if (multichainState.multichainNetworkConfigurationsByChainId[keyring_api_1.XlmScope.Pubnet]) {
266
- __classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this, state, stellarKeys.namespace);
267
- state.enabledNetworkMap[stellarKeys.namespace][stellarKeys.storageKey] =
268
- true;
269
- }
270
- });
271
- }
272
- /**
273
- * Initializes the network enablement state from network controller configurations.
274
- *
275
- * This method reads the current network configurations from both NetworkController
276
- * and MultichainNetworkController and syncs the enabled network map and nativeAssetIdentifiers accordingly.
277
- * It ensures proper namespace buckets exist for all configured networks and only
278
- * adds missing networks with a default value of false, preserving existing user settings.
279
- *
280
- * This method should be called after the NetworkController and MultichainNetworkController
281
- * have been initialized and their configurations are available.
282
- */
283
- async init() {
284
- // Get network configurations from NetworkController (EVM networks)
285
- const networkControllerState = this.messenger.call('NetworkController:getState');
286
- // Get network configurations from MultichainNetworkController (all networks)
287
- const multichainState = this.messenger.call('MultichainNetworkController:getState');
288
- // Build nativeAssetIdentifiers for EVM networks using chainid.network
289
- const evmNativeAssetUpdates = [];
290
- for (const [chainId] of Object.entries(networkControllerState.networkConfigurationsByChainId)) {
291
- const { caipChainId } = (0, utils_js_1.deriveKeys)(chainId);
292
- // Skip if already in state
293
- if (this.state.nativeAssetIdentifiers[caipChainId] !== undefined) {
294
- continue;
295
- }
296
- // Parse hex chainId to number for chainid.network lookup
297
- const numericChainId = parseInt(chainId, 16);
298
- // EVM networks: use getEvmSlip44 (chainid.network data)
299
- const slip44CoinType = await index_js_1.Slip44Service.getEvmSlip44(numericChainId);
300
- evmNativeAssetUpdates.push({
301
- caipChainId,
302
- identifier: buildNativeAssetIdentifier(caipChainId, slip44CoinType),
303
- });
304
- }
305
- // Update state synchronously
306
- this.update((state) => {
307
- // Initialize namespace buckets for EVM networks from NetworkController
308
- Object.entries(networkControllerState.networkConfigurationsByChainId).forEach(([chainId]) => {
309
- var _a;
310
- const { namespace, storageKey } = (0, utils_js_1.deriveKeys)(chainId);
311
- __classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this, state, namespace);
312
- // Only add network if it doesn't already exist in state (preserves user settings)
313
- (_a = state.enabledNetworkMap[namespace])[storageKey] ?? (_a[storageKey] = false);
314
- });
315
- // Apply nativeAssetIdentifier updates
316
- for (const { caipChainId, identifier } of evmNativeAssetUpdates) {
317
- state.nativeAssetIdentifiers[caipChainId] = identifier;
318
- }
319
- // Initialize namespace buckets for all networks from MultichainNetworkController
320
- Object.keys(multichainState.multichainNetworkConfigurationsByChainId).forEach((chainId) => {
321
- var _a;
322
- const { namespace, storageKey } = (0, utils_js_1.deriveKeys)(chainId);
323
- __classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this, state, namespace);
324
- // Only add network if it doesn't already exist in state (preserves user settings)
325
- (_a = state.enabledNetworkMap[namespace])[storageKey] ?? (_a[storageKey] = false);
326
- });
327
- });
328
- }
329
- /**
330
- * Initializes the native asset identifiers from network configurations.
331
- * This method should be called from the client during controller initialization
332
- * to populate the nativeAssetIdentifiers state based on actual network configurations.
333
- *
334
- * @param networks - Array of network configurations with chainId and nativeCurrency
335
- * @example
336
- * ```typescript
337
- * const evmNetworks = Object.values(networkControllerState.networkConfigurationsByChainId)
338
- * .map(config => ({
339
- * chainId: toEvmCaipChainId(config.chainId),
340
- * nativeCurrency: config.nativeCurrency,
341
- * }));
342
- *
343
- * const multichainNetworks = Object.values(multichainState.multichainNetworkConfigurationsByChainId)
344
- * .map(config => ({
345
- * chainId: config.chainId,
346
- * nativeCurrency: config.nativeCurrency,
347
- * }));
348
- *
349
- * await controller.initNativeAssetIdentifiers([...evmNetworks, ...multichainNetworks]);
350
- * ```
351
- */
352
- async initNativeAssetIdentifiers(networks) {
353
- // Process networks and collect updates
354
- const updates = [];
355
- for (const { chainId, nativeCurrency } of networks) {
356
- // Check if nativeCurrency is already in CAIP-19 format (e.g., "bip122:.../slip44:0")
357
- // Non-EVM networks from MultichainNetworkController use this format
358
- if (nativeCurrency.includes('/slip44:')) {
359
- updates.push({
360
- chainId,
361
- identifier: nativeCurrency,
362
- });
363
- continue;
364
- }
365
- // Extract namespace from CAIP-2 chainId
366
- const [namespace, reference] = chainId.split(':');
367
- let slip44CoinType;
368
- if (namespace === 'eip155') {
369
- // EVM networks: use getEvmSlip44 (chainid.network data)
370
- const numericChainId = parseInt(reference, 10);
371
- slip44CoinType = await index_js_1.Slip44Service.getEvmSlip44(numericChainId);
372
- }
373
- else {
374
- // Non-EVM networks: use getSlip44BySymbol (@metamask/slip44 package)
375
- slip44CoinType = index_js_1.Slip44Service.getSlip44BySymbol(nativeCurrency);
376
- }
377
- if (slip44CoinType !== undefined) {
378
- updates.push({
379
- chainId,
380
- identifier: buildNativeAssetIdentifier(chainId, slip44CoinType),
381
- });
382
- }
383
- }
384
- // Apply all updates synchronously
385
- this.update((state) => {
386
- for (const { chainId, identifier } of updates) {
387
- state.nativeAssetIdentifiers[chainId] = identifier;
388
- }
389
- });
390
- }
391
- /**
392
- * Disables a network for the user.
393
- *
394
- * This method accepts either a Hex chain ID (for EVM networks) or a CAIP-2 chain ID
395
- * (for any blockchain network). The method will automatically convert Hex chain IDs
396
- * to CAIP-2 format internally.
397
- *
398
- * Note: This method will prevent disabling the last remaining enabled network
399
- * to ensure at least one network is always available.
400
- *
401
- * @param chainId - The chain ID of the network to disable. Can be either:
402
- * - A Hex string (e.g., '0x1' for Ethereum mainnet) for EVM networks
403
- * - A CAIP-2 chain ID (e.g., 'eip155:1' for Ethereum mainnet, 'solana:mainnet' for Solana)
404
- */
405
- disableNetwork(chainId) {
406
- const derivedKeys = (0, utils_js_1.deriveKeys)(chainId);
407
- const { namespace, storageKey } = derivedKeys;
408
- this.update((state) => {
409
- state.enabledNetworkMap[namespace][storageKey] = false;
410
- });
411
- }
412
- /**
413
- * Restores the enabled network map to a previously snapshotted state.
414
- *
415
- * Not a general merge API: only updates keys already present in the current
416
- * map. Missing snapshot values default to `false`. Intended for callers with
417
- * direct controller access (e.g. extension) to undo `#onAddNetwork` filter
418
- * switches when adding a network without changing the active selection.
419
- *
420
- * @param enabledNetworkMap - Previously snapshotted enabledNetworkMap.
421
- */
422
- restoreEnabledNetworkMap(enabledNetworkMap) {
423
- this.update((state) => {
424
- Object.entries(state.enabledNetworkMap).forEach(([namespace, currentNetworks]) => {
425
- Object.keys(currentNetworks).forEach((chainId) => {
426
- const storageKey = chainId;
427
- const previousValue = enabledNetworkMap[namespace]?.[storageKey];
428
- state.enabledNetworkMap[namespace][storageKey] =
429
- previousValue ?? false;
430
- });
431
- });
432
- });
433
- }
434
- /**
435
- * Checks if a network is enabled.
436
- *
437
- * @param chainId - The chain ID of the network to check. Can be either:
438
- * - A Hex string (e.g., '0x1' for Ethereum mainnet) for EVM networks
439
- * - A CAIP-2 chain ID (e.g., 'eip155:1' for Ethereum mainnet, 'solana:mainnet' for Solana)
440
- * @returns True if the network is enabled, false otherwise
441
- */
442
- isNetworkEnabled(chainId) {
443
- const derivedKeys = (0, utils_js_1.deriveKeys)(chainId);
444
- const { namespace, storageKey } = derivedKeys;
445
- return this.state.enabledNetworkMap[namespace]?.[storageKey] ?? false;
446
- }
447
- /**
448
- * Returns popular EVM network chain IDs in hex form, restricted to networks
449
- * that exist in NetworkController (networkConfigurationsByChainId). Source is
450
- * the bundled `POPULAR_NETWORKS` unioned with registry-featured EVM chains.
451
- *
452
- * @returns Hex chain IDs for popular EVM networks that are configured.
453
- */
454
- listPopularEvmNetworks() {
455
- const networkControllerState = this.messenger.call('NetworkController:getState');
456
- return __classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_getPopularEvmChainIds).call(this).filter((chainIdHex) => networkControllerState.networkConfigurationsByChainId[chainIdHex]);
457
- }
458
- /**
459
- * Returns popular multichain (Bitcoin, Solana, Tron, Stellar) mainnet chain IDs in
460
- * CAIP-2 form, restricted to networks that exist in MultichainNetworkController
461
- * (multichainNetworkConfigurationsByChainId).
462
- *
463
- * @returns CAIP-2 chain IDs for Bitcoin, Solana, Tron, and Stellar mainnets that are configured.
464
- */
465
- listPopularMultichainNetworks() {
466
- const multichainState = this.messenger.call('MultichainNetworkController:getState');
467
- const multichainMainnets = [
468
- keyring_api_1.BtcScope.Mainnet,
469
- keyring_api_1.SolScope.Mainnet,
470
- keyring_api_1.TrxScope.Mainnet,
471
- keyring_api_1.XlmScope.Pubnet,
472
- ];
473
- return multichainMainnets.filter((chainId) => multichainState.multichainNetworkConfigurationsByChainId[chainId]);
474
- }
475
- /**
476
- * Returns the list of popular network chain IDs in CAIP-2 form, restricted to
477
- * networks that exist in NetworkController (networkConfigurationsByChainId) and
478
- * MultichainNetworkController (multichainNetworkConfigurationsByChainId). EVM
479
- * popular networks come from POPULAR_NETWORKS; multichain popular are Bitcoin,
480
- * Solana, Tron, and Stellar mainnets.
481
- *
482
- * @returns CAIP-2 chain IDs for popular EVM networks and multichain mainnets that are configured.
483
- */
484
- listPopularNetworks() {
485
- const evmHex = this.listPopularEvmNetworks();
486
- const evmCaip = evmHex.map((chainIdHex) => (0, multichain_network_controller_1.toEvmCaipChainId)(chainIdHex));
487
- return [...evmCaip, ...this.listPopularMultichainNetworks()];
488
- }
489
- }
490
- exports.NetworkEnablementController = NetworkEnablementController;
491
- _NetworkEnablementController_instances = new WeakSet(), _NetworkEnablementController_ensureNamespaceBucket = function _NetworkEnablementController_ensureNamespaceBucket(state, ns) {
492
- if (!state.enabledNetworkMap[ns]) {
493
- state.enabledNetworkMap[ns] = {};
494
- }
495
- }, _NetworkEnablementController_isInPopularNetworksMode = function _NetworkEnablementController_isInPopularNetworksMode() {
496
- // Get current network configurations to check which popular networks exist
497
- const networkControllerState = this.messenger.call('NetworkController:getState');
498
- // Count how many popular networks are enabled
499
- const enabledPopularNetworksCount = __classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_getPopularEvmChainIds).call(this).reduce((count, chainId) => {
500
- // Only check networks that actually exist in NetworkController configurations
501
- if (!networkControllerState.networkConfigurationsByChainId[chainId]) {
502
- return count; // Skip networks that don't exist
503
- }
504
- const { namespace, storageKey } = (0, utils_js_1.deriveKeys)(chainId);
505
- const isEnabled = this.state.enabledNetworkMap[namespace]?.[storageKey];
506
- return isEnabled ? count + 1 : count;
507
- }, 0);
508
- // Return true if more than 2 popular networks are enabled
509
- return enabledPopularNetworksCount > 1;
510
- }, _NetworkEnablementController_removeNetworkEntry = function _NetworkEnablementController_removeNetworkEntry(chainId) {
511
- const derivedKeys = (0, utils_js_1.deriveKeys)(chainId);
512
- const { namespace, storageKey, caipChainId } = derivedKeys;
513
- this.update((state) => {
514
- // fallback and enable ethereum mainnet
515
- if ((0, utils_js_1.isOnlyNetworkEnabledInNamespace)(this.state, derivedKeys)) {
516
- state.enabledNetworkMap[namespace][controller_utils_1.ChainId[controller_utils_1.BuiltInNetworkName.Mainnet]] = true;
517
- }
518
- if (namespace in state.enabledNetworkMap) {
519
- delete state.enabledNetworkMap[namespace][storageKey];
520
- }
521
- // Remove from nativeAssetIdentifiers as well
522
- delete state.nativeAssetIdentifiers[caipChainId];
523
- });
524
- }, _NetworkEnablementController_onAddNetwork =
525
- /**
526
- * Handles the addition of a new EVM network to the controller.
527
- *
528
- * @param chainId - The chain ID to add (Hex format)
529
- *
530
- * @description
531
- * - If in popular networks mode (>2 popular networks enabled) AND adding a popular network:
532
- * - Keep current selection (add but don't enable the new network)
533
- * - Otherwise:
534
- * - Switch to the newly added network (disable all others, enable this one)
535
- * - Also updates the nativeAssetIdentifiers with the CAIP-19-like identifier
536
- */
537
- async function _NetworkEnablementController_onAddNetwork(chainId) {
538
- const { namespace, storageKey, reference, caipChainId } = (0, utils_js_1.deriveKeys)(chainId);
539
- // Parse reference (decimal string from CAIP-2) to number for chainid.network lookup
540
- const numericChainId = parseInt(reference, 10);
541
- // EVM networks: use getEvmSlip44 (chainid.network data)
542
- const slip44CoinType = await index_js_1.Slip44Service.getEvmSlip44(numericChainId);
543
- this.update((state) => {
544
- // Ensure the namespace bucket exists
545
- __classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this, state, namespace);
546
- // Check if popular networks mode is active (>2 popular networks enabled)
547
- const inPopularNetworksMode = __classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_isInPopularNetworksMode).call(this);
548
- // Check if the network being added is a popular network
549
- const isAddedNetworkPopular = __classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_getPopularEvmChainIds).call(this).includes(chainId);
550
- // Keep current selection only if in popular networks mode AND adding a popular network
551
- const shouldKeepCurrentSelection = inPopularNetworksMode && isAddedNetworkPopular;
552
- if (shouldKeepCurrentSelection) {
553
- // Add the popular network but don't enable it (keep current selection)
554
- state.enabledNetworkMap[namespace][storageKey] = true;
555
- }
556
- else {
557
- // Switch to the newly added network (disable all others, enable this one)
558
- Object.keys(state.enabledNetworkMap).forEach((ns) => {
559
- Object.keys(state.enabledNetworkMap[ns]).forEach((key) => {
560
- state.enabledNetworkMap[ns][key] = false;
561
- });
562
- });
563
- // Enable the newly added network
564
- state.enabledNetworkMap[namespace][storageKey] = true;
565
- }
566
- // Update nativeAssetIdentifiers with the CAIP-19-like identifier
567
- state.nativeAssetIdentifiers[caipChainId] = buildNativeAssetIdentifier(caipChainId, slip44CoinType);
568
- });
569
- }, _NetworkEnablementController_getRegistryPopularEvmChainIds = function _NetworkEnablementController_getRegistryPopularEvmChainIds() {
570
- const { configs: { networks }, } = this.messenger.call('ConfigRegistryController:getState');
571
- return Object.values(networks).reduce((popularChains, network) => {
572
- if (network.config.isFeatured &&
573
- network.config.isActive &&
574
- !network.config.isTestnet &&
575
- network.chainId.startsWith('eip155:')) {
576
- const hexChainId = (0, controller_utils_1.toHex)((0, utils_1.parseCaipChainId)(network.chainId).reference);
577
- popularChains.push(hexChainId);
578
- }
579
- return popularChains;
580
- }, []);
581
- }, _NetworkEnablementController_getPopularEvmChainIds = function _NetworkEnablementController_getPopularEvmChainIds() {
582
- return [
583
- ...new Set([
584
- // `toHex` is used to normalize chain IDs from the bundled POPULAR_NETWORKS.
585
- ...constants_js_1.POPULAR_NETWORKS.map((chainId) => (0, controller_utils_1.toHex)(chainId)),
586
- ...__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_getRegistryPopularEvmChainIds).call(this),
587
- ]),
588
- ];
589
- };
590
- //# sourceMappingURL=NetworkEnablementController.cjs.map