UNPKG

@metamask/account-api

Version:
1 lines 6.62 kB
{"version":3,"file":"wallet.mjs","sourceRoot":"","sources":["../../src/api/wallet.ts"],"names":[],"mappings":"AAOA;;;;GAIG;AACH,MAAM,CAAN,IAAY,iBASX;AATD,WAAY,iBAAiB;IAC3B,8DAA8D;IAC9D,wCAAmB,CAAA;IAEnB,8DAA8D;IAC9D,wCAAmB,CAAA;IAEnB,2EAA2E;IAC3E,kCAAa,CAAA;AACf,CAAC,EATW,iBAAiB,KAAjB,iBAAiB,QAS5B;AAOD;;GAEG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAClC,2DAA2D,CAAC;AAyH9D;;;;;;GAMG;AACH,MAAM,UAAU,iBAAiB,CAC/B,IAAgB,EAChB,EAAU;IAEV,OAAO,GAAG,IAAI,IAAI,EAAE,EAAE,CAAC;AACzB,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,iBAAiB,CAAC,KAAa;IAC7C,OAAO,uBAAuB,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AAC7C,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,oBAAoB,CAAC,QAAgB;IACnD,MAAM,KAAK,GAAG,uBAAuB,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IACrD,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,CAAC;QACnB,MAAM,IAAI,KAAK,CAAC,+BAA+B,QAAQ,GAAG,CAAC,CAAC;IAC9D,CAAC;IAED,OAAO;QACL,IAAI,EAAE,KAAK,CAAC,MAAM,CAAC,UAA+B;QAClD,KAAK,EAAE,KAAK,CAAC,MAAM,CAAC,WAAqB;KAC1C,CAAC;AACJ,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,sBAAsB,CAAC,QAAgB;IACrD,OAAO,oBAAoB,CAAC,QAAQ,CAAC,CAAC,KAAK,CAAC;AAC9C,CAAC","sourcesContent":["import type { KeyringAccount } from '@metamask/keyring-api';\n\n// Circular import are allowed when using `import type`.\nimport type { Bip44Account } from './bip44';\nimport type { AccountGroup, AccountGroupId } from './group';\nimport type { MultichainAccountWallet } from './multichain';\n\n/**\n * Wallet type.\n *\n * Each wallet types groups accounts using different criterias.\n */\nexport enum AccountWalletType {\n /** Wallet grouping accounts based on their entropy source. */\n Entropy = 'entropy',\n\n /** Wallet grouping accounts based on their keyring's type. */\n Keyring = 'keyring',\n\n /** Wallet grouping accounts associated with an account management Snap. */\n Snap = 'snap',\n}\n\n/**\n * Account wallet ID.\n */\nexport type AccountWalletId = `${AccountWalletType}:${string}`;\n\n/**\n * Regex to validate an account wallet ID.\n */\nexport const ACCOUNT_WALLET_ID_REGEX =\n /^(?<walletType>entropy|keyring|snap):(?<walletSubId>.+)$/u;\n\n/**\n * Wallet status.\n *\n * Those status are used to report in which \"state\" the wallet is currently\n * in. All of those operations cannot run concurrently, thus, the wallet\n * cannot have multiple status at once.\n */\nexport type AccountWalletStatus =\n /**\n * The wallet is not initialized yet.\n */\n | 'uninitialized'\n /**\n * The wallet is ready to run any operation.\n */\n | 'ready';\n\n/**\n * Parsed account wallet ID with its wallet type and sub-ID.\n */\nexport type ParsedAccountWalletId = {\n type: AccountWalletType;\n subId: string;\n};\n\n/**\n * Keyring account wallet that can hold multiple account groups.\n */\nexport type BaseAccountWallet<Account extends KeyringAccount> = {\n /**\n * Account wallet ID.\n */\n get id(): AccountWalletId;\n\n /**\n * Account wallet type.\n */\n get type(): AccountWalletType;\n\n /**\n * Account wallet status.\n */\n get status(): string; // Has to be refined by the type extending this base type.\n\n /**\n * Gets account group for a given ID.\n *\n * @param id - Account group ID.\n * @returns Account group.\n */\n getAccountGroup(id: AccountGroupId): AccountGroup<Account> | undefined;\n\n /**\n * Gets all account groups.\n *\n * @returns Account groups.\n */\n getAccountGroups(): AccountGroup<Account>[];\n};\n\n/**\n * Keyring account wallet that can hold multiple account groups.\n */\nexport type KeyringAccountWallet<Account extends KeyringAccount> =\n BaseAccountWallet<Account> & {\n /**\n * Keyring account wallet type, which is always {@link AccountWalletType.Keyring}.\n */\n get type(): AccountWalletType.Keyring;\n\n /**\n * Account wallet status.\n */\n get status(): AccountWalletStatus;\n };\n\n/**\n * Snap keyring account wallet that can hold multiple account groups.\n */\nexport type SnapAccountWallet<Account extends KeyringAccount> =\n BaseAccountWallet<Account> & {\n /**\n * Snap account wallet type, which is always {@link AccountWalletType.Snap}.\n */\n get type(): AccountWalletType.Snap;\n\n /**\n * Account wallet status.\n */\n get status(): AccountWalletStatus;\n };\n\n/**\n * Type constraint for a {@link AccountGroupObject}. If one of its union-members\n * does not match this contraint, {@link AccountGroupObject} will resolve\n * to `never`.\n */\ntype IsAccountWallet<\n Wallet extends BaseAccountWallet<Account>,\n Account extends KeyringAccount,\n> = Wallet;\n\n/**\n * Account wallet that can hold multiple account groups.\n */\nexport type AccountWallet<Account extends KeyringAccount> = IsAccountWallet<\n | KeyringAccountWallet<Account>\n | SnapAccountWallet<Account>\n | MultichainAccountWallet<Bip44Account<Account>>,\n Account\n>;\n\n/**\n * Type utility to compute a constrained {@link AccountWalletId} type given a\n * specifc {@link AccountWalletType}.\n */\nexport type AccountWalletIdOf<WalletType extends AccountWalletType> =\n `${WalletType}:${string}`;\n\n/**\n * Convert a unique ID to a wallet ID for a given type.\n *\n * @param type - A wallet type.\n * @param id - A unique ID.\n * @returns A wallet ID.\n */\nexport function toAccountWalletId<WalletType extends AccountWalletType>(\n type: WalletType,\n id: string,\n): AccountWalletIdOf<WalletType> {\n return `${type}:${id}`;\n}\n\n/**\n * Checks if the given value is {@link AccountWalletId}.\n *\n * @param value - The value to check.\n * @returns Whether the value is a {@link AccountWalletId}.\n */\nexport function isAccountWalletId(value: string): value is AccountWalletId {\n return ACCOUNT_WALLET_ID_REGEX.test(value);\n}\n\n/**\n * Parse an account wallet ID to an object containing a wallet ID information\n * (wallet type and wallet sub-ID).\n *\n * @param walletId - The account wallet ID to validate and parse.\n * @returns The parsed account wallet ID.\n * @throws When the wallet ID format is invalid.\n */\nexport function parseAccountWalletId(walletId: string): ParsedAccountWalletId {\n const match = ACCOUNT_WALLET_ID_REGEX.exec(walletId);\n if (!match?.groups) {\n throw new Error(`Invalid account wallet ID: \"${walletId}\"`);\n }\n\n return {\n type: match.groups.walletType as AccountWalletType,\n subId: match.groups.walletSubId as string,\n };\n}\n\n/**\n * Strip the account wallet type from an account wallet ID.\n *\n * @param walletId - Account wallet ID.\n * @returns Account wallet sub-ID.\n * @throws When the wallet ID format is invalid.\n */\nexport function stripAccountWalletType(walletId: string): string {\n return parseAccountWalletId(walletId).subId;\n}\n"]}