From bcf77bd3475c02dd1e4ade5a777df4303524be47 Mon Sep 17 00:00:00 2001 From: Tomas Susanka Date: Mon, 11 Jun 2018 15:10:28 +0200 Subject: [PATCH] stellar: get address including show_display var --- src/apps/nem/get_address.py | 1 - src/apps/stellar/__init__.py | 7 +++++++ src/apps/stellar/get_address.py | 20 ++++++++++++++++++++ src/apps/stellar/get_public_key.py | 24 ++++++++++-------------- src/apps/stellar/helpers.py | 2 ++ 5 files changed, 39 insertions(+), 15 deletions(-) create mode 100644 src/apps/stellar/get_address.py diff --git a/src/apps/nem/get_address.py b/src/apps/nem/get_address.py index a4894598f..b1f1dd0ce 100644 --- a/src/apps/nem/get_address.py +++ b/src/apps/nem/get_address.py @@ -18,7 +18,6 @@ async def get_address(ctx, msg): address = node.nem_address(network) if msg.show_display: - while True: if await _show_address(ctx, address, network): break diff --git a/src/apps/stellar/__init__.py b/src/apps/stellar/__init__.py index 28f380a1a..52ac906bb 100644 --- a/src/apps/stellar/__init__.py +++ b/src/apps/stellar/__init__.py @@ -1,8 +1,14 @@ from trezor.wire import register, protobuf_workflow +from trezor.messages.wire_types import StellarGetAddress from trezor.messages.wire_types import StellarGetPublicKey from trezor.messages.wire_types import StellarSignTx +def dispatch_StellarGetAddress(*args, **kwargs): + from .get_address import get_address + return get_address(*args, **kwargs) + + def dispatch_StellarGetPublicKey(*args, **kwargs): from .get_public_key import get_public_key return get_public_key(*args, **kwargs) @@ -14,5 +20,6 @@ def dispatch_StellarSignTx(*args, **kwargs): def boot(): + register(StellarGetAddress, protobuf_workflow, dispatch_StellarGetAddress) register(StellarGetPublicKey, protobuf_workflow, dispatch_StellarGetPublicKey) register(StellarSignTx, protobuf_workflow, dispatch_StellarSignTx) diff --git a/src/apps/stellar/get_address.py b/src/apps/stellar/get_address.py new file mode 100644 index 000000000..31cd9931d --- /dev/null +++ b/src/apps/stellar/get_address.py @@ -0,0 +1,20 @@ +from apps.common import seed +from apps.common.display_address import show_address, show_qr +from apps.stellar import helpers +from trezor.messages.StellarAddress import StellarAddress +from trezor.messages.StellarGetAddress import StellarGetAddress + + +async def get_address(ctx, msg: StellarGetAddress): + node = await seed.derive_node(ctx, msg.address_n, helpers.STELLAR_CURVE) + pubkey = seed.remove_ed25519_public_key_prefix(node.public_key()) # todo better? + address = helpers.address_from_public_key(pubkey) + + if msg.show_display: + while True: + if await show_address(ctx, address): + break + if await show_qr(ctx, address.upper()): + break + + return StellarAddress(address=address) diff --git a/src/apps/stellar/get_public_key.py b/src/apps/stellar/get_public_key.py index 45f402b3d..36d735630 100644 --- a/src/apps/stellar/get_public_key.py +++ b/src/apps/stellar/get_public_key.py @@ -1,31 +1,31 @@ from apps.common import seed from apps.common.confirm import confirm +from apps.common.display_address import split_address from apps.stellar import helpers from trezor import ui from trezor.messages.StellarPublicKey import StellarPublicKey from trezor.messages.StellarGetPublicKey import StellarGetPublicKey from trezor.messages import ButtonRequestType from trezor.ui.text import Text -from trezor.utils import chunks - -STELLAR_CURVE = 'ed25519' +from ubinascii import hexlify async def get_public_key(ctx, msg: StellarGetPublicKey): - node = await seed.derive_node(ctx, msg.address_n, STELLAR_CURVE) + node = await seed.derive_node(ctx, msg.address_n, helpers.STELLAR_CURVE) pubkey = seed.remove_ed25519_public_key_prefix(node.public_key()) # todo better? - while True: - if await _show(ctx, helpers.address_from_public_key(pubkey)): - break + if msg.show_display: + while True: + if await _show(ctx, pubkey): + break return StellarPublicKey(public_key=pubkey) -async def _show(ctx, address: str): - lines = _split_address(address) +async def _show(ctx, pubkey: bytes): + lines = split_address(hexlify(pubkey)) content = Text('Export Stellar ID', ui.ICON_RECEIVE, - ui.NORMAL, 'Share public account ID?', + ui.NORMAL, 'Share public account ID?', # todo only two lines are displayed ui.MONO, *lines, icon_color=ui.GREEN) @@ -34,7 +34,3 @@ async def _show(ctx, address: str): content, code=ButtonRequestType.Address, cancel_style=ui.BTN_KEY) - - -def _split_address(address: str): # todo merge with NEM - return chunks(address, 17) diff --git a/src/apps/stellar/helpers.py b/src/apps/stellar/helpers.py index 51cb7a105..8b74ca135 100644 --- a/src/apps/stellar/helpers.py +++ b/src/apps/stellar/helpers.py @@ -1,6 +1,8 @@ from trezor.crypto import base32 import ustruct +STELLAR_CURVE = 'ed25519' + def address_from_public_key(pubkey: bytes): """Returns the base32-encoded version of public key bytes (G...)"""