Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@metamask-previews/network-controller

Package Overview
Dependencies
Maintainers
2
Versions
386
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@metamask-previews/network-controller - npm Package Compare versions

Comparing version 12.1.2-preview.f71e42f to 12.2.0-preview.56ca173

11

CHANGELOG.md

@@ -9,2 +9,10 @@ # Changelog

## [12.2.0]
### Added
- Add `NetworkController:getNetworkClientById` action ([#1638](https://github.com/MetaMask/core/pull/1638))
- Add `lookupNetworkByClientId` and `get1555CompatibilityWithNetworkClientId` methods ([#1557](https://github.com/MetaMask/core/pull/1557))
### Changed
- Add optional `networkClientId` argument to methods `lookupNetwork` and `getEIP1559Compatibility` ([#1557](https://github.com/MetaMask/core/pull/1557))
## [12.1.2]

@@ -249,3 +257,4 @@ ### Changed

[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/network-controller@12.1.2...HEAD
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/network-controller@12.2.0...HEAD
[12.2.0]: https://github.com/MetaMask/core/compare/@metamask/network-controller@12.1.2...@metamask/network-controller@12.2.0
[12.1.2]: https://github.com/MetaMask/core/compare/@metamask/network-controller@12.1.1...@metamask/network-controller@12.1.2

@@ -252,0 +261,0 @@ [12.1.1]: https://github.com/MetaMask/core/compare/@metamask/network-controller@12.1.0...@metamask/network-controller@12.1.1

@@ -99,2 +99,3 @@ "use strict";

eth_chainId: controller_utils_1.ChainId[network],
net_version: controller_utils_1.NetworkId[network],
});

@@ -101,0 +102,0 @@ }

@@ -108,2 +108,6 @@ import type { RestrictedControllerMessenger } from '@metamask/base-controller';

/**
* The network ID of a network.
*/
export declare type NetworkId = `${number}`;
/**
* @type NetworkState

@@ -119,2 +123,3 @@ *

selectedNetworkClientId: NetworkClientId;
networkId: NetworkId | null;
providerConfig: ProviderConfig;

@@ -193,3 +198,7 @@ networkConfigurations: NetworkConfigurations;

};
export declare type NetworkControllerActions = NetworkControllerGetStateAction | NetworkControllerGetProviderConfigAction | NetworkControllerGetEthQueryAction;
export declare type NetworkControllerGetNetworkClientByIdAction = {
type: `NetworkController:getNetworkClientById`;
handler: NetworkController['getNetworkClientById'];
};
export declare type NetworkControllerActions = NetworkControllerGetStateAction | NetworkControllerGetProviderConfigAction | NetworkControllerGetEthQueryAction | NetworkControllerGetNetworkClientByIdAction;
export declare type NetworkControllerMessenger = RestrictedControllerMessenger<typeof name, NetworkControllerActions, NetworkControllerEvents, string, string>;

@@ -258,2 +267,9 @@ export declare type NetworkControllerOptions = {

/**
* Refreshes the network meta with EIP-1559 support and the network status
* based on the given network client ID.
*
* @param networkClientId - The ID of the network client to update.
*/
lookupNetworkByClientId(networkClientId: NetworkClientId): Promise<void>;
/**
* Performs side effects after switching to a network. If the network is

@@ -264,2 +280,4 @@ * available, updates the network state with the network ID of the network and

*
* @param networkClientId - (Optional) The ID of the network client to update.
* If no ID is provided, uses the currently selected network.
* @fires infuraIsBlocked if the network is Infura-supported and is blocking

@@ -270,3 +288,3 @@ * requests.

*/
lookupNetwork(): Promise<void>;
lookupNetwork(networkClientId?: NetworkClientId): Promise<void>;
/**

@@ -289,6 +307,8 @@ * Convenience method to update provider network type settings.

*
* @param networkClientId - The networkClientId to fetch the correct provider against which to check 1559 compatibility.
* @returns A promise that resolves to true if the network supports EIP-1559
* , false otherwise, or `undefined` if unable to determine the compatibility.
*/
getEIP1559Compatibility(): Promise<boolean | undefined>;
getEIP1559Compatibility(networkClientId?: NetworkClientId): Promise<boolean | undefined>;
get1555CompatibilityWithNetworkClientId(networkClientId: NetworkClientId): Promise<boolean>;
/**

@@ -295,0 +315,0 @@ * Re-initializes the provider and block tracker for the current network.

@@ -25,3 +25,3 @@ "use strict";

};
var _NetworkController_instances, _NetworkController_ethQuery, _NetworkController_infuraProjectId, _NetworkController_trackMetaMetricsEvent, _NetworkController_previousProviderConfig, _NetworkController_providerProxy, _NetworkController_provider, _NetworkController_blockTrackerProxy, _NetworkController_autoManagedNetworkClientRegistry, _NetworkController_refreshNetwork, _NetworkController_getLatestBlock, _NetworkController_determineEIP1559Compatibility, _NetworkController_ensureAutoManagedNetworkClientRegistryPopulated, _NetworkController_createAutoManagedNetworkClientRegistry, _NetworkController_buildIdentifiedInfuraNetworkClientConfigurations, _NetworkController_buildIdentifiedCustomNetworkClientConfigurations, _NetworkController_buildIdentifiedNetworkClientConfigurationsFromProviderConfig, _NetworkController_applyNetworkSelection;
var _NetworkController_instances, _NetworkController_ethQuery, _NetworkController_infuraProjectId, _NetworkController_trackMetaMetricsEvent, _NetworkController_previousProviderConfig, _NetworkController_providerProxy, _NetworkController_provider, _NetworkController_blockTrackerProxy, _NetworkController_autoManagedNetworkClientRegistry, _NetworkController_refreshNetwork, _NetworkController_getNetworkId, _NetworkController_getLatestBlock, _NetworkController_determineEIP1559Compatibility, _NetworkController_ensureAutoManagedNetworkClientRegistryPopulated, _NetworkController_createAutoManagedNetworkClientRegistry, _NetworkController_buildIdentifiedInfuraNetworkClientConfigurations, _NetworkController_buildIdentifiedCustomNetworkClientConfigurations, _NetworkController_buildIdentifiedNetworkClientConfigurationsFromProviderConfig, _NetworkController_applyNetworkSelection;
Object.defineProperty(exports, "__esModule", { value: true });

@@ -89,2 +89,22 @@ exports.NetworkController = exports.defaultState = exports.knownKeysOf = void 0;

/**
* Convert the given value into a valid network ID. The ID is accepted
* as either a number, a decimal string, or a 0x-prefixed hex string.
*
* @param value - The network ID to convert, in an unknown format.
* @returns A valid network ID (as a decimal string)
* @throws If the given value cannot be safely parsed.
*/
function convertNetworkId(value) {
if (typeof value === 'number' && !Number.isNaN(value)) {
return `${value}`;
}
else if ((0, utils_1.isStrictHexString)(value)) {
return `${(0, controller_utils_1.convertHexToDecimal)(value)}`;
}
else if (typeof value === 'string' && /^\d+$/u.test(value)) {
return value;
}
throw new Error(`Cannot parse as a valid network ID: '${value}'`);
}
/**
* Type guard for determining whether the given value is an error object with a

@@ -195,2 +215,3 @@ * `code` property, such as an instance of Error.

selectedNetworkClientId: controller_utils_1.NetworkType.mainnet,
networkId: null,
providerConfig: {

@@ -216,2 +237,6 @@ type: controller_utils_1.NetworkType.mainnet,

},
networkId: {
persist: true,
anonymous: false,
},
networksMetadata: {

@@ -253,2 +278,3 @@ persist: true,

});
this.messagingSystem.registerActionHandler(`${this.name}:getNetworkClientById`, this.getNetworkClientById.bind(this));
__classPrivateFieldSet(this, _NetworkController_previousProviderConfig, this.state.providerConfig, "f");

@@ -309,2 +335,70 @@ }

/**
* Refreshes the network meta with EIP-1559 support and the network status
* based on the given network client ID.
*
* @param networkClientId - The ID of the network client to update.
*/
lookupNetworkByClientId(networkClientId) {
return __awaiter(this, void 0, void 0, function* () {
const isInfura = isInfuraProviderType(networkClientId);
let updatedNetworkStatus;
let updatedIsEIP1559Compatible;
try {
updatedIsEIP1559Compatible = yield __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_determineEIP1559Compatibility).call(this, networkClientId);
updatedNetworkStatus = constants_1.NetworkStatus.Available;
}
catch (error) {
if (isErrorWithCode(error)) {
let responseBody;
if (isInfura &&
(0, utils_1.hasProperty)(error, 'message') &&
typeof error.message === 'string') {
try {
responseBody = JSON.parse(error.message);
}
catch (_a) {
// error.message must not be JSON
}
}
if ((0, utils_1.isPlainObject)(responseBody) &&
responseBody.error === constants_1.INFURA_BLOCKED_KEY) {
updatedNetworkStatus = constants_1.NetworkStatus.Blocked;
}
else if (error.code === eth_rpc_errors_1.errorCodes.rpc.internal) {
updatedNetworkStatus = constants_1.NetworkStatus.Unknown;
}
else {
updatedNetworkStatus = constants_1.NetworkStatus.Unavailable;
}
}
else if (typeof Error !== 'undefined' &&
(0, utils_1.hasProperty)(error, 'message') &&
typeof error.message === 'string' &&
error.message.includes('No custom network client was found with the ID')) {
throw error;
}
else {
log('NetworkController - could not determine network status', error);
updatedNetworkStatus = constants_1.NetworkStatus.Unknown;
}
}
this.update((state) => {
if (state.networksMetadata[networkClientId] === undefined) {
state.networksMetadata[networkClientId] = {
status: constants_1.NetworkStatus.Unknown,
EIPS: {},
};
}
const meta = state.networksMetadata[networkClientId];
meta.status = updatedNetworkStatus;
if (updatedIsEIP1559Compatible === undefined) {
delete meta.EIPS[1559];
}
else {
meta.EIPS[1559] = updatedIsEIP1559Compatible;
}
});
});
}
/**
* Performs side effects after switching to a network. If the network is

@@ -315,2 +409,4 @@ * available, updates the network state with the network ID of the network and

*
* @param networkClientId - (Optional) The ID of the network client to update.
* If no ID is provided, uses the currently selected network.
* @fires infuraIsBlocked if the network is Infura-supported and is blocking

@@ -321,4 +417,8 @@ * requests.

*/
lookupNetwork() {
lookupNetwork(networkClientId) {
return __awaiter(this, void 0, void 0, function* () {
if (networkClientId) {
yield this.lookupNetworkByClientId(networkClientId);
return;
}
if (!__classPrivateFieldGet(this, _NetworkController_ethQuery, "f")) {

@@ -335,6 +435,11 @@ return;

let updatedNetworkStatus;
let updatedNetworkId = null;
let updatedIsEIP1559Compatible;
try {
const isEIP1559Compatible = yield __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_determineEIP1559Compatibility).call(this);
const [networkId, isEIP1559Compatible] = yield Promise.all([
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_getNetworkId).call(this, this.state.selectedNetworkClientId),
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_determineEIP1559Compatibility).call(this, this.state.selectedNetworkClientId),
]);
updatedNetworkStatus = constants_1.NetworkStatus.Available;
updatedNetworkId = networkId;
updatedIsEIP1559Compatible = isEIP1559Compatible;

@@ -378,2 +483,3 @@ }

this.update((state) => {
state.networkId = updatedNetworkId;
const meta = state.networksMetadata[state.selectedNetworkClientId];

@@ -461,7 +567,11 @@ meta.status = updatedNetworkStatus;

*
* @param networkClientId - The networkClientId to fetch the correct provider against which to check 1559 compatibility.
* @returns A promise that resolves to true if the network supports EIP-1559
* , false otherwise, or `undefined` if unable to determine the compatibility.
*/
getEIP1559Compatibility() {
getEIP1559Compatibility(networkClientId) {
return __awaiter(this, void 0, void 0, function* () {
if (networkClientId) {
return this.get1555CompatibilityWithNetworkClientId(networkClientId);
}
if (!__classPrivateFieldGet(this, _NetworkController_ethQuery, "f")) {

@@ -474,3 +584,3 @@ return false;

}
const isEIP1559Compatible = yield __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_determineEIP1559Compatibility).call(this);
const isEIP1559Compatible = yield __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_determineEIP1559Compatibility).call(this, this.state.selectedNetworkClientId);
this.update((state) => {

@@ -485,2 +595,14 @@ if (isEIP1559Compatible !== undefined) {

}
get1555CompatibilityWithNetworkClientId(networkClientId) {
return __awaiter(this, void 0, void 0, function* () {
let metadata = this.state.networksMetadata[networkClientId];
if (metadata === undefined) {
yield this.lookupNetwork(networkClientId);
metadata = this.state.networksMetadata[networkClientId];
}
const { EIPS } = metadata;
// may want to include some 'freshness' value - something to make sure we refetch this from time to time
return EIPS[1559];
});
}
/**

@@ -668,2 +790,5 @@ * Re-initializes the provider and block tracker for the current network.

this.messagingSystem.publish('NetworkController:networkWillChange');
this.update((state) => {
state.networkId = null;
});
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_applyNetworkSelection).call(this);

@@ -673,8 +798,33 @@ this.messagingSystem.publish('NetworkController:networkDidChange');

});
}, _NetworkController_getLatestBlock = function _NetworkController_getLatestBlock() {
}, _NetworkController_getNetworkId = function _NetworkController_getNetworkId(networkClientId) {
return __awaiter(this, void 0, void 0, function* () {
const possibleNetworkId = yield new Promise((resolve, reject) => {
let ethQuery = __classPrivateFieldGet(this, _NetworkController_ethQuery, "f");
if (networkClientId) {
const networkClient = this.getNetworkClientById(networkClientId);
ethQuery = new eth_query_1.default(networkClient.provider);
}
if (!ethQuery) {
throw new Error('Provider has not been initialized');
}
ethQuery.sendAsync({ method: 'net_version' }, (error, result) => {
if (error) {
reject(error);
}
else {
// TODO: Validate this type
resolve(result);
}
});
});
return convertNetworkId(possibleNetworkId);
});
}, _NetworkController_getLatestBlock = function _NetworkController_getLatestBlock(networkClientId) {
if (networkClientId === undefined) {
networkClientId = this.state.selectedNetworkClientId;
}
const networkClient = this.getNetworkClientById(networkClientId);
const ethQuery = new eth_query_1.default(networkClient.provider);
return new Promise((resolve, reject) => {
if (!__classPrivateFieldGet(this, _NetworkController_ethQuery, "f")) {
throw new Error('Provider has not been initialized');
}
__classPrivateFieldGet(this, _NetworkController_ethQuery, "f").sendAsync({ method: 'eth_getBlockByNumber', params: ['latest', false] }, (error, block) => {
ethQuery.sendAsync({ method: 'eth_getBlockByNumber', params: ['latest', false] }, (error, block) => {
if (error) {

@@ -689,5 +839,5 @@ reject(error);

});
}, _NetworkController_determineEIP1559Compatibility = function _NetworkController_determineEIP1559Compatibility() {
}, _NetworkController_determineEIP1559Compatibility = function _NetworkController_determineEIP1559Compatibility(networkClientId) {
return __awaiter(this, void 0, void 0, function* () {
const latestBlock = yield __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_getLatestBlock).call(this);
const latestBlock = yield __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_getLatestBlock).call(this, networkClientId);
if (!latestBlock) {

@@ -694,0 +844,0 @@ return undefined;

4

package.json
{
"name": "@metamask-previews/network-controller",
"version": "12.1.2-preview.f71e42f",
"version": "12.2.0-preview.56ca173",
"description": "Provides an interface to the currently selected network via a MetaMask-compatible provider object",

@@ -34,3 +34,3 @@ "keywords": [

"@metamask/eth-json-rpc-infura": "^8.1.1",
"@metamask/eth-json-rpc-middleware": "^11.0.0",
"@metamask/eth-json-rpc-middleware": "^11.0.2",
"@metamask/eth-json-rpc-provider": "^1.0.0",

@@ -37,0 +37,0 @@ "@metamask/eth-query": "^3.0.1",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc