diff --git a/packages/network-enablement-controller/CHANGELOG.md b/packages/network-enablement-controller/CHANGELOG.md index 9dd19450301..593c3d45d36 100644 --- a/packages/network-enablement-controller/CHANGELOG.md +++ b/packages/network-enablement-controller/CHANGELOG.md @@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - 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)) - Bump `@metamask/base-controller` from `^8.3.0` to `^8.4.0` ([#6632](https://github.com/MetaMask/core/pull/6632)) +### Fixed + +- 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)) + ## [1.1.0] ### Added diff --git a/packages/network-enablement-controller/src/NetworkEnablementController.test.ts b/packages/network-enablement-controller/src/NetworkEnablementController.test.ts index 2b81b3b8005..21aecd7182e 100644 --- a/packages/network-enablement-controller/src/NetworkEnablementController.test.ts +++ b/packages/network-enablement-controller/src/NetworkEnablementController.test.ts @@ -453,12 +453,12 @@ describe('NetworkEnablementController', () => { expect(controller.state).toStrictEqual({ enabledNetworkMap: { [KnownCaipNamespace.Eip155]: { - '0x1': true, // Ethereum Mainnet (exists in config) - '0xe708': true, // Linea Mainnet (exists in config) + '0x1': false, // Ethereum Mainnet (exists in config) + '0xe708': false, // Linea Mainnet (exists in config) // Other popular networks not enabled because they don't exist in config }, [KnownCaipNamespace.Solana]: { - [SolScope.Mainnet]: true, // Solana Mainnet (exists in config) + [SolScope.Mainnet]: false, // Solana Mainnet (exists in config) }, }, }); diff --git a/packages/network-enablement-controller/src/NetworkEnablementController.ts b/packages/network-enablement-controller/src/NetworkEnablementController.ts index 856f0884d34..5669a645e98 100644 --- a/packages/network-enablement-controller/src/NetworkEnablementController.ts +++ b/packages/network-enablement-controller/src/NetworkEnablementController.ts @@ -343,9 +343,9 @@ export class NetworkEnablementController extends BaseController< * Initializes the network enablement state from network controller configurations. * * This method reads the current network configurations from both NetworkController - * and MultichainNetworkController and initializes the enabled network map accordingly. - * It ensures proper namespace buckets exist for all configured networks and enables - * popular networks by default. + * and MultichainNetworkController and syncs the enabled network map accordingly. + * It ensures proper namespace buckets exist for all configured networks and only + * adds missing networks with a default value of false, preserving existing user settings. * * This method should be called after the NetworkController and MultichainNetworkController * have been initialized and their configurations are available. @@ -366,79 +366,27 @@ export class NetworkEnablementController extends BaseController< Object.keys( networkControllerState.networkConfigurationsByChainId, ).forEach((chainId) => { - const { namespace } = deriveKeys(chainId as Hex); + const { namespace, storageKey } = deriveKeys(chainId as Hex); this.#ensureNamespaceBucket(s, namespace); + + // Only add network if it doesn't already exist in state (preserves user settings) + if (s.enabledNetworkMap[namespace][storageKey] === undefined) { + s.enabledNetworkMap[namespace][storageKey] = false; + } }); // Initialize namespace buckets for all networks from MultichainNetworkController Object.keys( multichainState.multichainNetworkConfigurationsByChainId, ).forEach((chainId) => { - const { namespace } = deriveKeys(chainId as CaipChainId); + const { namespace, storageKey } = deriveKeys(chainId as CaipChainId); this.#ensureNamespaceBucket(s, namespace); - }); - // Enable popular networks that exist in the configurations - POPULAR_NETWORKS.forEach((chainId) => { - const { namespace, storageKey } = deriveKeys(chainId as Hex); - - // Check if network exists in NetworkController configurations - if ( - s.enabledNetworkMap[namespace] && - networkControllerState.networkConfigurationsByChainId[chainId as Hex] - ) { - s.enabledNetworkMap[namespace][storageKey] = true; + // Only add network if it doesn't already exist in state (preserves user settings) + if (s.enabledNetworkMap[namespace][storageKey] === undefined) { + s.enabledNetworkMap[namespace][storageKey] = false; } }); - - // Enable Solana mainnet if it exists in configurations - const solanaKeys = deriveKeys(SolScope.Mainnet as CaipChainId); - if ( - s.enabledNetworkMap[solanaKeys.namespace] && - multichainState.multichainNetworkConfigurationsByChainId[ - SolScope.Mainnet - ] - ) { - s.enabledNetworkMap[solanaKeys.namespace][solanaKeys.storageKey] = true; - } - - // Enable Bitcoin mainnet if it exists in configurations - const bitcoinKeys = deriveKeys(BtcScope.Mainnet as CaipChainId); - if ( - s.enabledNetworkMap[bitcoinKeys.namespace] && - multichainState.multichainNetworkConfigurationsByChainId[ - BtcScope.Mainnet - ] - ) { - s.enabledNetworkMap[bitcoinKeys.namespace][bitcoinKeys.storageKey] = - true; - } - - // Enable Bitcoin testnet if it exists in configurations - const bitcoinTestnetKeys = deriveKeys(BtcScope.Testnet as CaipChainId); - if ( - s.enabledNetworkMap[bitcoinTestnetKeys.namespace] && - multichainState.multichainNetworkConfigurationsByChainId[ - BtcScope.Testnet - ] - ) { - s.enabledNetworkMap[bitcoinTestnetKeys.namespace][ - bitcoinTestnetKeys.storageKey - ] = false; - } - - // Enable Bitcoin signet testnet if it exists in configurations - const bitcoinSignetKeys = deriveKeys(BtcScope.Signet as CaipChainId); - if ( - s.enabledNetworkMap[bitcoinSignetKeys.namespace] && - multichainState.multichainNetworkConfigurationsByChainId[ - BtcScope.Signet - ] - ) { - s.enabledNetworkMap[bitcoinSignetKeys.namespace][ - bitcoinSignetKeys.storageKey - ] = false; - } }); }