UNPKG

@metamask/account-api

Version:
67 lines 2.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MULTICHAIN_ACCOUNT_GROUP_ID_REGEX = void 0; exports.toMultichainAccountGroupId = toMultichainAccountGroupId; exports.isMultichainAccountGroupId = isMultichainAccountGroupId; exports.parseMultichainAccountGroupId = parseMultichainAccountGroupId; exports.getGroupIndexFromMultichainAccountGroupId = getGroupIndexFromMultichainAccountGroupId; /** * Regex to validate a valid multichain account group ID. */ exports.MULTICHAIN_ACCOUNT_GROUP_ID_REGEX = /^(?<walletId>(?<walletType>entropy):(?<walletSubId>.+))\/(?<groupIndex>[0-9]+)$/u; /** * Gets the multichain account group ID from its multichain account wallet ID and its index. * * @param walletId - Multichain account wallet ID. * @param groupIndex - Index of that multichain account. * @returns The multichain account ID. */ function toMultichainAccountGroupId(walletId, groupIndex) { return `${walletId}/${groupIndex}`; } /** * Checks if the given value is {@link MultichainAccountGroupId}. * * @param value - The value to check. * @returns Whether the value is a {@link MultichainAccountGroupId}. */ function isMultichainAccountGroupId(value) { return exports.MULTICHAIN_ACCOUNT_GROUP_ID_REGEX.test(value); } /** * Parse a multichain account group ID to an object containing a multichain * wallet ID information (wallet type and wallet sub-ID), as well as * multichain account group ID information (group index). * * @param groupId - The multichain account group ID to validate and parse. * @returns The parsed multichain account group ID. * @throws When the group ID format is invalid. */ function parseMultichainAccountGroupId(groupId) { const match = exports.MULTICHAIN_ACCOUNT_GROUP_ID_REGEX.exec(groupId); if (!match?.groups) { throw new Error(`Invalid multichain account group ID: "${groupId}"`); } const walletId = match.groups.walletId; const walletType = match.groups.walletType; const walletSubId = match.groups.walletSubId; return { wallet: { id: walletId, type: walletType, subId: walletSubId, }, groupIndex: Number(match.groups.groupIndex), }; } /** * Gets the multichain account index from an account group ID. * * @param id - Multichain account ID. * @returns The multichain account index if extractable, undefined otherwise. * @throws When the group ID format is invalid. */ function getGroupIndexFromMultichainAccountGroupId(id) { return parseMultichainAccountGroupId(id).groupIndex; } //# sourceMappingURL=group.cjs.map