Update Server.js

This commit is contained in:
sairajzero 2021-08-03 19:41:06 +05:30
parent 81ebd39fbb
commit ac21c53d88

View File

@ -1,10 +1,10 @@
const http = require('http');
const floCrypto = require('./floCrypto');
module.exports = function startServer(port){
module.exports = function startServer(port) {
const httpServer = http.createServer((req, res) => {
res.setHeader("Access-Control-Allow-Origin", "*");
if(req.method === "GET")
if (req.method === "GET")
res.end("");
else if (req.method === "POST") {
//POST: All data processing (required JSON input)
@ -12,20 +12,22 @@ module.exports = function startServer(port){
req.on('data', chunk => data += chunk);
req.on('end', () => {
//process verification
let result = {};
let result = {
success: false
};
try {
var d = JSON.parse(data);
if(!floCrypto.validateAddr(d.floID))
result.failed = "Invalid floID";
else if(floCrypto.getFloID(d.pubKey) !== d.floID)
result.failed = "Public key mismatched";
if (!floCrypto.validateAddr(d.floID))
result.reason = "Invalid floID";
else if (floCrypto.getFloID(d.pubKey) !== d.floID)
result.reason = "Public key mismatched";
else if (!floCrypto.verifySign(d.message, d.sign, d.pubKey))
result.failed = "Signature not verified";
result.reason = "Signature not verified";
else
result.success = true;
} catch (error) {
result.Error = "Invalid request";
}finally{
result.reason = "Invalid request";
} finally {
res.end(JSON.stringify(result));
};
});
@ -41,4 +43,4 @@ module.exports = function startServer(port){
Object.defineProperty(this, "http", {
get: () => httpServer
});
};
};