Skip to content

Commit

Permalink
DOCS: hello world feature - core, protobuf, trezorlib, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
grdddj authored and obrusvit committed Feb 5, 2024
1 parent ed1785a commit e1cbb8a
Show file tree
Hide file tree
Showing 19 changed files with 750 additions and 26 deletions.
28 changes: 28 additions & 0 deletions common/protob/messages-hello.proto
@@ -0,0 +1,28 @@
syntax = "proto2";
package hw.trezor.messages.helloworld;

// Sugar for easier handling in Java
option java_package = "com.satoshilabs.trezor.lib.protobuf";
option java_outer_classname = "TrezorMessageHelloWorld";

import "messages.proto";


/**
* Request: Hello world request for text
* @next HelloWorldResponse
* @next Failure
*/
message HelloWorldRequest {
required string name = 1;
optional uint32 amount = 2 [default=1];
optional bool show_display = 3;
}

/**
* Response: Hello world text
* @end
*/
message HelloWorldResponse {
required string text = 1;
}
4 changes: 4 additions & 0 deletions common/protob/messages.proto
Expand Up @@ -372,4 +372,8 @@ enum MessageType {
MessageType_SolanaAddress = 903 [(wire_out) = true];
MessageType_SolanaSignTx = 904 [(wire_in) = true];
MessageType_SolanaTxSignature = 905 [(wire_out) = true];

// Hello world
MessageType_HelloWorldRequest = 1000 [(wire_in) = true];
MessageType_HelloWorldResponse = 1001 [(wire_out) = true];
}
2 changes: 2 additions & 0 deletions core/src/all_modules.py
Expand Up @@ -365,6 +365,8 @@
import apps.misc.get_entropy
apps.misc.get_firmware_hash
import apps.misc.get_firmware_hash
apps.misc.hello_world
import apps.misc.hello_world
apps.misc.sign_identity
import apps.misc.sign_identity
apps.workflow_handlers
Expand Down
23 changes: 23 additions & 0 deletions core/src/apps/misc/hello_world.py
@@ -0,0 +1,23 @@
from typing import TYPE_CHECKING

from trezor.messages import HelloWorldResponse
from trezor.ui.layouts import confirm_text

if TYPE_CHECKING:
from trezor.messages import HelloWorldRequest


async def hello_world(msg: HelloWorldRequest) -> HelloWorldResponse:
text = _get_text_from_msg(msg)
if msg.show_display:
await confirm_text(
"confirm_hello_world",
"Hello world",
text,
description="Hello world example",
)
return HelloWorldResponse(text=text)


def _get_text_from_msg(msg: HelloWorldRequest) -> str:
return msg.amount * f"Hello {msg.name}!\n"
2 changes: 2 additions & 0 deletions core/src/apps/workflow_handlers.py
Expand Up @@ -93,6 +93,8 @@ def _find_message_handler_module(msg_type: int) -> str:
return "apps.misc.get_firmware_hash"
if msg_type == MessageType.CosiCommit:
return "apps.misc.cosi_commit"
if msg_type == MessageType.HelloWorldRequest:
return "apps.misc.hello_world"

if not utils.BITCOIN_ONLY:
if msg_type == MessageType.SetU2FCounter:
Expand Down
2 changes: 2 additions & 0 deletions core/src/trezor/enums/MessageType.py
Expand Up @@ -243,3 +243,5 @@
SolanaAddress = 903
SolanaSignTx = 904
SolanaTxSignature = 905
HelloWorldRequest = 1000
HelloWorldResponse = 1001
2 changes: 2 additions & 0 deletions core/src/trezor/enums/__init__.py
Expand Up @@ -261,6 +261,8 @@ class MessageType(IntEnum):
SolanaAddress = 903
SolanaSignTx = 904
SolanaTxSignature = 905
HelloWorldRequest = 1000
HelloWorldResponse = 1001

