-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
57 lines (54 loc) · 1.54 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
49
50
51
52
53
54
55
56
57
const {startMining, stopMining, getReward, getDifficulty, getLength, getEffort} = require('./mine');
const {PORT} = require('./config');
const {utxos, blockchain, reward} = require('./db');
const express = require('express');
const app = express();
const cors = require('cors');
// localhost can have cross origin errors
// depending on the browser you use!
app.use(cors());
app.use(express.json());
app.post('/', (req, res) => {
const {method, params} = req.body;
if(method === 'startMining') {
startMining();
res.send({ blockNumber: blockchain.blockHeight() });
return;
}
if(method === 'stopMining') {
stopMining();
res.send({ blockNumber: blockchain.blockHeight() });
return;
}
if(method === "getBalance") {
const [address] = params;
const ourUTXOs = utxos.filter(x => {
return x.owner === address && !x.spent;
});
const sum = ourUTXOs.reduce((p,c) => p + c.amount, 0);
res.send({ balance: sum.toString()});
}
if(method === "getReward") {
let reward_res = getReward();
res.send({blockReward: reward_res});
return;
}
if(method === "getDifficulty") {
let diff_res = getDifficulty();
res.send({blockDifficulty: diff_res});
return;
}
if(method === "getLength") {
let length_res = getLength();
res.send({blockLength: length_res});
return;
}
if(method === "getEffort") {
let effort_res = getEffort();
res.send({blockEffort: effort_res});
return;
}
});
app.listen(PORT, () => {
console.log(`Listening on port ${PORT}!`);
});