-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserverMockDataEndpoints.js
143 lines (111 loc) · 4.46 KB
/
serverMockDataEndpoints.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const express = require('express');
const app = express();
const port = process.env.PORT || 5000;
const {spawn, exec} = require('child_process');
const fs = require('fs');
const EC = require('elliptic').ec;
const keccak256 = require('js-sha3').keccak256;
function getAddress () {
console.log('here is the key');
const ec = new EC('secp256k1');
const compKey = fs.readFileSync('./data-simulator/pubKey_compressed', {encoding : 'utf8'});
// Decode public key
const key = ec.keyFromPublic(compKey, 'hex');
// Convert to uncompressed format
const publicKey = key.getPublic().encode('hex').slice(2);
// Now apply keccak
const address = keccak256(Buffer.from(publicKey, 'hex')).slice(64 - 40);
console.log(`Public Key: 0x${publicKey}`);
console.log(`Address: 0x${address.toString()}`);
return `0x${address.toString()}`;
}
// This displays message that the server running and listening to specified port
app.listen(port, () => console.log(`Listening on port ${port}`));
// create a GET route
app.get('/express_backend', (req, res) => {
/*const child = exec('bash ./pebble-simulator/simulator.sh');
child.stdout.on('data', (data) => {
console.log(`child stdout:\n${data}`);
});
child.stderr.on('data', (data) => {
console.error(`child stderr:\n${data}`);
}); */
res.send({ express: 'Welcome to Starknet IoT DAO!'});
});
app.get('/get_address', (req, res) => {
const addressEth = getAddress();
if (req.query.readData==1){
console.log('here in read data');
var dataLog = fs.readFileSync('./pebble.dat').toString().split("\n");
const stringforAddr = dataLog[0];
var keyFile = fs.readFileSync('./data-simulator/privKey').toString().split("\n");
const keyToSave = keyFile[0].trim();
res.send({currentAddr : addressEth, firstLine : stringforAddr, privKey : keyToSave});
}
else{
res.send({currentAddr : addressEth});
}
});
app.get('/run_simulator', (req, res) => {
var data = fs.readFileSync('./data-simulator/simulator.sh').toString().split("\n");
data[6] = `TargetMax=${req.query.TargetMax}`;
data[7] = `TargetMin=${req.query.TargetMin}`;
data[68] = `CountPkg=${req.query.runs}`;
var text = data.join("\n");
fs.writeFile('./data-simulator/simulator.sh', text, function (err) {
if (err) return console.log(err);
});
const child = exec('bash ./data-simulator/simulator.sh');
child.stdout.on('data', (data) => {
console.log(`child stdout:\n${data}`);
});
child.stderr.on('data', (data) => {
console.error(`child stderr:\n${data}`);
});
res.send({simComplete : true});
})
app.get('/run_simulator_verify', (req, res) => {
fs.writeFile('./data-simulator/privKeyVerify', req.query.PrivKey, function (err) {
if (err) return console.log(err);
});
var data = fs.readFileSync('./data-simulator/simulatorVerify.sh').toString().split("\n");
data[6] = `TargetMax=${req.query.TargetMax}`;
data[7] = `TargetMin=${req.query.TargetMin}`;
data[61] = `START=${req.query.Start}`;
data[62] = `DELTA=${req.query.Delta}`;
var text = data.join("\n");
fs.writeFile('./data-simulator/simulatorVerify.sh', text, function (err) {
if (err) return console.log(err);
});
const child = exec('bash ./data-simulator/simulatorVerify.sh');
child.stdout.on('data', (data) => {
console.log(`child stdout:\n${data}`);
});
child.stderr.on('data', (data) => {
console.error(`child stderr:\n${data}`);
});
res.send({simComplete : true});
})
app.get('/deal_verify_final', (req, res) => {
var dataLog = fs.readFileSync('./pebble.dat').toString().split("\n");
res.send({dataVerify : dataLog});
})
app.get('/python_test', (req,res)=> {
var dataToSend;
// spawn new child process to call the python script
const python = spawn('python3', ['script1.py', `node.js`, `python`]);
// collect data from script
python.stdout.on('data', function (data) {
console.log('Pipe data from python script ...');
dataToSend = data.toString();
});
python.stderr.on('data', function(data) {
console.log(`Pipe error from python script ...${data}`);
})
// in close event we are sure that stream from child process is closed
python.on('close', (code) => {
console.log(`child process close all stdio with code ${code}`);
// send data to browser
res.send(dataToSend)
})
})