UNPKG

@metamask-previews/multichain-network-controller

Version:
1 lines 4.53 kB
{"version":3,"file":"utils.mjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,8BAA8B;AAE3D,OAAO,EAGL,kBAAkB,EAClB,aAAa,EACb,WAAW,EACZ,wBAAwB;AACzB,OAAO,EAAE,SAAS,IAAI,eAAe,EAAE,0BAA0B;AAEjE,OAAO,EAAE,2CAA2C,EAAE,wBAAoB;AAM1E;;;;;GAKG;AACH,MAAM,UAAU,0BAA0B,CACxC,OAAe;IAEf,2GAA2G;IAC3G,IAAI,eAAe,CAAC,OAAO,CAAC,EAAE;QAC5B,OAAO,QAAQ,CAAC,OAAO,CAAC;KACzB;IACD,OAAO,QAAQ,CAAC,OAAO,CAAC;AAC1B,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;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,gCAAgC,GAAG,CAC9C,OAA6B,EACG,EAAE;IAClC,OAAO;QACL,OAAO,EAAE,gBAAgB,CAAC,OAAO,CAAC,OAAO,CAAC;QAC1C,KAAK,EAAE,IAAI;QACX,IAAI,EAAE,OAAO,CAAC,IAAI;QAClB,cAAc,EAAE,OAAO,CAAC,cAAc;QACtC,iBAAiB,EAAE,OAAO,CAAC,iBAAiB;QAC5C,4BAA4B,EAAE,OAAO,CAAC,4BAA4B,IAAI,CAAC;KACxE,CAAC;AACJ,CAAC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,0CAA0C,GAAG,CACxD,8BAAoE,EACf,EAAE,CACvD,MAAM,CAAC,OAAO,CAAC,8BAA8B,CAAC,CAAC,MAAM,CACnD,CAAC,GAAG,EAAE,CAAC,EAAE,OAAO,CAAC,EAAE,EAAE,CAAC,CAAC;IACrB,GAAG,GAAG;IACN,CAAC,gBAAgB,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,EACjC,gCAAgC,CAAC,OAAO,CAAC;CAC5C,CAAC,EACF,EAAE,CACH,CAAC","sourcesContent":["import { BtcScope, SolScope } from '@metamask/keyring-api';\nimport type { NetworkConfiguration } from '@metamask/network-controller';\nimport {\n type Hex,\n type CaipChainId,\n KnownCaipNamespace,\n toCaipChainId,\n hexToNumber,\n} from '@metamask/utils';\nimport { isAddress as isSolanaAddress } from '@solana/addresses';\n\nimport { AVAILABLE_MULTICHAIN_NETWORK_CONFIGURATIONS } from './constants';\nimport type {\n SupportedCaipChainId,\n MultichainNetworkConfiguration,\n} from './types';\n\n/**\n * Returns the chain id of the non-EVM network based on the account address.\n *\n * @param address - The address to check.\n * @returns The caip chain id of the non-EVM network.\n */\nexport function getChainIdForNonEvmAddress(\n address: string,\n): SupportedCaipChainId {\n // This condition is not the most robust. Once we support more networks, we will need to update this logic.\n if (isSolanaAddress(address)) {\n return SolScope.Mainnet;\n }\n return BtcScope.Mainnet;\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 * 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 return {\n chainId: toEvmCaipChainId(network.chainId),\n isEvm: true,\n name: network.name,\n nativeCurrency: network.nativeCurrency,\n blockExplorerUrls: network.blockExplorerUrls,\n defaultBlockExplorerUrlIndex: network.defaultBlockExplorerUrlIndex || 0,\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"]}