@metamask-previews/multichain-network-controller
Version:
Multichain network controller
71 lines • 3.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.toMultichainNetworkConfigurationsByChainId = exports.toMultichainNetworkConfiguration = exports.toEvmCaipChainId = exports.checkIfSupportedCaipChainId = exports.getChainIdForNonEvmAddress = void 0;
const keyring_api_1 = require("@metamask/keyring-api");
const utils_1 = require("@metamask/utils");
const addresses_1 = require("@solana/addresses");
const constants_1 = require("./constants.cjs");
/**
* Returns the chain id of the non-EVM network based on the account address.
*
* @param address - The address to check.
* @returns The caip chain id of the non-EVM network.
*/
function getChainIdForNonEvmAddress(address) {
// This condition is not the most robust. Once we support more networks, we will need to update this logic.
if ((0, addresses_1.isAddress)(address)) {
return keyring_api_1.SolScope.Mainnet;
}
return keyring_api_1.BtcScope.Mainnet;
}
exports.getChainIdForNonEvmAddress = getChainIdForNonEvmAddress;
/**
* Checks if the Caip chain ID is supported.
*
* @param id - The Caip chain IDto check.
* @returns Whether the chain ID is supported.
*/
function checkIfSupportedCaipChainId(id) {
// Check if the chain id is supported
return Object.keys(constants_1.AVAILABLE_MULTICHAIN_NETWORK_CONFIGURATIONS).includes(id);
}
exports.checkIfSupportedCaipChainId = checkIfSupportedCaipChainId;
/**
* Converts a hex chain ID to a Caip chain ID.
*
* @param chainId - The hex chain ID to convert.
* @returns The Caip chain ID.
*/
const toEvmCaipChainId = (chainId) => (0, utils_1.toCaipChainId)(utils_1.KnownCaipNamespace.Eip155, (0, utils_1.hexToNumber)(chainId).toString());
exports.toEvmCaipChainId = toEvmCaipChainId;
/**
* Updates a network configuration to the format used by the MultichainNetworkController.
* This method is exclusive for EVM networks with hex identifiers from the NetworkController.
*
* @param network - The network configuration to update.
* @returns The updated network configuration.
*/
const toMultichainNetworkConfiguration = (network) => {
return {
chainId: (0, exports.toEvmCaipChainId)(network.chainId),
isEvm: true,
name: network.name,
nativeCurrency: network.nativeCurrency,
blockExplorerUrls: network.blockExplorerUrls,
defaultBlockExplorerUrlIndex: network.defaultBlockExplorerUrlIndex || 0,
};
};
exports.toMultichainNetworkConfiguration = toMultichainNetworkConfiguration;
/**
* Updates a record of network configurations to the format used by the MultichainNetworkController.
* This method is exclusive for EVM networks with hex identifiers from the NetworkController.
*
* @param networkConfigurationsByChainId - The network configurations to update.
* @returns The updated network configurations.
*/
const toMultichainNetworkConfigurationsByChainId = (networkConfigurationsByChainId) => Object.entries(networkConfigurationsByChainId).reduce((acc, [, network]) => ({
...acc,
[(0, exports.toEvmCaipChainId)(network.chainId)]: (0, exports.toMultichainNetworkConfiguration)(network),
}), {});
exports.toMultichainNetworkConfigurationsByChainId = toMultichainNetworkConfigurationsByChainId;
//# sourceMappingURL=utils.cjs.map