Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Session-based trezorlib #4577

Draft
wants to merge 31 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
ac3149e
chore(core): adapt emu.py to the new trezorlib
M1nd3r Feb 4, 2025
a8f216b
chore(vendor): update fido2-tests
M1nd3r Feb 4, 2025
a20291b
chore(python): update python tools
M1nd3r Feb 4, 2025
68443d1
test: update device tests
M1nd3r Feb 4, 2025
fcc013f
test: update persistence tests
M1nd3r Feb 4, 2025
b89b71f
test: update ui tests
M1nd3r Feb 4, 2025
6bf48c8
test: update click tests
M1nd3r Feb 4, 2025
3d18c9d
test: update upgrade tests
M1nd3r Feb 4, 2025
564396f
chore(tests): adapt testing framework to session based
M1nd3r Feb 4, 2025
3057738
chore(python): add refresh of invalid client to internal emulator
M1nd3r Feb 4, 2025
3f71358
feat(python): implement session based trezorctl
M1nd3r Feb 4, 2025
ff0baa7
feat(python): implement session based trezorlib
M1nd3r Feb 4, 2025
c756ce3
chore(core): adapt trezorlib transports to session based
M1nd3r Feb 4, 2025
737ce62
chore(tests): update fixtures
M1nd3r Feb 4, 2025
55fd099
feat(python): add platformdirs to requirements
M1nd3r Feb 4, 2025
224e382
test(core): remove dead code
mmilata Feb 13, 2025
12a00e5
debuglink and tests fixes
mmilata Feb 12, 2025
c26c721
wip: channel database changes
M1nd3r Feb 17, 2025
c3e3982
ci: preserve emulator logs for upgrade and persistence tests
mmilata Feb 18, 2025
058c163
fixup! chore(core): adapt trezorlib transports to session based
mmilata Feb 17, 2025
d755154
session wrapper fixup
mmilata Feb 17, 2025
32dd2d5
fix(python): make stylechecker happy
M1nd3r Feb 18, 2025
5c1475b
fix(tests): remove unused import
M1nd3r Feb 18, 2025
3aede06
fixup! feat(python): implement session based trezorlib
M1nd3r Feb 18, 2025
77f8be2
Revert "fixup! chore(core): adapt trezorlib transports to session based"
M1nd3r Feb 18, 2025
e8bc030
fixup! fixup! feat(python): implement session based trezorlib
M1nd3r Feb 19, 2025
37cec1e
fixup! ci: preserve emulator logs for upgrade and persistence tests
mmilata Feb 18, 2025
a872550
fix(tests): port legacy device tests to trezorlib with sessions
mmilata Feb 19, 2025
357d4f1
fixup! chore(tests): update fixtures [no changelog]
M1nd3r Feb 20, 2025
6d8ac21
fixup! fix(tests): port legacy device tests to trezorlib with sessions
M1nd3r Feb 20, 2025
65c20d6
fixup! fixup! chore(tests): update fixtures [no changelog]
M1nd3r Feb 20, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .github/workflows/core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ jobs:
env:
TREZOR_UPGRADE_TEST: core
PYTEST_TIMEOUT: 400
TREZOR_PYTEST_LOGS_DIR: ${{ github.workspace }}/tests/
steps:
- uses: actions/checkout@v4
with:
Expand All @@ -377,6 +378,13 @@ jobs:
- uses: ./.github/actions/environment
- run: nix-shell --run "tests/download_emulators.sh"
- run: nix-shell --run "poetry run pytest tests/upgrade_tests"
- uses: actions/upload-artifact@v4
with:
name: core-test-upgrade-${{ matrix.model }}-${{ matrix.asan }}
path: |
tests/trezor.log
retention-days: 7
if: always()


# Persistence tests - UI.
Expand All @@ -395,6 +403,7 @@ jobs:
env:
TREZOR_PROFILING: ${{ matrix.asan == 'noasan' && '1' || '0' }}
PYTEST_TIMEOUT: 400
TREZOR_PYTEST_LOGS_DIR: ${{ github.workspace }}/tests/
steps:
- uses: actions/checkout@v4
with:
Expand All @@ -417,6 +426,13 @@ jobs:
if: always()
continue-on-error: true
- uses: ./.github/actions/upload-coverage
- uses: actions/upload-artifact@v4
with:
name: core-test-persistence-${{ matrix.model }}-${{ matrix.asan }}
path: |
tests/trezor.log
retention-days: 7
if: always()

