Browse Source

ledger: nice error msg if pin locked for sign_tx/sign_msg/show_addr

3.2.x
SomberNight 7 years ago
parent
commit
e00499f040
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 38
      plugins/ledger/ledger.py

38
plugins/ledger/ledger.py

@ -34,6 +34,21 @@ SEGWIT_SUPPORT = '1.1.10'
SEGWIT_SUPPORT_SPECIAL = '1.0.4' 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(): class Ledger_Client():
def __init__(self, hidDevice): def __init__(self, hidDevice):
self.dongleObject = btchip(hidDevice) self.dongleObject = btchip(hidDevice)
@ -64,20 +79,6 @@ class Ledger_Client():
return False return False
return True 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 @test_pin_unlocked
def get_xpub(self, bip32_path, xtype): def get_xpub(self, bip32_path, xtype):
self.checkDevice() self.checkDevice()
@ -256,6 +257,7 @@ class Ledger_KeyStore(Hardware_KeyStore):
def decrypt_message(self, pubkey, message, password): def decrypt_message(self, pubkey, message, password):
raise RuntimeError(_('Encryption and decryption are currently not supported for {}').format(self.device)) raise RuntimeError(_('Encryption and decryption are currently not supported for {}').format(self.device))
@test_pin_unlocked
@set_and_unset_signing @set_and_unset_signing
def sign_message(self, sequence, message, password): def sign_message(self, sequence, message, password):
message = message.encode('utf8') 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.") 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 elif e.sw == 0x6985: # cancelled by user
return b'' return b''
elif e.sw == 0x6982:
raise # pin lock. decorator will catch it
else: else:
self.give_error(e, True) self.give_error(e, True)
except UserWarning: except UserWarning:
@ -299,6 +303,7 @@ class Ledger_KeyStore(Hardware_KeyStore):
# And convert it # And convert it
return bytes([27 + 4 + (signature[0] & 0x01)]) + r + s return bytes([27 + 4 + (signature[0] & 0x01)]) + r + s
@test_pin_unlocked
@set_and_unset_signing @set_and_unset_signing
def sign_transaction(self, tx, password): def sign_transaction(self, tx, password):
if tx.is_complete(): if tx.is_complete():
@ -480,6 +485,8 @@ class Ledger_KeyStore(Hardware_KeyStore):
except BTChipException as e: except BTChipException as e:
if e.sw == 0x6985: # cancelled by user if e.sw == 0x6985: # cancelled by user
return return
elif e.sw == 0x6982:
raise # pin lock. decorator will catch it
else: else:
traceback.print_exc(file=sys.stderr) traceback.print_exc(file=sys.stderr)
self.give_error(e, True) self.give_error(e, True)
@ -494,6 +501,7 @@ class Ledger_KeyStore(Hardware_KeyStore):
txin['signatures'][signingPos] = bh2u(signatures[i]) txin['signatures'][signingPos] = bh2u(signatures[i])
tx.raw = tx.serialize() tx.raw = tx.serialize()
@test_pin_unlocked
@set_and_unset_signing @set_and_unset_signing
def show_address(self, sequence, txin_type): def show_address(self, sequence, txin_type):
client = self.get_client() client = self.get_client()
@ -506,6 +514,8 @@ class Ledger_KeyStore(Hardware_KeyStore):
except BTChipException as e: except BTChipException as e:
if e.sw == 0x6985: # cancelled by user if e.sw == 0x6985: # cancelled by user
pass pass
elif e.sw == 0x6982:
raise # pin lock. decorator will catch it
else: else:
traceback.print_exc(file=sys.stderr) traceback.print_exc(file=sys.stderr)
self.handler.show_error(e) self.handler.show_error(e)

Loading…
Cancel
Save