UNPKG

@metamask-previews/account-api

Version:
1 lines 2.64 kB
{"version":3,"file":"id.cjs","sourceRoot":"","sources":["../../../src/api/multichain/id.ts"],"names":[],"mappings":";;AAqBA,kEAIC;AASD,sDAKC;AAQD,0EAYC;AAxDD,0CAAkD;AAOlD,MAAM,iBAAiB,GAAG,IAAI,MAAM,CAClC,IAAI,8BAAqB,CAAC,OAAO,0BAA0B,EAC3D,GAAG,CACJ,CAAC;AAEF;;;;;GAKG;AACH,SAAgB,2BAA2B,CACzC,aAA8B;IAE9B,OAAO,GAAG,8BAAqB,CAAC,OAAO,IAAI,aAAa,EAAE,CAAC;AAC7D,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,qBAAqB,CACnC,QAAmC,EACnC,UAAkB;IAElB,OAAO,GAAG,QAAQ,IAAI,UAAU,EAAE,CAAC;AACrC,CAAC;AAED;;;;;GAKG;AACH,SAAgB,+BAA+B,CAC7C,OAAuB;IAEvB,MAAM,OAAO,GAAG,OAAO,CAAC,KAAK,CAAC,iBAAiB,CAAC,CAAC;IACjD,IAAI,OAAO,EAAE,CAAC;QACZ,IAAI,OAAO,CAAC,MAAM,EAAE,UAAU,KAAK,SAAS,EAAE,CAAC;YAC7C,OAAO,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;QAC3C,CAAC;IACH,CAAC;IAED,iCAAiC;IACjC,OAAO,SAAS,CAAC;AACnB,CAAC","sourcesContent":["import type { EntropySourceId } from '@metamask/keyring-api';\n\nimport type { AccountGroupId } from '../group';\nimport { AccountWalletCategory } from '../wallet';\n\nexport type MultichainAccountWalletId =\n `${AccountWalletCategory.Entropy}:${string}`;\n\nexport type MultichainAccountId = `${MultichainAccountWalletId}:${number}`; // Use number for the account group index.\n\nconst GROUP_INDEX_REGEX = new RegExp(\n `^${AccountWalletCategory.Entropy}:.*:(?<groupIndex>\\\\d+)$`,\n 'u',\n);\n\n/**\n * Gets the multichain account wallet ID from its entropy source.\n *\n * @param entropySource - Entropy source ID of that wallet.\n * @returns The multichain account wallet ID.\n */\nexport function toMultichainAccountWalletId(\n entropySource: EntropySourceId,\n): MultichainAccountWalletId {\n return `${AccountWalletCategory.Entropy}:${entropySource}`;\n}\n\n/**\n * Gets the multichain account 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 toMultichainAccountId(\n walletId: MultichainAccountWalletId,\n groupIndex: number,\n): MultichainAccountId {\n return `${walletId}:${groupIndex}`;\n}\n\n/**\n * Gets the multichain account index from an account group ID.\n *\n * @param groupId - Account group ID.\n * @returns The multichain account index if extractable, undefined otherwise.\n */\nexport function getGroupIndexFromAccountGroupId(\n groupId: AccountGroupId,\n): number | undefined {\n const matched = groupId.match(GROUP_INDEX_REGEX);\n if (matched) {\n if (matched.groups?.groupIndex !== undefined) {\n return Number(matched.groups.groupIndex);\n }\n }\n\n // Unable to extract group index.\n return undefined;\n}\n"]}