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

trezorlib API improvements #4490

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion common/protob/messages-management.proto
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ message UnlockPath {
* @next GetAddress
*/
message UnlockedPathRequest {
optional bytes mac = 1; // authentication code for future UnlockPath calls
required bytes mac = 1; // authentication code for future UnlockPath calls
}

/**
Expand Down
6 changes: 3 additions & 3 deletions common/protob/messages-monero.proto
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ message MoneroGetAddress {
* @end
*/
message MoneroAddress {
optional bytes address = 1;
required bytes address = 1;
}

/**
Expand All @@ -117,8 +117,8 @@ message MoneroGetWatchKey {
* @end
*/
message MoneroWatchKey {
optional bytes watch_key = 1;
optional bytes address = 2;
required bytes watch_key = 1;
required bytes address = 2;
}

/**
Expand Down
16 changes: 8 additions & 8 deletions core/src/trezor/messages.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/src/trezor/ui/layouts/bolt/reset.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ async def show_share_confirmation_success(
)
text = TR.reset__continue_with_next_share

return await show_success("success_recovery", text, subheader)
return await show_success("success_share_confirm", text, subheader)


async def show_share_confirmation_failure() -> None:
Expand Down
2 changes: 1 addition & 1 deletion core/src/trezor/ui/layouts/quicksilver/reset.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ async def show_share_confirmation_success(
)
)

await show_success("success_recovery", title, subheader=footer_description)
await show_success("success_share_confirm", title, subheader=footer_description)


def show_share_confirmation_failure() -> Awaitable[None]:
Expand Down
2 changes: 1 addition & 1 deletion core/src/trezor/ui/layouts/samson/reset.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ async def show_share_confirmation_success(
)
text = TR.reset__continue_with_next_share

return await show_success("success_recovery", text, subheader)
return await show_success("success_share_confirm", text, subheader)


async def show_share_confirmation_failure() -> None:
Expand Down
6 changes: 3 additions & 3 deletions docs/developers/hello_world_feature_TT.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ if TYPE_CHECKING:
from .protobuf import MessageType


@expect(messages.HelloWorldResponse, field="text", ret_type=str)
def say_hello(
client: "TrezorClient",
name: str,
Expand All @@ -166,8 +165,9 @@ def say_hello(
name=name,
amount=amount,
show_display=show_display,
)
)
),
expect=messages.HelloWorldResponse,
).text
```

Code above is sending `HelloWorldRequest` into Trezor and is expecting to get `HelloWorldResponse` back (from which it extracts the `text` string as a response).
Expand Down
1 change: 0 additions & 1 deletion legacy/firmware/fsm_msg_coin.h
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,6 @@ void fsm_msgUnlockPath(const UnlockPath *msg) {

unlock_path = msg->address_n[0];
resp->mac.size = SHA256_DIGEST_LENGTH;
resp->has_mac = true;
msg_write(MessageType_MessageType_UnlockedPathRequest, resp);
layoutHome();
}
2 changes: 1 addition & 1 deletion python/.changelog.d/4155.added
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Added support for entropy check workflow in device.reset().
Added support for entropy check workflow in `device.reset()`.
1 change: 1 addition & 0 deletions python/.changelog.d/4464.added
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Introduced `device.setup()` as a cleaner upgrade to `device.reset()`.
1 change: 1 addition & 0 deletions python/.changelog.d/4464.added.1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added an `expect` argument to `TrezorClient.call()`, to enforce the returned message type.
1 change: 1 addition & 0 deletions python/.changelog.d/4464.deprecated
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`device.reset()` is deprecated, migrate to `device.setup()`
1 change: 1 addition & 0 deletions python/.changelog.d/4464.deprecated.1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
String return values are deprecated in functions where the semantic result is a success (specifically those that were returning the message from Trezor's `Success` response). Type annotations are updated to `str | None`, and in a future release those functions will be returning `None` on success, or raise an exception on a failure.
1 change: 1 addition & 0 deletions python/.changelog.d/4464.deprecated.2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`@expect` decorator is deprecated -- use `TrezorClient.call(expect=...)` instead.
1 change: 1 addition & 0 deletions python/.changelog.d/4464.deprecated.3
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Return value of `device.recover()` is deprecated. In the future, this function will return `None`.
1 change: 1 addition & 0 deletions python/.changelog.d/4464.incompatible
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Return values in `solana` module were changed from the wrapping protobuf messages to the raw inner values (`str` for address, `bytes` for pubkey / signature).
1 change: 1 addition & 0 deletions python/.changelog.d/4464.incompatible.1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`trezorctl device` commands whose default result is a success will not print anything to stdout anymore, in line with Unix philosophy.
14 changes: 6 additions & 8 deletions python/src/trezorlib/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,18 @@
from typing import TYPE_CHECKING

from . import messages
from .tools import expect

if TYPE_CHECKING:
from .client import TrezorClient
from .protobuf import MessageType


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


@expect(messages.BenchmarkResult)
def run(client: "TrezorClient", name: str) -> "MessageType":
return client.call(messages.BenchmarkRun(name=name))
def run(client: "TrezorClient", name: str) -> messages.BenchmarkResult:
return client.call(
messages.BenchmarkRun(name=name), expect=messages.BenchmarkResult
)
37 changes: 11 additions & 26 deletions python/src/trezorlib/binance.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,34 @@

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

if TYPE_CHECKING:
from .client import TrezorClient
from .protobuf import MessageType
from .tools import Address


@expect(messages.BinanceAddress, field="address", ret_type=str)
def get_address(
client: "TrezorClient",
address_n: "Address",
show_display: bool = False,
chunkify: bool = False,
) -> "MessageType":
) -> str:
return client.call(
messages.BinanceGetAddress(
address_n=address_n, show_display=show_display, chunkify=chunkify
)
)
),
expect=messages.BinanceAddress,
).address


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


@session
Expand All @@ -60,13 +59,7 @@ def sign_tx(
tx_msg["chunkify"] = chunkify
envelope = dict_to_proto(messages.BinanceSignTx, tx_msg)

response = client.call(envelope)

if not isinstance(response, messages.BinanceTxRequest):
raise RuntimeError(
"Invalid response, expected BinanceTxRequest, received "
+ type(response).__name__
)
client.call(envelope, expect=messages.BinanceTxRequest)

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

response = client.call(msg)

if not isinstance(response, messages.BinanceSignedTx):
raise RuntimeError(
"Invalid response, expected BinanceSignedTx, received "
+ type(response).__name__
)

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