core_hwi_test:
name: HWI tests
Expand Down
5 changes: 3 additions & 2 deletions core/emu.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,10 @@ def cli(
label = "Emulator"

assert emulator.client is not None
trezorlib.device.wipe(emulator.client)
trezorlib.device.wipe(emulator.client.get_seedless_session())

trezorlib.debuglink.load_device(
emulator.client,
emulator.client.get_seedless_session(),
mnemonics,
pin=None,
passphrase_protection=False,
Expand Down
2 changes: 1 addition & 1 deletion core/tools/codegen/get_trezor_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import binascii
from trezorlib.client import TrezorClient
from trezorlib.transport_hid import HidTransport
from trezorlib.transport.hid import HidTransport

devices = HidTransport.enumerate()
if len(devices) > 0:
Expand Down
1 change: 1 addition & 0 deletions python/.changelog.d/4577.changed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Changed trezorlib to session-based. Changes affect also trezorctl and tests.
1 change: 1 addition & 0 deletions python/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ construct>=2.9,!=2.10.55
typing_extensions>=4.7.1
construct-classes>=0.1.2
cryptography>=41
platformdirs>=2
4 changes: 3 additions & 1 deletion python/src/trezorlib/_internal/emulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ def client(self) -> TrezorClientDebugLink:
"""
if self._client is None:
raise RuntimeError
if self._client.is_invalidated:
self._client = self._client.get_new_client()
return self._client

def make_args(self) -> List[str]:
Expand All @@ -112,7 +114,7 @@ def wait_until_ready(self, timeout: float = EMULATOR_WAIT_TIME) -> None:
start = time.monotonic()
try:
while True:
if transport._ping():
if transport.ping():
break
if self.process.poll() is not None:
raise RuntimeError("Emulator process died")
Expand Down
6 changes: 3 additions & 3 deletions python/src/trezorlib/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from cryptography.hazmat.primitives.asymmetric import ec, utils

from . import device
from .client import TrezorClient
from .transport.session import Session

LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -349,7 +349,7 @@ def verify_authentication_response(


def authenticate_device(
client: TrezorClient,
session: Session,
challenge: bytes | None = None,
*,
whitelist: t.Collection[bytes] | None = None,
Expand All @@ -359,7 +359,7 @@ def authenticate_device(
if challenge is None:
challenge = secrets.token_bytes(16)

resp = device.authenticate(client, challenge)
resp = device.authenticate(session, challenge)

return verify_authentication_response(
challenge,
Expand Down
10 changes: 5 additions & 5 deletions python/src/trezorlib/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
from . import messages

if TYPE_CHECKING:
from .client import TrezorClient
from .transport.session import Session


def list_names(
client: "TrezorClient",
session: "Session",
) -> messages.BenchmarkNames:
return client.call(messages.BenchmarkListNames(), expect=messages.BenchmarkNames)
return session.call(messages.BenchmarkListNames(), expect=messages.BenchmarkNames)


def run(client: "TrezorClient", name: str) -> messages.BenchmarkResult:
return client.call(
def run(session: "Session", name: str) -> messages.BenchmarkResult:
return session.call(
messages.BenchmarkRun(name=name), expect=messages.BenchmarkResult
)
18 changes: 8 additions & 10 deletions python/src/trezorlib/binance.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,19 @@

from . import messages
from .protobuf import dict_to_proto
from .tools import session

if TYPE_CHECKING:
from .client import TrezorClient
from .tools import Address
from .transport.session import Session


def get_address(
client: "TrezorClient",
session: "Session",
address_n: "Address",
show_display: bool = False,
chunkify: bool = False,
) -> str:
return client.call(
return session.call(
messages.BinanceGetAddress(
address_n=address_n, show_display=show_display, chunkify=chunkify
),
Expand All @@ -40,17 +39,16 @@ def get_address(


def get_public_key(
client: "TrezorClient", address_n: "Address", show_display: bool = False
session: "Session", address_n: "Address", show_display: bool = False
) -> bytes:
return client.call(
return session.call(
messages.BinanceGetPublicKey(address_n=address_n, show_display=show_display),
expect=messages.BinancePublicKey,
).public_key


@session
def sign_tx(
client: "TrezorClient", address_n: "Address", tx_json: dict, chunkify: bool = False
session: "Session", address_n: "Address", tx_json: dict, chunkify: bool = False
) -> messages.BinanceSignedTx:
msg = tx_json["msgs"][0]
tx_msg = tx_json.copy()
Expand All @@ -59,7 +57,7 @@ def sign_tx(
tx_msg["chunkify"] = chunkify
envelope = dict_to_proto(messages.BinanceSignTx, tx_msg)

client.call(envelope, expect=messages.BinanceTxRequest)
session.call(envelope, expect=messages.BinanceTxRequest)

if "refid" in msg:
msg = dict_to_proto(messages.BinanceCancelMsg, msg)
Expand All @@ -70,4 +68,4 @@ def sign_tx(
else:
raise ValueError("can not determine msg type")

return client.call(msg, expect=messages.BinanceSignedTx)
return session.call(msg, expect=messages.BinanceSignedTx)
Loading
Loading