1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-27 01:48:17 +00:00

client: simplify MovedTo now that we only need it to raise an error

this also removes most of client's imports, which will FINALLY let us
import client where it is needed without circular dependencies
This commit is contained in:
matejcik 2018-11-13 16:15:15 +01:00
parent ef46bd38ef
commit 4f9bdff564

View File

@ -14,26 +14,11 @@
# You should have received a copy of the License along with this library. # You should have received a copy of the License along with this library.
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>. # If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
import functools
import logging import logging
import sys import sys
import warnings import warnings
from . import ( from . import exceptions, messages as proto, tools
btc,
cosi,
device,
ethereum,
exceptions,
firmware,
lisk,
mapping,
messages as proto,
misc,
nem,
stellar,
tools,
)
if sys.version_info.major < 3: if sys.version_info.major < 3:
raise Exception("Trezorlib does not support Python 2 anymore.") raise Exception("Trezorlib does not support Python 2 anymore.")
@ -44,6 +29,12 @@ LOG = logging.getLogger(__name__)
PinException = exceptions.PinException PinException = exceptions.PinException
DEPRECATION_ERROR = """
Incompatible Trezor library detected.
(Original error: {})
""".strip()
def get_buttonrequest_value(code): def get_buttonrequest_value(code):
# Converts integer code to its string representation of ButtonRequestType # Converts integer code to its string representation of ButtonRequestType
@ -54,29 +45,6 @@ def get_buttonrequest_value(code):
][0] ][0]
class MovedTo:
"""Deprecation redirector for methods that were formerly part of TrezorClient"""
def __init__(self, where):
self.where = where
self.name = where.__module__ + "." + where.__name__
def _deprecated_redirect(self, client, *args, **kwargs):
"""Redirector for a deprecated method on TrezorClient"""
warnings.warn(
"Function has been moved to %s" % self.name,
DeprecationWarning,
stacklevel=2,
)
return self.where(client, *args, **kwargs)
def __get__(self, instance, cls):
if instance is None:
return self._deprecated_redirect
else:
return functools.partial(self._deprecated_redirect, instance)
class TrezorClient: class TrezorClient:
VENDORS = ("bitcointrezor.com", "trezor.io") VENDORS = ("bitcointrezor.com", "trezor.io")
# Implements very basic layer of sending raw protobuf # Implements very basic layer of sending raw protobuf
@ -209,6 +177,14 @@ class TrezorClient:
return self.call_raw(proto.ClearSession()) return self.call_raw(proto.ClearSession())
def MovedTo(where):
def moved_to(*args, **kwargs):
msg = "Function has been moved to " + where
raise RuntimeError(DEPRECATION_ERROR.format(msg))
return moved_to
class ProtocolMixin(object): class ProtocolMixin(object):
"""Fake mixin for old-style software that constructed TrezorClient class """Fake mixin for old-style software that constructed TrezorClient class
from separate mixins. from separate mixins.
@ -235,58 +211,63 @@ class ProtocolMixin(object):
return tools.parse_path(n) return tools.parse_path(n)
# Device functionality # Device functionality
wipe_device = MovedTo(device.wipe) wipe_device = MovedTo("device.wipe")
recovery_device = MovedTo(device.recover) recovery_device = MovedTo("device.recover")
reset_device = MovedTo(device.reset) reset_device = MovedTo("device.reset")
backup_device = MovedTo(device.backup) backup_device = MovedTo("device.backup")
set_u2f_counter = MovedTo(device.set_u2f_counter) set_u2f_counter = MovedTo("device.set_u2f_counter")
apply_settings = MovedTo(device.apply_settings) apply_settings = MovedTo("device.apply_settings")
apply_flags = MovedTo(device.apply_flags) apply_flags = MovedTo("device.apply_flags")
change_pin = MovedTo(device.change_pin) change_pin = MovedTo("device.change_pin")
# Firmware functionality # Firmware functionality
firmware_update = MovedTo(firmware.update) firmware_update = MovedTo("firmware.update")
# BTC-like functionality # BTC-like functionality
get_public_node = MovedTo(btc.get_public_node) get_public_node = MovedTo("btc.get_public_node")
get_address = MovedTo(btc.get_address) get_address = MovedTo("btc.get_address")
sign_tx = MovedTo(btc.sign_tx) sign_tx = MovedTo("btc.sign_tx")
sign_message = MovedTo(btc.sign_message) sign_message = MovedTo("btc.sign_message")
verify_message = MovedTo(btc.verify_message) verify_message = MovedTo("btc.verify_message")
# CoSi functionality # CoSi functionality
cosi_commit = MovedTo(cosi.commit) cosi_commit = MovedTo("cosi.commit")
cosi_sign = MovedTo(cosi.sign) cosi_sign = MovedTo("cosi.sign")
# Ethereum functionality # Ethereum functionality
ethereum_get_address = MovedTo(ethereum.get_address) ethereum_get_address = MovedTo("ethereum.get_address")
ethereum_sign_tx = MovedTo(ethereum.sign_tx) ethereum_sign_tx = MovedTo("ethereum.sign_tx")
ethereum_sign_message = MovedTo(ethereum.sign_message) ethereum_sign_message = MovedTo("ethereum.sign_message")
ethereum_verify_message = MovedTo(ethereum.verify_message) ethereum_verify_message = MovedTo("ethereum.verify_message")
# Lisk functionality # Lisk functionality
lisk_get_address = MovedTo(lisk.get_address) lisk_get_address = MovedTo("lisk.get_address")
lisk_get_public_key = MovedTo(lisk.get_public_key) lisk_get_public_key = MovedTo("lisk.get_public_key")
lisk_sign_message = MovedTo(lisk.sign_message) lisk_sign_message = MovedTo("lisk.sign_message")
lisk_verify_message = MovedTo(lisk.verify_message) lisk_verify_message = MovedTo("lisk.verify_message")
lisk_sign_tx = MovedTo(lisk.sign_tx) lisk_sign_tx = MovedTo("lisk.sign_tx")
# NEM functionality # NEM functionality
nem_get_address = MovedTo(nem.get_address) nem_get_address = MovedTo("nem.get_address")
nem_sign_tx = MovedTo(nem.sign_tx) nem_sign_tx = MovedTo("nem.sign_tx")
# Stellar functionality # Stellar functionality
stellar_get_address = MovedTo(stellar.get_address) stellar_get_address = MovedTo("stellar.get_address")
stellar_sign_transaction = MovedTo(stellar.sign_tx) stellar_sign_transaction = MovedTo("stellar.sign_tx")
# Miscellaneous cryptographic functionality # Miscellaneous cryptographic functionality
get_entropy = MovedTo(misc.get_entropy) get_entropy = MovedTo("misc.get_entropy")
sign_identity = MovedTo(misc.sign_identity) sign_identity = MovedTo("misc.sign_identity")
get_ecdh_session_key = MovedTo(misc.get_ecdh_session_key) get_ecdh_session_key = MovedTo("misc.get_ecdh_session_key")
encrypt_keyvalue = MovedTo(misc.encrypt_keyvalue) encrypt_keyvalue = MovedTo("misc.encrypt_keyvalue")
decrypt_keyvalue = MovedTo(misc.decrypt_keyvalue) decrypt_keyvalue = MovedTo("misc.decrypt_keyvalue")
# Debug device functionality
load_device_by_mnemonic = MovedTo("debuglink.load_device_by_mnemonic")
load_device_by_xprv = MovedTo("debuglink.load_device_by_xprv")
class BaseClient: class BaseClient:
"""Compatibility proxy for original BaseClient class. """Compatibility proxy for original BaseClient class.