base58check.d.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. /// <reference types="node" />
  15. import type { OutputFormat, Uint8ArrayBuffer } from './array.js';
  16. /**
  17. * Hash function type that takes Uint8Array and returns a Promise of Uint8Array
  18. */
  19. export type HashFunction = (data: Uint8Array) => Promise<Uint8Array>;
  20. /**
  21. * Synchronous hash function type that takes Uint8Array and returns Uint8Array
  22. */
  23. export type HashFunctionSync = (data: Uint8Array) => Uint8Array;
  24. /**
  25. * Base58Check encoder/decoder instance with async methods
  26. */
  27. export interface Base58CheckAsync {
  28. /**
  29. * Encode bytes to base58check string asynchronously
  30. *
  31. * @param arr - The input bytes to encode
  32. * @returns A Promise that resolves to the base58check encoded string
  33. */
  34. encode(arr: Uint8Array): Promise<string>;
  35. /**
  36. * Decode a base58check string to bytes asynchronously
  37. *
  38. * @param string - The base58check encoded string
  39. * @param format - Output format (default: 'uint8')
  40. * @returns A Promise that resolves to the decoded bytes
  41. */
  42. decode(string: string, format?: 'uint8'): Promise<Uint8ArrayBuffer>;
  43. decode(string: string, format: 'arraybuffer'): Promise<ArrayBuffer>;
  44. decode(string: string, format: 'buffer'): Promise<Buffer>;
  45. decode(string: string, format?: OutputFormat): Promise<Uint8ArrayBuffer | ArrayBuffer | Buffer>;
  46. }
  47. /**
  48. * Base58Check encoder/decoder instance with both async and sync methods
  49. */
  50. export interface Base58CheckSync extends Base58CheckAsync {
  51. /**
  52. * Encode bytes to base58check string synchronously
  53. *
  54. * @param arr - The input bytes to encode
  55. * @returns The base58check encoded string
  56. */
  57. encodeSync(arr: Uint8Array): string;
  58. /**
  59. * Decode a base58check string to bytes synchronously
  60. *
  61. * @param string - The base58check encoded string
  62. * @param format - Output format (default: 'uint8')
  63. * @returns The decoded bytes
  64. */
  65. decodeSync(string: string, format?: 'uint8'): Uint8ArrayBuffer;
  66. decodeSync(string: string, format: 'arraybuffer'): ArrayBuffer;
  67. decodeSync(string: string, format: 'buffer'): Buffer;
  68. decodeSync(string: string, format?: OutputFormat): Uint8ArrayBuffer | ArrayBuffer | Buffer;
  69. }
  70. /**
  71. * Create a base58check encoder/decoder with custom hash functions
  72. *
  73. * @param hashAlgo - Async hash function (typically double SHA-256)
  74. * @param hashAlgoSync - Optional sync hash function
  75. * @returns Base58Check encoder/decoder instance
  76. */
  77. export function makeBase58check(hashAlgo: HashFunction | HashFunctionSync, hashAlgoSync: HashFunctionSync): Base58CheckSync;
  78. export function makeBase58check(hashAlgo: HashFunction | HashFunctionSync, hashAlgoSync?: undefined): Base58CheckAsync;
  79. /**
  80. * Encode bytes to base58check string asynchronously
  81. *
  82. * Uses double SHA-256 for checksum calculation
  83. *
  84. * @param arr - The input bytes to encode
  85. * @returns A Promise that resolves to the base58check encoded string
  86. */
  87. export function toBase58check(arr: Uint8Array): Promise<string>;
  88. /**
  89. * Decode a base58check string to bytes asynchronously
  90. *
  91. * Validates the checksum using double SHA-256
  92. *
  93. * @param string - The base58check encoded string
  94. * @param format - Output format (default: 'uint8')
  95. * @returns A Promise that resolves to the decoded bytes
  96. */
  97. export function fromBase58check(string: string, format?: 'uint8'): Promise<Uint8ArrayBuffer>;
  98. export function fromBase58check(string: string, format: 'arraybuffer'): Promise<ArrayBuffer>;
  99. export function fromBase58check(string: string, format: 'buffer'): Promise<Buffer>;
  100. export function fromBase58check(string: string, format?: OutputFormat): Promise<Uint8ArrayBuffer | ArrayBuffer | Buffer>;
  101. /**
  102. * Encode bytes to base58check string synchronously
  103. *
  104. * Uses double SHA-256 for checksum calculation
  105. *
  106. * @param arr - The input bytes to encode
  107. * @returns The base58check encoded string
  108. */
  109. export function toBase58checkSync(arr: Uint8Array): string;
  110. /**
  111. * Decode a base58check string to bytes synchronously
  112. *
  113. * Validates the checksum using double SHA-256
  114. *
  115. * @param string - The base58check encoded string
  116. * @param format - Output format (default: 'uint8')
  117. * @returns The decoded bytes
  118. */
  119. export function fromBase58checkSync(string: string, format?: 'uint8'): Uint8ArrayBuffer;
  120. export function fromBase58checkSync(string: string, format: 'arraybuffer'): ArrayBuffer;
  121. export function fromBase58checkSync(string: string, format: 'buffer'): Buffer;
  122. export function fromBase58checkSync(string: string, format?: OutputFormat): Uint8ArrayBuffer | ArrayBuffer | Buffer;