@metamask/multichain-account-service
Version:
Service to manage multichain accounts
1 lines • 4.13 kB
Source Map (JSON)
{"version":3,"file":"utils.mjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAQA;;;;;;GAMG;AACH,MAAM,UAAU,4BAA4B,CAAC,EAC3C,IAAI,GAAG,CAAC,EACR,EAAE,GACc;IAChB,IAAI,IAAI,GAAG,CAAC,EAAE,CAAC;QACb,MAAM,IAAI,KAAK,CAAC,oBAAoB,IAAI,gBAAgB,CAAC,CAAC;IAC5D,CAAC;IAED,IAAI,EAAE,GAAG,CAAC,EAAE,CAAC;QACX,MAAM,IAAI,KAAK,CAAC,kBAAkB,EAAE,gBAAgB,CAAC,CAAC;IACxD,CAAC;IAED,IAAI,EAAE,GAAG,IAAI,EAAE,CAAC;QACd,MAAM,IAAI,KAAK,CAAC,kBAAkB,EAAE,sBAAsB,IAAI,GAAG,CAAC,CAAC;IACrE,CAAC;AACH,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,uBAAuB,CACrC,UAAkB,EAClB,cAAsB;IAEtB,IAAI,UAAU,GAAG,cAAc,EAAE,CAAC;QAChC,MAAM,IAAI,KAAK,CACb,gCAAgC,UAAU,sDAAsD,cAAc,GAAG,CAClH,CAAC;IACJ,CAAC;AACH,CAAC;AAkBD;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAG/B,OAAe,EACf,UAAiB,EACjB,OAAkB,EACK,EAAE;IACzB,MAAM,KAAK,GAAG,IAAI,KAAK,CAAC,OAAO,CAA0B,CAAC;IAC1D,KAAK,CAAC,KAAK,GAAG,UAAU,CAAC;IACzB,IAAI,OAAO,EAAE,CAAC;QACZ,KAAK,CAAC,OAAO,GAAG,OAAO,CAAC;IAC1B,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,UAAU,cAAc,CAAC,KAAc;IAC3C,OAAO,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAChE,CAAC","sourcesContent":["/**\n * Range-based multichain account creations type.\n */\nexport type GroupIndexRange = {\n from?: number;\n to: number;\n};\n\n/**\n * Asserts that a range is valid.\n *\n * @param range - The range to assert.\n * @param range.from - The starting index of the range (inclusive).\n * @param range.to - The ending index of the range (inclusive).\n */\nexport function assertGroupIndexRangeIsValid({\n from = 0,\n to,\n}: GroupIndexRange): void {\n if (from < 0) {\n throw new Error(`Bad range, from (${from}) must be >= 0`);\n }\n\n if (to < 0) {\n throw new Error(`Bad range, to (${to}) must be >= 0`);\n }\n\n if (to < from) {\n throw new Error(`Bad range, to (${to}) must be >= from (${from})`);\n }\n}\n\n/**\n * Asserts that a group index is valid given the next available group index.\n *\n * @param groupIndex - The group index to assert.\n * @param nextGroupIndex - The next available group index.\n */\nexport function assertGroupIndexIsValid(\n groupIndex: number,\n nextGroupIndex: number,\n): void {\n if (groupIndex > nextGroupIndex) {\n throw new Error(\n `Bad group index, groupIndex (${groupIndex}) cannot be higher than the next available one (<= ${nextGroupIndex})`,\n );\n }\n}\n\n/**\n * Augmented `Error` shape produced by {@link createSentryError}. The runtime\n * value carries a `cause` and (optionally) a structured `context` payload\n * that downstream Sentry tooling can read.\n *\n * The `TContext` type parameter narrows the shape of `context` for callers\n * that know what they put in — most useful in tests when asserting on a\n * captured error.\n */\nexport type SentryError<\n TContext extends Record<string, unknown> = Record<string, unknown>,\n> = Error & {\n cause: Error;\n context?: TContext;\n};\n\n/**\n * Creates a Sentry error from an error message, an inner error and a context.\n *\n * NOTE: Sentry defaults to a depth of 3 when extracting non-native attributes.\n * As such, the context depth shouldn't be too deep.\n *\n * @param message - The error message to create a Sentry error from.\n * @param innerError - The inner error to create a Sentry error from.\n * @param context - The context to add to the Sentry error.\n * @returns A Sentry error.\n */\nexport const createSentryError = <\n TContext extends Record<string, unknown> = Record<string, unknown>,\n>(\n message: string,\n innerError: Error,\n context?: TContext,\n): SentryError<TContext> => {\n const error = new Error(message) as SentryError<TContext>;\n error.cause = innerError;\n if (context) {\n error.context = context;\n }\n return error;\n};\n\n/**\n * Converts an unknown error value to a string message.\n *\n * @param error - The error to convert.\n * @returns The error message if the error is an `Error` instance, otherwise\n * the string representation of the value.\n */\nexport function toErrorMessage(error: unknown): string {\n return error instanceof Error ? error.message : String(error);\n}\n"]}