@metamask-previews/network-controller 22.0.1-preview-3f52b814 → 22.0.1-preview-c10c8117

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.
@@ -52,6 +52,7 @@ const util_1 = require("util");
52
52
  const uuid_1 = require("uuid");
53
53
  const constants_1 = require("./constants.cjs");
54
54
  const create_auto_managed_network_client_1 = require("./create-auto-managed-network-client.cjs");
55
+ const last_updated_at_network_configuration_1 = require("./last-updated-at-network-configuration.cjs");
55
56
  const logger_1 = require("./logger.cjs");
56
57
  const types_1 = require("./types.cjs");
57
58
  const debugLog = (0, logger_1.createModuleLogger)(logger_1.projectLogger, 'NetworkController');
@@ -336,6 +337,19 @@ class NetworkController extends base_controller_1.BaseController {
336
337
  // TODO: Either fix this lint violation or explain why it's necessary to ignore.
337
338
  // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
338
339
  `${this.name}:getSelectedNetworkClient`, this.getSelectedNetworkClient.bind(this));
340
+ this.messagingSystem.registerActionHandler(
341
+ // ESLint is mistaken here; `name` is a string.
342
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
343
+ `${this.name}:addNetwork`, this.addNetwork.bind(this));
344
+ this.messagingSystem.registerActionHandler(
345
+ // ESLint is mistaken here; `name` is a string.
346
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
347
+ `${this.name}:updateNetwork`, this.updateNetwork.bind(this));
348
+ this.messagingSystem.registerActionHandler(
349
+ // ESLint is mistaken here; `name` is a string.
350
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
351
+ `${this.name}:removeNetwork`, this.removeNetwork.bind(this));
352
+ this.messagingSystem.registerActionHandler(`${this.name}:dangerouslySetNetworkConfiguration`, this.dangerouslySetNetworkConfiguration.bind(this));
339
353
  }
