From 53c73b3b5cc93618bf3fe2fc9e004f75f16b87c0 Mon Sep 17 00:00:00 2001 From: Jan Pochyla Date: Mon, 12 Dec 2016 15:19:51 +0100 Subject: [PATCH] apps: accomodate latest seed changes --- src/apps/ethereum/ethereum_get_address.py | 7 +- src/apps/wallet/cipher_key_value.py | 37 +++++---- src/apps/wallet/get_address.py | 4 +- src/apps/wallet/get_public_key.py | 4 +- src/apps/wallet/sign_identity.py | 97 +++++++++++++++-------- src/apps/wallet/sign_message.py | 4 +- src/apps/wallet/sign_tx/__init__.py | 2 +- 7 files changed, 95 insertions(+), 60 deletions(-) diff --git a/src/apps/ethereum/ethereum_get_address.py b/src/apps/ethereum/ethereum_get_address.py index c0e0f132..d164a61e 100644 --- a/src/apps/ethereum/ethereum_get_address.py +++ b/src/apps/ethereum/ethereum_get_address.py @@ -7,11 +7,10 @@ async def layout_ethereum_get_address(session_id, msg): from trezor.messages.EthereumAddress import EthereumAddress from trezor.crypto.curve import secp256k1 from trezor.crypto.hashlib import sha3_256 - from ..common.seed import get_node + from ..common import seed - address_n = msg.address_n or () - - node = await get_node(session_id, address_n) + node = await seed.get_root(session_id) + node.derive_path(msg.address_n or ()) seckey = node.private_key() public_key = secp256k1.publickey(seckey, False) # uncompressed diff --git a/src/apps/wallet/cipher_key_value.py b/src/apps/wallet/cipher_key_value.py index d5b9acfa..7657e717 100644 --- a/src/apps/wallet/cipher_key_value.py +++ b/src/apps/wallet/cipher_key_value.py @@ -2,25 +2,11 @@ from trezor import ui from trezor.utils import unimport -@unimport -async def layout_cipher_key_value(session_id, msg): - from trezor.messages.CipheredKeyValue import CipheredKeyValue - from ..common import seed +def cipher_key_value(msg, seckey: bytes) -> bytes: from trezor.crypto.hashlib import sha512 from trezor.crypto import hmac from trezor.crypto.aes import AES_CBC_Encrypt, AES_CBC_Decrypt - if len(msg.value) % 16 > 0: - raise ValueError('Value length must be a multiple of 16') - - ui.display.clear() - ui.display.text(10, 30, 'CipherKeyValue', - ui.BOLD, ui.LIGHT_GREEN, ui.BLACK) - ui.display.text(10, 60, msg.key, ui.MONO, ui.WHITE, ui.BLACK) - - node = await seed.get_node(session_id, msg.address_n) - seckey = node.private_key() - data = msg.key data += 'E1' if msg.ask_on_encrypt else 'E0' data += 'D1' if msg.ask_on_decrypt else 'D0' @@ -36,6 +22,25 @@ async def layout_cipher_key_value(session_id, msg): else: aes = AES_CBC_Decrypt(key=key, iv=iv) - value = aes.update(msg.value) + return aes.update(msg.value) + + +@unimport +async def layout_cipher_key_value(session_id, msg): + from trezor.messages.CipheredKeyValue import CipheredKeyValue + from ..common import seed + + if len(msg.value) % 16 > 0: + raise ValueError('Value length must be a multiple of 16') + + ui.display.clear() + ui.display.text(10, 30, 'CipherKeyValue', + ui.BOLD, ui.LIGHT_GREEN, ui.BLACK) + ui.display.text(10, 60, msg.key, ui.MONO, ui.WHITE, ui.BLACK) + + node = await seed.get_root(session_id) + node.derive_path(msg.address_n) + + value = cipher_key_value(msg, node.private_key()) return CipheredKeyValue(value=value) diff --git a/src/apps/wallet/get_address.py b/src/apps/wallet/get_address.py index c06a2108..51ea72cc 100644 --- a/src/apps/wallet/get_address.py +++ b/src/apps/wallet/get_address.py @@ -14,7 +14,9 @@ async def layout_get_address(session_id, msg): address_n = msg.address_n or () coin_name = msg.coin_name or 'Bitcoin' - node = await seed.get_node(session_id, address_n) + + node = await seed.get_root(session_id) + node.derive_path(address_n) coin = coins.by_name(coin_name) address = node.address(coin.address_type) diff --git a/src/apps/wallet/get_public_key.py b/src/apps/wallet/get_public_key.py index 2de9f66d..22910a4b 100644 --- a/src/apps/wallet/get_public_key.py +++ b/src/apps/wallet/get_public_key.py @@ -7,8 +7,8 @@ async def layout_get_public_key(session_id, msg): from trezor.messages.PublicKey import PublicKey from ..common import seed - address_n = msg.address_n or () - node = await seed.get_node(session_id, address_n) + node = await seed.get_root(session_id) + node.derive_path(msg.address_n or ()) node_xpub = node.serialize_public() node_type = HDNodeType( diff --git a/src/apps/wallet/sign_identity.py b/src/apps/wallet/sign_identity.py index c1422472..8debb888 100644 --- a/src/apps/wallet/sign_identity.py +++ b/src/apps/wallet/sign_identity.py @@ -1,52 +1,79 @@ +from typing import List + from trezor import ui from trezor.utils import unimport -@unimport -async def layout_sign_identity(session_id, msg): - from trezor.messages.SignedIdentity import SignedIdentity - from trezor.crypto.curve import secp256k1 - from trezor.crypto.hashlib import sha256 + +def serialize_identity(identity): + s = '' + if identity.proto: + s += identity.proto + '://' + if identity.user: + s += identity.user + '@' + if identity.host: + s += identity.host + if identity.port: + s += ':' + identity.port + if identity.path: + s += identity.path + return s + + +def display_identity(identity: str, challenge_visual: str): + ui.display.clear() + ui.display.text(10, 30, 'Identity:', + ui.BOLD, ui.LIGHT_GREEN, ui.BLACK) + ui.display.text(10, 60, challenge_visual, ui.MONO, ui.WHITE, ui.BLACK) + ui.display.text(10, 80, identity, ui.MONO, ui.WHITE, ui.BLACK) + + +def get_identity_path(identity: str, index: int) -> List[int]: from ustruct import pack, unpack - from ..common import coins - from ..common import seed - from ..common.signverify import message_digest + from trezor.crypto.hashlib import sha256 - identity = '' - if msg.identity.proto: - identity += msg.identity.proto + '://' - if msg.identity.user: - identity += msg.identity.user + '@' - if msg.identity.host: - identity += msg.identity.host - if msg.identity.port: - identity += ':' + msg.identity.port - if msg.identity.path: - identity += msg.identity.path - - index = msg.identity.index or 0 identity_hash = sha256(pack(' bytes: + from trezor.crypto.hashlib import sha256 + from trezor.crypto.curve import secp256k1 + from ..common.signverify import message_digest - coin = coins.by_name('Bitcoin') - address = node.address(coin.address_type) # hardcoded Bitcoin address type - pubkey = node.public_key() - seckey = node.private_key() - challenge = sha256(msg.challenge_hidden).digest() + sha256(msg.challenge_visual).digest() + challenge = sha256(challenge_hidden).digest() + \ + sha256(challenge_visual).digest() digest = message_digest(coin, challenge) - signature = secp256k1.sign(seckey, digest) + return signature + + +@unimport +async def layout_sign_identity(session_id, msg): + from trezor.messages.SignedIdentity import SignedIdentity + from ..common import coins + from ..common import seed + + identity = serialize_identity(msg.identity) + display_identity(identity, msg.challenge_visual) + + address_n = get_identity_path(identity, msg.identity.index or 0) + node = await seed.get_root(session_id, msg.ecdsa_curve_name) + node.derive_path(address_n) + + coin = coins.by_name('Bitcoin') + address = node.address(coin.address_type) # hardcoded bitcoin address type + pubkey = node.public_key() + seckey = node.private_key() + + signature = sign_challenge( + seckey, msg.challenge_hidden, msg.challenge_visual, coin) + return SignedIdentity(address=address, public_key=pubkey, signature=signature) diff --git a/src/apps/wallet/sign_message.py b/src/apps/wallet/sign_message.py index c725424c..b812d9d5 100644 --- a/src/apps/wallet/sign_message.py +++ b/src/apps/wallet/sign_message.py @@ -18,7 +18,9 @@ async def layout_sign_message(session_id, msg): coin_name = msg.coin_name or 'Bitcoin' coin = coins.by_name(coin_name) - node = await seed.get_node(session_id, msg.address_n) + node = await seed.get_root(session_id) + node.derive_path(msg.address_n) + seckey = node.private_key() address = node.address(coin.address_type) diff --git a/src/apps/wallet/sign_tx/__init__.py b/src/apps/wallet/sign_tx/__init__.py index 4951c6d2..e753d133 100644 --- a/src/apps/wallet/sign_tx/__init__.py +++ b/src/apps/wallet/sign_tx/__init__.py @@ -11,7 +11,7 @@ async def sign_tx(session_id, msg): from . import signing from . import layout - root = await seed.get_root_node(session_id) + root = await seed.get_root(session_id) signer = signing.sign_tx(msg, root) res = None