Skip to content

Commit

Permalink
Improve error handle for Ledger devices
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoerg committed Dec 26, 2023
1 parent 7fd2cea commit 0d5412a
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions hwilib/devices/ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
LegacyClient,
TransportClient,
)
from .ledger_bitcoin.client_base import ApduException
from .ledger_bitcoin.exception import NotSupportedError
from .ledger_bitcoin.wallet import (
MultisigWallet,
Expand Down Expand Up @@ -118,6 +119,18 @@ def check_keypath(key_path: str) -> bool:
AddressType.LEGACY: 3,
}

def handle_chip_exception(e, func_name: str) -> bool:
if e.sw in bad_args:
raise BadArgumentError('Bad argument')
elif e.sw == 0x6F00: # BTCHIP_SW_TECHNICAL_PROBLEM
raise DeviceFailureError(e.message)
elif e.sw == 0x6FAA: # BTCHIP_SW_HALTED
raise DeviceConnectionError('Device is asleep')
elif e.sw in cancels:
raise ActionCanceledError('{} canceled'.format(func_name))
else:
raise e

def ledger_exception(f: Callable[..., Any]) -> Any:
@wraps(f)
def func(*args: Any, **kwargs: Any) -> Any:
Expand All @@ -126,16 +139,10 @@ def func(*args: Any, **kwargs: Any) -> Any:
except ValueError as e:
raise BadArgumentError(str(e))
except BTChipException as e:
if e.sw in bad_args:
raise BadArgumentError('Bad argument')
elif e.sw == 0x6F00: # BTCHIP_SW_TECHNICAL_PROBLEM
raise DeviceFailureError(e.message)
elif e.sw == 0x6FAA: # BTCHIP_SW_HALTED
raise DeviceConnectionError('Device is asleep')
elif e.sw in cancels:
raise ActionCanceledError('{} canceled'.format(f.__name__))
else:
raise e
handle_chip_exception(e, f.__name__)
except ApduException as e:
e.message = e.data.decode("utf-8")
handle_chip_exception(e, f.__name__)
return func

# This class extends the HardwareWalletClient for Ledger Nano S and Nano X specific things
Expand Down

0 comments on commit 0d5412a

Please sign in to comment.