-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
151 lines (115 loc) · 3.94 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#! /usr/bin/env node
var express = require('express')
var parse = require('url-parse')
const sha256 = require('sha256');
var app = express()
var bodyParser = require('body-parser')
app.use(bodyParser.json()) // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })) // support encoded bodies
const requests = require('request')
// Blockchain class
const Blockchain = require('./blockchain')
// Regulator class
const Regulator = require('./regulator')
// Generate a globally unique address for this node
function uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8)
return v.toString(16)
})
}
node_identifier = uuidv4()
blockChain = new Blockchain()
blockChain.genesis()
regulator = new Regulator()
app.get('/mine', function mine(req, res) {
// We run the proof of work algorithm to get the next proof...
var lastBlock = blockChain.lastBlock()
var lastNonce = lastBlock.nonce
var nonce = blockChain.proofOfWork(lastNonce)
// We must receive a reward for finding the proof.
// The sender is "0" to signify that this node has mined a new coin.
blockChain.newTransaction(
sender="0",
recipient=node_identifier,
amount=1,
)
// Forge the new Block by adding it to the chain
previousHash = blockChain.constructor.hash(blockChain.lastBlock())
block = blockChain.newBlock(nonce, previousHash)
response = {
'message': "New Block Forged",
'index': block['index'],
'transactions': block['transactions'],
'nonce': block['nonce'],
'previous_hash': block['previous_hash'],
}
res.status(200).json(response)
})
app.post('/transactions/new', function newTransaction(req, res) {
const values = req.body
console.log(values)
const keys = Object.keys(values)
// Check that the required fields are in the POST'ed data
const required = ['sender', 'recipient', 'amount', 'signature', 'public_key']
if(keys.toString() !== required.toString()) {
res.status(400).send("Missing values")
}
// Validate transaction
if(!regulator.identify(values, values.signature, values.public_key)) {
res.status(400).send("Malicious transaction detected; Signature does not match your identity")
}
// Create a new Transaction
const index = blockChain.newTransaction(values['sender'], values['recipient'], values['amount'])
// Broadcast Transaction to other nodes
var neighbours = this.nodes
neighbours.forEach(function (node) {
requests.post(`http://${node}/transactions/new`).form({'sender' : values['sender'], 'recipient' : values['recipient'], 'amount' : values['amount']})
})
response = {'message': `Transaction will be added to Block ${index}`}
res.status(201).json(response)
})
app.get('/chain', function fullChain(req, res){
response = {
'chain': blockChain.chain,
'length': blockChain.chain.length,
}
res.status(200).json(response)
})
app.post('/nodes/register', function registerNodes(req, res){
values = req.body
nodes = values['nodes']
if(nodes == null){
res.status(400).send("Error: Please supply a valid list of nodes")
}
nodes.forEach(function(node) {
blockChain.registerNode(node)
})
response = {
'message': "New nodes have been added",
'total_nodes': Array(blockChain.nodes)
}
res.status(201).json(response)
})
app.get('/nodes/resolve', function consensus(req, res){
replaced = blockChain.resolveConflicts()
if(replaced) {
response = {
'message': "Our chain was replaced",
'new_chain': blockChain.chain
}
}
else {
response = {
'message': "Our chain is authoritative",
'new_chain': blockChain.chain
}
}
res.status(200).json(response)
})
app.get('/wallet/generate', function generate(req, res){
var wallet = regulator.generate()
response = Object.assign({},wallet,{'message' : 'New wallet has been generated!'});
res.status(200).json(response)
})
app.listen(3000)