@metamask/account-api
Version:
MetaMask Account API
37 lines • 1.45 kB
JavaScript
import { AccountWalletType } from "../wallet.mjs";
const MULTICHAIN_ACCOUNT_GROUP_ID_REGEX = new RegExp(`^${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.
*/
export 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}.
*/
export 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.
*/
export 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.mjs.map