Browse Source

trustedcoin: clean up imports, flake warnings

master
Sander van Grieken 1 year ago
parent
commit
974ad64688
No known key found for this signature in database
GPG Key ID: 9BCF8209EA402EBA
  1. 1
      electrum/plugins/trustedcoin/cmdline.py
  2. 7
      electrum/plugins/trustedcoin/common_qt.py
  3. 5
      electrum/plugins/trustedcoin/qml.py
  4. 26
      electrum/plugins/trustedcoin/qt.py
  5. 37
      electrum/plugins/trustedcoin/trustedcoin.py

1
electrum/plugins/trustedcoin/cmdline.py

@ -24,7 +24,6 @@
# SOFTWARE. # SOFTWARE.
from electrum.i18n import _ from electrum.i18n import _
from electrum.plugin import hook
from .trustedcoin import TrustedCoinPlugin from .trustedcoin import TrustedCoinPlugin

7
electrum/plugins/trustedcoin/common_qt.py

@ -1,7 +1,6 @@
import threading import threading
import socket import socket
import base64 import base64
import sys
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from PyQt6.QtCore import pyqtSignal, pyqtProperty, pyqtSlot from PyQt6.QtCore import pyqtSignal, pyqtProperty, pyqtSlot
@ -83,7 +82,7 @@ class TrustedcoinPluginQObject(PluginQObject):
return self._billingModel return self._billingModel
def updateBillingInfo(self, wallet): def updateBillingInfo(self, wallet):
billingModel = [] billing_model = []
price_per_tx = wallet.price_per_tx price_per_tx = wallet.price_per_tx
for k, v in sorted(price_per_tx.items()): for k, v in sorted(price_per_tx.items()):
@ -94,9 +93,9 @@ class TrustedcoinPluginQObject(PluginQObject):
'value': k, 'value': k,
'sats_per_tx': v / k 'sats_per_tx': v / k
} }
billingModel.append(item) billing_model.append(item)
self._billingModel = billingModel self._billingModel = billing_model
self.billingModelChanged.emit() self.billingModelChanged.emit()
@pyqtSlot() @pyqtSlot()

5
electrum/plugins/trustedcoin/qml.py

@ -18,6 +18,11 @@ if TYPE_CHECKING:
class Plugin(TrustedCoinPlugin): class Plugin(TrustedCoinPlugin):
def __init__(self, *args): def __init__(self, *args):
super().__init__(*args) super().__init__(*args)
self._app = None
self.so = None
self.on_success = None
self.on_failure = None
self.tx = None
@hook @hook
def load_wallet(self, wallet: 'Abstract_Wallet'): def load_wallet(self, wallet: 'Abstract_Wallet'):

26
electrum/plugins/trustedcoin/qt.py

