diff --git a/server.py b/server.py index 9eb404c..2dbdd80 100644 --- a/server.py +++ b/server.py @@ -326,6 +326,89 @@ def api_nameresource(name): except requests.RequestException as e: return jsonify({"error": str(e)}), 500 +@app.route("/api/v1/namesummary/") +def api_namesummary(name): + """ + This endpoint returns a summary of a specific name. + """ + try: + summary = { + "name": name, + "value": None, + "blocksUntilExpire": None, + "owner": None, + "hash": None, + "state": None, + "resources" : [] + } + + url = f"{HSD_URL()}/" + data = { + "method": "getnameinfo", + "params": [name] + } + response = requests.post(url, json=data) + if response.status_code != 200: + return jsonify({"error": "Failed to get name summary", "status_code": response.status_code}), response.status_code + + # Check if error is null + if 'error' in response.json() and response.json()['error'] is not None: + return jsonify({"error": response.json()['error']}), 400 + + # Check if result is empty + if 'result' not in response.json() or not response.json()['result']: + return jsonify({"error": "Name summary not found"}), 404 + name_info = response.json()['result'] + + if 'info' in name_info: + summary["hash"] = name_info['info'].get('nameHash', None) + summary["state"] = name_info['info'].get('state', None) + + summary["value"] = name_info['info'].get('value', None) + # Convert from satoshis to HNS + if summary["value"] is not None: + summary["value"] = summary["value"] / 1000000 + + if 'stats' in name_info['info']: + summary["blocksUntilExpire"] = name_info['info']['stats'].get('blocksUntilExpire', None) + + if 'owner' in name_info['info']: + owner_hash = name_info['info']['owner'].get('hash', None) + owner_index = name_info['info']['owner'].get('index', None) + if owner_hash is not None and owner_index is not None: + # Fetch the owner address using the coin endpoint + owner_response = requests.get(f"{HSD_URL()}/coin/{owner_hash}/{owner_index}") + if owner_response.status_code == 200: + owner_data = owner_response.json() + summary["owner"] = owner_data.get('address', None) + + + # Get resources + data = { + "method": "getnameresource", + "params": [name] + } + response = requests.post(url, json=data) + if response.status_code == 200: + # Check if error is null + if 'error' in response.json() and response.json()['error'] is not None: + return jsonify({"error": response.json()['error']}), 400 + + # Check if result is empty + if 'result' not in response.json() or not response.json()['result']: + return jsonify({"error": "Name resources not found"}), 404 + + resources = response.json()['result'] + if isinstance(resources, list): + summary["resources"] = resources + else: + summary["resources"].append(resources) + + return jsonify(summary), 200 + + + except requests.RequestException as e: + return jsonify({"error": str(e)}), 500 @app.route("/api/v1/help") def api_help(): @@ -346,6 +429,7 @@ def api_help(): {"endpoint": "/api/v1/namehash/", "description": "Get name by hash"}, {"endpoint": "/api/v1/nameresource/", "description": "Get name resource"}, {"endpoint": "/api/v1/help", "description": "List all API endpoints"}, + {"endpoint": "/api/v1/namesummary/", "description": "Get a summary of a name"}, ] return jsonify({"api": api_endpoints}), 200