@metamask/network-controller 23.1.0 → 23.2.1-backport

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.
Files changed (40) hide show
  1. package/dist/NetworkController.cjs +68 -9
  2. package/dist/NetworkController.cjs.map +1 -1
  3. package/dist/NetworkController.d.cts +20 -5
  4. package/dist/NetworkController.d.cts.map +1 -1
  5. package/dist/NetworkController.d.mts +20 -5
  6. package/dist/NetworkController.d.mts.map +1 -1
  7. package/dist/NetworkController.mjs +70 -11
  8. package/dist/NetworkController.mjs.map +1 -1
  9. package/dist/constants.cjs +7 -1
  10. package/dist/constants.cjs.map +1 -1
  11. package/dist/constants.d.cts +6 -0
  12. package/dist/constants.d.cts.map +1 -1
  13. package/dist/constants.d.mts +6 -0
  14. package/dist/constants.d.mts.map +1 -1
  15. package/dist/constants.mjs +6 -0
  16. package/dist/constants.mjs.map +1 -1
  17. package/dist/create-network-client.cjs +1 -1
  18. package/dist/create-network-client.cjs.map +1 -1
  19. package/dist/create-network-client.mjs +1 -1
  20. package/dist/create-network-client.mjs.map +1 -1
  21. package/dist/index.cjs.map +1 -1
  22. package/dist/index.d.cts +1 -1
  23. package/dist/index.d.cts.map +1 -1
  24. package/dist/index.d.mts +1 -1
  25. package/dist/index.d.mts.map +1 -1
  26. package/dist/index.mjs.map +1 -1
  27. package/dist/rpc-service/rpc-service.cjs +36 -46
  28. package/dist/rpc-service/rpc-service.cjs.map +1 -1
  29. package/dist/rpc-service/rpc-service.d.cts.map +1 -1
  30. package/dist/rpc-service/rpc-service.d.mts.map +1 -1
  31. package/dist/rpc-service/rpc-service.mjs +37 -47
  32. package/dist/rpc-service/rpc-service.mjs.map +1 -1
  33. package/dist/types.cjs.map +1 -1
  34. package/dist/types.d.cts +6 -2
  35. package/dist/types.d.cts.map +1 -1
  36. package/dist/types.d.mts +6 -2
  37. package/dist/types.d.mts.map +1 -1
  38. package/dist/types.mjs.map +1 -1
  39. package/package.json +5 -5
  40. package/CHANGELOG.md +0 -843
@@ -106,11 +106,33 @@ const controllerName = 'NetworkController';
106
106
  * Constructs a value for the state property `networkConfigurationsByChainId`
107
107
  * which will be used if it has not been provided to the constructor.
108
108
  *
109
+ * @param [additionalDefaultNetworks] - An array of Hex Chain IDs representing the additional networks to be included as default.
109
110
  * @returns The default value for `networkConfigurationsByChainId`.
110
111
  */
