mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-09-02 18:25:21 +00:00
ledger: nice error msg if pin locked for sign_tx/sign_msg/show_addr
This commit is contained in:
parent
23c29715af
commit
e00499f040
1 changed files with 24 additions and 14 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Reference in a new issue