This repository has been archived by the owner on Jun 17, 2021. It is now read-only.
/
bytes.ts
158 lines (146 loc) · 4.28 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
const ethjsUtil = require('ethjs-util')
import BN = require('bn.js')
/**
* 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)
}
/**
* Left Pads an `Array` or `Buffer` with leading zeros till it has `length` bytes.
* Or it truncates the beginning if it exceeds.
* @param msg the value to pad (Buffer|Array)
* @param length the number of bytes the output should be
* @param right whether to start padding form the left or right
* @return (Buffer|Array)
*/
export const setLengthLeft = function(msg: any, length: number, right: boolean = false) {
const buf = zeros(length)
msg = toBuffer(msg)
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)
}
}
export const setLength = setLengthLeft
/**
* Right Pads an `Array` or `Buffer` with leading zeros till it has `length` bytes.
* Or it truncates the beginning if it exceeds.
* @param msg the value to pad (Buffer|Array)
* @param length the number of bytes the output should be
* @return (Buffer|Array)
*/
export const setLengthRight = function(msg: any, length: number) {
return setLength(msg, length, true)
}
/**
* Trims leading zeros from a `Buffer` or an `Array`.
* @param a (Buffer|Array|String)
* @return (Buffer|Array|String)
*/
export const unpad = function(a: any) {
a = ethjsUtil.stripHexPrefix(a)
let first = a[0]
while (a.length > 0 && first.toString() === '0') {
a = a.slice(1)
first = a[0]
}
return a
}
export const stripZeros = unpad
/**
* Attempts to turn a value into a `Buffer`. As input it supports `Buffer`, `String`, `Number`, null/undefined, `BN` and other objects with a `toArray()` method.
* @param v the value
*/
export const toBuffer = function(v: any): Buffer {
if (!Buffer.isBuffer(v)) {
if (Array.isArray(v)) {
v = Buffer.from(v)
} else if (typeof v === 'string') {
if (ethjsUtil.isHexString(v)) {
v = Buffer.from(ethjsUtil.padToEven(ethjsUtil.stripHexPrefix(v)), 'hex')
} else {
throw new Error(
`Cannot convert string to buffer. toBuffer only supports 0x-prefixed hex strings and this string was given: ${v}`,
)
}
} else if (typeof v === 'number') {
v = ethjsUtil.intToBuffer(v)
} else if (v === null || v === undefined) {
v = Buffer.allocUnsafe(0)
} else if (BN.isBN(v)) {
v = v.toArrayLike(Buffer)
} else if (v.toArray) {
// converts a BN to a Buffer
v = Buffer.from(v.toArray())
} else {
throw new Error('invalid type')
}
}
return v
}
/**
* 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 {
return new BN(toBuffer(buf)).toNumber()
}
/**
* Converts a `Buffer` into a hex `String`.
* @param buf `Buffer` object to convert
*/
export const bufferToHex = function(buf: Buffer): string {
buf = toBuffer(buf)
return '0x' + buf.toString('hex')
}
/**
* Interprets a `Buffer` as a signed integer and returns a `BN`. Assumes 256-bit numbers.
* @param num Signed integer value
*/
export const fromSigned = function(num: Buffer): BN {
return new BN(num).fromTwos(256)
}
/**
* Converts a `BN` to an unsigned integer and returns it as a `Buffer`. Assumes 256-bit numbers.
* @param num
*/
export const toUnsigned = function(num: BN): Buffer {
return Buffer.from(num.toTwos(256).toArray())
}
/**
* 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 ethjsUtil.isHexPrefixed(str) ? str : '0x' + str
}
/**
* 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
}
}