Skip to content

Commit

Permalink
Broadcast transactions through tor
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtoth committed Jun 21, 2019
1 parent 6de954a commit 30de99a
Show file tree
Hide file tree
Showing 5 changed files with 800 additions and 2 deletions.
5 changes: 5 additions & 0 deletions config.ini_sample
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,15 @@ disable_mempool_fee_histogram = false
# Parameter for broadcasting unconfirmed transactions
# Options are:
# * own-node (broadcast using the connected full node)
# * tor (broadcast to random nodes over tor)
# * system <cmd> %s (save transaction to file, and invoke system command
# with file path as parameter %s)
broadcast_method = own-node

# For tor broadcasting (broadcast_method = tor) configure the tor proxy host and port below
tor_host = localhost
tor_port = 9050


[watch-only-addresses]
#Add individual addresses to this section, for example paper wallets
Expand Down
8 changes: 8 additions & 0 deletions electrumpersonalserver/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,11 @@
parse_electrum_master_public_key,
DeterministicWallet,
)
from electrumpersonalserver.server.socks import (
socksocket,
setdefaultproxy,
PROXY_TYPE_SOCKS5,
)
from electrumpersonalserver.server.peertopeer import (
tor_broadcast_tx,
)
23 changes: 21 additions & 2 deletions electrumpersonalserver/server/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
from ipaddress import ip_network, ip_address
import logging
import tempfile
import threading

from electrumpersonalserver.server.jsonrpc import JsonRpc, JsonRpcError
import electrumpersonalserver.server.hashes as hashes
import electrumpersonalserver.server.merkleproof as merkleproof
import electrumpersonalserver.server.deterministicwallet as deterministicwallet
import electrumpersonalserver.server.transactionmonitor as transactionmonitor
import electrumpersonalserver.server.peertopeer as p2p

SERVER_VERSION_NUMBER = "0.1.7"

Expand Down Expand Up @@ -93,7 +95,7 @@ def on_disconnect(txmonitor):
txmonitor.unsubscribe_all_addresses()

def handle_query(sock, line, rpc, txmonitor, disable_mempool_fee_histogram,
broadcast_method):
broadcast_method, tor_hostport=None):
logger = logging.getLogger('ELECTRUMPERSONALSERVER')
logger.debug("=> " + line)
try:
Expand Down Expand Up @@ -244,6 +246,20 @@ def handle_query(sock, line, rpc, txmonitor, disable_mempool_fee_histogram,
rpc.call("sendrawtransaction", [txhex])
except JsonRpcError as e:
pass
elif broadcast_method == "tor":
# send through tor
TOR_CONNECTIONS = 8
network = "mainnet"
chaininfo = rpc.call("getblockchaininfo", [])
if chaininfo["chain"] == "test":
network = "testnet"
elif chaininfo["chain"] == "regtest":
network = "regtest"
for i in range(TOR_CONNECTIONS):
t = threading.Thread(target=p2p.tor_broadcast_tx,
args=(txhex, tor_hostport, network, rpc,))
t.start()
time.sleep(0.1)
elif broadcast_method.startswith("system "):
with tempfile.NamedTemporaryFile() as fd:
system_line = broadcast_method[7:].replace("%s", fd.name)
Expand Down Expand Up @@ -451,6 +467,9 @@ def run_electrum_server(rpc, txmonitor, config):
"disable_mempool_fee_histogram", fallback=False)
broadcast_method = config.get("electrum-server", "broadcast_method",
fallback="own-node")
tor_host = config.get("electrum-server", "tor_host", fallback="localhost")
tor_port = int(config.get("electrum-server", "tor_port", fallback="9050"))
tor_hostport = (tor_host, tor_port)

server_sock = create_server_socket(hostport)
server_sock.settimeout(poll_interval_listening)
Expand Down Expand Up @@ -491,7 +510,7 @@ def run_electrum_server(rpc, txmonitor, config):
lb = recv_buffer.find(b'\n')
handle_query(sock, line.decode("utf-8"), rpc,
txmonitor, disable_mempool_fee_histogram,
broadcast_method)
broadcast_method, tor_hostport)
except socket.timeout:
on_heartbeat_connected(sock, rpc, txmonitor)
except (IOError, EOFError) as e:
Expand Down
Loading

0 comments on commit 30de99a

Please sign in to comment.