forked from edgraaff/quorum-rpc-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (39 loc) · 1.34 KB
/
index.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
const Koa = require('koa');
const body = require('koa-json-body');
const fetch = require('node-fetch');
const BN = require('bn.js');
const config = require('./config');
for (const proxy of config) {
console.log(`starting proxy to ${proxy.rpcUrl} on port ${proxy.port}`);
const app = new Koa();
app.use(body({
limit: proxy.limit,
fallback: true
}));
app.use(async ctx => {
if (ctx.request.body) {
console.log('request', ctx.request.body);
const response = await fetch(proxy.rpcUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(ctx.request.body)
}).then(res => res.json());
console.log('response', response);
if (ctx.request.body.method === 'eth_getBlockByNumber' || ctx.request.body.method === 'eth_getBlockByHash') {
if (response && response.result && response.result.timestamp) {
const timestamp = new BN(response.result.timestamp.substring(2), 16);
// must be a timestamp in nanos if this is the case ...
if (timestamp.bitLength() > 53) {
// ... so convert it to seconds.
response.result.timestamp =
'0x' + timestamp.div(new BN(1000000000)).toString(16);
}
}
}
ctx.body = response;
}
});
app.listen(proxy.port);
}