From e2da6d2db20093ebd2a65aad35c9991ab1a02176 Mon Sep 17 00:00:00 2001 From: kaotisk Date: Thu, 30 Mar 2023 01:09:30 +0300 Subject: Introducing an HTTP JSON API --- api/routes/default/index.js | 22 ++++++++++++++++++ api/routes/getSBlock/index.js | 43 +++++++++++++++++++++++++++++++++++ api/routes/getSLatest/index.js | 29 +++++++++++++++++++++++ api/routes/getZChain/index.js | 29 +++++++++++++++++++++++ api/routes/getZLatest/index.js | 28 +++++++++++++++++++++++ api/routes/index.js | 43 +++++++++++++++++++++++++++++++++++ api/routes/receiveZBlock/index.js | 28 +++++++++++++++++++++++ api/routes/receiveZChain/index.js | 27 ++++++++++++++++++++++ api/routes/showEntriesFile/index.js | 14 ++++++++++++ api/routes/showNSEntriesFile/index.js | 15 ++++++++++++ 10 files changed, 278 insertions(+) create mode 100644 api/routes/default/index.js create mode 100644 api/routes/getSBlock/index.js create mode 100644 api/routes/getSLatest/index.js create mode 100644 api/routes/getZChain/index.js create mode 100644 api/routes/getZLatest/index.js create mode 100644 api/routes/index.js create mode 100644 api/routes/receiveZBlock/index.js create mode 100644 api/routes/receiveZChain/index.js create mode 100644 api/routes/showEntriesFile/index.js create mode 100644 api/routes/showNSEntriesFile/index.js (limited to 'api/routes') diff --git a/api/routes/default/index.js b/api/routes/default/index.js new file mode 100644 index 0000000..e9be5cb --- /dev/null +++ b/api/routes/default/index.js @@ -0,0 +1,22 @@ +const settings = require('../../settings'); +module.exports = (req, res) => { + res.send({ + message:"Hello! Welcome to Arching Kaos API! See available routes below!", + routes:{ + GET:[ + {welcome:settings.DEF_PROTO+settings.LOCAL_IP+":"+settings.PORT+"/"}, + {node_local_chain:settings.DEF_PROTO+settings.LOCAL_IP+":"+settings.PORT+settings.URL_PREFIX+"/chain"}, + {node_local_zlatest:settings.DEF_PROTO+settings.LOCAL_IP+":"+settings.PORT+settings.URL_PREFIX+"/zlatest"}, + {gathered_zblocks:settings.DEF_PROTO+settings.LOCAL_IP+":"+settings.PORT+settings.URL_PREFIX+"/see"}, + {gathered_zchain_zlatest_pairs:settings.DEF_PROTO+settings.LOCAL_IP+":"+settings.PORT+settings.URL_PREFIX+"/seens"}, + {latest_known_mined_block:settings.DEF_PROTO+settings.LOCAL_IP+":"+settings.PORT+settings.URL_PREFIX+"/slatest"}, + {show_mined_block:settings.DEF_PROTO+settings.LOCAL_IP+":"+settings.PORT+settings.URL_PREFIX+"/sblock"} + ], + POST:[ + {send_me_a_zchain_link:settings.DEF_PROTO+settings.LOCAL_IP+":"+settings.PORT+settings.URL_PREFIX+"/szch"}, + {send_me_a_zblock:settings.DEF_PROTO+settings.LOCAL_IP+":"+settings.PORT+settings.URL_PREFIX+"/sblk"}, + ] + } + }); +} + diff --git a/api/routes/getSBlock/index.js b/api/routes/getSBlock/index.js new file mode 100644 index 0000000..4ad9127 --- /dev/null +++ b/api/routes/getSBlock/index.js @@ -0,0 +1,43 @@ +const { spawn } = require('child_process'); +const config = require("../../config.js"); + + +/* + * Gets a SBLOCK from superchain + * LOL + * + */ +module.exports = (req, res) => { + regex= /[a-f0-9]{128}/ + if (regex.test(req.query.sblock)){ + genesisreg = /0{128}/ + if (!genesisreg.test(req.query.sblock)){ + const command = spawn("cat",[config.minedBlocksDir+req.query.sblock]); + response_string = ""; + command.stdout.on("data", data => { + response_string = 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 => { + smt = JSON.stringify(response_string); + res.send({hrefPrevious:"http://127.0.0.1:8610/v0/sblock?sblock="+smt.previous,sblock:smt}); + // res.send(JSON.parse(response_string)); + console.log(`child process exited with code ${code}`); + }); + } else { + res.send({sblock:"Genesis Block - Arching Kaos Net"}); + } + } else { + res.send({"error":"No hash"}) + } +} + diff --git a/api/routes/getSLatest/index.js b/api/routes/getSLatest/index.js new file mode 100644 index 0000000..ea6f30c --- /dev/null +++ b/api/routes/getSLatest/index.js @@ -0,0 +1,29 @@ +const { spawn } = require('child_process'); +const os = require("os"); +const HomeDir = os.userInfo().homedir; + +/* + * Gets the latest SBLOCK from superchain + * LOL + * sorry I was laughing at the term.. superchain + */ +module.exports = (req, res) => { + const command = spawn("ak-find-latest-mined-block.sh"); + response_string = ""; + command.stdout.on("data", data => { + response_string = response_string + data; + }); + + command.stderr.on("data", data => { + console.log(`stderr: ${data}`); + }); + + command.on('error', (error) => { + console.log(`error: ${error.message}`); + }); + + command.on("close", code => { + res.send(JSON.parse(response_string)); + console.log(`child process exited with code ${code}`); + }); +} diff --git a/api/routes/getZChain/index.js b/api/routes/getZChain/index.js new file mode 100644 index 0000000..6aaa5d9 --- /dev/null +++ b/api/routes/getZChain/index.js @@ -0,0 +1,29 @@ +const { spawn } = require('child_process'); + +/* + * Gets the local chain as minified version + * + * Returns: + * - A JSON array representing the nodes' arching-kaos-zchain + * + */ +module.exports = (req, res) => { + const command = spawn("ak-get-chain-minified"); + response_string = ""; + command.stdout.on("data", data => { + response_string = response_string + data; + }); + + command.stderr.on("data", data => { + console.log(`stderr: ${data}`); + }); + + command.on('error', (error) => { + console.log(`error: ${error.message}`); + }); + + command.on("close", code => { + res.send(JSON.parse(response_string)/*.reverse()*/); + console.log(`child process exited with code ${code}`); + }); +}; diff --git a/api/routes/getZLatest/index.js b/api/routes/getZLatest/index.js new file mode 100644 index 0000000..3eeca81 --- /dev/null +++ b/api/routes/getZLatest/index.js @@ -0,0 +1,28 @@ +const { spawn } = require('child_process'); + +/* + * Gets the local latest zblock + * + * Returns: + * - JSON object + * { zlatest: "Qm..." } + * + */ +module.exports = (req, res) => { + const command = spawn("ak-get-latest"); + command.stdout.on("data", data => { + res.send({zlatest:`${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 exited with code ${code}`); + }); +}; \ No newline at end of file diff --git a/api/routes/index.js b/api/routes/index.js new file mode 100644 index 0000000..b771758 --- /dev/null +++ b/api/routes/index.js @@ -0,0 +1,43 @@ +const settings = require('../settings'); +const {Router} = require('express'); +const cors = require('cors'); +const hi = require('./default'); +const seeNSEntriesFile = require('./showNSEntriesFile'); +const seeEntriesFile = require('./showEntriesFile'); +const getSLatest = require('./getSLatest'); +const getZLatest = require('./getZLatest'); +const getSBlock = require('./getSBlock'); +const getZChain = require('./getZChain'); +const receiveZBlock = require('./receiveZBlock'); +const receiveZChain = require('./receiveZChain'); +const corsOptions = { + origin: '*', + optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204 +}; +const router = new Router(); +// Basic route, welcomes and provides the available routes to the visitor +router.route('/').get(hi); + +/* + * Replies with contents of files + * + */ +// Gathered zchain zlatest pairs +router.route(settings.URL_PREFIX+'/seens').get(seeNSEntriesFile); +// Gathered zblocks +router.route(settings.URL_PREFIX+'/see').get(seeEntriesFile); +// Latest known mined block +router.route(settings.URL_PREFIX+'/slatest').get(getSLatest); +// Shows a mined block (provided that /sblock?sblock=SHA512 hash) +router.route(settings.URL_PREFIX+'/sblock').get(getSBlock); +// Outputs node's local chain +router.route(settings.URL_PREFIX+'/zchain').get(getZChain); +// Returns latest zblock from node's local chain +router.route(settings.URL_PREFIX+'/zlatest').get(getZLatest); +// Send a block to the node (zchain block) +router.route(settings.URL_PREFIX+'/sblk').post(receiveZBlock); +// Send a zchain link to the node (refering to a valid zchain out there) +router.route(settings.URL_PREFIX+'/szch').post(receiveZChain); + +router.route('/*').get((req,res)=>{console.log(req.url);res.send({errno:"404"})}); +module.exports = router; diff --git a/api/routes/receiveZBlock/index.js b/api/routes/receiveZBlock/index.js new file mode 100644 index 0000000..3ad6100 --- /dev/null +++ b/api/routes/receiveZBlock/index.js @@ -0,0 +1,28 @@ +/* + * Accepts a ZBLOCK! + * + * Checks: + * 1. Exists, + * 2. Length is 46 bytes, + * 3. Matches regular expression /Qm[A-Za-z0-9]{44}/ + * + * Returns: + * - errno on failure + * - on success the string is processed for further + * validation to the function getvalidity() + * + */ +const getvalidity = require('../../validators/ZblockValidator') +module.exports = (req, res) => { + console.log("okay we got called") + if ( (req.body.zblock) && req.body.zblock.length === 46 ){ + regex= /Qm[A-Za-z0-9]{44}/; + if (regex.test(req.body.zblock)){ + getvalidity(req.body.zblock,res); + } else { + res.send({errno:"Invalid data"}); + } + } else { + res.send({errno:"Invalid data"}); + } +} diff --git a/api/routes/receiveZChain/index.js b/api/routes/receiveZChain/index.js new file mode 100644 index 0000000..85d1854 --- /dev/null +++ b/api/routes/receiveZChain/index.js @@ -0,0 +1,27 @@ +/* + * Accepts a zchain + * + * Checks: + * 1. Exists, + * 2. Length is 62 bytes, + * 3. Matches regular expression /k51qzi5uqu5d[A-Za-z0-9]{50}/ + * + * Returns: + * - errno on failure + * - on success the string is processed for further validation to the + * function getNSvalidity() + * + */ + +module.exports = (req, res) => { + if ( (req.body.zchain) && req.body.zchain.length === 62 ){//&& req.body.block_signature.length === 46){ + regex= /k51qzi5uqu5d[A-Za-z0-9]{50}/ + if (regex.test(req.body.zchain)){ // && regex.test(req.body.block_signature)){ + getNSvalidity(req.body.zchain,res); + } else { + res.send({errno:"Invalid data"}); + } + } else { + res.send({errno:"Invalid data"}); + } +} diff --git a/api/routes/showEntriesFile/index.js b/api/routes/showEntriesFile/index.js new file mode 100644 index 0000000..3dd1c40 --- /dev/null +++ b/api/routes/showEntriesFile/index.js @@ -0,0 +1,14 @@ +const fs = require('fs'); +const config = require('../../config'); + +/* + * Reads ./lol file where the zblocks are saved + * + * Returns: + * - the file, it's already in JSON format. + * + */ +module.exports = (req, res) => { + var data = JSON.parse(fs.readFileSync(config.blocksFile)); + res.send(data); +}; \ No newline at end of file diff --git a/api/routes/showNSEntriesFile/index.js b/api/routes/showNSEntriesFile/index.js new file mode 100644 index 0000000..745ddee --- /dev/null +++ b/api/routes/showNSEntriesFile/index.js @@ -0,0 +1,15 @@ +const fs = require('fs'); +const config = require('../../config'); + +/* + * Reads ./szch file where the zchains with their + * zlatest's are saved. + * + * Returns: + * - the file, it's already in JSON format. + * + */ +module.exports = (req, res) => { + var data = JSON.parse(fs.readFileSync(config.pairsFile)); + res.send(data); +}; \ No newline at end of file -- cgit v1.2.3