-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpHandler.js
More file actions
173 lines (153 loc) · 5.4 KB
/
httpHandler.js
File metadata and controls
173 lines (153 loc) · 5.4 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const Blockchain = require('./simpleChain');
const currentChain = new Blockchain();
const HashMap = require('hashmap');
const addressMap = new HashMap(); // Cache: addressMap[address] -> ID
const validationProcess = require('./validation');
// TODO: elements stays forever in addressMap if no call to addBlock happens after validation process.
// startValidation adds a new created ID to the addressMap and responses
// the message to validate with a deadline.
// Handles: POST /requestValidation
// Expects: JSON -> { address }
async function startValidation(req, res) {
const address = req.body.address;
if (!address && typeof address === "string") {
console.log('parsing address failed. req.body=%s', req.body);
errorHandler(req, res, 'Unable to parse address');
return;
}
const customer = new validationProcess(address);
addressMap.set(address, customer);
const resMessage = {
"address": customer.address,
"requestTimestamp": customer.timestamp,
"message": customer.message,
"validationWindow": customer.deadline
};
res.send(resMessage);
}
// validateSignature validates the signature and responses a status.
// Handles: POST /message-signature/validate
// Expects: JSON -> { address, signature }
async function validateSignature(req, res) {
const address = req.body.address;
const signature = req.body.signature;
const customer = addressMap.get(address);
if (customer === undefined) {
console.log('ID not found in map. address=%s signature=%s', address, signature);
errorHandler(req, res, 'Validation failed.');
return;
}
console.log(customer);
await customer.verifySignature(signature);
if (!customer.isVerified()) {
console.log('validation failed. ID=%s signature=%s', customer, signature);
errorHandler(req, res, 'Validation failed.');
return;
}
const status = {
"registerStar": true,
"status": {
"address": customer.address,
"requestTimestamp": customer.timestamp,
"message": customer.message,
"validationWindow": customer.deadline,
"messageSignature": true
}
};
res.send(status);
}
// addBlock adds a new star to the blockchain, if address is verified.
// Handles: POST /block
// Expects: JSON -> { address, star: { dec, ra, story } }
async function addBlock(req, res) {
const address = req.body.address;
if (!address && typeof address === "string") {
console.log('parsing address failed. req.body=%s', req.body);
errorHandler(req, res, 'Unable to parse address');
return;
}
const star = req.body.star;
// TODO: check star-object
const customer = addressMap.get(address);
if (customer === undefined) {
console.log('ID not found in map. address=%s', address);
errorHandler(req, res, 'Please validate before submitting a new star.');
return;
}
addressMap.delete(address);
if (!customer.isVerified()) {
console.log('ID not verified. ID=%s', customer);
errorHandler(req, res, 'Please validate before submitting a new star.');
return;
}
const blockContent = {
"address": address,
"star": JSON.stringify(star)
};
try {
const block = await currentChain.addBlock(Blockchain.createBlock(blockContent));
res.send(block);
} catch (err) {
console.log('addBlock received error:', err);
errorHandler(req, res, 'Currently unable to add star');
}
}
// getBlock returns the requested block.
// Handles: GET /block/:blockID
async function getBlock(req, res) {
const blockID = parseInt(req.params.blockID, 10);
if (isNaN(blockID) || blockID < 0) {
errorHandler(req, res, 'Unable to parse block ID');
return;
}
try {
const block = await currentChain.getBlock(blockID);
res.send(block);
} catch (err) {
console.log('getBlock received error:', err);
errorHandler(req, res, 'Currently unable to get block #%s', blockID);
}
}
// getBlockForAddress returns all blocks added by this address.
// Handles: GET /stars/address/:address
async function getBlocksForAddress(req, res) {
const address = req.params.address;
if (!address && typeof address === "string") {
errorHandler(req, res, 'Unable to parse address');
return;
}
try {
const blocks = await currentChain.getBlocksAddedBy(address);
res.send(blocks);
} catch (err) {
console.log('getBlocksForAddress received error:', err);
errorHandler(req, res, 'Currently unable to get blocks added by '+ address);
}
}
// getBlockWithHash returns the block with requested hash.
// Handles: GET /stars/hash/:hash
async function getBlockWithHash(req, res) {
const hash = req.params.hash;
if (!hash && typeof hash === "string") {
errorHandler(req, res, 'Unable to parse hash');
return;
}
try {
const block = await currentChain.getBlockWithHash(hash);
res.send(block);
} catch (err) {
console.log('getBlockWithHash received error:', err);
errorHandler(req, res, 'Currently unable to get block with hash '+ hash);
}
}
function errorHandler(req, res, errMsg) {
res.send(errMsg);
}
module.exports = {
addBlock,
getBlock,
startValidation,
validateSignature,
getBlocksForAddress,
getBlockWithHash
};