Skip to content

Commit 54ed642

Browse files
CopilotChALkeR
andauthored
feat: add TypeScript types for base58check.js (#33)
Co-authored-by: Nikita Skovoroda <chalkerx@gmail.com>
1 parent 613d893 commit 54ed642

3 files changed

Lines changed: 157 additions & 0 deletions

File tree

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,8 @@ Uses the XRP variant base58 alphabet
503503

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

506+
Implements [base58check](https://en.bitcoin.it/wiki/Base58Check_encoding) encoding.
507+
506508
```js
507509
import { fromBase58check, toBase58check } from '@exodus/bytes/base58check.js'
508510
import { fromBase58checkSync, toBase58checkSync } from '@exodus/bytes/base58check.js'
@@ -512,11 +514,33 @@ import { makeBase58check } from '@exodus/bytes/base58check.js'
512514
On non-Node.js, requires peer dependency [@noble/hashes](https://www.npmjs.com/package/@noble/hashes) to be installed.
513515

514516
#### `async fromBase58check(string, format = 'uint8')`
517+
518+
Decode a base58check string to bytes asynchronously
519+
520+
Validates the checksum using double SHA-256
521+
515522
#### `async toBase58check(arr)`
523+
524+
Encode bytes to base58check string asynchronously
525+
526+
Uses double SHA-256 for checksum calculation
527+
516528
#### `fromBase58checkSync(string, format = 'uint8')`
529+
530+
Decode a base58check string to bytes synchronously
531+
532+
Validates the checksum using double SHA-256
533+
517534
#### `toBase58checkSync(arr)`
535+
536+
Encode bytes to base58check string synchronously
537+
538+
Uses double SHA-256 for checksum calculation
539+
518540
#### `makeBase58check(hashAlgo, hashAlgoSync)`
519541

542+
Create a base58check encoder/decoder with custom hash functions
543+
520544
### `@exodus/bytes/wif.js`
521545

522546
```js

base58check.d.ts

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/**
2+
* Implements [base58check](https://en.bitcoin.it/wiki/Base58Check_encoding) encoding.
3+
*
4+
* ```js
5+
* import { fromBase58check, toBase58check } from '@exodus/bytes/base58check.js'
6+
* import { fromBase58checkSync, toBase58checkSync } from '@exodus/bytes/base58check.js'
7+
* import { makeBase58check } from '@exodus/bytes/base58check.js'
8+
* ```
9+
*
10+
* On non-Node.js, requires peer dependency [@noble/hashes](https://www.npmjs.com/package/@noble/hashes) to be installed.
11+
*
12+
* @module @exodus/bytes/base58check.js
13+
*/
14+
15+
/// <reference types="node" />
16+
17+
import type { OutputFormat, Uint8ArrayBuffer } from './array.js';
18+
19+
/**
20+
* Hash function type that takes Uint8Array and returns a Promise of Uint8Array
21+
*/
22+
export type HashFunction = (data: Uint8Array) => Promise<Uint8Array>;
23+
24+
/**
25+
* Synchronous hash function type that takes Uint8Array and returns Uint8Array
26+
*/
27+
export type HashFunctionSync = (data: Uint8Array) => Uint8Array;
28+
29+
/**
30+
* Base58Check encoder/decoder instance with async methods
31+
*/
32+
export interface Base58CheckAsync {
33+
/**
34+
* Encode bytes to base58check string asynchronously
35+
*
36+
* @param arr - The input bytes to encode
37+
* @returns A Promise that resolves to the base58check encoded string
38+
*/
39+
encode(arr: Uint8ArrayBuffer): Promise<string>;
40+
41+
/**
42+
* Decode a base58check string to bytes asynchronously
43+
*
44+
* @param str - The base58check encoded string
45+
* @param format - Output format (default: 'uint8')
46+
* @returns A Promise that resolves to the decoded bytes
47+
*/
48+
decode(str: string, format?: 'uint8'): Promise<Uint8ArrayBuffer>;
49+
decode(str: string, format: 'buffer'): Promise<Buffer>;
50+
decode(str: string, format?: OutputFormat): Promise<Uint8ArrayBuffer | Buffer>;
51+
}
52+
53+
/**
54+
* Base58Check encoder/decoder instance with both async and sync methods
55+
*/
56+
export interface Base58CheckSync extends Base58CheckAsync {
57+
/**
58+
* Encode bytes to base58check string synchronously
59+
*
60+
* @param arr - The input bytes to encode
61+
* @returns The base58check encoded string
62+
*/
63+
encodeSync(arr: Uint8ArrayBuffer): string;
64+
65+
/**
66+
* Decode a base58check string to bytes synchronously
67+
*
68+
* @param str - The base58check encoded string
69+
* @param format - Output format (default: 'uint8')
70+
* @returns The decoded bytes
71+
*/
72+
decodeSync(str: string, format?: 'uint8'): Uint8ArrayBuffer;
73+
decodeSync(str: string, format: 'buffer'): Buffer;
74+
decodeSync(str: string, format?: OutputFormat): Uint8ArrayBuffer | Buffer;
75+
}
76+
77+
/**
78+
* Create a base58check encoder/decoder with custom hash functions
79+
*
80+
* @param hashAlgo - Async hash function (typically double SHA-256)
81+
* @param hashAlgoSync - Optional sync hash function
82+
* @returns Base58Check encoder/decoder instance
83+
*/
84+
export function makeBase58check(hashAlgo: HashFunction, hashAlgoSync?: HashFunctionSync): Base58CheckSync;
85+
export function makeBase58check(hashAlgo: HashFunction): Base58CheckAsync;
86+
87+
/**
88+
* Encode bytes to base58check string asynchronously
89+
*
90+
* Uses double SHA-256 for checksum calculation
91+
*
92+
* @param arr - The input bytes to encode
93+
* @returns A Promise that resolves to the base58check encoded string
94+
*/
95+
export function toBase58check(arr: Uint8ArrayBuffer): Promise<string>;
96+
97+
/**
98+
* Decode a base58check string to bytes asynchronously
99+
*
100+
* Validates the checksum using double SHA-256
101+
*
102+
* @param str - The base58check encoded string
103+
* @param format - Output format (default: 'uint8')
104+
* @returns A Promise that resolves to the decoded bytes
105+
*/
106+
export function fromBase58check(str: string, format?: 'uint8'): Promise<Uint8ArrayBuffer>;
107+
export function fromBase58check(str: string, format: 'buffer'): Promise<Buffer>;
108+
export function fromBase58check(str: string, format?: OutputFormat): Promise<Uint8ArrayBuffer | Buffer>;
109+
110+
/**
111+
* Encode bytes to base58check string synchronously
112+
*
113+
* Uses double SHA-256 for checksum calculation
114+
*
115+
* @param arr - The input bytes to encode
116+
* @returns The base58check encoded string
117+
*/
118+
export function toBase58checkSync(arr: Uint8ArrayBuffer): string;
119+
120+
/**
121+
* Decode a base58check string to bytes synchronously
122+
*
123+
* Validates the checksum using double SHA-256
124+
*
125+
* @param str - The base58check encoded string
126+
* @param format - Output format (default: 'uint8')
127+
* @returns The decoded bytes
128+
*/
129+
export function fromBase58checkSync(str: string, format?: 'uint8'): Uint8ArrayBuffer;
130+
export function fromBase58checkSync(str: string, format: 'buffer'): Buffer;
131+
export function fromBase58checkSync(str: string, format?: OutputFormat): Uint8ArrayBuffer | Buffer;

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
"/base58.js",
8787
"/base58.d.ts",
8888
"/base58check.js",
89+
"/base58check.d.ts",
8990
"/base58check.node.js",
9091
"/base64.js",
9192
"/base64.d.ts",
@@ -140,6 +141,7 @@
140141
"default": "./base58.js"
141142
},
142143
"./base58check.js": {
144+
"types": "./base58check.d.ts",
143145
"node": "./base58check.node.js",
144146
"default": "./base58check.js"
145147
},

0 commit comments

Comments
 (0)