@metamask-previews/network-enablement-controller 4.0.0-preview-821afcb8 → 4.0.0-preview-0c691324
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 +240 -61
- 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 +240 -61
- 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))
|
|
@@ -4,7 +4,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
4
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
5
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
6
6
|
};
|
|
7
|
-
var _NetworkEnablementController_instances, _NetworkEnablementController_ensureNamespaceBucket, _NetworkEnablementController_isInPopularNetworksMode, _NetworkEnablementController_removeNetworkEntry, _NetworkEnablementController_onAddNetwork;
|
|
7
|
+
var _NetworkEnablementController_instances, _NetworkEnablementController_onNativeCurrencyChange, _NetworkEnablementController_ensureNamespaceBucket, _NetworkEnablementController_updateNativeAssetIdentifier, _NetworkEnablementController_isInPopularNetworksMode, _NetworkEnablementController_removeNetworkEntry, _NetworkEnablementController_onAddNetwork;
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
9
|
exports.NetworkEnablementController = void 0;
|
|
10
10
|
const base_controller_1 = require("@metamask/base-controller");
|
|
@@ -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,12 +105,20 @@ 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);
|
|
93
114
|
});
|
|
115
|
+
// Subscribe to nativeCurrency changes using selector approach
|
|
116
|
+
messenger.subscribe('NetworkController:stateChange', (currentNativeCurrencies, previousNativeCurrencies) => {
|
|
117
|
+
// eslint-disable-next-line no-void
|
|
118
|
+
void __classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_onNativeCurrencyChange).call(this, currentNativeCurrencies, previousNativeCurrencies);
|
|
119
|
+
},
|
|
120
|
+
// Selector: extract chainId -> nativeCurrency map
|
|
121
|
+
(networkState) => Object.fromEntries(Object.entries(networkState.networkConfigurationsByChainId).map(([chainId, config]) => [chainId, config.nativeCurrency])));
|
|
94
122
|
}
|
|
95
123
|
/**
|
|
96
124
|
* Enables or disables a network for the user.
|
|
@@ -110,20 +138,20 @@ class NetworkEnablementController extends base_controller_1.BaseController {
|
|
|
110
138
|
*/
|
|
111
139
|
enableNetwork(chainId) {
|
|
112
140
|
const { namespace, storageKey } = (0, utils_2.deriveKeys)(chainId);
|
|
113
|
-
this.update((
|
|
141
|
+
this.update((state) => {
|
|
114
142
|
// disable all networks in all namespaces first
|
|
115
|
-
Object.keys(
|
|
116
|
-
Object.keys(
|
|
117
|
-
|
|
143
|
+
Object.keys(state.enabledNetworkMap).forEach((ns) => {
|
|
144
|
+
Object.keys(state.enabledNetworkMap[ns]).forEach((key) => {
|
|
145
|
+
state.enabledNetworkMap[ns][key] = false;
|
|
118
146
|
});
|
|
119
147
|
});
|
|
120
148
|
// if the namespace bucket does not exist, return
|
|
121
149
|
// new nemespace are added only when a new network is added
|
|
122
|
-
if (!
|
|
150
|
+
if (!state.enabledNetworkMap[namespace]) {
|
|
123
151
|
return;
|
|
124
152
|
}
|
|
125
153
|
// enable the network
|
|
126
|
-
|
|
154
|
+
state.enabledNetworkMap[namespace][storageKey] = true;
|
|
127
155
|
});
|
|
128
156
|
}
|
|
129
157
|
/**
|
|
@@ -148,17 +176,17 @@ class NetworkEnablementController extends base_controller_1.BaseController {
|
|
|
148
176
|
if (derivedNamespace !== namespace) {
|
|
149
177
|
throw new Error(`Chain ID ${chainId} belongs to namespace ${derivedNamespace}, but namespace ${namespace} was specified`);
|
|
150
178
|
}
|
|
151
|
-
this.update((
|
|
179
|
+
this.update((state) => {
|
|
152
180
|
// Ensure the namespace bucket exists
|
|
153
|
-
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this,
|
|
181
|
+
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this, state, namespace);
|
|
154
182
|
// Disable all networks in the specified namespace first
|
|
155
|
-
if (
|
|
156
|
-
Object.keys(
|
|
157
|
-
|
|
183
|
+
if (state.enabledNetworkMap[namespace]) {
|
|
184
|
+
Object.keys(state.enabledNetworkMap[namespace]).forEach((key) => {
|
|
185
|
+
state.enabledNetworkMap[namespace][key] = false;
|
|
158
186
|
});
|
|
159
187
|
}
|
|
160
188
|
// Enable the target network in the specified namespace
|
|
161
|
-
|
|
189
|
+
state.enabledNetworkMap[namespace][storageKey] = true;
|
|
162
190
|
});
|
|
163
191
|
}
|
|
164
192
|
/**
|
|
@@ -172,11 +200,11 @@ class NetworkEnablementController extends base_controller_1.BaseController {
|
|
|
172
200
|
* Popular networks that don't exist in NetworkController or MultichainNetworkController configurations will be skipped silently.
|
|
173
201
|
*/
|
|
174
202
|
enableAllPopularNetworks() {
|
|
175
|
-
this.update((
|
|
203
|
+
this.update((state) => {
|
|
176
204
|
// First disable all networks across all namespaces
|
|
177
|
-
Object.keys(
|
|
178
|
-
Object.keys(
|
|
179
|
-
|
|
205
|
+
Object.keys(state.enabledNetworkMap).forEach((ns) => {
|
|
206
|
+
Object.keys(state.enabledNetworkMap[ns]).forEach((key) => {
|
|
207
|
+
state.enabledNetworkMap[ns][key] = false;
|
|
180
208
|
});
|
|
181
209
|
});
|
|
182
210
|
// Get current network configurations to check if networks exist
|
|
@@ -188,35 +216,36 @@ class NetworkEnablementController extends base_controller_1.BaseController {
|
|
|
188
216
|
// Check if network exists in NetworkController configurations
|
|
189
217
|
if (networkControllerState.networkConfigurationsByChainId[chainId]) {
|
|
190
218
|
// Ensure namespace bucket exists
|
|
191
|
-
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this,
|
|
219
|
+
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this, state, namespace);
|
|
192
220
|
// Enable the network
|
|
193
|
-
|
|
221
|
+
state.enabledNetworkMap[namespace][storageKey] = true;
|
|
194
222
|
}
|
|
195
223
|
});
|
|
196
224
|
// Enable Solana mainnet if it exists in MultichainNetworkController configurations
|
|
197
225
|
const solanaKeys = (0, utils_2.deriveKeys)(keyring_api_1.SolScope.Mainnet);
|
|
198
226
|
if (multichainState.multichainNetworkConfigurationsByChainId[keyring_api_1.SolScope.Mainnet]) {
|
|
199
227
|
// Ensure namespace bucket exists
|
|
200
|
-
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this,
|
|
228
|
+
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this, state, solanaKeys.namespace);
|
|
201
229
|
// Enable Solana mainnet
|
|
202
|
-
|
|
230
|
+
state.enabledNetworkMap[solanaKeys.namespace][solanaKeys.storageKey] =
|
|
231
|
+
true;
|
|
203
232
|
}
|
|
204
233
|
// Enable Bitcoin mainnet if it exists in MultichainNetworkController configurations
|
|
205
234
|
const bitcoinKeys = (0, utils_2.deriveKeys)(keyring_api_1.BtcScope.Mainnet);
|
|
206
235
|
if (multichainState.multichainNetworkConfigurationsByChainId[keyring_api_1.BtcScope.Mainnet]) {
|
|
207
236
|
// Ensure namespace bucket exists
|
|
208
|
-
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this,
|
|
237
|
+
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this, state, bitcoinKeys.namespace);
|
|
209
238
|
// Enable Bitcoin mainnet
|
|
210
|
-
|
|
239
|
+
state.enabledNetworkMap[bitcoinKeys.namespace][bitcoinKeys.storageKey] =
|
|
211
240
|
true;
|
|
212
241
|
}
|
|
213
242
|
// Enable Tron mainnet if it exists in MultichainNetworkController configurations
|
|
214
243
|
const tronKeys = (0, utils_2.deriveKeys)(keyring_api_1.TrxScope.Mainnet);
|
|
215
244
|
if (multichainState.multichainNetworkConfigurationsByChainId[keyring_api_1.TrxScope.Mainnet]) {
|
|
216
245
|
// Ensure namespace bucket exists
|
|
217
|
-
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this,
|
|
246
|
+
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this, state, tronKeys.namespace);
|
|
218
247
|
// Enable Tron mainnet
|
|
219
|
-
|
|
248
|
+
state.enabledNetworkMap[tronKeys.namespace][tronKeys.storageKey] = true;
|
|
220
249
|
}
|
|
221
250
|
});
|
|
222
251
|
}
|
|
@@ -224,39 +253,124 @@ class NetworkEnablementController extends base_controller_1.BaseController {
|
|
|
224
253
|
* Initializes the network enablement state from network controller configurations.
|
|
225
254
|
*
|
|
226
255
|
* This method reads the current network configurations from both NetworkController
|
|
227
|
-
* and MultichainNetworkController and syncs the enabled network map accordingly.
|
|
256
|
+
* and MultichainNetworkController and syncs the enabled network map and nativeAssetIdentifiers accordingly.
|
|
228
257
|
* It ensures proper namespace buckets exist for all configured networks and only
|
|
229
258
|
* adds missing networks with a default value of false, preserving existing user settings.
|
|
230
259
|
*
|
|
231
260
|
* This method should be called after the NetworkController and MultichainNetworkController
|
|
232
261
|
* have been initialized and their configurations are available.
|
|
233
262
|
*/
|
|
234
|
-
init() {
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
263
|
+
async init() {
|
|
264
|
+
// Get network configurations from NetworkController (EVM networks)
|
|
265
|
+
const networkControllerState = this.messenger.call('NetworkController:getState');
|
|
266
|
+
// Get network configurations from MultichainNetworkController (all networks)
|
|
267
|
+
const multichainState = this.messenger.call('MultichainNetworkController:getState');
|
|
268
|
+
// Build nativeAssetIdentifiers for EVM networks using chainid.network
|
|
269
|
+
const evmNativeAssetUpdates = [];
|
|
270
|
+
for (const [chainId, config] of Object.entries(networkControllerState.networkConfigurationsByChainId)) {
|
|
271
|
+
const { caipChainId } = (0, utils_2.deriveKeys)(chainId);
|
|
272
|
+
// Skip if already in state
|
|
273
|
+
if (this.state.nativeAssetIdentifiers[caipChainId] !== undefined) {
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
276
|
+
// Parse hex chainId to number for chainid.network lookup
|
|
277
|
+
const numericChainId = parseInt(chainId, 16);
|
|
278
|
+
// EVM networks: use getSlip44ByChainId (chainid.network data)
|
|
279
|
+
// Default to 60 (Ethereum) if no specific mapping is found
|
|
280
|
+
const slip44CoinType = (await services_1.Slip44Service.getSlip44ByChainId(numericChainId, config.nativeCurrency)) ?? 60;
|
|
281
|
+
evmNativeAssetUpdates.push({
|
|
282
|
+
caipChainId,
|
|
283
|
+
identifier: buildNativeAssetIdentifier(caipChainId, slip44CoinType),
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
// Update state synchronously
|
|
287
|
+
this.update((state) => {
|
|
240
288
|
// Initialize namespace buckets for EVM networks from NetworkController
|
|
241
|
-
Object.
|
|
289
|
+
Object.entries(networkControllerState.networkConfigurationsByChainId).forEach(([chainId]) => {
|
|
290
|
+
var _a;
|
|
242
291
|
const { namespace, storageKey } = (0, utils_2.deriveKeys)(chainId);
|
|
243
|
-
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this,
|
|
292
|
+
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this, state, namespace);
|
|
244
293
|
// Only add network if it doesn't already exist in state (preserves user settings)
|
|
245
|
-
|
|
246
|
-
s.enabledNetworkMap[namespace][storageKey] = false;
|
|
247
|
-
}
|
|
294
|
+
(_a = state.enabledNetworkMap[namespace])[storageKey] ?? (_a[storageKey] = false);
|
|
248
295
|
});
|
|
296
|
+
// Apply nativeAssetIdentifier updates
|
|
297
|
+
for (const { caipChainId, identifier } of evmNativeAssetUpdates) {
|
|
298
|
+
state.nativeAssetIdentifiers[caipChainId] = identifier;
|
|
299
|
+
}
|
|
249
300
|
// Initialize namespace buckets for all networks from MultichainNetworkController
|
|
250
301
|
Object.keys(multichainState.multichainNetworkConfigurationsByChainId).forEach((chainId) => {
|
|
302
|
+
var _a;
|
|
251
303
|
const { namespace, storageKey } = (0, utils_2.deriveKeys)(chainId);
|
|
252
|
-
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this,
|
|
304
|
+
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this, state, namespace);
|
|
253
305
|
// Only add network if it doesn't already exist in state (preserves user settings)
|
|
254
|
-
|
|
255
|
-
s.enabledNetworkMap[namespace][storageKey] = false;
|
|
256
|
-
}
|
|
306
|
+
(_a = state.enabledNetworkMap[namespace])[storageKey] ?? (_a[storageKey] = false);
|
|
257
307
|
});
|
|
258
308
|
});
|
|
259
309
|
}
|
|
310
|
+
/**
|
|
311
|
+
* Initializes the native asset identifiers from network configurations.
|
|
312
|
+
* This method should be called from the client during controller initialization
|
|
313
|
+
* to populate the nativeAssetIdentifiers state based on actual network configurations.
|
|
314
|
+
*
|
|
315
|
+
* @param networks - Array of network configurations with chainId and nativeCurrency
|
|
316
|
+
* @example
|
|
317
|
+
* ```typescript
|
|
318
|
+
* const evmNetworks = Object.values(networkControllerState.networkConfigurationsByChainId)
|
|
319
|
+
* .map(config => ({
|
|
320
|
+
* chainId: toEvmCaipChainId(config.chainId),
|
|
321
|
+
* nativeCurrency: config.nativeCurrency,
|
|
322
|
+
* }));
|
|
323
|
+
*
|
|
324
|
+
* const multichainNetworks = Object.values(multichainState.multichainNetworkConfigurationsByChainId)
|
|
325
|
+
* .map(config => ({
|
|
326
|
+
* chainId: config.chainId,
|
|
327
|
+
* nativeCurrency: config.nativeCurrency,
|
|
328
|
+
* }));
|
|
329
|
+
*
|
|
330
|
+
* await controller.initNativeAssetIdentifiers([...evmNetworks, ...multichainNetworks]);
|
|
331
|
+
* ```
|
|
332
|
+
*/
|
|
333
|
+
async initNativeAssetIdentifiers(networks) {
|
|
334
|
+
// Process networks and collect updates
|
|
335
|
+
const updates = [];
|
|
336
|
+
for (const { chainId, nativeCurrency } of networks) {
|
|
337
|
+
// Check if nativeCurrency is already in CAIP-19 format (e.g., "bip122:.../slip44:0")
|
|
338
|
+
// Non-EVM networks from MultichainNetworkController use this format
|
|
339
|
+
if (nativeCurrency.includes('/slip44:')) {
|
|
340
|
+
updates.push({
|
|
341
|
+
chainId,
|
|
342
|
+
identifier: nativeCurrency,
|
|
343
|
+
});
|
|
344
|
+
continue;
|
|
345
|
+
}
|
|
346
|
+
// Extract namespace from CAIP-2 chainId
|
|
347
|
+
const [namespace, reference] = chainId.split(':');
|
|
348
|
+
let slip44CoinType;
|
|
349
|
+
if (namespace === 'eip155') {
|
|
350
|
+
// EVM networks: use getSlip44ByChainId (chainid.network data)
|
|
351
|
+
// Default to 60 (Ethereum) if no specific mapping is found
|
|
352
|
+
const numericChainId = parseInt(reference, 10);
|
|
353
|
+
slip44CoinType =
|
|
354
|
+
(await services_1.Slip44Service.getSlip44ByChainId(numericChainId, nativeCurrency)) ?? 60;
|
|
355
|
+
}
|
|
356
|
+
else {
|
|
357
|
+
// Non-EVM networks: use getSlip44BySymbol (@metamask/slip44 package)
|
|
358
|
+
slip44CoinType = services_1.Slip44Service.getSlip44BySymbol(nativeCurrency);
|
|
359
|
+
}
|
|
360
|
+
if (slip44CoinType !== undefined) {
|
|
361
|
+
updates.push({
|
|
362
|
+
chainId,
|
|
363
|
+
identifier: buildNativeAssetIdentifier(chainId, slip44CoinType),
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
// Apply all updates synchronously
|
|
368
|
+
this.update((state) => {
|
|
369
|
+
for (const { chainId, identifier } of updates) {
|
|
370
|
+
state.nativeAssetIdentifiers[chainId] = identifier;
|
|
371
|
+
}
|
|
372
|
+
});
|
|
373
|
+
}
|
|
260
374
|
/**
|
|
261
375
|
* Disables a network for the user.
|
|
262
376
|
*
|
|
@@ -274,8 +388,8 @@ class NetworkEnablementController extends base_controller_1.BaseController {
|
|
|
274
388
|
disableNetwork(chainId) {
|
|
275
389
|
const derivedKeys = (0, utils_2.deriveKeys)(chainId);
|
|
276
390
|
const { namespace, storageKey } = derivedKeys;
|
|
277
|
-
this.update((
|
|
278
|
-
|
|
391
|
+
this.update((state) => {
|
|
392
|
+
state.enabledNetworkMap[namespace][storageKey] = false;
|
|
279
393
|
});
|
|
280
394
|
}
|
|
281
395
|
/**
|
|
@@ -293,10 +407,53 @@ class NetworkEnablementController extends base_controller_1.BaseController {
|
|
|
293
407
|
}
|
|
294
408
|
}
|
|
295
409
|
exports.NetworkEnablementController = NetworkEnablementController;
|
|
296
|
-
_NetworkEnablementController_instances = new WeakSet(),
|
|
410
|
+
_NetworkEnablementController_instances = new WeakSet(), _NetworkEnablementController_onNativeCurrencyChange =
|
|
411
|
+
/**
|
|
412
|
+
* Handles changes to network nativeCurrency values.
|
|
413
|
+
* Compares current and previous nativeCurrency maps to detect updates.
|
|
414
|
+
*
|
|
415
|
+
* @param currentNativeCurrencies - Current map of chainId to nativeCurrency
|
|
416
|
+
* @param previousNativeCurrencies - Previous map of chainId to nativeCurrency
|
|
417
|
+
*/
|
|
418
|
+
async function _NetworkEnablementController_onNativeCurrencyChange(currentNativeCurrencies, previousNativeCurrencies) {
|
|
419
|
+
// Skip if no previous state (initial subscription)
|
|
420
|
+
if (!previousNativeCurrencies) {
|
|
421
|
+
return;
|
|
422
|
+
}
|
|
423
|
+
// Find chains where nativeCurrency has changed
|
|
424
|
+
for (const [chainId, currentCurrency] of Object.entries(currentNativeCurrencies)) {
|
|
425
|
+
const previousCurrency = previousNativeCurrencies[chainId];
|
|
426
|
+
// Only update if the nativeCurrency changed (not for new chains - those are handled by networkAdded)
|
|
427
|
+
if (previousCurrency !== undefined &&
|
|
428
|
+
previousCurrency !== currentCurrency) {
|
|
429
|
+
await __classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_updateNativeAssetIdentifier).call(this, chainId, currentCurrency);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
}, _NetworkEnablementController_ensureNamespaceBucket = function _NetworkEnablementController_ensureNamespaceBucket(state, ns) {
|
|
297
433
|
if (!state.enabledNetworkMap[ns]) {
|
|
298
434
|
state.enabledNetworkMap[ns] = {};
|
|
299
435
|
}
|
|
436
|
+
}, _NetworkEnablementController_updateNativeAssetIdentifier =
|
|
437
|
+
/**
|
|
438
|
+
* Updates the native asset identifier for an EVM network based on its symbol.
|
|
439
|
+
*
|
|
440
|
+
* This method looks up the SLIP-44 coin type using chainid.network data
|
|
441
|
+
* (via getSlip44ByChainId) and updates the nativeAssetIdentifiers state.
|
|
442
|
+
* This is only called for EVM networks from NetworkController state changes.
|
|
443
|
+
*
|
|
444
|
+
* @param chainId - The chain ID of the network (Hex format)
|
|
445
|
+
* @param symbol - The native currency symbol of the network (e.g., 'ETH')
|
|
446
|
+
*/
|
|
447
|
+
async function _NetworkEnablementController_updateNativeAssetIdentifier(chainId, symbol) {
|
|
448
|
+
const { caipChainId, reference } = (0, utils_2.deriveKeys)(chainId);
|
|
449
|
+
// Parse hex chainId to number for chainid.network lookup
|
|
450
|
+
const numericChainId = parseInt(reference, 16);
|
|
451
|
+
// EVM networks: use getSlip44ByChainId (chainid.network data)
|
|
452
|
+
// Default to 60 (Ethereum) if no specific mapping is found
|
|
453
|
+
const slip44CoinType = (await services_1.Slip44Service.getSlip44ByChainId(numericChainId, symbol)) ?? 60;
|
|
454
|
+
this.update((state) => {
|
|
455
|
+
state.nativeAssetIdentifiers[caipChainId] = buildNativeAssetIdentifier(caipChainId, slip44CoinType);
|
|
456
|
+
});
|
|
300
457
|
}, _NetworkEnablementController_isInPopularNetworksMode = function _NetworkEnablementController_isInPopularNetworksMode() {
|
|
301
458
|
// Get current network configurations to check which popular networks exist
|
|
302
459
|
const networkControllerState = this.messenger.call('NetworkController:getState');
|
|
@@ -314,22 +471,42 @@ _NetworkEnablementController_instances = new WeakSet(), _NetworkEnablementContro
|
|
|
314
471
|
return enabledPopularNetworksCount > 1;
|
|
315
472
|
}, _NetworkEnablementController_removeNetworkEntry = function _NetworkEnablementController_removeNetworkEntry(chainId) {
|
|
316
473
|
const derivedKeys = (0, utils_2.deriveKeys)(chainId);
|
|
317
|
-
const { namespace, storageKey } = derivedKeys;
|
|
318
|
-
this.update((
|
|
474
|
+
const { namespace, storageKey, caipChainId } = derivedKeys;
|
|
475
|
+
this.update((state) => {
|
|
319
476
|
// fallback and enable ethereum mainnet
|
|
320
477
|
if ((0, utils_2.isOnlyNetworkEnabledInNamespace)(this.state, derivedKeys)) {
|
|
321
|
-
|
|
322
|
-
true;
|
|
478
|
+
state.enabledNetworkMap[namespace][controller_utils_1.ChainId[controller_utils_1.BuiltInNetworkName.Mainnet]] = true;
|
|
323
479
|
}
|
|
324
|
-
if (namespace in
|
|
325
|
-
delete
|
|
480
|
+
if (namespace in state.enabledNetworkMap) {
|
|
481
|
+
delete state.enabledNetworkMap[namespace][storageKey];
|
|
326
482
|
}
|
|
483
|
+
// Remove from nativeAssetIdentifiers as well
|
|
484
|
+
delete state.nativeAssetIdentifiers[caipChainId];
|
|
327
485
|
});
|
|
328
|
-
}, _NetworkEnablementController_onAddNetwork =
|
|
329
|
-
|
|
330
|
-
|
|
486
|
+
}, _NetworkEnablementController_onAddNetwork =
|
|
487
|
+
/**
|
|
488
|
+
* Handles the addition of a new EVM network to the controller.
|
|
489
|
+
*
|
|
490
|
+
* @param chainId - The chain ID to add (Hex format)
|
|
491
|
+
* @param nativeCurrency - The native currency symbol of the network (e.g., 'ETH')
|
|
492
|
+
*
|
|
493
|
+
* @description
|
|
494
|
+
* - If in popular networks mode (>2 popular networks enabled) AND adding a popular network:
|
|
495
|
+
* - Keep current selection (add but don't enable the new network)
|
|
496
|
+
* - Otherwise:
|
|
497
|
+
* - Switch to the newly added network (disable all others, enable this one)
|
|
498
|
+
* - Also updates the nativeAssetIdentifiers with the CAIP-19-like identifier
|
|
499
|
+
*/
|
|
500
|
+
async function _NetworkEnablementController_onAddNetwork(chainId, nativeCurrency) {
|
|
501
|
+
const { namespace, storageKey, reference, caipChainId } = (0, utils_2.deriveKeys)(chainId);
|
|
502
|
+
// Parse hex chainId to number for chainid.network lookup
|
|
503
|
+
const numericChainId = parseInt(reference, 16);
|
|
504
|
+
// EVM networks: use getSlip44ByChainId (chainid.network data)
|
|
505
|
+
// Default to 60 (Ethereum) if no specific mapping is found
|
|
506
|
+
const slip44CoinType = (await services_1.Slip44Service.getSlip44ByChainId(numericChainId, nativeCurrency)) ?? 60;
|
|
507
|
+
this.update((state) => {
|
|
331
508
|
// Ensure the namespace bucket exists
|
|
332
|
-
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this,
|
|
509
|
+
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this, state, namespace);
|
|
333
510
|
// Check if popular networks mode is active (>2 popular networks enabled)
|
|
334
511
|
const inPopularNetworksMode = __classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_isInPopularNetworksMode).call(this);
|
|
335
512
|
// Check if the network being added is a popular network
|
|
@@ -338,18 +515,20 @@ _NetworkEnablementController_instances = new WeakSet(), _NetworkEnablementContro
|
|
|
338
515
|
const shouldKeepCurrentSelection = inPopularNetworksMode && isAddedNetworkPopular;
|
|
339
516
|
if (shouldKeepCurrentSelection) {
|
|
340
517
|
// Add the popular network but don't enable it (keep current selection)
|
|
341
|
-
|
|
518
|
+
state.enabledNetworkMap[namespace][storageKey] = true;
|
|
342
519
|
}
|
|
343
520
|
else {
|
|
344
521
|
// Switch to the newly added network (disable all others, enable this one)
|
|
345
|
-
Object.keys(
|
|
346
|
-
Object.keys(
|
|
347
|
-
|
|
522
|
+
Object.keys(state.enabledNetworkMap).forEach((ns) => {
|
|
523
|
+
Object.keys(state.enabledNetworkMap[ns]).forEach((key) => {
|
|
524
|
+
state.enabledNetworkMap[ns][key] = false;
|
|
348
525
|
});
|
|
349
526
|
});
|
|
350
527
|
// Enable the newly added network
|
|
351
|
-
|
|
528
|
+
state.enabledNetworkMap[namespace][storageKey] = true;
|
|
352
529
|
}
|
|
530
|
+
// Update nativeAssetIdentifiers with the CAIP-19-like identifier
|
|
531
|
+
state.nativeAssetIdentifiers[caipChainId] = buildNativeAssetIdentifier(caipChainId, slip44CoinType);
|
|
353
532
|
});
|
|
354
533
|
};
|
|
355
534
|
//# sourceMappingURL=NetworkEnablementController.cjs.map
|