1717********************************************************************************
1818"""
1919
20- from binascii import hexlify
21- import hid
2220import os
23- import time
2421import sys
22+ import time
23+ from binascii import hexlify
2524
25+ import hid
2626import nfc
27- from nfc .clf import RemoteTarget
2827
28+ from .BleComm import BleDevice
2929from .commException import CommException
3030from .commHTTP import getDongle as getDongleHTTP
3131from .commTCP import getDongle as getDongleTCP
3232from .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
3836APDUGEN = None
3937if "APDUGEN" in os .environ and len (os .environ ["APDUGEN" ]) != 0 :
7270 PCSC = os .environ ["PCSC" ]
7371if 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\n Please run 'python -m ledgerblue.BleComm' to select wich device to connect to"
234+ "Key Error\n Please 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