@metamask-previews/network-controller 22.0.2-preview-1c8fd633 → 22.0.2-preview-a82fd89

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