Browse Source

Remove need for self.wallet for h/w wallets

283
Neil Booth 9 years ago
parent
commit
1d51335827
  1. 2
      gui/qt/main_window.py
  2. 2
      lib/plugins.py
  3. 4
      plugins/keepkey/cmdline.py
  4. 5
      plugins/ledger/cmdline.py
  5. 27
      plugins/ledger/ledger.py
  6. 15
      plugins/ledger/qt.py
  7. 6
      plugins/trezor/cmdline.py
  8. 31
      plugins/trezor/plugin.py
  9. 17
      plugins/trezor/qt_generic.py

2
gui/qt/main_window.py

@ -1537,7 +1537,7 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError):
if any(can_send(addr) for addr in addrs): if any(can_send(addr) for addr in addrs):
menu.addAction(_("Send From"), lambda: self.send_from_addresses(addrs)) menu.addAction(_("Send From"), lambda: self.send_from_addresses(addrs))
run_hook('receive_menu', menu, addrs) run_hook('receive_menu', menu, addrs, self.wallet)
menu.exec_(self.address_list.viewport().mapToGlobal(position)) menu.exec_(self.address_list.viewport().mapToGlobal(position))

2
lib/plugins.py

@ -137,8 +137,6 @@ def _run_hook(name, always, *args):
results = [] results = []
f_list = hooks.get(name, []) f_list = hooks.get(name, [])
for p, f in f_list: for p, f in f_list:
if name == 'load_wallet':
p.wallet = args[0] # For for p.is_enabled() below
if always or p.is_enabled(): if always or p.is_enabled():
try: try:
r = f(*args) r = f(*args)

4
plugins/keepkey/cmdline.py

@ -25,8 +25,6 @@ class KeepKeyCmdLineHandler:
class Plugin(KeepKeyPlugin): class Plugin(KeepKeyPlugin):
@hook @hook
def cmdline_load_wallet(self, wallet): def cmdline_load_wallet(self, wallet):
self.wallet = wallet wallet.plugin = self
self.wallet.plugin = self
if self.handler is None: if self.handler is None:
self.handler = KeepKeyCmdLineHandler() self.handler = KeepKeyCmdLineHandler()

5
plugins/ledger/cmdline.py

@ -20,9 +20,6 @@ class BTChipCmdLineHandler:
class Plugin(LedgerPlugin): class Plugin(LedgerPlugin):
@hook @hook
def cmdline_load_wallet(self, wallet): def cmdline_load_wallet(self, wallet):
self.wallet = wallet wallet.plugin = self
self.wallet.plugin = self
if self.handler is None: if self.handler is None:
self.handler = BTChipCmdLineHandler() self.handler = BTChipCmdLineHandler()

27
plugins/ledger/ledger.py

@ -261,8 +261,6 @@ class BTChipWallet(BIP32_HD_Wallet):
return BIP32_HD_Wallet.sign_transaction(self, tx, password) return BIP32_HD_Wallet.sign_transaction(self, tx, password)
if tx.is_complete(): if tx.is_complete():
return return
#if tx.error:
# raise BaseException(tx.error)
self.signing = True self.signing = True
inputs = [] inputs = []
inputsPaths = [] inputsPaths = []
@ -421,33 +419,24 @@ class LedgerPlugin(BasePlugin):
def __init__(self, parent, config, name): def __init__(self, parent, config, name):
BasePlugin.__init__(self, parent, config, name) BasePlugin.__init__(self, parent, config, name)
self.wallet = None
self.handler = None self.handler = None
def constructor(self, s): def constructor(self, s):
return BTChipWallet(s) return BTChipWallet(s)
def is_enabled(self): def is_enabled(self):
if not BTCHIP: return BTCHIP:
return False
if not self.wallet:
return False
if self.wallet.storage.get('wallet_type') != 'btchip':
return False
if self.wallet.has_seed():
return False
return True
def btchip_is_connected(self): def btchip_is_connected(self, wallet):
try: try:
self.wallet.get_client().getFirmwareVersion() wallet.get_client().getFirmwareVersion()
except: except:
return False return False
return True return True
@hook @hook
def close_wallet(self): def close_wallet(self):
self.wallet = None pass
@hook @hook
def installwizard_load_wallet(self, wallet, window): def installwizard_load_wallet(self, wallet, window):
@ -466,11 +455,3 @@ class LedgerPlugin(BasePlugin):
QMessageBox.information(None, _('Error'), str(e), _('OK')) QMessageBox.information(None, _('Error'), str(e), _('OK'))
return return
return wallet return wallet
@hook
def sign_tx(self, window, tx):
tx.error = None
try:
self.wallet.sign_transaction(tx, None)
except Exception as e:
tx.error = str(e)

