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 committed Mar 7, 2022
1 parent b9858f0 commit 8a855b3
Show file tree
Hide file tree
Showing 16 changed files with 249 additions and 1 deletion.
28 changes: 28 additions & 0 deletions common/protob/messages-hello.proto
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -350,4 +350,8 @@ enum MessageType {
MessageType_WebAuthnCredentials = 801 [(wire_out) = true];
MessageType_WebAuthnAddResidentCredential = 802 [(wire_in) = true];
MessageType_WebAuthnRemoveResidentCredential = 803 [(wire_in) = true];

// Hello world
MessageType_HelloWorldRequest = 900 [(wire_in) = true];
MessageType_HelloWorldResponse = 901 [(wire_out) = true];
}
2 changes: 2 additions & 0 deletions core/src/all_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@
import apps.misc.get_ecdh_session_key
apps.misc.get_entropy
import apps.misc.get_entropy
apps.misc.hello_world
import apps.misc.hello_world
apps.misc.sign_identity
import apps.misc.sign_identity
apps.workflow_handlers
Expand Down
25 changes: 25 additions & 0 deletions core/src/apps/misc/hello_world.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from typing import TYPE_CHECKING

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

if TYPE_CHECKING:
from trezor.wire import Context
from trezor.messages import HelloWorldRequest


async def hello_world(ctx: Context, msg: HelloWorldRequest) -> HelloWorldResponse:
text = _get_text_from_msg(msg)
if msg.show_display:
await confirm_text(
ctx,
"confirm_hello_world",
title="Hello world",
data=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
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ def find_message_handler_module(msg_type: int) -> str:
return "apps.misc.get_ecdh_session_key"
if msg_type == MessageType.CipherKeyValue:
return "apps.misc.cipher_key_value"
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
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,5 @@
WebAuthnCredentials = 801
WebAuthnAddResidentCredential = 802
WebAuthnRemoveResidentCredential = 803
HelloWorldRequest = 900
HelloWorldResponse = 901
2 changes: 2 additions & 0 deletions core/src/trezor/enums/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ class MessageType(IntEnum):
WebAuthnCredentials = 801
WebAuthnAddResidentCredential = 802
WebAuthnRemoveResidentCredential = 803
HelloWorldRequest = 900
HelloWorldResponse = 901

class FailureType(IntEnum):
UnexpectedMessage = 1
Expand Down
32 changes: 32 additions & 0 deletions core/src/trezor/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -3506,6 +3506,38 @@ def __init__(
def is_type_of(cls, msg: protobuf.MessageType) -> 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: protobuf.MessageType) -> 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: protobuf.MessageType) -> 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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ SKIPPED_MESSAGES := Binance Cardano DebugMonero Eos Monero Ontology Ripple SdPro
CancelAuthorization DebugLinkLayout GetNonce \
TxAckInput TxAckOutput TxAckPrev TxAckPaymentRequest \
EthereumSignTypedData EthereumTypedDataStructRequest EthereumTypedDataStructAck \
EthereumTypedDataValueRequest EthereumTypedDataValueAck
EthereumTypedDataValueRequest EthereumTypedDataValueAck \
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
ethereum,
fido,
firmware,
hello_world,
monero,
nem,
ripple,
Expand Down Expand Up @@ -360,6 +361,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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ class MessageType(IntEnum):
WebAuthnCredentials = 801
WebAuthnAddResidentCredential = 802
WebAuthnRemoveResidentCredential = 803
HelloWorldRequest = 900
HelloWorldResponse = 901


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


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

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 = 901
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
43 changes: 43 additions & 0 deletions tests/device_tests/misc/test_hello_world.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# This file is part of the Trezor project.
#
# Copyright (C) 2012-2019 SatoshiLabs and contributors
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3
# as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the License along with this library.
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.

from typing import Optional

import pytest

from trezorlib import hello_world
from trezorlib.debuglink import TrezorClientDebugLink as Client

VECTORS = ( # name, amount, show_display
("George", 2, True),
("John", 3, False),
("Hannah", None, False),
)


@pytest.mark.skip_t1
@pytest.mark.parametrize("name, amount, show_display", VECTORS)
def test_hello_world(
client: Client, name: str, amount: Optional[int], show_display: bool
):
with client:
greeting_text = hello_world.say_hello(
client, name=name, amount=amount, show_display=show_display
)
greeting_lines = greeting_text.strip().splitlines()

assert len(greeting_lines) == amount or 1
assert all(name in line for line in greeting_lines)
3 changes: 3 additions & 0 deletions tests/ui_tests/fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -1271,6 +1271,9 @@
"TT_ethereum-test_signtx.py::test_signtx_eip1559[unknown_erc20]": "5064a9b5a10d47770358942af1bfe517a0f9f9b568994d791ad6d2d8c9e21a82",
"TT_ethereum-test_signtx.py::test_signtx_eip1559_access_list": "c2b8fd24103c444473af2611f10f2e458a933b13fe49dfc0fdbb617fa762aa8c",
"TT_ethereum-test_signtx.py::test_signtx_eip1559_access_list_larger": "c2b8fd24103c444473af2611f10f2e458a933b13fe49dfc0fdbb617fa762aa8c",
"TT_misc-test_hello_world.py::test_hello_world[George-2-True]": "589e3a9d3b2092368f86928da429c023bc5ce26320fe3d88fb99cc51beb6bcc4",
"TT_misc-test_hello_world.py::test_hello_world[Hannah-None-False]": "c09de07fbbf1e047442180e2facb5482d06a1a428891b875b7dd93c9e4704ae1",
"TT_misc-test_hello_world.py::test_hello_world[John-3-False]": "c09de07fbbf1e047442180e2facb5482d06a1a428891b875b7dd93c9e4704ae1",
"TT_misc-test_msg_cipherkeyvalue.py::test_decrypt": "4d8563e8130e51628290f0acbced7c294457c4501f7767b00ff3dd1bddc809da",
"TT_misc-test_msg_cipherkeyvalue.py::test_decrypt_badlen": "c09de07fbbf1e047442180e2facb5482d06a1a428891b875b7dd93c9e4704ae1",
"TT_misc-test_msg_cipherkeyvalue.py::test_encrypt": "871df86a7553681c5f17ec6759204f9b0727cc16c4868f227a4b364146fd9293",
Expand Down

0 comments on commit 8a855b3

Please sign in to comment.