Skip to content

Commit 14cb51b

Browse files
authored
fix: fix init function (#6658)
## Explanation fix init function. , every new network added to the conf should not change the users config <!-- Thanks for your contribution! Take a moment to answer these questions so that reviewers have the information they need to properly understand your changes: * What is the current state of things and why does it need to change? * What is the solution your changes offer and how does it work? * Are there any changes whose purpose might not obvious to those unfamiliar with the domain? * If your primary goal was to update one package but you found you had to update another one along the way, why did you do so? * If you had to upgrade a dependency, why did you do so? --> ## References <!-- Are there any issues that this pull request is tied to? Are there other links that reviewers should consult to understand these changes better? Are there client or consumer pull requests to adopt any breaking changes? For example: * Fixes #12345 * Related to #67890 --> ## Checklist - [ ] I've updated the test suite for new or updated code as appropriate - [ ] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [ ] I've communicated my changes to consumers by [updating changelogs for packages I've changed](https://github.com/MetaMask/core/tree/main/docs/contributing.md#updating-changelogs), highlighting breaking changes as necessary - [ ] I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes
1 parent 5257765 commit 14cb51b

File tree

3 files changed

+20
-68
lines changed

3 files changed

+20
-68
lines changed

packages/network-enablement-controller/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- Bump `@metamask/controller-utils` from `^11.12.0` to `^11.14.0` ([#6620](https://github.com/MetaMask/core/pull/6620), [#6629](https://github.com/MetaMask/core/pull/6629))
1313
- Bump `@metamask/base-controller` from `^8.3.0` to `^8.4.0` ([#6632](https://github.com/MetaMask/core/pull/6632))
1414

15+
### Fixed
16+
17+
- Fix `init()` method to preserve existing user network settings instead of resetting them, while syncing with NetworkController and MultichainNetworkController states ([#6658](https://github.com/MetaMask/core/pull/6658))
18+
1519
## [1.1.0]
1620

1721
### Added

packages/network-enablement-controller/src/NetworkEnablementController.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -453,12 +453,12 @@ describe('NetworkEnablementController', () => {
453453
expect(controller.state).toStrictEqual({
454454
enabledNetworkMap: {
455455
[KnownCaipNamespace.Eip155]: {
456-
'0x1': true, // Ethereum Mainnet (exists in config)
457-
'0xe708': true, // Linea Mainnet (exists in config)
456+
'0x1': false, // Ethereum Mainnet (exists in config)
457+
'0xe708': false, // Linea Mainnet (exists in config)
458458
// Other popular networks not enabled because they don't exist in config
459459
},
460460
[KnownCaipNamespace.Solana]: {
461-
[SolScope.Mainnet]: true, // Solana Mainnet (exists in config)
461+
[SolScope.Mainnet]: false, // Solana Mainnet (exists in config)
462462
},
463463
},
464464
});

packages/network-enablement-controller/src/NetworkEnablementController.ts

Lines changed: 13 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -343,9 +343,9 @@ export class NetworkEnablementController extends BaseController<
343343
* Initializes the network enablement state from network controller configurations.
344344
*
345345
* This method reads the current network configurations from both NetworkController
346-
* and MultichainNetworkController and initializes the enabled network map accordingly.
347-
* It ensures proper namespace buckets exist for all configured networks and enables
348-
* popular networks by default.
346+
* and MultichainNetworkController and syncs the enabled network map accordingly.
347+
* It ensures proper namespace buckets exist for all configured networks and only
348+
* adds missing networks with a default value of false, preserving existing user settings.
349349
*
350350
* This method should be called after the NetworkController and MultichainNetworkController
351351
* have been initialized and their configurations are available.
@@ -366,79 +366,27 @@ export class NetworkEnablementController extends BaseController<
366366
Object.keys(
367367
networkControllerState.networkConfigurationsByChainId,
368368
).forEach((chainId) => {
369-
const { namespace } = deriveKeys(chainId as Hex);
369+
const { namespace, storageKey } = deriveKeys(chainId as Hex);
370370
this.#ensureNamespaceBucket(s, namespace);
371+
372+
// Only add network if it doesn't already exist in state (preserves user settings)
373+
if (s.enabledNetworkMap[namespace][storageKey] === undefined) {
374+
s.enabledNetworkMap[namespace][storageKey] = false;
375+
}
371376
});
372377

373378
// Initialize namespace buckets for all networks from MultichainNetworkController
374379
Object.keys(
375380
multichainState.multichainNetworkConfigurationsByChainId,
376381
).forEach((chainId) => {
377-
const { namespace } = deriveKeys(chainId as CaipChainId);
382+
const { namespace, storageKey } = deriveKeys(chainId as CaipChainId);
378383
this.#ensureNamespaceBucket(s, namespace);
379-
});
380384

381-
// Enable popular networks that exist in the configurations
382-
POPULAR_NETWORKS.forEach((chainId) => {
383-
const { namespace, storageKey } = deriveKeys(chainId as Hex);
384-
385-
// Check if network exists in NetworkController configurations
386-
if (
387-
s.enabledNetworkMap[namespace] &&
388-
networkControllerState.networkConfigurationsByChainId[chainId as Hex]
389-
) {
390-
s.enabledNetworkMap[namespace][storageKey] = true;
385+
// Only add network if it doesn't already exist in state (preserves user settings)
386+
if (s.enabledNetworkMap[namespace][storageKey] === undefined) {
387+
s.enabledNetworkMap[namespace][storageKey] = false;
391388
}
392389
});
393-
394-
// Enable Solana mainnet if it exists in configurations
395-
const solanaKeys = deriveKeys(SolScope.Mainnet as CaipChainId);
396-
if (
397-
s.enabledNetworkMap[solanaKeys.namespace] &&
398-
multichainState.multichainNetworkConfigurationsByChainId[
399-
SolScope.Mainnet
400-
]
401-
) {
402-
s.enabledNetworkMap[solanaKeys.namespace][solanaKeys.storageKey] = true;
403-
}
404-
405-
// Enable Bitcoin mainnet if it exists in configurations
406-
const bitcoinKeys = deriveKeys(BtcScope.Mainnet as CaipChainId);
407-
if (
408-
s.enabledNetworkMap[bitcoinKeys.namespace] &&
409-
multichainState.multichainNetworkConfigurationsByChainId[
410-
BtcScope.Mainnet
411-
]
412-
) {
413-
s.enabledNetworkMap[bitcoinKeys.namespace][bitcoinKeys.storageKey] =
414-
true;
415-
}
416-
417-
// Enable Bitcoin testnet if it exists in configurations
418-
const bitcoinTestnetKeys = deriveKeys(BtcScope.Testnet as CaipChainId);
419-
if (
420-
s.enabledNetworkMap[bitcoinTestnetKeys.namespace] &&
421-
multichainState.multichainNetworkConfigurationsByChainId[
422-
BtcScope.Testnet
423-
]
424-
) {
425-
s.enabledNetworkMap[bitcoinTestnetKeys.namespace][
426-
bitcoinTestnetKeys.storageKey
427-
] = false;
428-
}
429-
430-
// Enable Bitcoin signet testnet if it exists in configurations
431-
const bitcoinSignetKeys = deriveKeys(BtcScope.Signet as CaipChainId);
432-
if (
433-
s.enabledNetworkMap[bitcoinSignetKeys.namespace] &&
434-
multichainState.multichainNetworkConfigurationsByChainId[
435-
BtcScope.Signet
436-
]
437-
) {
438-
s.enabledNetworkMap[bitcoinSignetKeys.namespace][
439-
bitcoinSignetKeys.storageKey
440-
] = false;
441-
}
442390
});
443391
}
444392

0 commit comments

Comments
 (0)