bigint.d.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /**
  2. * Convert between BigInt and Uint8Array
  3. *
  4. * ```js
  5. * import { fromBigInt, toBigInt } from '@exodus/bytes/bigint.js'
  6. * ```
  7. *
  8. * @module @exodus/bytes/bigint.js
  9. */
  10. /// <reference types="node" />
  11. import type { OutputFormat, Uint8ArrayBuffer } from './array.js';
  12. /**
  13. * Options for converting BigInt to bytes
  14. */
  15. export interface FromBigIntOptions {
  16. /** The length in bytes of the output array */
  17. length: number;
  18. /** Output format (default: 'uint8') */
  19. format?: OutputFormat;
  20. }
  21. /**
  22. * Convert a BigInt to a Uint8Array or Buffer
  23. *
  24. * The output bytes are in big-endian format.
  25. *
  26. * Throws if the BigInt is negative or cannot fit into the specified length.
  27. *
  28. * @param bigint - The BigInt to convert (must be non-negative)
  29. * @param options - Conversion options
  30. * @returns The converted bytes in big-endian format
  31. */
  32. export function fromBigInt(bigint: bigint, options: { length: number; format?: 'uint8' }): Uint8ArrayBuffer;
  33. export function fromBigInt(bigint: bigint, options: { length: number; format: 'buffer' }): Buffer;
  34. export function fromBigInt(bigint: bigint, options: FromBigIntOptions): Uint8ArrayBuffer | Buffer;
  35. /**
  36. * Convert a Uint8Array or Buffer to a BigInt
  37. *
  38. * The bytes are interpreted as a big-endian unsigned integer.
  39. *
  40. * @param arr - The bytes to convert
  41. * @returns The BigInt representation
  42. */
  43. export function toBigInt(arr: Uint8Array): bigint;