-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
52 lines (40 loc) · 1.61 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
const express = require('express')
const app = express()
const path = require('path')
const port = process.env.PORT || 8080;
// Blockchain Initialization
const Blockchain = require('./blockchain.js')
let blockchain = new Blockchain()
function createBlock(req) {
const previousBlock = blockchain.getPreviousBlock(req.params.id);
const previousHash = blockchain.hash(previousBlock);
const previousProof = previousBlock['proof'] // why is this not working?
const proof = blockchain.proofOfWork(previousProof);
console.log(proof)
blockchain.addTransaction('NA', 'THE BLOCKCHAIN', blockchain.getRandomArbitrary(0, 10))
const block = blockchain.createBlock(proof, previousHash)
const response = {
message: 'Block mined',
index: block['index'],
timestamp: block['timestamp'],
proof: block['proof'],
previousHash: block['previousHash'],
transactions: block['transactions']
}
return response
}
app.get('/api/blockchain', (req, res) => {
res.set("Content-Type", "text/plain");
res.send(JSON.stringify(blockchain.getChain(), null, 4));
});
app.get('/api/block/:id', (req, res) => {
if (req.params.id == undefined) return res.status(400).send("No Id Provided!");
if (blockchain[req.params.id] == undefined) return res.status(404).send("Block not found!");
res.json(blockchain[req.params.id]);
})
app.get('/api/mine_block', (req, res) => {
res.set("Content-Type", "text/plain");
const block = createBlock(req);
return res.send(JSON.stringify(block, null, 4));
})
app.listen(port, () => { console.log('Listening on port ' + port) })