index.d.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*! scure-base - MIT License (c) 2022 Paul Miller (paulmillr.com) */
  2. /**
  3. * @__NO_SIDE_EFFECTS__
  4. */
  5. export declare function assertNumber(n: number): void;
  6. export interface Coder<F, T> {
  7. encode(from: F): T;
  8. decode(to: T): F;
  9. }
  10. export interface BytesCoder extends Coder<Uint8Array, string> {
  11. encode: (data: Uint8Array) => string;
  12. decode: (str: string) => Uint8Array;
  13. }
  14. type Chain = [Coder<any, any>, ...Coder<any, any>[]];
  15. type Input<F> = F extends Coder<infer T, any> ? T : never;
  16. type Output<F> = F extends Coder<any, infer T> ? T : never;
  17. type First<T> = T extends [infer U, ...any[]] ? U : never;
  18. type Last<T> = T extends [...any[], infer U] ? U : never;
  19. type Tail<T> = T extends [any, ...infer U] ? U : never;
  20. type AsChain<C extends Chain, Rest = Tail<C>> = {
  21. [K in keyof C]: Coder<Input<C[K]>, Input<K extends keyof Rest ? Rest[K] : any>>;
  22. };
  23. /**
  24. * @__NO_SIDE_EFFECTS__
  25. */
  26. declare function chain<T extends Chain & AsChain<T>>(...args: T): Coder<Input<First<T>>, Output<Last<T>>>;
  27. type Alphabet = string[] | string;
  28. /**
  29. * Encodes integer radix representation to array of strings using alphabet and back
  30. * @__NO_SIDE_EFFECTS__
  31. */
  32. declare function alphabet(alphabet: Alphabet): Coder<number[], string[]>;
  33. /**
  34. * @__NO_SIDE_EFFECTS__
  35. */
  36. declare function join(separator?: string): Coder<string[], string>;
  37. /**
  38. * Pad strings array so it has integer number of bits
  39. * @__NO_SIDE_EFFECTS__
  40. */
  41. declare function padding(bits: number, chr?: string): Coder<string[], string[]>;
  42. /**
  43. * Slow: O(n^2) time complexity
  44. * @__NO_SIDE_EFFECTS__
  45. */
  46. declare function convertRadix(data: number[], from: number, to: number): number[];
  47. /**
  48. * Implemented with numbers, because BigInt is 5x slower
  49. * @__NO_SIDE_EFFECTS__
  50. */
  51. declare function convertRadix2(data: number[], from: number, to: number, padding: boolean): number[];
  52. /**
  53. * @__NO_SIDE_EFFECTS__
  54. */
  55. declare function radix(num: number): Coder<Uint8Array, number[]>;
  56. /**
  57. * If both bases are power of same number (like `2**8 <-> 2**64`),
  58. * there is a linear algorithm. For now we have implementation for power-of-two bases only.
  59. * @__NO_SIDE_EFFECTS__
  60. */
  61. declare function radix2(bits: number, revPadding?: boolean): Coder<Uint8Array, number[]>;
  62. /**
  63. * @__NO_SIDE_EFFECTS__
  64. */
  65. declare function checksum(len: number, fn: (data: Uint8Array) => Uint8Array): Coder<Uint8Array, Uint8Array>;
  66. export declare const utils: {
  67. alphabet: typeof alphabet;
  68. chain: typeof chain;
  69. checksum: typeof checksum;
  70. convertRadix: typeof convertRadix;
  71. convertRadix2: typeof convertRadix2;
  72. radix: typeof radix;
  73. radix2: typeof radix2;
  74. join: typeof join;
  75. padding: typeof padding;
  76. };
  77. export declare const base16: BytesCoder;
  78. export declare const base32: BytesCoder;
  79. export declare const base32nopad: BytesCoder;
  80. export declare const base32hex: BytesCoder;
  81. export declare const base32hexnopad: BytesCoder;
  82. export declare const base32crockford: BytesCoder;
  83. export declare const base64: BytesCoder;
  84. export declare const base64nopad: BytesCoder;
  85. export declare const base64url: BytesCoder;
  86. export declare const base64urlnopad: BytesCoder;
  87. export declare const base58: BytesCoder;
  88. export declare const base58flickr: BytesCoder;
  89. export declare const base58xrp: BytesCoder;
  90. export declare const base58xmr: BytesCoder;
  91. export declare const createBase58check: (sha256: (data: Uint8Array) => Uint8Array) => BytesCoder;
  92. export declare const base58check: (sha256: (data: Uint8Array) => Uint8Array) => BytesCoder;
  93. export interface Bech32Decoded<Prefix extends string = string> {
  94. prefix: Prefix;
  95. words: number[];
  96. }
  97. export interface Bech32DecodedWithArray<Prefix extends string = string> {
  98. prefix: Prefix;
  99. words: number[];
  100. bytes: Uint8Array;
  101. }
  102. export interface Bech32 {
  103. encode<Prefix extends string>(prefix: Prefix, words: number[] | Uint8Array, limit?: number | false): `${Lowercase<Prefix>}1${string}`;
  104. decode<Prefix extends string>(str: `${Prefix}1${string}`, limit?: number | false): Bech32Decoded<Prefix>;
  105. encodeFromBytes(prefix: string, bytes: Uint8Array): string;
  106. decodeToBytes(str: string): Bech32DecodedWithArray;
  107. decodeUnsafe(str: string, limit?: number | false): void | Bech32Decoded<string>;
  108. fromWords(to: number[]): Uint8Array;
  109. fromWordsUnsafe(to: number[]): void | Uint8Array;
  110. toWords(from: Uint8Array): number[];
  111. }
  112. export declare const bech32: Bech32;
  113. export declare const bech32m: Bech32;
  114. export declare const utf8: BytesCoder;
  115. export declare const hex: BytesCoder;
  116. declare const CODERS: {
  117. utf8: BytesCoder;
  118. hex: BytesCoder;
  119. base16: BytesCoder;
  120. base32: BytesCoder;
  121. base64: BytesCoder;
  122. base64url: BytesCoder;
  123. base58: BytesCoder;
  124. base58xmr: BytesCoder;
  125. };
  126. type CoderType = keyof typeof CODERS;
  127. export declare const bytesToString: (type: CoderType, bytes: Uint8Array) => string;
  128. export declare const str: (type: CoderType, bytes: Uint8Array) => string;
  129. export declare const stringToBytes: (type: CoderType, str: string) => Uint8Array;
  130. export declare const bytes: (type: CoderType, str: string) => Uint8Array;
  131. export {};
  132. //# sourceMappingURL=index.d.ts.map