From 1c51e97354b62b69c00903b9148c122607bd45cd Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Fri, 21 Nov 2025 13:11:44 +1100 Subject: [PATCH] feat: Add database for namehash caching --- .gitignore | 1 + server.py | 76 +++++++++++++++++++++++++++++++++++++++++++- templates/index.html | 26 +++++++++------ 3 files changed, 92 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 7d847cc..6fc113c 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ __pycache__/ .env .vs/ .venv/ +fireexplorer.db diff --git a/server.py b/server.py index 4d35848..d94a1a0 100644 --- a/server.py +++ b/server.py @@ -5,9 +5,12 @@ from flask import ( send_from_directory, send_file, jsonify, + g, + request, ) import os import requests +import sqlite3 from datetime import datetime import dotenv from tools import hip2, wallet_txt @@ -15,6 +18,39 @@ from tools import hip2, wallet_txt dotenv.load_dotenv() app = Flask(__name__) +DATABASE = "fireexplorer.db" + + +def get_db(): + db = getattr(g, "_database", None) + if db is None: + db = g._database = sqlite3.connect(DATABASE) + db.row_factory = sqlite3.Row + return db + + +@app.teardown_appcontext +def close_connection(exception): + db = getattr(g, "_database", None) + if db is not None: + db.close() + + +def init_db(): + with app.app_context(): + db = get_db() + db.execute( + """ + CREATE TABLE IF NOT EXISTS names ( + namehash TEXT PRIMARY KEY, + name TEXT NOT NULL + ) + """ + ) + db.commit() + + +init_db() def find(name, path): @@ -133,6 +169,32 @@ def catch_all(path: str): return render_template("404.html"), 404 +# endregion + + +# region API routes +@app.route("/api/v1/namehash/") +def namehash_api(namehash): + db = get_db() + cur = db.execute("SELECT * FROM names WHERE namehash = ?", (namehash,)) + row = cur.fetchone() + if row is None: + # Get namehash from hsd.hns.au + req = requests.get(f"https://hsd.hns.au/api/v1/namehash/{namehash}") + if req.status_code == 200: + name = req.json().get("result") + if not name: + return jsonify({"name": "Error", "namehash": namehash}) + # Insert into database + db.execute( + "INSERT OR REPLACE INTO names (namehash, name) VALUES (?, ?)", + (namehash, name), + ) + db.commit() + return jsonify({"name": name, "namehash": namehash}) + return jsonify(dict(row)) + + @app.route("/api/v1/status") def api_status(): return jsonify( @@ -176,6 +238,18 @@ def hip02(domain: str): ) +@app.route("/api/v1/covenant", methods=["POST"]) +def covenant_api(): + data = request.get_json() + + return jsonify( + { + "success": True, + "data": data, + } + ) + + # endregion @@ -188,4 +262,4 @@ def not_found(e): # endregion if __name__ == "__main__": - app.run(debug=True, port=5000, host="0.0.0.0") + app.run(debug=True, port=5000, host="127.0.0.1") diff --git a/templates/index.html b/templates/index.html index 042a506..9dbe6eb 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1178,17 +1178,23 @@ } showLoading('name-result'); - const data = await apiCall(`namehash/${nameHash}`); - // Check if result is valid and redirect to name page - const resultElement = document.getElementById('name-result'); - if (data.error) { - resultElement.innerHTML = `
Error: ${data.error.message ? data.error.message : "Failed to lookup hash"}
`; - } else if (data.result && typeof data.result === 'string') { - // Valid name found, redirect to name page - window.location.href = `/name/${data.result}`; - } else { - resultElement.innerHTML = `
No name found for this hash
`; + try { + const response = await fetch(`/api/v1/namehash/${nameHash}`); + const data = await response.json(); + + const resultElement = document.getElementById('name-result'); + if (data.error) { + resultElement.innerHTML = `
Error: ${data.error}
`; + } else if (data.name) { + // Valid name found, redirect to name page + window.location.href = `/name/${data.name}`; + } else { + resultElement.innerHTML = `
No name found for this hash
`; + } + } catch (e) { + const resultElement = document.getElementById('name-result'); + resultElement.innerHTML = `
Error: ${e.message}
`; } }