Skip to content

Commit f180b96

Browse files
Merge pull request #136 from LedgerHQ/tdj/add_error_codes
Add detailed descriptions for some error codes to ease debug.
2 parents 39f1b55 + 2f2bee6 commit f180b96

File tree

3 files changed

+31
-51
lines changed

3 files changed

+31
-51
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Ledgerblue - Python tools for Ledger Blue, Nano S and Nano X
1+
# Ledgerblue - Python tools for Ledger devices
22

3-
This package contains Python tools to communicate with Ledger Blue, Nano S and Nano X and manage applications life cycle.
3+
This package contains Python tools to communicate with Ledger devices and manage applications life cycle.
44

55
## Installation
66

@@ -14,7 +14,7 @@ pip install ledgerblue
1414

1515
## Supported devices
1616

17-
At the moment these tools work for all Nano S, Nano S+, and Blue devices, but only for special Nano X developer units which are not available to the general public.
17+
At the moment these tools work for all ledger devices, but only for special Nano X developer units which are not available to the general public.
1818
The Recover scripts, will work with Nano X starting from a specific version.
1919

2020
Please check [Ledger Developer Portal](https://developers.ledger.com/docs/nano-app/introduction/) to see how to debug your application on a Nano X simulator using [Speculos](https://github.com/LedgerHQ/speculos)
@@ -55,9 +55,10 @@ Use the following Target IDs (--targetId option) when running commands directly:
5555

5656
| Device name | Firmware Version | Target ID |
5757
|------------------|------------------------------------|--------------|
58+
| `Flex` | all | `0x33300004` |
59+
| `Stax` | all | `0x33200004` |
5860
| `Nano S Plus` | all | `0x33100004` |
59-
| `Nano X` | < 2.2.1 (**developer units only**) | `0x33000004` |
60-
| `Nano X` | \>= 2.2.1 | `0x33000004` |
61+
| `Nano X` | (**developer units only**) | `0x33000004` |
6162
| `Nano S` | <= 1.3.1 | `0x31100002` |
6263
| `Nano S` | 1.4.x | `0x31100003` |
6364
| `Nano S` | \>= 1.5.x | `0x31100004` |

ledgerblue/comm.py

Lines changed: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,26 @@
7171
except ImportError:
7272
PCSC = False
7373

74+
def get_possible_error_cause(sw):
75+
cause_map = {
76+
0x6982: "Have you uninstalled the existing CA with resetCustomCA first?",
77+
0x6985: "Condition of use not satisfied (denied by the user?)",
78+
0x6a84: "Not enough space?",
79+
0x6a85: "Not enough space?",
80+
0x6a83: "Maybe this app requires a library to be installed first?",
81+
0x6484: "Are you using the correct targetId?",
82+
0x6d00: "Unexpected state of device: verify that the right application is opened?",
83+
0x6e00: "Unexpected state of device: verify that the right application is opened?",
84+
0x5515: "Did you unlock the device?",
85+
0x6814: "Unexpected target device: verify that you are using the right device?",
86+
0x511F: "The OS version on your device does not seem compatible with the SDK version used to build the app",
87+
0x5120: "Sideload is not supported on Nano X",
88+
}
89+
90+
# If the status word is in the map, return the corresponding cause, otherwise return a default message
91+
return cause_map.get(sw, "Unknown reason")
92+
93+
7494
class HIDDongleHIDAPI(Dongle, DongleWait):
7595

7696
def __init__(self, device, ledger=False, debug=False):
@@ -139,21 +159,7 @@ def exchange(self, apdu, timeout=TIMEOUT):
139159
if self.debug:
140160
print("HID <= %s%.2x" % (response.hex(), sw))
141161
if sw != 0x9000 and (sw & 0xFF00) != 0x6100 and (sw & 0xFF00) != 0x6C00:
142-
possibleCause = "Unknown reason"
143-
if sw == 0x6982:
144-
possibleCause = "Have you uninstalled the existing CA with resetCustomCA first?"
145-
if sw == 0x6985:
146-
possibleCause = "Condition of use not satisfied (denied by the user?)"
147-
if sw == 0x6a84 or sw == 0x6a85:
148-
possibleCause = "Not enough space?"
149-
if sw == 0x6a83:
150-
possibleCause = "Maybe this app requires a library to be installed first?"
151-
if sw == 0x6484:
152-
possibleCause = "Are you using the correct targetId?"
153-
if sw == 0x6d00:
154-
possibleCause = "Unexpected state of device: verify that the right application is opened?"
155-
if sw == 0x6e00:
156-
possibleCause = "Unexpected state of device: verify that the right application is opened?"
162+
possibleCause = get_possible_error_cause(sw)
157163
raise CommException("Invalid status %04x (%s)" % (sw, possibleCause), sw, response)
158164
return response
159165

@@ -194,21 +200,8 @@ def exchange(self, apdu, timeout=TIMEOUT):
194200
response = self.tag.transceive(apdu, 5.0)
195201
sw = (response[-2] << 8) + response[-1]
196202
if sw != 0x9000 and (sw & 0xFF00) != 0x6100 and (sw & 0xFF00) != 0x6C00:
197-
possibleCause = "Unknown reason"
198-
if sw == 0x6982:
199-
possibleCause = "Have you uninstalled the existing CA with resetCustomCA first?"
200-
if sw == 0x6985:
201-
possibleCause = "Condition of use not satisfied (denied by the user?)"
202-
if sw == 0x6a84 or sw == 0x6a85:
203-
possibleCause = "Not enough space?"
204-
if sw == 0x6a83:
205-
possibleCause = "Maybe this app requires a library to be installed first?"
206-
if sw == 0x6484:
207-
possibleCause = "Are you using the correct targetId?"
208-
if sw == 0x6d00:
209-
possibleCause = "Unexpected state of device: verify that the right application is opened?"
210-
if sw == 0x6e00:
211-
possibleCause = "Unexpected state of device: verify that the right application is opened?"
203+
possibleCause = get_possible_error_cause(sw)
204+
self.close()
212205
raise CommException("Invalid status %04x (%s)" % (sw, possibleCause), sw, response)
213206
if self.debug:
214207
print(f"[NFC] <= {response.hex()}")
@@ -241,21 +234,7 @@ def exchange(self, apdu, timeout=TIMEOUT):
241234
if self.debug:
242235
print("[BLE] <= %s%.2x" % (response.hex(), sw))
243236
if sw != 0x9000 and (sw & 0xFF00) != 0x6100 and (sw & 0xFF00) != 0x6C00:
244-
possibleCause = "Unknown reason"
245-
if sw == 0x6982:
246-
possibleCause = "Have you uninstalled the existing CA with resetCustomCA first?"
247-
if sw == 0x6985:
248-
possibleCause = "Condition of use not satisfied (denied by the user?)"
249-
if sw == 0x6a84 or sw == 0x6a85:
250-
possibleCause = "Not enough space?"
251-
if sw == 0x6a83:
252-
possibleCause = "Maybe this app requires a library to be installed first?"
253-
if sw == 0x6484:
254-
possibleCause = "Are you using the correct targetId?"
255-
if sw == 0x6d00:
256-
possibleCause = "Unexpected state of device: verify that the right application is opened?"
257-
if sw == 0x6e00:
258-
possibleCause = "Unexpected state of device: verify that the right application is opened?"
237+
possibleCause = get_possible_error_cause(sw)
259238
self.close()
260239
raise CommException("Invalid status %04x (%s)" % (sw, possibleCause), sw, response)
261240
return response

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ name = "ledgerblue"
1111
authors = [
1212
{ name = "Ledger", email = "hello@ledger.fr" }
1313
]
14-
description = "Python library to communicate with Ledger Blue/Nano S"
14+
description = "Python library to communicate with Ledger devices"
1515
readme = { file = "README.md", content-type = "text/markdown" }
1616
license = { file = "LICENSE" }
1717
classifiers = [

0 commit comments

Comments
 (0)