UNPKG

@metamask/account-api

Version:
131 lines 4.26 kB
import type { KeyringAccount } from "@metamask/keyring-api"; import type { AccountSelector } from "./selector.mjs"; import { type AccountWallet, type AccountWalletId, type AccountWalletIdOf, type AccountWalletType } from "./wallet.mjs"; /** * Default account group unique ID. * * This constant can be used to reference the "default" group from * an account wallet. */ export declare const DEFAULT_ACCOUNT_GROUP_UNIQUE_ID: string; /** * Account group object. * * Each group types groups accounts using different criterias. */ export declare enum AccountGroupType { /** Group that represents a multichain account. */ MultichainAccount = "multichain-account", /** Group that represents a single account. */ SingleAccount = "single-account" } /** * Account group ID. */ export type AccountGroupId = `${AccountWalletId}/${string}`; /** * Regex to validate a valid account group ID. */ export declare const ACCOUNT_GROUP_ID_REGEX: RegExp; /** * Parsed account group ID with its parsed wallet component and its sub-ID. */ export type ParsedAccountGroupId = { wallet: { id: AccountWalletId; type: AccountWalletType; subId: string; }; subId: string; }; /** * Account group that can hold multiple accounts. */ export type AccountGroup<Account extends KeyringAccount> = { /** * Account group ID. */ get id(): AccountGroupId; /** * Account group type. */ get type(): AccountGroupType; /** * Account wallet (parent). */ get wallet(): AccountWallet<Account>; /** * Gets the accounts for this account group. * * @returns The accounts. */ getAccounts(): Account[]; /** * Gets the account for a given account ID. * * @param id - Account ID. * @returns The account or undefined if not found. */ getAccount(id: Account['id']): Account | undefined; /** * Query an account matching the selector. * * @param selector - Query selector. * @returns The account matching the selector or undefined if not matching. * @throws If multiple accounts match the selector. */ get(selector: AccountSelector<Account>): Account | undefined; /** * Query accounts matching the selector. * * @param selector - Query selector. * @returns The accounts matching the selector. */ select(selector: AccountSelector<Account>): Account[]; }; /** * Type utility to compute a constrained {@link AccountGroupId} type given a * specifc {@link AccountWalletType}. */ export type AccountGroupIdOf<WalletType extends AccountWalletType> = `${AccountWalletIdOf<WalletType>}/${string}`; /** * Convert a wallet ID and a unique ID, to a group ID. * * @param walletId - A wallet ID. * @param id - A unique ID. * @returns A group ID. */ export declare function toAccountGroupId<WalletType extends AccountWalletType>(walletId: AccountWalletIdOf<WalletType>, id: string): AccountGroupIdOf<WalletType>; /** * Convert a wallet ID to the default group ID. * * @param walletId - A wallet ID. * @returns The default group ID. */ export declare function toDefaultAccountGroupId<WalletType extends AccountWalletType>(walletId: AccountWalletIdOf<WalletType>): AccountGroupIdOf<WalletType>; /** * Checks if the given value is {@link AccountGroupId}. * * @param value - The value to check. * @returns Whether the value is a {@link AccountGroupId}. */ export declare function isAccountGroupId(value: string): value is AccountGroupId; /** * Parse a multichain account group ID to an object containing a wallet ID * information (wallet type and wallet sub-ID), as well as account group ID * information (group sub-ID). * * @param groupId - The account group ID to validate and parse. * @returns The parsed account group ID. * @throws When the group ID format is invalid. */ export declare function parseAccountGroupId(groupId: string): ParsedAccountGroupId; /** * Strip the account wallet ID from an account group ID. * * @param groupId - Account group ID. * @returns Account group sub-ID. * @throws When the group ID format is invalid. */ export declare function stripAccountWalletId(groupId: string): string; //# sourceMappingURL=group.d.mts.map