-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlockchainController.js
135 lines (123 loc) · 5.04 KB
/
BlockchainController.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
/**
* BlockchainController
*
* This class expose the endpoints that the client applications will use to interact with the
* Blockchain dataset
*/
class BlockchainController {
//The constructor receive the instance of the express.js app and the Blockchain class
constructor(app, blockchainObj) {
this.app = app;
this.blockchain = blockchainObj;
// All the endpoints methods needs to be called in the constructor to initialize the route.
this.getBlockByHeight();
this.requestOwnership();
this.submitStar();
this.getBlockByHash();
this.getStarsByOwner();
this.getChainValidation();
}
/// Endppint to validate the chain
getChainValidation(){
try {
this.app.get("/chainValidation", async(req, res) => {
await this.blockchain.validateChain();
return res.status(200).send("Successfully validated");
})
} catch (e) {
reject(new Error(e));
}
}
// Enpoint to Get a Block by Height (GET Endpoint)
getBlockByHeight() {
this.app.get("/block/:height", async (req, res) => {
if(req.params.height) {
const height = parseInt(req.params.height);
let block = await this.blockchain.getBlockByHeight(height);
if(block){
return res.status(200).json(block);
} else {
return res.status(404).send("Block Not Found!");
}
} else {
return res.status(404).send("Block Not Found! Review the Parameters!");
}
});
}
// Endpoint that allows user to request Ownership of a Wallet address (POST Endpoint)
requestOwnership() {
this.app.post("/requestValidation", async (req, res) => {
if(req.body.address) {
const address = req.body.address;
const message = await this.blockchain.requestMessageOwnershipVerification(address);
if(message){
return res.status(200).json(message);
} else {
return res.status(500).send("An error happened!");
}
} else {
return res.status(500).send("Check the Body Parameter!");
}
});
}
// Endpoint that allow Submit a Star, yu need first to `requestOwnership` to have the message (POST endpoint)
submitStar() {
this.app.post("/submitstar", async (req, res) => {
if(req.body.address && req.body.message && req.body.signature && req.body.star) {
const address = req.body.address;
const message = req.body.message;
const signature = req.body.signature;
const star = req.body.star;
try {
let block = await this.blockchain.submitStar(address, message, signature, star);
if(block){
return res.status(200).json(block);
} else {
return res.status(500).send("An error happened!");
}
} catch (error) {
return res.status(500).send(error);
}
} else {
return res.status(500).send("Check the Body Parameter!");
}
});
}
// This endpoint allows you to retrieve the block by hash (GET endpoint)
getBlockByHash() {
this.app.get("/block/hash/:hash", async (req, res) => {
if(req.params.hash) {
const hash = req.params.hash;
let block = await this.blockchain.getBlockByHash(hash);
if(block)
return res.status(200).json(block);
else
return res.status(404).send("Block Not Found!");
} else {
return res.status(404).send("Block Not Found! Review the Parameters!");
}
});
}
// This endpoint allows you to request the list of Stars registered by an owner
getStarsByOwner() {
this.app.get("/blocks/:address", async (req, res) => {
if(req.params.address) {
const address = req.params.address;
try {
let stars = await this.blockchain.getStarsByWalletAddress(address);
if(stars.length==0)
return res.status(404).send("Wallet not found");
else if (stars)
return res.status(200).json(stars);
else
return res.status(404).send("Block Not Found!");
} catch (error) {
return res.status(500).send("An error happened!");
}
} else {
return res.status(500).send("Block Not Found! Review the Parameters!");
}
});
}
}
module.exports = (app, blockchainObj) => { return new BlockchainController(app, blockchainObj);}