@ -24,23 +24,22 @@
# SOFTWARE. # SOFTWARE.
from functools import partial from functools import partial
import threading
import os import os
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from PyQt6.QtGui import QPixmap, QMovie, QColor from PyQt6.QtGui import QPixmap, QMovie, QColor
from PyQt6.QtCore import QObject, pyqtSignal, QSize, Qt from PyQt6.QtCore import QObject, pyqtSignal, QSize, Qt
from PyQt6.QtWidgets import (QTextEdit, QVBoxLayout, QLabel, QGridLayout, QHBoxLayout, from PyQt6.QtWidgets import (QTextEdit, QVBoxLayout, QLabel, QGridLayout, QHBoxLayout,
QRadioButton, QCheckBox, QLineEdit, QPushButton, QWidget) QRadioButton, QCheckBox, QPushButton, QWidget)
from electrum.i18n import _ from electrum.i18n import _
from electrum.plugin import hook from electrum.plugin import hook
from electrum.util import is_valid_email, InvalidPassword from electrum.util import InvalidPassword
from electrum.logging import Logger, get_logger from electrum.logging import Logger, get_logger
from electrum import keystore from electrum import keystore
from electrum.gui.qt.util import (read_QIcon, WindowModalDialog, WaitingDialog, OkButton, from electrum.gui.qt.util import (WindowModalDialog, WaitingDialog, OkButton, CancelButton, Buttons, icon_path,
CancelButton, Buttons, icon_path, internal_plugin_icon_path, WWLabel, CloseButton, ColorScheme, internal_plugin_icon_path, WWLabel, CloseButton, ColorScheme,
ChoiceWidget, PasswordLineEdit, char_width_in_lineedit) ChoiceWidget, PasswordLineEdit, char_width_in_lineedit)
from electrum.gui.qt.qrcodewidget import QRCodeWidget from electrum.gui.qt.qrcodewidget import QRCodeWidget
from electrum.gui.qt.amountedit import AmountEdit from electrum.gui.qt.amountedit import AmountEdit
@ -50,7 +49,7 @@ from electrum.gui.qt.wizard.wizard import WizardComponent
from electrum.gui.qt.util import read_QIcon_from_bytes from electrum.gui.qt.util import read_QIcon_from_bytes
from .common_qt import TrustedcoinPluginQObject from .common_qt import TrustedcoinPluginQObject
from .trustedcoin import TrustedCoinPlugin, server, DISCLAIMER from .trustedcoin import TrustedCoinPlugin, DISCLAIMER
if TYPE_CHECKING: if TYPE_CHECKING:
from electrum.gui.qt.main_window import ElectrumWindow from electrum.gui.qt.main_window import ElectrumWindow
@ -116,7 +115,7 @@ class Plugin(TrustedCoinPlugin):
def auth_dialog(self, window): def auth_dialog(self, window):
d = WindowModalDialog(window, _("Authorization")) d = WindowModalDialog(window, _("Authorization"))
vbox = QVBoxLayout(d) vbox = QVBoxLayout(d)
pw = AmountEdit(None, is_int = True) pw = AmountEdit(None, is_int=True)
msg = _('Please enter your Google Authenticator code') msg = _('Please enter your Google Authenticator code')
vbox.addWidget(QLabel(msg)) vbox.addWidget(QLabel(msg))
grid = QGridLayout() grid = QGridLayout()
@ -139,6 +138,7 @@ class Plugin(TrustedCoinPlugin):
def waiting_dialog_for_billing_info(self, window, *, on_finished=None): def waiting_dialog_for_billing_info(self, window, *, on_finished=None):
def task(): def task():
return self.request_billing_info(window.wallet, suppress_connection_error=False) return self.request_billing_info(window.wallet, suppress_connection_error=False)
def on_error(exc_info): def on_error(exc_info):
e = exc_info[1] e = exc_info[1]
window.show_error("{header}\n{exc}\n\n{tor}" window.show_error("{header}\n{exc}\n\n{tor}"
@ -216,6 +216,7 @@ class Plugin(TrustedCoinPlugin):
grid.addWidget(QLabel(window.format_amount(v/k) + ' ' + window.base_unit() + "/tx"), i, 1) grid.addWidget(QLabel(window.format_amount(v/k) + ' ' + window.base_unit() + "/tx"), i, 1)
b = QRadioButton() b = QRadioButton()
b.setChecked(k == n_prepay) b.setChecked(k == n_prepay)
def on_click(b, k): def on_click(b, k):
self.config.PLUGIN_TRUSTEDCOIN_NUM_PREPAY = k self.config.PLUGIN_TRUSTEDCOIN_NUM_PREPAY = k
b.clicked.connect(partial(on_click, k=k)) b.clicked.connect(partial(on_click, k=k))
@ -374,12 +375,11 @@ class WCTerms(WizardComponent):
def __init__(self, parent, wizard): def __init__(self, parent, wizard):
WizardComponent.__init__(self, parent, wizard, title=_('Terms and conditions')) WizardComponent.__init__(self, parent, wizard, title=_('Terms and conditions'))
self._has_tos = False self._has_tos = False
def on_ready(self):
self.tos_e = TOS() self.tos_e = TOS()
self.tos_e.setReadOnly(True) self.tos_e.setReadOnly(True)
self.layout().addWidget(self.tos_e) self.layout().addWidget(self.tos_e)
def on_ready(self):
self.fetch_terms_and_conditions() self.fetch_terms_and_conditions()
def fetch_terms_and_conditions(self): def fetch_terms_and_conditions(self):
@ -580,6 +580,7 @@ class WCKeepDisable(WizardComponent):
class WCContinueOnline(WizardComponent): class WCContinueOnline(WizardComponent):
def __init__(self, parent, wizard): def __init__(self, parent, wizard):
WizardComponent.__init__(self, parent, wizard, title=_('Continue Online')) WizardComponent.__init__(self, parent, wizard, title=_('Continue Online'))
self.cb_online = QCheckBox(_('Go online to complete wallet creation'))
def on_ready(self): def on_ready(self):
path = os.path.join(os.path.dirname(self.wizard._daemon.config.get_wallet_path()), self.wizard_data['wallet_name']) path = os.path.join(os.path.dirname(self.wizard._daemon.config.get_wallet_path()), self.wizard_data['wallet_name'])
@ -596,7 +597,6 @@ class WCContinueOnline(WizardComponent):
self.layout().addWidget(WWLabel('\n\n'.join(msg))) self.layout().addWidget(WWLabel('\n\n'.join(msg)))
self.layout().addStretch(1) self.layout().addStretch(1)
self.cb_online = QCheckBox(_('Go online to complete wallet creation'))
self.cb_online.setChecked(True) self.cb_online.setChecked(True)
self.cb_online.stateChanged.connect(self.on_updated) self.cb_online.stateChanged.connect(self.on_updated)
# self.cb_online.setToolTip(_("Check this box to request a new secret. You will need to retype your seed.")) # self.cb_online.setToolTip(_("Check this box to request a new secret. You will need to retype your seed."))
@ -613,8 +613,6 @@ class WCContinueOnline(WizardComponent):
class WCKeystorePassword(WizardComponent): class WCKeystorePassword(WizardComponent):
def __init__(self, parent, wizard): def __init__(self, parent, wizard):
WizardComponent.__init__(self, parent, wizard, title=_('Unlock Keystore')) WizardComponent.__init__(self, parent, wizard, title=_('Unlock Keystore'))
def on_ready(self):
self.layout().addStretch(1) self.layout().addStretch(1)
hbox2 = QHBoxLayout() hbox2 = QHBoxLayout()
@ -627,11 +625,11 @@ class WCKeystorePassword(WizardComponent):
hbox2.addWidget(self.pw_e) hbox2.addWidget(self.pw_e)
hbox2.addStretch(1) hbox2.addStretch(1)
self.layout().addLayout(hbox2) self.layout().addLayout(hbox2)
self.layout().addStretch(1) self.layout().addStretch(1)
self._valid = False self.ks = None
def on_ready(self):
self.ks = self.wizard_data['xprv1'] self.ks = self.wizard_data['xprv1']
def on_text(self): def on_text(self):

37
electrum/plugins/trustedcoin/trustedcoin.py

@ -225,7 +225,7 @@ class TrustedCoinCosignerClient(Logger):
def reset_auth(self, id, challenge, signatures): def reset_auth(self, id, challenge, signatures):
""" Reset Google Auth secret """ """ Reset Google Auth secret """
payload = {'challenge':challenge, 'signatures':signatures} payload = {'challenge': challenge, 'signatures': signatures}
return self.send_request('post', 'cosigner/%s/otp_secret' % quote(id), payload) return self.send_request('post', 'cosigner/%s/otp_secret' % quote(id), payload)
def sign(self, id, transaction, otp): def sign(self, id, transaction, otp):
@ -420,6 +420,15 @@ def make_billing_address(wallet, num, addr_type):
raise ValueError(f'unexpected billing type: {addr_type}') raise ValueError(f'unexpected billing type: {addr_type}')
def finish_requesting(func):
def f(self, *args, **kwargs):
try:
return func(self, *args, **kwargs)
finally:
self.requesting = False
return f
class TrustedCoinPlugin(BasePlugin): class TrustedCoinPlugin(BasePlugin):
wallet_class = Wallet_2fa wallet_class = Wallet_2fa
disclaimer_msg = DISCLAIMER disclaimer_msg = DISCLAIMER
@ -459,20 +468,12 @@ class TrustedCoinPlugin(BasePlugin):
@hook @hook
def get_tx_extra_fee(self, wallet, tx: Transaction): def get_tx_extra_fee(self, wallet, tx: Transaction):
if type(wallet) != Wallet_2fa: if type(wallet) is not Wallet_2fa:
return return
for o in tx.outputs(): for o in tx.outputs():
if wallet.is_billing_address(o.address): if wallet.is_billing_address(o.address):
return o.address, o.value return o.address, o.value
def finish_requesting(func):
def f(self, *args, **kwargs):
try:
return func(self, *args, **kwargs)
finally:
self.requesting = False
return f
@finish_requesting @finish_requesting
def request_billing_info(self, wallet: 'Wallet_2fa', *, suppress_connection_error=True): def request_billing_info(self, wallet: 'Wallet_2fa', *, suppress_connection_error=True):
if wallet.can_sign_without_server(): if wallet.can_sign_without_server():
@ -525,7 +526,7 @@ class TrustedCoinPlugin(BasePlugin):
window.wallet.is_billing = False window.wallet.is_billing = False
@classmethod @classmethod
def get_xkeys(self, seed, t, passphrase, derivation): def get_xkeys(cls, seed, t, passphrase, derivation):
assert is_any_2fa_seed_type(t) assert is_any_2fa_seed_type(t)
xtype = 'standard' if t == '2fa' else 'p2wsh' xtype = 'standard' if t == '2fa' else 'p2wsh'
bip32_seed = Mnemonic.mnemonic_to_seed(seed, passphrase=passphrase) bip32_seed = Mnemonic.mnemonic_to_seed(seed, passphrase=passphrase)
@ -534,7 +535,7 @@ class TrustedCoinPlugin(BasePlugin):
return child_node.to_xprv(), child_node.to_xpub() return child_node.to_xprv(), child_node.to_xpub()
@classmethod @classmethod
def xkeys_from_seed(self, seed, passphrase): def xkeys_from_seed(cls, seed, passphrase):
t = calc_seed_type(seed) t = calc_seed_type(seed)
if not is_any_2fa_seed_type(t): if not is_any_2fa_seed_type(t):
raise Exception(f'unexpected seed type: {t!r}') raise Exception(f'unexpected seed type: {t!r}')
@ -547,16 +548,16 @@ class TrustedCoinPlugin(BasePlugin):
# the probability of it being < 20 words is about 2^(-(256+12-19*11)) = 2^(-59) # the probability of it being < 20 words is about 2^(-(256+12-19*11)) = 2^(-59)
if passphrase: if passphrase:
raise Exception("old '2fa'-type electrum seed cannot have passphrase") raise Exception("old '2fa'-type electrum seed cannot have passphrase")
xprv1, xpub1 = self.get_xkeys(' '.join(words[0:12]), t, '', "m/") xprv1, xpub1 = cls.get_xkeys(' '.join(words[0:12]), t, '', "m/")
xprv2, xpub2 = self.get_xkeys(' '.join(words[12:]), t, '', "m/") xprv2, xpub2 = cls.get_xkeys(' '.join(words[12:]), t, '', "m/")
elif n == 12: # new scheme elif n == 12: # new scheme
xprv1, xpub1 = self.get_xkeys(seed, t, passphrase, "m/0'/") xprv1, xpub1 = cls.get_xkeys(seed, t, passphrase, "m/0'/")
xprv2, xpub2 = self.get_xkeys(seed, t, passphrase, "m/1'/") xprv2, xpub2 = cls.get_xkeys(seed, t, passphrase, "m/1'/")
else: else:
raise Exception(f'unrecognized seed length for "2fa" seed: {n}') raise Exception(f'unrecognized seed length for "2fa" seed: {n}')
elif t == '2fa_segwit': elif t == '2fa_segwit':
xprv1, xpub1 = self.get_xkeys(seed, t, passphrase, "m/0'/") xprv1, xpub1 = cls.get_xkeys(seed, t, passphrase, "m/0'/")
xprv2, xpub2 = self.get_xkeys(seed, t, passphrase, "m/1'/") xprv2, xpub2 = cls.get_xkeys(seed, t, passphrase, "m/1'/")
else: else:
raise Exception(f'unexpected seed type: {t!r}') raise Exception(f'unexpected seed type: {t!r}')
return xprv1, xpub1, xprv2, xpub2 return xprv1, xpub1, xprv2, xpub2

Loading…
Cancel
Save