@metamask-previews/network-controller 22.0.2-preview-57e6ab5d → 22.0.2-preview-e58ce4ab

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.
@@ -45,9 +45,10 @@ const eth_query_1 = __importDefault(require("@metamask/eth-query"));
45
45
  const rpc_errors_1 = require("@metamask/rpc-errors");
46
46
  const swappable_obj_proxy_1 = require("@metamask/swappable-obj-proxy");
47
47
  const utils_1 = require("@metamask/utils");
48
- const fast_deep_equal_1 = __importDefault(require("fast-deep-equal"));
48
+ const assert_1 = require("assert");
49
49
  const reselect_1 = require("reselect");
50
50
  const URI = __importStar(require("uri-js"));
51
+ const util_1 = require("util");
51
52
  const uuid_1 = require("uuid");
52
53
  const constants_1 = require("./constants.cjs");
53
54
  const create_auto_managed_network_client_1 = require("./create-auto-managed-network-client.cjs");
@@ -111,7 +112,7 @@ function getDefaultNetworkConfigurationsByChainId() {
111
112
  return Object.values(controller_utils_1.InfuraNetworkType).reduce((obj, infuraNetworkType) => {
112
113
  const chainId = controller_utils_1.ChainId[infuraNetworkType];
113
114
  const rpcEndpointUrl =
114
- // This ESLint rule mistakenly produces an error.
115
+ // False negative - this is a string.
115
116
  // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
116
117
  `https://${infuraNetworkType}.infura.io/v3/{infuraProjectId}`;
117
118
  const networkConfiguration = {
@@ -236,10 +237,7 @@ function validateNetworkControllerState(state) {
236
237
  throw new Error('NetworkController state has invalid `networkConfigurationsByChainId`: Every RPC endpoint across all network configurations must have a unique `networkClientId`');
237
238
  }
238
239
  if (!networkClientIds.includes(state.selectedNetworkClientId)) {
239
- throw new Error(
240
- // This ESLint rule mistakenly produces an error.
241
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
242
- `NetworkController state is invalid: \`selectedNetworkClientId\` '${state.selectedNetworkClientId}' does not refer to an RPC endpoint within a network configuration`);
240
+ throw new Error(`NetworkController state is invalid: \`selectedNetworkClientId\` ${(0, util_1.inspect)(state.selectedNetworkClientId)} does not refer to an RPC endpoint within a network configuration`);
243
241
  }
244
242
  }
245
243
  /**
@@ -338,18 +336,6 @@ class NetworkController extends base_controller_1.BaseController {
338
336
  // TODO: Either fix this lint violation or explain why it's necessary to ignore.
339
337
  // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
340
338
  `${this.name}:getSelectedNetworkClient`, this.getSelectedNetworkClient.bind(this));
341
- this.messagingSystem.registerActionHandler(
342
- // ESLint is mistaken here; `name` is a string.
343
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
344
- `${this.name}:addNetwork`, this.addNetwork.bind(this));
345
- this.messagingSystem.registerActionHandler(
346
- // ESLint is mistaken here; `name` is a string.
347
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
348
- `${this.name}:removeNetwork`, this.removeNetwork.bind(this));
349
- this.messagingSystem.registerActionHandler(
350
- // ESLint is mistaken here; `name` is a string.
351
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
352
- `${this.name}:updateNetwork`, this.updateNetwork.bind(this));
353
339
  }
354
340
  /**
355
341
  * Accesses the provider and block tracker for the currently selected network.
@@ -609,15 +595,14 @@ class NetworkController extends base_controller_1.BaseController {
609
595
  * removed in a future release
610
596
  */
611
597
  async setProviderType(type) {
612
- if (type === controller_utils_1.NetworkType.rpc) {
613
- throw new Error(
614
- // This ESLint rule mistakenly produces an error.
615
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
616
- `NetworkController - cannot call "setProviderType" with type "${controller_utils_1.NetworkType.rpc}". Use "setActiveNetwork"`);
617
- }
618
- if (!(0, controller_utils_1.isInfuraNetworkType)(type)) {
619
- throw new Error(`Unknown Infura provider type "${String(type)}".`);
620
- }
598
+ assert_1.strict.notStrictEqual(type, controller_utils_1.NetworkType.rpc,
599
+ // TODO: Either fix this lint violation or explain why it's necessary to ignore.
600
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
601
+ `NetworkController - cannot call "setProviderType" with type "${controller_utils_1.NetworkType.rpc}". Use "setActiveNetwork"`);
602
+ assert_1.strict.ok((0, controller_utils_1.isInfuraNetworkType)(type),
603
+ // TODO: Either fix this lint violation or explain why it's necessary to ignore.
604
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
605
+ `Unknown Infura provider type "${type}".`);
621
606
  await this.setActiveNetwork(type);
622
607
  }
623
608
  /**
@@ -779,7 +764,7 @@ class NetworkController extends base_controller_1.BaseController {
779
764
  async updateNetwork(chainId, fields, { replacementSelectedRpcEndpointIndex, } = {}) {
780
765
  const existingNetworkConfiguration = this.state.networkConfigurationsByChainId[chainId];
781
766
  if (existingNetworkConfiguration === undefined) {
782
- throw new Error(`Could not update network: Cannot find network configuration for chain '${chainId}'`);
767
+ throw new Error(`Could not update network: Cannot find network configuration for chain ${(0, util_1.inspect)(chainId)}`);
783
768
  }
784
769
  const existingChainId = chainId;
785
770
  const { chainId: newChainId, rpcEndpoints: setOfNewRpcEndpointFields } = fields;
@@ -908,7 +893,7 @@ class NetworkController extends base_controller_1.BaseController {
908
893
  this.state.selectedNetworkClientId);
909
894
  })) {
910
895
  throw new Error(
911
- // This ESLint rule mistakenly produces an error.
896
+ // False negative - this is a string.
912
897
  // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
913
898
  `Could not update network: Cannot update RPC endpoints in such a way that the selected network '${this.state.selectedNetworkClientId}' would be removed without a replacement. Choose a different RPC endpoint as the selected network via the \`replacementSelectedRpcEndpointIndex\` option.`);
914
899
  }
@@ -978,7 +963,7 @@ class NetworkController extends base_controller_1.BaseController {
978
963
  removeNetwork(chainId) {
979
964
  const existingNetworkConfiguration = this.state.networkConfigurationsByChainId[chainId];
980
965
  if (existingNetworkConfiguration === undefined) {
981
- throw new Error(`Cannot find network configuration for chain '${chainId}'`);
966
+ throw new Error(`Cannot find network configuration for chain ${(0, util_1.inspect)(chainId)}`);
982
967
  }
983
968
  if (existingNetworkConfiguration.rpcEndpoints.some((rpcEndpoint) => rpcEndpoint.networkClientId === this.state.selectedNetworkClientId)) {
984
969
  throw new Error(`Cannot remove the currently selected network`);
@@ -1002,7 +987,6 @@ class NetworkController extends base_controller_1.BaseController {
1002
987
  });
1003
988
  });
1004
989
  __classPrivateFieldSet(this, _NetworkController_networkConfigurationsByNetworkClientId, buildNetworkConfigurationsByNetworkClientId(this.state.networkConfigurationsByChainId), "f");
1005
- this.messagingSystem.publish('NetworkController:networkRemoved', existingNetworkConfiguration);
1006
990
  }
1007
991
  /**
1008
992
  * Assuming that the network has been previously switched, switches to this
@@ -1118,7 +1102,7 @@ async function _NetworkController_determineEIP1559Compatibility(networkClientId)
1118
1102
  const errorMessagePrefix = mode === 'update' ? 'Could not update network' : 'Could not add network';
1119
1103
  if (!(0, utils_1.isStrictHexString)(networkFields.chainId) ||
1120
1104
  !(0, controller_utils_1.isSafeChainId)(networkFields.chainId)) {
1121
- throw new Error(`${errorMessagePrefix}: Invalid \`chainId\` '${networkFields.chainId}' (must start with "0x" and not exceed the maximum)`);
1105
+ throw new Error(`${errorMessagePrefix}: Invalid \`chainId\` ${(0, util_1.inspect)(networkFields.chainId)} (must start with "0x" and not exceed the maximum)`);
1122
1106
  }
1123
1107
  if (existingNetworkConfiguration === null ||
1124
1108
  networkFields.chainId !== existingNetworkConfiguration.chainId) {
@@ -1126,13 +1110,13 @@ async function _NetworkController_determineEIP1559Compatibility(networkClientId)
1126
1110
  if (existingNetworkConfigurationViaChainId !== undefined) {
1127
1111
  if (existingNetworkConfiguration === null) {
1128
1112
  throw new Error(
1129
- // This ESLint rule mistakenly produces an error.
1113
+ // False negative - these are strings.
1130
1114
  // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1131
1115
  `Could not add network for chain ${args.networkFields.chainId} as another network for that chain already exists ('${existingNetworkConfigurationViaChainId.name}')`);
1132
1116
  }
1133
1117
  else {
1134
1118
  throw new Error(
1135
- // This ESLint rule mistakenly produces an error.
1119
+ // False negative - these are strings.
1136
1120
  // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1137
1121
  `Cannot move network from chain ${existingNetworkConfiguration.chainId} to ${networkFields.chainId} as another network for that chain already exists ('${existingNetworkConfigurationViaChainId.name}')`);
1138
1122
  }
@@ -1150,10 +1134,7 @@ async function _NetworkController_determineEIP1559Compatibility(networkClientId)
1150
1134
  }
1151
1135
  for (const rpcEndpointFields of networkFields.rpcEndpoints) {
1152
1136
  if (!isValidUrl(rpcEndpointFields.url)) {
1153
- throw new Error(
1154
- // This ESLint rule mistakenly produces an error.
1155
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1156
- `${errorMessagePrefix}: An entry in \`rpcEndpoints\` has invalid URL '${rpcEndpointFields.url}'`);
1137
+ throw new Error(`${errorMessagePrefix}: An entry in \`rpcEndpoints\` has invalid URL ${(0, util_1.inspect)(rpcEndpointFields.url)}`);
1157
1138
  }
1158
1139
  const networkClientId = 'networkClientId' in rpcEndpointFields
1159
1140
  ? rpcEndpointFields.networkClientId
@@ -1170,10 +1151,10 @@ async function _NetworkController_determineEIP1559Compatibility(networkClientId)
1170
1151
  networkClientId !== undefined &&
1171
1152
  rpcEndpointFields.type === RpcEndpointType.Custom &&
1172
1153
  !Object.values(autoManagedNetworkClientRegistry).some((networkClientsById) => networkClientId in networkClientsById)) {
1173
- throw new Error(
1154
+ throw new Error(`${errorMessagePrefix}: RPC endpoint '${
1174
1155
  // This is a string.
1175
1156
  // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1176
- `${errorMessagePrefix}: RPC endpoint '${rpcEndpointFields.url}' refers to network client '${networkClientId}' that does not exist`);
1157
+ rpcEndpointFields.url}' refers to network client ${(0, util_1.inspect)(networkClientId)} that does not exist`);
1177
1158
  }
1178
1159
  if (networkFields.rpcEndpoints.some((otherRpcEndpointFields) => otherRpcEndpointFields !== rpcEndpointFields &&
1179
1160
  URI.equal(otherRpcEndpointFields.url, rpcEndpointFields.url))) {
@@ -1186,18 +1167,13 @@ async function _NetworkController_determineEIP1559Compatibility(networkClientId)
1186
1167
  for (const networkConfiguration of networkConfigurationsForOtherChains) {
1187
1168
  const rpcEndpoint = networkConfiguration.rpcEndpoints.find((existingRpcEndpoint) => URI.equal(rpcEndpointFields.url, existingRpcEndpoint.url));
1188
1169
  if (rpcEndpoint) {
1189
- if (mode === 'update') {
1190
- throw new Error(
1191
- // This ESLint rule mistakenly produces an error.
1192
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1193
- `Could not update network to point to same RPC endpoint as existing network for chain ${networkConfiguration.chainId} ('${networkConfiguration.name}')`);
1194
- }
1195
- else {
1196
- throw new Error(
1197
- // This ESLint rule mistakenly produces an error.
1198
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1199
- `Could not add network that points to same RPC endpoint as existing network for chain ${networkConfiguration.chainId} ('${networkConfiguration.name}')`);
1200
- }
1170
+ throw new Error(mode === 'update'
1171
+ ? // False negative - these are strings.
1172
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1173
+ `Could not update network to point to same RPC endpoint as existing network for chain ${networkConfiguration.chainId} ('${networkConfiguration.name}')`
1174
+ : // False negative - these are strings.
1175
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1176
+ `Could not add network that points to same RPC endpoint as existing network for chain ${networkConfiguration.chainId} ('${networkConfiguration.name}')`);
1201
1177
  }
1202
1178
  }
1203
1179
  }
@@ -1304,9 +1280,6 @@ async function _NetworkController_determineEIP1559Compatibility(networkClientId)
1304
1280
  delete state.networkConfigurationsByChainId[args.existingNetworkConfiguration.chainId];
1305
1281
  }
1306
1282
  if (mode === 'add' || mode === 'update') {
1307
- if (!(0, fast_deep_equal_1.default)(state.networkConfigurationsByChainId[args.networkFields.chainId], args.networkConfigurationToPersist)) {
1308
- args.networkConfigurationToPersist.lastUpdatedAt = Date.now();
1309
- }
1310
1283
  state.networkConfigurationsByChainId[args.networkFields.chainId] =
1311
1284
  args.networkConfigurationToPersist;
1312
1285
  }
@@ -1361,14 +1334,14 @@ async function _NetworkController_determineEIP1559Compatibility(networkClientId)
1361
1334
  // This is impossible to reach
1362
1335
  /* istanbul ignore if */
1363
1336
  if (!possibleAutoManagedNetworkClient) {
1364
- throw new Error(`No Infura network client found with ID '${networkClientId}'`);
1337
+ throw new Error(`No Infura network client found with ID ${(0, util_1.inspect)(networkClientId)}`);
1365
1338
  }
1366
1339
  autoManagedNetworkClient = possibleAutoManagedNetworkClient;
1367
1340
  }
1368
1341
  else {
1369
1342
  const possibleAutoManagedNetworkClient = autoManagedNetworkClientRegistry[types_1.NetworkClientType.Custom][networkClientId];
1370
1343
  if (!possibleAutoManagedNetworkClient) {
1371
- throw new Error(`No network client found with ID '${networkClientId}'`);
1344
+ throw new Error(`No network client found with ID ${(0, util_1.inspect)(networkClientId)}`);
1372
1345
  }
1373
1346
  autoManagedNetworkClient = possibleAutoManagedNetworkClient;
1374
1347
  }