-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
hexUtils.js
70 lines (59 loc) · 1.59 KB
/
hexUtils.js
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
module.exports = {
minBlockRef,
maxBlockRef,
sortBlockRefs,
bnToHex,
blockRefIsNumber,
hexToInt,
incrementHexInt,
intToHex,
unsafeRandomBytes,
}
function minBlockRef(...refs) {
const sortedRefs = sortBlockRefs(refs)
return sortedRefs[0]
}
function maxBlockRef(...refs) {
const sortedRefs = sortBlockRefs(refs)
return sortedRefs[sortedRefs.length-1]
}
function sortBlockRefs(refs) {
return refs.sort((refA, refB) => {
if (refA === 'latest' || refB === 'earliest') return 1
if (refB === 'latest' || refA === 'earliest') return -1
return hexToInt(refA) - hexToInt(refB)
})
}
function bnToHex(bn) {
return '0x' + bn.toString(16)
}
function blockRefIsNumber(blockRef){
return blockRef && !['earliest', 'latest', 'pending'].includes(blockRef)
}
function hexToInt(hexString) {
if (hexString === undefined || hexString === null) return hexString
return Number.parseInt(hexString, 16)
}
function incrementHexInt(hexString){
if (hexString === undefined || hexString === null) return hexString
const value = hexToInt(hexString)
return intToHex(value + 1)
}
function intToHex(int) {
if (int === undefined || int === null) return int
let hexString = int.toString(16)
const needsLeftPad = hexString.length % 2
if (needsLeftPad) hexString = '0' + hexString
return '0x' + hexString
}
function unsafeRandomBytes(byteCount) {
let result = '0x'
for (let i = 0; i < byteCount; i++) {
result += unsafeRandomNibble()
result += unsafeRandomNibble()
}
return result
}
function unsafeRandomNibble() {
return Math.floor(Math.random() * 16).toString(16)
}