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AAC","sourcesContent":["import { BtcScope, SolScope, EthScope, TrxScope } from '@metamask/keyring-api';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport { array, object } from '@metamask/superstruct';\nimport type { Infer } from '@metamask/superstruct';\nimport { CaipAccountIdStruct, parseCaipAccountId } from '@metamask/utils';\nimport type {\n CaipAccountAddress,\n CaipAccountId,\n CaipNamespace,\n CaipReference,\n} from '@metamask/utils';\n\nexport const ActiveNetworksResponseStruct = object({\n activeNetworks: array(CaipAccountIdStruct),\n});\n\nexport type ActiveNetworksResponse = Infer<typeof ActiveNetworksResponseStruct>;\n\n/**\n * The active networks for the currently selected account.\n */\nexport type ActiveNetworksByAddress = Record<\n CaipAccountAddress,\n {\n // CAIP-2 namespace of the network.\n namespace: CaipNamespace;\n // Active chain IDs (CAIP-2 references) on that network (primarily used for EVM networks).\n activeChains: CaipReference[];\n }\n>;\n\n/**\n * The domain for multichain accounts API.\n */\nexport const MULTICHAIN_ACCOUNTS_BASE_URL =\n 'https://accounts.api.cx.metamask.io';\n\n/**\n * The client header for the multichain accounts API.\n */\nexport const MULTICHAIN_ACCOUNTS_CLIENT_HEADER = 'x-metamask-clientproduct';\n\n/**\n * The client ID for the multichain accounts API.\n */\nexport const MULTICHAIN_ACCOUNTS_CLIENT_ID =\n 'metamask-multichain-network-controller';\n\n/**\n * The allowed active network scopes for the multichain network controller.\n */\nexport const MULTICHAIN_ALLOWED_ACTIVE_NETWORK_SCOPES = [\n String(BtcScope.Mainnet),\n String(BtcScope.Testnet),\n String(BtcScope.Testnet4),\n String(BtcScope.Signet),\n String(BtcScope.Regtest),\n String(SolScope.Mainnet),\n String(SolScope.Devnet),\n String(EthScope.Mainnet),\n String(EthScope.Testnet),\n String(EthScope.Eoa),\n String(TrxScope.Mainnet),\n String(TrxScope.Nile),\n String(TrxScope.Shasta),\n];\n\n/**\n * Converts an internal account to an array of CAIP-10 account IDs.\n *\n * @param account - The internal account to convert\n * @returns The CAIP-10 account IDs\n */\nexport function toAllowedCaipAccountIds(\n account: InternalAccount,\n): CaipAccountId[] {\n const formattedAccounts: CaipAccountId[] = [];\n for (const scope of account.scopes) {\n if (MULTICHAIN_ALLOWED_ACTIVE_NETWORK_SCOPES.includes(scope)) {\n formattedAccounts.push(`${scope}:${account.address}`);\n }\n }\n\n return formattedAccounts;\n}\n\n/**\n * Formats the API response into our state structure.\n * Example input: [\"eip155:1:0x123...\", \"eip155:137:0x123...\", \"solana:1:0xabc...\"]\n *\n * @param response - The raw API response\n * @returns Formatted networks by address\n */\nexport function toActiveNetworksByAddress(\n response: ActiveNetworksResponse,\n): ActiveNetworksByAddress {\n const networksByAddress: ActiveNetworksByAddress = {};\n\n response.activeNetworks.forEach((network) => {\n const {\n address,\n chain: { namespace, reference },\n } = parseCaipAccountId(network);\n\n if (!networksByAddress[address]) {\n networksByAddress[address] = {\n namespace,\n activeChains: [],\n };\n }\n networksByAddress[address].activeChains.push(reference);\n });\n\n return networksByAddress;\n}\n\n/**\n * Constructs the URL for the active networks API endpoint.\n *\n * @param accountIds - Array of account IDs\n * @returns URL object for the API endpoint\n */\nexport function buildActiveNetworksUrl(accountIds: CaipAccountId[]): URL {\n const url = new URL(`${MULTICHAIN_ACCOUNTS_BASE_URL}/v2/activeNetworks`);\n url.searchParams.append('accountIds', accountIds.join(','));\n return url;\n}\n"]}