From 11a6d6c0036bbed213d0d4752d0c6c27aa4d9ebb Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Wed, 5 Feb 2025 18:08:31 +1100 Subject: [PATCH] feat: Initial version --- hsd.py | 43 ++++++++++++++++++++++++ server.py | 79 ++++++++++++++++++++++++++++++++++++++++++++ templates/data.html | 21 ++++++++++++ templates/index.html | 16 ++++++++- 4 files changed, 158 insertions(+), 1 deletion(-) create mode 100644 hsd.py create mode 100644 templates/data.html diff --git a/hsd.py b/hsd.py new file mode 100644 index 0000000..098f59e --- /dev/null +++ b/hsd.py @@ -0,0 +1,43 @@ +import requests +import os +import dotenv + +dotenv.load_dotenv() + +HSD_IP = '127.0.0.1' +HSD_PORT = 12037 +HSD_API_KEY = '' + +if os.getenv("HSD_IP"): + HSD_IP = os.getenv("HSD_IP") + +if os.getenv("HSD_PORT"): + HSD_PORT = os.getenv("HSD_PORT") + +if os.getenv("HSD_API_KEY"): + HSD_API_KEY = os.getenv("HSD_API_KEY") + +HSD_URL = f'http://x:{HSD_API_KEY}@{HSD_IP}:{HSD_PORT}' + +if os.getenv("HSD_URL"): + HSD_URL = os.getenv("HSD_URL") + + +print(f"Using HSD_URL: {HSD_URL}") + +def get_tx(txid): + return requests.get(f"{HSD_URL}/tx/{txid}").json() + +def get_block(blockhash): + return requests.get(f"{HSD_URL}/block/{blockhash}").json() + + +def get_name(name): + return requests.post(HSD_URL, json={"method": "getnameinfo", "params": [name]}).json() + +def get_name_resource(name): + return requests.post(HSD_URL, json={"method": "getnameresource", "params": [name]}).json() + + +def get_address(address): + return requests.get(f"{HSD_URL}/tx/address/{address}").json() \ No newline at end of file diff --git a/server.py b/server.py index 240a1a8..aa8868b 100644 --- a/server.py +++ b/server.py @@ -15,6 +15,7 @@ import json import requests from datetime import datetime import dotenv +import hsd dotenv.load_dotenv() @@ -76,6 +77,33 @@ def wellknown(path): def index(): return render_template("index.html") +@app.route("/name") +def name(): + if request.args.get("name"): + name = request.args.get("name") + data = hsd.get_name(name) + dns = hsd.get_name_resource(name) + data = json.dumps(data, indent=4) + "

" + json.dumps(dns, indent=4) + return render_template("data.html", data=data) + else: + return render_template("index.html") + +@app.route("/tx") +def tx(): + if request.args.get("tx"): + tx = hsd.get_tx(request.args.get("tx")) + return render_template("data.html", data=json.dumps(tx, indent=4)) + else: + return render_template("index.html") + +@app.route("/block") +def block(): + if request.args.get("block"): + block = hsd.get_block(request.args.get("block")) + return render_template("data.html", data=json.dumps(block, indent=4)) + else: + return render_template("index.html") + @app.route("/") def catch_all(path: str): @@ -102,6 +130,57 @@ def catch_all(path: str): # endregion +# region API routes +@app.route("/api/v1/version") +def api_version(): + return jsonify({"version": "1.0.0"}) + +@app.route("/api/v1/tx/") +def api_tx(txid): + tx = hsd.get_tx(txid) + if tx: + return jsonify(tx) + else: + return jsonify({"error": "tx not found"}), 404 + +@app.route("/api/v1/block/") +def api_block(blockhash): + block = hsd.get_block(blockhash) + if block: + return jsonify(block) + else: + return jsonify({"error": "block not found"}), 404 + +@app.route("/api/v1/name/") +def api_name(name): + name = hsd.get_name(name) + if name: + return jsonify(name) + else: + return jsonify({"error": "name not found"}), 404 + +@app.route("/api/v1/name//resource") +@app.route("/api/v1/name//dns") +@app.route("/api/v1/resource/") +@app.route("/api/v1/dns/") +def api_name_resource(name): + name = hsd.get_name_resource(name) + if name: + return jsonify(name) + else: + return jsonify({"error": "name not found"}), 404 + +@app.route("/api/v1/address/
") +def api_address(address): + address = hsd.get_address(address) + if address: + return jsonify(address) + else: + return jsonify({"error": "address not found"}), 404 + + +# endregion + # region Error Catching # 404 catch all @app.errorhandler(404) diff --git a/templates/data.html b/templates/data.html new file mode 100644 index 0000000..2b3f1a3 --- /dev/null +++ b/templates/data.html @@ -0,0 +1,21 @@ + + + + + + + Nathan.Woodburn/ + + + + + +
+
+

RESPONSE

+
{{ data | safe }}
+
+ + + + \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index bb349f8..5563ae7 100644 --- a/templates/index.html +++ b/templates/index.html @@ -12,8 +12,22 @@
-

Nathan.Woodburn/

+

Nathan.Woodburn/ EXPLORER ALPHA

+ +
+ + +
+
+ + +
+
+ + +
+ \ No newline at end of file