Skip to content

Commit

Permalink
Add type stubs for ecdsa and tlv8
Browse files Browse the repository at this point in the history
Fixes: #6
  • Loading branch information
robin-nitrokey committed Jul 26, 2024
1 parent c011b93 commit 646f605
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 19 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ check: check-format check-import-sorting check-poetry check-style check-typing

.PHONY: check-format
check-format:
poetry run black --check src
poetry run black --check src stubs

.PHONY: check-import-sorting
check-import-sorting:
poetry run isort --check-only src
poetry run isort --check-only src stubs

.PHONY: check-poetry
check-poetry:
Expand All @@ -35,8 +35,8 @@ check-typing:

.PHONY: fix
fix:
poetry run black src
poetry run isort src
poetry run black src stubs
poetry run isort src stubs

.PHONY: test
test:
Expand Down
9 changes: 1 addition & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ py_version = "39"
profile = "black"

[tool.mypy]
mypy_path = "stubs"
show_error_codes = true
python_version = "3.9"
strict = true
Expand All @@ -62,11 +63,3 @@ ignore_errors = true
[[tool.mypy.overrides]]
module = "nitrokey.trussed._bootloader.nrf52"
disallow_untyped_calls = false

# libraries without annotations
[[tool.mypy.overrides]]
module = [
"ecdsa.*",
"tlv8.*",
]
ignore_missing_imports = true
11 changes: 5 additions & 6 deletions src/nitrokey/nk3/secrets_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ def tlv_encode(self) -> list[tlv8.Entry]:
),
]
# Filter out empty entries
entries = [r for r in entries if r is not None]
return entries
return [r for r in entries if r is not None]


@dataclasses.dataclass
Expand Down Expand Up @@ -607,7 +606,7 @@ def register(
f"Setting new credential: {credid!r}, {kind}, {algo}, counter: {initial_counter_value}, {touch_button_required=}, {pin_based_encryption=}"
)

structure = [
structure: list[Optional[Union[tlv8.Entry, RawBytes]]] = [
tlv8.Entry(Tag.CredentialId.value, credid),
# header (2) + secret (N)
tlv8.Entry(
Expand All @@ -625,8 +624,8 @@ def register(
name=credid, login=login, password=password, metadata=metadata
).tlv_encode(),
]
structure = list(filter(lambda x: x is not None, structure))
self._send_receive(Instruction.Put, structure)
entries = [x for x in structure if x is not None]
self._send_receive(Instruction.Put, entries)

@classmethod
def encode_properties_to_send(
Expand Down Expand Up @@ -769,7 +768,7 @@ def validate_raw(self, challenge: bytes, response: bytes) -> bytes:
]
raw_res = self._send_receive(Instruction.Validate, structure=structure)
resd: tlv8.EntryList = tlv8.decode(raw_res)
return resd.data # type: ignore[no-any-return]
return resd.data # type: ignore[return-value]

def select(self) -> SelectResponse:
"""
Expand Down
1 change: 0 additions & 1 deletion src/nitrokey/trussed/_bootloader/nrf52.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from zipfile import ZipFile

import ecdsa
import ecdsa.curves
from ecdsa.keys import BadSignatureError

from nitrokey.trussed import Uuid, Version
Expand Down
1 change: 1 addition & 0 deletions stubs/ecdsa/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .keys import VerifyingKey as VerifyingKey
16 changes: 16 additions & 0 deletions stubs/ecdsa/keys.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from hashlib import _Hash
from typing import Any, Callable, Literal, Optional, TypeVar, Union

T = TypeVar("T")

class BadSignatureError(Exception): ...

class VerifyingKey:
def verify(
self,
signature: Union[bytes | str],
data: Union[bytes | str],
hashfunc: Callable[[bytes], _Hash],
) -> bool: ...
@classmethod
def from_der(cls, s: bytes) -> "VerifyingKey": ...
15 changes: 15 additions & 0 deletions stubs/tlv8.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from typing import Any, Iterator, Union

class EntryList:
data: list["Entry"]

def __iter__(self) -> Iterator["Entry"]: ...

class Entry:
type_id: int
data: Any

def __init__(self, type_id: int, data: Any) -> None: ...

def decode(data: bytes) -> EntryList: ...
def encode(entries: Union[list[Entry], EntryList]) -> bytes: ...

0 comments on commit 646f605

Please sign in to comment.