updates for Legder plugin
This commit is contained in:
parent
6e6ba37e7e
commit
97417e06e4
|
@ -7,7 +7,7 @@ import electrum
|
||||||
from electrum.bitcoin import EncodeBase58Check, DecodeBase58Check, TYPE_ADDRESS
|
from electrum.bitcoin import EncodeBase58Check, DecodeBase58Check, TYPE_ADDRESS
|
||||||
from electrum.i18n import _
|
from electrum.i18n import _
|
||||||
from electrum.plugins import BasePlugin, hook
|
from electrum.plugins import BasePlugin, hook
|
||||||
from ..hw_wallet import BIP32_HW_Wallet
|
from electrum.keystore import Hardware_KeyStore
|
||||||
from ..hw_wallet import HW_PluginBase
|
from ..hw_wallet import HW_PluginBase
|
||||||
from electrum.util import format_satoshis_plain, print_error
|
from electrum.util import format_satoshis_plain, print_error
|
||||||
|
|
||||||
|
@ -26,12 +26,12 @@ except ImportError:
|
||||||
BTCHIP = False
|
BTCHIP = False
|
||||||
|
|
||||||
|
|
||||||
class BTChipWallet(BIP32_HW_Wallet):
|
class Ledger_KeyStore(Hardware_KeyStore):
|
||||||
wallet_type = 'btchip'
|
wallet_type = 'btchip'
|
||||||
device = 'Ledger'
|
device = 'Ledger'
|
||||||
|
|
||||||
def __init__(self, storage):
|
def __init__(self):
|
||||||
BIP32_HW_Wallet.__init__(self, storage)
|
Hardware_KeyStore.__init__(self)
|
||||||
# Errors and other user interaction is done through the wallet's
|
# Errors and other user interaction is done through the wallet's
|
||||||
# handler. The handler is per-window and preserved across
|
# handler. The handler is per-window and preserved across
|
||||||
# device reconnects
|
# device reconnects
|
||||||
|
@ -39,6 +39,18 @@ class BTChipWallet(BIP32_HW_Wallet):
|
||||||
self.force_watching_only = False
|
self.force_watching_only = False
|
||||||
self.device_checked = False
|
self.device_checked = False
|
||||||
self.signing = False
|
self.signing = False
|
||||||
|
self.client = None
|
||||||
|
|
||||||
|
def get_derivation(self):
|
||||||
|
return "m/44'/0'/%d'"%self.account_id
|
||||||
|
|
||||||
|
def load(self, storage, name):
|
||||||
|
self.xpub = storage.get('master_public_keys', {}).get(name)
|
||||||
|
self.account_id = int(storage.get('account_id'))
|
||||||
|
|
||||||
|
def init_xpub(self):
|
||||||
|
client = self.get_client()
|
||||||
|
self.xpub = self.get_public_key(self.get_derivation())
|
||||||
|
|
||||||
def give_error(self, message, clear_client = False):
|
def give_error(self, message, clear_client = False):
|
||||||
print_error(message)
|
print_error(message)
|
||||||
|
@ -47,7 +59,7 @@ class BTChipWallet(BIP32_HW_Wallet):
|
||||||
else:
|
else:
|
||||||
self.signing = False
|
self.signing = False
|
||||||
if clear_client:
|
if clear_client:
|
||||||
self.plugin.client = None
|
self.client = None
|
||||||
self.device_checked = False
|
self.device_checked = False
|
||||||
raise Exception(message)
|
raise Exception(message)
|
||||||
|
|
||||||
|
@ -271,7 +283,7 @@ class BTChipWallet(BIP32_HW_Wallet):
|
||||||
self.signing = False
|
self.signing = False
|
||||||
|
|
||||||
def check_proper_device(self):
|
def check_proper_device(self):
|
||||||
pubKey = DecodeBase58Check(self.master_public_keys["x/0'"])[45:]
|
pubKey = DecodeBase58Check(self.xpub)[45:]
|
||||||
if not self.device_checked:
|
if not self.device_checked:
|
||||||
self.handler.show_message("Checking device")
|
self.handler.show_message("Checking device")
|
||||||
try:
|
try:
|
||||||
|
@ -306,24 +318,7 @@ class BTChipWallet(BIP32_HW_Wallet):
|
||||||
return False, None, None
|
return False, None, None
|
||||||
return True, response, response
|
return True, response, response
|
||||||
|
|
||||||
|
def get_client(self, force_pair=True, noPin=False):
|
||||||
class LedgerPlugin(HW_PluginBase):
|
|
||||||
libraries_available = BTCHIP
|
|
||||||
wallet_class = BTChipWallet
|
|
||||||
|
|
||||||
def __init__(self, parent, config, name):
|
|
||||||
HW_PluginBase.__init__(self, parent, config, name)
|
|
||||||
# FIXME shouldn't be a plugin member. Then this constructor can go.
|
|
||||||
self.client = None
|
|
||||||
|
|
||||||
def btchip_is_connected(self, wallet):
|
|
||||||
try:
|
|
||||||
wallet.get_client().getFirmwareVersion()
|
|
||||||
except:
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
def get_client(self, wallet, force_pair=True, noPin=False):
|
|
||||||
aborted = False
|
aborted = False
|
||||||
client = self.client
|
client = self.client
|
||||||
if not client or client.bad:
|
if not client or client.bad:
|
||||||
|
@ -388,8 +383,21 @@ class LedgerPlugin(HW_PluginBase):
|
||||||
else:
|
else:
|
||||||
raise e
|
raise e
|
||||||
client.bad = False
|
client.bad = False
|
||||||
wallet.device_checked = False
|
self.device_checked = False
|
||||||
wallet.proper_device = False
|
self.proper_device = False
|
||||||
self.client = client
|
self.client = client
|
||||||
|
|
||||||
return self.client
|
return self.client
|
||||||
|
|
||||||
|
|
||||||
|
class LedgerPlugin(HW_PluginBase):
|
||||||
|
libraries_available = BTCHIP
|
||||||
|
keystore_class = Ledger_KeyStore
|
||||||
|
|
||||||
|
def btchip_is_connected(self, keystore):
|
||||||
|
try:
|
||||||
|
keystore.get_client().getFirmwareVersion()
|
||||||
|
except Exception as e:
|
||||||
|
self.print_error("get_client", str(e))
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
|
@ -6,36 +6,37 @@ import PyQt4.QtCore as QtCore
|
||||||
|
|
||||||
from electrum.i18n import _
|
from electrum.i18n import _
|
||||||
from electrum.plugins import hook
|
from electrum.plugins import hook
|
||||||
from .ledger import LedgerPlugin, BTChipWallet
|
from .ledger import LedgerPlugin, Ledger_KeyStore
|
||||||
from ..hw_wallet.qt import QtHandlerBase
|
from ..hw_wallet.qt import QtHandlerBase
|
||||||
|
|
||||||
class Plugin(LedgerPlugin):
|
class Plugin(LedgerPlugin):
|
||||||
|
|
||||||
@hook
|
@hook
|
||||||
def load_wallet(self, wallet, window):
|
def load_wallet(self, wallet, window):
|
||||||
if type(wallet) != BTChipWallet:
|
keystore = wallet.get_keystore()
|
||||||
|
if type(keystore) != self.keystore_class:
|
||||||
return
|
return
|
||||||
wallet.handler = BTChipQTHandler(window)
|
keystore.handler = BTChipQTHandler(window)
|
||||||
if self.btchip_is_connected(wallet):
|
if self.btchip_is_connected(keystore):
|
||||||
if not wallet.check_proper_device():
|
if not keystore.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"))
|
||||||
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."))
|
||||||
wallet.force_watching_only = True
|
wallet.force_watching_only = True
|
||||||
|
|
||||||
def on_create_wallet(self, wallet, wizard):
|
def on_create_wallet(self, keystore, wizard):
|
||||||
assert type(wallet) == self.wallet_class
|
assert type(keystore) == self.keystore_class
|
||||||
wallet.handler = BTChipQTHandler(wizard)
|
keystore.handler = BTChipQTHandler(wizard)
|
||||||
# self.select_device(wallet)
|
keystore.init_xpub()
|
||||||
wallet.create_hd_account(None)
|
print keystore.xpub
|
||||||
|
wizard.create_wallet(keystore, None)
|
||||||
|
|
||||||
class BTChipQTHandler(QtHandlerBase):
|
class BTChipQTHandler(QtHandlerBase):
|
||||||
|
|
||||||
def __init__(self, win):
|
def __init__(self, win):
|
||||||
super(BTChipQTHandler, self).__init__(win, 'Ledger')
|
super(BTChipQTHandler, self).__init__(win, 'Ledger')
|
||||||
|
|
||||||
|
|
||||||
def word_dialog(self, msg):
|
def word_dialog(self, msg):
|
||||||
response = QInputDialog.getText(self.top_level_window(), "Ledger Wallet Authentication", msg, QLineEdit.Password)
|
response = QInputDialog.getText(self.top_level_window(), "Ledger Wallet Authentication", msg, QLineEdit.Password)
|
||||||
if not response[1]:
|
if not response[1]:
|
||||||
|
|
Loading…
Reference in New Issue