-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
59 lines (43 loc) · 1.54 KB
/
index.js
File metadata and controls
59 lines (43 loc) · 1.54 KB
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
const express = require('express');
const bodyParser = require('body-parser');
const Blockchain = require('./blockchain/blockchain');
const P2pServer = require('./p2p-server');
const Wallet = require('../blockchain/wallet/index');
const TransactionPool = require('../blockchain/wallet/transaction-pool');
const Miner = require('../blockchain/miner');
const HTTP_PORT = process.env.HTTP_PORT || 3001;
const app = express();
const bc = new Blockchain();
const wallet = new Wallet();
const tp = new TransactionPool();
const p2pserver = new P2pServer(bc, tp);
const miner = new Miner(bc, tp, wallet, p2pserver);
app.use(bodyParser.json());
app.get('/blocks', (req, res) => {
res.json(bc.chain);
});
app.post('/mine', (req,res) => {
const block = bc.addBlock(req.body.data);
console.log(`New block was added: ${block.toString()}`);
p2pserver.syncChains();
res.redirect('/blocks');
})
app.get('/transactions', (req, res) => {
res.json(tp.transactions);
});
app.post('/transactions', (req,res) => {
const { recipient, amount} = req.body;
const transaction = wallet.createTransaction(recipient, amount, bc, tp);
p2pserver.broadcastTransaction(transaction);
res.redirect('/transactions');
});
app.get('/public-key', (req, res) => {
res.json( {publicKey: wallet.publicKey });
});
app.get('/mine-transactions', (req, res) => {
const block = miner.mine();
console.log(`New block added: ${block.toString()}`);
res.redirect('/blocks');
})
app.listen(HTTP_PORT, () => console.log(`Listening on port ${HTTP_PORT}`));
p2pserver.listen();