Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions bigint.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Utilities for converting between BigInt and Uint8Array.
Comment thread
ChALkeR marked this conversation as resolved.
Outdated
*
* ```js
* import { fromBigInt, toBigInt } from '@exodus/bytes/bigint.js'
* ```
*
* @module @exodus/bytes/bigint.js
*/

/// <reference types="node" />

import type { OutputFormat, Uint8ArrayBuffer } from './array.js';

/**
* Options for converting BigInt to bytes
*/
export interface FromBigIntOptions {
/** The length in bytes of the output array */
length: number;
/** Output format (default: 'uint8') */
format?: OutputFormat;
}

/**
* Convert a BigInt to a Uint8Array or Buffer
*
* The BigInt is converted to a hex string and then padded to the specified length.
Comment thread
ChALkeR marked this conversation as resolved.
Outdated
* Throws if the BigInt is negative or cannot fit into the specified length.
Comment thread
ChALkeR marked this conversation as resolved.
Comment thread
ChALkeR marked this conversation as resolved.
*
* @param x - The BigInt to convert (must be non-negative)
* @param options - Conversion options
* @returns The converted bytes
*/
export function fromBigInt(x: bigint, options: { length: number; format?: 'uint8' }): Uint8ArrayBuffer;
export function fromBigInt(x: bigint, options: { length: number; format: 'buffer' }): Buffer;
export function fromBigInt(x: bigint, options: FromBigIntOptions): Uint8ArrayBuffer | Buffer;

/**
* Convert a Uint8Array or Buffer to a BigInt
*
* The bytes are interpreted as a big-endian unsigned integer.
*
* @param a - The bytes to convert
Comment thread
ChALkeR marked this conversation as resolved.
Outdated
* @returns The BigInt representation
*/
export function toBigInt(a: ArrayBufferView): bigint;
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
"/base64.d.ts",
"/bech32.js",
"/bigint.js",
"/bigint.d.ts",
"/encoding-browser.js",
"/encoding-browser.browser.js",
"/encoding-browser.native.js",
Expand Down Expand Up @@ -140,7 +141,10 @@
"default": "./base64.js"
},
"./bech32.js": "./bech32.js",
"./bigint.js": "./bigint.js",
"./bigint.js": {
"types": "./bigint.d.ts",
"default": "./bigint.js"
},
"./hex.js": {
"types": "./hex.d.ts",
"node": "./hex.node.js",
Expand Down