-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
86 lines (60 loc) · 2.25 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
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
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const http_port = process.env.HTTP_PORT || 8080;
const walletConnectedVersion = process.env.WALLET_CONNECTED || false;
const validator = require('validator');
const BlockChain = require('./server/blockchain');
const blockchain = new BlockChain();
const app = express();
app.use(bodyParser.json());
app.all('*', (req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
});
if (walletConnectedVersion) {
app.use(express.static(path.join(__dirname, './wallet/dist')));
} else {
app.use(express.static(path.join(__dirname, '../client')));
}
app.get('/hosts', (req, res) => {
res.send(['']);
});
app.get('/blocks', (req, res) => res.send(JSON.stringify(blockchain.chain)));
app.get('/transactions', async(req, res) => {
const txs = await blockchain.getTransactions();
res.send(JSON.stringify(txs));
});
app.post('/mine', async(req, res) => {
if (!req.body.wallet) {
res.sendStatus(400);
}
const minedBlock = await blockchain.mineBlock(req.body.wallet);
res.send(JSON.stringify(minedBlock));
});
app.get('/balance/:address', async(req, res) => {
const senderAddress = req.params.address;
if (!validator.isHash(senderAddress, 'sha256')) {
console.log(senderAddress)
res.sendStatus(400);
}
const balance = await blockchain.getBalance(senderAddress);
res.send(JSON.stringify(balance));
});
app.post('/transfer', async(req, res) => {
if (!validator.isHash(req.body.from, 'sha256') && !validator.isHash(req.body.to, 'sha256')) {
res.sendStatus(400);
}
if (req.body.to === req.body.from) {
res.status(400).send('Sender and Address can\'t have the same hash!');
}
const balance = await blockchain.getBalance(req.body.from);
if (balance < req.body.amount) {
res.status(406).send('Out of balance!');
}
const transaction = await blockchain.sendCoins(req.body);
res.send(JSON.stringify(transaction));
});
app.listen(http_port, () => console.log('Listening http on port: ' + http_port));