From b22f0aad869878351b41844ceba47cfa5a2e0244 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Tue, 5 Jun 2018 20:21:31 +0200 Subject: [PATCH] src/apps: some import refactoring --- src/apps/common/__init__.py | 4 ++++ src/apps/ethereum/get_address.py | 2 +- src/apps/ethereum/layout.py | 11 ++++++----- src/apps/ethereum/sign_tx.py | 2 +- src/apps/fido_u2f/__init__.py | 8 ++++---- src/apps/management/change_pin.py | 4 ++-- src/apps/wallet/ecdh.py | 4 ++-- src/apps/wallet/sign_identity.py | 11 ++++++----- src/apps/wallet/sign_tx/__init__.py | 4 +++- src/apps/wallet/sign_tx/addresses.py | 4 ++-- src/apps/wallet/sign_tx/multisig.py | 2 +- src/apps/wallet/sign_tx/scripts.py | 3 ++- src/apps/wallet/sign_tx/segwit_bip143.py | 7 +++++-- src/apps/wallet/sign_tx/signing.py | 9 +++++---- src/apps/wallet/sign_tx/writers.py | 4 ++-- 15 files changed, 46 insertions(+), 33 deletions(-) diff --git a/src/apps/common/__init__.py b/src/apps/common/__init__.py index e69de29bb..52ed864f3 100644 --- a/src/apps/common/__init__.py +++ b/src/apps/common/__init__.py @@ -0,0 +1,4 @@ +from micropython import const + +HARDENED = const(0x80000000) +OVERWINTERED = const(0x80000000) diff --git a/src/apps/ethereum/get_address.py b/src/apps/ethereum/get_address.py index a5265ba5e..b6ec3dd28 100644 --- a/src/apps/ethereum/get_address.py +++ b/src/apps/ethereum/get_address.py @@ -5,7 +5,7 @@ async def ethereum_get_address(ctx, msg): from trezor.messages.EthereumAddress import EthereumAddress from trezor.crypto.curve import secp256k1 from trezor.crypto.hashlib import sha3_256 - from ..common import seed + from apps.common import seed address_n = msg.address_n or () diff --git a/src/apps/ethereum/layout.py b/src/apps/ethereum/layout.py index 9d17e0de0..ce78827a3 100644 --- a/src/apps/ethereum/layout.py +++ b/src/apps/ethereum/layout.py @@ -1,12 +1,13 @@ -from apps.common.confirm import * -from apps.ethereum import tokens +from ubinascii import hexlify + from trezor import ui from trezor.utils import chunks, format_amount from trezor.messages import ButtonRequestType from trezor.ui.text import Text -from ubinascii import hexlify -from . import networks -from .get_address import _ethereum_address_hex + +from apps.common.confirm import require_confirm, require_hold_to_confirm +from apps.ethereum import networks, tokens +from apps.ethereum.get_address import _ethereum_address_hex async def require_confirm_tx(ctx, to, value, chain_id, token=None, tx_type=None): diff --git a/src/apps/ethereum/sign_tx.py b/src/apps/ethereum/sign_tx.py index 762ddcc87..b2e46f44e 100644 --- a/src/apps/ethereum/sign_tx.py +++ b/src/apps/ethereum/sign_tx.py @@ -104,7 +104,7 @@ async def send_request_chunk(ctx, data_left: int): async def send_signature(ctx, msg: EthereumSignTx, digest): from trezor.crypto.curve import secp256k1 - from ..common import seed + from apps.common import seed address_n = msg.address_n or () node = await seed.derive_node(ctx, address_n) diff --git a/src/apps/fido_u2f/__init__.py b/src/apps/fido_u2f/__init__.py index e7109a03e..dcfdf81d1 100644 --- a/src/apps/fido_u2f/__init__.py +++ b/src/apps/fido_u2f/__init__.py @@ -15,7 +15,7 @@ from trezor.crypto import hashlib from trezor.crypto import hmac from trezor.crypto import random from trezor.crypto.curve import nist256p1 -from apps.common import storage +from apps.common import storage, HARDENED _HID_RPT_SIZE = const(64) @@ -418,7 +418,7 @@ class ConfirmContent(ui.Widget): def boot(self) -> None: from ubinascii import hexlify from trezor import res - from . import knownapps + from apps.fido_u2f import knownapps app_id = bytes(self.app_id) # could be bytearray, which doesn't have __hash__ @@ -563,7 +563,7 @@ def msg_register_sign(challenge: bytes, app_id: bytes) -> bytes: from apps.common import seed # derivation path is m/U2F'/r'/r'/r'/r'/r'/r'/r'/r' - keypath = [0x80000000 | random.uniform(0xf0000000) for _ in range(0, 8)] + keypath = [HARDENED | random.uniform(0xf0000000) for _ in range(0, 8)] nodepath = [_U2F_KEY_PATH] + keypath # prepare signing key from random path, compute decompressed public key @@ -679,7 +679,7 @@ def msg_authenticate_genkey(app_id: bytes, keyhandle: bytes): # check high bit for hardened keys for i in keypath: - if not i & 0x80000000: + if not i & HARDENED: if __debug__: log.warning(__name__, 'invalid key path') return None diff --git a/src/apps/management/change_pin.py b/src/apps/management/change_pin.py index 44cb2f2ab..71e9f7a25 100644 --- a/src/apps/management/change_pin.py +++ b/src/apps/management/change_pin.py @@ -1,7 +1,7 @@ from trezor import config, loop, ui, wire from trezor.messages import wire_types +from trezor.messages import ButtonRequestType from trezor.messages.ButtonRequest import ButtonRequest -from trezor.messages.ButtonRequestType import Other from trezor.messages.Success import Success from trezor.pin import pin_to_int, show_pin_timeout from trezor.ui.text import Text @@ -71,7 +71,7 @@ async def request_pin_confirm(ctx, *args, **kwargs): async def request_pin_ack(ctx, *args, **kwargs): try: - await ctx.call(ButtonRequest(code=Other), wire_types.ButtonAck) + await ctx.call(ButtonRequest(code=ButtonRequestType.Other), wire_types.ButtonAck) return await ctx.wait(request_pin(*args, **kwargs)) except PinCancelled: raise wire.ActionCancelled('Cancelled') diff --git a/src/apps/wallet/ecdh.py b/src/apps/wallet/ecdh.py index a69fc40ed..c40f3c238 100644 --- a/src/apps/wallet/ecdh.py +++ b/src/apps/wallet/ecdh.py @@ -7,7 +7,7 @@ from apps.common.confirm import require_confirm from apps.wallet.sign_identity import serialize_identity, serialize_identity_without_proto from trezor.ui.text import Text -from ..common import seed +from apps.common import seed, HARDENED async def get_ecdh_session_key(ctx, msg): @@ -39,7 +39,7 @@ def get_ecdh_path(identity: str, index: int): identity_hash = sha256(pack(' int: def get_tx_header(coin: CoinInfo, tx: SignTx, segwit: bool = False): w_txi = bytearray() if tx.overwintered: - write_uint32(w_txi, tx.version | 0x80000000) # nVersion | fOverwintered + write_uint32(w_txi, tx.version | OVERWINTERED) # nVersion | fOverwintered write_uint32(w_txi, coin.version_group_id) # nVersionGroupId else: write_uint32(w_txi, tx.version) # nVersion diff --git a/src/apps/wallet/sign_tx/writers.py b/src/apps/wallet/sign_tx/writers.py index b14c02598..279118579 100644 --- a/src/apps/wallet/sign_tx/writers.py +++ b/src/apps/wallet/sign_tx/writers.py @@ -1,6 +1,6 @@ from trezor.crypto.hashlib import sha256 - -from apps.wallet.sign_tx.writers import * +from trezor.messages.TxInputType import TxInputType +from trezor.messages.TxOutputBinType import TxOutputBinType # TX Serialization