UNPKG

@metamask/account-api

Version:
66 lines 2.11 kB
/** * Wallet type. * * Each wallet types groups accounts using different criterias. */ export var AccountWalletType; (function (AccountWalletType) { /** Wallet grouping accounts based on their entropy source. */ AccountWalletType["Entropy"] = "entropy"; /** Wallet grouping accounts based on their keyring's type. */ AccountWalletType["Keyring"] = "keyring"; /** Wallet grouping accounts associated with an account management Snap. */ AccountWalletType["Snap"] = "snap"; })(AccountWalletType || (AccountWalletType = {})); /** * Regex to validate an account wallet ID. */ export const ACCOUNT_WALLET_ID_REGEX = /^(?<walletType>entropy|keyring|snap):(?<walletSubId>.+)$/u; /** * Convert a unique ID to a wallet ID for a given type. * * @param type - A wallet type. * @param id - A unique ID. * @returns A wallet ID. */ export function toAccountWalletId(type, id) { return `${type}:${id}`; } /** * Checks if the given value is {@link AccountWalletId}. * * @param value - The value to check. * @returns Whether the value is a {@link AccountWalletId}. */ export function isAccountWalletId(value) { return ACCOUNT_WALLET_ID_REGEX.test(value); } /** * Parse an account wallet ID to an object containing a wallet ID information * (wallet type and wallet sub-ID). * * @param walletId - The account wallet ID to validate and parse. * @returns The parsed account wallet ID. * @throws When the wallet ID format is invalid. */ export function parseAccountWalletId(walletId) { const match = ACCOUNT_WALLET_ID_REGEX.exec(walletId); if (!match?.groups) { throw new Error(`Invalid account wallet ID: "${walletId}"`); } return { type: match.groups.walletType, subId: match.groups.walletSubId, }; } /** * Strip the account wallet type from an account wallet ID. * * @param walletId - Account wallet ID. * @returns Account wallet sub-ID. * @throws When the wallet ID format is invalid. */ export function stripAccountWalletType(walletId) { return parseAccountWalletId(walletId).subId; } //# sourceMappingURL=wallet.mjs.map