diff --git a/server.py b/server.py index d337b56..4ec7ce5 100644 --- a/server.py +++ b/server.py @@ -242,13 +242,37 @@ def hip02(domain: str): @app.route("/api/v1/covenant", methods=["POST"]) def covenant_api(): data = request.get_json() + # Get the covenant data + action = data.get("action") + items = data.get("items", []) - return jsonify( - { - "success": True, - "data": data, - } - ) + if not action: + return jsonify({"success": False, "data": data}) + + display = f"{action}" + if len(items) > 0: + name_hash = items[0] + # Lookup name from database + db = get_db() + cur = db.execute("SELECT * FROM names WHERE namehash = ?", (name_hash,)) + row = cur.fetchone() + if row: + name = row["name"] + display += f' {name}' + else: + req = requests.get(f"https://hsd.hns.au/api/v1/namehash/{name_hash}") + if req.status_code == 200: + name = req.json().get("result") + if name: + display += f" {name}" + # Insert into database + db.execute( + "INSERT OR REPLACE INTO names (namehash, name) VALUES (?, ?)", + (name_hash, name), + ) + db.commit() + + return jsonify({"success": True, "data": data, "display": display}) # endregion diff --git a/templates/index.html b/templates/index.html index 9dbe6eb..603514c 100644 --- a/templates/index.html +++ b/templates/index.html @@ -469,7 +469,7 @@
Height: ${coin.height.toLocaleString()} Coinbase: ${coin.coinbase ? 'Yes' : 'No'} - Covenant: ${coin.covenant.action} + Covenant: ${coin.covenant.action}
`).join('')} @@ -521,7 +521,7 @@
- ${coin.covenant.action} + ${coin.covenant.action}
@@ -861,7 +861,7 @@ ${input.coin ? formatValue(input.coin.value) : 'Unknown'}
${input.coin ? input.coin.address : (input.address || 'Unknown')}
- ${input.coin && input.coin.covenant.action !== 'NONE' ? `
Covenant: ${input.coin.covenant.action}
` : ''} + ${input.coin && input.coin.covenant.action !== 'NONE' ? `
Covenant: ${input.coin.covenant.action}
` : ''} `; } @@ -879,7 +879,7 @@ ${formatValue(output.value)}
${output.address}
- ${output.covenant.action !== 'NONE' ? `
Covenant: ${output.covenant.action}
` : ''} + ${output.covenant.action !== 'NONE' ? `
Covenant: ${output.covenant.action}
` : ''} `).join('')} @@ -919,6 +919,7 @@ `; document.body.appendChild(modal); + if (!data.error) updateCovenants(); } // Display helper @@ -940,6 +941,66 @@ } } + // Update covenant information from API + async function updateCovenants() { + const elements = document.querySelectorAll('[data-covenant-action]'); + const cache = {}; + + for (const el of elements) { + const action = el.dataset.covenantAction; + if (action === 'NONE') continue; + + // Skip if already updated + if (el.dataset.covenantUpdated) continue; + + // Get full covenant data + let covenantData = null; + if (el.dataset.covenant) { + try { + covenantData = JSON.parse(decodeURIComponent(el.dataset.covenant)); + } catch (e) { + console.error('Failed to parse covenant data:', e); + } + } + + if (!covenantData) continue; + + // Create a cache key based on the full covenant data + const cacheKey = JSON.stringify(covenantData); + + let display = action; + if (cache[cacheKey]) { + display = cache[cacheKey]; + } else { + try { + const res = await fetch(`/api/v1/covenant`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify(covenantData) + }); + + if (res.ok) { + const data = await res.json(); + display = data.display || action; + cache[cacheKey] = display; + } + } catch (e) { + console.error('Failed to fetch covenant info:', e); + } + } + + // Check if it's the .tx-covenant div which includes "Covenant: " text + if (el.classList.contains('tx-covenant')) { + el.innerHTML = `Covenant: ${display}`; + } else { + el.textContent = display; + } + el.dataset.covenantUpdated = "true"; + } + } + // Show loading animation function showLoading(elementId) { const element = document.getElementById(elementId); @@ -1031,6 +1092,7 @@ resultElement.innerHTML = `
Error: ${data.error}
`; } else { resultElement.innerHTML = formatTransactionData(data); + updateCovenants(); } } @@ -1106,6 +1168,7 @@ resultElement.innerHTML = `
Error: ${data.error}
`; } else { resultElement.innerHTML = formatAddressCoins(data); + updateCovenants(); } }