UNPKG

@metamask-previews/multichain-network-controller

Version:
63 lines 2.64 kB
import { BtcScope, SolScope } from "@metamask/keyring-api"; import { KnownCaipNamespace, toCaipChainId, hexToNumber } from "@metamask/utils"; import { isAddress as isSolanaAddress } from "@solana/addresses"; import { AVAILABLE_MULTICHAIN_NETWORK_CONFIGURATIONS } from "./constants.mjs"; /** * 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. */ export function getChainIdForNonEvmAddress(address) { // This condition is not the most robust. Once we support more networks, we will need to update this logic. if (isSolanaAddress(address)) { return SolScope.Mainnet; } return BtcScope.Mainnet; } /** * 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()); /** * 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) => { return { chainId: toEvmCaipChainId(network.chainId), isEvm: true, name: network.name, nativeCurrency: network.nativeCurrency, blockExplorerUrls: network.blockExplorerUrls, defaultBlockExplorerUrlIndex: network.defaultBlockExplorerUrlIndex || 0, }; }; /** * 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), }), {}); //# sourceMappingURL=utils.mjs.map