1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-18 12:28:09 +00:00

fix(python): allowing PIN/passphrase input for Git Bash

This commit is contained in:
grdddj 2021-11-30 11:51:52 +01:00 committed by matejcik
parent 70f8174c57
commit d6b99ba3eb
2 changed files with 17 additions and 2 deletions

View File

@ -0,0 +1 @@
Fix PIN and passphrase entry in certain terminals on Windows

View File

@ -15,6 +15,7 @@
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
import os
import sys
from typing import Any, Callable, Optional, Union
import click
@ -54,6 +55,12 @@ PIN_CONFIRM = PinMatrixRequestType.NewSecond
WIPE_CODE_NEW = PinMatrixRequestType.WipeCodeFirst
WIPE_CODE_CONFIRM = PinMatrixRequestType.WipeCodeSecond
# Workaround for limitation of Git Bash
# getpass function does not work correctly on Windows when not using a real terminal
# (the hidden input is not allowed and it also freezes the script completely)
# Details: https://bugs.python.org/issue44762
CAN_HANDLE_HIDDEN_INPUT = sys.stdin and sys.stdin.isatty()
class TrezorClientUI(Protocol):
def button_request(self, br: messages.ButtonRequest) -> None:
@ -70,8 +77,12 @@ def echo(*args: Any, **kwargs: Any) -> None:
return click.echo(*args, err=True, **kwargs)
def prompt(*args: Any, **kwargs: Any) -> Any:
return click.prompt(*args, err=True, **kwargs)
def prompt(text: str, *, hide_input: bool = False, **kwargs: Any) -> Any:
# Disallowing hidden input and warning user when it would cause issues
if not CAN_HANDLE_HIDDEN_INPUT and hide_input:
hide_input = False
text += " (WARNING: will be displayed!)"
return click.prompt(text, hide_input=hide_input, err=True, **kwargs)
class ClickUI:
@ -144,6 +155,9 @@ class ClickUI:
default="",
show_default=False,
)
# In case user sees the input on the screen, we do not need confirmation
if not CAN_HANDLE_HIDDEN_INPUT:
return passphrase
second = prompt(
"Confirm your passphrase",
hide_input=True,