15
plugins/ledger/qt.py

@ -4,23 +4,24 @@ import threading
from electrum.plugins import BasePlugin, hook from electrum.plugins import BasePlugin, hook
from ledger import LedgerPlugin from ledger import LedgerPlugin, BTChipWallet
class Plugin(LedgerPlugin): class Plugin(LedgerPlugin):
@hook @hook
def load_wallet(self, wallet, window): def load_wallet(self, wallet, window):
self.wallet = wallet if type(wallet) != BTChipWallet:
self.wallet.plugin = self return
wallet.plugin = self
if self.handler is None: if self.handler is None:
self.handler = BTChipQTHandler(window) self.handler = BTChipQTHandler(window)
if self.btchip_is_connected(): if self.btchip_is_connected(wallet):
if not self.wallet.check_proper_device(): if not wallet.check_proper_device():
window.show_error(_("This wallet does not match your Ledger device")) window.show_error(_("This wallet does not match your Ledger device"))
self.wallet.force_watching_only = True wallet.force_watching_only = True
else: else:
window.show_error(_("Ledger device not detected.\nContinuing in watching-only mode.")) window.show_error(_("Ledger device not detected.\nContinuing in watching-only mode."))
self.wallet.force_watching_only = True wallet.force_watching_only = True
class BTChipQTHandler: class BTChipQTHandler:

6
plugins/trezor/cmdline.py

@ -27,8 +27,8 @@ class Plugin(TrezorPlugin):
@hook @hook
def cmdline_load_wallet(self, wallet): def cmdline_load_wallet(self, wallet):
self.wallet = wallet if type(wallet) != self.wallet_class:
self.wallet.plugin = self return
wallet.plugin = self
if self.handler is None: if self.handler is None:
self.handler = TrezorCmdLineHandler() self.handler = TrezorCmdLineHandler()

31
plugins/trezor/plugin.py

@ -113,7 +113,7 @@ class TrezorCompatibleWallet(BIP44_Wallet):
acc_id = re.match("x/(\d+)'", k).group(1) acc_id = re.match("x/(\d+)'", k).group(1)
xpub_path[xpub] = self.account_derivation(acc_id) xpub_path[xpub] = self.account_derivation(acc_id)
self.plugin.sign_transaction(tx, prev_tx, xpub_path) self.plugin.sign_transaction(self, tx, prev_tx, xpub_path)
def is_proper_device(self): def is_proper_device(self):
self.get_client().ping('t') self.get_client().ping('t')
@ -154,7 +154,6 @@ class TrezorCompatiblePlugin(BasePlugin):
def __init__(self, parent, config, name): def __init__(self, parent, config, name):
BasePlugin.__init__(self, parent, config, name) BasePlugin.__init__(self, parent, config, name)
self.device = self.wallet_class.device self.device = self.wallet_class.device
self.wallet = None
self.handler = None self.handler = None
self.client = None self.client = None
@ -166,16 +165,7 @@ class TrezorCompatiblePlugin(BasePlugin):
raise Exception(message) raise Exception(message)
def is_enabled(self): def is_enabled(self):
if not self.libraries_available: return self.libraries_available
return False
if not self.wallet:
return False
wallet_type = self.wallet_class.wallet_type
if self.wallet.storage.get('wallet_type') != wallet_type:
return False
if self.wallet.has_seed():
return False
return True
def create_client(self): def create_client(self):
if not self.libraries_available: if not self.libraries_available:
@ -212,14 +202,13 @@ class TrezorCompatiblePlugin(BasePlugin):
self.client.clear_session() self.client.clear_session()
self.client.transport.close() self.client.transport.close()
self.client = None self.client = None
self.wallet = None
def sign_transaction(self, tx, prev_tx, xpub_path): def sign_transaction(self, wallet, tx, prev_tx, xpub_path):
self.prev_tx = prev_tx self.prev_tx = prev_tx
self.xpub_path = xpub_path self.xpub_path = xpub_path
client = self.get_client() client = self.get_client()
inputs = self.tx_inputs(tx, True) inputs = self.tx_inputs(tx, True)
outputs = self.tx_outputs(tx) outputs = self.tx_outputs(wallet, tx)
try: try:
signed_tx = client.sign_tx('Bitcoin', inputs, outputs)[1] signed_tx = client.sign_tx('Bitcoin', inputs, outputs)[1]
except Exception as e: except Exception as e:
@ -229,11 +218,11 @@ class TrezorCompatiblePlugin(BasePlugin):
raw = signed_tx.encode('hex') raw = signed_tx.encode('hex')
tx.update_signatures(raw) tx.update_signatures(raw)
def show_address(self, address): def show_address(self, wallet, address):
client = self.get_client() client = self.get_client()
self.wallet.check_proper_device() wallet.check_proper_device()
try: try:
address_path = self.wallet.address_id(address) address_path = wallet.address_id(address)
address_n = client.expand_path(address_path) address_n = client.expand_path(address_path)
except Exception as e: except Exception as e:
self.give_error(e) self.give_error(e)
@ -306,15 +295,15 @@ class TrezorCompatiblePlugin(BasePlugin):
return inputs return inputs
def tx_outputs(self, tx): def tx_outputs(self, wallet, tx):
client = self.get_client() client = self.get_client()
outputs = [] outputs = []
for type, address, amount in tx.outputs: for type, address, amount in tx.outputs:
assert type == 'address' assert type == 'address'
txoutputtype = self.types.TxOutputType() txoutputtype = self.types.TxOutputType()
if self.wallet.is_change(address): if wallet.is_change(address):
address_path = self.wallet.address_id(address) address_path = wallet.address_id(address)
address_n = client.expand_path(address_path) address_n = client.expand_path(address_path)
txoutputtype.address_n.extend(address_n) txoutputtype.address_n.extend(address_n)
else: else:

