-
Notifications
You must be signed in to change notification settings - Fork 0
/
responseFixes.js
59 lines (53 loc) · 1.42 KB
/
responseFixes.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
function isArray(what) {
return Object.prototype.toString.call(what) === '[object Array]';
}
function isString(x) {
return Object.prototype.toString.call(x) === "[object String]"
}
function prefixCheck(obj) {
if(!obj || obj.length < 4) {
return false
}
if(obj.indexOf("0x0") == -1) {
return false
}
return true
}
const requiredKeys = ['v', 'r', 's', "input", "value"]
const shortEncodingKeys = ["nonce", "gasPrice", "gasLimit", "value", "v", "r", "s"]
module.exports = {
apply: function(json) {
if(isArray(json)) {
for(const rpc of json) {
if(rpc.result) {
this.fixResult(rpc.result)
}
}
}else {
if(json.result) {
this.fixResult(json.result)
}
}
return json
},
fixResult: function(res) {
if(!isString(res)) {
this.leadingZeroes(res)
}
},
leadingZeroes: function(json) {
if(!json.transactions) {
return
}
json.transactions.forEach(t => {
for(const key in t) {
if(requiredKeys.includes(key) && t[key] == null) {
t[key] = '0x00'
}
if(shortEncodingKeys.includes(key) && prefixCheck(t[key])) {
t[key] = t[key].replace("0x0", "0x")
}
}
})
},
}