diff options
author | kaotisk <kaotisk@arching-kaos.org> | 2023-03-30 01:09:30 +0300 |
---|---|---|
committer | kaotisk <kaotisk@arching-kaos.org> | 2023-03-30 01:09:30 +0300 |
commit | e2da6d2db20093ebd2a65aad35c9991ab1a02176 (patch) | |
tree | e5c77a8f4a6fd106b13b659e248cab5768d07cdc /api/routes | |
parent | a4901ad47d2945e9a6c6616661840c97ebbf03e7 (diff) | |
download | arching-kaos-tools-e2da6d2db20093ebd2a65aad35c9991ab1a02176.tar.gz arching-kaos-tools-e2da6d2db20093ebd2a65aad35c9991ab1a02176.tar.bz2 arching-kaos-tools-e2da6d2db20093ebd2a65aad35c9991ab1a02176.zip |
Introducing an HTTP JSON API
Diffstat (limited to 'api/routes')
-rw-r--r-- | api/routes/default/index.js | 22 | ||||
-rw-r--r-- | api/routes/getSBlock/index.js | 43 | ||||
-rw-r--r-- | api/routes/getSLatest/index.js | 29 | ||||
-rw-r--r-- | api/routes/getZChain/index.js | 29 | ||||
-rw-r--r-- | api/routes/getZLatest/index.js | 28 | ||||
-rw-r--r-- | api/routes/index.js | 43 | ||||
-rw-r--r-- | api/routes/receiveZBlock/index.js | 28 | ||||
-rw-r--r-- | api/routes/receiveZChain/index.js | 27 | ||||
-rw-r--r-- | api/routes/showEntriesFile/index.js | 14 | ||||
-rw-r--r-- | api/routes/showNSEntriesFile/index.js | 15 |
10 files changed, 278 insertions, 0 deletions
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 |