1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-22 05:10:56 +00:00

refactor(core): improve create_new_session func

[no changelog]
This commit is contained in:
M1nd3r 2024-11-30 00:20:25 +01:00
parent a2e6204db7
commit 495009ed5e

View File

@ -7,16 +7,22 @@ from trezor.wire.thp import SessionState
async def create_new_session(message: ThpCreateNewSession) -> ThpNewSession | Failure:
"""
Creates a new `ThpSession` based on the provided parameters and returns a
`ThpNewSession` message containing the new session ID.
Returns an appropriate `Failure` message if session creation fails.
"""
from trezor.wire import NotInitialized
from trezor.wire.thp.session_context import GenericSessionContext
from trezor.wire.thp.session_manager import create_new_session
from apps.common.seed import derive_and_store_roots
ctx = get_context()
# Assert that context `ctx` is ManagementSessionContext
from trezor.wire.thp.session_context import ManagementSessionContext
assert isinstance(ctx, ManagementSessionContext)
# Assert that context `ctx` is `GenericSessionContext`
assert isinstance(ctx, GenericSessionContext)
channel = ctx.channel
@ -30,7 +36,11 @@ async def create_new_session(message: ThpCreateNewSession) -> ThpNewSession | Fa
return Failure(code=FailureType.DataError, message=e.message)
except ActionCancelled as e:
return Failure(code=FailureType.ActionCancelled, message=e.message)
# TODO handle other errors
except NotInitialized as e:
return Failure(code=FailureType.NotInitialized, message=e.message)
# TODO handle other errors (`Exception`` when "Cardano icarus secret is already set!"
# and `RuntimeError` when accessing storage for mnemonic.get_secret - it actually
# happens for locked devices)
new_session.set_session_state(SessionState.ALLOCATED)
channel.sessions[new_session.session_id] = new_session