@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.
- package/dist/NetworkController.cjs +68 -9
- package/dist/NetworkController.cjs.map +1 -1
- package/dist/NetworkController.d.cts +20 -5
- package/dist/NetworkController.d.cts.map +1 -1
- package/dist/NetworkController.d.mts +20 -5
- package/dist/NetworkController.d.mts.map +1 -1
- package/dist/NetworkController.mjs +70 -11
- package/dist/NetworkController.mjs.map +1 -1
- package/dist/constants.cjs +7 -1
- package/dist/constants.cjs.map +1 -1
- package/dist/constants.d.cts +6 -0
- package/dist/constants.d.cts.map +1 -1
- package/dist/constants.d.mts +6 -0
- package/dist/constants.d.mts.map +1 -1
- package/dist/constants.mjs +6 -0
- package/dist/constants.mjs.map +1 -1
- package/dist/create-network-client.cjs +1 -1
- package/dist/create-network-client.cjs.map +1 -1
- package/dist/create-network-client.mjs +1 -1
- package/dist/create-network-client.mjs.map +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/rpc-service/rpc-service.cjs +36 -46
- package/dist/rpc-service/rpc-service.cjs.map +1 -1
- package/dist/rpc-service/rpc-service.d.cts.map +1 -1
- package/dist/rpc-service/rpc-service.d.mts.map +1 -1
- package/dist/rpc-service/rpc-service.mjs +37 -47
- package/dist/rpc-service/rpc-service.mjs.map +1 -1
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +6 -2
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +6 -2
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs.map +1 -1
- package/package.json +5 -5
- 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 = {
|
|
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
|
-
|
|
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
|