forked from MetaMask/web3-provider-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
86 lines (68 loc) · 2.12 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
const request = require('request');
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const ProviderEngine = require('./index.js')
const ZeroClientProvider = require('./zero.js')
const PORT = process.env.PORT || 9000;
const engine = ZeroClientProvider({
getAccounts: function(){},
rpcUrl: 'https://ropsten.infura.io/',
});
// log new blocks
engine.on('block', function(block){
console.log('BLOCK CHANGED:', '#'+block.number.toString('hex'), '0x'+block.hash.toString('hex'))
});
const app = express();
app.use(cors());
app.use(bodyParser.text({ type: '*/*' }));
app.use(bodyParser.json());
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));
}*/
var requestObject;
if ( typeof req.body !== 'object' ){
var strBody = req.body;
if ( strBody[0] === '[') {
strBody = strBody.substr(1, strBody.length-2);
requestObject = JSON.parse(strBody);
}
else {
requestObject = JSON.parse(strBody);
}
}
else
requestObject = req.body;
console.log('RPC REQ:', requestObject);
// validate request
if (!validateRequest( requestObject ))
return invalidRequest();
// process request
engine.sendAsync( requestObject, function(err, result){
if (err) {
didError(err);
} else {
console.log('RPC RES:', result);
res.send(result);
}
});
function didError(err){
console.error(err.stack);
res.status(500).json({ error: err.message });
}
function invalidRequest(){
console.error('BAD REQUEST');
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);
});
function validateRequest( requestObject ){
return typeof requestObject === 'object' && !!requestObject.method;
}