UNPKG

@metamask/multichain-network-controller

Version:
1 lines 6.75 kB
{"version":3,"file":"utils.mjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AACA,OAAO,EACL,kBAAkB,EAClB,aAAa,EACb,gBAAgB,EAChB,WAAW,EACX,KAAK,EACN,wBAAwB;AAGzB,OAAO,EAAE,2CAA2C,EAAE,wBAAoB;AAM1E;;;;;GAKG;AACH,MAAM,UAAU,gBAAgB,CAAC,OAAoB;IACnD,MAAM,EAAE,SAAS,EAAE,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAC;IAChD,OAAO,SAAS,KAAM,kBAAkB,CAAC,MAAiB,CAAC;AAC7D,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,mBAAmB,CACjC,MAAqB;IAErB,MAAM,cAAc,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAC3C,MAAM,CAAC,IAAI,CAAC,2CAA2C,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CACzE,CAAC;IACF,IAAI,cAAc,EAAE,CAAC;QACnB,OAAO,cAAsC,CAAC;IAChD,CAAC;IAED,MAAM,IAAI,KAAK,CAAC,sBAAsB,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAC9D,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,2BAA2B,CACzC,EAAe;IAEf,qCAAqC;IACrC,OAAO,MAAM,CAAC,IAAI,CAAC,2CAA2C,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;AAC/E,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,OAAY,EAAe,EAAE,CAC5D,aAAa,CAAC,kBAAkB,CAAC,MAAM,EAAE,WAAW,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAC;AAE5E;;;;;GAKG;AACH,MAAM,UAAU,0BAA0B,CAAC,OAAoB;IAC7D,MAAM,EAAE,S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,CAAC,SAAS,CAAC,CAAC;AACvE,CAAC","sourcesContent":["import type { NetworkConfiguration } from '@metamask/network-controller';\nimport {\n KnownCaipNamespace,\n toCaipChainId,\n parseCaipChainId,\n hexToNumber,\n add0x,\n} from '@metamask/utils';\nimport type { Hex, CaipChainId } from '@metamask/utils';\n\nimport { AVAILABLE_MULTICHAIN_NETWORK_CONFIGURATIONS } from './constants';\nimport type {\n SupportedCaipChainId,\n MultichainNetworkConfiguration,\n} from './types';\n\n/**\n * Checks if the chain ID is EVM.\n *\n * @param chainId - The account type to check.\n * @returns Whether the network is EVM.\n */\nexport function isEvmCaipChainId(chainId: CaipChainId): boolean {\n const { namespace } = parseCaipChainId(chainId);\n return namespace === (KnownCaipNamespace.Eip155 as string);\n}\n\n/**\n * Returns the chain id of the non-EVM network based on the account scopes.\n *\n * @param scopes - The scopes to check.\n * @returns The caip chain id of the non-EVM network.\n */\nexport function getChainIdForNonEvm(\n scopes: CaipChainId[],\n): SupportedCaipChainId {\n const supportedScope = scopes.find((scope) =>\n Object.keys(AVAILABLE_MULTICHAIN_NETWORK_CONFIGURATIONS).includes(scope),\n );\n if (supportedScope) {\n return supportedScope as SupportedCaipChainId;\n }\n\n throw new Error(`Unsupported scope: ${scopes.join(', ')}.`);\n}\n\n/**\n * Checks if the Caip chain ID is supported.\n *\n * @param id - The Caip chain IDto check.\n * @returns Whether the chain ID is supported.\n */\nexport function checkIfSupportedCaipChainId(\n id: CaipChainId,\n): id is SupportedCaipChainId {\n // Check if the chain id is supported\n return Object.keys(AVAILABLE_MULTICHAIN_NETWORK_CONFIGURATIONS).includes(id);\n}\n\n/**\n * Converts a hex chain ID to a Caip chain ID.\n *\n * @param chainId - The hex chain ID to convert.\n * @returns The Caip chain ID.\n */\nexport const toEvmCaipChainId = (chainId: Hex): CaipChainId =>\n toCaipChainId(KnownCaipNamespace.Eip155, hexToNumber(chainId).toString());\n\n/**\n * Convert an eip155 CAIP chain ID to a hex chain ID.\n *\n * @param chainId - The CAIP chain ID to convert.\n * @returns The hex chain ID.\n */\nexport function convertEvmCaipToHexChainId(chainId: CaipChainId): Hex {\n const { namespace, reference } = parseCaipChainId(chainId);\n if (namespace === (KnownCaipNamespace.Eip155 as string)) {\n return add0x(parseInt(reference, 10).toString(16));\n }\n\n throw new Error(\n `Unsupported CAIP chain ID namespace: ${namespace}. Only eip155 is supported.`,\n );\n}\n\n/**\n * Updates a network configuration to the format used by the MultichainNetworkController.\n * This method is exclusive for EVM networks with hex identifiers from the NetworkController.\n *\n * @param network - The network configuration to update.\n * @returns The updated network configuration.\n */\nexport const toMultichainNetworkConfiguration = (\n network: NetworkConfiguration,\n): MultichainNetworkConfiguration => {\n const {\n chainId,\n name,\n rpcEndpoints,\n defaultRpcEndpointIndex,\n nativeCurrency,\n blockExplorerUrls,\n defaultBlockExplorerUrlIndex = 0,\n } = network;\n return {\n chainId: toEvmCaipChainId(chainId),\n isEvm: true,\n name: name || rpcEndpoints[defaultRpcEndpointIndex].url,\n nativeCurrency,\n blockExplorerUrls,\n defaultBlockExplorerUrlIndex,\n };\n};\n\n/**\n * Updates a record of network configurations to the format used by the MultichainNetworkController.\n * This method is exclusive for EVM networks with hex identifiers from the NetworkController.\n *\n * @param networkConfigurationsByChainId - The network configurations to update.\n * @returns The updated network configurations.\n */\nexport const toMultichainNetworkConfigurationsByChainId = (\n networkConfigurationsByChainId: Record<string, NetworkConfiguration>,\n): Record<CaipChainId, MultichainNetworkConfiguration> =>\n Object.entries(networkConfigurationsByChainId).reduce(\n (acc, [, network]) => ({\n ...acc,\n [toEvmCaipChainId(network.chainId)]:\n toMultichainNetworkConfiguration(network),\n }),\n {},\n );\n\n// TODO: This currently isn't being used anymore but could benefit from being moved to @metamask/utils\n/**\n * Type guard to check if a namespace is a known CAIP namespace.\n *\n * @param namespace - The namespace to check\n * @returns Whether the namespace is a known CAIP namespace\n */\nexport function isKnownCaipNamespace(\n namespace: string,\n): namespace is KnownCaipNamespace {\n return Object.values<string>(KnownCaipNamespace).includes(namespace);\n}\n"]}