Skip to content

Commit 61afe24

Browse files
committed
working python3
1 parent a5b5b07 commit 61afe24

3 files changed

Lines changed: 48 additions & 33 deletions

File tree

onlykey/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# coding: utf-8
2-
from .client import OnlyKey, Message, MessageField # Make `OnlyKey` available
2+
from __future__ import absolute_import
3+
from .client import OnlyKey, Message, MessageField # Make `OnlyKey` available

onlykey/cli.py

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# coding: utf-8
22
from __future__ import unicode_literals, print_function
3+
from __future__ import absolute_import
34

5+
from builtins import input
6+
from builtins import next
7+
from builtins import range
48
import base64
59
import binascii
610
import time
@@ -15,7 +19,7 @@
1519
from prompt_toolkit.filters import Condition
1620
import ed25519
1721

18-
from client import OnlyKey, Message, MessageField
22+
from .client import OnlyKey, Message, MessageField
1923

2024
only_key = OnlyKey()
2125

@@ -71,16 +75,16 @@ def prompt_pin():
7175

7276
with open(privkey, 'rb') as f:
7377
raw_privkey = f.read()
74-
print
78+
print ()
7579
print ('Enter ECC key slot number to use (1 - 32) or enter 0 to list key labels')
76-
print
80+
print ()
7781
slot = int(input())
7882

7983
while slot == 0:
8084
ok.displaykeylabels()
81-
print
85+
print ()
8286
print ('Enter ECC key slot number to use (1 - 32) or enter 0 to list key labels')
83-
print
87+
print ()
8488
slot = int(input())
8589

8690
slot = slot + 100 # ECC keys in slot 101 - 132
@@ -99,47 +103,47 @@ def prompt_pin():
99103
for msg in [Message.OKSETPIN]:
100104
only_key.send_message(msg=msg)
101105
print(only_key.read_string())
102-
print
106+
print ()
103107
input('Press the Enter key once you are done')
104108
only_key.send_message(msg=msg)
105109
print(only_key.read_string())
106110
only_key.send_message(msg=msg)
107111
print(only_key.read_string())
108-
print
112+
print ()
109113
input('Press the Enter key once you are done')
110114
only_key.send_message(msg=msg)
111115
print(only_key.read_string())
112-
print
116+
print ()
113117

114118
for msg in [Message.OKSETPDPIN]:
115119
only_key.send_message(msg=msg)
116120
print(only_key.read_string() + ' for second profile')
117-
print
121+
print ()
118122
input('Press the Enter key once you are done')
119123
only_key.send_message(msg=msg)
120124
print(only_key.read_string() + ' for second profile')
121125
only_key.send_message(msg=msg)
122126
print(only_key.read_string())
123-
print
127+
print ()
124128
input('Press the Enter key once you are done')
125129
only_key.send_message(msg=msg)
126130
print(only_key.read_string())
127-
print
131+
print ()
128132

129133
for msg in [Message.OKSETSDPIN]:
130134
only_key.send_message(msg=msg)
131135
print(only_key.read_string())
132-
print
136+
print ()
133137
input('Press the Enter key once you are done')
134138
only_key.send_message(msg=msg)
135139
print(only_key.read_string())
136140
only_key.send_message(msg=msg)
137141
print(only_key.read_string())
138-
print
142+
print ()
139143
input('Press the Enter key once you are done')
140144
only_key.send_message(msg=msg)
141145
print(only_key.read_string())
142-
print
146+
print ()
143147

144148
elif sys.argv[1] == 'getlabels':
145149
tmp = {}
@@ -298,7 +302,7 @@ def prompt_pin():
298302

299303
elif sys.argv[1]:
300304
print('Command not found')
301-
print
305+
print()
302306

303307
else:
304308

@@ -328,22 +332,22 @@ def mprompt():
328332
for msg in [Message.OKSETPIN]:
329333
only_key.send_message(msg=msg)
330334
print(only_key.read_string())
331-
print
335+
print()
332336
input('Press the Enter key once you are done')
333337
only_key.send_message(msg=msg)
334338
print(only_key.read_string())
335339
only_key.send_message(msg=msg)
336340
print(only_key.read_string())
337-
print
341+
print()
338342
input('Press the Enter key once you are done')
339343
only_key.send_message(msg=msg)
340344
print(only_key.read_string())
341-
print
345+
print()
342346

