-
-
Notifications
You must be signed in to change notification settings - Fork 632
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DOCS: hello world feature - core, protobuf, trezorlib, tests
- Loading branch information
Showing
16 changed files
with
249 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters