Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions packages/network-enablement-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- Clean up Slip44Service ([#7626](https://github.com/MetaMask/core/pull/7626))
- Add missing MegaETH to POPULARE_NETWORKS list ([#7286](https://github.com/MetaMask/core/pull/7286))

## [4.0.0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,18 @@ import type {
import { Slip44Service } from './services';
import { advanceTime } from '../../../tests/helpers';

// Mock Slip44Service.getSlip44ByChainId to avoid network calls
jest
.spyOn(Slip44Service, 'getSlip44ByChainId')
.mockImplementation(async (chainId: number, symbol?: string) => {
// Known chainId mappings from chainid.network
const chainIdToSlip44: Record<number, number> = {
1: 60, // Ethereum
10: 60, // Optimism
56: 714, // BNB Chain
137: 966, // Polygon
43114: 9000, // Avalanche
42161: 60, // Arbitrum
8453: 60, // Base
59144: 60, // Linea
1329: 60, // Sei (uses ETH as native)
};
if (chainIdToSlip44[chainId] !== undefined) {
return chainIdToSlip44[chainId];
}
// Fall back to symbol lookup if chainId not found
if (symbol) {
return Slip44Service.getSlip44BySymbol(symbol);
}
return undefined;
});
// Known chainId mappings from chainid.network for mocking
const chainIdToSlip44: Record<number, number> = {
1: 60, // Ethereum
10: 60, // Optimism
56: 714, // BNB Chain
137: 966, // Polygon
43114: 9000, // Avalanche
42161: 60, // Arbitrum
8453: 60, // Base
59144: 60, // Linea
1329: 60, // Sei (uses ETH as native)
};

const controllerName = 'NetworkEnablementController';

Expand Down Expand Up @@ -159,10 +146,17 @@ describe('NetworkEnablementController', () => {

beforeEach(() => {
clock = useFakeTimers();
// Mock Slip44Service.getEvmSlip44 to avoid network calls
jest
.spyOn(Slip44Service, 'getEvmSlip44')
.mockImplementation(async (chainId) => {
return chainIdToSlip44[chainId] ?? 60;
});
});

afterEach(() => {
clock.restore();
jest.restoreAllMocks();
});

it('initializes with default state', () => {
Expand Down Expand Up @@ -1585,7 +1579,7 @@ describe('NetworkEnablementController', () => {
},
nativeAssetIdentifiers: {
...getDefaultNativeAssetIdentifiers(),
'eip155:2': 'eip155:2/slip44:966', // MATIC
'eip155:2': 'eip155:2/slip44:60', // Defaults to 60 as chainId 2 is not in chainid.network
},
});

Expand Down Expand Up @@ -1623,7 +1617,7 @@ describe('NetworkEnablementController', () => {
},
nativeAssetIdentifiers: {
...getDefaultNativeAssetIdentifiers(),
'eip155:2': 'eip155:2/slip44:966', // MATIC
'eip155:2': 'eip155:2/slip44:60', // Defaults to 60 as chainId 2 is not in chainid.network
},
});

Expand Down Expand Up @@ -1661,7 +1655,7 @@ describe('NetworkEnablementController', () => {
},
nativeAssetIdentifiers: {
...getDefaultNativeAssetIdentifiers(),
'eip155:2': 'eip155:2/slip44:966', // MATIC
'eip155:2': 'eip155:2/slip44:60', // Defaults to 60 as chainId 2 is not in chainid.network
},
});
});
Expand Down Expand Up @@ -1805,8 +1799,10 @@ describe('NetworkEnablementController', () => {
},
nativeAssetIdentifiers: {
...getDefaultNativeAssetIdentifiers(),
// Note: This is testing invalid input (non-EVM chainId to EVM event handler)
// getEvmSlip44 defaults to 60 for unknown chainIds
'bip122:000000000019d6689c085ae165831e93':
'bip122:000000000019d6689c085ae165831e93/slip44:0', // BTC
'bip122:000000000019d6689c085ae165831e93/slip44:60',
},
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,10 @@ export class NetworkEnablementController extends BaseController<
},
});

messenger.subscribe(
'NetworkController:networkAdded',
({ chainId, nativeCurrency }) => {
// eslint-disable-next-line no-void
void this.#onAddNetwork(chainId, nativeCurrency);
},
);
messenger.subscribe('NetworkController:networkAdded', ({ chainId }) => {
// eslint-disable-next-line no-void
void this.#onAddNetwork(chainId);
});

messenger.subscribe('NetworkController:networkRemoved', ({ chainId }) => {
this.#removeNetworkEntry(chainId);
Expand Down Expand Up @@ -449,7 +446,7 @@ export class NetworkEnablementController extends BaseController<
identifier: NativeAssetIdentifier;
}[] = [];

for (const [chainId, config] of Object.entries(
for (const [chainId] of Object.entries(
networkControllerState.networkConfigurationsByChainId,
)) {
const { caipChainId } = deriveKeys(chainId as Hex);
Expand All @@ -462,13 +459,8 @@ export class NetworkEnablementController extends BaseController<
// Parse hex chainId to number for chainid.network lookup
const numericChainId = parseInt(chainId, 16);

// EVM networks: use getSlip44ByChainId (chainid.network data)
// Default to 60 (Ethereum) if no specific mapping is found
const slip44CoinType =
(await Slip44Service.getSlip44ByChainId(
numericChainId,
config.nativeCurrency,
)) ?? 60;
// EVM networks: use getEvmSlip44 (chainid.network data)
const slip44CoinType = await Slip44Service.getEvmSlip44(numericChainId);

evmNativeAssetUpdates.push({
caipChainId,
Expand Down Expand Up @@ -553,14 +545,9 @@ export class NetworkEnablementController extends BaseController<
let slip44CoinType: number | undefined;

if (namespace === 'eip155') {
// EVM networks: use getSlip44ByChainId (chainid.network data)
// Default to 60 (Ethereum) if no specific mapping is found
// EVM networks: use getEvmSlip44 (chainid.network data)
const numericChainId = parseInt(reference, 10);
slip44CoinType =
(await Slip44Service.getSlip44ByChainId(
numericChainId,
nativeCurrency,
)) ?? 60;
slip44CoinType = await Slip44Service.getEvmSlip44(numericChainId);
} else {
// Non-EVM networks: use getSlip44BySymbol (@metamask/slip44 package)
slip44CoinType = Slip44Service.getSlip44BySymbol(nativeCurrency);
Expand Down Expand Up @@ -708,7 +695,6 @@ export class NetworkEnablementController extends BaseController<
* Handles the addition of a new EVM network to the controller.
*
* @param chainId - The chain ID to add (Hex format)
* @param nativeCurrency - The native currency symbol of the network (e.g., 'ETH')
*
* @description
* - If in popular networks mode (>2 popular networks enabled) AND adding a popular network:
Expand All @@ -717,20 +703,15 @@ export class NetworkEnablementController extends BaseController<
* - Switch to the newly added network (disable all others, enable this one)
* - Also updates the nativeAssetIdentifiers with the CAIP-19-like identifier
*/
async #onAddNetwork(chainId: Hex, nativeCurrency: string): Promise<void> {
async #onAddNetwork(chainId: Hex): Promise<void> {
const { namespace, storageKey, reference, caipChainId } =
deriveKeys(chainId);

// Parse hex chainId to number for chainid.network lookup
const numericChainId = parseInt(reference, 16);
// Parse reference (decimal string from CAIP-2) to number for chainid.network lookup
const numericChainId = parseInt(reference, 10);

// EVM networks: use getSlip44ByChainId (chainid.network data)
// Default to 60 (Ethereum) if no specific mapping is found
const slip44CoinType =
(await Slip44Service.getSlip44ByChainId(
numericChainId,
nativeCurrency,
)) ?? 60;
// EVM networks: use getEvmSlip44 (chainid.network data)
const slip44CoinType = await Slip44Service.getEvmSlip44(numericChainId);

this.update((state) => {
// Ensure the namespace bucket exists
Expand Down
7 changes: 1 addition & 6 deletions packages/network-enablement-controller/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,4 @@ export {
selectEnabledSolanaNetworks,
} from './selectors';

export {
Slip44Service,
getSlip44BySymbol,
getSlip44ByChainId,
} from './services';
export type { Slip44Entry } from './services';
export { Slip44Service, getEvmSlip44, getSlip44BySymbol } from './services';
Loading
Loading