Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
03beba4
fix: migrate to network enablement controller
salimtb Sep 21, 2025
0fca3b8
fix: clean up
salimtb Sep 22, 2025
0287805
Update LavaMoat policies
metamaskbot Sep 22, 2025
3f8372c
fix: clean up
salimtb Sep 22, 2025
30a1844
Merge branch 'main' into fix/migrate-to-network-enablement
salimtb Sep 23, 2025
ff2518f
fix: clean up
salimtb Sep 23, 2025
139eb7e
fix: fix linter
salimtb Sep 23, 2025
24116e2
Merge branch 'main' into fix/migrate-to-network-enablement
salimtb Sep 23, 2025
46e9201
fix: fix linter
salimtb Sep 23, 2025
a5d748a
fix : fix e2e tests
salimtb Sep 23, 2025
67bcd00
fix: clean up
salimtb Sep 23, 2025
08fde97
fix: fix e2e tests
salimtb Sep 23, 2025
695f056
fix: clean up
salimtb Sep 23, 2025
37b38a9
Merge branch 'main' into fix/migrate-to-network-enablement
salimtb Sep 23, 2025
cf8baca
Merge branch 'main' into fix/migrate-to-network-enablement
salimtb Sep 23, 2025
5e1ca3f
fix: clean up
salimtb Sep 24, 2025
a642c5f
fix: clean up
salimtb Sep 24, 2025
de516ec
fix: clean up
salimtb Sep 24, 2025
cf85678
Merge branch 'main' into fix/migrate-to-network-enablement
salimtb Sep 24, 2025
487f19a
fix: clean up
salimtb Sep 24, 2025
3af5b7c
fix: clean up
salimtb Sep 24, 2025
5b63878
fix: clean up
salimtb Sep 24, 2025
568df46
fix: fix e2e tests
salimtb Sep 24, 2025
b01d26e
fix: fix e2e tests
salimtb Sep 24, 2025
abf7733
fix: fix e2e tests
salimtb Sep 24, 2025
9b35abc
fix: fix e2e tests
salimtb Sep 24, 2025
1652991
fix: fix linter
salimtb Sep 24, 2025
a1b479a
fix: fix PR comments
salimtb Sep 25, 2025
8b4547a
fix: fix PR comments
salimtb Sep 25, 2025
ef9e484
Merge branch 'main' into fix/migrate-to-network-enablement
salimtb Sep 25, 2025
5f84941
Merge branch 'main' into fix/migrate-to-network-enablement
salimtb Sep 25, 2025
60732c9
Merge branch 'main' into fix/migrate-to-network-enablement
salimtb Sep 25, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/scripts/constants/sentry-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export const SENTRY_BACKGROUND_STATE = {
},
NetworkOrderController: {
orderedNetworkList: [],
},
NetworkEnablementController: {
enabledNetworkMap: {},
},
AccountOrderController: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import {
NetworkEnablementController,
NetworkEnablementControllerState,
} from '@metamask/network-enablement-controller';
import { NetworkState } from '@metamask/network-controller';
import { MultichainNetworkControllerState } from '@metamask/multichain-network-controller';
import { NetworkEnablementControllerMessenger } from '../messengers/assets';
import { ControllerInitFunction } from '../types';
import { CHAIN_IDS } from '../../../../shared/constants/network';

/**
* Generates a map of EVM chain IDs to their enabled status based on NetworkController state.
*
* @param networkConfigurationsByChainId - The network configurations from NetworkController
* @param enabledChainId - The single chain ID that should be enabled
* @returns Record mapping chain IDs to boolean enabled status
*/
const generateEVMNetworkMap = (
networkConfigurationsByChainId: NetworkState['networkConfigurationsByChainId'],
enabledChainId?: string,
): Record<string, boolean> => {
const networkMap: Record<string, boolean> = {};

// Add all available EVM networks from NetworkController with default disabled status
Object.keys(networkConfigurationsByChainId).forEach((chainId) => {
networkMap[chainId] = chainId === enabledChainId;
});

return networkMap;
};

/**
* Generates a map of multichain networks organized by network type based on MultichainNetworkController state.
*
* @param multichainNetworkConfigurationsByChainId - The multichain network configurations
* @param enabledNetworks - Array of network IDs that should be enabled (empty by default)
* @returns Record mapping network types to their network maps
*/
const generateMultichainNetworkMaps = (
multichainNetworkConfigurationsByChainId: MultichainNetworkControllerState['multichainNetworkConfigurationsByChainId'],
enabledNetworks: string[] = [],
): Record<string, Record<string, boolean>> => {
const networkMaps: Record<string, Record<string, boolean>> = {
solana: {},
bitcoin: {},
};

// Organize multichain networks by their prefix/type
Object.keys(multichainNetworkConfigurationsByChainId).forEach((chainId) => {
const isEnabled = enabledNetworks.includes(chainId);

if (chainId.startsWith('solana:')) {
networkMaps.solana[chainId] = isEnabled;
} else if (chainId.startsWith('bip122:')) {
networkMaps.bitcoin[chainId] = isEnabled;
}
// Add other network types as needed
});

return networkMaps;
};

const generateDefaultNetworkEnablementControllerState = (
networkControllerState: NetworkState,
multichainNetworkControllerState: MultichainNetworkControllerState,
): NetworkEnablementControllerState => {
const { networkConfigurationsByChainId } = networkControllerState;
const { multichainNetworkConfigurationsByChainId } =
multichainNetworkControllerState;

// Generate multichain network maps (always empty for all environments currently)
const multichainMaps = generateMultichainNetworkMaps(
multichainNetworkConfigurationsByChainId,
[],
);

if (process.env.IN_TEST) {
return {
enabledNetworkMap: {
eip155: generateEVMNetworkMap(
networkConfigurationsByChainId,
CHAIN_IDS.LOCALHOST,
),
...multichainMaps,
},
};
} else if (
process.env.METAMASK_DEBUG ||
process.env.METAMASK_ENVIRONMENT === 'test'
) {
return {
enabledNetworkMap: {
eip155: generateEVMNetworkMap(
networkConfigurationsByChainId,
CHAIN_IDS.SEPOLIA,
),
...multichainMaps,
},
};
}

return {
enabledNetworkMap: {
eip155: generateEVMNetworkMap(
networkConfigurationsByChainId,
CHAIN_IDS.MAINNET,
),
...multichainMaps,
},
};
};

export const NetworkEnablementControllerInit: ControllerInitFunction<
NetworkEnablementController,
NetworkEnablementControllerMessenger
> = ({ controllerMessenger, persistedState, getController }) => {
const multichainNetworkControllerState = getController(
'MultichainNetworkController',
).state;

const networkControllerState = getController('NetworkController').state;

const controller = new NetworkEnablementController({
messenger: controllerMessenger,
state: {
...generateDefaultNetworkEnablementControllerState(
networkControllerState,
multichainNetworkControllerState,
),
...persistedState.NetworkEnablementController,
},
});

return {
controller,
};
};
Loading
Loading