From de3fab3c70befe6920d5436ce0bef62f3e70ba29 Mon Sep 17 00:00:00 2001 From: kaotisk Date: Tue, 22 Jul 2025 02:29:12 +0300 Subject: [api+ns] ns tool outputs JSON format and it's used for the API --- api/routes/getAKNSKey/index.js | 7 ++----- api/routes/getAKNSKeyFromBase/index.js | 7 ++----- bin/ak-ns | 2 ++ lib/_ak_ns | 17 +++++++++++++++++ 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/api/routes/getAKNSKey/index.js b/api/routes/getAKNSKey/index.js index c5adff0..5e78f81 100644 --- a/api/routes/getAKNSKey/index.js +++ b/api/routes/getAKNSKey/index.js @@ -13,7 +13,7 @@ const config = require("../../config.js"); function replyIfOkay(key, res) { const program = "ak-ns"; - const command = spawn(program, ["-rk", key]); + const command = spawn(program, ["-rj", key]); var buffer = ""; command.stdout.on("data", data => { buffer += data; @@ -29,10 +29,7 @@ function replyIfOkay(key, res) if (code === 0){ buffer = buffer.trim() res.writeHead(200, {'Content-Type': 'application/json'}); - res.end(JSON.stringify({ - key:`${key}`, - resolved:`${buffer}` - })); + res.end(`${buffer}`); } else { res.writeHead(404, {'Content-Type': 'application/json'}); res.end({"error":"unreachable"}); diff --git a/api/routes/getAKNSKeyFromBase/index.js b/api/routes/getAKNSKeyFromBase/index.js index 587314a..7da3d9b 100644 --- a/api/routes/getAKNSKeyFromBase/index.js +++ b/api/routes/getAKNSKeyFromBase/index.js @@ -35,7 +35,7 @@ function replyIfOkay(key, res) const base64String = key; const decodedHexString = decodeBase64ToHex(base64String); formatted_key = decodedHexString.toUpperCase(); - const command = spawn(program, ["-rk", `${formatted_key}`]); + const command = spawn(program, ["-rj", `${formatted_key}`]); var buffer = ""; command.stdout.on("data", data => { @@ -52,10 +52,7 @@ function replyIfOkay(key, res) if (code === 0){ buffer = buffer.trim() res.writeHead(200, {'Content-Type': 'application/json'}); - res.end(JSON.stringify({ - key:`${formatted_key}`, - resolved:`${buffer}` - })); + res.end(`${buffer}`); } else { res.writeHead(404, {'Content-Type': 'application/json'}); res.end(JSON.stringify({error:"unreachable"})); diff --git a/bin/ak-ns b/bin/ak-ns index 4238250..4e08cfc 100755 --- a/bin/ak-ns +++ b/bin/ak-ns @@ -30,6 +30,7 @@ ## -rk, --resolve-key Resolves value from key ## -rp, --resolve-key-proof Resolves value from key and provides ## an akfs_map_v3 to clear signed proof +## -rj, --resolve-key-json Resolves a key to a JSON format ## -p, --publish Publishes value to key ## -pn, --publish2name Publishes value to name ## -pz, --publish-zchain Publishes zchain @@ -56,6 +57,7 @@ then -rn | --resolve-name) shift; _ak_ns_resolve_from_name $1; exit;; -rk | --resolve-key) shift; _ak_ns_resolve_from_key $1; exit;; -rp | --resolve-key-proof) shift; _ak_ns_resolve_from_key_with_proof $1; exit;; + -rj | --resolve-key-json) shift; _ak_ns_resolve_from_key_with_proof_json $1; exit;; -ek | --encode-key) shift; _ak_ns_encode_key $1; exit;; -dk | --decode-key) shift; _ak_ns_decode_key $1; exit;; -p | --publish) shift; _ak_ns_publish $1 $2; exit;; diff --git a/lib/_ak_ns b/lib/_ak_ns index 8afef83..3499232 100755 --- a/lib/_ak_ns +++ b/lib/_ak_ns @@ -99,6 +99,23 @@ function _ak_ns_resolve_from_key_with_proof(){ cat ${AK_NS_DIR}/${key}.map } +function _ak_ns_resolve_from_key_with_proof_json(){ + # $1; exit;; + if [ -z $1 ] || [ ! -n "$1" ] + then + _ak_log_error "No key was given" + exit 1 + fi + key="$1" + json=$(_ak_ns_resolve_from_key_with_proof $key | while read proof resolved; do printf '{"fingerprint":"%s","proof":"%s","resolved":"%s"}' "$1" "$proof" "$resolved"; done) + if [ $? -ne 0 ] + then + _ak_log_error "Something happened" + exit 1 + fi + echo $json +} + function _ak_ns_encode_key(){ # $1; exit;; if [ -z $1 ] || [ ! -n "$1" ] -- cgit v1.2.3