Skip to content

Commit 109fd5c

Browse files
authored
Merge pull request #147 from LedgerHQ/mru-minor-fixes
minor fixes
2 parents 824d6c2 + 43f9e65 commit 109fd5c

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

ledgerblue/BleComm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ async def _read(mtu) -> bytes:
122122
total_size = int.from_bytes(response[3:5], "big")
123123

124124
total_size_bkup = total_size
125-
if total_size >= (mtu - 5):
125+
if total_size > (mtu - 5):
126126
apdu = response[5:mtu]
127127
total_size -= mtu - 5
128128

ledgerblue/comm.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,21 @@
1717
********************************************************************************
1818
"""
1919

20-
from binascii import hexlify
21-
import hid
2220
import os
23-
import time
2421
import sys
22+
import time
23+
from binascii import hexlify
2524

25+
import hid
2626
import nfc
27-
from nfc.clf import RemoteTarget
2827

28+
from .BleComm import BleDevice
2929
from .commException import CommException
3030
from .commHTTP import getDongle as getDongleHTTP
3131
from .commTCP import getDongle as getDongleTCP
3232
from .commU2F import getDongle as getDongleU2F
33-
from .Dongle import Dongle, DongleWait, TIMEOUT
34-
from .ledgerWrapper import wrapCommandAPDU, unwrapResponseAPDU
35-
from .BleComm import BleDevice
36-
33+
from .Dongle import TIMEOUT, Dongle, DongleWait
34+
from .ledgerWrapper import unwrapResponseAPDU, wrapCommandAPDU
3735

3836
APDUGEN = None
3937
if "APDUGEN" in os.environ and len(os.environ["APDUGEN"]) != 0:
@@ -72,9 +70,8 @@
7270
PCSC = os.environ["PCSC"]
7371
if PCSC:
7472
try:
75-
from smartcard.Exceptions import NoCardException
7673
from smartcard.System import readers
77-
from smartcard.util import toHexString, toBytes
74+
from smartcard.util import toBytes
7875
except ImportError:
7976
PCSC = False
8077

@@ -87,15 +84,14 @@ def get_possible_error_cause(sw):
8784
0x6A85: "Not enough space?",
8885
0x6A83: "Maybe this app requires a library to be installed first?",
8986
0x6484: "Are you using the correct targetId?",
90-
0x6D00: "Unexpected state of device: verify that the right application is opened?",
91-
0x6E00: "Unexpected state of device: verify that the right application is opened?",
87+
0x6D00: "Unexpected state of device: verify that the right application is opened?", # noqa: E501
88+
0x6E00: "Unexpected state of device: verify that the right application is opened?", # noqa: E501
9289
0x5515: "Did you unlock the device?",
9390
0x6814: "Unexpected target device: verify that you are using the right device?",
94-
0x511F: "The OS version on your device does not seem compatible with the SDK version used to build the app",
91+
0x511F: "The OS version on your device does not seem compatible with the SDK version used to build the app", # noqa: E501
9592
0x5120: "Sideload is not supported on Nano X",
9693
}
9794

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

10197

@@ -190,7 +186,7 @@ def close(self):
190186
if self.opened:
191187
try:
192188
self.device.close()
193-
except:
189+
except Exception:
194190
pass
195191
self.opened = False
196192

@@ -233,9 +229,10 @@ def __init__(self, debug=False):
233229
try:
234230
self.device = BleDevice(os.environ["LEDGER_BLE_MAC"])
235231
self.device.open()
236-
except KeyError as ex:
232+
except KeyError:
237233
sys.exit(
238-
f"Key Error\nPlease run 'python -m ledgerblue.BleComm' to select wich device to connect to"
234+
"Key Error\nPlease run 'python -m ledgerblue.BleComm'"
235+
" to select wich device to connect to"
239236
)
240237
self.opened = self.device.opened
241238

@@ -284,7 +281,7 @@ def close(self):
284281
if self.opened:
285282
try:
286283
self.device.disconnect()
287-
except:
284+
except Exception:
288285
pass
289286
self.opened = False
290287

@@ -293,7 +290,7 @@ def getDongle(debug=False, selectCommand=None):
293290
if APDUGEN:
294291
return HIDDongleHIDAPI(None, True, debug)
295292

296-
if not U2FKEY is None:
293+
if U2FKEY is not None:
297294
return getDongleU2F(scrambleKey=U2FKEY, debug=debug)
298295
elif MCUPROXY is not None:
299296
return getDongleHTTP(remote_host=MCUPROXY, debug=debug)
@@ -312,6 +309,10 @@ def getDongle(debug=False, selectCommand=None):
312309
"interface_number" in hidDevice and hidDevice["interface_number"] == 0
313310
) or ("usage_page" in hidDevice and hidDevice["usage_page"] == 0xFFA0):
314311
hidDevicePath = hidDevice["path"]
312+
313+
usb_port = os.getenv("LEDGER_PROXY_USB_PORT")
314+
if usb_port:
315+
hidDevicePath = usb_port.encode()
315316
if hidDevicePath is not None:
316317
dev = hid.device()
317318
dev.open_path(hidDevicePath)
@@ -323,7 +324,7 @@ def getDongle(debug=False, selectCommand=None):
323324
try:
324325
connection = reader.createConnection()
325326
connection.connect()
326-
if selectCommand != None:
327+
if selectCommand is not None:
327328
response, sw1, sw2 = connection.transmit(
328329
toBytes("00A4040010FF4C4547522E57414C5430312E493031")
329330
)
@@ -335,7 +336,7 @@ def getDongle(debug=False, selectCommand=None):
335336
connection = None
336337
else:
337338
break
338-
except:
339+
except Exception:
339340
connection = None
340341
pass
341342
if connection is not None:

0 commit comments

Comments
 (0)