UNPKG

@metamask/multichain-network-controller

Version:
82 lines 3.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.buildActiveNetworksUrl = exports.toActiveNetworksByAddress = exports.toAllowedCaipAccountIds = exports.MULTICHAIN_ALLOWED_ACTIVE_NETWORK_SCOPES = exports.MULTICHAIN_ACCOUNTS_CLIENT_ID = exports.MULTICHAIN_ACCOUNTS_CLIENT_HEADER = exports.MULTICHAIN_ACCOUNTS_BASE_URL = exports.ActiveNetworksResponseStruct = void 0; const keyring_api_1 = require("@metamask/keyring-api"); const superstruct_1 = require("@metamask/superstruct"); const utils_1 = require("@metamask/utils"); exports.ActiveNetworksResponseStruct = (0, superstruct_1.object)({ activeNetworks: (0, superstruct_1.array)(utils_1.CaipAccountIdStruct), }); /** * The domain for multichain accounts API. */ exports.MULTICHAIN_ACCOUNTS_BASE_URL = 'https://accounts.api.cx.metamask.io'; /** * The client header for the multichain accounts API. */ exports.MULTICHAIN_ACCOUNTS_CLIENT_HEADER = 'x-metamask-clientproduct'; /** * The client ID for the multichain accounts API. */ exports.MULTICHAIN_ACCOUNTS_CLIENT_ID = 'metamask-multichain-network-controller'; /** * The allowed active network scopes for the multichain network controller. */ exports.MULTICHAIN_ALLOWED_ACTIVE_NETWORK_SCOPES = [ String(keyring_api_1.BtcScope.Mainnet), String(keyring_api_1.SolScope.Mainnet), String(keyring_api_1.EthScope.Mainnet), String(keyring_api_1.EthScope.Testnet), String(keyring_api_1.EthScope.Eoa), ]; /** * Converts an internal account to an array of CAIP-10 account IDs. * * @param account - The internal account to convert * @returns The CAIP-10 account IDs */ function toAllowedCaipAccountIds(account) { const formattedAccounts = []; for (const scope of account.scopes) { if (exports.MULTICHAIN_ALLOWED_ACTIVE_NETWORK_SCOPES.includes(scope)) { formattedAccounts.push(`${scope}:${account.address}`); } } return formattedAccounts; } exports.toAllowedCaipAccountIds = toAllowedCaipAccountIds; /** * Formats the API response into our state structure. * Example input: ["eip155:1:0x123...", "eip155:137:0x123...", "solana:1:0xabc..."] * * @param response - The raw API response * @returns Formatted networks by address */ function toActiveNetworksByAddress(response) { const networksByAddress = {}; response.activeNetworks.forEach((network) => { const { address, chain: { namespace, reference }, } = (0, utils_1.parseCaipAccountId)(network); if (!networksByAddress[address]) { networksByAddress[address] = { namespace, activeChains: [], }; } networksByAddress[address].activeChains.push(reference); }); return networksByAddress; } exports.toActiveNetworksByAddress = toActiveNetworksByAddress; /** * Constructs the URL for the active networks API endpoint. * * @param accountIds - Array of account IDs * @returns URL object for the API endpoint */ function buildActiveNetworksUrl(accountIds) { const url = new URL(`${exports.MULTICHAIN_ACCOUNTS_BASE_URL}/v2/activeNetworks`); url.searchParams.append('accountIds', accountIds.join(',')); return url; } exports.buildActiveNetworksUrl = buildActiveNetworksUrl; //# sourceMappingURL=accounts-api.cjs.map