@metamask/account-api
Version:
MetaMask Account API
1 lines • 3.41 kB
Source Map (JSON)
{"version":3,"file":"group.mjs","sourceRoot":"","sources":["../../../src/api/multichain/group.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,iBAAiB,EAAE,sBAAkB;AAE9C,MAAM,iCAAiC,GAAG,IAAI,MAAM,CAClD,IAAI,iBAAiB,CAAC,OAAO,0BAA0B,EACvD,GAAG,CACJ,CAAC;AAkCF;;;;;;GAMG;AACH,MAAM,UAAU,0BAA0B,CACxC,QAAmC,EACnC,UAAkB;IAElB,OAAO,GAAG,QAAQ,IAAI,UAAU,EAAE,CAAC;AACrC,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,0BAA0B,CACxC,KAAa;IAEb,OAAO,iCAAiC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AACvD,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,yCAAyC,CACvD,EAA4B;IAE5B,MAAM,OAAO,GAAG,EAAE,CAAC,KAAK,CAAC,iCAAiC,CAAC,CAAC;IAC5D,IAAI,OAAO,EAAE,MAAM,EAAE,UAAU,KAAK,SAAS,EAAE,CAAC;QAC9C,yEAAyE;QACzE,eAAe;QACf,MAAM,IAAI,KAAK,CAAC,+BAA+B,CAAC,CAAC;IACnD,CAAC;IAED,OAAO,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;AAC3C,CAAC","sourcesContent":["import { type KeyringAccount } from '@metamask/keyring-api';\n\nimport type {\n MultichainAccountWallet,\n MultichainAccountWalletId,\n} from './wallet';\nimport type { Bip44Account } from '../bip44';\nimport type { AccountGroup, AccountGroupType } from '../group';\nimport { AccountWalletType } from '../wallet';\n\nconst MULTICHAIN_ACCOUNT_GROUP_ID_REGEX = new RegExp(\n `^${AccountWalletType.Entropy}:.*/(?<groupIndex>\\\\d+)$`,\n 'u',\n);\n\n/**\n * Multichain account ID.\n */\nexport type MultichainAccountGroupId = `${MultichainAccountWalletId}/${number}`; // Use number for the account group index.\n\n/**\n * A multichain account that holds multiple accounts.\n */\nexport type MultichainAccountGroup<\n Account extends Bip44Account<KeyringAccount>,\n> = AccountGroup<Account> & {\n /**\n * Multichain account group ID.\n */\n get id(): MultichainAccountGroupId;\n\n /**\n * Multichain account type.\n */\n get type(): AccountGroupType.MultichainAccount;\n\n /**\n * Multichain account's wallet reference (parent).\n */\n get wallet(): MultichainAccountWallet<Account>;\n\n /**\n * Multichain account group index.\n */\n get groupIndex(): number;\n};\n\n/**\n * Gets the multichain account group ID from its multichain account wallet ID and its index.\n *\n * @param walletId - Multichain account wallet ID.\n * @param groupIndex - Index of that multichain account.\n * @returns The multichain account ID.\n */\nexport function toMultichainAccountGroupId(\n walletId: MultichainAccountWalletId,\n groupIndex: number,\n): MultichainAccountGroupId {\n return `${walletId}/${groupIndex}`;\n}\n\n/**\n * Checks if the given value is {@link MultichainAccountGroupId}.\n *\n * @param value - The value to check.\n * @returns Whether the value is a {@link MultichainAccountGroupId}.\n */\nexport function isMultichainAccountGroupId(\n value: string,\n): value is MultichainAccountGroupId {\n return MULTICHAIN_ACCOUNT_GROUP_ID_REGEX.test(value);\n}\n\n/**\n * Gets the multichain account index from an account group ID.\n *\n * @param id - Multichain account ID.\n * @returns The multichain account index if extractable, undefined otherwise.\n */\nexport function getGroupIndexFromMultichainAccountGroupId(\n id: MultichainAccountGroupId,\n): number {\n const matched = id.match(MULTICHAIN_ACCOUNT_GROUP_ID_REGEX);\n if (matched?.groups?.groupIndex === undefined) {\n // Unable to extract group index, even though, type wise, this should not\n // be possible!\n throw new Error('Unable to extract group index');\n }\n\n return Number(matched.groups.groupIndex);\n}\n"]}