forked from ethereumjs/ethereumjs-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bytes.ts
392 lines (353 loc) · 10.5 KB
/
bytes.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
import { assertIsArray, assertIsBuffer, assertIsHexString } from './helpers'
import { isHexPrefixed, isHexString, padToEven, stripHexPrefix } from './internal'
import type {
NestedBufferArray,
NestedUint8Array,
PrefixedHexString,
TransformableToArray,
TransformableToBuffer,
} from './types'
/**
* Converts a `Number` into a hex `String`
* @param {Number} i
* @return {String}
*/
export const intToHex = function (i: number) {
if (!Number.isSafeInteger(i) || i < 0) {
throw new Error(`Received an invalid integer type: ${i}`)
}
return `0x${i.toString(16)}`
}
/**
* Converts an `Number` to a `Buffer`
* @param {Number} i
* @return {Buffer}
*/
export const intToBuffer = function (i: number) {
const hex = intToHex(i)
return Buffer.from(padToEven(hex.slice(2)), 'hex')
}
/**
* Returns a buffer filled with 0s.
* @param bytes the number of bytes the buffer should be
*/
export const zeros = function (bytes: number): Buffer {
return Buffer.allocUnsafe(bytes).fill(0)
}
/**
* Pads a `Buffer` with zeros till it has `length` bytes.
* Truncates the beginning or end of input if its length exceeds `length`.
* @param msg the value to pad (Buffer)
* @param length the number of bytes the output should be
* @param right whether to start padding form the left or right
* @return (Buffer)
*/
const setLength = function (msg: Buffer, length: number, right: boolean) {
const buf = zeros(length)
if (right) {
if (msg.length < length) {
msg.copy(buf)
return buf
}
return msg.slice(0, length)
} else {
if (msg.length < length) {
msg.copy(buf, length - msg.length)
return buf
}
return msg.slice(-length)
}
}
/**
* Left Pads a `Buffer` with leading zeros till it has `length` bytes.
* Or it truncates the beginning if it exceeds.
* @param msg the value to pad (Buffer)
* @param length the number of bytes the output should be
* @return (Buffer)
*/
export const setLengthLeft = function (msg: Buffer, length: number) {
assertIsBuffer(msg)
return setLength(msg, length, false)
}
/**
* Right Pads a `Buffer` with trailing zeros till it has `length` bytes.
* it truncates the end if it exceeds.
* @param msg the value to pad (Buffer)
* @param length the number of bytes the output should be
* @return (Buffer)
*/
export const setLengthRight = function (msg: Buffer, length: number) {
assertIsBuffer(msg)
return setLength(msg, length, true)
}
/**
* Trims leading zeros from a `Buffer`, `String` or `Number[]`.
* @param a (Buffer|Array|String)
* @return (Buffer|Array|String)
*/
const stripZeros = function (a: any): Buffer | number[] | string {
let first = a[0]
while (a.length > 0 && first.toString() === '0') {
a = a.slice(1)
first = a[0]
}
return a
}
/**
* Trims leading zeros from a `Buffer`.
* @param a (Buffer)
* @return (Buffer)
*/
export const unpadBuffer = function (a: Buffer): Buffer {
assertIsBuffer(a)
return stripZeros(a) as Buffer
}
/**
* Trims leading zeros from an `Array` (of numbers).
* @param a (number[])
* @return (number[])
*/
export const unpadArray = function (a: number[]): number[] {
assertIsArray(a)
return stripZeros(a) as number[]
}
/**
* Trims leading zeros from a hex-prefixed `String`.
* @param a (String)
* @return (String)
*/
export const unpadHexString = function (a: string): string {
assertIsHexString(a)
a = stripHexPrefix(a)
return ('0x' + stripZeros(a)) as string
}
export type ToBufferInputTypes =
| PrefixedHexString
| number
| bigint
| Buffer
| Uint8Array
| number[]
| TransformableToArray
| TransformableToBuffer
| null
| undefined
/**
* Attempts to turn a value into a `Buffer`.
* Inputs supported: `Buffer`, `String` (hex-prefixed), `Number`, null/undefined, `BigInt` and other objects
* with a `toArray()` or `toBuffer()` method.
* @param v the value
*/
export const toBuffer = function (v: ToBufferInputTypes): Buffer {
if (v === null || v === undefined) {
return Buffer.allocUnsafe(0)
}
if (Buffer.isBuffer(v)) {
return Buffer.from(v)
}
if (Array.isArray(v) || v instanceof Uint8Array) {
return Buffer.from(v as Uint8Array)
}
if (typeof v === 'string') {
if (!isHexString(v)) {
throw new Error(
`Cannot convert string to buffer. toBuffer only supports 0x-prefixed hex strings and this string was given: ${v}`
)
}
return Buffer.from(padToEven(stripHexPrefix(v)), 'hex')
}
if (typeof v === 'number') {
return intToBuffer(v)
}
if (typeof v === 'bigint') {
if (v < BigInt(0)) {
throw new Error(`Cannot convert negative bigint to buffer. Given: ${v}`)
}
let n = v.toString(16)
if (n.length % 2) n = '0' + n
return Buffer.from(n, 'hex')
}
if (v.toArray) {
// converts a BN to a Buffer
return Buffer.from(v.toArray())
}
if (v.toBuffer) {
return Buffer.from(v.toBuffer())
}
throw new Error('invalid type')
}
/**
* Converts a `Buffer` into a `0x`-prefixed hex `String`.
* @param buf `Buffer` object to convert
*/
export const bufferToHex = function (buf: Buffer): string {
buf = toBuffer(buf)
return '0x' + buf.toString('hex')
}
/**
* Converts a {@link Buffer} to a {@link bigint}
*/
export function bufferToBigInt(buf: Buffer) {
const hex = bufferToHex(buf)
if (hex === '0x') {
return BigInt(0)
}
return BigInt(hex)
}
/**
* Converts a {@link bigint} to a {@link Buffer}
*/
export function bigIntToBuffer(num: bigint) {
return toBuffer('0x' + num.toString(16))
}
/**
* Converts a `Buffer` to a `Number`.
* @param buf `Buffer` object to convert
* @throws If the input number exceeds 53 bits.
*/
export const bufferToInt = function (buf: Buffer): number {
const res = Number(bufferToBigInt(buf))
if (!Number.isSafeInteger(res)) throw new Error('Number exceeds 53 bits')
return res
}
/**
* Interprets a `Buffer` as a signed integer and returns a `BigInt`. Assumes 256-bit numbers.
* @param num Signed integer value
*/
export const fromSigned = function (num: Buffer): bigint {
return BigInt.asIntN(256, bufferToBigInt(num))
}
/**
* Converts a `BigInt` to an unsigned integer and returns it as a `Buffer`. Assumes 256-bit numbers.
* @param num
*/
export const toUnsigned = function (num: bigint): Buffer {
return bigIntToBuffer(BigInt.asUintN(256, num))
}
/**
* Adds "0x" to a given `String` if it does not already start with "0x".
*/
export const addHexPrefix = function (str: string): string {
if (typeof str !== 'string') {
return str
}
return isHexPrefixed(str) ? str : '0x' + str
}
/**
* Shortens a string or buffer's hex string representation to maxLength (default 50).
*
* Examples:
*
* Input: '657468657265756d000000000000000000000000000000000000000000000000'
* Output: '657468657265756d0000000000000000000000000000000000…'
*/
export function short(buffer: Buffer | string, maxLength: number = 50): string {
const bufferStr = Buffer.isBuffer(buffer) ? buffer.toString('hex') : buffer
if (bufferStr.length <= maxLength) {
return bufferStr
}
return bufferStr.slice(0, maxLength) + '…'
}
/**
* Returns the utf8 string representation from a hex string.
*
* Examples:
*
* Input 1: '657468657265756d000000000000000000000000000000000000000000000000'
* Input 2: '657468657265756d'
* Input 3: '000000000000000000000000000000000000000000000000657468657265756d'
*
* Output (all 3 input variants): 'ethereum'
*
* Note that this method is not intended to be used with hex strings
* representing quantities in both big endian or little endian notation.
*
* @param string Hex string, should be `0x` prefixed
* @return Utf8 string
*/
export const toUtf8 = function (hex: string): string {
const zerosRegexp = /^(00)+|(00)+$/g
hex = stripHexPrefix(hex)
if (hex.length % 2 !== 0) {
throw new Error('Invalid non-even hex string input for toUtf8() provided')
}
const bufferVal = Buffer.from(hex.replace(zerosRegexp, ''), 'hex')
return bufferVal.toString('utf8')
}
/**
* Converts a `Buffer` or `Array` to JSON.
* @param ba (Buffer|Array)
* @return (Array|String|null)
*/
export const baToJSON = function (ba: any): any {
if (Buffer.isBuffer(ba)) {
return `0x${ba.toString('hex')}`
} else if (ba instanceof Array) {
const array = []
for (let i = 0; i < ba.length; i++) {
array.push(baToJSON(ba[i]))
}
return array
}
}
/**
* Checks provided Buffers for leading zeroes and throws if found.
*
* Examples:
*
* Valid values: 0x1, 0x, 0x01, 0x1234
* Invalid values: 0x0, 0x00, 0x001, 0x0001
*
* Note: This method is useful for validating that RLP encoded integers comply with the rule that all
* integer values encoded to RLP must be in the most compact form and contain no leading zero bytes
* @param values An object containing string keys and Buffer values
* @throws if any provided value is found to have leading zero bytes
*/
export const validateNoLeadingZeroes = function (values: { [key: string]: Buffer | undefined }) {
for (const [k, v] of Object.entries(values)) {
if (v !== undefined && v.length > 0 && v[0] === 0) {
throw new Error(`${k} cannot have leading zeroes, received: ${v.toString('hex')}`)
}
}
}
/**
* Converts a {@link Uint8Array} or {@link NestedUint8Array} to {@link Buffer} or {@link NestedBufferArray}
*/
export function arrToBufArr(arr: Uint8Array): Buffer
export function arrToBufArr(arr: NestedUint8Array): NestedBufferArray
export function arrToBufArr(arr: Uint8Array | NestedUint8Array): Buffer | NestedBufferArray
export function arrToBufArr(arr: Uint8Array | NestedUint8Array): Buffer | NestedBufferArray {
if (!Array.isArray(arr)) {
return Buffer.from(arr)
}
return arr.map((a) => arrToBufArr(a))
}
/**
* Converts a {@link Buffer} or {@link NestedBufferArray} to {@link Uint8Array} or {@link NestedUint8Array}
*/
export function bufArrToArr(arr: Buffer): Uint8Array
export function bufArrToArr(arr: NestedBufferArray): NestedUint8Array
export function bufArrToArr(arr: Buffer | NestedBufferArray): Uint8Array | NestedUint8Array
export function bufArrToArr(arr: Buffer | NestedBufferArray): Uint8Array | NestedUint8Array {
if (!Array.isArray(arr)) {
return Uint8Array.from(arr ?? [])
}
return arr.map((a) => bufArrToArr(a))
}
/**
* Converts a {@link bigint} to a `0x` prefixed hex string
*/
export const bigIntToHex = (num: bigint) => {
return '0x' + num.toString(16)
}
/**
* Convert value from bigint to an unpadded Buffer
* (useful for RLP transport)
* @param value value to convert
*/
export function bigIntToUnpaddedBuffer(value: bigint): Buffer {
return unpadBuffer(bigIntToBuffer(value))
}
export function intToUnpaddedBuffer(value: number): Buffer {
return unpadBuffer(intToBuffer(value))
}