async def do_get(self, url = "/labels"): url = 'https://' + self.target_host + url network = Network.get_instance() proxy = network.proxy if network else None async with make_aiohttp_session(proxy) as session: async with session.get(url) as result: return await result.json()
def update(self, network: Network): self.clear() self.addChild = self.addTopLevelItem chains = network.get_blockchains() n_chains = len(chains) for chain_id, interfaces in chains.items(): b = blockchain.blockchains.get(chain_id) if b is None: continue name = b.get_name() if n_chains > 1: x = QTreeWidgetItem( [name + '@%d' % b.get_max_forkpoint(), '%d' % b.height()]) x.setData(0, Qt.UserRole, 1) x.setData(1, Qt.UserRole, b.get_id()) else: x = self for i in interfaces: star = ' *' if i == network.interface else '' item = QTreeWidgetItem([i.host + star, '%d' % i.tip]) item.setData(0, Qt.UserRole, 0) item.setData(1, Qt.UserRole, i.server) x.addChild(item) if n_chains > 1: self.addTopLevelItem(x) x.setExpanded(True) h = self.header() h.setStretchLastSection(False) h.setSectionResizeMode(0, QHeaderView.Stretch) h.setSectionResizeMode(1, QHeaderView.ResizeToContents) super().update()
async def do_scan(self, wallet, cnt): w = wallet try: async with self.wallet_scans_lock: ws = self.wallet_scans.get(w) if not ws or ws.running: return ws.running = True scans = list(ws.scans.values()) n = Network.get_instance() loop = n.asyncio_loop done_cnt = 0 to_scan_cnt = 0 for s in scans: if not s.active: continue to_scan_cnt += len(s.uncompleted) if to_scan_cnt: self.logger.info(f'total count to rescan: {to_scan_cnt}') else: for s in scans: if not s.active: continue async with self.wallet_scans_lock: await loop.run_in_executor(None, s.derive_addrs, cnt) to_scan_cnt += cnt self.logger.info(f'total count to scan: {to_scan_cnt}') if not to_scan_cnt: return async with TaskGroup() as group: for s in scans: for i, addr in s.addrs.items(): if i in s.balances: continue script = bitcoin.address_to_script(addr) scripthash = bitcoin.script_to_scripthash(script) coro = n.get_balance_for_scripthash(scripthash) s.tasks[i] = await group.spawn(coro) while True: task = await group.next_done() if task is None: break done_cnt += 1 await self.on_progress(w, 100 * done_cnt / to_scan_cnt) for s in scans: for i, task in s.tasks.items(): try: balance = task.result() s.balances[i] = balance_total(**balance) if i in s.errors: s.errors.pop(i) except BaseException as e: self.logger.info(f'Exception on get_balance {repr(e)}') s.errors[i] = e s.tasks.clear() await self.on_completed(w) except Exception as e: self.logger.info(f'Exception during wallet_scan: {repr(e)}') await self.on_error(w, e)
def __init__(self, config: 'SimpleConfig', exctype, value, tb): BaseCrashReporter.__init__(self, exctype, value, tb) self.network = Network.get_instance() self.config = config QWidget.__init__(self) self.setWindowTitle('Dash Electrum - ' + _('An Error Occurred')) self.setMinimumSize(600, 300) Logger.__init__(self) main_box = QVBoxLayout() heading = QLabel('<h2>' + BaseCrashReporter.CRASH_TITLE + '</h2>') main_box.addWidget(heading) main_box.addWidget(QLabel(BaseCrashReporter.CRASH_MESSAGE)) main_box.addWidget(QLabel(BaseCrashReporter.REQUEST_HELP_MESSAGE)) collapse_info = QPushButton(_("Show report contents")) collapse_info.clicked.connect( lambda: self.msg_box(QMessageBox.NoIcon, self, _("Report contents"), self.get_report_string(), rich_text=True)) main_box.addWidget(collapse_info) main_box.addWidget(QLabel(BaseCrashReporter.DESCRIBE_ERROR_MESSAGE)) self.description_textfield = QTextEdit() self.description_textfield.setFixedHeight(50) self.description_textfield.setPlaceholderText( _("Do not enter sensitive/private information here. " "The report will be visible on the public issue tracker.")) main_box.addWidget(self.description_textfield) main_box.addWidget(QLabel(BaseCrashReporter.ASK_CONFIRM_SEND)) buttons = QHBoxLayout() report_button = QPushButton(_('Send Bug Report')) report_button.clicked.connect(self.send_report) report_button.setIcon(read_QIcon("tab_send.png")) buttons.addWidget(report_button) never_button = QPushButton(_('Never')) never_button.clicked.connect(self.show_never) buttons.addWidget(never_button) close_button = QPushButton(_('Not Now')) close_button.clicked.connect(self.close) buttons.addWidget(close_button) main_box.addLayout(buttons) self.setLayout(main_box) self.show()
async def do_post(self, url = "/labels", data=None): url = 'https://' + self.target_host + url network = Network.get_instance() proxy = network.proxy if network else None async with make_aiohttp_session(proxy) as session: async with session.post(url, json=data) as result: try: return await result.json() except Exception as e: raise Exception('Could not decode: ' + await result.text()) from e
def comserver_post_notification(self, payload): assert self.is_mobile_paired(), "unexpected mobile pairing error" url = 'https://digitalbitbox.com/smartverification/index.php' key_s = base64.b64decode(self.digitalbitbox_config[ENCRYPTION_PRIVKEY_KEY]) args = 'c=data&s=0&dt=0&uuid=%s&pl=%s' % ( self.digitalbitbox_config[CHANNEL_ID_KEY], EncodeAES_base64(key_s, json.dumps(payload).encode('ascii')).decode('ascii'), ) try: text = Network.send_http_on_proxy('post', url, body=args.encode('ascii'), headers={'content-type': 'application/x-www-form-urlencoded'}) _logger.info(f'digitalbitbox reply from server {text}') except Exception as e: self.handler.show_error(repr(e)) # repr because str(Exception()) == ''
async def add_found(self, wallet): w = wallet try: n = Network.get_instance() loop = n.asyncio_loop async with self.wallet_scans_lock: ws = self.wallet_scans.get(w) if not ws or ws.running: return ws.running = True scans = list(ws.scans.values()) for s in scans: await loop.run_in_executor(None, s.create_new_addrs, w) await self.on_completed(w) except Exception as e: self.logger.info(f'Exception during add_found: {repr(e)}') await self.on_error(w, e)
def __init__(self, app, plugin): super(ScanOverGapPopup, self).__init__() self.app = app self.wallet = w = app.wallet self.plugin = p = plugin self.title = plugin.MSG_TITLE self.network = Network.get_instance() coro = plugin.init_scans(w) fut = asyncio.run_coroutine_threadsafe(coro, self.network.asyncio_loop) fut.result() box = self.box box.add_widget(Factory.WWLabel(text=p.MSG_SCAN_TITLE), len(box.children)) self.scan_list = ScanList(self) self.scan_list.update() box.add_widget(self.scan_list, len(box.children) - 1) self.set_do_scan_btn_txt(p.DEF_SCAN_CNT) self.reset_scans_btn.text = p.MSG_RESET self.add_found_btn.text = p.MSG_ADD_FOUND self.scan_count_msg = p.MSG_SCAN_COUNT self.scan_cnt_sb.min = p.MIN_SCAN_CNT self.scan_cnt_sb.max = p.MAX_SCAN_CNT self.scan_cnt_sb.step = p.DEF_SCAN_CNT self.scan_cnt_sb.val = p.DEF_SCAN_CNT self.scan_progress_msg = p.MSG_PROGRESS ws = self.plugin.wallet_scans.get(w) self.scan_progress_pb.value = ws.progress if ws.running: self.scan_cnt_sb.disabled = True self.do_scan_btn.disabled = True self.reset_scans_btn.disabled = True self.set_add_found_bnt_state() ScanOverGapEvents.bind(on_completed=self.on_completed_kivy) ScanOverGapEvents.bind(on_progress=self.on_progress_kivy) ScanOverGapEvents.bind(on_error=self.on_error_kivy)
import asyncio from electrum_dash.network import filter_protocol, Network from electrum_dash.util import create_and_start_event_loop, log_exceptions from electrum_dash.simple_config import SimpleConfig try: txid = sys.argv[1] except: print("usage: txradar txid") sys.exit(1) config = SimpleConfig() loop, stopping_fut, loop_thread = create_and_start_event_loop() network = Network(config) network.start() @log_exceptions async def f(): try: peers = await network.get_peers() peers = filter_protocol(peers) results = await network.send_multiple_requests( peers, 'blockchain.transaction.get', [txid]) r1, r2 = [], [] for k, v in results.items(): (r1 if not isinstance(v, Exception) else r2).append(k) print(f"Received {len(results)} answers") try:
#!/usr/bin/env python3 # A simple script that connects to a server and displays block headers import time import asyncio from electrum_dash.network import Network from electrum_dash.util import print_msg, json_encode, create_and_start_event_loop, log_exceptions from electrum_dash.simple_config import SimpleConfig config = SimpleConfig() # start network loop, stopping_fut, loop_thread = create_and_start_event_loop() network = Network(config) network.start() # wait until connected while not network.is_connected(): time.sleep(1) print_msg("waiting for network to get connected...") header_queue = asyncio.Queue() @log_exceptions async def f(): try: await network.interface.session.subscribe('blockchain.headers.subscribe', [], header_queue) # 3. wait for results while network.is_connected():
def __init__(self): QThread.__init__(self) Logger.__init__(self) self.network = Network.get_instance()
def update(self, *, network: Network, servers: dict, use_tor: bool): self.clear() # connected servers connected_servers_item = QTreeWidgetItem([_("Connected nodes"), '']) connected_servers_item.setData(0, self.ITEMTYPE_ROLE, self.ItemType.TOPLEVEL) chains = network.get_blockchains() n_chains = len(chains) for chain_id, interfaces in chains.items(): b = blockchain.blockchains.get(chain_id) if b is None: continue name = b.get_name() if n_chains > 1: x = QTreeWidgetItem( [name + '@%d' % b.get_max_forkpoint(), '%d' % b.height()]) x.setData(0, self.ITEMTYPE_ROLE, self.ItemType.CHAIN) x.setData(0, self.CHAIN_ID_ROLE, b.get_id()) else: x = connected_servers_item for i in interfaces: star = ' *' if i == network.interface else '' item = QTreeWidgetItem( [f"{i.server.net_addr_str()}" + star, '%d' % i.tip]) item.setData(0, self.ITEMTYPE_ROLE, self.ItemType.CONNECTED_SERVER) item.setData(0, self.SERVER_ADDR_ROLE, i.server) item.setToolTip(0, str(i.server)) x.addChild(item) if n_chains > 1: connected_servers_item.addChild(x) # disconnected servers disconnected_servers_item = QTreeWidgetItem( [_("Other known servers"), ""]) disconnected_servers_item.setData(0, self.ITEMTYPE_ROLE, self.ItemType.TOPLEVEL) connected_hosts = set( [iface.host for ifaces in chains.values() for iface in ifaces]) protocol = PREFERRED_NETWORK_PROTOCOL for _host, d in sorted(servers.items()): if _host in connected_hosts: continue if _host.endswith('.onion') and not use_tor: continue port = d.get(protocol) if port: server = ServerAddr(_host, port, protocol=protocol) item = QTreeWidgetItem([server.net_addr_str(), ""]) item.setData(0, self.ITEMTYPE_ROLE, self.ItemType.DISCONNECTED_SERVER) item.setData(0, self.SERVER_ADDR_ROLE, server) disconnected_servers_item.addChild(item) self.addTopLevelItem(connected_servers_item) self.addTopLevelItem(disconnected_servers_item) connected_servers_item.setExpanded(True) for i in range(connected_servers_item.childCount()): connected_servers_item.child(i).setExpanded(True) disconnected_servers_item.setExpanded(True) # headers h = self.header() h.setStretchLastSection(False) h.setSectionResizeMode(0, QHeaderView.Stretch) h.setSectionResizeMode(1, QHeaderView.ResizeToContents) super().update()
def is_available(self): if Network.get_instance(): return True self.logger.warning(f'Plugin {self.name} unavailable in offline mode') return False
def __init__(self, window, plugin): WindowModalDialog.__init__(self, window, plugin.MSG_TITLE) self.setMinimumSize(800, 400) self.wallet = w = window.parent().wallet self.plugin = plugin self.config = plugin.config self.network = Network.get_instance() coro = plugin.init_scans(w) fut = asyncio.run_coroutine_threadsafe(coro, self.network.asyncio_loop) fut.result() g = QGridLayout(self) g.addWidget(WWLabel(plugin.MSG_SCAN_TITLE), 0, 0, 1, -1) self.scan_list = ScanListWidget(self) self.scan_list.update() self.scan_list.itemChanged.connect(self.scan_list_item_changed) g.addWidget(self.scan_list, 1, 0, 1, -1) g.addWidget(QLabel(plugin.MSG_SCAN_COUNT), 2, 0) self.scan_cnt_sb = QSpinBox() self.scan_cnt_sb.setRange(plugin.MIN_SCAN_CNT, plugin.MAX_SCAN_CNT) self.scan_cnt_sb.setValue(plugin.DEF_SCAN_CNT) self.scan_cnt_sb.setSingleStep(plugin.DEF_SCAN_CNT) self.scan_cnt_sb.valueChanged.connect(self.on_scan_cnt_value_changed) g.addWidget(self.scan_cnt_sb, 2, 1) self.do_scan_btn = QPushButton() self.set_do_scan_btn_txt(plugin.DEF_SCAN_CNT) self.do_scan_btn.clicked.connect(partial(self.do_scan, w)) g.addWidget(self.do_scan_btn, 2, 3) g.addWidget(QLabel(plugin.MSG_PROGRESS), 2, 4) self.scan_progress_pb = QProgressBar() self.scan_progress_pb.setValue(0) g.addWidget(self.scan_progress_pb, 2, 5) g.setRowStretch(10, 1) hbox = QHBoxLayout() self.reset_scans_btn = QPushButton(plugin.MSG_RESET) self.reset_scans_btn.clicked.connect(partial(self.reset_scans, w)) hbox.addWidget(self.reset_scans_btn) hbox.addStretch(1) self.add_found_btn = QPushButton(plugin.MSG_ADD_FOUND) self.add_found_btn.clicked.connect(partial(self.add_found_coins, w)) hbox.addWidget(self.add_found_btn) hbox.addWidget(CloseButton(self)) g.addLayout(hbox, 11, 0, 1, -1) self.plugin.progress_sig.s.connect(self.on_progress_qt) self.plugin.completed_sig.s.connect(self.on_completed_qt) self.plugin.error_sig.s.connect(self.on_error_qt) self.cleaned_up = False ws = self.plugin.wallet_scans.get(w) self.scan_progress_pb.setValue(ws.progress) if ws.running: self.scan_cnt_sb.setEnabled(False) self.do_scan_btn.setEnabled(False) self.reset_scans_btn.setEnabled(False) self.set_add_found_bnt_state()
def __init__(self, network: Network, config, wizard=False): self.network = network self.config = config self.protocol = None self.tor_proxy = None self.tabs = tabs = QTabWidget() server_tab = QWidget() proxy_tab = QWidget() blockchain_tab = QWidget() tabs.addTab(blockchain_tab, _('Overview')) tabs.addTab(server_tab, _('Server')) tabs.addTab(proxy_tab, _('Proxy')) # server tab grid = QGridLayout(server_tab) grid.setSpacing(8) self.server_host = QLineEdit() self.server_host.setFixedWidth(200) self.server_port = QLineEdit() self.server_port.setFixedWidth(60) self.autoconnect_cb = QCheckBox(_('Select server automatically')) self.autoconnect_cb.setEnabled( self.config.is_modifiable('auto_connect')) self.server_host.editingFinished.connect(self.set_server) self.server_port.editingFinished.connect(self.set_server) self.autoconnect_cb.clicked.connect(self.set_server) self.autoconnect_cb.clicked.connect(self.update) msg = ' '.join([ _("If auto-connect is enabled, Dash Electrum will always use a server that is on the longest blockchain." ), _("If it is disabled, you have to choose a server you want to use. Dash Electrum will warn you if your server is lagging." ) ]) grid.addWidget(self.autoconnect_cb, 0, 0, 1, 3) grid.addWidget(HelpButton(msg), 0, 4) grid.addWidget(QLabel(_('Server') + ':'), 1, 0) grid.addWidget(self.server_host, 1, 1, 1, 2) grid.addWidget(self.server_port, 1, 3) label = _('Server peers') if network.is_connected() else _( 'Default Servers') grid.addWidget(QLabel(label), 2, 0, 1, 5) self.servers_list = ServerListWidget(self) grid.addWidget(self.servers_list, 3, 0, 1, 5) # Proxy tab grid = QGridLayout(proxy_tab) grid.setSpacing(8) # proxy setting self.proxy_cb = QCheckBox(_('Use proxy')) self.proxy_cb.clicked.connect(self.check_disable_proxy) self.proxy_cb.clicked.connect(self.set_proxy) self.proxy_mode = QComboBox() self.proxy_mode.addItems(['SOCKS4', 'SOCKS5']) self.proxy_host = QLineEdit() self.proxy_host.setFixedWidth(200) self.proxy_port = QLineEdit() self.proxy_port.setFixedWidth(60) self.proxy_user = QLineEdit() self.proxy_user.setPlaceholderText(_("Proxy user")) self.proxy_password = QLineEdit() self.proxy_password.setPlaceholderText(_("Password")) self.proxy_password.setEchoMode(QLineEdit.Password) self.proxy_password.setFixedWidth(60) self.proxy_mode.currentIndexChanged.connect(self.set_proxy) self.proxy_host.editingFinished.connect(self.set_proxy) self.proxy_port.editingFinished.connect(self.set_proxy) self.proxy_user.editingFinished.connect(self.set_proxy) self.proxy_password.editingFinished.connect(self.set_proxy) self.proxy_mode.currentIndexChanged.connect( self.proxy_settings_changed) self.proxy_host.textEdited.connect(self.proxy_settings_changed) self.proxy_port.textEdited.connect(self.proxy_settings_changed) self.proxy_user.textEdited.connect(self.proxy_settings_changed) self.proxy_password.textEdited.connect(self.proxy_settings_changed) self.tor_cb = QCheckBox(_("Use Tor Proxy")) self.tor_cb.setIcon(read_QIcon("tor_logo.png")) self.tor_cb.hide() self.tor_cb.clicked.connect(self.use_tor_proxy) self.tor_auto_on = QCheckBox(_("Use Tor Proxy on startup")) self.tor_auto_on.setIcon(read_QIcon("tor_logo.png")) self.tor_auto_on.setChecked(self.config.get('tor_auto_on', True)) self.tor_auto_on.clicked.connect(self.use_tor_auto_on) grid.addWidget(self.tor_cb, 1, 0, 1, 3) grid.addWidget(self.proxy_cb, 2, 0, 1, 3) grid.addWidget( HelpButton( _('Proxy settings apply to all connections: with Dash Electrum servers, but also with third-party services.' )), 2, 4) grid.addWidget(self.proxy_mode, 4, 1) grid.addWidget(self.proxy_host, 4, 2) grid.addWidget(self.proxy_port, 4, 3) grid.addWidget(self.proxy_user, 5, 2) grid.addWidget(self.proxy_password, 5, 3) grid.addWidget(self.tor_auto_on, 6, 0, 1, 3) grid.addWidget( HelpButton( _('During wallet startup try to detect and use Tor Proxy.')), 6, 4) grid.setRowStretch(7, 1) # Blockchain Tab grid = QGridLayout(blockchain_tab) msg = ' '.join([ _("Dash Electrum connects to several nodes in order to download block headers and find out the longest blockchain." ), _("This blockchain is used to verify the transactions sent by your transaction server." ) ]) self.status_label = QLabel('') grid.addWidget(QLabel(_('Status') + ':'), 0, 0) grid.addWidget(self.status_label, 0, 1, 1, 3) grid.addWidget(HelpButton(msg), 0, 4) self.server_label = QLabel('') msg = _( "Dash Electrum sends your wallet addresses to a single server, in order to receive your transaction history." ) grid.addWidget(QLabel(_('Server') + ':'), 1, 0) grid.addWidget(self.server_label, 1, 1, 1, 3) grid.addWidget(HelpButton(msg), 1, 4) self.height_label = QLabel('') msg = _('This is the height of your local copy of the blockchain.') grid.addWidget(QLabel(_('Blockchain') + ':'), 2, 0) grid.addWidget(self.height_label, 2, 1) grid.addWidget(HelpButton(msg), 2, 4) self.split_label = QLabel('') grid.addWidget(self.split_label, 3, 0, 1, 3) self.nodes_list_widget = NodesListWidget(self) grid.addWidget(self.nodes_list_widget, 5, 0, 1, 5) vbox = QVBoxLayout() vbox.addWidget(tabs) self.layout_ = vbox # tor detector self.td = td = TorDetector() td.found_proxy.connect(self.suggest_proxy) td.start() self.fill_in_proxy_settings() self.update()
def recovery(self): network = Network.get_instance() coroutine = account_discovery(network, self.get_account_xpub) return network.run_from_another_thread(coroutine)
#!/usr/bin/env python3 import asyncio from electrum_dash.network import filter_protocol, Network from electrum_dash.util import create_and_start_event_loop, log_exceptions from electrum_dash.blockchain import hash_raw_header loop, stopping_fut, loop_thread = create_and_start_event_loop() network = Network() network.start() @log_exceptions async def f(): try: peers = await network.get_peers() peers = filter_protocol(peers, 's') results = await network.send_multiple_requests(peers, 'blockchain.headers.subscribe', []) for server, header in sorted(results.items(), key=lambda x: x[1].get('height')): height = header.get('height') blockhash = hash_raw_header(header.get('hex')) print(server, height, blockhash) finally: stopping_fut.set_result(1) asyncio.run_coroutine_threadsafe(f(), loop)