2018-11-08 17:15:49 +00:00
|
|
|
# This file is part of the Trezor project.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2012-2018 SatoshiLabs and contributors
|
|
|
|
#
|
|
|
|
# This library is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser General Public License version 3
|
|
|
|
# as published by the Free Software Foundation.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# 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-09-13 16:47:19 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
import click
|
|
|
|
from mnemonic import Mnemonic
|
|
|
|
|
|
|
|
from . import device
|
|
|
|
from .exceptions import Cancelled
|
2018-10-02 15:37:03 +00:00
|
|
|
from .messages import PinMatrixRequestType, WordRequestType
|
2018-09-13 16:47:19 +00:00
|
|
|
|
|
|
|
PIN_MATRIX_DESCRIPTION = """
|
|
|
|
Use the numeric keypad to describe number positions. The layout is:
|
|
|
|
7 8 9
|
|
|
|
4 5 6
|
|
|
|
1 2 3
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
RECOVERY_MATRIX_DESCRIPTION = """
|
|
|
|
Use the numeric keypad to describe positions.
|
|
|
|
For the word list use only left and right keys.
|
|
|
|
Use backspace to correct an entry.
|
|
|
|
|
|
|
|
The keypad layout is:
|
|
|
|
7 8 9 7 | 9
|
|
|
|
4 5 6 4 | 6
|
|
|
|
1 2 3 1 | 3
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
PIN_GENERIC = None
|
|
|
|
PIN_CURRENT = PinMatrixRequestType.Current
|
|
|
|
PIN_NEW = PinMatrixRequestType.NewFirst
|
|
|
|
PIN_CONFIRM = PinMatrixRequestType.NewSecond
|
|
|
|
|
|
|
|
|
2018-10-22 08:28:07 +00:00
|
|
|
def echo(*args, **kwargs):
|
|
|
|
return click.echo(*args, err=True, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
def prompt(*args, **kwargs):
|
|
|
|
return click.prompt(*args, err=True, **kwargs)
|
|
|
|
|
|
|
|
|
2018-09-13 16:47:19 +00:00
|
|
|
class ClickUI:
|
|
|
|
@staticmethod
|
|
|
|
def button_request(code):
|
2018-10-22 08:28:07 +00:00
|
|
|
echo("Please confirm action on your Trezor device")
|
2018-09-13 16:47:19 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_pin(code=None):
|
|
|
|
if code == PIN_CURRENT:
|
|
|
|
desc = "current PIN"
|
|
|
|
elif code == PIN_NEW:
|
|
|
|
desc = "new PIN"
|
|
|
|
elif code == PIN_CONFIRM:
|
|
|
|
desc = "new PIN again"
|
|
|
|
else:
|
|
|
|
desc = "PIN"
|
2018-10-22 08:28:07 +00:00
|
|
|
echo(PIN_MATRIX_DESCRIPTION)
|
2018-09-13 16:47:19 +00:00
|
|
|
|
|
|
|
while True:
|
2018-10-22 08:28:07 +00:00
|
|
|
pin = prompt("Please enter {}".format(desc), hide_input=True)
|
2018-09-13 16:47:19 +00:00
|
|
|
if not pin.isdigit():
|
2018-10-22 08:28:07 +00:00
|
|
|
echo("Non-numerical PIN provided, please try again")
|
2018-09-13 16:47:19 +00:00
|
|
|
else:
|
|
|
|
return pin
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_passphrase():
|
|
|
|
if os.getenv("PASSPHRASE") is not None:
|
2018-10-22 08:28:07 +00:00
|
|
|
echo("Passphrase required. Using PASSPHRASE environment variable.")
|
2018-09-13 16:47:19 +00:00
|
|
|
return os.getenv("PASSPHRASE")
|
|
|
|
|
|
|
|
while True:
|
2018-10-22 08:28:07 +00:00
|
|
|
passphrase = prompt("Passphrase required", hide_input=True)
|
|
|
|
second = prompt("Confirm your passphrase", hide_input=True)
|
2018-09-13 16:47:19 +00:00
|
|
|
if passphrase == second:
|
|
|
|
return passphrase
|
|
|
|
else:
|
2018-10-22 08:28:07 +00:00
|
|
|
echo("Passphrase did not match. Please try again.")
|
2018-09-13 16:47:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
def mnemonic_words(expand=False, language="english"):
|
|
|
|
if expand:
|
|
|
|
wordlist = Mnemonic(language).wordlist
|
|
|
|
else:
|
|
|
|
wordlist = set()
|
|
|
|
|
|
|
|
def expand_word(word):
|
|
|
|
if not expand:
|
|
|
|
return word
|
|
|
|
if word in wordlist:
|
|
|
|
return word
|
|
|
|
matches = [w for w in wordlist if w.startswith(word)]
|
|
|
|
if len(matches) == 1:
|
|
|
|
return word
|
2018-10-22 08:28:07 +00:00
|
|
|
echo("Choose one of: " + ", ".join(matches))
|
2018-09-13 16:47:19 +00:00
|
|
|
raise KeyError(word)
|
|
|
|
|
|
|
|
def get_word(type):
|
|
|
|
assert type == WordRequestType.Plain
|
|
|
|
while True:
|
|
|
|
try:
|
2018-10-22 08:28:07 +00:00
|
|
|
word = prompt("Enter one word of mnemonic")
|
2018-09-13 16:47:19 +00:00
|
|
|
return expand_word(word)
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
except (KeyboardInterrupt, click.Abort):
|
|
|
|
raise Cancelled from None
|
|
|
|
|
|
|
|
return get_word
|
|
|
|
|
|
|
|
|
|
|
|
def matrix_words(type):
|
|
|
|
while True:
|
|
|
|
try:
|
2018-10-03 13:11:21 +00:00
|
|
|
ch = click.getchar()
|
2018-09-13 16:47:19 +00:00
|
|
|
except (KeyboardInterrupt, EOFError):
|
|
|
|
raise Cancelled from None
|
|
|
|
|
|
|
|
if ch in "\x04\x1b":
|
|
|
|
# Ctrl+D, Esc
|
|
|
|
raise Cancelled
|
|
|
|
if ch in "\x08\x7f":
|
|
|
|
# Backspace, Del
|
|
|
|
return device.RECOVERY_BACK
|
|
|
|
if type == WordRequestType.Matrix6 and ch in "147369":
|
|
|
|
return ch
|
|
|
|
if type == WordRequestType.Matrix9 and ch in "123456789":
|
|
|
|
return ch
|