2019-05-13 13:06:34 +00:00
|
|
|
from trezor import wire
|
2019-07-03 13:07:04 +00:00
|
|
|
from trezor.messages import ButtonRequestType
|
2019-09-19 07:37:23 +00:00
|
|
|
from trezor.ui.confirm import CONFIRMED, INFO, Confirm, HoldToConfirm, InfoConfirm
|
2016-11-15 12:48:31 +00:00
|
|
|
|
2020-05-25 10:49:16 +00:00
|
|
|
from . import button_request
|
|
|
|
|
2019-05-13 13:06:34 +00:00
|
|
|
if __debug__:
|
2019-10-02 12:40:25 +00:00
|
|
|
from trezor.ui.scroll import Paginated
|
2016-06-06 12:10:36 +00:00
|
|
|
|
2020-05-25 10:49:16 +00:00
|
|
|
|
2019-07-03 13:07:04 +00:00
|
|
|
if False:
|
2019-10-18 12:36:40 +00:00
|
|
|
from typing import Any, Callable, Optional
|
2019-07-03 13:07:04 +00:00
|
|
|
from trezor import ui
|
|
|
|
from trezor.ui.confirm import ButtonContent, ButtonStyleType
|
|
|
|
from trezor.ui.loader import LoaderStyleType
|
2019-10-02 12:40:25 +00:00
|
|
|
from trezor.messages.ButtonRequest import EnumTypeButtonRequestType
|
2019-07-03 13:07:04 +00:00
|
|
|
|
2019-05-13 13:06:34 +00:00
|
|
|
|
|
|
|
async def confirm(
|
2019-10-21 13:52:02 +00:00
|
|
|
ctx: wire.GenericContext,
|
2019-08-20 14:20:02 +00:00
|
|
|
content: ui.Component,
|
2019-10-02 12:40:25 +00:00
|
|
|
code: EnumTypeButtonRequestType = ButtonRequestType.Other,
|
2019-10-18 12:36:40 +00:00
|
|
|
confirm: Optional[ButtonContent] = Confirm.DEFAULT_CONFIRM,
|
2019-07-03 13:07:04 +00:00
|
|
|
confirm_style: ButtonStyleType = Confirm.DEFAULT_CONFIRM_STYLE,
|
2019-10-18 12:36:40 +00:00
|
|
|
cancel: Optional[ButtonContent] = Confirm.DEFAULT_CANCEL,
|
2019-07-03 13:07:04 +00:00
|
|
|
cancel_style: ButtonStyleType = Confirm.DEFAULT_CANCEL_STYLE,
|
|
|
|
major_confirm: bool = False,
|
|
|
|
) -> bool:
|
2020-05-25 10:49:16 +00:00
|
|
|
await button_request(ctx, code=code)
|
2017-09-16 13:00:31 +00:00
|
|
|
|
2019-05-13 13:06:34 +00:00
|
|
|
if content.__class__.__name__ == "Paginated":
|
2019-10-02 12:40:25 +00:00
|
|
|
# The following works because asserts are omitted in non-debug builds.
|
|
|
|
# IOW if the assert runs, that means __debug__ is True and Paginated is imported
|
|
|
|
assert isinstance(content, Paginated)
|
|
|
|
|
2019-05-13 13:06:34 +00:00
|
|
|
content.pages[-1] = Confirm(
|
2019-06-23 10:18:59 +00:00
|
|
|
content.pages[-1],
|
|
|
|
confirm,
|
|
|
|
confirm_style,
|
|
|
|
cancel,
|
|
|
|
cancel_style,
|
|
|
|
major_confirm,
|
2019-05-13 13:06:34 +00:00
|
|
|
)
|
2019-10-02 12:40:25 +00:00
|
|
|
dialog = content # type: ui.Layout
|
2019-05-13 13:06:34 +00:00
|
|
|
else:
|
2019-06-23 10:18:59 +00:00
|
|
|
dialog = Confirm(
|
|
|
|
content, confirm, confirm_style, cancel, cancel_style, major_confirm
|
|
|
|
)
|
2018-02-26 17:40:44 +00:00
|
|
|
|
2020-01-22 15:50:20 +00:00
|
|
|
return await ctx.wait(dialog) is CONFIRMED
|
2016-06-06 12:10:36 +00:00
|
|
|
|
2016-09-26 11:06:28 +00:00
|
|
|
|
2019-09-19 07:37:23 +00:00
|
|
|
async def info_confirm(
|
2019-10-21 13:52:02 +00:00
|
|
|
ctx: wire.GenericContext,
|
2019-09-19 07:37:23 +00:00
|
|
|
content: ui.Component,
|
|
|
|
info_func: Callable,
|
2019-10-02 12:40:25 +00:00
|
|
|
code: EnumTypeButtonRequestType = ButtonRequestType.Other,
|
2019-09-19 07:37:23 +00:00
|
|
|
confirm: ButtonContent = InfoConfirm.DEFAULT_CONFIRM,
|
|
|
|
confirm_style: ButtonStyleType = InfoConfirm.DEFAULT_CONFIRM_STYLE,
|
|
|
|
cancel: ButtonContent = InfoConfirm.DEFAULT_CANCEL,
|
|
|
|
cancel_style: ButtonStyleType = InfoConfirm.DEFAULT_CANCEL_STYLE,
|
|
|
|
info: ButtonContent = InfoConfirm.DEFAULT_INFO,
|
|
|
|
info_style: ButtonStyleType = InfoConfirm.DEFAULT_INFO_STYLE,
|
|
|
|
) -> bool:
|
2020-05-25 10:49:16 +00:00
|
|
|
await button_request(ctx, code=code)
|
2019-09-19 07:37:23 +00:00
|
|
|
|
|
|
|
dialog = InfoConfirm(
|
|
|
|
content, confirm, confirm_style, cancel, cancel_style, info, info_style
|
|
|
|
)
|
|
|
|
|
|
|
|
while True:
|
2020-01-22 15:50:20 +00:00
|
|
|
result = await ctx.wait(dialog)
|
2019-09-19 07:37:23 +00:00
|
|
|
|
|
|
|
if result is INFO:
|
|
|
|
await info_func(ctx)
|
|
|
|
|
|
|
|
else:
|
|
|
|
return result is CONFIRMED
|
|
|
|
|
|
|
|
|
2019-05-13 13:06:34 +00:00
|
|
|
async def hold_to_confirm(
|
2020-02-17 14:51:39 +00:00
|
|
|
ctx: wire.GenericContext,
|
|
|
|
content: ui.Component,
|
2019-10-02 12:40:25 +00:00
|
|
|
code: EnumTypeButtonRequestType = ButtonRequestType.Other,
|
|
|
|
confirm: str = HoldToConfirm.DEFAULT_CONFIRM,
|
2019-07-15 12:16:49 +00:00
|
|
|
confirm_style: ButtonStyleType = HoldToConfirm.DEFAULT_CONFIRM_STYLE,
|
2019-07-03 13:07:04 +00:00
|
|
|
loader_style: LoaderStyleType = HoldToConfirm.DEFAULT_LOADER_STYLE,
|
2020-03-30 15:56:30 +00:00
|
|
|
cancel: bool = True,
|
2019-07-03 13:07:04 +00:00
|
|
|
) -> bool:
|
2020-05-25 10:49:16 +00:00
|
|
|
await button_request(ctx, code=code)
|
2017-09-16 13:00:31 +00:00
|
|
|
|
2019-05-13 13:06:34 +00:00
|
|
|
if content.__class__.__name__ == "Paginated":
|
2019-10-02 12:40:25 +00:00
|
|
|
# The following works because asserts are omitted in non-debug builds.
|
|
|
|
# IOW if the assert runs, that means __debug__ is True and Paginated is imported
|
|
|
|
assert isinstance(content, Paginated)
|
|
|
|
|
2019-05-13 13:06:34 +00:00
|
|
|
content.pages[-1] = HoldToConfirm(
|
2020-03-30 15:56:30 +00:00
|
|
|
content.pages[-1], confirm, confirm_style, loader_style, cancel
|
2019-05-13 13:06:34 +00:00
|
|
|
)
|
2019-10-02 12:40:25 +00:00
|
|
|
dialog = content # type: ui.Layout
|
2019-05-13 13:06:34 +00:00
|
|
|
else:
|
2020-03-30 15:56:30 +00:00
|
|
|
dialog = HoldToConfirm(content, confirm, confirm_style, loader_style, cancel)
|
2018-02-26 17:40:44 +00:00
|
|
|
|
2020-01-22 15:50:20 +00:00
|
|
|
return await ctx.wait(dialog) is CONFIRMED
|
2016-09-27 13:32:12 +00:00
|
|
|
|
|
|
|
|
2019-07-03 13:07:04 +00:00
|
|
|
async def require_confirm(*args: Any, **kwargs: Any) -> None:
|
2016-09-26 11:06:28 +00:00
|
|
|
confirmed = await confirm(*args, **kwargs)
|
|
|
|
if not confirmed:
|
2020-04-21 12:18:53 +00:00
|
|
|
raise wire.ActionCancelled
|
2018-02-28 19:20:39 +00:00
|
|
|
|
|
|
|
|
2019-07-03 13:07:04 +00:00
|
|
|
async def require_hold_to_confirm(*args: Any, **kwargs: Any) -> None:
|
2018-02-28 19:20:39 +00:00
|
|
|
confirmed = await hold_to_confirm(*args, **kwargs)
|
|
|
|
if not confirmed:
|
2020-04-21 12:18:53 +00:00
|
|
|
raise wire.ActionCancelled
|