aboutsummaryrefslogtreecommitdiff
path: root/api/routes/getMrk/index.js
blob: 92f8c1e0c6d923ab853f63f09cfc1e67ff0bbeed (plain)
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
const fs = require("fs");
const config = require("../../config");

/*
 * Gets a local merkle leaf
 *
 * Returns:
 *  the merkle leaf
 *
 */
function fetchFmrk(mrk, res){
    res.set('Content-Type', 'application/json');
    const path = config.workDir+"/fmrk/"+mrk;
    // console.log(path)
    try {
        if(fs.existsSync(path)){
            res.send(fs.readFileSync(path));
        }
    } catch (error) {
        res.send({"error":error.message});
    }
};

module.exports = (req, res) => {
    console.log(req.params)
    res.set('Content-Type', 'application/json');
    if ( (req.params.mrk) && typeof req.params.mrk === "string" && req.params.mrk.length === 128 ){
        regex= /[a-f0-9]{128}/;
        if (regex.test(req.params.mrk)){
            let mrk = req.params.mrk;
            fetchFmrk(mrk,res);
        } else {
            res.send({error:"Invalid data: regexp failed to pass"});
        }
    } else {
        res.send({error:"Invalid data: no valid zblock was provided"});
    }
}