diff --git a/mnemonic_check.py b/mnemonic_check.py index df0eff4..3d18d2f 100755 --- a/mnemonic_check.py +++ b/mnemonic_check.py @@ -1,5 +1,8 @@ #!/usr/bin/env python from __future__ import print_function +import binascii +import hashlib +import mnemonic __doc__ = ''' Use this script to cross-check that TREZOR generated valid @@ -13,10 +16,6 @@ __doc__ = ''' without an internet connection). ''' -import binascii -import hashlib -import mnemonic - # Python2 vs Python3 try: input = raw_input diff --git a/tests/common.py b/tests/common.py index 581d302..3a8bd90 100644 --- a/tests/common.py +++ b/tests/common.py @@ -1,10 +1,9 @@ from __future__ import print_function import unittest -import config - from trezorlib.client import TrezorDebugClient from trezorlib.tx_api import TXAPIBitcoin +import config class TrezorTest(unittest.TestCase): def setUp(self): diff --git a/tools/encfs_aes_getpass.py b/tools/encfs_aes_getpass.py index 87cc70e..dc9ea34 100755 --- a/tools/encfs_aes_getpass.py +++ b/tools/encfs_aes_getpass.py @@ -110,9 +110,9 @@ def main(): sys.stderr.write('Please confirm action on your device.\n') passw = client.decrypt_keyvalue(data['bip32_path'], - data['label'], - binascii.unhexlify(data['password_encrypted_hex']), - False, True) + data['label'], + binascii.unhexlify(data['password_encrypted_hex']), + False, True) print(passw) diff --git a/trezorctl b/trezorctl index 22e8d0b..31ad968 100755 --- a/trezorctl +++ b/trezorctl @@ -13,7 +13,7 @@ from trezorlib.client import TrezorClient, TrezorClientDebug def parse_args(commands): parser = argparse.ArgumentParser(description='Commandline tool for TREZOR devices.') parser.add_argument('-v', '--verbose', dest='verbose', action='store_true', help='Prints communication to device') - parser.add_argument('-t', '--transport', dest='transport', choices=['usb', 'udp', 'serial', 'pipe', 'socket', 'bridge'], default='usb', help="Transport used for talking with the device") + parser.add_argument('-t', '--transport', dest='transport', choices=['usb', 'udp', 'serial', 'pipe', 'socket', 'bridge'], default='usb', help="Transport used for talking with the device") parser.add_argument('-p', '--path', dest='path', default='', help="Path used by the transport (usually serial port)") # parser.add_argument('-dt', '--debuglink-transport', dest='debuglink_transport', choices=['usb', 'serial', 'pipe', 'socket'], default='usb', help="Debuglink transport") # parser.add_argument('-dp', '--debuglink-path', dest='debuglink_path', default='', help="Path used by the transport (usually serial port)") @@ -80,7 +80,7 @@ def get_transport(transport_string, path, **kwargs): from trezorlib.transport_fake import FakeTransport return FakeTransport(path, **kwargs) - raise NotImplemented("Unknown transport") + raise NotImplementedError("Unknown transport") class Commands(object): def __init__(self, client): @@ -88,7 +88,7 @@ class Commands(object): @classmethod def _list_commands(cls): - return [ x for x in dir(cls) if not x.startswith('_') ] + return [x for x in dir(cls) if not x.startswith('_')] def list(self, args): # Fake method for advertising 'list' command @@ -110,7 +110,7 @@ class Commands(object): return self.client.features def list_coins(self, args): - return [ coin.coin_name for coin in self.client.features.coins ] + return [coin.coin_name for coin in self.client.features.coins] def ping(self, args): return self.client.ping(args.msg, button_protection=args.button_protection, pin_protection=args.pin_protection, passphrase_protection=args.passphrase_protection) @@ -122,11 +122,11 @@ class Commands(object): def set_label(self, args): return self.client.apply_settings(label=args.label) - def set_homescreen(self,args): + def set_homescreen(self, args): if args.filename: from PIL import Image im = Image.open(args.filename) - if im.size != (128,64): + if im.size != (128, 64): raise Exception('Wrong size of the image') im = im.convert('1') pix = im.load() @@ -150,7 +150,7 @@ class Commands(object): def recovery_device(self, args): return self.client.recovery_device(args.words, args.passphrase_protection, - args.pin_protection, args.label, 'english') + args.pin_protection, args.label, 'english') def load_device(self, args): if not args.mnemonic and not args.xprv: @@ -159,11 +159,13 @@ class Commands(object): if args.mnemonic: mnemonic = ' '.join(args.mnemonic) return self.client.load_device_by_mnemonic(mnemonic, args.pin, - args.passphrase_protection, args.label, 'english', args.skip_checksum) - + args.passphrase_protection, + args.label, 'english', + args.skip_checksum) else: return self.client.load_device_by_xprv(args.xprv, args.pin, - args.passphrase_protection, args.label, 'english') + args.passphrase_protection, + args.label, 'english') def reset_device(self, args): return self.client.reset_device(True, args.strength, args.passphrase_protection, @@ -296,14 +298,14 @@ class Commands(object): set_label.arguments = ( (('-l', '--label',), {'type': str, 'default': ''}), -# (('-c', '--clear'), {'action': 'store_true', 'default': False}) + # (('-c', '--clear'), {'action': 'store_true', 'default': False}) ) set_homescreen.arguments = ( (('-f', '--filename',), {'type': str, 'default': ''}), ) change_pin.arguments = ( - (('-r', '--remove'), {'action': 'store_true', 'default': False}), + (('-r', '--remove'), {'action': 'store_true', 'default': False}), ) wipe_device.arguments = () diff --git a/trezorlib/ckd_public.py b/trezorlib/ckd_public.py index 3a12044..7f49d28 100644 --- a/trezorlib/ckd_public.py +++ b/trezorlib/ckd_public.py @@ -99,7 +99,7 @@ def serialize(node, version=0x0488B21E): s += node.chain_code if node.private_key: s += '\x00' + node.private_key - else : + else: s += node.public_key s += tools.Hash(s)[:4] return tools.b58encode(s) diff --git a/trezorlib/debuglink.py b/trezorlib/debuglink.py index f2c7cd5..f3120ce 100644 --- a/trezorlib/debuglink.py +++ b/trezorlib/debuglink.py @@ -51,7 +51,7 @@ class DebugLink(object): # We have to encode that into encoded pin, # because application must send back positions # on keypad, not a real PIN. - pin_encoded = ''.join([ str(matrix.index(p) + 1) for p in pin]) + pin_encoded = ''.join([str(matrix.index(p) + 1) for p in pin]) print("Encoded PIN:", pin_encoded) return pin_encoded diff --git a/trezorlib/protobuf_json.py b/trezorlib/protobuf_json.py index 042ef42..64a5f47 100644 --- a/trezorlib/protobuf_json.py +++ b/trezorlib/protobuf_json.py @@ -37,15 +37,14 @@ Provide serialization and de-serialization of Google's protobuf Messages into/fr # Note that preservation of unknown fields is currently not available for Python (c) google docs # extensions is not supported from 0.0.5 (due to gpb2.3 changes) -__version__='0.0.5' -__author__='Paul Dovbush ' - - import json from google.protobuf.descriptor import FieldDescriptor as FD import binascii from . import types_pb2 as types +__version__ = '0.0.5' +__author__ = 'Paul Dovbush ' + class ParseError(Exception): pass @@ -82,7 +81,7 @@ def pb2json(pb): js = {} # fields = pb.DESCRIPTOR.fields #all fields fields = pb.ListFields() #only filled (including extensions) - for field,value in fields: + for field, value in fields: if field.type == FD.TYPE_MESSAGE: ftype = pb2json elif field.type == FD.TYPE_ENUM: diff --git a/trezorlib/qt/pinmatrix.py b/trezorlib/qt/pinmatrix.py index 4eaabde..ce2a7c6 100644 --- a/trezorlib/qt/pinmatrix.py +++ b/trezorlib/qt/pinmatrix.py @@ -1,10 +1,8 @@ from __future__ import print_function - import sys import math -import operator from PyQt4.QtGui import (QPushButton, QLineEdit, QSizePolicy, QRegExpValidator, QLabel, - QApplication, QWidget, QGridLayout, QVBoxLayout, QHBoxLayout) + QApplication, QWidget, QGridLayout, QVBoxLayout, QHBoxLayout) from PyQt4.QtCore import QObject, SIGNAL, QRegExp, Qt class PinButton(QPushButton): diff --git a/trezorlib/transport.py b/trezorlib/transport.py index 4cdbf24..9ff25df 100644 --- a/trezorlib/transport.py +++ b/trezorlib/transport.py @@ -77,7 +77,7 @@ class Transport(object): return None data = self._read() - if data == None: + if data is None: return None return self._parse_message(data) diff --git a/trezorlib/transport_bridge.py b/trezorlib/transport_bridge.py index 5134827..75ce071 100644 --- a/trezorlib/transport_bridge.py +++ b/trezorlib/transport_bridge.py @@ -1,9 +1,8 @@ '''BridgeTransport implements transport TREZOR Bridge (aka trezord).''' -import requests import json +import requests from . import protobuf_json -from . import mapping from . import messages_pb2 as proto from .transport import Transport @@ -21,7 +20,7 @@ class BridgeTransport(Transport): self.session = None self.response = None - self.conn = requests.Session(); + self.conn = requests.Session() super(BridgeTransport, self).__init__(device, *args, **kwargs) @@ -81,7 +80,7 @@ class BridgeTransport(Transport): self.response = r.json() def _read(self): - if self.response == None: + if self.response is None: raise Exception('No response stored') cls = getattr(proto, self.response['type']) inst = cls() diff --git a/trezorlib/transport_hid.py b/trezorlib/transport_hid.py index 9b27859..57f36db 100644 --- a/trezorlib/transport_hid.py +++ b/trezorlib/transport_hid.py @@ -5,7 +5,7 @@ import time from .transport import Transport, ConnectionError DEVICE_IDS = [ -# (0x10c4, 0xea80), # TREZOR Shield + # (0x10c4, 0xea80), # TREZOR Shield (0x534c, 0x0001), # TREZOR ] diff --git a/trezorlib/transport_pipe.py b/trezorlib/transport_pipe.py index fe3ca57..5faa219 100644 --- a/trezorlib/transport_pipe.py +++ b/trezorlib/transport_pipe.py @@ -1,12 +1,11 @@ from __future__ import print_function - -'''PipeTransport implements fake wire transport over local named pipe. -Use this transport for talking with trezor simulator.''' - import os from select import select from .transport import Transport +"""PipeTransport implements fake wire transport over local named pipe. +Use this transport for talking with trezor simulator.""" + class PipeTransport(Transport): def __init__(self, device, is_device, *args, **kwargs): self.is_device = is_device # Set True if act as device diff --git a/trezorlib/transport_serial.py b/trezorlib/transport_serial.py index 6109173..e9b1cec 100644 --- a/trezorlib/transport_serial.py +++ b/trezorlib/transport_serial.py @@ -4,8 +4,8 @@ from __future__ import print_function # Local serial port loopback: socat PTY,link=COM8 PTY,link=COM9 -import serial from select import select +import serial from .transport import Transport class SerialTransport(Transport): diff --git a/trezorlib/tx_api.py b/trezorlib/tx_api.py index c7d0832..5ef2b49 100644 --- a/trezorlib/tx_api.py +++ b/trezorlib/tx_api.py @@ -1,17 +1,19 @@ import binascii -import json from decimal import Decimal # from filecache import filecache, DAY -from . import types_pb2 as proto_types import requests +from . import types_pb2 as proto_types + +def fetch_json(url): + try: + r = requests.get(url, headers={'User-agent': 'Mozilla/5.0'}) + return r.json() + except: + raise Exception('URL error: %s' % url) def insight_tx(url, rawdata=False): if not rawdata: - try: - r = requests.get(url, headers = {'User-agent': 'Mozilla/5.0'}) - data = r.json() - except: - raise Exception('URL error: %s' % url) + data = fetch_json(url) else: data = url @@ -42,11 +44,7 @@ def insight_tx(url, rawdata=False): def smartbit_tx(url, rawdata=False): if not rawdata: - try: - r = requests.get(url, headers = {'User-agent': 'Mozilla/5.0'}) - data = r.json() - except: - raise Exception('URL error: %s' % url) + data = fetch_json(url) else: data = url