-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApi.js
52 lines (43 loc) · 1.71 KB
/
Api.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 bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}));
const {v4 : uuidv4} =require('uuid');
const nodeAddress = uuidv4().split('-').join();
const port = process.argv[2];
const Blockchain = require('./blockchain');
const bitcoin = new Blockchain();
app.get('/createNewBlock', function (req, res) {
res.send(bitcoin);
});
app.post('/createNewTransaction', function (req, res) {
const blockIndex = bitcoin.createNewTransactions(req.body.amount, req.body.sender, req.body.receiver);
res.json({ note: `The new transaction has been added to block ${blockIndex}` });
});
app.get('/mine' , function(req,res){
const lastblock = bitcoin.getLastBlock();
const previousBlockHash = lastblock['hash'];
const currentBlockData = {
transactions: bitcoin.pendingTransactions,
index: lastblock.index+1
}
const nonce = bitcoin.proofOfWork(previousBlockHash,currentBlockData);
const blockHash = bitcoin.createHashBlock(previousBlockHash,currentBlockData,nonce);
bitcoin.createNewTransactions(100,"000000",nodeAddress);
const newBlock = bitcoin.createNewBlock(nonce,previousBlockHash,blockHash);
res.json({
note: "The new Block has been Mined Successfully!",
block: newBlock
});
});
app.get('/wallet', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
app.post('/wallet', function (req, res) {
const blockIndex = bitcoin.createNewTransactions(req.body.amount, req.body.sender, req.body.receiver);
res.json({ note: `The new transaction has been added to block ${blockIndex}` });
});
app.listen(port,function(){
console.log(`Server is Running in ${port}`);
});