Skip to content

Commit

Permalink
ledger: don't throw exception if user cancels signing
Browse files Browse the repository at this point in the history
  • Loading branch information
icodeface committed Mar 15, 2018
1 parent 317c48e commit fef868d
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions plugins/ledger/ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,18 @@ def give_error(self, message, clear_client=False):
self.client = None
raise Exception(message)

def set_and_unset_signing(func):
"""Function decorator to set and unset self.signing."""

def wrapper(self, *args, **kwargs):
try:
self.signing = True
return func(self, *args, **kwargs)
finally:
self.signing = False

return wrapper

def address_id_stripped(self, address):
# Strip the leading "m/"
change, index = self.get_address_index(address)
Expand All @@ -259,8 +271,8 @@ def address_id_stripped(self, address):
def decrypt_message(self, pubkey, message, password):
raise RuntimeError(_('Encryption and decryption are currently not supported for %s') % self.device)

@set_and_unset_signing
def sign_message(self, sequence, message, password):
self.signing = True
message = message.encode('utf8')
message_hash = hashlib.sha256(message).hexdigest().upper()
# prompt for the PIN before displaying the dialog if necessary
Expand All @@ -281,16 +293,17 @@ def sign_message(self, sequence, message, password):
if e.sw == 0x6a80:
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''
else:
self.give_error(e, True)
except UserWarning:
self.handler.show_error(_('Cancelled by user'))
return ''
return b''
except Exception as e:
self.give_error(e, True)
finally:
self.handler.finished()
self.signing = False

# Parse the ASN.1 signature
rLength = signature[3]
Expand All @@ -305,11 +318,11 @@ def sign_message(self, sequence, message, password):
# And convert it
return bytes([27 + 4 + (signature[0] & 0x01)]) + r + s

@set_and_unset_signing
def sign_transaction(self, tx, password):
if tx.is_complete():
return
client = self.get_client()
self.signing = True
inputs = []
inputsPaths = []
pubKeys = []
Expand Down Expand Up @@ -475,6 +488,12 @@ def sign_transaction(self, tx, password):
except UserWarning:
self.handler.show_error(_('Cancelled by user'))
return
except BTChipException as e:
if e.sw == 0x6985: # cancelled by user
return
else:
traceback.print_exc(file=sys.stderr)
self.give_error(e, True)
except BaseException as e:
traceback.print_exc(file=sys.stdout)
self.give_error(e, True)
Expand All @@ -485,10 +504,9 @@ def sign_transaction(self, tx, password):
signingPos = inputs[i][4]
txin['signatures'][signingPos] = bh2u(signatures[i])
tx.raw = tx.serialize()
self.signing = False

@set_and_unset_signing
def show_address(self, sequence, txin_type):
self.signing = True
client = self.get_client()
address_path = self.get_derivation()[2:] + "/%d/%d" % sequence
self.handler.show_message(_("Showing address ..."))
Expand All @@ -507,7 +525,6 @@ def show_address(self, sequence, txin_type):
self.handler.show_error(e)
finally:
self.handler.finished()
self.signing = False


class LedgerPlugin(HW_PluginBase):
Expand Down

0 comments on commit fef868d

Please sign in to comment.