-
-
Notifications
You must be signed in to change notification settings - Fork 9
feat(account-api): add group/wallet id parsing support #360
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9df6869
442cdcb
3923bca
4cffe65
67a405d
f02f1bc
035c4b0
67bee33
9d443f9
c6a479d
7137f01
e9fae68
558db26
6b56635
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,18 +6,31 @@ import type { | |
| } from './wallet'; | ||
| import type { Bip44Account } from '../bip44'; | ||
| import type { AccountGroup, AccountGroupType } from '../group'; | ||
| import { AccountWalletType } from '../wallet'; | ||
|
|
||
| const MULTICHAIN_ACCOUNT_GROUP_ID_REGEX = new RegExp( | ||
| `^${AccountWalletType.Entropy}:.*/(?<groupIndex>\\d+)$`, | ||
| 'u', | ||
| ); | ||
| import type { AccountWalletType } from '../wallet'; | ||
|
|
||
| /** | ||
| * Multichain account ID. | ||
| */ | ||
| export type MultichainAccountGroupId = `${MultichainAccountWalletId}/${number}`; // Use number for the account group index. | ||
|
|
||
| /** | ||
| * Regex to validate a valid multichain account group ID. | ||
| */ | ||
| export const MULTICHAIN_ACCOUNT_GROUP_ID_REGEX = | ||
| /^(?<walletId>(?<walletType>entropy):(?<walletSubId>.+))\/(?<groupIndex>[0-9]+)$/u; | ||
|
|
||
| /** | ||
| * Parsed account group ID with its parsed wallet component and its sub-ID. | ||
| */ | ||
| export type ParsedMultichainAccountGroupId = { | ||
| wallet: { | ||
| id: MultichainAccountWalletId; | ||
| type: AccountWalletType.Entropy; | ||
| subId: string; | ||
| }; | ||
| groupIndex: number; | ||
| }; | ||
|
|
||
| /** | ||
| * A multichain account that holds multiple accounts. | ||
| */ | ||
|
|
@@ -71,21 +84,46 @@ export function isMultichainAccountGroupId( | |
| return MULTICHAIN_ACCOUNT_GROUP_ID_REGEX.test(value); | ||
| } | ||
|
|
||
| /** | ||
| * Parse a multichain account group ID to an object containing a multichain | ||
| * wallet ID information (wallet type and wallet sub-ID), as well as | ||
| * multichain account group ID information (group index). | ||
| * | ||
| * @param groupId - The multichain account group ID to validate and parse. | ||
| * @returns The parsed multichain account group ID. | ||
| * @throws When the group ID format is invalid. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reason I suggested having a type of the error in front is because it's proposed by TSDoc and also some editors (like mine) are recognizing it that way. It's not a blocker, but something to think about. https://tsdoc.org/pages/tags/throws/ They're more specific about their suggestion: For example what I see in editor: So, it's kinda "more semantic" and IDE uses it in some way. But, anyway this is not hard requirement, just some stuff good to know if you want to consider having in the future.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wasn't aware of this TBH. It's good that you share this! I'll definitely start using those then, I thought those were just old type annotation that we were not really using anymore in tsdoc (cause we don't really have rule to enforce this in all the linter/toolings we have in place). Anyway, I'll make sure to update our docs with this next time! 👍 |
||
| */ | ||
| export function parseMultichainAccountGroupId( | ||
| groupId: string, | ||
| ): ParsedMultichainAccountGroupId { | ||
| const match = MULTICHAIN_ACCOUNT_GROUP_ID_REGEX.exec(groupId); | ||
| if (!match?.groups) { | ||
| throw new Error(`Invalid multichain account group ID: "${groupId}"`); | ||
| } | ||
|
|
||
| const walletId = match.groups.walletId as MultichainAccountWalletId; | ||
| const walletType = match.groups.walletType as AccountWalletType.Entropy; | ||
| const walletSubId = match.groups.walletSubId as string; | ||
|
|
||
| return { | ||
| wallet: { | ||
| id: walletId, | ||
| type: walletType, | ||
| subId: walletSubId, | ||
| }, | ||
| groupIndex: Number(match.groups.groupIndex), | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the multichain account index from an account group ID. | ||
| * | ||
| * @param id - Multichain account ID. | ||
| * @returns The multichain account index if extractable, undefined otherwise. | ||
| * @throws When the group ID format is invalid. | ||
| */ | ||
| export function getGroupIndexFromMultichainAccountGroupId( | ||
| id: MultichainAccountGroupId, | ||
| ): number { | ||
| const matched = id.match(MULTICHAIN_ACCOUNT_GROUP_ID_REGEX); | ||
| if (matched?.groups?.groupIndex === undefined) { | ||
| // Unable to extract group index, even though, type wise, this should not | ||
| // be possible! | ||
| throw new Error('Unable to extract group index'); | ||
| } | ||
|
|
||
| return Number(matched.groups.groupIndex); | ||
| return parseMultichainAccountGroupId(id).groupIndex; | ||
| } | ||


Uh oh!
There was an error while loading. Please reload this page.