forked from MetaMask/eth-rpc-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
111 lines (93 loc) · 2.63 KB
/
server.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
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
const request = require('request')
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const rpcWrapperEngine = require('./index.js')
const PORT = process.env.PORT || 9000
const RPC_NODE = process.env.RPC_NODE
if (!RPC_NODE) throw new Error('Env var RPC_NODE not specified.')
const METHOD_WHITELIST =
process.env.METHOD_WHITELIST
? process.env.METHOD_WHITELIST.split(',')
: [
'eth_gasPrice',
'eth_blockNumber',
'eth_getBalance',
'eth_getBlockByHash',
'eth_getBlockByNumber',
'eth_getBlockTransactionCountByHash',
'eth_getBlockTransactionCountByNumber',
'eth_getCode',
'eth_getStorageAt',
'eth_getTransactionByBlockHashAndIndex',
'eth_getTransactionByBlockNumberAndIndex',
'eth_getTransactionByHash',
'eth_getTransactionCount',
'eth_getTransactionReceipt',
'eth_getUncleByBlockHashAndIndex',
'eth_getUncleByBlockNumberAndIndex',
'eth_getUncleCountByBlockHash',
'eth_getUncleCountByBlockNumber',
'eth_sendRawTransaction',
'eth_getLogs',
]
//
// create engine
//
// ProviderEngine based caching layer, with fallback to geth
var engine = rpcWrapperEngine({
rpcUrl: RPC_NODE,
})
// log new blocks
engine.on('block', function(block){
console.log('BLOCK CHANGED:', '#'+block.number.toString('hex'), '0x'+block.hash.toString('hex'))
})
// start polling
engine.start()
//
// create webserver
//
const app = express()
app.use(cors())
app.use(bodyParser.text({ type: '*/*' }))
app.post('/', function(req, res){
// parse request
try {
var requestObject = JSON.parse(req.body)
} catch (err) {
return didError(new Error('JSON parse failure - '+err.message))
}
// validate request
if (!validateRequest( requestObject )) return invalidRequest()
console.log('RPC:', requestObject.method, requestObject.params)//, '->', result.result || result.error)
// process request
engine.sendAsync(requestObject, function(err, result){
if (err) {
didError(err)
} else {
res.send(result)
}
})
function didError(err){
console.error(err.stack)
res.status(500).json({ error: err.message })
}
function invalidRequest(){
res.status(400).json({ error: 'Not a valid request.' })
}
})
app.listen(PORT, function(){
console.log('ethereum rpc listening on', PORT)
console.log('and proxying to', RPC_NODE)
})
// example request format
//
// {
// "jsonrpc": "2.0",
// "method": "eth_getBalance",
// "params": ["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "latest"],
// "id":1
// }
function validateRequest( requestObject ){
return typeof requestObject === 'object' && !!requestObject.method
}