111
- function getDefaultNetworkConfigurationsByChainId() {
112
+ function getDefaultNetworkConfigurationsByChainId(additionalDefaultNetworks = []) {
113
+ const infuraNetworks = getDefaultInfuraNetworkConfigurationsByChainId();
114
+ const customNetworks = getDefaultCustomNetworkConfigurationsByChainId();
115
+ return additionalDefaultNetworks.reduce((obj, chainId) => {
116
+ if ((0, utils_1.hasProperty)(customNetworks, chainId)) {
117
+ obj[chainId] = customNetworks[chainId];
118
+ }
119
+ return obj;
120
+ },
121
+ // Always include the infura networks in the default networks
122
+ infuraNetworks);
123
+ }
124
+ /**
125
+ * Constructs a `networkConfigurationsByChainId` object for all default Infura networks.
126
+ *
127
+ * @returns The `networkConfigurationsByChainId` object of all Infura networks.
128
+ */
129
+ function getDefaultInfuraNetworkConfigurationsByChainId() {
112
130
  return Object.values(controller_utils_1.InfuraNetworkType).reduce((obj, infuraNetworkType) => {
113
131
  const chainId = controller_utils_1.ChainId[infuraNetworkType];
132
+ // Skip deprecated network as default network.
133
+ if (constants_1.DEPRECATED_NETWORKS.has(chainId)) {
134
+ return obj;
135
+ }
114
136
  const rpcEndpointUrl =
115
137
  // This ESLint rule mistakenly produces an error.
116
138
  // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
@@ -133,15 +155,42 @@ function getDefaultNetworkConfigurationsByChainId() {
133
155
  return { ...obj, [chainId]: networkConfiguration };
134
156
  }, {});
135
157
  }
158
+ /**
159
+ * Constructs a `networkConfigurationsByChainId` object for all default custom networks.
160
+ *
161
+ * @returns The `networkConfigurationsByChainId` object of all custom networks.
162
+ */
163
+ function getDefaultCustomNetworkConfigurationsByChainId() {
164
+ const { ticker, rpcPrefs } = controller_utils_1.BUILT_IN_NETWORKS[controller_utils_1.BuiltInNetworkName.MegaETHTestnet];
165
+ return {
166
+ [controller_utils_1.ChainId[controller_utils_1.BuiltInNetworkName.MegaETHTestnet]]: {
167
+ blockExplorerUrls: [rpcPrefs.blockExplorerUrl],
168
+ chainId: controller_utils_1.ChainId[controller_utils_1.BuiltInNetworkName.MegaETHTestnet],
169
+ defaultRpcEndpointIndex: 0,
170
+ defaultBlockExplorerUrlIndex: 0,
171
+ name: controller_utils_1.NetworkNickname[controller_utils_1.BuiltInNetworkName.MegaETHTestnet],
172
+ nativeCurrency: ticker,
173
+ rpcEndpoints: [
174
+ {
175
+ failoverUrls: [],
176
+ networkClientId: controller_utils_1.BuiltInNetworkName.MegaETHTestnet,
177
+ type: RpcEndpointType.Custom,
178
+ url: controller_utils_1.BUILT_IN_CUSTOM_NETWORKS_RPC.MEGAETH_TESTNET,
179
+ },
180
+ ],
181
+ },
182
+ };
183
+ }
136
184
  /**
137
185
  * Constructs properties for the NetworkController state whose values will be
138
186
  * used if not provided to the constructor.
139
187
  *
188
+ * @param [additionalDefaultNetworks] - An array of Hex Chain IDs representing the additional networks to be included as default.
140
189
  * @returns The default NetworkController state.
141
190
  */
142
- function getDefaultNetworkControllerState() {
191
+ function getDefaultNetworkControllerState(additionalDefaultNetworks) {
143
192
  const networksMetadata = {};
144
- const networkConfigurationsByChainId = getDefaultNetworkConfigurationsByChainId();
193
+ const networkConfigurationsByChainId = getDefaultNetworkConfigurationsByChainId(additionalDefaultNetworks);
145
194
  return {
146
195
  selectedNetworkClientId: controller_utils_1.InfuraNetworkType.mainnet,
147
196
  networksMetadata,
@@ -293,8 +342,11 @@ class NetworkController extends base_controller_1.BaseController {
293
342
  * @param options - The options; see {@link NetworkControllerOptions}.
294
343
  */
295
344
  constructor(options) {
296
- const { messenger, state, infuraProjectId, log, getRpcServiceOptions } = options;
297
- const initialState = { ...getDefaultNetworkControllerState(), ...state };
345
+ const { messenger, state, infuraProjectId, log, getRpcServiceOptions, additionalDefaultNetworks, } = options;
346
+ const initialState = {
347
+ ...getDefaultNetworkControllerState(additionalDefaultNetworks),
348
+ ...state,
349
+ };
298
350
  validateNetworkControllerState(initialState);
299
351
  if (!infuraProjectId || typeof infuraProjectId !== 'string') {
300
352
  throw new Error('Invalid Infura project ID');
@@ -368,10 +420,8 @@ class NetworkController extends base_controller_1.BaseController {
368
420
  // ESLint is mistaken here; `name` is a string.
369
421
  // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
370
422
  `${this.name}:getNetworkConfigurationByNetworkClientId`, this.getNetworkConfigurationByNetworkClientId.bind(this));
371
- this.messagingSystem.registerActionHandler(
372
- // TODO: Either fix this lint violation or explain why it's necessary to ignore.
373
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
374
- `${this.name}:getSelectedNetworkClient`, this.getSelectedNetworkClient.bind(this));
423
+ this.messagingSystem.registerActionHandler(`${this.name}:getSelectedNetworkClient`, this.getSelectedNetworkClient.bind(this));
424
+ this.messagingSystem.registerActionHandler(`${this.name}:getSelectedChainId`, this.getSelectedChainId.bind(this));
375
425
  this.messagingSystem.registerActionHandler(
376
426
  // ESLint is mistaken here; `name` is a string.
377
427
  // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
@@ -410,6 +460,15 @@ class NetworkController extends base_controller_1.BaseController {
410
460
  }
411
461
  return undefined;
412
462
  }
463
+ /**
464
+ * Accesses the chain ID from the selected network client.
465
+ *
466
+ * @returns The chain ID of the selected network client in hex format or undefined if there is no network client.
467
+ */
468
+ getSelectedChainId() {
469
+ const networkConfiguration = this.getNetworkConfigurationByNetworkClientId(this.state.selectedNetworkClientId);
470
+ return networkConfiguration?.chainId;
471
+ }
413
472
  /**
414
473
  * Internally, the Infura and custom network clients are categorized by type
415
474
  * so that when accessing either kind of network client, TypeScript knows