@metamask-previews/network-controller 34.0.0-preview-ec4680201 → 34.0.0-preview-40d5fb1e2

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,17 +9,6 @@ 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`.
23
12
  - Bump `@metamask/messenger` from `^1.2.0` to `^2.0.0` ([#9392](https://github.com/MetaMask/core/pull/9392))
24
13
  - Bump `@metamask/connectivity-controller` from `^0.2.0` to `^0.3.0` ([#9435](https://github.com/MetaMask/core/pull/9435))
25
14
 
@@ -286,12 +286,16 @@ 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 no Infura network is present in the URL.
289
+ * @throws if the URL is not an Infura API URL, or if an Infura network is not
290
+ * present in the URL.
290
291
  */
291
292
  function deriveInfuraNetworkNameFromRpcEndpointUrl(rpcEndpointUrl) {
292
293
  const match = INFURA_URL_REGEX.exec(rpcEndpointUrl);
293
294
  if (match?.groups) {
294
- return match.groups.networkName;
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}'`);
295
299
  }
296
300
  throw new Error('Could not derive Infura network from RPC endpoint URL');
297
301
  }
@@ -527,13 +531,18 @@ class NetworkController extends base_controller_1.BaseController {
527
531
  throw new Error('No network client ID was provided.');
528
532
  }
529
533
  const autoManagedNetworkClientRegistry = __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_ensureAutoManagedNetworkClientRegistryPopulated).call(this);
530
- const infuraNetworkClient = autoManagedNetworkClientRegistry[types_1.NetworkClientType.Infura][networkClientId];
531
- if (infuraNetworkClient) {
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
+ }
532
541
  return infuraNetworkClient;
533
542
  }
534
543
  const customNetworkClient = autoManagedNetworkClientRegistry[types_1.NetworkClientType.Custom][networkClientId];
535
544
  if (!customNetworkClient) {
536
- throw new Error(`No network client was found with ID "${networkClientId}".`);
545
+ throw new Error(`No custom network client was found with the ID "${networkClientId}".`);
537
546
  }
538
547
  return customNetworkClient;
539
548
  }
@@ -1044,6 +1053,7 @@ _NetworkController_ethQuery = new WeakMap(), _NetworkController_infuraProjectId
1044
1053
  const autoManagedNetworkClientRegistry = __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_ensureAutoManagedNetworkClientRegistryPopulated).call(this);
1045
1054
  for (const networkClientsById of Object.values(autoManagedNetworkClientRegistry)) {
1046
1055
  for (const networkClientId of Object.keys(networkClientsById)) {
1056
+ // Type assertion: We can assume that `networkClientId` is valid here.
1047
1057
  const networkClient = networkClientsById[networkClientId];
1048
1058
  if (networkClient.configuration.failoverRpcUrls &&
1049
1059
  networkClient.configuration.failoverRpcUrls.length > 0) {
@@ -1091,7 +1101,10 @@ async function _NetworkController_refreshNetwork(networkClientId, options = {})
1091
1101
  * @returns The resulting metadata for the network.
1092
1102
  */
1093
1103
  async function _NetworkController_determineNetworkMetadata(networkClientId) {
1094
- const networkClient = this.getNetworkClientById(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);
1095
1108
  const isInfura = networkClient.configuration.type === types_1.NetworkClientType.Infura;
1096
1109
  let networkStatus;
1097
1110
  let isEIP1559Compatible;
@@ -1318,6 +1331,11 @@ async function _NetworkController_determineEIP1559Compatibility(networkClientId)
1318
1331
  const networkClientId = 'networkClientId' in rpcEndpointFields
1319
1332
  ? rpcEndpointFields.networkClientId
1320
1333
  : 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
+ }
1321
1339
  if (mode === 'update' &&
1322
1340
  networkClientId !== undefined &&
1323
1341
  rpcEndpointFields.type === RpcEndpointType.Custom &&
@@ -1360,6 +1378,17 @@ async function _NetworkController_determineEIP1559Compatibility(networkClientId)
1360
1378
  if (infuraRpcEndpoints.length > 1) {
1361
1379
  throw new Error(`${errorMessagePrefix}: There cannot be more than one Infura RPC endpoint`);
1362
1380
  }
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
+ }
1363
1392
  if (networkFields.rpcEndpoints[networkFields.defaultRpcEndpointIndex] ===
1364
1393
  undefined) {
1365
1394
  throw new Error(`${errorMessagePrefix}: \`defaultRpcEndpointIndex\` must refer to an entry in \`rpcEndpoints\``);
@@ -1521,7 +1550,25 @@ async function _NetworkController_determineEIP1559Compatibility(networkClientId)
1521
1550
  [types_1.NetworkClientType.Infura]: {},
1522
1551
  });
1523
1552
  }, _NetworkController_applyNetworkSelection = function _NetworkController_applyNetworkSelection(networkClientId, { updateState, } = {}) {
1524
- __classPrivateFieldSet(this, _NetworkController_autoManagedNetworkClient, this.getNetworkClientById(networkClientId), "f");
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");
1525
1572
  this.update((state) => {
1526
1573
  var _a;
1527
1574
  state.selectedNetworkClientId = networkClientId;