-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockController.js
More file actions
225 lines (199 loc) · 9.1 KB
/
BlockController.js
File metadata and controls
225 lines (199 loc) · 9.1 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//Importing the Block and Blockchain Classes
const Block = require('./Block.js');
const Blockchain = require('./Blockchain.js');
const bitcoinMessage = require('bitcoinjs-message');
/**
* Controller Definition to encapsulate routes to work with blocks
*/
class BlockController {
/**
* Constructor to create a new BlockController, you need to initialize here all your endpoints
* @param {*} app
*/
constructor(app) {
this.app = app;
this.Blockchain = new Blockchain();
this.getBlockByHeight();
this.postNewBlock();
this.postRequestValidation();
this.postValidateSignature();
this.getBlockByHash();
this.getBlockByWalletAddress();
this.TimeoutRequestsWindowTime = 5*60*1000;
this.mempool = [];
this.timeoutRequests = {};
this.mempoolValid = [];
}
/**
* Implement a GET Endpoint to retrieve a block by height, url: "/block/:index"
*/
getBlockByHeight() {
this.app.get("/block/:height", async (req, res) => {
let reqBlock = null;
try {
reqBlock = await this.Blockchain.getBlockByHeight(req.params.height);
res.send(reqBlock);
} catch (err) {
if (err.message.includes("Key not found")) {
res.send(`No block found with height of ${req.params.height}`);
} else {
res.send(`Error in getBlockByHeight method of Block Controller. \n ${err}`);
}
}
});
}
/**
* Implement a GET Endpoint to retrieve a block by hash, url: "/stars/hash:[hash]"
*/
getBlockByHash() {
this.app.get("/stars/hash::hash", async (req, res) => {
let reqBlock = null;
try {
reqBlock = await this.Blockchain.getBlockByHash(req.params.hash);
console.log(reqBlock);
if (!reqBlock) throw Error("Hash not found")
res.send(reqBlock);
} catch (err) {
if (err.message.includes("Hash not found")) {
res.send(`No block found with hash: ${req.params.hash}`);
} else {
res.send(`Error in getBlockByHash method of Block Controller. \n ${err}`);
}
}
});
}
/**
* Implement a GET Endpoint to retrieve a block by walletAddress, url: "/stars/address:[address]"
*/
getBlockByWalletAddress(){
this.app.get("/stars/address::address", async (req, res) => {
let reqBlocks = null;
try {
reqBlocks = await this.Blockchain.getBlockByWalletAddress(req.params.address);
console.log(reqBlocks);
if (reqBlocks.length == 0) throw Error("No blocks found associated to this wallet")
res.send(reqBlocks);
} catch (err) {
if (err.message.includes("to this wallet")) {
res.send(`No block found with hash: ${req.params.address}`);
} else {
res.send(`Error in getBlockByWalletAddress method of Block Controller. \n ${err}`);
}
}
});
}
/**
* Implement a POST Endpoint to add a new Block, url: "/block"
*/
postNewBlock() {
this.app.post("/block", async (req, res) => {
try {
let starObj = req.body;
// Wallet Address has requested validation
if (!this.mempoolValid.includes(starObj.address)) throw Error("Wallet Address has not validated a request");
// Star Object has required properties
else if (!['address', 'star'].every( r => Object.keys(starObj).includes(r) ) ||
!['dec', 'ra', 'story'].every( s => Object.keys(starObj.star).includes(s) ) ){
throw Error("Invalid star object format for Block's body");
}
// Star story is not too long
else if (starObj.star.story.split(" ").length > 250 ||
Buffer.byteLength(starObj.star.story, 'ascii') > 500) {
throw Error("Invalid star story, > 250 words or > 500 bites");
}
// Star story has only ascii characters
else if ([...starObj.star.story].some(char => char.charCodeAt(0) > 127)) throw Error("String story has non-Ascii characters")
let body = {
address: starObj.address,
star: starObj.star
};
let savedBlockHeight = await this.Blockchain.addBlock(new Block(body));
let savedBlock = await this.Blockchain.getBlockByHeight(savedBlockHeight);
this.mempoolValid = this.mempoolValid.filter(a => a !== starObj.address);
res.send(savedBlock);
} catch (err) {
if (err.message.includes("validated a request") ||
err.message.includes("Invalid ") ||
err.message.includes("non-Ascii")) {
console.log(err)
res.send(err.message);
} else {
console.log(err)
res.send(`Error in postNewBlock method of Block Controller. \n ${err}`);
}
}
});
}
/**
* Project 4 - request validation
* Implement a POST Endpoint to initiate request validation, url: "/validateRequest"
*/
postRequestValidation() {
this.app.post("/requestValidation", (req, res) => {
const address = req.body.address;
const reqTimestamp = new Date().getTime().toString().slice(0,-3);
let reqValObj = {};
// check if this wallet address has a previous, valid, validation request
if (Object.keys(this.timeoutRequests).includes(address)) {
// getting original request validation object
reqValObj = this.timeoutRequests[address];
// calculate remaining time in validation window and update dict property
let timeElapsed = reqTimestamp - reqValObj["requestTimeStamp"];
let timeLeft = (this.TimeoutRequestsWindowTime/1000) - timeElapsed;
reqValObj["validationWindow"] = timeLeft;
// if there isn't a valid, validation requests by this walletAddress, create one
} else {
reqValObj["address"] = address,
reqValObj["requestTimeStamp"] = reqTimestamp;
reqValObj["message"] = `${address}:${reqTimestamp}:starRegistry`; // sub 0 for ${reqTimestamp} for testing
reqValObj["validationWindow"] = this.TimeoutRequestsWindowTime/1000;
}
// saving the original validation request object
// timeoutRequests is a dictionary with walletAddress as keys and requestValidationObjects (also dicts) as values
this.timeoutRequests[address] = reqValObj;
setTimeout( () => {
delete this.timeoutRequests[address];
// console.log(this.timeoutRequests);
}, this.TimeoutRequestsWindowTime, address);
res.send(reqValObj);
});
}
/**
* Project 4 - Validate Signature
* Implement a POST Endpoint to validate message signature, url: "/message-signature/validate"
*/
postValidateSignature() {
this.app.post("/message-signature/validate", (req, res) => {
const reqTimestamp = new Date().getTime().toString().slice(0,-3);
const address = req.body.address;
const signature = req.body.signature;
if (Object.keys(this.timeoutRequests).includes(address)) {
// verify signature
let reqValObj = this.timeoutRequests[address];
let isValid = bitcoinMessage.verify(reqValObj.message, address, signature);
if (isValid) {
// calculate remaining time in validation window and update dict property
let timeElapsed = reqTimestamp - reqValObj["requestTimeStamp"];
let timeLeft = (this.TimeoutRequestsWindowTime/1000) - timeElapsed;
reqValObj["validationWindow"] = timeLeft
// create a valid request object
let valObj = { "registerStar": isValid, "status": reqValObj };
valObj["status"]["messageSignature"] = isValid;
this.mempoolValid.push(valObj.status.address)
delete this.timeoutRequests[address];
res.send(valObj);
}
else {
res.send("Signature Validation Invalid")
}
} else {
res.send("You must first submit a Validation Request, go to: /validateRequest")
}
})
}
} // Block Controller Class closing bracket
/**
* Exporting the BlockController class
* @param {*} app
*/
module.exports = (app) => { return new BlockController(app);}