-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
119 lines (82 loc) · 2.62 KB
/
app.js
File metadata and controls
119 lines (82 loc) · 2.62 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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
let Block = require('./block')
let Blockchain = require('./blockchain')
let BlockchainNode = require('./BlockchainNode')
let Transaction = require('./transaction')
let sha256 = require('js-sha256')
let DrivingRecordSmartContract = require('./smartContract')
let fetch = require('node-fetch')
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
let port = 3000
// access the arguments
process.argv.forEach(function(val,index,array){
port = array[2]
})
if(port == undefined) {
port = 3000
}
let transactions = []
let nodes = []
let genesisBlock = new Block()
let blockchain = new Blockchain(genesisBlock)
app.use(bodyParser.json())
app.get('/resolve',function(req,res){
nodes.forEach(function(node){
fetch(node.url + '/blockchain')
.then(function(response){
return response.json()
})
.then(function(otherNodeBlockchain){
if(blockchain.blocks.length < otherNodeBlockchain.blocks.length) {
blockchain = otherNodeBlockchain
}
res.send(blockchain)
})
})
})
app.post('/nodes/register',function(req,res){
let nodesLists = req.body.urls
nodesLists.forEach(function(nodeDictionary){
let node = new BlockchainNode(nodeDictionary["url"])
nodes.push(node)
})
res.json(nodes)
})
app.get('/nodes',function(req,res){
res.json(nodes)
})
app.get('/',function(req,res){
res.send("hello world")
})
app.get('/mine',function(req,res){
let block = blockchain.getNextBlock(transactions)
blockchain.addBlock(block)
transactions = []
console.log(transactions)
res.json(block)
})
app.get('/driving-records/:drivingLicenseNumber',(request, response)=>{
console.log("Reached the method")
console.log("Data received :", request.params.drivingLicenseNumber)
let drivingLicenseNumber = sha256(request.params.drivingLicenseNumber)
let transactions = blockchain.transactionsByDrivingLicenseNumber(drivingLicenseNumber)
response.json(transactions)
})
app.post('/transactions',function(req,res){
console.log(transactions)
let drivingRecordSmartContract = new DrivingRecordSmartContract()
let driverLicenseNumber = sha256(req.body.driverLicenseNumber)
let voilationDate = req.body.voilationDate
let voilationType = req.body.voilationType
let transaction = new Transaction(driverLicenseNumber,voilationDate,voilationType)
drivingRecordSmartContract.apply(transaction,blockchain.blocks)
transactions.push(transaction)
res.json(transactions)
})
app.get('/blockchain',function(req,res){
res.json(blockchain)
})
app.listen(port,function(){
console.log("server has started")
})