bigint.d.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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: 'arraybuffer' }): ArrayBuffer;
  34. export function fromBigInt(bigint: bigint, options: { length: number; format: 'buffer' }): Buffer;
  35. export function fromBigInt(bigint: bigint, options: FromBigIntOptions): Uint8ArrayBuffer | ArrayBuffer | Buffer;
  36. /**
  37. * Convert a Uint8Array or Buffer to a BigInt
  38. *
  39. * The bytes are interpreted as a big-endian unsigned integer.
  40. *
  41. * @param arr - The bytes to convert
  42. * @returns The BigInt representation
  43. */
  44. export function toBigInt(arr: Uint8Array): bigint;