@metamask/multichain-network-controller
Version:
Multichain network controller
108 lines • 4.85 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isKnownCaipNamespace = exports.toMultichainNetworkConfigurationsByChainId = exports.toMultichainNetworkConfiguration = exports.convertEvmCaipToHexChainId = exports.toEvmCaipChainId = exports.checkIfSupportedCaipChainId = exports.getChainIdForNonEvmAddress = exports.isEvmCaipChainId = 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");
/**
* Checks if the chain ID is EVM.
*
* @param chainId - The account type to check.
* @returns Whether the network is EVM.
*/
function isEvmCaipChainId(chainId) {
const { namespace } = (0, utils_1.parseCaipChainId)(chainId);
return namespace === utils_1.KnownCaipNamespace.Eip155;
}
exports.isEvmCaipChainId = isEvmCaipChainId;
/**
* 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;
/**
* Convert an eip155 CAIP chain ID to a hex chain ID.
*
* @param chainId - The CAIP chain ID to convert.
* @returns The hex chain ID.
*/
function convertEvmCaipToHexChainId(chainId) {
const { namespace, reference } = (0, utils_1.parseCaipChainId)(chainId);
if (namespace === utils_1.KnownCaipNamespace.Eip155) {
return (0, utils_1.add0x)(parseInt(reference, 10).toString(16));
}
throw new Error(`Unsupported CAIP chain ID namespace: ${namespace}. Only eip155 is supported.`);
}
exports.convertEvmCaipToHexChainId = convertEvmCaipToHexChainId;
/**
* 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) => {
const { chainId, name, rpcEndpoints, defaultRpcEndpointIndex, nativeCurrency, blockExplorerUrls, defaultBlockExplorerUrlIndex, } = network;
return {
chainId: (0, exports.toEvmCaipChainId)(chainId),
isEvm: true,
name: name || rpcEndpoints[defaultRpcEndpointIndex].url,
nativeCurrency,
blockExplorerUrls,
defaultBlockExplorerUrlIndex: 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;
// TODO: This currently isn't being used anymore but could benefit from being moved to @metamask/utils
/**
* Type guard to check if a namespace is a known CAIP namespace.
*
* @param namespace - The namespace to check
* @returns Whether the namespace is a known CAIP namespace
*/
function isKnownCaipNamespace(namespace) {
return Object.values(utils_1.KnownCaipNamespace).includes(namespace);
}
exports.isKnownCaipNamespace = isKnownCaipNamespace;
//# sourceMappingURL=utils.cjs.map