From b400448b97536a0bd7cae297ea49a94de2ed9c72 Mon Sep 17 00:00:00 2001 From: Jan Pochyla Date: Wed, 23 Nov 2016 14:51:05 +0100 Subject: [PATCH] apps: adjust to last commit --- src/apps/common/signtx.py | 44 +++++++++++----------- src/apps/management/layout_load_device.py | 23 ++++------- src/apps/management/layout_reset_device.py | 12 +++--- src/apps/wallet/layout_get_address.py | 15 +++----- src/apps/wallet/layout_get_public_key.py | 3 +- src/apps/wallet/layout_sign_identity.py | 14 +++---- src/apps/wallet/layout_sign_message.py | 4 +- src/apps/wallet/layout_sign_tx.py | 21 ++++++----- src/apps/wallet/layout_verify_message.py | 5 ++- 9 files changed, 65 insertions(+), 76 deletions(-) diff --git a/src/apps/common/signtx.py b/src/apps/common/signtx.py index d9f1343a5..4b44c0fa9 100644 --- a/src/apps/common/signtx.py +++ b/src/apps/common/signtx.py @@ -101,11 +101,11 @@ def request_tx_finish(tx_req: TxRequest): async def sign_tx(tx: SignTx, root): - tx_version = getattr(tx, 'version', 1) - tx_lock_time = getattr(tx, 'lock_time', 0) - tx_inputs_count = getattr(tx, 'inputs_count', 0) - tx_outputs_count = getattr(tx, 'outputs_count', 0) - coin_name = getattr(tx, 'coin_name', 'Bitcoin') + tx_version = tx.version if tx.version is not None else 1 + tx_lock_time = tx.lock_time or 0 + tx_inputs_count = tx.inputs_count or 0 + tx_outputs_count = tx.outputs_count or 0 + coin_name = tx.coin_name or 'Bitcoin' coin = coins.by_name(coin_name) @@ -265,10 +265,10 @@ async def get_prevtx_output_value(tx_req: TxRequest, prev_hash: bytes, prev_inde # STAGE_REQUEST_2_PREV_META tx = await request_tx_meta(tx_req, prev_hash) - tx_version = getattr(tx, 'version', 0) - tx_lock_time = getattr(tx, 'lock_time', 1) - tx_inputs_count = getattr(tx, 'inputs_cnt', 0) - tx_outputs_count = getattr(tx, 'outputs_cnt', 0) + tx_version = tx.version if tx.version is not None else 1 + tx_lock_time = tx.lock_time or 0 + tx_inputs_count = tx.inputs_cnt or 0 + tx_outputs_count = tx.outputs_cnt or 0 txh = HashWriter(sha256) @@ -339,17 +339,16 @@ def output_derive_script(o: TxOutputType, coin: CoinType, root) -> bytes: 'Invalid output script type') -def output_paytoaddress_extract_raw_address(o: TxOutputType, coin: CoinType, root, p2sh=False) -> bytes: +def output_paytoaddress_extract_raw_address( + o: TxOutputType, coin: CoinType, root, p2sh: bool=False) -> bytes: addr_type = coin.address_type_p2sh if p2sh else coin.address_type # TODO: dont encode/decode more then necessary - address_n = getattr(o, 'address_n', None) - if address_n is not None: - node = node_derive(root, address_n) + if o.address_n is not None: + node = node_derive(root, o.address_n) address = node.address(addr_type) return base58.decode_check(address) - address = getattr(o, 'address', None) - if address: - raw = base58.decode_check(address) + if o.address: + raw = base58.decode_check(o.address) if not address_type.check(addr_type, raw): raise SigningError(FailureType.SyntaxError, 'Invalid address type') @@ -358,9 +357,8 @@ def output_paytoaddress_extract_raw_address(o: TxOutputType, coin: CoinType, roo 'Missing address') -def output_is_change(o: TxOutputType): - address_n = getattr(o, 'address_n', None) - return bool(address_n) +def output_is_change(o: TxOutputType) -> bool: + return bool(o.address_n) # Tx Inputs @@ -368,9 +366,9 @@ def output_is_change(o: TxOutputType): def input_derive_script(i: TxInputType, pubkey: bytes, signature: bytes=None) -> bytes: - i_script_type = getattr(i, 'script_type', InputScriptType.SPENDADDRESS) + script_type = i.script_type if i.script_type is not None else InputScriptType.SPENDADDRESS - if i_script_type == InputScriptType.SPENDADDRESS: + if script_type == InputScriptType.SPENDADDRESS: if signature is None: return script_paytoaddress_new(ecdsa_hash_pubkey(pubkey)) else: @@ -452,7 +450,7 @@ def script_spendaddress_new(pubkey: bytes, signature: bytes) -> bytearray: def write_tx_input(w, i: TxInputType): - i_sequence = getattr(i, 'sequence', 4294967295) + i_sequence = i.sequence if i.sequence is not None else 4294967295 write_bytes_rev(w, i.prev_hash) write_uint32(w, i.prev_index) write_varint(w, len(i.script_sig)) @@ -461,7 +459,7 @@ def write_tx_input(w, i: TxInputType): def write_tx_input_check(w, i: TxInputType): - i_sequence = getattr(i, 'sequence', 4294967295) + i_sequence = i.sequence if i.sequence is not None else 4294967295 write_bytes(w, i.prev_hash) write_uint32(w, i.prev_index) write_uint32(w, len(i.address_n)) diff --git a/src/apps/management/layout_load_device.py b/src/apps/management/layout_load_device.py index 3e21bcf00..015a32458 100644 --- a/src/apps/management/layout_load_device.py +++ b/src/apps/management/layout_load_device.py @@ -3,7 +3,7 @@ from trezor.utils import unimport @unimport -async def layout_load_device(message, session_id): +async def layout_load_device(msg, session_id): from trezor.crypto import bip39 from trezor.messages.Success import Success from trezor.messages.FailureType import UnexpectedMessage, Other @@ -14,17 +14,10 @@ async def layout_load_device(message, session_id): if storage.is_initialized(): raise wire.FailureError(UnexpectedMessage, 'Already initialized') - if hasattr(message, 'node'): + if msg.node is not None: raise wire.FailureError(Other, 'LoadDevice.node is not supported') - skip_checksum = getattr(message, 'skip_checksum', False) - mnemonic = getattr(message, 'mnemonic') - pin = getattr(message, 'pin', None) - label = getattr(message, 'label', None) - language = getattr(message, 'language', None) - passphrase_protection = getattr(message, 'passphrase_protection', False) - - if not skip_checksum and not bip39.check(mnemonic): + if not msg.skip_checksum and not bip39.check(msg.mnemonic): raise wire.FailureError(Other, 'Mnemonic is not valid') await require_confirm(session_id, Text( @@ -32,10 +25,10 @@ async def layout_load_device(message, session_id): ui.BOLD, 'Loading private seed', 'is not recommended.', ui.NORMAL, 'Continue only if you', 'know what you are doing!')) - storage.load_mnemonic(mnemonic) - storage.load_settings(pin=pin, - passphrase_protection=passphrase_protection, - language=language, - label=label) + storage.load_mnemonic(msg.mnemonic) + storage.load_settings(pin=msg.pin, + passphrase_protection=msg.passphrase_protection, + language=msg.language, + label=msg.label) return Success(message='Device loaded') diff --git a/src/apps/management/layout_reset_device.py b/src/apps/management/layout_reset_device.py index 0b4d7dcba..86d3e8aea 100644 --- a/src/apps/management/layout_reset_device.py +++ b/src/apps/management/layout_reset_device.py @@ -4,7 +4,7 @@ from trezor.utils import unimport, chunks @unimport -async def layout_reset_device(message, session_id): +async def layout_reset_device(msg, session_id): from trezor.messages.Success import Success from trezor.messages.FailureType import UnexpectedMessage from ..common.request_pin import request_pin_twice @@ -14,20 +14,20 @@ async def layout_reset_device(message, session_id): raise wire.FailureError(UnexpectedMessage, 'Already initialized') mnemonic = await generate_mnemonic( - message.strength, message.display_random, session_id) + msg.strength, msg.display_random, session_id) await show_mnemonic(mnemonic) - if message.pin_protection: + if msg.pin_protection: pin = await request_pin_twice(session_id) else: pin = None storage.load_mnemonic(mnemonic) storage.load_settings(pin=pin, - passphrase_protection=message.passphrase_protection, - language=message.language, - label=message.label) + passphrase_protection=msg.passphrase_protection, + language=msg.language, + label=msg.label) return Success(message='Initialized') diff --git a/src/apps/wallet/layout_get_address.py b/src/apps/wallet/layout_get_address.py index b77553bd5..73301be05 100644 --- a/src/apps/wallet/layout_get_address.py +++ b/src/apps/wallet/layout_get_address.py @@ -9,23 +9,18 @@ async def layout_get_address(msg, session_id): from ..common.seed import get_node from ..common import coins - address_n = getattr(msg, 'address_n', ()) - coin_name = getattr(msg, 'coin_name', 'Bitcoin') - multisig = getattr(msg, 'multisig', None) - show_display = getattr(msg, 'show_display', False) - - # TODO: support multisig addresses - - if multisig: + if msg.multisig: raise wire.FailureError(Other, 'GetAddress.multisig is unsupported') + address_n = msg.address_n or () + coin_name = msg.coin_name or 'Bitcoin' node = await get_node(session_id, address_n) - coin = coins.by_name(coin_name) address = node.address(coin.address_type) - if show_display: + if msg.show_display: await _show_address(session_id, address) + return Address(address=address) diff --git a/src/apps/wallet/layout_get_public_key.py b/src/apps/wallet/layout_get_public_key.py index 55ce65c71..ce1062f0f 100644 --- a/src/apps/wallet/layout_get_public_key.py +++ b/src/apps/wallet/layout_get_public_key.py @@ -7,8 +7,7 @@ async def layout_get_public_key(msg, session_id): from trezor.messages.HDNodeType import HDNodeType from trezor.messages.PublicKey import PublicKey - address_n = getattr(msg, 'address_n', ()) - + address_n = msg.address_n or () node = await seed.get_node(session_id, address_n) node_xpub = node.serialize_public() diff --git a/src/apps/wallet/layout_sign_identity.py b/src/apps/wallet/layout_sign_identity.py index f221ad2b3..a46a48d62 100644 --- a/src/apps/wallet/layout_sign_identity.py +++ b/src/apps/wallet/layout_sign_identity.py @@ -1,4 +1,4 @@ -from trezor import wire, ui +from trezor import ui from trezor.utils import unimport @unimport @@ -12,18 +12,18 @@ async def layout_sign_identity(msg, session_id): from ..common.signverify import message_digest identity = '' - if hasattr(msg.identity, 'proto') and msg.identity.proto: + if msg.identity.proto: identity += msg.identity.proto + '://' - if hasattr(msg.identity, 'user') and msg.identity.user: + if msg.identity.user: identity += msg.identity.user + '@' - if hasattr(msg.identity, 'host') and msg.identity.host: + if msg.identity.host: identity += msg.identity.host - if hasattr(msg.identity, 'port') and msg.identity.port: + if msg.identity.port: identity += ':' + msg.identity.port - if hasattr(msg.identity, 'path') and msg.identity.path: + if msg.identity.path: identity += msg.identity.path - index = getattr(msg.identity, 'index', 0) + index = msg.identity.index or 0 identity_hash = sha256(pack('