1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-03-09 20:56:05 +00:00

style(python): update trezorlib.firmware to new-style type annotations

[no changelog]
This commit is contained in:
matejcik 2025-01-20 11:39:04 +01:00
parent 138a6410fc
commit aa49d1abf8
6 changed files with 32 additions and 22 deletions

View File

@ -14,6 +14,8 @@
# You should have received a copy of the License along with this library. # You should have received a copy of the License along with this library.
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>. # If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
from __future__ import annotations
import typing as t import typing as t
from hashlib import blake2s from hashlib import blake2s
@ -21,9 +23,9 @@ from typing_extensions import Protocol, TypeGuard
from .. import messages from .. import messages
from ..tools import session from ..tools import session
from .models import Model
from .core import VendorFirmware from .core import VendorFirmware
from .legacy import LegacyFirmware, LegacyV2Firmware from .legacy import LegacyFirmware, LegacyV2Firmware
from .models import Model
# re-exports: # re-exports:
if True: if True:
@ -45,7 +47,7 @@ if t.TYPE_CHECKING:
class FirmwareType(Protocol): class FirmwareType(Protocol):
@classmethod @classmethod
def parse(cls: t.Type[T], data: bytes) -> T: ... def parse(cls: type[T], data: bytes) -> T: ...
def verify(self, dev_keys: bool = False) -> None: ... def verify(self, dev_keys: bool = False) -> None: ...
@ -109,7 +111,7 @@ def update(
raise RuntimeError(f"Unexpected message {resp}") raise RuntimeError(f"Unexpected message {resp}")
def get_hash(client: "TrezorClient", challenge: t.Optional[bytes]) -> bytes: def get_hash(client: "TrezorClient", challenge: bytes | None) -> bytes:
return client.call( return client.call(
messages.GetFirmwareHash(challenge=challenge), expect=messages.FirmwareHash messages.GetFirmwareHash(challenge=challenge), expect=messages.FirmwareHash
).hash ).hash

View File

@ -14,7 +14,8 @@
# You should have received a copy of the License along with this library. # You should have received a copy of the License along with this library.
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>. # If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
import typing as t from __future__ import annotations
from copy import copy from copy import copy
from enum import Enum from enum import Enum
@ -45,15 +46,15 @@ class FirmwareHeader(Struct):
header_len: int header_len: int
expiry: int expiry: int
code_length: int code_length: int
version: t.Tuple[int, int, int, int] version: tuple[int, int, int, int]
fix_version: t.Tuple[int, int, int, int] fix_version: tuple[int, int, int, int]
hw_model: t.Union[Model, bytes] hw_model: Model | bytes
hw_revision: int hw_revision: int
monotonic: int monotonic: int
hashes: t.List[bytes] hashes: list[bytes]
v1_signatures: t.List[bytes] v1_signatures: list[bytes]
v1_key_indexes: t.List[int] v1_key_indexes: list[int]
sigmask: int sigmask: int
signature: bytes signature: bytes
@ -132,10 +133,10 @@ class FirmwareImage(Struct):
alignment = Model.T3W1.code_alignment() alignment = Model.T3W1.code_alignment()
return ((len + alignment - 1) & ~(alignment - 1)) - len return ((len + alignment - 1) & ~(alignment - 1)) - len
def get_hash_params(self) -> "util.FirmwareHashParameters": def get_hash_params(self) -> util.FirmwareHashParameters:
return Model.from_hw_model(self.header.hw_model).hash_params() return Model.from_hw_model(self.header.hw_model).hash_params()
def code_hashes(self) -> t.List[bytes]: def code_hashes(self) -> list[bytes]:
"""Calculate hashes of chunks of `code`. """Calculate hashes of chunks of `code`.
Assume that the first `code_offset` bytes of `code` are taken up by the header. Assume that the first `code_offset` bytes of `code` are taken up by the header.
@ -168,7 +169,6 @@ class FirmwareImage(Struct):
raise util.FirmwareIntegrityError("Invalid firmware data.") raise util.FirmwareIntegrityError("Invalid firmware data.")
def digest(self) -> bytes: def digest(self) -> bytes:
hash_params = self.get_hash_params() hash_params = self.get_hash_params()
header = copy(self.header) header = copy(self.header)

View File

@ -14,6 +14,8 @@
# You should have received a copy of the License along with this library. # You should have received a copy of the License along with this library.
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>. # If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
from __future__ import annotations
import hashlib import hashlib
import typing as t import typing as t
from dataclasses import field from dataclasses import field
@ -160,11 +162,11 @@ class LegacyFirmware(Struct):
expected format of firmware binary for Trezor One version 1.8.0, which can be installed expected format of firmware binary for Trezor One version 1.8.0, which can be installed
by both the older and the newer bootloader.""" by both the older and the newer bootloader."""
key_indexes: t.List[int] key_indexes: list[int]
signatures: t.List[bytes] signatures: list[bytes]
code: bytes code: bytes
flags: t.Dict[str, t.Any] = field(default_factory=dict) flags: dict[str, t.Any] = field(default_factory=dict)
embedded_v2: t.Optional[LegacyV2Firmware] = subcon(LegacyV2Firmware, default=None) embedded_v2: LegacyV2Firmware | None = subcon(LegacyV2Firmware, default=None)
# fmt: off # fmt: off
SUBCON = c.Struct( SUBCON = c.Struct(

View File

@ -14,6 +14,8 @@
# You should have received a copy of the License along with this library. # You should have received a copy of the License along with this library.
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>. # If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
from __future__ import annotations
import hashlib import hashlib
import typing as t import typing as t
from dataclasses import dataclass from dataclasses import dataclass

View File

@ -14,6 +14,8 @@
# You should have received a copy of the License along with this library. # You should have received a copy of the License along with this library.
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>. # If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
from __future__ import annotations
import typing as t import typing as t
from dataclasses import dataclass from dataclasses import dataclass
@ -45,4 +47,4 @@ Hasher = t.Callable[[bytes], DigestCalculator]
class FirmwareHashParameters: class FirmwareHashParameters:
hash_function: Hasher hash_function: Hasher
chunk_size: int chunk_size: int
padding_byte: t.Optional[bytes] padding_byte: bytes | None

View File

@ -14,6 +14,8 @@
# You should have received a copy of the License along with this library. # You should have received a copy of the License along with this library.
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>. # If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
from __future__ import annotations
import hashlib import hashlib
import typing as t import typing as t
from copy import copy from copy import copy
@ -85,13 +87,13 @@ class VendorTrust(Struct):
class VendorHeader(Struct): class VendorHeader(Struct):
header_len: int header_len: int
expiry: int expiry: int
version: t.Tuple[int, int] version: tuple[int, int]
sig_m: int sig_m: int
# sig_n: int # sig_n: int
hw_model: t.Union[Model, bytes] hw_model: Model | bytes
pubkeys: t.List[bytes] pubkeys: list[bytes]
text: str text: str
image: t.Dict[str, t.Any] image: dict[str, t.Any]
sigmask: int sigmask: int
signature: bytes signature: bytes