From 50f0dc5469fc264dc1f58c7f3fe24d4c9c12d755 Mon Sep 17 00:00:00 2001 From: mikesposito Date: Wed, 22 Jul 2026 14:37:43 +0200 Subject: [PATCH 1/2] feat(ConfigRegistryController): add `getNetworkConfigByCaip2ChainId` --- .../config-registry-controller/CHANGELOG.md | 3 ++ ...gRegistryController-method-action-types.ts | 12 ++++++ .../src/ConfigRegistryController.test.ts | 40 +++++++++++++++++++ .../src/ConfigRegistryController.ts | 18 ++++++++- 4 files changed, 72 insertions(+), 1 deletion(-) diff --git a/packages/config-registry-controller/CHANGELOG.md b/packages/config-registry-controller/CHANGELOG.md index be0b7904c2e..165486d8acb 100644 --- a/packages/config-registry-controller/CHANGELOG.md +++ b/packages/config-registry-controller/CHANGELOG.md @@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Add `ConfigRegistryControllerStateChangedEvent` (`ConfigRegistryController:stateChanged`) to the controller's events ([#9595](https://github.com/MetaMask/core/pull/9595)) +- Add `ConfigRegistryController.getNetworkConfigByCaip2ChainId` method to retrieve a network config by its CAIP-2 chain ID ([#0000](https://github.com/MetaMask/core/pull/0000)) + - The method returns the network config if found, or `undefined` if not found. + - The method is also accessible via the controller's messenger as `ConfigRegistryController:getNetworkConfigByCaip2ChainId`. ### Changed diff --git a/packages/config-registry-controller/src/ConfigRegistryController-method-action-types.ts b/packages/config-registry-controller/src/ConfigRegistryController-method-action-types.ts index 4415001e537..8db316ca8e5 100644 --- a/packages/config-registry-controller/src/ConfigRegistryController-method-action-types.ts +++ b/packages/config-registry-controller/src/ConfigRegistryController-method-action-types.ts @@ -5,6 +5,17 @@ import type { ConfigRegistryController } from './ConfigRegistryController.js'; +/** + * Get the network configuration for a given CAIP-2 chain ID. + * + * @param caip2ChainId - The CAIP-2 chain ID (e.g., "eip155:1"). + * @returns The network configuration if found, otherwise undefined. + */ +export type ConfigRegistryControllerGetNetworkConfigByCaip2ChainIdAction = { + type: `ConfigRegistryController:getNetworkConfigByCaip2ChainId`; + handler: ConfigRegistryController['getNetworkConfigByCaip2ChainId']; +}; + /** * Stop all polling. */ @@ -22,5 +33,6 @@ export type ConfigRegistryControllerStartPollingAction = { * Union of all ConfigRegistryController action types. */ export type ConfigRegistryControllerMethodActions = + | ConfigRegistryControllerGetNetworkConfigByCaip2ChainIdAction | ConfigRegistryControllerStopPollingAction | ConfigRegistryControllerStartPollingAction; diff --git a/packages/config-registry-controller/src/ConfigRegistryController.test.ts b/packages/config-registry-controller/src/ConfigRegistryController.test.ts index 12c5dbb0f23..d737bd04c4a 100644 --- a/packages/config-registry-controller/src/ConfigRegistryController.test.ts +++ b/packages/config-registry-controller/src/ConfigRegistryController.test.ts @@ -927,6 +927,46 @@ describe('ConfigRegistryController', () => { }); }); + describe('getNetworkConfigByCaip2ChainId', () => { + it('returns the correct network config for a given CAIP-2 chainId', async () => { + const mockNetworkConfig = createMockNetworkConfig({ + chainId: 'eip155:1', + name: 'Ethereum Mainnet', + }); + await withController( + { + options: { + state: { + configs: { + networks: { + 'eip155:1': createMockNetworkConfig({ + chainId: 'eip155:1', + name: 'Ethereum Mainnet', + }), + }, + }, + }, + }, + }, + async ({ controller }) => { + const networkConfig = + controller.getNetworkConfigByCaip2ChainId('eip155:1'); + + expect(networkConfig).toStrictEqual(mockNetworkConfig); + }, + ); + }); + + it('returns undefined for a non-existent CAIP-2 chainId', async () => { + await withController(async ({ controller }) => { + const networkConfig = + controller.getNetworkConfigByCaip2ChainId('eip155:9999'); + + expect(networkConfig).toBeUndefined(); + }); + }); + }); + describe('feature flag', () => { it('uses API when feature flag is enabled', async () => { await withController( diff --git a/packages/config-registry-controller/src/ConfigRegistryController.ts b/packages/config-registry-controller/src/ConfigRegistryController.ts index 01c02a0f13c..2903f8ce5fa 100644 --- a/packages/config-registry-controller/src/ConfigRegistryController.ts +++ b/packages/config-registry-controller/src/ConfigRegistryController.ts @@ -94,7 +94,11 @@ const stateMetadata = { */ const DEFAULT_FALLBACK_CONFIG: Record = {}; -const MESSENGER_EXPOSED_METHODS = ['startPolling', 'stopPolling'] as const; +const MESSENGER_EXPOSED_METHODS = [ + 'startPolling', + 'stopPolling', + 'getNetworkConfigByCaip2ChainId', +] as const; /** * Published when the state of {@link ConfigRegistryController} changes. @@ -205,6 +209,18 @@ export class ConfigRegistryController extends StaticIntervalPollingController { try { const result = await this.messenger.call( From 318ec0e315072b47aa988ba49662fcc83cff47fc Mon Sep 17 00:00:00 2001 From: Michele Esposito <34438276+mikesposito@users.noreply.github.com> Date: Wed, 22 Jul 2026 14:45:12 +0200 Subject: [PATCH 2/2] update changelog --- packages/config-registry-controller/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/config-registry-controller/CHANGELOG.md b/packages/config-registry-controller/CHANGELOG.md index 165486d8acb..f78e83636aa 100644 --- a/packages/config-registry-controller/CHANGELOG.md +++ b/packages/config-registry-controller/CHANGELOG.md @@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Add `ConfigRegistryControllerStateChangedEvent` (`ConfigRegistryController:stateChanged`) to the controller's events ([#9595](https://github.com/MetaMask/core/pull/9595)) -- Add `ConfigRegistryController.getNetworkConfigByCaip2ChainId` method to retrieve a network config by its CAIP-2 chain ID ([#0000](https://github.com/MetaMask/core/pull/0000)) +- Add `ConfigRegistryController.getNetworkConfigByCaip2ChainId` method to retrieve a network config by its CAIP-2 chain ID ([#9597](https://github.com/MetaMask/core/pull/9597)) - The method returns the network config if found, or `undefined` if not found. - The method is also accessible via the controller's messenger as `ConfigRegistryController:getNetworkConfigByCaip2ChainId`.