Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,26 @@ That function will have state while `stream = true` is used.

### `@exodus/bytes/bigint.js`

Convert between BigInt and Uint8Array

```js
import { fromBigInt, toBigInt } from '@exodus/bytes/bigint.js'
```

#### `fromBigInt(bigint, { length, format = 'uint8' })`

Convert a BigInt to a Uint8Array or Buffer

The output bytes are in big-endian format.

Throws if the BigInt is negative or cannot fit into the specified length.

#### `toBigInt(arr)`

Convert a Uint8Array or Buffer to a BigInt

The bytes are interpreted as a big-endian unsigned integer.

### `@exodus/bytes/hex.js`

Implements Base16 from [RFC4648](https://datatracker.ietf.org/doc/html/rfc4648)
Expand Down
48 changes: 48 additions & 0 deletions bigint.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Convert between BigInt and Uint8Array
*
* ```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 output bytes are in big-endian format.
*
* 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 in big-endian format
*/
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