@metamask-previews/network-controller 25.0.0-preview-4419d7e4 → 25.0.0-preview-359767df

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -13,6 +13,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
13
13
  - Providers accessible either via network clients or global proxies no longer emit events (or inherit from EventEmitter, for that matter).
14
14
  - Bump `@metamask/controller-utils` from `^11.14.1` to `^11.15.0` ([#7003](https://github.com/MetaMask/core/pull/7003))
15
15
 
16
+ ### Fixed
17
+
18
+ - Ensure `networksMetadata` never references old network client IDs ([#7047](https://github.com/MetaMask/core/pull/7047))
19
+ - When removing a network configuration, ensure that metadata for all RPC endpoints in the network configuration are also removed from `networksMetadata`
20
+ - When initializing the controller, remove metadata for RPC endpoints in `networksMetadata` that are not present in a network configuration
21
+
16
22
  ## [25.0.0]
17
23
 
18
24
  ### Changed
@@ -335,14 +335,21 @@ function validateInitialState(state) {
335
335
  */
336
336
  function correctInitialState(state, messenger) {
337
337
  const networkConfigurationsSortedByChainId = getNetworkConfigurations(state).sort((a, b) => a.chainId.localeCompare(b.chainId));
338
- const networkClientIds = getAvailableNetworkClientIds(networkConfigurationsSortedByChainId);
338
+ const availableNetworkClientIds = getAvailableNetworkClientIds(networkConfigurationsSortedByChainId);
339
+ const invalidNetworkClientIdsWithMetadata = Object.keys(state.networksMetadata).filter((networkClientId) => !availableNetworkClientIds.includes(networkClientId));
339
340
  return (0, immer_1.produce)(state, (newState) => {
340
- if (!networkClientIds.includes(state.selectedNetworkClientId)) {
341
+ if (!availableNetworkClientIds.includes(state.selectedNetworkClientId)) {
341
342
  const firstNetworkConfiguration = networkConfigurationsSortedByChainId[0];
342
343
  const newSelectedNetworkClientId = firstNetworkConfiguration.rpcEndpoints[firstNetworkConfiguration.defaultRpcEndpointIndex].networkClientId;
343
344
  messenger.call('ErrorReportingService:captureException', new Error(`\`selectedNetworkClientId\` '${state.selectedNetworkClientId}' does not refer to an RPC endpoint within a network configuration; correcting to '${newSelectedNetworkClientId}'`));
344
345
  newState.selectedNetworkClientId = newSelectedNetworkClientId;
345
346
  }
347
+ if (invalidNetworkClientIdsWithMetadata.length > 0) {
348
+ for (const invalidNetworkClientId of invalidNetworkClientIdsWithMetadata) {
349
+ delete newState.networksMetadata[invalidNetworkClientId];
350
+ }
351
+ messenger.call('ErrorReportingService:captureException', new Error('`networksMetadata` had invalid network client IDs, which have been removed'));
352
+ }
346
353
  });
347
354
  }
348
355
  /**
@@ -962,6 +969,9 @@ class NetworkController extends base_controller_1.BaseController {
962
969
  mode: 'remove',
963
970
  existingNetworkConfiguration,
964
971
  });
972
+ for (const rpcEndpoint of existingNetworkConfiguration.rpcEndpoints) {
973
+ delete state.networksMetadata[rpcEndpoint.networkClientId];
974
+ }
965
975
  });
966
976
  this.messenger.publish('NetworkController:networkRemoved', existingNetworkConfiguration);
967
977
  }
@@ -1236,6 +1246,8 @@ async function _NetworkController_lookupSelectedNetwork() {
1236
1246
  return new Promise((resolve, reject) => {
1237
1247
  ethQuery.sendAsync({ method: 'eth_getBlockByNumber', params: ['latest', false] }, (error, block) => {
1238
1248
  if (error) {
1249
+ // This error comes from JsonRpcEngine, we don't control it.
1250
+ // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
1239
1251
  reject(error);
1240
1252
  }
1241
1253
  else {