base58check.d.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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: 'buffer'): Promise<Buffer>;
  44. decode(string: string, format?: OutputFormat): Promise<Uint8ArrayBuffer | Buffer>;
  45. }
  46. /**
  47. * Base58Check encoder/decoder instance with both async and sync methods
  48. */
  49. export interface Base58CheckSync extends Base58CheckAsync {
  50. /**
  51. * Encode bytes to base58check string synchronously
  52. *
  53. * @param arr - The input bytes to encode
  54. * @returns The base58check encoded string
  55. */
  56. encodeSync(arr: Uint8Array): string;
  57. /**
  58. * Decode a base58check string to bytes synchronously
  59. *
  60. * @param string - The base58check encoded string
  61. * @param format - Output format (default: 'uint8')
  62. * @returns The decoded bytes
  63. */
  64. decodeSync(string: string, format?: 'uint8'): Uint8ArrayBuffer;
  65. decodeSync(string: string, format: 'buffer'): Buffer;
  66. decodeSync(string: string, format?: OutputFormat): Uint8ArrayBuffer | Buffer;
  67. }
  68. /**
  69. * Create a base58check encoder/decoder with custom hash functions
  70. *
  71. * @param hashAlgo - Async hash function (typically double SHA-256)
  72. * @param hashAlgoSync - Optional sync hash function
  73. * @returns Base58Check encoder/decoder instance
  74. */
  75. export function makeBase58check(hashAlgo: HashFunction | HashFunctionSync, hashAlgoSync: HashFunctionSync): Base58CheckSync;
  76. export function makeBase58check(hashAlgo: HashFunction | HashFunctionSync, hashAlgoSync?: undefined): Base58CheckAsync;
  77. /**
  78. * Encode bytes to base58check string asynchronously
  79. *
  80. * Uses double SHA-256 for checksum calculation
  81. *
  82. * @param arr - The input bytes to encode
  83. * @returns A Promise that resolves to the base58check encoded string
  84. */
  85. export function toBase58check(arr: Uint8Array): Promise<string>;
  86. /**
  87. * Decode a base58check string to bytes asynchronously
  88. *
  89. * Validates the checksum using double SHA-256
  90. *
  91. * @param string - The base58check encoded string
  92. * @param format - Output format (default: 'uint8')
  93. * @returns A Promise that resolves to the decoded bytes
  94. */
  95. export function fromBase58check(string: string, format?: 'uint8'): Promise<Uint8ArrayBuffer>;
  96. export function fromBase58check(string: string, format: 'buffer'): Promise<Buffer>;
  97. export function fromBase58check(string: string, format?: OutputFormat): Promise<Uint8ArrayBuffer | Buffer>;
  98. /**
  99. * Encode bytes to base58check string synchronously
  100. *
  101. * Uses double SHA-256 for checksum calculation
  102. *
  103. * @param arr - The input bytes to encode
  104. * @returns The base58check encoded string
  105. */
  106. export function toBase58checkSync(arr: Uint8Array): string;
  107. /**
  108. * Decode a base58check string to bytes synchronously
  109. *
  110. * Validates the checksum using double SHA-256
  111. *
  112. * @param string - The base58check encoded string
  113. * @param format - Output format (default: 'uint8')
  114. * @returns The decoded bytes
  115. */
  116. export function fromBase58checkSync(string: string, format?: 'uint8'): Uint8ArrayBuffer;
  117. export function fromBase58checkSync(string: string, format: 'buffer'): Buffer;
  118. export function fromBase58checkSync(string: string, format?: OutputFormat): Uint8ArrayBuffer | Buffer;