commit e00499f04013b7d43c73fe64b74eb393c3700420
parent 23c29715af210f49899d5a68f948b4f594135a52
Author: SomberNight <somber.night@protonmail.com>
Date: Sun, 22 Apr 2018 02:15:08 +0200
ledger: nice error msg if pin locked for sign_tx/sign_msg/show_addr
Diffstat:
1 file changed, 24 insertions(+), 14 deletions(-)
diff --git a/plugins/ledger/ledger.py b/plugins/ledger/ledger.py
@@ -34,6 +34,21 @@ SEGWIT_SUPPORT = '1.1.10'
SEGWIT_SUPPORT_SPECIAL = '1.0.4'
+def test_pin_unlocked(func):
+ """Function decorator to test the Ledger for being unlocked, and if not,
+ raise a human-readable exception.
+ """
+ def catch_exception(self, *args, **kwargs):
+ try:
+ return func(self, *args, **kwargs)
+ except BTChipException as e:
+ if e.sw == 0x6982:
+ raise Exception(_('Your Ledger is locked. Please unlock it.'))
+ else:
+ raise
+ return catch_exception
+
+
class Ledger_Client():
def __init__(self, hidDevice):
self.dongleObject = btchip(hidDevice)
@@ -64,20 +79,6 @@ class Ledger_Client():
return False
return True
- def test_pin_unlocked(func):
- """Function decorator to test the Ledger for being unlocked, and if not,
- raise a human-readable exception.
- """
- def catch_exception(self, *args, **kwargs):
- try:
- return func(self, *args, **kwargs)
- except BTChipException as e:
- if e.sw == 0x6982:
- raise Exception(_('Your Ledger is locked. Please unlock it.'))
- else:
- raise
- return catch_exception
-
@test_pin_unlocked
def get_xpub(self, bip32_path, xtype):
self.checkDevice()
@@ -256,6 +257,7 @@ class Ledger_KeyStore(Hardware_KeyStore):
def decrypt_message(self, pubkey, message, password):
raise RuntimeError(_('Encryption and decryption are currently not supported for {}').format(self.device))
+ @test_pin_unlocked
@set_and_unset_signing
def sign_message(self, sequence, message, password):
message = message.encode('utf8')
@@ -278,6 +280,8 @@ class Ledger_KeyStore(Hardware_KeyStore):
self.give_error("Unfortunately, this message cannot be signed by the Ledger wallet. Only alphanumerical messages shorter than 140 characters are supported. Please remove any extra characters (tab, carriage return) and retry.")
elif e.sw == 0x6985: # cancelled by user
return b''
+ elif e.sw == 0x6982:
+ raise # pin lock. decorator will catch it
else:
self.give_error(e, True)
except UserWarning:
@@ -299,6 +303,7 @@ class Ledger_KeyStore(Hardware_KeyStore):
# And convert it
return bytes([27 + 4 + (signature[0] & 0x01)]) + r + s
+ @test_pin_unlocked
@set_and_unset_signing
def sign_transaction(self, tx, password):
if tx.is_complete():
@@ -480,6 +485,8 @@ class Ledger_KeyStore(Hardware_KeyStore):
except BTChipException as e:
if e.sw == 0x6985: # cancelled by user
return
+ elif e.sw == 0x6982:
+ raise # pin lock. decorator will catch it
else:
traceback.print_exc(file=sys.stderr)
self.give_error(e, True)
@@ -494,6 +501,7 @@ class Ledger_KeyStore(Hardware_KeyStore):
txin['signatures'][signingPos] = bh2u(signatures[i])
tx.raw = tx.serialize()
+ @test_pin_unlocked
@set_and_unset_signing
def show_address(self, sequence, txin_type):
client = self.get_client()
@@ -506,6 +514,8 @@ class Ledger_KeyStore(Hardware_KeyStore):
except BTChipException as e:
if e.sw == 0x6985: # cancelled by user
pass
+ elif e.sw == 0x6982:
+ raise # pin lock. decorator will catch it
else:
traceback.print_exc(file=sys.stderr)
self.handler.show_error(e)