diff --git a/python/src/trezorlib/btc.py b/python/src/trezorlib/btc.py index 8b77a189b4..e1a43f3d11 100644 --- a/python/src/trezorlib/btc.py +++ b/python/src/trezorlib/btc.py @@ -16,8 +16,8 @@ from decimal import Decimal -from . import messages -from .tools import CallException, expect, normalize_nfc, session +from . import exceptions, messages +from .tools import expect, normalize_nfc, session def from_json(json_dict): @@ -114,8 +114,8 @@ def verify_message(client, coin_name, address, signature, message): coin_name=coin_name, ) ) - except CallException as e: - resp = e + except exceptions.TrezorFailure as e: + return False return isinstance(resp, messages.Success) @@ -197,13 +197,10 @@ def sign_tx(client, coin_name, inputs, outputs, details=None, prev_txes=None): msg.extra_data = current_tx.extra_data[o : o + l] res = client.call(messages.TxAck(tx=msg)) - if isinstance(res, messages.Failure): - raise CallException("Signing failed") - if not isinstance(res, messages.TxRequest): - raise CallException("Unexpected message") + raise exceptions.TrezorException("Unexpected message") if None in signatures: - raise RuntimeError("Some signatures are missing!") + raise exceptions.TrezorException("Some signatures are missing!") return signatures, serialized_tx diff --git a/python/src/trezorlib/eos.py b/python/src/trezorlib/eos.py index 61fdff1951..d2e8176eff 100644 --- a/python/src/trezorlib/eos.py +++ b/python/src/trezorlib/eos.py @@ -16,8 +16,8 @@ from datetime import datetime -from . import messages -from .tools import CallException, b58decode, expect, session +from . import exceptions, messages +from .tools import b58decode, expect, session def name_to_number(name): @@ -337,12 +337,13 @@ def sign_tx(client, address, transaction, chain_id): response = client.call(actions.pop(0)) except IndexError: # pop from empty list - raise CallException( - "Eos.UnexpectedEndOfOperations", - "Reached end of operations without a signature.", + raise exceptions.TrezorException( + "Reached end of operations without a signature." ) from None if not isinstance(response, messages.EosSignedTx): - raise CallException(messages.FailureType.UnexpectedMessage, response) + raise exceptions.TrezorException( + "Unexpected message: {}".format(response.__class__.__name__) + ) return response diff --git a/python/src/trezorlib/ethereum.py b/python/src/trezorlib/ethereum.py index cd3719694e..76a6121c4f 100644 --- a/python/src/trezorlib/ethereum.py +++ b/python/src/trezorlib/ethereum.py @@ -14,8 +14,8 @@ # You should have received a copy of the License along with this library. # If not, see . -from . import messages as proto -from .tools import CallException, expect, normalize_nfc, session +from . import exceptions, messages +from .tools import expect, normalize_nfc, session def int_to_big_endian(value): @@ -25,15 +25,17 @@ def int_to_big_endian(value): # ====== Client functions ====== # -@expect(proto.EthereumAddress, field="address") +@expect(messages.EthereumAddress, field="address") def get_address(client, n, show_display=False, multisig=None): - return client.call(proto.EthereumGetAddress(address_n=n, show_display=show_display)) + return client.call( + messages.EthereumGetAddress(address_n=n, show_display=show_display) + ) -@expect(proto.EthereumPublicKey) +@expect(messages.EthereumPublicKey) def get_public_node(client, n, show_display=False): return client.call( - proto.EthereumGetPublicKey(address_n=n, show_display=show_display) + messages.EthereumGetPublicKey(address_n=n, show_display=show_display) ) @@ -50,7 +52,7 @@ def sign_tx( chain_id=None, tx_type=None, ): - msg = proto.EthereumSignTx( + msg = messages.EthereumSignTx( address_n=n, nonce=int_to_big_endian(nonce), gas_price=int_to_big_endian(gas_price), @@ -71,7 +73,7 @@ def sign_tx( while response.data_length is not None: data_length = response.data_length data, chunk = data[data_length:], data[:data_length] - response = client.call(proto.EthereumTxAck(data_chunk=chunk)) + response = client.call(messages.EthereumTxAck(data_chunk=chunk)) # https://github.com/trezor/trezor-core/pull/311 # only signature bit returned. recalculate signature_v @@ -81,22 +83,20 @@ def sign_tx( return response.signature_v, response.signature_r, response.signature_s -@expect(proto.EthereumMessageSignature) +@expect(messages.EthereumMessageSignature) def sign_message(client, n, message): message = normalize_nfc(message) - return client.call(proto.EthereumSignMessage(address_n=n, message=message)) + return client.call(messages.EthereumSignMessage(address_n=n, message=message)) def verify_message(client, address, signature, message): message = normalize_nfc(message) try: resp = client.call( - proto.EthereumVerifyMessage( + messages.EthereumVerifyMessage( address=address, signature=signature, message=message ) ) - except CallException as e: - resp = e - if isinstance(resp, proto.Success): - return True - return False + except exceptions.TrezorFailure: + return False + return isinstance(resp, messages.Success) diff --git a/python/src/trezorlib/lisk.py b/python/src/trezorlib/lisk.py index 2cd0354d17..79d572ab39 100644 --- a/python/src/trezorlib/lisk.py +++ b/python/src/trezorlib/lisk.py @@ -14,45 +14,47 @@ # You should have received a copy of the License along with this library. # If not, see . -from . import messages as proto +from . import exceptions, messages from .protobuf import dict_to_proto -from .tools import CallException, dict_from_camelcase, expect, normalize_nfc +from .tools import dict_from_camelcase, expect, normalize_nfc -@expect(proto.LiskAddress, field="address") +@expect(messages.LiskAddress, field="address") def get_address(client, n, show_display=False): - return client.call(proto.LiskGetAddress(address_n=n, show_display=show_display)) + return client.call(messages.LiskGetAddress(address_n=n, show_display=show_display)) -@expect(proto.LiskPublicKey) +@expect(messages.LiskPublicKey) def get_public_key(client, n, show_display=False): - return client.call(proto.LiskGetPublicKey(address_n=n, show_display=show_display)) + return client.call( + messages.LiskGetPublicKey(address_n=n, show_display=show_display) + ) -@expect(proto.LiskMessageSignature) +@expect(messages.LiskMessageSignature) def sign_message(client, n, message): message = normalize_nfc(message) - return client.call(proto.LiskSignMessage(address_n=n, message=message)) + return client.call(messages.LiskSignMessage(address_n=n, message=message)) def verify_message(client, pubkey, signature, message): message = normalize_nfc(message) try: resp = client.call( - proto.LiskVerifyMessage( + messages.LiskVerifyMessage( signature=signature, public_key=pubkey, message=message ) ) - except CallException as e: - resp = e - return isinstance(resp, proto.Success) + except exceptions.TrezorFailure: + return False + return isinstance(resp, messages.Success) RENAMES = {"lifetime": "life_time", "keysgroup": "keys_group"} -@expect(proto.LiskSignedTx) +@expect(messages.LiskSignedTx) def sign_tx(client, n, transaction): transaction = dict_from_camelcase(transaction, renames=RENAMES) - msg = dict_to_proto(proto.LiskTransactionCommon, transaction) - return client.call(proto.LiskSignTx(address_n=n, transaction=msg)) + msg = dict_to_proto(messages.LiskTransactionCommon, transaction) + return client.call(messages.LiskSignTx(address_n=n, transaction=msg)) diff --git a/python/src/trezorlib/nem.py b/python/src/trezorlib/nem.py index 57f03e50de..92752fefe6 100644 --- a/python/src/trezorlib/nem.py +++ b/python/src/trezorlib/nem.py @@ -16,8 +16,8 @@ import json -from . import messages as proto -from .tools import CallException, expect +from . import exceptions, messages +from .tools import expect TYPE_TRANSACTION_TRANSFER = 0x0101 TYPE_IMPORTANCE_TRANSFER = 0x0801 @@ -30,7 +30,7 @@ TYPE_MOSAIC_SUPPLY_CHANGE = 0x4002 def create_transaction_common(transaction): - msg = proto.NEMTransactionCommon() + msg = messages.NEMTransactionCommon() msg.network = (transaction["version"] >> 24) & 0xFF msg.timestamp = transaction["timeStamp"] msg.fee = transaction["fee"] @@ -43,7 +43,7 @@ def create_transaction_common(transaction): def create_transfer(transaction): - msg = proto.NEMTransfer() + msg = messages.NEMTransfer() msg.recipient = transaction["recipient"] msg.amount = transaction["amount"] @@ -55,7 +55,7 @@ def create_transfer(transaction): if "mosaics" in transaction: msg.mosaics = [ - proto.NEMMosaic( + messages.NEMMosaic( namespace=mosaic["mosaicId"]["namespaceId"], mosaic=mosaic["mosaicId"]["name"], quantity=mosaic["quantity"], @@ -67,9 +67,9 @@ def create_transfer(transaction): def create_aggregate_modification(transactions): - msg = proto.NEMAggregateModification() + msg = messages.NEMAggregateModification() msg.modifications = [ - proto.NEMCosignatoryModification( + messages.NEMCosignatoryModification( type=modification["modificationType"], public_key=bytes.fromhex(modification["cosignatoryAccount"]), ) @@ -83,7 +83,7 @@ def create_aggregate_modification(transactions): def create_provision_namespace(transaction): - msg = proto.NEMProvisionNamespace() + msg = messages.NEMProvisionNamespace() msg.namespace = transaction["newPart"] if transaction["parent"]: @@ -96,8 +96,8 @@ def create_provision_namespace(transaction): def create_mosaic_creation(transaction): definition = transaction["mosaicDefinition"] - msg = proto.NEMMosaicCreation() - msg.definition = proto.NEMMosaicDefinition() + msg = messages.NEMMosaicCreation() + msg.definition = messages.NEMMosaicDefinition() msg.definition.namespace = definition["id"]["namespaceId"] msg.definition.mosaic = definition["id"]["name"] @@ -129,7 +129,7 @@ def create_mosaic_creation(transaction): def create_supply_change(transaction): - msg = proto.NEMMosaicSupplyChange() + msg = messages.NEMMosaicSupplyChange() msg.namespace = transaction["mosaicId"]["namespaceId"] msg.mosaic = transaction["mosaicId"]["name"] msg.type = transaction["supplyType"] @@ -138,7 +138,7 @@ def create_supply_change(transaction): def create_importance_transfer(transaction): - msg = proto.NEMImportanceTransfer() + msg = messages.NEMImportanceTransfer() msg.mode = transaction["importanceTransfer"]["mode"] msg.public_key = bytes.fromhex(transaction["importanceTransfer"]["publicKey"]) return msg @@ -162,7 +162,7 @@ def fill_transaction_by_type(msg, transaction): def create_sign_tx(transaction): - msg = proto.NEMSignTx() + msg = messages.NEMSignTx() msg.transaction = create_transaction_common(transaction) msg.cosigning = transaction["type"] == TYPE_MULTISIG_SIGNATURE @@ -181,19 +181,19 @@ def create_sign_tx(transaction): # ====== Client functions ====== # -@expect(proto.NEMAddress, field="address") +@expect(messages.NEMAddress, field="address") def get_address(client, n, network, show_display=False): return client.call( - proto.NEMGetAddress(address_n=n, network=network, show_display=show_display) + messages.NEMGetAddress(address_n=n, network=network, show_display=show_display) ) -@expect(proto.NEMSignedTx) +@expect(messages.NEMSignedTx) def sign_tx(client, n, transaction): try: msg = create_sign_tx(transaction) except ValueError as e: - raise CallException(e.args) + raise exceptions.TrezorException("Failed to encode transaction") from e assert msg.transaction is not None msg.transaction.address_n = n diff --git a/python/src/trezorlib/stellar.py b/python/src/trezorlib/stellar.py index 3307cee24d..44a2bbe41e 100644 --- a/python/src/trezorlib/stellar.py +++ b/python/src/trezorlib/stellar.py @@ -18,8 +18,8 @@ import base64 import struct import xdrlib -from . import messages -from .tools import CallException, expect +from . import exceptions, messages +from .tools import expect # Memo types MEMO_TYPE_NONE = 0 @@ -368,18 +368,18 @@ def sign_tx( resp = client.call(operations.pop(0)) except IndexError: # pop from empty list - raise CallException( - "Stellar.UnexpectedEndOfOperations", - "Reached end of operations without a signature.", + raise exceptions.TrezorException( + "Reached end of operations without a signature." ) from None if not isinstance(resp, messages.StellarSignedTx): - raise CallException(messages.FailureType.UnexpectedMessage, resp) + raise exceptions.TrezorException( + "Unexpected message: {}".format(resp.__class__.__name__) + ) if operations: - raise CallException( - "Stellar.UnprocessedOperations", - "Received a signature before processing all operations.", + raise exceptions.TrezorException( + "Received a signature before processing all operations." ) return resp diff --git a/python/src/trezorlib/tools.py b/python/src/trezorlib/tools.py index 96540b7337..1934a0ddbb 100644 --- a/python/src/trezorlib/tools.py +++ b/python/src/trezorlib/tools.py @@ -23,8 +23,6 @@ from typing import List, NewType from .exceptions import TrezorFailure -CallException = TrezorFailure - HARDENED_FLAG = 1 << 31 Address = NewType("Address", List[int]) diff --git a/tests/device_tests/test_msg_getaddress.py b/tests/device_tests/test_msg_getaddress.py index 8791f1ff91..df28701128 100644 --- a/tests/device_tests/test_msg_getaddress.py +++ b/tests/device_tests/test_msg_getaddress.py @@ -17,7 +17,8 @@ import pytest from trezorlib import btc, messages as proto -from trezorlib.tools import H_, CallException, parse_path +from trezorlib.exceptions import TrezorFailure +from trezorlib.tools import H_, parse_path from .. import bip32 from ..common import MNEMONIC12 @@ -163,7 +164,7 @@ class TestMsgGetaddress: node = btc.get_public_node(client, parse_path("44'/0'/%d'" % n)) xpubs.append(node.xpub) for nr in range(1, 4): - with pytest.raises(CallException): + with pytest.raises(TrezorFailure): btc.get_address( client, "Bitcoin", @@ -171,7 +172,7 @@ class TestMsgGetaddress: show_display=(nr == 1), multisig=getmultisig(0, 0, xpubs=xpubs), ) - with pytest.raises(CallException): + with pytest.raises(TrezorFailure): btc.get_address( client, "Bitcoin", diff --git a/tests/device_tests/test_msg_getpublickey_curve.py b/tests/device_tests/test_msg_getpublickey_curve.py index 69db020560..f5b5b4ba41 100644 --- a/tests/device_tests/test_msg_getpublickey_curve.py +++ b/tests/device_tests/test_msg_getpublickey_curve.py @@ -17,7 +17,8 @@ import pytest from trezorlib import btc -from trezorlib.tools import H_, CallException +from trezorlib.exceptions import TrezorFailure +from trezorlib.tools import H_ from ..common import MNEMONIC12 @@ -80,5 +81,5 @@ class TestMsgGetpublickeyCurve: == "00514f73a05184458611b14c348fee4fd988d36cf3aee7207737861bac611de991" ) # test failure when using public derivation - with pytest.raises(CallException): + with pytest.raises(TrezorFailure): btc.get_public_node(client, [H_(111), 42], ecdsa_curve_name="ed25519") diff --git a/tests/device_tests/test_msg_ripple_sign_tx.py b/tests/device_tests/test_msg_ripple_sign_tx.py index e774a3d89b..2798d8e2cc 100644 --- a/tests/device_tests/test_msg_ripple_sign_tx.py +++ b/tests/device_tests/test_msg_ripple_sign_tx.py @@ -17,7 +17,8 @@ import pytest from trezorlib import messages, ripple -from trezorlib.tools import CallException, parse_path +from trezorlib.exceptions import TrezorFailure +from trezorlib.tools import parse_path @pytest.mark.altcoin @@ -105,7 +106,7 @@ class TestMsgRippleSignTx: "Sequence": 1, } ) - with pytest.raises(CallException) as exc: + with pytest.raises(TrezorFailure) as exc: ripple.sign_tx(client, parse_path("m/44'/144'/0'/0/2"), msg) assert exc.value.args[0] == messages.FailureType.ProcessError assert exc.value.args[1].endswith( diff --git a/tests/device_tests/test_msg_signtx.py b/tests/device_tests/test_msg_signtx.py index a2e958b961..e60bec1cc9 100644 --- a/tests/device_tests/test_msg_signtx.py +++ b/tests/device_tests/test_msg_signtx.py @@ -17,7 +17,8 @@ import pytest from trezorlib import btc, messages as proto -from trezorlib.tools import H_, CallException, btc_hash, parse_path +from trezorlib.exceptions import TrezorFailure +from trezorlib.tools import H_, btc_hash, parse_path from ..common import MNEMONIC12 from ..tx_cache import TxCache @@ -490,7 +491,7 @@ class TestMsgSigntx: script_type=proto.OutputScriptType.PAYTOADDRESS, ) - with pytest.raises(CallException) as exc: + with pytest.raises(TrezorFailure) as exc: check_sign_tx( client, "Bitcoin", @@ -605,7 +606,7 @@ class TestMsgSigntx: # Set up attack processors client.set_filter(proto.TxAck, attack_processor) - with pytest.raises(CallException) as exc: + with pytest.raises(TrezorFailure) as exc: btc.sign_tx( client, "Bitcoin", @@ -661,7 +662,7 @@ class TestMsgSigntx: # Set up attack processors client.set_filter(proto.TxAck, attack_processor) - with pytest.raises(CallException) as exc: + with pytest.raises(TrezorFailure) as exc: btc.sign_tx( client, "Testnet", [inp1], [out1, out2], prev_txes=TxCache("Testnet") ) @@ -762,7 +763,7 @@ class TestMsgSigntx: ] ) # Now run the attack, must trigger the exception - with pytest.raises(CallException) as exc: + with pytest.raises(TrezorFailure) as exc: btc.sign_tx( client, "Testnet", diff --git a/tests/device_tests/test_msg_signtx_bcash.py b/tests/device_tests/test_msg_signtx_bcash.py index 4d4d74a1e6..a003650aa9 100644 --- a/tests/device_tests/test_msg_signtx_bcash.py +++ b/tests/device_tests/test_msg_signtx_bcash.py @@ -17,7 +17,8 @@ import pytest from trezorlib import btc, messages as proto -from trezorlib.tools import H_, CallException, parse_path +from trezorlib.exceptions import TrezorFailure +from trezorlib.tools import H_, parse_path from ..tx_cache import TxCache @@ -329,7 +330,7 @@ class TestMsgSigntxBch: ] ) - with pytest.raises(CallException) as exc: + with pytest.raises(TrezorFailure) as exc: btc.sign_tx(client, "Bcash", [inp1, inp2], [out1], prev_txes=TX_API) assert exc.value.args[0] in ( @@ -398,7 +399,7 @@ class TestMsgSigntxBch: proto.Failure(code=proto.FailureType.ProcessError), ] ) - with pytest.raises(CallException): + with pytest.raises(TrezorFailure): btc.sign_tx(client, "Bcash", [inp1], [out1, out2], prev_txes=TX_API) @pytest.mark.multisig diff --git a/tests/device_tests/test_msg_signtx_bgold.py b/tests/device_tests/test_msg_signtx_bgold.py index 3d4e427cf9..60b26bf802 100644 --- a/tests/device_tests/test_msg_signtx_bgold.py +++ b/tests/device_tests/test_msg_signtx_bgold.py @@ -17,7 +17,8 @@ import pytest from trezorlib import btc, messages as proto -from trezorlib.tools import H_, CallException, parse_path +from trezorlib.exceptions import TrezorFailure +from trezorlib.tools import H_, parse_path from ..tx_cache import TxCache @@ -213,7 +214,7 @@ class TestMsgSigntxBitcoinGold: proto.Failure(code=proto.FailureType.ProcessError), ] ) - with pytest.raises(CallException): + with pytest.raises(TrezorFailure): btc.sign_tx(client, "Bgold", [inp1], [out1, out2], prev_txes=TX_API) @pytest.mark.multisig diff --git a/tests/device_tests/test_msg_signtx_segwit.py b/tests/device_tests/test_msg_signtx_segwit.py index fba9b77b7d..8e6f135b7b 100644 --- a/tests/device_tests/test_msg_signtx_segwit.py +++ b/tests/device_tests/test_msg_signtx_segwit.py @@ -17,7 +17,8 @@ import pytest from trezorlib import btc, messages as proto -from trezorlib.tools import H_, CallException, parse_path +from trezorlib.exceptions import TrezorFailure +from trezorlib.tools import H_, parse_path from ..tx_cache import TxCache @@ -412,7 +413,7 @@ class TestMsgSigntxSegwit: proto.Failure(code=proto.FailureType.ProcessError), ] ) - with pytest.raises(CallException) as exc: + with pytest.raises(TrezorFailure) as exc: btc.sign_tx(client, "Testnet", [inp1], [out1, out2], prev_txes=TX_API) assert exc.value.args[0] == proto.FailureType.ProcessError if client.features.model == "1": diff --git a/tests/device_tests/test_msg_stellar_get_address.py b/tests/device_tests/test_msg_stellar_get_address.py index 19268538b7..605d768c34 100644 --- a/tests/device_tests/test_msg_stellar_get_address.py +++ b/tests/device_tests/test_msg_stellar_get_address.py @@ -17,7 +17,8 @@ import pytest from trezorlib import messages as proto, stellar -from trezorlib.tools import CallException, parse_path +from trezorlib.exceptions import TrezorFailure +from trezorlib.tools import parse_path from ..common import MNEMONIC12 @@ -44,7 +45,7 @@ class TestMsgStellarGetAddress: assert address == "GBAW5XGWORWVFE2XTJYDTLDHXTY2Q2MO73HYCGB3XMFMQ562Q2W2GJQX" def test_stellar_get_address_fail(self, client): - with pytest.raises(CallException) as exc: + with pytest.raises(TrezorFailure) as exc: stellar.get_address(client, parse_path("m/0/1")) if client.features.model == "1": diff --git a/tests/device_tests/test_multisig.py b/tests/device_tests/test_multisig.py index 1cb89b6c0c..df971e8f28 100644 --- a/tests/device_tests/test_multisig.py +++ b/tests/device_tests/test_multisig.py @@ -17,7 +17,8 @@ import pytest from trezorlib import btc, messages as proto -from trezorlib.tools import CallException, parse_path +from trezorlib.exceptions import TrezorFailure +from trezorlib.tools import parse_path from .. import bip32 from ..common import MNEMONIC12 @@ -297,7 +298,7 @@ class TestMultisig: script_type=proto.OutputScriptType.PAYTOADDRESS, ) - with pytest.raises(CallException) as exc: + with pytest.raises(TrezorFailure) as exc: btc.sign_tx(client, "Bitcoin", [inp1], [out1], prev_txes=TX_API) assert exc.value.args[0] == proto.FailureType.DataError diff --git a/tests/device_tests/test_op_return.py b/tests/device_tests/test_op_return.py index 378afa7d04..794074c6f6 100644 --- a/tests/device_tests/test_op_return.py +++ b/tests/device_tests/test_op_return.py @@ -17,7 +17,8 @@ import pytest from trezorlib import btc, messages as proto -from trezorlib.tools import CallException, parse_path +from trezorlib.exceptions import TrezorFailure +from trezorlib.tools import parse_path from ..tx_cache import TxCache @@ -168,7 +169,7 @@ class TestOpReturn: ] ) - with pytest.raises(CallException) as exc: + with pytest.raises(TrezorFailure) as exc: btc.sign_tx(client, "Bitcoin", [inp1], [out1], prev_txes=TX_API) if client.features.model == "1":