17
plugins/trezor/qt_generic.py

@ -108,16 +108,17 @@ class QtPlugin(TrezorPlugin):
@hook @hook
def load_wallet(self, wallet, window): def load_wallet(self, wallet, window):
if type(wallet) != self.wallet_class:
return
self.print_error("load_wallet") self.print_error("load_wallet")
self.wallet = wallet wallet.plugin = self
self.wallet.plugin = self
self.button = StatusBarButton(QIcon(self.icon_file), self.device, self.button = StatusBarButton(QIcon(self.icon_file), self.device,
partial(self.settings_dialog, window)) partial(self.settings_dialog, window))
if type(window) is ElectrumWindow: if type(window) is ElectrumWindow:
window.statusBar().addPermanentWidget(self.button) window.statusBar().addPermanentWidget(self.button)
if self.handler is None: if self.handler is None:
self.handler = self.create_handler(window) self.handler = self.create_handler(window)
msg = self.wallet.sanity_check() msg = wallet.sanity_check()
if msg: if msg:
window.show_error(msg) window.show_error(msg)
@ -139,7 +140,7 @@ class QtPlugin(TrezorPlugin):
# Restored wallets are not hardware wallets # Restored wallets are not hardware wallets
wallet_class = self.wallet_class.restore_wallet_class wallet_class = self.wallet_class.restore_wallet_class
storage.put('wallet_type', wallet_class.wallet_type) storage.put('wallet_type', wallet_class.wallet_type)
self.wallet = wallet = wallet_class(storage) wallet = wallet_class(storage)
handler = self.create_handler(wizard) handler = self.create_handler(wizard)
msg = "\n".join([_("Please enter your %s passphrase.") % self.device, msg = "\n".join([_("Please enter your %s passphrase.") % self.device,
@ -154,11 +155,13 @@ class QtPlugin(TrezorPlugin):
return wallet return wallet
@hook @hook
def receive_menu(self, menu, addrs): def receive_menu(self, menu, addrs, wallet):
if (not self.wallet.is_watching_only() and if type(wallet) != self.wallet_class:
return
if (not wallet.is_watching_only() and
self.atleast_version(1, 3) and len(addrs) == 1): self.atleast_version(1, 3) and len(addrs) == 1):
menu.addAction(_("Show on %s") % self.device, menu.addAction(_("Show on %s") % self.device,
lambda: self.show_address(addrs[0])) lambda: self.show_address(wallet, addrs[0]))
def settings_dialog(self, window): def settings_dialog(self, window):

Loading…
Cancel
Save