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E,SAAS,EAAE,SAAS,EAAE,GAChC,GAAG,IAAA,0BAAkB,EAAC,OAAO,CAAC,CAAC;QAEhC,IAAI,CAAC,iBAAiB,CAAC,OAAO,CAAC,EAAE;YAC/B,iBAAiB,CAAC,OAAO,CAAC,GAAG;gBAC3B,SAAS;gBACT,YAAY,EAAE,EAAE;aACjB,CAAC;SACH;QACD,iBAAiB,CAAC,OAAO,CAAC,CAAC,YAAY,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IAC1D,CAAC,CAAC,CAAC;IAEH,OAAO,iBAAiB,CAAC;AAC3B,CAAC;AArBD,8DAqBC;AAED;;;;;GAKG;AACH,SAAgB,sBAAsB,CAAC,UAA2B;IAChE,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,GAAG,oCAA4B,oBAAoB,CAAC,CAAC;IACzE,GAAG,CAAC,YAAY,CAAC,MAAM,CAAC,YAAY,EAAE,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;IAC5D,OAAO,GAAG,CAAC;AACb,CAAC;AAJD,wDAIC","sourcesContent":["import { BtcScope, SolScope, EthScope } from '@metamask/keyring-api';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport { type Infer, array, object } 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(SolScope.Mainnet),\n  String(EthScope.Mainnet),\n  String(EthScope.Testnet),\n  String(EthScope.Eoa),\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"]}