UNPKG

5.21 kBTypeScriptView Raw
1/// <reference types="node" />
2/// <reference types="bn.js" />
3import { BN } from './externals';
4import { PrefixedHexString, TransformableToArray, TransformableToBuffer, NestedBufferArray, NestedUint8Array } from './types';
5/**
6 * Converts a `Number` into a hex `String`
7 * @param {Number} i
8 * @return {String}
9 */
10export declare const intToHex: (i: number) => string;
11/**
12 * Converts an `Number` to a `Buffer`
13 * @param {Number} i
14 * @return {Buffer}
15 */
16export declare const intToBuffer: (i: number) => Buffer;
17/**
18 * Returns a buffer filled with 0s.
19 * @param bytes the number of bytes the buffer should be
20 */
21export declare const zeros: (bytes: number) => Buffer;
22/**
23 * Left Pads a `Buffer` with leading zeros till it has `length` bytes.
24 * Or it truncates the beginning if it exceeds.
25 * @param msg the value to pad (Buffer)
26 * @param length the number of bytes the output should be
27 * @return (Buffer)
28 */
29export declare const setLengthLeft: (msg: Buffer, length: number) => Buffer;
30/**
31 * Right Pads a `Buffer` with trailing zeros till it has `length` bytes.
32 * it truncates the end if it exceeds.
33 * @param msg the value to pad (Buffer)
34 * @param length the number of bytes the output should be
35 * @return (Buffer)
36 */
37export declare const setLengthRight: (msg: Buffer, length: number) => Buffer;
38/**
39 * Trims leading zeros from a `Buffer`.
40 * @param a (Buffer)
41 * @return (Buffer)
42 */
43export declare const unpadBuffer: (a: Buffer) => Buffer;
44/**
45 * Trims leading zeros from an `Array` (of numbers).
46 * @param a (number[])
47 * @return (number[])
48 */
49export declare const unpadArray: (a: number[]) => number[];
50/**
51 * Trims leading zeros from a hex-prefixed `String`.
52 * @param a (String)
53 * @return (String)
54 */
55export declare const unpadHexString: (a: string) => string;
56export declare type ToBufferInputTypes = PrefixedHexString | number | BN | Buffer | Uint8Array | number[] | TransformableToArray | TransformableToBuffer | null | undefined;
57/**
58 * Attempts to turn a value into a `Buffer`.
59 * Inputs supported: `Buffer`, `String` (hex-prefixed), `Number`, null/undefined, `BN` and other objects
60 * with a `toArray()` or `toBuffer()` method.
61 * @param v the value
62 */
63export declare const toBuffer: (v: ToBufferInputTypes) => Buffer;
64/**
65 * Converts a `Buffer` to a `Number`.
66 * @param buf `Buffer` object to convert
67 * @throws If the input number exceeds 53 bits.
68 */
69export declare const bufferToInt: (buf: Buffer) => number;
70/**
71 * Converts a `Buffer` into a `0x`-prefixed hex `String`.
72 * @param buf `Buffer` object to convert
73 */
74export declare const bufferToHex: (buf: Buffer) => string;
75/**
76 * Interprets a `Buffer` as a signed integer and returns a `BN`. Assumes 256-bit numbers.
77 * @param num Signed integer value
78 */
79export declare const fromSigned: (num: Buffer) => BN;
80/**
81 * Converts a `BN` to an unsigned integer and returns it as a `Buffer`. Assumes 256-bit numbers.
82 * @param num
83 */
84export declare const toUnsigned: (num: BN) => Buffer;
85/**
86 * Adds "0x" to a given `String` if it does not already start with "0x".
87 */
88export declare const addHexPrefix: (str: string) => string;
89/**
90 * Returns the utf8 string representation from a hex string.
91 *
92 * Examples:
93 *
94 * Input 1: '657468657265756d000000000000000000000000000000000000000000000000'
95 * Input 2: '657468657265756d'
96 * Input 3: '000000000000000000000000000000000000000000000000657468657265756d'
97 *
98 * Output (all 3 input variants): 'ethereum'
99 *
100 * Note that this method is not intended to be used with hex strings
101 * representing quantities in both big endian or little endian notation.
102 *
103 * @param string Hex string, should be `0x` prefixed
104 * @return Utf8 string
105 */
106export declare const toUtf8: (hex: string) => string;
107/**
108 * Converts a `Buffer` or `Array` to JSON.
109 * @param ba (Buffer|Array)
110 * @return (Array|String|null)
111 */
112export declare const baToJSON: (ba: any) => any;
113/**
114 * Checks provided Buffers for leading zeroes and throws if found.
115 *
116 * Examples:
117 *
118 * Valid values: 0x1, 0x, 0x01, 0x1234
119 * Invalid values: 0x0, 0x00, 0x001, 0x0001
120 *
121 * Note: This method is useful for validating that RLP encoded integers comply with the rule that all
122 * integer values encoded to RLP must be in the most compact form and contain no leading zero bytes
123 * @param values An object containing string keys and Buffer values
124 * @throws if any provided value is found to have leading zero bytes
125 */
126export declare const validateNoLeadingZeroes: (values: {
127 [key: string]: Buffer | undefined;
128}) => void;
129/**
130 * Converts a {@link Uint8Array} or {@link NestedUint8Array} to {@link Buffer} or {@link NestedBufferArray}
131 */
132export declare function arrToBufArr(arr: Uint8Array): Buffer;
133export declare function arrToBufArr(arr: NestedUint8Array): NestedBufferArray;
134export declare function arrToBufArr(arr: Uint8Array | NestedUint8Array): Buffer | NestedBufferArray;
135/**
136 * Converts a {@link Buffer} or {@link NestedBufferArray} to {@link Uint8Array} or {@link NestedUint8Array}
137 */
138export declare function bufArrToArr(arr: Buffer): Uint8Array;
139export declare function bufArrToArr(arr: NestedBufferArray): NestedUint8Array;
140export declare function bufArrToArr(arr: Buffer | NestedBufferArray): Uint8Array | NestedUint8Array;