utils.d.ts 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. export type Hex = Uint8Array | string;
  2. export type PrivKey = Hex | bigint;
  3. export type CHash = {
  4. (message: Uint8Array | string): Uint8Array;
  5. blockLen: number;
  6. outputLen: number;
  7. create(opts?: {
  8. dkLen?: number;
  9. }): any;
  10. };
  11. export type FHash = (message: Uint8Array | string) => Uint8Array;
  12. export declare function isBytes(a: unknown): a is Uint8Array;
  13. export declare function abytes(item: unknown): void;
  14. /**
  15. * @example bytesToHex(Uint8Array.from([0xca, 0xfe, 0x01, 0x23])) // 'cafe0123'
  16. */
  17. export declare function bytesToHex(bytes: Uint8Array): string;
  18. export declare function numberToHexUnpadded(num: number | bigint): string;
  19. export declare function hexToNumber(hex: string): bigint;
  20. /**
  21. * @example hexToBytes('cafe0123') // Uint8Array.from([0xca, 0xfe, 0x01, 0x23])
  22. */
  23. export declare function hexToBytes(hex: string): Uint8Array;
  24. export declare function bytesToNumberBE(bytes: Uint8Array): bigint;
  25. export declare function bytesToNumberLE(bytes: Uint8Array): bigint;
  26. export declare function numberToBytesBE(n: number | bigint, len: number): Uint8Array;
  27. export declare function numberToBytesLE(n: number | bigint, len: number): Uint8Array;
  28. export declare function numberToVarBytesBE(n: number | bigint): Uint8Array;
  29. /**
  30. * Takes hex string or Uint8Array, converts to Uint8Array.
  31. * Validates output length.
  32. * Will throw error for other types.
  33. * @param title descriptive title for an error e.g. 'private key'
  34. * @param hex hex string or Uint8Array
  35. * @param expectedLength optional, will compare to result array's length
  36. * @returns
  37. */
  38. export declare function ensureBytes(title: string, hex: Hex, expectedLength?: number): Uint8Array;
  39. /**
  40. * Copies several Uint8Arrays into one.
  41. */
  42. export declare function concatBytes(...arrays: Uint8Array[]): Uint8Array;
  43. export declare function equalBytes(a: Uint8Array, b: Uint8Array): boolean;
  44. /**
  45. * @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99])
  46. */
  47. export declare function utf8ToBytes(str: string): Uint8Array;
  48. /**
  49. * Calculates amount of bits in a bigint.
  50. * Same as `n.toString(2).length`
  51. */
  52. export declare function bitLen(n: bigint): number;
  53. /**
  54. * Gets single bit at position.
  55. * NOTE: first bit position is 0 (same as arrays)
  56. * Same as `!!+Array.from(n.toString(2)).reverse()[pos]`
  57. */
  58. export declare function bitGet(n: bigint, pos: number): bigint;
  59. /**
  60. * Sets single bit at position.
  61. */
  62. export declare function bitSet(n: bigint, pos: number, value: boolean): bigint;
  63. /**
  64. * Calculate mask for N bits. Not using ** operator with bigints because of old engines.
  65. * Same as BigInt(`0b${Array(i).fill('1').join('')}`)
  66. */
  67. export declare const bitMask: (n: number) => bigint;
  68. type Pred<T> = (v: Uint8Array) => T | undefined;
  69. /**
  70. * Minimal HMAC-DRBG from NIST 800-90 for RFC6979 sigs.
  71. * @returns function that will call DRBG until 2nd arg returns something meaningful
  72. * @example
  73. * const drbg = createHmacDRBG<Key>(32, 32, hmac);
  74. * drbg(seed, bytesToKey); // bytesToKey must return Key or undefined
  75. */
  76. export declare function createHmacDrbg<T>(hashLen: number, qByteLen: number, hmacFn: (key: Uint8Array, ...messages: Uint8Array[]) => Uint8Array): (seed: Uint8Array, predicate: Pred<T>) => T;
  77. declare const validatorFns: {
  78. readonly bigint: (val: any) => val is bigint;
  79. readonly function: (val: any) => boolean;
  80. readonly boolean: (val: any) => val is boolean;
  81. readonly string: (val: any) => val is string;
  82. readonly stringOrUint8Array: (val: any) => val is string | Uint8Array;
  83. readonly isSafeInteger: (val: any) => boolean;
  84. readonly array: (val: any) => val is any[];
  85. readonly field: (val: any, object: any) => any;
  86. readonly hash: (val: any) => boolean;
  87. };
  88. type Validator = keyof typeof validatorFns;
  89. type ValMap<T extends Record<string, any>> = {
  90. [K in keyof T]?: Validator;
  91. };
  92. export declare function validateObject<T extends Record<string, any>>(object: T, validators: ValMap<T>, optValidators?: ValMap<T>): T;
  93. export {};
  94. //# sourceMappingURL=utils.d.ts.map