Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,8 @@ Uses the XRP variant base58 alphabet

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

Implements [base58check](https://en.bitcoin.it/wiki/Base58Check_encoding) encoding.

```js
import { fromBase58check, toBase58check } from '@exodus/bytes/base58check.js'
import { fromBase58checkSync, toBase58checkSync } from '@exodus/bytes/base58check.js'
Expand All @@ -512,11 +514,33 @@ import { makeBase58check } from '@exodus/bytes/base58check.js'
On non-Node.js, requires peer dependency [@noble/hashes](https://www.npmjs.com/package/@noble/hashes) to be installed.

#### `async fromBase58check(string, format = 'uint8')`

Decode a base58check string to bytes asynchronously

Validates the checksum using double SHA-256

#### `async toBase58check(arr)`

Encode bytes to base58check string asynchronously

Uses double SHA-256 for checksum calculation

#### `fromBase58checkSync(string, format = 'uint8')`

Decode a base58check string to bytes synchronously

Validates the checksum using double SHA-256

#### `toBase58checkSync(arr)`

Encode bytes to base58check string synchronously

Uses double SHA-256 for checksum calculation

#### `makeBase58check(hashAlgo, hashAlgoSync)`

Create a base58check encoder/decoder with custom hash functions

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

```js
Expand Down
131 changes: 131 additions & 0 deletions base58check.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/**
* Implements [base58check](https://en.bitcoin.it/wiki/Base58Check_encoding) encoding.
*
* ```js
* import { fromBase58check, toBase58check } from '@exodus/bytes/base58check.js'
* import { fromBase58checkSync, toBase58checkSync } from '@exodus/bytes/base58check.js'
* import { makeBase58check } from '@exodus/bytes/base58check.js'
* ```
*
* On non-Node.js, requires peer dependency [@noble/hashes](https://www.npmjs.com/package/@noble/hashes) to be installed.
*
* @module @exodus/bytes/base58check.js
*/

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

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

/**
* Hash function type that takes Uint8Array and returns a Promise of Uint8Array
*/
export type HashFunction = (data: Uint8Array) => Promise<Uint8Array>;

/**
* Synchronous hash function type that takes Uint8Array and returns Uint8Array
*/
export type HashFunctionSync = (data: Uint8Array) => Uint8Array;

/**
* Base58Check encoder/decoder instance with async methods
*/
export interface Base58CheckAsync {
/**
* Encode bytes to base58check string asynchronously
*
* @param arr - The input bytes to encode
* @returns A Promise that resolves to the base58check encoded string
*/
encode(arr: Uint8ArrayBuffer): Promise<string>;

/**
* Decode a base58check string to bytes asynchronously
*
* @param str - The base58check encoded string
* @param format - Output format (default: 'uint8')
* @returns A Promise that resolves to the decoded bytes
*/
decode(str: string, format?: 'uint8'): Promise<Uint8Array>;
decode(str: string, format: 'buffer'): Promise<Buffer>;
decode(str: string, format?: OutputFormat): Promise<Uint8Array | Buffer>;
}

/**
* Base58Check encoder/decoder instance with both async and sync methods
*/
export interface Base58CheckSync extends Base58CheckAsync {
/**
* Encode bytes to base58check string synchronously
*
* @param arr - The input bytes to encode
* @returns The base58check encoded string
*/
encodeSync(arr: Uint8ArrayBuffer): string;

/**
* Decode a base58check string to bytes synchronously
*
* @param str - The base58check encoded string
* @param format - Output format (default: 'uint8')
* @returns The decoded bytes
*/
decodeSync(str: string, format?: 'uint8'): Uint8Array;
decodeSync(str: string, format: 'buffer'): Buffer;
decodeSync(str: string, format?: OutputFormat): Uint8Array | Buffer;
}

/**
* Create a base58check encoder/decoder with custom hash functions
*
* @param hashAlgo - Async hash function (typically double SHA-256)
* @param hashAlgoSync - Optional sync hash function
* @returns Base58Check encoder/decoder instance
*/
export function makeBase58check(hashAlgo: HashFunction, hashAlgoSync?: HashFunctionSync): Base58CheckSync;
export function makeBase58check(hashAlgo: HashFunction): Base58CheckAsync;

/**
* Encode bytes to base58check string asynchronously
*
* Uses double SHA-256 for checksum calculation
*
* @param arr - The input bytes to encode
* @returns A Promise that resolves to the base58check encoded string
*/
export function toBase58check(arr: Uint8ArrayBuffer): Promise<string>;

/**
* Decode a base58check string to bytes asynchronously
*
* Validates the checksum using double SHA-256
*
* @param str - The base58check encoded string
* @param format - Output format (default: 'uint8')
* @returns A Promise that resolves to the decoded bytes
*/
export function fromBase58check(str: string, format?: 'uint8'): Promise<Uint8Array>;
export function fromBase58check(str: string, format: 'buffer'): Promise<Buffer>;
export function fromBase58check(str: string, format?: OutputFormat): Promise<Uint8Array | Buffer>;

/**
* Encode bytes to base58check string synchronously
*
* Uses double SHA-256 for checksum calculation
*
* @param arr - The input bytes to encode
* @returns The base58check encoded string
*/
export function toBase58checkSync(arr: Uint8ArrayBuffer): string;

/**
* Decode a base58check string to bytes synchronously
*
* Validates the checksum using double SHA-256
*
* @param str - The base58check encoded string
* @param format - Output format (default: 'uint8')
* @returns The decoded bytes
*/
export function fromBase58checkSync(str: string, format?: 'uint8'): Uint8Array;
export function fromBase58checkSync(str: string, format: 'buffer'): Buffer;
export function fromBase58checkSync(str: string, format?: OutputFormat): Uint8Array | Buffer;
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"/base58.js",
"/base58.d.ts",
"/base58check.js",
"/base58check.d.ts",
"/base58check.node.js",
"/base64.js",
"/base64.d.ts",
Expand Down Expand Up @@ -140,6 +141,7 @@
"default": "./base58.js"
},
"./base58check.js": {
"types": "./base58check.d.ts",
"node": "./base58check.node.js",
"default": "./base58check.js"
},
Expand Down
Loading