generated from nathanwoodburn/python-webserver-template
Compare commits
3 Commits
b6662f400a
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
9a6748b156
|
|||
|
90de6042b1
|
|||
|
eea558361c
|
109
server.py
109
server.py
@@ -198,11 +198,18 @@ def namehash_api(namehash):
|
|||||||
|
|
||||||
@app.route("/api/v1/status")
|
@app.route("/api/v1/status")
|
||||||
def api_status():
|
def api_status():
|
||||||
|
# Count number of names in database
|
||||||
|
db = get_db()
|
||||||
|
cur = db.execute("SELECT COUNT(*) as count FROM names")
|
||||||
|
row = cur.fetchone()
|
||||||
|
name_count = row["count"] if row else 0
|
||||||
|
|
||||||
return jsonify(
|
return jsonify(
|
||||||
{
|
{
|
||||||
"status": "ok",
|
"status": "ok",
|
||||||
"service": "FireExplorer",
|
"service": "FireExplorer",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
|
"names_cached": name_count,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -243,12 +250,102 @@ def hip02(domain: str):
|
|||||||
def covenant_api():
|
def covenant_api():
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
|
|
||||||
return jsonify(
|
if isinstance(data, list):
|
||||||
{
|
covenants = data
|
||||||
"success": True,
|
results = []
|
||||||
"data": data,
|
|
||||||
}
|
# Collect all namehashes needed
|
||||||
)
|
namehashes = set()
|
||||||
|
for cov in covenants:
|
||||||
|
items = cov.get("items", [])
|
||||||
|
if items:
|
||||||
|
namehashes.add(items[0])
|
||||||
|
|
||||||
|
# Batch DB lookup
|
||||||
|
db = get_db()
|
||||||
|
known_names = {}
|
||||||
|
if namehashes:
|
||||||
|
placeholders = ",".join("?" for _ in namehashes)
|
||||||
|
cur = db.execute(
|
||||||
|
f"SELECT namehash, name FROM names WHERE namehash IN ({placeholders})",
|
||||||
|
list(namehashes),
|
||||||
|
)
|
||||||
|
for row in cur:
|
||||||
|
known_names[row["namehash"]] = row["name"]
|
||||||
|
|
||||||
|
# Identify missing namehashes
|
||||||
|
missing_hashes = [nh for nh in namehashes if nh not in known_names]
|
||||||
|
|
||||||
|
# Fetch missing from HSD
|
||||||
|
session = requests.Session()
|
||||||
|
for nh in missing_hashes:
|
||||||
|
try:
|
||||||
|
req = session.get(f"https://hsd.hns.au/api/v1/namehash/{nh}")
|
||||||
|
if req.status_code == 200:
|
||||||
|
name = req.json().get("result")
|
||||||
|
if name:
|
||||||
|
known_names[nh] = name
|
||||||
|
# Update DB
|
||||||
|
db.execute(
|
||||||
|
"INSERT OR REPLACE INTO names (namehash, name) VALUES (?, ?)",
|
||||||
|
(nh, name),
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error fetching namehash {nh}: {e}")
|
||||||
|
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
# Build results
|
||||||
|
for cov in covenants:
|
||||||
|
action = cov.get("action")
|
||||||
|
items = cov.get("items", [])
|
||||||
|
|
||||||
|
if not action:
|
||||||
|
results.append({"covenant": cov, "display": "Unknown"})
|
||||||
|
continue
|
||||||
|
|
||||||
|
display = f"{action}"
|
||||||
|
if items:
|
||||||
|
nh = items[0]
|
||||||
|
if nh in known_names:
|
||||||
|
name = known_names[nh]
|
||||||
|
display += f' <a href="/name/{name}">{name}</a>'
|
||||||
|
|
||||||
|
results.append({"covenant": cov, "display": display})
|
||||||
|
|
||||||
|
return jsonify(results)
|
||||||
|
|
||||||
|
# Get the covenant data
|
||||||
|
action = data.get("action")
|
||||||
|
items = data.get("items", [])
|
||||||
|
|
||||||
|
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' <a href="/name/{name}">{name}</a>'
|
||||||
|
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
|
# endregion
|
||||||
|
|||||||
@@ -469,7 +469,7 @@
|
|||||||
<div style="display: flex; justify-content: space-between; margin-top: 0.5rem; font-size: 0.85rem; color: #b0b0b0;">
|
<div style="display: flex; justify-content: space-between; margin-top: 0.5rem; font-size: 0.85rem; color: #b0b0b0;">
|
||||||
<span>Height: ${coin.height.toLocaleString()}</span>
|
<span>Height: ${coin.height.toLocaleString()}</span>
|
||||||
<span>Coinbase: ${coin.coinbase ? 'Yes' : 'No'}</span>
|
<span>Coinbase: ${coin.coinbase ? 'Yes' : 'No'}</span>
|
||||||
<span>Covenant: ${coin.covenant.action}</span>
|
<span>Covenant: <span data-covenant-action="${coin.covenant.action}" data-covenant="${encodeURIComponent(JSON.stringify(coin.covenant))}">${coin.covenant.action}</span></span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
`).join('')}
|
`).join('')}
|
||||||
@@ -521,7 +521,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="info-item">
|
<div class="info-item">
|
||||||
<label>Covenant:</label>
|
<label>Covenant:</label>
|
||||||
<span>${coin.covenant.action}</span>
|
<span data-covenant-action="${coin.covenant.action}" data-covenant="${encodeURIComponent(JSON.stringify(coin.covenant))}">${coin.covenant.action}</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -696,7 +696,7 @@
|
|||||||
recordDetails += `<br><span style="font-size: 0.9rem;">KeyTag: ${record.keyTag}, Algorithm: ${record.algorithm}, DigestType: ${record.digestType}</span><br><span class="mono" style="font-size: 0.85rem; color: #b0b0b0; word-break: break-all;">${record.digest}</span>`;
|
recordDetails += `<br><span style="font-size: 0.9rem;">KeyTag: ${record.keyTag}, Algorithm: ${record.algorithm}, DigestType: ${record.digestType}</span><br><span class="mono" style="font-size: 0.85rem; color: #b0b0b0; word-break: break-all;">${record.digest}</span>`;
|
||||||
break;
|
break;
|
||||||
case 'TXT':
|
case 'TXT':
|
||||||
recordDetails += `<br><span style="color: #b0b0b0;">${record.txt.map(t => `"${t}"`).join('<br>')}</span>`;
|
recordDetails += `<br><span style="color: #b0b0b0;">${record.txt.join('<br>')}</span>`;
|
||||||
break;
|
break;
|
||||||
case 'GLUE4':
|
case 'GLUE4':
|
||||||
case 'GLUE6':
|
case 'GLUE6':
|
||||||
@@ -779,7 +779,7 @@
|
|||||||
recordDetails += `<br><span style="font-size: 0.9rem;">KeyTag: ${record.keyTag}, Algorithm: ${record.algorithm}</span>`;
|
recordDetails += `<br><span style="font-size: 0.9rem;">KeyTag: ${record.keyTag}, Algorithm: ${record.algorithm}</span>`;
|
||||||
break;
|
break;
|
||||||
case 'TXT':
|
case 'TXT':
|
||||||
recordDetails += `<br><span style="color: #b0b0b0;">${record.txt.map(t => `"${t}"`).join('<br>')}</span>`;
|
recordDetails += `<br><span style="color: #b0b0b0;">${record.txt.join('<br>')}</span>`;
|
||||||
break;
|
break;
|
||||||
case 'GLUE4':
|
case 'GLUE4':
|
||||||
case 'GLUE6':
|
case 'GLUE6':
|
||||||
@@ -861,7 +861,7 @@
|
|||||||
<span class="tx-io-value">${input.coin ? formatValue(input.coin.value) : 'Unknown'}</span>
|
<span class="tx-io-value">${input.coin ? formatValue(input.coin.value) : 'Unknown'}</span>
|
||||||
</div>
|
</div>
|
||||||
<div class="tx-io-address">${input.coin ? input.coin.address : (input.address || 'Unknown')}</div>
|
<div class="tx-io-address">${input.coin ? input.coin.address : (input.address || 'Unknown')}</div>
|
||||||
${input.coin && input.coin.covenant.action !== 'NONE' ? `<div class="tx-covenant">Covenant: ${input.coin.covenant.action}</div>` : ''}
|
${input.coin && input.coin.covenant.action !== 'NONE' ? `<div class="tx-covenant" data-covenant-action="${input.coin.covenant.action}" data-covenant="${encodeURIComponent(JSON.stringify(input.coin.covenant))}">Covenant: ${input.coin.covenant.action}</div>` : ''}
|
||||||
</div>
|
</div>
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
@@ -879,7 +879,7 @@
|
|||||||
<span class="tx-io-value">${formatValue(output.value)}</span>
|
<span class="tx-io-value">${formatValue(output.value)}</span>
|
||||||
</div>
|
</div>
|
||||||
<div class="tx-io-address">${output.address}</div>
|
<div class="tx-io-address">${output.address}</div>
|
||||||
${output.covenant.action !== 'NONE' ? `<div class="tx-covenant">Covenant: ${output.covenant.action}</div>` : ''}
|
${output.covenant.action !== 'NONE' ? `<div class="tx-covenant" data-covenant-action="${output.covenant.action}" data-covenant="${encodeURIComponent(JSON.stringify(output.covenant))}">Covenant: ${output.covenant.action}</div>` : ''}
|
||||||
</div>
|
</div>
|
||||||
`).join('')}
|
`).join('')}
|
||||||
</div>
|
</div>
|
||||||
@@ -919,6 +919,7 @@
|
|||||||
</div>
|
</div>
|
||||||
`;
|
`;
|
||||||
document.body.appendChild(modal);
|
document.body.appendChild(modal);
|
||||||
|
if (!data.error) updateCovenants();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Display helper
|
// Display helper
|
||||||
@@ -940,6 +941,74 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update covenant information from API
|
||||||
|
async function updateCovenants() {
|
||||||
|
const elements = document.querySelectorAll('[data-covenant-action]');
|
||||||
|
const covenantsToFetch = [];
|
||||||
|
const elementMap = new Map(); // Map JSON string -> Array of elements
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
const key = JSON.stringify(covenantData);
|
||||||
|
if (!elementMap.has(key)) {
|
||||||
|
elementMap.set(key, []);
|
||||||
|
covenantsToFetch.push(covenantData);
|
||||||
|
}
|
||||||
|
elementMap.get(key).push(el);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (covenantsToFetch.length === 0) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch(`/api/v1/covenant`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify(covenantsToFetch)
|
||||||
|
});
|
||||||
|
|
||||||
|
if (res.ok) {
|
||||||
|
const results = await res.json();
|
||||||
|
|
||||||
|
for (const result of results) {
|
||||||
|
const key = JSON.stringify(result.covenant);
|
||||||
|
const els = elementMap.get(key);
|
||||||
|
|
||||||
|
if (els) {
|
||||||
|
for (const el of els) {
|
||||||
|
if (el.classList.contains('tx-covenant')) {
|
||||||
|
el.innerHTML = `Covenant: ${result.display}`;
|
||||||
|
} else {
|
||||||
|
el.innerHTML = result.display;
|
||||||
|
}
|
||||||
|
el.dataset.covenantUpdated = "true";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Failed to fetch covenant info:', e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Show loading animation
|
// Show loading animation
|
||||||
function showLoading(elementId) {
|
function showLoading(elementId) {
|
||||||
const element = document.getElementById(elementId);
|
const element = document.getElementById(elementId);
|
||||||
@@ -1031,6 +1100,7 @@
|
|||||||
resultElement.innerHTML = `<div class="error">Error: ${data.error}</div>`;
|
resultElement.innerHTML = `<div class="error">Error: ${data.error}</div>`;
|
||||||
} else {
|
} else {
|
||||||
resultElement.innerHTML = formatTransactionData(data);
|
resultElement.innerHTML = formatTransactionData(data);
|
||||||
|
updateCovenants();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1106,6 +1176,7 @@
|
|||||||
resultElement.innerHTML = `<div class="error">Error: ${data.error}</div>`;
|
resultElement.innerHTML = `<div class="error">Error: ${data.error}</div>`;
|
||||||
} else {
|
} else {
|
||||||
resultElement.innerHTML = formatAddressCoins(data);
|
resultElement.innerHTML = formatAddressCoins(data);
|
||||||
|
updateCovenants();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user