-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.js
89 lines (75 loc) · 2.67 KB
/
node.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
87
88
89
const express = require('express')
const cors = require('cors')
const SHA256 = require('crypto-js/sha256')
const prompt = require('prompt-sync')()
const axios = require('axios')
const chalk = require("chalk")
const CryptBlockchain = require('./CryptBlockchain')
const CryptBlock = require('./CryptBlock')
var config = {
PORT: prompt("Enter PORT: "),
USER_NAME: prompt("Enter Username: "),
USER_PASS: prompt.hide("Enter password: "),
EN_KEY: "",
PACK_ID: "",
BCLK: null
}
axios
.post(`http://localhost:8080/register`, {
user: config.USER_NAME,
pass: config.USER_PASS,
PORT: config.PORT
})
.then(res => {
config.EN_KEY = res.data["key"]
config.PACK_ID = prompt("Enter Package ID: ")
config.BCLK = new CryptBlockchain(id=config.PACK_ID, res.data.blockchain.blockchain)
console.log(`\n\n${chalk.green("Registered Successfully")}: ${config.EN_KEY}\n`);
axios
.post(`http://localhost:8080/add`, {
id: config.PACK_ID,
data: prompt("Enter Status: "),
PORT: config.PORT,
hash: config.BCLK.obtainLatestBlock().hash
})
.then(res => {
console.log("Adding block")
})
.catch(err => {
console.error("Invalid Package ID")
process.exit(1)
})
})
.catch(error => {
console.log(`statusCode: ${error.status}`);
console.error(`${chalk.yellow("ERROR")}: ${error}`);
process.exit(1)
})
const app = express()
const PORT = config.PORT || 8080
const DIFFICULTY = 3
app.use(cors())
app.use(express.json())
app.post("/addblock", (req, res) => {
console.log(`POST: /addblock, PORT: ${req.body.nodeHash}`)
config.BCLK = new CryptBlockchain(id=config.PACK_ID, req.body.blockchain.blockchain)
console.log(config.BCLK)
res.status(200).send("OK")
})
app.get("/", (req, res) => {
console.log("GET: /")
res.send(JSON.stringify(trackerBlockchain))
})
app.post("/verify", (req, res) => {
let block = new CryptBlock(req.body.date, req.body.data, req.body.reqPORT, req.body.nodeHash, req.body.index, 0)
let hash= SHA256(this.index + req.body.date + JSON.stringify(req.body.date) + req.body.PORT + config.BCLK.obtainLatestBlock().prevHash + 0).toString()
block.proofOfWork()
//console.log(block)
res.status(200).json({nonce: block.nonce})
})
app.post("/end", (req, res) => {
res.status(200).send("OK")
process.exit(0)
})
app.get("/get", (req, res) => res.status(200).json({data: "nodePORTS[req.body.PORT]", blockchain: config.BCLK}))
app.listen(PORT, () => console.log(`Listening on PORT: ${PORT}`))