343347
for msg in [Message.OKSETPDPIN]:
344348
only_key.send_message(msg=msg)
345349
print(only_key.read_string() + ' for second profile')
346-
print
350+
print()
347351
input('Press the Enter key once you are done')
348352
only_key.send_message(msg=msg)
349353
print(only_key.read_string() + ' for second profile')
@@ -353,22 +357,22 @@ def mprompt():
353357
input('Press the Enter key once you are done')
354358
only_key.send_message(msg=msg)
355359
print(only_key.read_string())
356-
print
360+
print()
357361

358362
for msg in [Message.OKSETSDPIN]:
359363
only_key.send_message(msg=msg)
360364
print(only_key.read_string())
361-
print
365+
print()
362366
input('Press the Enter key once you are done')
363367
only_key.send_message(msg=msg)
364368
print(only_key.read_string())
365369
only_key.send_message(msg=msg)
366370
print(only_key.read_string())
367-
print
371+
print()
368372
input('Press the Enter key once you are done')
369373
only_key.send_message(msg=msg)
370374
print(only_key.read_string())
371-
print
375+
print()
372376
elif data[0] == 'getlabels':
373377
tmp = {}
374378
for slot in only_key.getlabels():

onlykey/client.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
# coding: utf-8
2+
from __future__ import print_function
3+
from builtins import input
4+
from builtins import chr
5+
from builtins import range
6+
from builtins import object
27
import logging
38
import time
49
import binascii
@@ -158,7 +163,8 @@ def __init__(self, connect=True):
158163
self._connect()
159164
log.debug('connected')
160165
return
161-
except Exception as e:
166+
except Exception as E:
167+
e = E
162168
log.debug('connect failed, trying again in 1 second...')
163169
time.sleep(1.5)
164170
tries -= 1
@@ -173,17 +179,19 @@ def _connect(self):
173179
serial_number = d['serial_number']
174180
interface_number = d['interface_number']
175181
usage_page = d['usage_page']
176-
path = d['path']
182+
self.path = d['path']
177183

178184
if (vendor_id, product_id) in DEVICE_IDS:
179185
if serial_number == '1000000000':
180186
if usage_page == 0xffab or interface_number == 2:
181-
self._hid = hid.Device(vendor_id, product_id, path=path)
182-
self._hid.nonblocking = True
187+
self._hid = hid.device()
188+
self._hid.open_path(self.path)
189+
self._hid.set_nonblocking(True)
183190
else:
184191
if usage_page == 0xf1d0 or interface_number == 1:
185-
self._hid = hid.Device(vendor_id, product_id, path=path)
186-
self._hid.nonblocking = True
192+
self._hid = hid.device()
193+
self._hid.open_path(self.path)
194+
self._hid.set_nonblocking(True)
187195

188196
except:
189197
log.exception('failed to connect')
@@ -203,7 +211,7 @@ def set_time(self, timestamp):
203211
log.debug('Setting current epoch time =', current_epoch_time)
204212
payload = [int(current_epoch_time[i: i+2], 16) for i in range(0, len(current_epoch_time), 2)]
205213

206-
log.debug('SENDING OKSETTIME:', [x for x in enumerate(payload)]);
214+
log.debug('SENDING OKSETTIME:', [x for x in enumerate(payload)])
207215
self.send_message(msg=Message.OKSETTIME, payload=payload)
208216

209217
def set_ecc_key(self, key_type, slot, key):
@@ -329,7 +337,7 @@ def send_large_message3(self, payload=None, msg=None, slot_id=101, key_type=1):
329337

330338
def read_bytes(self, n=64, to_str=False, timeout_ms=100):
331339
"""Read n bytes and return an array of uint8 (int)."""
332-
out = self._hid.read(n, timeout=timeout_ms)
340+
out = self._hid.read(n)
333341
if to_str:
334342
# Returns the bytes a string if requested
335343
return out.hex()
@@ -339,7 +347,8 @@ def read_bytes(self, n=64, to_str=False, timeout_ms=100):
339347

340348
def read_string(self, timeout_ms=100):
341349
"""Read an ASCII string."""
342-
return self.read_chunk(timeout_ms=timeout_ms).decode("ascii")
350+
return ''.join([chr(item) for item in self.read_bytes(MAX_INPUT_REPORT_SIZE, timeout_ms=timeout_ms) if item != 0])
351+
343352

344353
def read_chunk(self, timeout_ms=100):
345354
return self.read_bytes(MAX_INPUT_REPORT_SIZE, timeout_ms=timeout_ms)
@@ -388,6 +397,7 @@ def displaykeylabels(self):
388397

389398
time.sleep(1)
390399
print('You should see your OnlyKey blink 3 times\n')
400+
print()
391401

392402
tmp = {}
393403
for slot in self.getkeylabels():

0 commit comments

Comments
 (0)