Skip to content

Commit 759821e

Browse files
authored
fix: adjust initialize logic (#370)
* fix initialize logic * increase code robustness
1 parent 4fc4980 commit 759821e

File tree

5 files changed

+19
-35
lines changed

5 files changed

+19
-35
lines changed

core/src/apps/base.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -247,33 +247,33 @@ def get_onekey_features() -> OnekeyFeatures:
247247
async def handle_Initialize(
248248
ctx: wire.Context | wire.QRContext, msg: Initialize
249249
) -> Features:
250-
if hasattr(msg, "is_contains_attach") and msg.is_contains_attach is not None:
250+
has_attach = (
251+
hasattr(msg, "is_contains_attach") and msg.is_contains_attach is not None
252+
)
253+
if has_attach:
251254
storage.cache.set(storage.cache.APP_COMMON_CLIENT_CONTAINS_ATTACH, b"\x01")
252255
else:
253256
storage.cache.delete(storage.cache.APP_COMMON_CLIENT_CONTAINS_ATTACH)
257+
ps_raw = getattr(msg, "passphrase_state", None)
258+
if isinstance(ps_raw, bytes):
259+
passphrase_state = ps_raw.decode() if ps_raw else None
260+
elif isinstance(ps_raw, str):
261+
passphrase_state = ps_raw
262+
else:
263+
passphrase_state = None
254264

255-
prev_session_id = storage.cache.get_session_id()
256-
257-
from apps.common import passphrase
258-
259-
passphrase_pin_enabled = passphrase.is_passphrase_pin_enabled()
260-
if (
261-
device_is_unlocked()
262-
and prev_session_id != msg.session_id
263-
and hasattr(msg, "passphrase_state")
264-
and msg.passphrase_state is not None
265-
and passphrase_pin_enabled
266-
and msg.passphrase_state != ""
267-
and se_thd89.check_passphrase_btc_test_address(
268-
msg.passphrase_state
269-
if isinstance(msg.passphrase_state, str)
270-
else msg.passphrase_state.decode()
271-
)
265+
session_id_in_msg = getattr(msg, "session_id", None)
266+
if passphrase_state and se_thd89.check_passphrase_btc_test_address(
267+
passphrase_state
272268
):
273269
session_id = storage.cache.start_session()
270+
elif has_attach and session_id_in_msg is not None and passphrase_state is None:
271+
session_id = storage.cache.start_session()
274272
else:
275-
session_id = storage.cache.start_session(msg.session_id)
273+
session_id = storage.cache.start_session(session_id_in_msg)
274+
276275
if not utils.BITCOIN_ONLY:
276+
277277
if utils.USE_THD89:
278278
if msg.derive_cardano is not None and msg.derive_cardano:
279279
state = se_thd89.get_session_state()
@@ -664,7 +664,6 @@ def get_pinlocked_handler(
664664

665665
async def wrapper(ctx: wire.Context, msg: wire.Msg) -> protobuf.MessageType:
666666
await unlock_device(ctx)
667-
storage.cache.start_session()
668667
return await orig_handler(ctx, msg)
669668

670669
return wrapper
@@ -756,7 +755,6 @@ async def handle_UnLockDevice(
756755
"""Handle UnLockDevice message to unlock the device if needed."""
757756
if not config.is_unlocked():
758757
await unlock_device(ctx, pin_use_type=PinType.USER_AND_PASSPHRASE_PIN)
759-
storage.cache.start_session()
760758

761759
# Get current device state after unlock attempt
762760
from apps.common import passphrase

core/src/apps/homescreen/lockscreen.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ async def lockscreen() -> None:
1616
# to an unlocked state.
1717
try:
1818
await unlock_device()
19-
storage.cache.start_session()
2019
except wire.PinCancelled:
2120
pass
2221

core/src/apps/webauthn/fido2.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -663,9 +663,6 @@ async def verify_user(keepalive_callback: KeepaliveCallback) -> bool:
663663
trezor.pin.keepalive_callback = keepalive_callback
664664

665665
await unlock_device()
666-
import storage.cache
667-
668-
storage.cache.start_session()
669666
return True
670667
except Exception:
671668
return False
@@ -809,9 +806,6 @@ async def confirm_dialog(self) -> bool:
809806

810807
try:
811808
await unlock_device()
812-
import storage.cache
813-
814-
storage.cache.start_session()
815809
return True
816810
except Exception:
817811
return False

core/src/boot.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ async def bootscreen() -> None:
3636
from apps.common.pin_constants import PinType
3737

3838
await verify_user_pin(pin_use_type=PinType.USER_AND_PASSPHRASE_PIN)
39-
storage.cache.start_session()
4039
storage.init_unlocked()
4140
loop.close(lvgl_task)
4241
return

core/src/trezor/uart.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,6 @@ async def handle_fingerprint():
160160
if __debug__:
161161
print(f"fingerprint unlock result {res}")
162162
await base.unlock_device()
163-
import storage.cache
164-
165-
storage.cache.start_session()
166163

167164
# await loop.sleep(2000)
168165
return
@@ -335,9 +332,6 @@ async def _deal_ble_pair(value):
335332
if not base.device_is_unlocked():
336333
try:
337334
await base.unlock_device()
338-
import storage.cache
339-
340-
storage.cache.start_session()
341335
except Exception:
342336
await safe_reloop()
343337
workflow.spawn(utils.internal_reloop())

0 commit comments

Comments
 (0)