-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
76 lines (70 loc) · 2.29 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
var net = require('net');
var client = new net.Socket();
var init = false;
client.connect(17785, '127.0.0.1', function () {
console.log('Connected to rpc');
client.write('init');
});
var express = require("express");
var app = express();
var cors = require('cors')
app.use(cors())
var tot_json = `[`
app.listen(1234, () => {
console.log("API running on port 1234");
});
app.get("/dag", (req, res, next) => {
res.json(tot_json + "]");
});
client.on('data', function (data) {
console.log('Received: ' + data);
if (!init) {
console.log("Not init, sending *");
client.write('*');
init = true;
} else {
var rec = JSON.parse(data)
if (rec.m_type == "block") {
var block = JSON.parse(rec.content)
console.log(block.block_type)
if (block.block_type = "Send") {
if (block.header.prev_hash == "00000000000") {
let new_json = `{"hash":"${block.hash}", "time": ${block.header.timestamp}, "chain_key": "${block.header.chain_key}", "links": [] }`
if (tot_json = '[') {
tot_json = tot_json + new_json
} else {
tot_json = tot_json + "," + new_json
}
} else {
let new_json = `{"hash":"${block.hash}", "time": ${block.header.timestamp}, "chain_key": "${block.header.chain_key}", "links": [ {"hash": "${block.header.prev_hash}", "type": 0 } ] }`
if (tot_json = '[') {
tot_json = tot_json + new_json
} else {
tot_json = tot_json + "," + new_json
}
}
} else {
if (block.header.prev_hash == "00000000000") {
let new_json = `{"hash":"${block.hash}"", "time": ${block.header.timestamp}, "chain_key": "${block.header.chain_key}", "links": [ {"hash": "${block.send_block}", "type": 1 } ] }`
if (tot_json = '[') {
tot_json = tot_json + new_json
} else {
tot_json = tot_json + "," + new_json
}
} else {
let new_json = `{"hash":"${block.hash}", "time": ${block.header.timestamp}, "chain_key": "${block.header.chain_key}", "links": [ {"hash": "${block.header.prev_hash}", "type": 0 }, {"hash": "${block.send_block}", "type": 1 } ] }`
if (tot_json = '[') {
tot_json = tot_json + new_json
} else {
tot_json = tot_json + "," + new_json
}
}
}
} else {
console.log(`Recieved non block msg, type=${rec.m_type}`)
}
}
});
client.on('close', function () {
console.log('rpc closed');
});