2018-06-21 14:28:34 +00:00
|
|
|
# This file is part of the Trezor project.
|
2016-11-25 21:53:55 +00:00
|
|
|
#
|
2018-06-21 14:28:34 +00:00
|
|
|
# Copyright (C) 2012-2018 SatoshiLabs and contributors
|
2016-11-25 21:53:55 +00:00
|
|
|
#
|
|
|
|
# This library is free software: you can redistribute it and/or modify
|
2018-06-21 14:28:34 +00:00
|
|
|
# it under the terms of the GNU Lesser General Public License version 3
|
|
|
|
# as published by the Free Software Foundation.
|
2016-11-25 21:53:55 +00:00
|
|
|
#
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser General Public License for more details.
|
|
|
|
#
|
2018-06-21 14:28:34 +00:00
|
|
|
# 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>.
|
2016-11-25 21:53:55 +00:00
|
|
|
|
2018-05-11 13:24:24 +00:00
|
|
|
import logging
|
2014-06-12 11:26:24 +00:00
|
|
|
import sys
|
2017-12-29 18:18:04 +00:00
|
|
|
import warnings
|
2013-09-13 03:31:24 +00:00
|
|
|
|
2018-11-13 15:17:04 +00:00
|
|
|
from . import exceptions, messages, tools
|
2017-06-23 19:31:42 +00:00
|
|
|
|
2017-12-29 18:18:04 +00:00
|
|
|
if sys.version_info.major < 3:
|
2018-02-27 15:30:32 +00:00
|
|
|
raise Exception("Trezorlib does not support Python 2 anymore.")
|
2017-12-29 18:18:04 +00:00
|
|
|
|
2018-05-11 13:24:24 +00:00
|
|
|
LOG = logging.getLogger(__name__)
|
2016-06-29 20:40:37 +00:00
|
|
|
|
2018-11-14 13:44:05 +00:00
|
|
|
VENDORS = ("bitcointrezor.com", "trezor.io")
|
2016-06-29 20:40:37 +00:00
|
|
|
|
2018-11-13 15:15:15 +00:00
|
|
|
DEPRECATION_ERROR = """
|
|
|
|
Incompatible Trezor library detected.
|
|
|
|
|
|
|
|
(Original error: {})
|
|
|
|
""".strip()
|
|
|
|
|
2017-06-23 19:31:42 +00:00
|
|
|
|
2014-02-13 15:46:21 +00:00
|
|
|
def get_buttonrequest_value(code):
|
|
|
|
# Converts integer code to its string representation of ButtonRequestType
|
2018-08-13 16:21:24 +00:00
|
|
|
return [
|
|
|
|
k
|
2018-11-13 15:17:04 +00:00
|
|
|
for k in dir(messages.ButtonRequestType)
|
|
|
|
if getattr(messages.ButtonRequestType, k) == code
|
2018-08-13 16:21:24 +00:00
|
|
|
][0]
|
2017-06-23 19:31:42 +00:00
|
|
|
|
2014-02-02 17:27:44 +00:00
|
|
|
|
2018-11-13 15:12:20 +00:00
|
|
|
class TrezorClient:
|
2018-11-14 13:44:05 +00:00
|
|
|
"""Trezor client, a connection to a Trezor device.
|
|
|
|
|
|
|
|
This class allows you to manage connection state, send and receive protobuf
|
|
|
|
messages, handle user interactions, and perform some generic tasks
|
|
|
|
(send a cancel message, initialize or clear a session, ping the device).
|
|
|
|
|
|
|
|
You have to provide a transport, i.e., a raw connection to the device. You can use
|
|
|
|
`trezorlib.transport.get_transport` to find one.
|
|
|
|
|
|
|
|
You have to provide an UI implementation for the three kinds of interaction:
|
|
|
|
- button request (notify the user that their interaction is needed)
|
|
|
|
- PIN request (on T1, ask the user to input numbers for a PIN matrix)
|
|
|
|
- passphrase request (ask the user to enter a passphrase)
|
|
|
|
See `trezorlib.ui` for details.
|
|
|
|
|
|
|
|
You can supply a `state` you saved in the previous session. If you do,
|
|
|
|
the user might not need to enter their passphrase again.
|
|
|
|
"""
|
2018-11-13 15:12:20 +00:00
|
|
|
|
|
|
|
def __init__(self, transport, ui=None, state=None):
|
2018-05-24 17:14:05 +00:00
|
|
|
LOG.info("creating client instance for device: {}".format(transport.get_path()))
|
2017-09-04 11:36:08 +00:00
|
|
|
self.transport = transport
|
2018-09-13 16:47:19 +00:00
|
|
|
self.ui = ui
|
2018-11-13 15:12:20 +00:00
|
|
|
self.state = state
|
|
|
|
|
|
|
|
if ui is None:
|
|
|
|
warnings.warn("UI class not supplied. This will probably crash soon.")
|
2018-11-08 17:08:02 +00:00
|
|
|
|
|
|
|
self.session_counter = 0
|
2018-11-13 15:12:20 +00:00
|
|
|
self.init_device()
|
2013-12-30 22:35:20 +00:00
|
|
|
|
2018-11-08 17:08:02 +00:00
|
|
|
def open(self):
|
|
|
|
if self.session_counter == 0:
|
|
|
|
self.transport.begin_session()
|
|
|
|
self.session_counter += 1
|
|
|
|
|
2017-09-04 09:30:34 +00:00
|
|
|
def close(self):
|
2018-11-08 17:08:02 +00:00
|
|
|
if self.session_counter == 1:
|
|
|
|
self.transport.end_session()
|
|
|
|
self.session_counter -= 1
|
2017-09-04 09:30:34 +00:00
|
|
|
|
2016-02-10 15:46:58 +00:00
|
|
|
def cancel(self):
|
2018-11-13 15:17:04 +00:00
|
|
|
self._raw_write(messages.Cancel())
|
2016-02-10 15:46:58 +00:00
|
|
|
|
2014-02-15 19:30:39 +00:00
|
|
|
def call_raw(self, msg):
|
2018-10-02 15:18:13 +00:00
|
|
|
__tracebackhide__ = True # for pytest # pylint: disable=W0612
|
|
|
|
self._raw_write(msg)
|
|
|
|
return self._raw_read()
|
|
|
|
|
|
|
|
def _raw_write(self, msg):
|
2018-08-13 16:21:24 +00:00
|
|
|
__tracebackhide__ = True # for pytest # pylint: disable=W0612
|
2017-09-04 11:36:08 +00:00
|
|
|
self.transport.write(msg)
|
2018-10-02 15:18:13 +00:00
|
|
|
|
|
|
|
def _raw_read(self):
|
|
|
|
__tracebackhide__ = True # for pytest # pylint: disable=W0612
|
2017-09-04 11:36:08 +00:00
|
|
|
return self.transport.read()
|
2014-02-15 19:30:39 +00:00
|
|
|
|
2018-11-13 15:05:49 +00:00
|
|
|
def _callback_pin(self, msg):
|
2018-11-16 12:36:04 +00:00
|
|
|
try:
|
|
|
|
pin = self.ui.get_pin(msg.type)
|
|
|
|
except exceptions.Cancelled:
|
|
|
|
self.call_raw(messages.Cancel())
|
|
|
|
raise
|
|
|
|
|
2018-09-13 16:47:19 +00:00
|
|
|
if not pin.isdigit():
|
|
|
|
raise ValueError("Non-numeric PIN provided")
|
2014-02-13 15:46:21 +00:00
|
|
|
|
2018-11-13 15:17:04 +00:00
|
|
|
resp = self.call_raw(messages.PinMatrixAck(pin=pin))
|
|
|
|
if isinstance(resp, messages.Failure) and resp.code in (
|
|
|
|
messages.FailureType.PinInvalid,
|
|
|
|
messages.FailureType.PinCancelled,
|
|
|
|
messages.FailureType.PinExpected,
|
2018-08-13 16:21:24 +00:00
|
|
|
):
|
2018-10-02 15:18:13 +00:00
|
|
|
raise exceptions.PinException(resp.code, resp.message)
|
2014-03-28 15:26:48 +00:00
|
|
|
else:
|
2018-09-13 16:47:19 +00:00
|
|
|
return resp
|
2014-02-13 15:46:21 +00:00
|
|
|
|
2018-11-13 15:05:49 +00:00
|
|
|
def _callback_passphrase(self, msg):
|
2018-09-13 16:47:19 +00:00
|
|
|
if msg.on_device:
|
|
|
|
passphrase = None
|
2014-11-07 00:09:53 +00:00
|
|
|
else:
|
2018-11-16 12:36:04 +00:00
|
|
|
try:
|
|
|
|
passphrase = self.ui.get_passphrase()
|
|
|
|
except exceptions.Cancelled:
|
|
|
|
self.call_raw(messages.Cancel())
|
|
|
|
raise
|
2014-02-13 15:46:21 +00:00
|
|
|
|
2018-11-13 15:17:04 +00:00
|
|
|
resp = self.call_raw(messages.PassphraseAck(passphrase=passphrase))
|
|
|
|
if isinstance(resp, messages.PassphraseStateRequest):
|
2018-11-05 20:30:50 +00:00
|
|
|
self.state = resp.state
|
2018-11-13 15:17:04 +00:00
|
|
|
return self.call_raw(messages.PassphraseStateAck())
|
2018-11-05 20:30:50 +00:00
|
|
|
else:
|
|
|
|
return resp
|
2016-01-12 23:17:38 +00:00
|
|
|
|
2018-11-13 15:05:49 +00:00
|
|
|
def _callback_button(self, msg):
|
2018-10-02 15:18:13 +00:00
|
|
|
__tracebackhide__ = True # for pytest # pylint: disable=W0612
|
2018-09-13 16:47:19 +00:00
|
|
|
# do this raw - send ButtonAck first, notify UI later
|
2018-11-13 15:17:04 +00:00
|
|
|
self._raw_write(messages.ButtonAck())
|
2018-09-13 16:47:19 +00:00
|
|
|
self.ui.button_request(msg.code)
|
2018-10-02 15:18:13 +00:00
|
|
|
return self._raw_read()
|
2014-02-13 15:46:21 +00:00
|
|
|
|
2018-09-13 16:47:19 +00:00
|
|
|
@tools.session
|
|
|
|
def call(self, msg):
|
|
|
|
resp = self.call_raw(msg)
|
|
|
|
while True:
|
2018-11-13 15:17:04 +00:00
|
|
|
if isinstance(resp, messages.PinMatrixRequest):
|
2018-11-13 15:05:49 +00:00
|
|
|
resp = self._callback_pin(resp)
|
2018-11-13 15:17:04 +00:00
|
|
|
elif isinstance(resp, messages.PassphraseRequest):
|
2018-11-13 15:05:49 +00:00
|
|
|
resp = self._callback_passphrase(resp)
|
2018-11-13 15:17:04 +00:00
|
|
|
elif isinstance(resp, messages.ButtonRequest):
|
2018-11-13 15:05:49 +00:00
|
|
|
resp = self._callback_button(resp)
|
2018-11-13 15:17:04 +00:00
|
|
|
elif isinstance(resp, messages.Failure):
|
|
|
|
if resp.code == messages.FailureType.ActionCancelled:
|
2018-11-13 15:05:49 +00:00
|
|
|
raise exceptions.Cancelled
|
|
|
|
raise exceptions.TrezorFailure(resp)
|
|
|
|
else:
|
|
|
|
return resp
|
2014-02-13 15:46:21 +00:00
|
|
|
|
2018-11-08 17:08:02 +00:00
|
|
|
@tools.session
|
2014-02-13 15:46:21 +00:00
|
|
|
def init_device(self):
|
2018-11-13 15:17:04 +00:00
|
|
|
resp = self.call_raw(messages.Initialize(state=self.state))
|
|
|
|
if not isinstance(resp, messages.Features):
|
2018-09-13 16:47:19 +00:00
|
|
|
raise exceptions.TrezorException("Unexpected initial response")
|
|
|
|
else:
|
|
|
|
self.features = resp
|
2018-11-14 13:44:05 +00:00
|
|
|
if self.features.vendor not in VENDORS:
|
2017-11-06 10:09:54 +00:00
|
|
|
raise RuntimeError("Unsupported device")
|
2018-11-06 13:13:08 +00:00
|
|
|
# A side-effect of this is a sanity check for broken protobuf definitions.
|
|
|
|
# If the `vendor` field doesn't exist, you probably have a mismatched
|
|
|
|
# checkout of trezor-common.
|
2014-01-27 10:25:27 +00:00
|
|
|
|
2018-11-13 15:17:04 +00:00
|
|
|
@tools.expect(messages.Success, field="message")
|
2018-08-13 16:21:24 +00:00
|
|
|
def ping(
|
|
|
|
self,
|
|
|
|
msg,
|
|
|
|
button_protection=False,
|
|
|
|
pin_protection=False,
|
|
|
|
passphrase_protection=False,
|
|
|
|
):
|
2018-11-13 15:17:04 +00:00
|
|
|
msg = messages.Ping(
|
2018-08-13 16:21:24 +00:00
|
|
|
message=msg,
|
|
|
|
button_protection=button_protection,
|
|
|
|
pin_protection=pin_protection,
|
|
|
|
passphrase_protection=passphrase_protection,
|
|
|
|
)
|
2014-02-13 15:46:21 +00:00
|
|
|
return self.call(msg)
|
2013-09-13 03:31:24 +00:00
|
|
|
|
2013-10-08 18:33:39 +00:00
|
|
|
def get_device_id(self):
|
|
|
|
return self.features.device_id
|
2013-09-13 03:31:24 +00:00
|
|
|
|
2018-11-13 15:17:04 +00:00
|
|
|
@tools.expect(messages.Success, field="message")
|
2018-06-13 17:04:18 +00:00
|
|
|
def clear_session(self):
|
2018-11-13 15:17:04 +00:00
|
|
|
return self.call_raw(messages.ClearSession())
|
2018-11-13 15:12:20 +00:00
|
|
|
|
|
|
|
|
2018-11-13 15:15:15 +00:00
|
|
|
def MovedTo(where):
|
|
|
|
def moved_to(*args, **kwargs):
|
|
|
|
msg = "Function has been moved to " + where
|
|
|
|
raise RuntimeError(DEPRECATION_ERROR.format(msg))
|
|
|
|
|
|
|
|
return moved_to
|
|
|
|
|
|
|
|
|
2018-11-13 15:12:20 +00:00
|
|
|
class ProtocolMixin(object):
|
|
|
|
"""Fake mixin for old-style software that constructed TrezorClient class
|
|
|
|
from separate mixins.
|
|
|
|
|
|
|
|
Now it only simulates existence of original attributes to prevent some early
|
|
|
|
crashes, and raises errors when any of the attributes are actually called.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
warnings.warn("TrezorClient mixins are not supported anymore")
|
|
|
|
self.tx_api = None # Electrum checks that this attribute exists
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def set_tx_api(self, tx_api):
|
|
|
|
warnings.warn("set_tx_api is deprecated, use new arguments to sign_tx")
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def expand_path(n):
|
|
|
|
warnings.warn(
|
|
|
|
"expand_path is deprecated, use tools.parse_path",
|
|
|
|
DeprecationWarning,
|
|
|
|
stacklevel=2,
|
|
|
|
)
|
|
|
|
return tools.parse_path(n)
|
2018-04-20 16:33:56 +00:00
|
|
|
|
2018-06-13 17:04:18 +00:00
|
|
|
# Device functionality
|
2018-11-13 15:15:15 +00:00
|
|
|
wipe_device = MovedTo("device.wipe")
|
|
|
|
recovery_device = MovedTo("device.recover")
|
|
|
|
reset_device = MovedTo("device.reset")
|
|
|
|
backup_device = MovedTo("device.backup")
|
2018-06-13 17:04:18 +00:00
|
|
|
|
2018-11-13 15:15:15 +00:00
|
|
|
set_u2f_counter = MovedTo("device.set_u2f_counter")
|
2018-06-13 17:04:18 +00:00
|
|
|
|
2018-11-13 15:15:15 +00:00
|
|
|
apply_settings = MovedTo("device.apply_settings")
|
|
|
|
apply_flags = MovedTo("device.apply_flags")
|
|
|
|
change_pin = MovedTo("device.change_pin")
|
2018-06-13 17:04:18 +00:00
|
|
|
|
|
|
|
# Firmware functionality
|
2018-11-13 15:15:15 +00:00
|
|
|
firmware_update = MovedTo("firmware.update")
|
2018-06-13 17:04:18 +00:00
|
|
|
|
|
|
|
# BTC-like functionality
|
2018-11-13 15:15:15 +00:00
|
|
|
get_public_node = MovedTo("btc.get_public_node")
|
|
|
|
get_address = MovedTo("btc.get_address")
|
|
|
|
sign_tx = MovedTo("btc.sign_tx")
|
|
|
|
sign_message = MovedTo("btc.sign_message")
|
|
|
|
verify_message = MovedTo("btc.verify_message")
|
2018-06-13 17:04:18 +00:00
|
|
|
|
|
|
|
# CoSi functionality
|
2018-11-13 15:15:15 +00:00
|
|
|
cosi_commit = MovedTo("cosi.commit")
|
|
|
|
cosi_sign = MovedTo("cosi.sign")
|
2018-06-13 17:04:18 +00:00
|
|
|
|
|
|
|
# Ethereum functionality
|
2018-11-13 15:15:15 +00:00
|
|
|
ethereum_get_address = MovedTo("ethereum.get_address")
|
|
|
|
ethereum_sign_tx = MovedTo("ethereum.sign_tx")
|
|
|
|
ethereum_sign_message = MovedTo("ethereum.sign_message")
|
|
|
|
ethereum_verify_message = MovedTo("ethereum.verify_message")
|
2018-06-13 17:04:18 +00:00
|
|
|
|
|
|
|
# Lisk functionality
|
2018-11-13 15:15:15 +00:00
|
|
|
lisk_get_address = MovedTo("lisk.get_address")
|
|
|
|
lisk_get_public_key = MovedTo("lisk.get_public_key")
|
|
|
|
lisk_sign_message = MovedTo("lisk.sign_message")
|
|
|
|
lisk_verify_message = MovedTo("lisk.verify_message")
|
|
|
|
lisk_sign_tx = MovedTo("lisk.sign_tx")
|
2018-06-13 17:04:18 +00:00
|
|
|
|
|
|
|
# NEM functionality
|
2018-11-13 15:15:15 +00:00
|
|
|
nem_get_address = MovedTo("nem.get_address")
|
|
|
|
nem_sign_tx = MovedTo("nem.sign_tx")
|
2018-06-13 17:04:18 +00:00
|
|
|
|
|
|
|
# Stellar functionality
|
2018-11-13 15:15:15 +00:00
|
|
|
stellar_get_address = MovedTo("stellar.get_address")
|
|
|
|
stellar_sign_transaction = MovedTo("stellar.sign_tx")
|
2018-06-13 17:04:18 +00:00
|
|
|
|
2018-06-13 17:35:01 +00:00
|
|
|
# Miscellaneous cryptographic functionality
|
2018-11-13 15:15:15 +00:00
|
|
|
get_entropy = MovedTo("misc.get_entropy")
|
|
|
|
sign_identity = MovedTo("misc.sign_identity")
|
|
|
|
get_ecdh_session_key = MovedTo("misc.get_ecdh_session_key")
|
|
|
|
encrypt_keyvalue = MovedTo("misc.encrypt_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")
|
|
|
|
|
2018-04-04 01:50:22 +00:00
|
|
|
|
2018-11-13 15:12:20 +00:00
|
|
|
class BaseClient:
|
|
|
|
"""Compatibility proxy for original BaseClient class.
|
|
|
|
Prevents early crash in Electrum forks and possibly other software.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
warnings.warn("TrezorClient mixins are not supported anymore")
|
|
|
|
self.trezor_client = TrezorClient(*args, **kwargs)
|
2017-06-23 19:31:42 +00:00
|
|
|
|
2018-11-13 15:12:20 +00:00
|
|
|
def __getattr__(self, key):
|
|
|
|
return getattr(self.trezor_client, key)
|