forked from MetaMask/web3-provider-engine
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsanitizer.js
78 lines (67 loc) · 1.59 KB
/
sanitizer.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
71
72
73
74
75
76
77
78
/* Sanitization Subprovider
* For Parity compatibility
* removes irregular keys
*/
const inherits = require('util').inherits
const Subprovider = require('./subprovider.js')
const extend = require('xtend')
const ethUtil = require('ethereumjs-util')
module.exports = SanitizerSubprovider
inherits(SanitizerSubprovider, Subprovider)
function SanitizerSubprovider(opts){
const self = this
}
SanitizerSubprovider.prototype.handleRequest = function(payload, next, end){
var txParams = payload.params[0]
if (typeof txParams === 'object' && !Array.isArray(txParams)) {
var sanitized = cloneTxParams(txParams)
payload.params[0] = sanitized
}
next()
}
// we use this to clean any custom params from the txParams
var permitted = [
'from',
'to',
'value',
'data',
'gas',
'gasPrice',
'nonce',
'fromBlock',
'toBlock',
'address',
'topics',
]
function cloneTxParams(txParams){
var sanitized = permitted.reduce(function(copy, permitted) {
if (permitted in txParams) {
if (Array.isArray(txParams[permitted])) {
copy[permitted] = txParams[permitted]
.map(function(item) {
return sanitize(item)
})
} else {
copy[permitted] = sanitize(txParams[permitted])
}
}
return copy
}, {})
return sanitized
}
function sanitize(value) {
switch (value) {
case 'latest':
return value
case 'pending':
return value
case 'earliest':
return value
default:
if (typeof value === 'string') {
return ethUtil.addHexPrefix(value.toLowerCase())
} else {
return value
}
}
}