340
354
  /**
341
355
  * Accesses the provider and block tracker for the currently selected network.
@@ -708,7 +722,7 @@ class NetworkController extends base_controller_1.BaseController {
708
722
  const rpcEndpoint = defaultOrCustomRpcEndpointFields.type === RpcEndpointType.Custom
709
723
  ? {
710
724
  ...defaultOrCustomRpcEndpointFields,
711
- networkClientId: (0, uuid_1.v4)(),
725
+ networkClientId: defaultOrCustomRpcEndpointFields.networkClientId ?? (0, uuid_1.v4)(),
712
726
  }
713
727
  : defaultOrCustomRpcEndpointFields;
714
728
  return {
@@ -949,6 +963,7 @@ class NetworkController extends base_controller_1.BaseController {
949
963
  networkClientOperations,
950
964
  autoManagedNetworkClientRegistry,
951
965
  });
966
+ this.messagingSystem.publish('NetworkController:networkUpdated', updatedNetworkConfiguration);
952
967
  return updatedNetworkConfiguration;
953
968
  }
954
969
  /**
@@ -987,6 +1002,79 @@ 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);
1006
+ }
1007
+ /**
1008
+ * This is used to override an existing network configuration.
1009
+ * This is only meant for internal use only and not to be exposed via the UI.
1010
+ * It is used as part of "Network Syncing", to sync networks, RPCs and block explorers cross devices.
1011
+ *
1012
+ * This will subsequently update the network client registry; state.networksMetadata, and state.selectedNetworkClientId
1013
+ * @param networkConfiguration - the network configuration to override
1014
+ */
1015
+ async dangerouslySetNetworkConfiguration(networkConfiguration) {
1016
+ const prevNetworkConfig = networkConfiguration.chainId in this.state.networkConfigurationsByChainId
1017
+ ? this.state.networkConfigurationsByChainId[networkConfiguration.chainId]
1018
+ : undefined;
1019
+ // Update Registry (remove old and add new)
1020
+ const autoManagedNetworkClientRegistry = __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_ensureAutoManagedNetworkClientRegistryPopulated).call(this);
1021
+ if (prevNetworkConfig) {
1022
+ const networkClientOperations = prevNetworkConfig.rpcEndpoints.map((rpcEndpoint) => {
1023
+ return {
1024
+ type: 'remove',
1025
+ rpcEndpoint,
1026
+ };
1027
+ });
1028
+ __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_unregisterNetworkClientsAsNeeded).call(this, {
1029
+ networkClientOperations,
1030
+ autoManagedNetworkClientRegistry,
1031
+ });
1032
+ }
1033
+ const networkClientOperations = networkConfiguration.rpcEndpoints.map((rpcEndpoint) => {
1034
+ return {
1035
+ type: 'add',
1036
+ rpcEndpoint,
1037
+ };
1038
+ });
1039
+ __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_registerNetworkClientsAsNeeded).call(this, {
1040
+ networkFields: networkConfiguration,
1041
+ networkClientOperations,
1042
+ autoManagedNetworkClientRegistry,
1043
+ });
1044
+ // update new `networkConfigurationsByChainId` (full replace)
1045
+ this.update((state) => {
1046
+ state.networkConfigurationsByChainId[networkConfiguration.chainId] =
1047
+ networkConfiguration;
1048
+ });
1049
+ __classPrivateFieldSet(this, _NetworkController_networkConfigurationsByNetworkClientId, buildNetworkConfigurationsByNetworkClientId(this.state.networkConfigurationsByChainId), "f");
1050
+ // update `networksMetadata` remove old
1051
+ if (prevNetworkConfig) {
1052
+ this.update((state) => {
1053
+ const newNetworksMetadata = {
1054
+ ...state.networksMetadata,
1055
+ };
1056
+ prevNetworkConfig.rpcEndpoints.forEach((rpcEndpoint) => {
1057
+ delete newNetworksMetadata[rpcEndpoint.networkClientId];
1058
+ });
1059
+ });
1060
+ }
1061
+ // update `networksMetadata` update new
1062
+ for (const rpcEndpoint of networkConfiguration.rpcEndpoints) {
1063
+ await this.lookupNetwork(rpcEndpoint.networkClientId);
1064
+ }
1065
+ // update selectedNetworkId
1066
+ const selectedClientId = this.state.selectedNetworkClientId;
1067
+ const wasReplacedClientId = prevNetworkConfig?.rpcEndpoints.some((r) => r.networkClientId === selectedClientId);
1068
+ const isValidClientIdInNewNetworks = networkConfiguration.rpcEndpoints.some((r) => r.networkClientId === selectedClientId);
1069
+ if (wasReplacedClientId) {
1070
+ if (!isValidClientIdInNewNetworks) {
1071
+ // Update clientId to something that exists
1072
+ const newSelectedNetworkMetadata = networkConfiguration.rpcEndpoints.find((r) => r.networkClientId in this.state.networksMetadata)?.networkClientId;
1073
+ const anyClientId = Object.keys(this.state.networksMetadata)[0];
1074
+ const newlySelectedNetwork = newSelectedNetworkMetadata ?? anyClientId ?? selectedClientId;
1075
+ await __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_refreshNetwork).call(this, newlySelectedNetwork);
1076
+ }
1077
+ }
990
1078
  }
991
1079
  /**
992
1080
  * Assuming that the network has been previously switched, switches to this
@@ -1280,6 +1368,7 @@ async function _NetworkController_determineEIP1559Compatibility(networkClientId)
1280
1368
  delete state.networkConfigurationsByChainId[args.existingNetworkConfiguration.chainId];
1281
1369
  }
1282
1370
  if (mode === 'add' || mode === 'update') {
1371
+ (0, last_updated_at_network_configuration_1.lastUpdatedNetworkConfiguration)(args.networkConfigurationToPersist);
1283
1372
  state.networkConfigurationsByChainId[args.networkFields.chainId] =
1284
1373
  args.networkConfigurationToPersist;
1285
1374
  }