@metamask/account-api
Version:
MetaMask Account API
42 lines • 1.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.toMultichainAccountGroupId = toMultichainAccountGroupId;
exports.isMultichainAccountGroupId = isMultichainAccountGroupId;
exports.getGroupIndexFromMultichainAccountGroupId = getGroupIndexFromMultichainAccountGroupId;
const wallet_1 = require("../wallet.cjs");
const MULTICHAIN_ACCOUNT_GROUP_ID_REGEX = new RegExp(`^${wallet_1.AccountWalletType.Entropy}:.*/(?<groupIndex>\\d+)$`, '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 MULTICHAIN_ACCOUNT_GROUP_ID_REGEX.test(value);
}
/**
* Gets the multichain account index from an account group ID.
*
* @param id - Multichain account ID.
* @returns The multichain account index if extractable, undefined otherwise.
*/
function getGroupIndexFromMultichainAccountGroupId(id) {
const matched = id.match(MULTICHAIN_ACCOUNT_GROUP_ID_REGEX);
if (matched?.groups?.groupIndex === undefined) {
// Unable to extract group index, even though, type wise, this should not
// be possible!
throw new Error('Unable to extract group index');
}
return Number(matched.groups.groupIndex);
}
//# sourceMappingURL=group.cjs.map