class FailureType(IntEnum):
UnexpectedMessage = 1
Expand Down
32 changes: 32 additions & 0 deletions core/src/trezor/messages.py
Expand Up @@ -3923,6 +3923,38 @@ def __init__(
def is_type_of(cls, msg: Any) -> TypeGuard["EthereumAccessList"]:
return isinstance(msg, cls)

class HelloWorldRequest(protobuf.MessageType):
name: "str"
amount: "int"
show_display: "bool | None"

def __init__(
self,
*,
name: "str",
amount: "int | None" = None,
show_display: "bool | None" = None,
) -> None:
pass

@classmethod
def is_type_of(cls, msg: Any) -> TypeGuard["HelloWorldRequest"]:
return isinstance(msg, cls)

class HelloWorldResponse(protobuf.MessageType):
text: "str"

def __init__(
self,
*,
text: "str",
) -> None:
pass

@classmethod
def is_type_of(cls, msg: Any) -> TypeGuard["HelloWorldResponse"]:
return isinstance(msg, cls)

class MoneroTransactionSourceEntry(protobuf.MessageType):
outputs: "list[MoneroOutputEntry]"
real_output: "int | None"
Expand Down
14 changes: 14 additions & 0 deletions core/tests/test_apps.misc.hello_world.py
@@ -0,0 +1,14 @@
from common import *

from trezor.messages import HelloWorldRequest
from apps.misc.hello_world import _get_text_from_msg


class TestHelloWorld(unittest.TestCase):
def test_get_text_from_msg(self):
msg = HelloWorldRequest(name="Satoshi", amount=2, show_display=False)
self.assertEqual(_get_text_from_msg(msg), "Hello Satoshi!\nHello Satoshi!\n")


if __name__ == "__main__":
unittest.main()
3 changes: 2 additions & 1 deletion legacy/firmware/protob/Makefile
Expand Up @@ -9,7 +9,8 @@ SKIPPED_MESSAGES := Binance Cardano DebugMonero Eos Monero Ontology Ripple SdPro
EthereumSignTypedData EthereumTypedDataStructRequest EthereumTypedDataStructAck \
EthereumTypedDataValueRequest EthereumTypedDataValueAck ShowDeviceTutorial \
UnlockBootloader AuthenticateDevice AuthenticityProof \
Solana StellarClaimClaimableBalanceOp
Solana StellarClaimClaimableBalanceOp \
HelloWorldRequest HelloWorldResponse

ifeq ($(BITCOIN_ONLY), 1)
SKIPPED_MESSAGES += Ethereum NEM Stellar
Expand Down
28 changes: 28 additions & 0 deletions python/src/trezorlib/cli/hello_world.py
@@ -0,0 +1,28 @@
from typing import TYPE_CHECKING, Optional

import click

from .. import hello_world
from . import with_client

if TYPE_CHECKING:
from ..client import TrezorClient


@click.group(name="helloworld")
def cli() -> None:
"""Hello world commands."""


@cli.command()
@click.argument("name")
@click.option("-a", "--amount", type=int, help="How many times to greet.")
@click.option(
"-d", "--show-display", is_flag=True, help="Whether to show confirmation screen."
)
@with_client
def say_hello(
client: "TrezorClient", name: str, amount: Optional[int], show_display: bool
) -> str:
"""Simply say hello to the supplied name."""
return hello_world.say_hello(client, name, amount, show_display=show_display)
2 changes: 2 additions & 0 deletions python/src/trezorlib/cli/trezorctl.py
Expand Up @@ -42,6 +42,7 @@
ethereum,
fido,
firmware,
hello_world,
monero,
nem,
ripple,
Expand Down Expand Up @@ -407,6 +408,7 @@ def wait_for_emulator(obj: TrezorConnection, timeout: float) -> None:
cli.add_command(device.cli)
cli.add_command(eos.cli)
cli.add_command(ethereum.cli)
cli.add_command(hello_world.cli)
cli.add_command(fido.cli)
cli.add_command(monero.cli)
cli.add_command(nem.cli)
Expand Down
24 changes: 24 additions & 0 deletions python/src/trezorlib/hello_world.py
@@ -0,0 +1,24 @@
from typing import TYPE_CHECKING, Optional

from . import messages
from .tools import expect

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


@expect(messages.HelloWorldResponse, field="text", ret_type=str)
def say_hello(
client: "TrezorClient",
name: str,
amount: Optional[int],
show_display: bool,
) -> "MessageType":
return client.call(
messages.HelloWorldRequest(
name=name,
amount=amount,
show_display=show_display,
)
)
36 changes: 36 additions & 0 deletions python/src/trezorlib/messages.py
Expand Up @@ -269,6 +269,8 @@ class MessageType(IntEnum):
SolanaAddress = 903
SolanaSignTx = 904
SolanaTxSignature = 905
HelloWorldRequest = 1000
HelloWorldResponse = 1001


class FailureType(IntEnum):
Expand Down Expand Up @@ -5211,6 +5213,40 @@ def __init__(
self.address = address


class HelloWorldRequest(protobuf.MessageType):
MESSAGE_WIRE_TYPE = 1000
FIELDS = {
1: protobuf.Field("name", "string", repeated=False, required=True),
2: protobuf.Field("amount", "uint32", repeated=False, required=False, default=1),
3: protobuf.Field("show_display", "bool", repeated=False, required=False, default=None),
}

def __init__(
self,
*,
name: "str",
amount: Optional["int"] = 1,
show_display: Optional["bool"] = None,
) -> None:
self.name = name
self.amount = amount
self.show_display = show_display


class HelloWorldResponse(protobuf.MessageType):
MESSAGE_WIRE_TYPE = 1001
FIELDS = {
1: protobuf.Field("text", "string", repeated=False, required=True),
}

def __init__(
self,
*,
text: "str",
) -> None:
self.text = text


class MoneroTransactionSourceEntry(protobuf.MessageType):
MESSAGE_WIRE_TYPE = None
FIELDS = {
Expand Down
2 changes: 2 additions & 0 deletions rust/trezor-client/src/messages/generated.rs

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

0 comments on commit e1cbb8a

Please sign in to comment.