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,CAAC;KACpD;IAED,MAAM,IAAI,KAAK,CACb,wCAAwC,SAAS,6BAA6B,CAC/E,CAAC;AACJ,CAAC;AATD,gEASC;AAED;;;;;;GAMG;AACI,MAAM,gCAAgC,GAAG,CAC9C,OAA6B,EACG,EAAE;IAClC,MAAM,EACJ,OAAO,EACP,IAAI,EACJ,YAAY,EACZ,uBAAuB,EACvB,cAAc,EACd,iBAAiB,EACjB,4BAA4B,GAC7B,GAAG,OAAO,CAAC;IACZ,OAAO;QACL,OAAO,EAAE,IAAA,wBAAgB,EAAC,OAAO,CAAC;QAClC,KAAK,EAAE,IAAI;QACX,IAAI,EAAE,IAAI,IAAI,YAAY,CAAC,uBAAuB,CAAC,CAAC,GAAG;QACvD,cAAc;QACd,iBAAiB;QACjB,4BAA4B,EAAE,4BAA4B,IAAI,CAAC;KAChE,CAAC;AACJ,CAAC,CAAC;AApBW,QAAA,gCAAgC,oCAoB3C;AAEF;;;;;;GAMG;AACI,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,IAAA,wBAAgB,EAAC,OAAO,CAAC,OAAO,CAAC,CAAC,EACjC,IAAA,wCAAgC,EAAC,OAAO,CAAC;CAC5C,CAAC,EACF,EAAE,CACH,CAAC;AAVS,QAAA,0CAA0C,8CAUnD;AAEJ,sGAAsG;AACtG;;;;;GAKG;AACH,SAAgB,oBAAoB,CAClC,SAAiB;IAEjB,OAAO,MAAM,CAAC,MAAM,CAAS,0BAAkB,CAAC,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC;AACvE,CAAC;AAJD,oDAIC","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  parseCaipChainId,\n  hexToNumber,\n  add0x,\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 * 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 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 * 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,\n  } = network;\n  return {\n    chainId: toEvmCaipChainId(chainId),\n    isEvm: true,\n    name: name || rpcEndpoints[defaultRpcEndpointIndex].url,\n    nativeCurrency,\n    blockExplorerUrls,\n    defaultBlockExplorerUrlIndex: 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\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"]}