-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbeamjs_service.js
96 lines (66 loc) · 2.47 KB
/
beamjs_service.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
const express = require('express')
const es = require('event-stream');
const { spawn, spawnSync } = require('child_process')
var dirty = require('dirty');
var db = dirty('user.db');
var net = require('net');
const app = express()
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*"); // update to match the domain you will make the request from
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.use(function(req, res, next){
var data = "";
req.on('data', function(chunk){ data += chunk})
req.on('end', function(){
req.rawBody = data;
req.jsonBody = JSON.parse(data);
next();
})
})
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.post('/rpc', async (req, response) => {
//http://116.203.203.109:3000/rpc?ethereumAddress=0x123123ðSign=0x123123ðSign
// ethAddr = users Ethereum address (generated by web3)
//ethSign web3.signMessage (for authentification)
//if (web3.ecrecover(req.rawBody) != req.ethereumAddress) throw;
console.log("\r\n\r\n request from: "+req.query.ethereumAddress);
//get асоцированый бим адрес
var myBeamAddr = db.get('user_'+req.query.ethereumAddress+"_beamAddr");
if (req.jsonBody.method == "tx_send" && req.jsonBody.params.from != myBeamAddr) {
response.send("Access denied or beam address was not created");
return;
}
var client = new net.Socket();
client.connect(20000, '127.0.0.1', function() {
console.log('Connected');
client.write(req.rawBody+ '\n');
});
var acc = '';
client.on('data', function(data) {
acc += data;
// searching for \n symbol to find end of response
if(data.indexOf('\n') != -1)
{
var res = JSON.parse(acc);
if (req.jsonBody.method == "create_address") {
//https://github.com/BeamMW/beam/wiki/Beam-wallet-protocol-API#create_address
//create beam address and save information about his ethereum address
var createdBeamAddress = res.result; //тут будет новый аддрес
console.log("created beam address "+req.ethereumAddress+" : "+createdBeamAddress);
db.set('user_'+req.query.ethereumAddress, createdBeamAddress);
}
response.send(res);
client.destroy(); // kill client after server's response
}
});
client.on('close', function() {
console.log('Connection closed');
});
})
app.listen(3000, () => {
console.log('Example app listening on port 3000!')
})