aboutsummaryrefslogtreecommitdiff
path: root/api
diff options
context:
space:
mode:
Diffstat (limited to 'api')
-rw-r--r--api/config.js3
-rwxr-xr-xapi/index.js4
-rw-r--r--api/routes/getAKNSKey/index.js75
-rw-r--r--api/routes/getAKNSKeyFromBase/index.js91
4 files changed, 172 insertions, 1 deletions
diff --git a/api/config.js b/api/config.js
index 3fe7c0d..82f90b8 100644
--- a/api/config.js
+++ b/api/config.js
@@ -25,6 +25,7 @@ const config = {
chunksDir: env.AK_CHUNKSDIR,
leafsDir: env.AK_LEAFSDIR,
mapsDir: env.AK_MAPSDIR,
- printDebug: env.AK_DEBUG
+ printDebug: env.AK_DEBUG,
+ akNSDir: `${env.AK_WORKDIR}/akns`
}
module.exports = config;
diff --git a/api/index.js b/api/index.js
index f2ca713..f22d448 100755
--- a/api/index.js
+++ b/api/index.js
@@ -1,6 +1,8 @@
const http = require("node:http");
const welcomeMessage = require("./routes/default/index.js");
+const getAKNSKey = require("./routes/getAKNSKey/index.js");
+const getAKNSKeyFromBase = require("./routes/getAKNSKeyFromBase/index.js");
const getNodeInfo = require('./routes/getNodeInfo/index.js');
const getPeers = require('./routes/getPeers/index.js');
const getIPFSHash = require('./routes/getIPFSHash/index.js');
@@ -68,6 +70,8 @@ function getRoutes(req, res)
case 'map': getMap(req, res); break;
case 'remote_node_info': getRemoteNodeInfo(req, res); break;
case 'remote_peers': getRemotePeers(req, res); break;
+ case 'ns_get': getAKNSKey(req, res); break;
+ case 'ns_get_base': getAKNSKeyFromBase(req, res); break;
default: notImplemented(req, res);
}
}
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;
+