Skip to content

Commit

Permalink
fix(python): allowing PIN/passphrase input for Git Bash
Browse files Browse the repository at this point in the history
  • Loading branch information
grdddj committed Dec 1, 2021
1 parent 70f8174 commit 027f875
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions python/.changelog.d/1959.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix PIN and passphrase entry in certain terminals on Windows
18 changes: 16 additions & 2 deletions python/src/trezorlib/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -54,6 +55,12 @@
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:
Expand All @@ -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:
Expand Down Expand Up @@ -144,6 +155,9 @@ def get_passphrase(self, available_on_device: bool) -> Union[str, object]:
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,
Expand Down

0 comments on commit 027f875

Please sign in to comment.