Skip to content

Commit

Permalink
Merge pull request #156 from LedgerHQ/fix/speculosbackend
Browse files Browse the repository at this point in the history
[fix] SpeculosBackend port and host parguments were not working
  • Loading branch information
lpascal-ledger authored Dec 7, 2023
2 parents f30a99f + fc5c207 commit 711e04f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.13.1] - 2023-12-06

### Fixed
- SpeculosBackend: Removing `host` and `port` arguments, as they were modifying the client behavior,
but not the server, so this feature probably never worked. The port can be changed through
Speculos arguments: `args=["--api-port", "9876"]`.

## [1.13.0] - 2023-11-8

### Added
Expand Down
29 changes: 18 additions & 11 deletions src/ragger/backend/speculos.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from io import BytesIO
from pathlib import Path
from PIL import Image
from typing import Optional, Generator
from typing import Optional, Generator, List
from time import time, sleep
from re import match

Expand Down Expand Up @@ -51,24 +51,31 @@ def decoration(self: 'SpeculosBackend', *args, **kwargs) -> RAPDU:

class SpeculosBackend(BackendInterface):

_DEFAULT_API_PORT = 5000
_ARGS_KEY = 'args'
_ARGS_API_PORT_KEY = '--api-port'

def __init__(self,
application: Path,
firmware: Firmware,
host: str = "127.0.0.1",
port: int = 5000,
log_apdu_file: Optional[Path] = None,
**kwargs):
super().__init__(firmware=firmware, log_apdu_file=log_apdu_file)
self._host = host
self._port = port
self._port = self._DEFAULT_API_PORT
args = ["--model", firmware.name]
if self._ARGS_KEY in kwargs:
assert isinstance(kwargs[self._ARGS_KEY], list), \
f"'{self._ARGS_KEY}' ({kwargs[self._ARGS_KEY]}) keyword " \
"argument must be a list of arguments"
kwargs[self._ARGS_KEY].extend(args)
speculos_args: Optional[List] = kwargs.get(self._ARGS_KEY)
if speculos_args is not None:
assert isinstance(speculos_args, list), \
f"'{self._ARGS_KEY}' ({speculos_args}) keyword argument must be a list of arguments"
# let's find if the API port is specified in the CLI arguments
try:
index = speculos_args.index(self._ARGS_API_PORT_KEY)
self._port = int(speculos_args[index + 1])
self.logger.info("Using custom port %d for the API", self._port)
except ValueError:
# '--api-port' was not found in `speculos_args`
pass
speculos_args.extend(args)
else:
kwargs[self._ARGS_KEY] = args
self._client: SpeculosClient = SpeculosClient(app=str(application),
Expand All @@ -81,7 +88,7 @@ def __init__(self,

@property
def url(self) -> str:
return f"http://{self._host}:{self._port}"
return f"http://127.0.0.1:{self._port}"

def _retrieve_client_screen_content(self) -> dict:
raw_content = self._client.get_current_screen_content()
Expand Down
Binary file modified tests/snapshots/stax/waiting_screen/00001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 711e04f

Please sign in to comment.