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