Skip to content

Commit

Permalink
Bugfix: Fix broadcast via block explorer (#1191)
Browse files Browse the repository at this point in the history
Co-authored-by: Kim Neunert <[email protected]>
  • Loading branch information
ben-kaufman and k9ert authored May 27, 2021
1 parent 61f9059 commit bd09743
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 49 deletions.
11 changes: 6 additions & 5 deletions src/cryptoadvance/specter/server_endpoints/wallets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1063,14 +1063,15 @@ def broadcast(wallet_alias):


@wallets_endpoint.route(
"/wallet/<wallet_alias>/broadcast_blockexplorer_tor/", methods=["GET", "POST"]
"/wallet/<wallet_alias>/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:
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<h1>Transaction is Ready to Broadcast</h1>
<div class="flex-center flex-column">
<button id="broadcast_local_btn" class="btn flex-item" style="width: 190px;">Send transaction</button>
<button type="button" id="broadcast_block_explorer_btn" class="btn flex-item {% if specter.chain != 'regtest' %}hidden{% endif %}" style="width: 190px;">Send via block explorer</button>
<button type="button" id="broadcast_block_explorer_btn" class="btn flex-item {% if specter.chain == 'regtest' %}hidden{% endif %}" style="width: 190px;">Send via block explorer</button>
<button type="button" id="broadcast_blockstream_btn" class="btn flex-item hidden" style="width: 190px;">Send via blockstream.info</button>
<button type="button" id="broadcast_mempool_btn" class="btn flex-item hidden" style="width: 190px;">Send via mempool.space</button>
<button type="button" id="broadcast_blockstream_tor_btn" class="btn flex-item hidden" style="width: 190px;">Send via blockstream.info<br>(over Tor hidden service)</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
Expand Down Expand Up @@ -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(
Expand All @@ -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() {
Expand Down

0 comments on commit bd09743

Please sign in to comment.