From bd09743fa829e60e13d45f96c2d66352535e4a27 Mon Sep 17 00:00:00 2001 From: benk10 Date: Thu, 27 May 2021 14:18:07 +0300 Subject: [PATCH] Bugfix: Fix broadcast via block explorer (#1191) Co-authored-by: Kim Neunert --- .../specter/server_endpoints/wallets.py | 11 ++-- .../includes/overlay/broadcast_tx.jinja | 2 +- .../templates/includes/sidebar/sidebar.jinja | 2 +- .../send/sign/wallet_send_sign_psbt.jinja | 50 +++---------------- 4 files changed, 16 insertions(+), 49 deletions(-) diff --git a/src/cryptoadvance/specter/server_endpoints/wallets.py b/src/cryptoadvance/specter/server_endpoints/wallets.py index dc6ef8ffc4..b0cc07c0cf 100644 --- a/src/cryptoadvance/specter/server_endpoints/wallets.py +++ b/src/cryptoadvance/specter/server_endpoints/wallets.py @@ -1063,14 +1063,15 @@ def broadcast(wallet_alias): @wallets_endpoint.route( - "/wallet//broadcast_blockexplorer_tor/", methods=["GET", "POST"] + "/wallet//broadcast_blockexplorer/", methods=["GET", "POST"] ) @login_required -def broadcast_blockexplorer_tor(wallet_alias): +def broadcast_blockexplorer(wallet_alias): wallet = app.specter.wallet_manager.get_by_alias(wallet_alias) if request.method == "POST": tx = request.form.get("tx") explorer = request.form.get("explorer") + use_tor = request.form.get("use_tor", "true") == "true" res = wallet.rpc.testmempoolaccept([tx])[0] if res["allowed"]: try: @@ -1088,15 +1089,15 @@ def broadcast_blockexplorer_tor(wallet_alias): error=f"Failed to broadcast transaction. Network not supported.", ) if explorer == "mempool": - explorer = "MEMPOOL_SPACE_ONION" + explorer = f"MEMPOOL_SPACE{'_ONION' if use_tor else ''}" elif explorer == "blockstream": - explorer = "BLOCKSTREAM_INFO_ONION" + explorer = f"BLOCKSTREAM_INFO{'_ONION' if use_tor else ''}" else: return jsonify( success=False, error=f"Failed to broadcast transaction. Block explorer not supported.", ) - requests_session = app.specter.requests_session(force_tor=True) + requests_session = app.specter.requests_session(force_tor=use_tor) requests_session.post( f"{app.config['EXPLORERS_LIST'][explorer]['url']}{url_network}api/tx", data=tx, diff --git a/src/cryptoadvance/specter/templates/includes/overlay/broadcast_tx.jinja b/src/cryptoadvance/specter/templates/includes/overlay/broadcast_tx.jinja index 7c5293a0ed..35a0dbfcf0 100644 --- a/src/cryptoadvance/specter/templates/includes/overlay/broadcast_tx.jinja +++ b/src/cryptoadvance/specter/templates/includes/overlay/broadcast_tx.jinja @@ -2,7 +2,7 @@

Transaction is Ready to Broadcast

- + diff --git a/src/cryptoadvance/specter/templates/includes/sidebar/sidebar.jinja b/src/cryptoadvance/specter/templates/includes/sidebar/sidebar.jinja index ffab907d83..2b4f7ea8de 100644 --- a/src/cryptoadvance/specter/templates/includes/sidebar/sidebar.jinja +++ b/src/cryptoadvance/specter/templates/includes/sidebar/sidebar.jinja @@ -243,7 +243,7 @@ } else { document.getElementById("wallets-loading-indicator").classList.add("hidden"); {% if not specter.wallet_manager.failed_load_wallets %} - if ('{{wallet_alias}}') { + if ('{{wallet_alias}}' && window.location.href.endsWith('history/')) { document.getElementById("wallets-loading-done").classList.remove("hidden"); } {% endif %} diff --git a/src/cryptoadvance/specter/templates/wallet/send/sign/wallet_send_sign_psbt.jinja b/src/cryptoadvance/specter/templates/wallet/send/sign/wallet_send_sign_psbt.jinja index 7c970eccf9..9c26629275 100644 --- a/src/cryptoadvance/specter/templates/wallet/send/sign/wallet_send_sign_psbt.jinja +++ b/src/cryptoadvance/specter/templates/wallet/send/sign/wallet_send_sign_psbt.jinja @@ -297,22 +297,22 @@ if ("{{ specter.chain }}" != "regtest") { document.getElementById("broadcast_blockstream_btn").addEventListener("click", async function(e) { e.preventDefault(); - broadcastBlockExplorer(raw, 'blockstream'); + broadcastBlockExplorer(raw, 'blockstream', false); }); document.getElementById("broadcast_mempool_btn").addEventListener("click", async function(e) { e.preventDefault(); - broadcastBlockExplorer(raw, 'mempool'); + broadcastBlockExplorer(raw, 'mempool', false); }); document.getElementById("broadcast_blockstream_tor_btn").addEventListener("click", async function(e) { e.preventDefault(); - broadcastBlockExplorerTor(raw, 'blockstream'); + broadcastBlockExplorer(raw, 'blockstream', true); }); document.getElementById("broadcast_mempool_tor_btn").addEventListener("click", async function(e) { e.preventDefault(); - broadcastBlockExplorerTor(raw, 'mempool'); + broadcastBlockExplorer(raw, 'mempool', true); }); } @@ -432,14 +432,16 @@ } } - async function broadcastBlockExplorerTor(tx, explorer) { + async function broadcastBlockExplorer(tx, explorer, useTor) { showNotification("Sending transaction...", 0); - let url="{{ url_for('wallets_endpoint.broadcast_blockexplorer_tor', wallet_alias=wallet.alias) }}"; + let url = "{{ url_for('wallets_endpoint.broadcast_blockexplorer', wallet_alias=wallet.alias) }}"; + var formData = new FormData(); formData.append("csrf_token", "{{ csrf_token() }}"); formData.append("tx", tx); formData.append("explorer", explorer); + formData.append("use_tor", useTor); try { const response = await fetch( @@ -463,42 +465,6 @@ } } - function broadcastBlockExplorer(tx, explorer) { - showNotification("Sending transaction via " + explorer + "...", 0); - // TODO: Use fetch instead of XMLHttpRequest - let url; - if (explorer == "blockstream") { - url = "https://blockstream.info/" - } else if (explorer == "mempool") { - url = "https://mempool.space/" - } - if ("{{ specter.chain }}" == "main") { - url += "api/tx"; - } else if("{{specter.chain}}" == "liquidv1"){ - url += "liquid/api/tx"; - } else if("{{specter.chain}}" == "signet"){ - url += "signet/api/tx"; - } else { - url += "testnet/api/tx"; - } - - var xmlHttp = new XMLHttpRequest(); - xmlHttp.onreadystatechange = function() { - if(xmlHttp.readyState === 4 && xmlHttp.status === 200) { - var deletePSBTBtn = document.getElementById("deletepsbt_btn"); - deletePSBTBtn.click(); - showNotification("Transaction was sent successfully!"); - } else { - if(xmlHttp.readyState === 4){ - showError("Server failed to broadcast transactions!\n" + xmlHttp.response); - } - } - } - - xmlHttp.open("POST", url, true); // true for asynchronous - xmlHttp.send(tx); - } - // ================== Signers various device support ================== {% if 'raw' not in psbt %} document.addEventListener("DOMContentLoaded", function() {