Skip to content

Commit

Permalink
fix PR review remarks
Browse files Browse the repository at this point in the history
  • Loading branch information
tdejoigny-ledger committed May 6, 2024
1 parent 5f87dd8 commit 2f2bee6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 71 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ Use the following Target IDs (--targetId option) when running commands directly:
| `Flex` | all | `0x33300004` |
| `Stax` | all | `0x33200004` |
| `Nano S Plus` | all | `0x33100004` |
| `Nano X` | < 2.2.1 (**developer units only**) | `0x33000004` |
| `Nano X` | \>= 2.2.1 | `0x33000004` |
| `Nano X` | (**developer units only**) | `0x33000004` |
| `Nano S` | <= 1.3.1 | `0x31100002` |
| `Nano S` | 1.4.x | `0x31100003` |
| `Nano S` | \>= 1.5.x | `0x31100004` |
Expand Down
93 changes: 24 additions & 69 deletions ledgerblue/comm.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,26 @@
except ImportError:
PCSC = False

def get_possible_error_cause(sw):
cause_map = {
0x6982: "Have you uninstalled the existing CA with resetCustomCA first?",
0x6985: "Condition of use not satisfied (denied by the user?)",
0x6a84: "Not enough space?",
0x6a85: "Not enough space?",
0x6a83: "Maybe this app requires a library to be installed first?",
0x6484: "Are you using the correct targetId?",
0x6d00: "Unexpected state of device: verify that the right application is opened?",
0x6e00: "Unexpected state of device: verify that the right application is opened?",
0x5515: "Did you unlock the device?",
0x6814: "Unexpected target device: verify that you are using the right device?",
0x511F: "The OS version on your device does not seem compatible with the SDK version used to build the app",
0x5120: "Sideload is not supported on Nano X",
}

# If the status word is in the map, return the corresponding cause, otherwise return a default message
return cause_map.get(sw, "Unknown reason")


class HIDDongleHIDAPI(Dongle, DongleWait):

def __init__(self, device, ledger=False, debug=False):
Expand Down Expand Up @@ -139,29 +159,7 @@ def exchange(self, apdu, timeout=TIMEOUT):
if self.debug:
print("HID <= %s%.2x" % (response.hex(), sw))
if sw != 0x9000 and (sw & 0xFF00) != 0x6100 and (sw & 0xFF00) != 0x6C00:
possibleCause = "Unknown reason"
if sw == 0x6982:
possibleCause = "Have you uninstalled the existing CA with resetCustomCA first?"
if sw == 0x6985:
possibleCause = "Condition of use not satisfied (denied by the user?)"
if sw == 0x6a84 or sw == 0x6a85:
possibleCause = "Not enough space?"
if sw == 0x6a83:
possibleCause = "Maybe this app requires a library to be installed first?"
if sw == 0x6484:
possibleCause = "Are you using the correct targetId?"
if sw == 0x6d00:
possibleCause = "Unexpected state of device: verify that the right application is opened?"
if sw == 0x6e00:
possibleCause = "Unexpected state of device: verify that the right application is opened?"
if sw == 0x5515:
possibleCause = "Did you unlock the device?"
if sw == 0x6814:
possibleCause = "Unexpected target device: verify that your are using the right device?"
if sw == 0x511F:
possibleCause = "The OS version on your device does not seem compatible with the SDK version used to build the app"
if sw == 0x5120:
possibleCause = "Sideload is not supported on Nano X"
possibleCause = get_possible_error_cause(sw)
raise CommException("Invalid status %04x (%s)" % (sw, possibleCause), sw, response)
return response

Expand Down Expand Up @@ -202,29 +200,8 @@ def exchange(self, apdu, timeout=TIMEOUT):
response = self.tag.transceive(apdu, 5.0)
sw = (response[-2] << 8) + response[-1]
if sw != 0x9000 and (sw & 0xFF00) != 0x6100 and (sw & 0xFF00) != 0x6C00:
possibleCause = "Unknown reason"
if sw == 0x6982:
possibleCause = "Have you uninstalled the existing CA with resetCustomCA first?"
if sw == 0x6985:
possibleCause = "Condition of use not satisfied (denied by the user?)"
if sw == 0x6a84 or sw == 0x6a85:
possibleCause = "Not enough space?"
if sw == 0x6a83:
possibleCause = "Maybe this app requires a library to be installed first?"
if sw == 0x6484:
possibleCause = "Are you using the correct targetId?"
if sw == 0x6d00:
possibleCause = "Unexpected state of device: verify that the right application is opened?"
if sw == 0x6e00:
possibleCause = "Unexpected state of device: verify that the right application is opened?"
if sw == 0x5515:
possibleCause = "Did you unlock the device?"
if sw == 0x6814:
possibleCause = "Unexpected target device: verify that your are using the right device?"
if sw == 0x511F:
possibleCause = "The OS version on your device does not seem compatible with the SDK version used to build the app"
if sw == 0x5120:
possibleCause = "Sideload is not supported on Nano X"
possibleCause = get_possible_error_cause(sw)
self.close()
raise CommException("Invalid status %04x (%s)" % (sw, possibleCause), sw, response)
if self.debug:
print(f"[NFC] <= {response.hex()}")
Expand Down Expand Up @@ -257,29 +234,7 @@ def exchange(self, apdu, timeout=TIMEOUT):
if self.debug:
print("[BLE] <= %s%.2x" % (response.hex(), sw))
if sw != 0x9000 and (sw & 0xFF00) != 0x6100 and (sw & 0xFF00) != 0x6C00:
possibleCause = "Unknown reason"
if sw == 0x6982:
possibleCause = "Have you uninstalled the existing CA with resetCustomCA first?"
if sw == 0x6985:
possibleCause = "Condition of use not satisfied (denied by the user?)"
if sw == 0x6a84 or sw == 0x6a85:
possibleCause = "Not enough space?"
if sw == 0x6a83:
possibleCause = "Maybe this app requires a library to be installed first?"
if sw == 0x6484:
possibleCause = "Are you using the correct targetId?"
if sw == 0x6d00:
possibleCause = "Unexpected state of device: verify that the right application is opened?"
if sw == 0x6e00:
possibleCause = "Unexpected state of device: verify that the right application is opened?"
if sw == 0x5515:
possibleCause = "Did you unlock the device?"
if sw == 0x6814:
possibleCause = "Unexpected target device: verify that your are using the right device?"
if sw == 0x511F:
possibleCause = "The OS version on your device does not seem compatible with the SDK version used to build the app"
if sw == 0x5120:
possibleCause = "Sideload is not supported on Nano X"
possibleCause = get_possible_error_cause(sw)
self.close()
raise CommException("Invalid status %04x (%s)" % (sw, possibleCause), sw, response)
return response
Expand Down

0 comments on commit 2f2bee6

Please sign in to comment.