aboutsummaryrefslogtreecommitdiffstats
path: root/vanilla/node_modules/@exodus/bytes/base58check.d.ts
blob: 9e6c29ad0193e02da89fbb8f72c5ac5a15fa655f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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: Uint8Array): Promise<string>;

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

  /**
   * Decode a base58check string to bytes synchronously
   *
   * @param string - The base58check encoded string
   * @param format - Output format (default: 'uint8')
   * @returns The decoded bytes
   */
  decodeSync(string: string, format?: 'uint8'): Uint8ArrayBuffer;
  decodeSync(string: string, format: 'buffer'): Buffer;
  decodeSync(string: string, format?: OutputFormat): Uint8ArrayBuffer | 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 | HashFunctionSync, hashAlgoSync: HashFunctionSync): Base58CheckSync;
export function makeBase58check(hashAlgo: HashFunction | HashFunctionSync, hashAlgoSync?: undefined): 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: Uint8Array): Promise<string>;

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

/**
 * Decode a base58check string to bytes synchronously
 *
 * Validates the checksum using double SHA-256
 *
 * @param string - The base58check encoded string
 * @param format - Output format (default: 'uint8')
 * @returns The decoded bytes
 */
export function fromBase58checkSync(string: string, format?: 'uint8'): Uint8ArrayBuffer;
export function fromBase58checkSync(string: string, format: 'buffer'): Buffer;
export function fromBase58checkSync(string: string, format?: OutputFormat): Uint8ArrayBuffer | Buffer;