UNPKG

@metamask/multichain-network-controller

Version:
95 lines 3.81 kB
import { KnownCaipNamespace, toCaipChainId, parseCaipChainId, hexToNumber, add0x } from "@metamask/utils"; import { AVAILABLE_MULTICHAIN_NETWORK_CONFIGURATIONS } from "./constants.mjs"; /** * Checks if the chain ID is EVM. * * @param chainId - The account type to check. * @returns Whether the network is EVM. */ export function isEvmCaipChainId(chainId) { const { namespace } = parseCaipChainId(chainId); return namespace === KnownCaipNamespace.Eip155; } /** * Returns the chain id of the non-EVM network based on the account scopes. * * @param scopes - The scopes to check. * @returns The caip chain id of the non-EVM network. */ export function getChainIdForNonEvm(scopes) { const supportedScope = scopes.find((scope) => Object.keys(AVAILABLE_MULTICHAIN_NETWORK_CONFIGURATIONS).includes(scope)); if (supportedScope) { return supportedScope; } throw new Error(`Unsupported scope: ${scopes.join(', ')}.`); } /** * Checks if the Caip chain ID is supported. * * @param id - The Caip chain IDto check. * @returns Whether the chain ID is supported. */ export function checkIfSupportedCaipChainId(id) { // Check if the chain id is supported return Object.keys(AVAILABLE_MULTICHAIN_NETWORK_CONFIGURATIONS).includes(id); } /** * Converts a hex chain ID to a Caip chain ID. * * @param chainId - The hex chain ID to convert. * @returns The Caip chain ID. */ export const toEvmCaipChainId = (chainId) => toCaipChainId(KnownCaipNamespace.Eip155, hexToNumber(chainId).toString()); /** * Convert an eip155 CAIP chain ID to a hex chain ID. * * @param chainId - The CAIP chain ID to convert. * @returns The hex chain ID. */ export function convertEvmCaipToHexChainId(chainId) { const { namespace, reference } = parseCaipChainId(chainId); if (namespace === KnownCaipNamespace.Eip155) { return add0x(parseInt(reference, 10).toString(16)); } throw new Error(`Unsupported CAIP chain ID namespace: ${namespace}. Only eip155 is supported.`); } /** * 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. */ export const toMultichainNetworkConfiguration = (network) => { const { chainId, name, rpcEndpoints, defaultRpcEndpointIndex, nativeCurrency, blockExplorerUrls, defaultBlockExplorerUrlIndex = 0, } = network; return { chainId: toEvmCaipChainId(chainId), isEvm: true, name: name || rpcEndpoints[defaultRpcEndpointIndex].url, nativeCurrency, blockExplorerUrls, defaultBlockExplorerUrlIndex, }; }; /** * 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. */ export const toMultichainNetworkConfigurationsByChainId = (networkConfigurationsByChainId) => Object.entries(networkConfigurationsByChainId).reduce((acc, [, network]) => ({ ...acc, [toEvmCaipChainId(network.chainId)]: toMultichainNetworkConfiguration(network), }), {}); // 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 */ export function isKnownCaipNamespace(namespace) { return Object.values(KnownCaipNamespace).includes(namespace); } //# sourceMappingURL=utils.mjs.map