2018-06-21 14:28:34 +00:00
|
|
|
# This file is part of the Trezor project.
|
2018-03-05 15:18:00 +00:00
|
|
|
#
|
2018-06-21 14:28:34 +00:00
|
|
|
# Copyright (C) 2012-2018 SatoshiLabs and contributors
|
2018-03-05 15:18:00 +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.
|
2018-03-05 15:18:00 +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>.
|
2018-03-05 15:18:00 +00:00
|
|
|
|
2018-06-13 17:04:18 +00:00
|
|
|
import os
|
2018-03-05 15:18:00 +00:00
|
|
|
import warnings
|
2018-08-13 16:21:24 +00:00
|
|
|
|
2018-06-13 17:04:18 +00:00
|
|
|
from mnemonic import Mnemonic
|
|
|
|
|
|
|
|
from . import messages as proto
|
2018-06-25 15:51:09 +00:00
|
|
|
from .tools import expect, session
|
2018-03-05 15:18:00 +00:00
|
|
|
from .transport import enumerate_devices, get_transport
|
|
|
|
|
|
|
|
|
|
|
|
class TrezorDevice:
|
2018-08-13 16:21:24 +00:00
|
|
|
"""
|
2018-03-05 15:18:00 +00:00
|
|
|
This class is deprecated. (There is no reason for it to exist in the first
|
|
|
|
place, it is nothing but a collection of two functions.)
|
|
|
|
Instead, please use functions from the ``trezorlib.transport`` module.
|
2018-08-13 16:21:24 +00:00
|
|
|
"""
|
2018-03-05 15:18:00 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def enumerate(cls):
|
2018-08-13 16:21:24 +00:00
|
|
|
warnings.warn("TrezorDevice is deprecated.", DeprecationWarning)
|
2018-03-05 15:18:00 +00:00
|
|
|
return enumerate_devices()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def find_by_path(cls, path):
|
2018-08-13 16:21:24 +00:00
|
|
|
warnings.warn("TrezorDevice is deprecated.", DeprecationWarning)
|
2018-03-05 15:18:00 +00:00
|
|
|
return get_transport(path, prefix_search=False)
|
2018-06-13 17:04:18 +00:00
|
|
|
|
|
|
|
|
2018-06-25 15:51:09 +00:00
|
|
|
@expect(proto.Success, field="message")
|
2018-08-13 16:21:24 +00:00
|
|
|
def apply_settings(
|
|
|
|
client,
|
|
|
|
label=None,
|
|
|
|
language=None,
|
|
|
|
use_passphrase=None,
|
|
|
|
homescreen=None,
|
|
|
|
passphrase_source=None,
|
|
|
|
auto_lock_delay_ms=None,
|
|
|
|
):
|
2018-06-13 17:04:18 +00:00
|
|
|
settings = proto.ApplySettings()
|
|
|
|
if label is not None:
|
|
|
|
settings.label = label
|
|
|
|
if language:
|
|
|
|
settings.language = language
|
|
|
|
if use_passphrase is not None:
|
|
|
|
settings.use_passphrase = use_passphrase
|
|
|
|
if homescreen is not None:
|
|
|
|
settings.homescreen = homescreen
|
|
|
|
if passphrase_source is not None:
|
|
|
|
settings.passphrase_source = passphrase_source
|
|
|
|
if auto_lock_delay_ms is not None:
|
|
|
|
settings.auto_lock_delay_ms = auto_lock_delay_ms
|
|
|
|
|
|
|
|
out = client.call(settings)
|
|
|
|
client.init_device() # Reload Features
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
2018-06-25 15:51:09 +00:00
|
|
|
@expect(proto.Success, field="message")
|
2018-06-13 17:04:18 +00:00
|
|
|
def apply_flags(client, flags):
|
|
|
|
out = client.call(proto.ApplyFlags(flags=flags))
|
|
|
|
client.init_device() # Reload Features
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
2018-06-25 15:51:09 +00:00
|
|
|
@expect(proto.Success, field="message")
|
2018-06-13 17:04:18 +00:00
|
|
|
def change_pin(client, remove=False):
|
|
|
|
ret = client.call(proto.ChangePin(remove=remove))
|
|
|
|
client.init_device() # Re-read features
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2018-06-25 15:51:09 +00:00
|
|
|
@expect(proto.Success, field="message")
|
2018-06-13 17:04:18 +00:00
|
|
|
def set_u2f_counter(client, u2f_counter):
|
|
|
|
ret = client.call(proto.SetU2FCounter(u2f_counter=u2f_counter))
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2018-06-25 15:51:09 +00:00
|
|
|
@expect(proto.Success, field="message")
|
2018-08-10 11:32:50 +00:00
|
|
|
def wipe(client):
|
2018-06-13 17:04:18 +00:00
|
|
|
ret = client.call(proto.WipeDevice())
|
|
|
|
client.init_device()
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2018-06-25 15:51:09 +00:00
|
|
|
@expect(proto.Success, field="message")
|
2018-08-13 16:21:24 +00:00
|
|
|
def recover(
|
|
|
|
client,
|
|
|
|
word_count,
|
|
|
|
passphrase_protection,
|
|
|
|
pin_protection,
|
|
|
|
label,
|
|
|
|
language,
|
|
|
|
type=proto.RecoveryDeviceType.ScrambledWords,
|
|
|
|
expand=False,
|
|
|
|
dry_run=False,
|
|
|
|
):
|
2018-06-13 17:04:18 +00:00
|
|
|
if client.features.initialized and not dry_run:
|
2018-08-13 16:21:24 +00:00
|
|
|
raise RuntimeError(
|
|
|
|
"Device is initialized already. Call wipe_device() and try again."
|
|
|
|
)
|
2018-06-13 17:04:18 +00:00
|
|
|
|
|
|
|
if word_count not in (12, 18, 24):
|
|
|
|
raise ValueError("Invalid word count. Use 12/18/24")
|
|
|
|
|
|
|
|
client.recovery_matrix_first_pass = True
|
|
|
|
|
|
|
|
client.expand = expand
|
|
|
|
if client.expand:
|
|
|
|
# optimization to load the wordlist once, instead of for each recovery word
|
2018-08-13 16:21:24 +00:00
|
|
|
client.mnemonic_wordlist = Mnemonic("english")
|
|
|
|
|
|
|
|
res = client.call(
|
|
|
|
proto.RecoveryDevice(
|
|
|
|
word_count=int(word_count),
|
|
|
|
passphrase_protection=bool(passphrase_protection),
|
|
|
|
pin_protection=bool(pin_protection),
|
|
|
|
label=label,
|
|
|
|
language=language,
|
|
|
|
enforce_wordlist=True,
|
|
|
|
type=type,
|
|
|
|
dry_run=dry_run,
|
|
|
|
)
|
|
|
|
)
|
2018-06-13 17:04:18 +00:00
|
|
|
|
|
|
|
client.init_device()
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
2018-06-25 15:51:09 +00:00
|
|
|
@expect(proto.Success, field="message")
|
2018-06-13 17:04:18 +00:00
|
|
|
@session
|
2018-08-13 16:21:24 +00:00
|
|
|
def reset(
|
|
|
|
client,
|
|
|
|
display_random,
|
|
|
|
strength,
|
|
|
|
passphrase_protection,
|
|
|
|
pin_protection,
|
|
|
|
label,
|
|
|
|
language,
|
|
|
|
u2f_counter=0,
|
|
|
|
skip_backup=False,
|
2018-10-08 12:17:07 +00:00
|
|
|
no_backup=False,
|
2018-08-13 16:21:24 +00:00
|
|
|
):
|
2018-06-13 17:04:18 +00:00
|
|
|
if client.features.initialized:
|
2018-08-13 16:21:24 +00:00
|
|
|
raise RuntimeError(
|
|
|
|
"Device is initialized already. Call wipe_device() and try again."
|
|
|
|
)
|
2018-06-13 17:04:18 +00:00
|
|
|
|
|
|
|
# Begin with device reset workflow
|
2018-08-13 16:21:24 +00:00
|
|
|
msg = proto.ResetDevice(
|
|
|
|
display_random=display_random,
|
|
|
|
strength=strength,
|
|
|
|
passphrase_protection=bool(passphrase_protection),
|
|
|
|
pin_protection=bool(pin_protection),
|
|
|
|
language=language,
|
|
|
|
label=label,
|
|
|
|
u2f_counter=u2f_counter,
|
|
|
|
skip_backup=bool(skip_backup),
|
2018-10-08 12:17:07 +00:00
|
|
|
no_backup=bool(no_backup),
|
2018-08-13 16:21:24 +00:00
|
|
|
)
|
2018-06-13 17:04:18 +00:00
|
|
|
|
|
|
|
resp = client.call(msg)
|
|
|
|
if not isinstance(resp, proto.EntropyRequest):
|
|
|
|
raise RuntimeError("Invalid response, expected EntropyRequest")
|
|
|
|
|
|
|
|
external_entropy = os.urandom(32)
|
2018-09-12 22:44:08 +00:00
|
|
|
# LOG.debug("Computer generated entropy: " + external_entropy.hex())
|
2018-06-13 17:04:18 +00:00
|
|
|
ret = client.call(proto.EntropyAck(entropy=external_entropy))
|
|
|
|
client.init_device()
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2018-06-25 15:51:09 +00:00
|
|
|
@expect(proto.Success, field="message")
|
2018-08-10 11:32:50 +00:00
|
|
|
def backup(client):
|
2018-06-13 17:04:18 +00:00
|
|
|
ret = client.call(proto.BackupDevice())
|
|
|
|
return ret
|