forked from OneKeyHQ/firmware-pro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchange_wipe_code.py
More file actions
125 lines (100 loc) · 3.72 KB
/
Copy pathchange_wipe_code.py
File metadata and controls
125 lines (100 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
from typing import TYPE_CHECKING
from storage.device import is_initialized
from trezor import config, ui, wire
from trezor.messages import Success
from trezor.ui.layouts import confirm_action, show_popup, show_success
from apps.common.request_pin import (
error_pin_invalid,
request_pin,
request_pin_and_sd_salt,
)
if TYPE_CHECKING:
from typing import Awaitable
from trezor.messages import ChangeWipeCode
async def change_wipe_code(ctx: wire.Context, msg: ChangeWipeCode) -> Success:
if not is_initialized():
raise wire.NotInitialized("Device is not initialized")
# Confirm that user wants to set or remove the wipe code.
has_wipe_code = config.has_wipe_code()
await _require_confirm_action(ctx, msg, has_wipe_code)
# Get the unlocking PIN.
pin, salt = await request_pin_and_sd_salt(ctx)
if not msg.remove:
# Pre-check the entered PIN.
from apps.common.pin_constants import PinType
if config.has_pin() and not config.check_pin(pin, salt,PinType.USER_CHECK)[0]:
await error_pin_invalid(ctx)
# Get new wipe code.
wipe_code = await _request_wipe_code_confirm(ctx, pin)
else:
wipe_code = ""
# Write into storage.
if not config.change_wipe_code(pin, salt, wipe_code):
await error_pin_invalid(ctx)
if wipe_code:
if has_wipe_code:
msg_screen = "You have successfully changed the wipe code."
msg_wire = "Wipe code changed"
else:
msg_screen = "You have successfully set the wipe code."
msg_wire = "Wipe code set"
else:
msg_screen = "You have successfully disabled the wipe code."
msg_wire = "Wipe code removed"
await show_success(ctx, "success_wipe_code", msg_screen)
return Success(message=msg_wire)
def _require_confirm_action(
ctx: wire.Context, msg: ChangeWipeCode, has_wipe_code: bool
) -> Awaitable[None]:
if msg.remove and has_wipe_code:
return confirm_action(
ctx,
"disable_wipe_code",
title="Disable wipe code",
description="Do you really want to",
action="disable wipe code protection?",
reverse=True,
icon=ui.ICON_CONFIG,
)
if not msg.remove and has_wipe_code:
return confirm_action(
ctx,
"change_wipe_code",
title="Change wipe code",
description="Do you really want to",
action="change the wipe code?",
reverse=True,
icon=ui.ICON_CONFIG,
)
if not msg.remove and not has_wipe_code:
return confirm_action(
ctx,
"set_wipe_code",
title="Set wipe code",
description="Do you really want to",
action="set the wipe code?",
reverse=True,
icon=ui.ICON_CONFIG,
)
# Removing non-existing wipe code.
raise wire.ProcessError("Wipe code protection is already disabled")
async def _request_wipe_code_confirm(ctx: wire.Context, pin: str) -> str:
while True:
code1 = await request_pin(ctx, "Enter new wipe code")
if code1 == pin:
await _wipe_code_invalid()
continue
code2 = await request_pin(ctx, "Re-enter new wipe code")
if code1 == code2:
return code1
await _wipe_code_mismatch()
async def _wipe_code_invalid() -> None:
await show_popup(
title="Invalid wipe code",
description="The wipe code must be\ndifferent from your PIN.\n\nPlease try again.",
)
async def _wipe_code_mismatch() -> None:
await show_popup(
title="Code mismatch",
description="The wipe codes you\nentered do not match.\n\nPlease try again.",
)