@metamask/account-api
Version:
MetaMask Account API
73 lines • 2.47 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.ACCOUNT_WALLET_ID_REGEX = exports.AccountWalletType = void 0;
exports.toAccountWalletId = toAccountWalletId;
exports.isAccountWalletId = isAccountWalletId;
exports.parseAccountWalletId = parseAccountWalletId;
exports.stripAccountWalletType = stripAccountWalletType;
/**
* Wallet type.
*
* Each wallet types groups accounts using different criterias.
*/
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 || (exports.AccountWalletType = AccountWalletType = {}));
/**
* Regex to validate an account wallet ID.
*/
exports.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.
*/
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}.
*/
function isAccountWalletId(value) {
return exports.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.
*/
function parseAccountWalletId(walletId) {
const match = exports.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.
*/
function stripAccountWalletType(walletId) {
return parseAccountWalletId(walletId).subId;
}
//# sourceMappingURL=wallet.cjs.map
;