@metamask-previews/network-controller 34.0.0-preview-75ce9748b → 34.0.0-preview-5cac90a

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
@@ -9,6 +9,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
9
9
 
10
10
  ### Changed
11
11
 
12
+ - **BREAKING:** `BuiltInNetworkClientId` is now `string` instead of `InfuraNetworkType` ([#9432](https://github.com/MetaMask/core/pull/9432))
13
+ - This type was previously constrained to known Infura network names (e.g. `"mainnet"`, `"sepolia"`). It is now `string` to allow dynamically configured Infura networks whose names are not bundled into the package.
14
+ - If you have code that narrows on `BuiltInNetworkClientId`, you will need to remove the narrowing or check the network client type via `NetworkClientType` instead.
15
+ - **BREAKING:** `getNetworkClientById` no longer uses the given network client ID to determine RPC endpoint type, prioritizing Infura RPC endpoints over custom RPC endpoints ([#9432](https://github.com/MetaMask/core/pull/9432))
16
+ - If you have an RPC endpoint with a `type` of `custom` but with a `networkClientId` that previously matched a known Infura network name (e.g. `mainnet`), this will now be treated as an Infura network rather than a custom network.
17
+ - This method is not only public but is also used internally to resolve network client IDs, so this is a change in behavior across the whole controller.
18
+ - `InfuraNetworkClientConfiguration.network` is now `string` instead of `InfuraNetworkType` ([#9432](https://github.com/MetaMask/core/pull/9432))
19
+ - Previously only known Infura network names were accepted. Any valid Infura subdomain string is now accepted.
20
+ - Remove validation that prevented a custom RPC endpoint from having a `networkClientId` that matches a known Infura network name ([#9432](https://github.com/MetaMask/core/pull/9432))
21
+ - Remove validation that required an Infura RPC endpoint URL's implied chain ID to match the chain ID of the network configuration it belongs to ([#9432](https://github.com/MetaMask/core/pull/9432))
22
+ - This allows Infura-backed networks to be added and updated dynamically without being constrained to the known Infura network list bundled in `@metamask/controller-utils`.
12
23
  - Bump `@metamask/messenger` from `^1.2.0` to `^2.0.0` ([#9392](https://github.com/MetaMask/core/pull/9392))
13
24
  - Bump `@metamask/connectivity-controller` from `^0.2.0` to `^0.3.0` ([#9435](https://github.com/MetaMask/core/pull/9435))
14
25
 
@@ -286,16 +286,12 @@ function isValidUrl(url) {
286
286
  *
287
287
  * @param rpcEndpointUrl - The URL to operate on.
288
288
  * @returns The Infura network name that the URL references.
289
- * @throws if the URL is not an Infura API URL, or if an Infura network is not
290
- * present in the URL.
289
+ * @throws if no Infura network is present in the URL.
291
290
  */
292
291
  function deriveInfuraNetworkNameFromRpcEndpointUrl(rpcEndpointUrl) {
293
292
  const match = INFURA_URL_REGEX.exec(rpcEndpointUrl);
294
293
  if (match?.groups) {
295
- if ((0, controller_utils_1.isInfuraNetworkType)(match.groups.networkName)) {
296
- return match.groups.networkName;
297
- }
298
- throw new Error(`Unknown Infura network '${match.groups.networkName}'`);
294
+ return match.groups.networkName;
299
295
  }
300
296
  throw new Error('Could not derive Infura network from RPC endpoint URL');
301
297
  }
@@ -531,18 +527,13 @@ class NetworkController extends base_controller_1.BaseController {
531
527
  throw new Error('No network client ID was provided.');
532
528
  }
533
529
  const autoManagedNetworkClientRegistry = __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_ensureAutoManagedNetworkClientRegistryPopulated).call(this);
534
- if ((0, controller_utils_1.isInfuraNetworkType)(networkClientId)) {
535
- const infuraNetworkClient = autoManagedNetworkClientRegistry[types_1.NetworkClientType.Infura][networkClientId];
536
- // This is impossible to reach
537
- /* istanbul ignore if */
538
- if (!infuraNetworkClient) {
539
- throw new Error(`No Infura network client was found with the ID "${networkClientId}".`);
540
- }
530
+ const infuraNetworkClient = autoManagedNetworkClientRegistry[types_1.NetworkClientType.Infura][networkClientId];
531
+ if (infuraNetworkClient) {
541
532
  return infuraNetworkClient;
542
533
  }
543
534
  const customNetworkClient = autoManagedNetworkClientRegistry[types_1.NetworkClientType.Custom][networkClientId];
544
535
  if (!customNetworkClient) {
545
- throw new Error(`No custom network client was found with the ID "${networkClientId}".`);
536
+ throw new Error(`No network client was found with ID "${networkClientId}".`);
546
537
  }
547
538
  return customNetworkClient;
548
539
  }
@@ -1053,7 +1044,6 @@ _NetworkController_ethQuery = new WeakMap(), _NetworkController_infuraProjectId
1053
1044
  const autoManagedNetworkClientRegistry = __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_ensureAutoManagedNetworkClientRegistryPopulated).call(this);
1054
1045
  for (const networkClientsById of Object.values(autoManagedNetworkClientRegistry)) {
1055
1046
  for (const networkClientId of Object.keys(networkClientsById)) {
1056
- // Type assertion: We can assume that `networkClientId` is valid here.
1057
1047
  const networkClient = networkClientsById[networkClientId];
1058
1048
  if (networkClient.configuration.failoverRpcUrls &&
1059
1049
  networkClient.configuration.failoverRpcUrls.length > 0) {
@@ -1101,10 +1091,7 @@ async function _NetworkController_refreshNetwork(networkClientId, options = {})
1101
1091
  * @returns The resulting metadata for the network.
1102
1092
  */
1103
1093
  async function _NetworkController_determineNetworkMetadata(networkClientId) {
1104
- // Force TypeScript to use one of the two overloads explicitly
1105
- const networkClient = (0, controller_utils_1.isInfuraNetworkType)(networkClientId)
1106
- ? this.getNetworkClientById(networkClientId)
1107
- : this.getNetworkClientById(networkClientId);
1094
+ const networkClient = this.getNetworkClientById(networkClientId);
1108
1095
  const isInfura = networkClient.configuration.type === types_1.NetworkClientType.Infura;
1109
1096
  let networkStatus;
1110
1097
  let isEIP1559Compatible;
@@ -1331,11 +1318,6 @@ async function _NetworkController_determineEIP1559Compatibility(networkClientId)
1331
1318
  const networkClientId = 'networkClientId' in rpcEndpointFields
1332
1319
  ? rpcEndpointFields.networkClientId
1333
1320
  : undefined;
1334
- if (rpcEndpointFields.type === RpcEndpointType.Custom &&
1335
- networkClientId !== undefined &&
1336
- (0, controller_utils_1.isInfuraNetworkType)(networkClientId)) {
1337
- throw new Error(`${errorMessagePrefix}: Custom RPC endpoint '${rpcEndpointFields.url}' has invalid network client ID '${networkClientId}'`);
1338
- }
1339
1321
  if (mode === 'update' &&
1340
1322
  networkClientId !== undefined &&
1341
1323
  rpcEndpointFields.type === RpcEndpointType.Custom &&
@@ -1378,17 +1360,6 @@ async function _NetworkController_determineEIP1559Compatibility(networkClientId)
1378
1360
  if (infuraRpcEndpoints.length > 1) {
1379
1361
  throw new Error(`${errorMessagePrefix}: There cannot be more than one Infura RPC endpoint`);
1380
1362
  }
1381
- const soleInfuraRpcEndpoint = infuraRpcEndpoints[0];
1382
- if (soleInfuraRpcEndpoint) {
1383
- const infuraNetworkName = deriveInfuraNetworkNameFromRpcEndpointUrl(soleInfuraRpcEndpoint.url);
1384
- const infuraNetworkNickname = controller_utils_1.NetworkNickname[infuraNetworkName];
1385
- const infuraChainId = controller_utils_1.ChainId[infuraNetworkName];
1386
- if (networkFields.chainId !== infuraChainId) {
1387
- throw new Error(mode === 'add'
1388
- ? `Could not add network with chain ID ${networkFields.chainId} and Infura RPC endpoint for '${infuraNetworkNickname}' which represents ${infuraChainId}, as the two conflict`
1389
- : `Could not update network with chain ID ${networkFields.chainId} and Infura RPC endpoint for '${infuraNetworkNickname}' which represents ${infuraChainId}, as the two conflict`);
1390
- }
1391
- }
1392
1363
  if (networkFields.rpcEndpoints[networkFields.defaultRpcEndpointIndex] ===
1393
1364
  undefined) {
1394
1365
  throw new Error(`${errorMessagePrefix}: \`defaultRpcEndpointIndex\` must refer to an entry in \`rpcEndpoints\``);
@@ -1550,25 +1521,7 @@ async function _NetworkController_determineEIP1559Compatibility(networkClientId)
1550
1521
  [types_1.NetworkClientType.Infura]: {},
1551
1522
  });
1552
1523
  }, _NetworkController_applyNetworkSelection = function _NetworkController_applyNetworkSelection(networkClientId, { updateState, } = {}) {
1553
- const autoManagedNetworkClientRegistry = __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_ensureAutoManagedNetworkClientRegistryPopulated).call(this);
1554
- let autoManagedNetworkClient;
1555
- if ((0, controller_utils_1.isInfuraNetworkType)(networkClientId)) {
1556
- const possibleAutoManagedNetworkClient = autoManagedNetworkClientRegistry[types_1.NetworkClientType.Infura][networkClientId];
1557
- // This is impossible to reach
1558
- /* istanbul ignore if */
1559
- if (!possibleAutoManagedNetworkClient) {
1560
- throw new Error(`No Infura network client found with ID '${networkClientId}'`);
1561
- }
1562
- autoManagedNetworkClient = possibleAutoManagedNetworkClient;
1563
- }
1564
- else {
1565
- const possibleAutoManagedNetworkClient = autoManagedNetworkClientRegistry[types_1.NetworkClientType.Custom][networkClientId];
1566
- if (!possibleAutoManagedNetworkClient) {
1567
- throw new Error(`No network client found with ID '${networkClientId}'`);
1568
- }
1569
- autoManagedNetworkClient = possibleAutoManagedNetworkClient;
1570
- }
1571
- __classPrivateFieldSet(this, _NetworkController_autoManagedNetworkClient, autoManagedNetworkClient, "f");
1524
+ __classPrivateFieldSet(this, _NetworkController_autoManagedNetworkClient, this.getNetworkClientById(networkClientId), "f");
1572
1525
  this.update((state) => {
1573
1526
  var _a;
1574
1527
  state.selectedNetworkClientId = networkClientId;