diff options
Diffstat (limited to 'api/routes')
| -rw-r--r-- | api/routes/announceZBlock/index.js | 4 | ||||
| -rw-r--r-- | api/routes/announceZChain/index.js | 4 | ||||
| -rw-r--r-- | api/routes/default/index.js | 5 | ||||
| -rw-r--r-- | api/routes/getAKNSKey/index.js | 75 | ||||
| -rw-r--r-- | api/routes/getAKNSKeyFromBase/index.js | 91 | ||||
| -rw-r--r-- | api/routes/getChunk/index.js | 5 | ||||
| -rw-r--r-- | api/routes/getIPFSHash/index.js | 23 | ||||
| -rw-r--r-- | api/routes/getInnerIPFSContent/index.js | 4 | ||||
| -rw-r--r-- | api/routes/getLeaf/index.js | 4 | ||||
| -rw-r--r-- | api/routes/getMap/index.js | 5 | ||||
| -rw-r--r-- | api/routes/getNodeInfo/index.js | 4 | ||||
| -rw-r--r-- | api/routes/getPeers/index.js | 4 | ||||
| -rw-r--r-- | api/routes/getRemoteNodeInfo/index.js | 8 | ||||
| -rw-r--r-- | api/routes/getRemotePeers/index.js | 7 | ||||
| -rw-r--r-- | api/routes/getSBlock/index.js | 8 | ||||
| -rw-r--r-- | api/routes/getZChain/index.js | 5 | ||||
| -rw-r--r-- | api/routes/showEntriesFile/index.js | 6 | ||||
| -rw-r--r-- | api/routes/showNSEntriesFile/index.js | 6 | 
18 files changed, 228 insertions, 40 deletions
diff --git a/api/routes/announceZBlock/index.js b/api/routes/announceZBlock/index.js index 254e1dc..dc1467a 100644 --- a/api/routes/announceZBlock/index.js +++ b/api/routes/announceZBlock/index.js @@ -13,7 +13,8 @@   *   */  const getvalidity = require('../../validators/ZblockValidator') -module.exports = (req, res) => { +function announceZBlock(req, res) +{      console.log(req);      if ( (req.body.zblock) && typeof req.body.zblock === "string" && req.body.zblock.length === 46 ){          let zblock = req.body.zblock; @@ -27,3 +28,4 @@ module.exports = (req, res) => {          res.send({error:"Invalid data"});      }  } +module.exports = announceZBlock; diff --git a/api/routes/announceZChain/index.js b/api/routes/announceZChain/index.js index 0261aa4..ca23a72 100644 --- a/api/routes/announceZChain/index.js +++ b/api/routes/announceZChain/index.js @@ -14,7 +14,8 @@   */  const getNSvalidity = require('../../validators/ZchainValidator'); -module.exports = (req, res) => { +function announceZChain(req, res) +{      console.log(req);      if ( (req.body.zchain) && typeof req.body.zchain === "string" && req.body.zchain.length === 62 ){          let zchain = req.body.zchain; @@ -30,3 +31,4 @@ module.exports = (req, res) => {          res.end(JSON.stringify({error:"Invalid data"}));      }  } +module.exports = announceZChain; diff --git a/api/routes/default/index.js b/api/routes/default/index.js index 24e334b..db1495f 100644 --- a/api/routes/default/index.js +++ b/api/routes/default/index.js @@ -1,7 +1,8 @@  const rsettings = require('../../settings');  const settings = rsettings(); -module.exports = (req, res) => { +function welcome(req, res) +{      res.writeHead(404, { 'Content-Type': 'application/json'});      res.end(JSON.stringify({          message:"Hello! Welcome to Arching Kaos API! See available routes below!", @@ -36,4 +37,4 @@ module.exports = (req, res) => {          }      }));  } - +module.exports = welcome; diff --git a/api/routes/getAKNSKey/index.js b/api/routes/getAKNSKey/index.js new file mode 100644 index 0000000..5e78f81 --- /dev/null +++ b/api/routes/getAKNSKey/index.js @@ -0,0 +1,75 @@ +const { spawn } = require('child_process'); +const fs = require('fs'); +const config = require("../../config.js"); + +/* + * Gets the local latest zblock AKA zlatest + * + * Returns: + *     - JSON object + *     { zlatest: "Qm..." } + * + */ +function replyIfOkay(key, res) +{ +    const program = "ak-ns"; +    const command = spawn(program, ["-rj", key]); +    var buffer = ""; +    command.stdout.on("data", data => { +        buffer += data; +    }); +    command.stderr.on("data", data => { +        console.log(`stderr: ${data}`); +    }); +    command.on('error', (error) => { +        console.log(`error: ${error.message}`); +    }); +    command.on("close", code => { +        console.log(`child process ${program} exited with code ${code}`); +        if (code === 0){ +            buffer = buffer.trim() +            res.writeHead(200, {'Content-Type': 'application/json'}); +            res.end(`${buffer}`); +        } else { +            res.writeHead(404, {'Content-Type': 'application/json'}); +            res.end({"error":"unreachable"}); +        } +    }); +} + +function getAKNSKey(req, res) +{ +    var args = req.url.split("/"); +    var key = args[3]; +    regex= /[a-fA-F0-9]{40}/ +    if (regex.test(key)) +    { +        key = key.toUpperCase(); +        var path = config.akNSDir+"/"+key; +        try +        { +            if(fs.existsSync(path)) +            { +                replyIfOkay(key, res) +            } +            else +            { +                res.writeHead(404, {'Content-Type': 'application/json'}); +                res.end(JSON.stringify({"error":"not found"})); +            } +        } +        catch (error) +        { +            res.writeHead(404, {'Content-Type': 'application/json'}); +            res.end(JSON.stringify({"error":error.message})); +        } +    } +    else +    { +        res.writeHead(404, {'Content-Type': 'application/json'}); +        res.end(JSON.stringify({error:"No hash"})); +    } +} + +module.exports = getAKNSKey; + diff --git a/api/routes/getAKNSKeyFromBase/index.js b/api/routes/getAKNSKeyFromBase/index.js new file mode 100644 index 0000000..7da3d9b --- /dev/null +++ b/api/routes/getAKNSKeyFromBase/index.js @@ -0,0 +1,91 @@ +const { spawn } = require('child_process'); +const fs = require('fs'); +const config = require("../../config.js"); + +/* + * Gets the local latest zblock AKA zlatest + * + * Returns: + *     - JSON object + *     { zlatest: "Qm..." } + * + */ +function decodeBase64ToHex(base64String) { +    // Decode the Base64 string +    const binaryString = atob(base64String); + +    // Convert the binary string to a byte array +    const byteArray = new Uint8Array(binaryString.length); +    for (let i = 0; i < binaryString.length; i++) { +        byteArray[i] = binaryString.charCodeAt(i); +    } + +    // Convert the byte array to a hex string representation +    return Array.from(byteArray) +        .map(byte => byte.toString(16).padStart(2, '0')) // Convert to hex and pad with zeros +        .join(''); // Join with spaces for readability +} + +// Example usage + + +function replyIfOkay(key, res) +{ +    const program = "ak-ns"; +    const base64String = key; +    const decodedHexString = decodeBase64ToHex(base64String); +    formatted_key = decodedHexString.toUpperCase(); +    const command = spawn(program, ["-rj", `${formatted_key}`]); + +    var buffer = ""; +    command.stdout.on("data", data => { +        buffer += data; +    }); +    command.stderr.on("data", data => { +        console.log(`stderr: ${data}`); +    }); +    command.on('error', (error) => { +        console.log(`error: ${error.message}`); +    }); +    command.on("close", code => { +        console.log(`child process ${program} exited with code ${code}`); +        if (code === 0){ +            buffer = buffer.trim() +            res.writeHead(200, {'Content-Type': 'application/json'}); +            res.end(`${buffer}`); +        } else { +            res.writeHead(404, {'Content-Type': 'application/json'}); +            res.end(JSON.stringify({error:"unreachable"})); +        } +    }); +} + +function getAKNSKeyFromBase(req, res) +{ +    var args = req.url.split("/"); +    var key = args[3]; +    regex= /[a-zA-Z0-9+\/=]{29}/ +    const base64Regex = /^[A-Z0-9+\/=]{28}/i; +    if (base64Regex.test(key)) +    { +    // key = key.toUpperCase(); +    //   var path = config.akNSDir+"/"+key; +        try +        { +            replyIfOkay(key, res) +        } +        catch (error) +        { +            res.writeHead(404, {'Content-Type': 'application/json'}); +            res.end(JSON.stringify({"error":error.message})); +        } +   } +   else +   { +       res.writeHead(404, {'Content-Type': 'application/json'}); +       res.end(JSON.stringify({error:"No hash"})); +   } +} + +module.exports = getAKNSKeyFromBase; + diff --git a/api/routes/getChunk/index.js b/api/routes/getChunk/index.js index 6106aad..dc86bd8 100644 --- a/api/routes/getChunk/index.js +++ b/api/routes/getChunk/index.js @@ -7,7 +7,8 @@  const fs = require('fs');  const config = require("../../config.js"); -module.exports = (req, res) => { +function getChunk(req, res) +{      var args = req.url.split("/");      var hash = args[3];      regex= /[a-f0-9]{128}/ @@ -39,4 +40,4 @@ module.exports = (req, res) => {          res.end(JSON.stringify({error:"No hash"}));      }  } - +module.exports = getChunk; diff --git a/api/routes/getIPFSHash/index.js b/api/routes/getIPFSHash/index.js index f412c80..d1bdc5f 100644 --- a/api/routes/getIPFSHash/index.js +++ b/api/routes/getIPFSHash/index.js @@ -1,26 +1,29 @@  const { spawn } = require('child_process');  const fs = require("fs");  const config = require("../../config"); +const akLogMessage = require("../../lib/akLogMessage"); +akLogMessage('lol');  /* - * Returns a cached zblock + * Returns a cached ipfs_hash   *   * Returns:   *     - JSON object   *   */ -function fetchIPFShash(zblock, res) +function fetchIPFShash(ipfs_hash, res)  {      regex= /Qm[A-Za-z0-9]{44}/; -    if (regex.test(zblock)){ -        const path = `${config.ipfsArtifactsDir}/${zblock}`; +    if (regex.test(ipfs_hash)){ +        const path = `${config.ipfsArtifactsDir}/${ipfs_hash}`;          console.log(path)          try          {              if(fs.existsSync(path))              { -                res.writeHead(200, {'Content-Type': 'application/json'}); -                res.end(JSON.stringify(JSON.parse(fs.readFileSync(path)))); +                res.writeHead(200); //, {'Content-Type': 'application/json'}); +                // res.end(JSON.stringify(JSON.parse(fs.readFileSync(path)))); +                res.end(fs.readFileSync(path));              }              else              { @@ -41,9 +44,10 @@ function fetchIPFShash(zblock, res)      }  }; -module.exports = (req, res) => { +function getIPFSHash(req, res) +{      var args = req.url.split("/"); -    if ( (args[2] === 'ipfs_hash') && args[3] && typeof args[3] === "string" && args[3].length === 46 ){ +    if ( (args[2] === 'ipfs_hash'||args[2] === 'ipfs') && args[3] && typeof args[3] === "string" && args[3].length === 46 ){          regex= /Qm[A-Za-z0-9]{44}/;          if (regex.test(args[3]))          { @@ -66,6 +70,7 @@ module.exports = (req, res) => {      else      {          res.writeHead(404, {'Content-Type': 'application/json'}); -        res.end(JSON.stringify({error:"Invalid data: no valid zblock was provided"})); +        res.end(JSON.stringify({error:"Invalid data: no valid ipfs_hash was provided"}));      }  } +module.exports = getIPFSHash; diff --git a/api/routes/getInnerIPFSContent/index.js b/api/routes/getInnerIPFSContent/index.js index 721e836..a45b507 100644 --- a/api/routes/getInnerIPFSContent/index.js +++ b/api/routes/getInnerIPFSContent/index.js @@ -15,7 +15,8 @@ function fetchZblock(zblock, res){      res.end(JSON.stringify(JSON.parse(fs.readFileSync(path))));  }; -module.exports = (req, res) => { +function getInnerIPFSContent(req, res) +{      console.log(req.query)      if ( (req.query.ipfs) && typeof req.query.ipfs === "string" && req.query.ipfs.length === 46 ){          let ipfs = req.query.ipfs; @@ -36,3 +37,4 @@ module.exports = (req, res) => {          res.end(JSON.stringify({error:"Invalid data: no valid zblock was provided"}));      }  } +module.exports = getInnerIPFSContent; diff --git a/api/routes/getLeaf/index.js b/api/routes/getLeaf/index.js index 8855712..5afbe70 100644 --- a/api/routes/getLeaf/index.js +++ b/api/routes/getLeaf/index.js @@ -7,7 +7,8 @@  const fs = require('fs');  const config = require("../../config.js"); -module.exports = (req, res) => { +function getLeaf(req, res) +{      var args = req.url.split("/");      var hash = args[3];      regex= /[a-f0-9]{128}/ @@ -40,3 +41,4 @@ module.exports = (req, res) => {      }  } +module.exports = getLeaf; diff --git a/api/routes/getMap/index.js b/api/routes/getMap/index.js index 791afb5..23b4e58 100644 --- a/api/routes/getMap/index.js +++ b/api/routes/getMap/index.js @@ -7,7 +7,8 @@  const fs = require('fs');  const config = require("../../config.js"); -module.exports = (req, res) => { +function getMap(req, res) +{      var args = req.url.split("/");      var hash = args[3];      regex= /[a-f0-9]{128}/ @@ -39,4 +40,4 @@ module.exports = (req, res) => {          res.end(JSON.stringify({error:"No hash"}));      }  } - +module.exports = getMap; diff --git a/api/routes/getNodeInfo/index.js b/api/routes/getNodeInfo/index.js index 27006ae..b267749 100644 --- a/api/routes/getNodeInfo/index.js +++ b/api/routes/getNodeInfo/index.js @@ -1,6 +1,7 @@  const { spawn } = require('child_process'); -module.exports = (req, res) => { +function getNodeInfo(req, res) +{      const command = spawn("ak-config", ["--get-published"]);      var buffer = "";      command.stdout.on("data", data => { @@ -22,3 +23,4 @@ module.exports = (req, res) => {          console.log(`child process exited with code ${code}`);      });  }; +module.exports = getNodeInfo; diff --git a/api/routes/getPeers/index.js b/api/routes/getPeers/index.js index b18a015..47c075d 100644 --- a/api/routes/getPeers/index.js +++ b/api/routes/getPeers/index.js @@ -1,7 +1,8 @@  const config = require('../../config');  const fs = require('fs'); -module.exports = (req, res) => { +function getPeers(req, res) +{      const path = config.peersFile;      if(fs.existsSync(path)){          res.writeHead(200, {'Content-Type': 'application/json'}); @@ -11,3 +12,4 @@ module.exports = (req, res) => {          res.end({"error":"No peers :("})      }  }; +module.exports = getPeers; diff --git a/api/routes/getRemoteNodeInfo/index.js b/api/routes/getRemoteNodeInfo/index.js index a88f528..4b766c0 100644 --- a/api/routes/getRemoteNodeInfo/index.js +++ b/api/routes/getRemoteNodeInfo/index.js @@ -8,7 +8,8 @@ const { spawn } = require('child_process');  const config = require("../../config.js");  const checkIfAllowedIP = require('../../lib/checkIfAllowedIP/index.js'); -module.exports = (req, res) => { +function getRemoteNodeInfo(req, res) +{      var args = req.url.split("/");      var ip = "";      if ( args.length === 4 ) @@ -23,15 +24,12 @@ module.exports = (req, res) => {          command.stdout.on("data", data => {              buffer = buffer + data;          }); -          command.stderr.on("data", data => {              console.log(`stderr: ${data}`);          }); -          command.on('error', (error) => {              console.log(`error: ${error.message}`);          }); -          command.on("close", code => {              res.writeHead(200, { 'Content-Type': 'application/json'});              res.end(buffer); @@ -44,4 +42,4 @@ module.exports = (req, res) => {          res.end(JSON.stringify({error:"No IP"}));      }  } - +module.exports = getRemoteNodeInfo; diff --git a/api/routes/getRemotePeers/index.js b/api/routes/getRemotePeers/index.js index c75a98a..dac0f04 100644 --- a/api/routes/getRemotePeers/index.js +++ b/api/routes/getRemotePeers/index.js @@ -8,7 +8,8 @@ const { spawn } = require('child_process');  const config = require("../../config.js");  const checkIfAllowedIP = require('../../lib/checkIfAllowedIP/index.js'); -module.exports = (req, res) => { +function getRemotePeers(req, res) +{      var args = req.url.split("/");      var ip = "";      if ( args.length === 4 ) @@ -22,15 +23,12 @@ module.exports = (req, res) => {          command.stdout.on("data", data => {              buffer = buffer + data;          }); -          command.stderr.on("data", data => {              console.log(`stderr: ${data}`);          }); -          command.on('error', (error) => {              console.log(`error: ${error.message}`);          }); -          command.on("close", code => {              if ( code === 0 )              { @@ -52,3 +50,4 @@ module.exports = (req, res) => {      }  } +module.exports = getRemotePeers; diff --git a/api/routes/getSBlock/index.js b/api/routes/getSBlock/index.js index 2fb8c80..85565b2 100644 --- a/api/routes/getSBlock/index.js +++ b/api/routes/getSBlock/index.js @@ -7,7 +7,8 @@ const { spawn } = require('child_process');  const fs = require('fs');  const config = require("../../config.js"); -module.exports = (req, res) => { +function getSBlock(req, res) +{      var args = req.url.split("/");      var sblock = args[3];      regex= /[a-f0-9]{128}/ @@ -20,16 +21,13 @@ module.exports = (req, res) => {              command.stdout.on("data", data => {                  response_string += data;              }); -              command.stderr.on("data", data => {                  console.log(`stderr: ${data}`);              }); -              command.on('error', (error) => {                  console.log(`error: ${error.message}`);                  response_string={err:"error.message"};              }); -              command.on("close", code => {                  if ( code === 0 ) {                      res.writeHead(200, {'Content-Type': 'application/json'}); @@ -49,4 +47,4 @@ module.exports = (req, res) => {          res.end(JSON.stringify({error:"No hash"}));      }  } - +module.exports = getSBlock; diff --git a/api/routes/getZChain/index.js b/api/routes/getZChain/index.js index 6df2d7f..934b436 100644 --- a/api/routes/getZChain/index.js +++ b/api/routes/getZChain/index.js @@ -7,7 +7,8 @@ const { spawn } = require('child_process');   *     - A JSON array representing the nodes' arching-kaos-zchain   *   */ -module.exports = (req, res) => { +function getZChain(req, res) +{      const command = spawn("ak", ["zchain", "--crawl"]);      response_string = "";      command.stdout.on("data", data => { @@ -27,3 +28,5 @@ module.exports = (req, res) => {              console.log(`child process exited with code ${code}`);      });  }; + +module.exports = getZChain; diff --git a/api/routes/showEntriesFile/index.js b/api/routes/showEntriesFile/index.js index 3dd1c40..1246460 100644 --- a/api/routes/showEntriesFile/index.js +++ b/api/routes/showEntriesFile/index.js @@ -8,7 +8,9 @@ const config = require('../../config');   *     - the file, it's already in JSON format.   *   */ -module.exports = (req, res) => { +function showEntriesFile(req, res) +{      var data = JSON.parse(fs.readFileSync(config.blocksFile));      res.send(data); -};
\ No newline at end of file +}; +module.exports = showEntriesFile; diff --git a/api/routes/showNSEntriesFile/index.js b/api/routes/showNSEntriesFile/index.js index 745ddee..cc5edf6 100644 --- a/api/routes/showNSEntriesFile/index.js +++ b/api/routes/showNSEntriesFile/index.js @@ -9,7 +9,9 @@ const config = require('../../config');   *     - the file, it's already in JSON format.   *   */ -module.exports = (req, res) => { +function showNSEntriesFile(req, res) +{      var data = JSON.parse(fs.readFileSync(config.pairsFile));      res.send(data); -};
\ No newline at end of file +}; +module.exports = showNSEntriesFile;  | 
