From 4cfcfb4ff458aa4fcb48679b47e1b5b2dc570365 Mon Sep 17 00:00:00 2001 From: Tomas Susanka Date: Fri, 11 May 2018 15:31:53 +0200 Subject: [PATCH] stellar: init; get public key --- src/apps/common/seed.py | 5 ++ src/apps/stellar/README.md | 11 +++++ src/apps/stellar/__init__.py | 11 +++++ src/apps/stellar/get_public_key.py | 74 ++++++++++++++++++++++++++++++ src/main.py | 2 + 5 files changed, 103 insertions(+) create mode 100644 src/apps/stellar/README.md create mode 100644 src/apps/stellar/__init__.py create mode 100644 src/apps/stellar/get_public_key.py diff --git a/src/apps/common/seed.py b/src/apps/common/seed.py index 1bb22f5937..aba0a23d69 100644 --- a/src/apps/common/seed.py +++ b/src/apps/common/seed.py @@ -43,3 +43,8 @@ def derive_node_without_passphrase( node = bip32.from_seed(seed, curve_name) node.derive_path(path) return node + + +def remove_ed25519_public_key_prefix(pubkey: bytes) -> bytes: + # 0x01 prefix is not part of the actual public key, hence removed + return pubkey[1:] diff --git a/src/apps/stellar/README.md b/src/apps/stellar/README.md new file mode 100644 index 0000000000..717b1d4b13 --- /dev/null +++ b/src/apps/stellar/README.md @@ -0,0 +1,11 @@ +# Stellar + +MAINTAINER = Tomas Susanka + +AUTHOR = Tomas Susanka + +REVIEWER = Jan Pochyla + +----- + +TODO diff --git a/src/apps/stellar/__init__.py b/src/apps/stellar/__init__.py new file mode 100644 index 0000000000..eb8ca62500 --- /dev/null +++ b/src/apps/stellar/__init__.py @@ -0,0 +1,11 @@ +from trezor.wire import register, protobuf_workflow +from trezor.messages.wire_types import StellarGetPublicKey + + +def dispatch_StellarGetPublicKey(*args, **kwargs): + from .get_public_key import get_public_key + return get_public_key(*args, **kwargs) + + +def boot(): + register(StellarGetPublicKey, protobuf_workflow, dispatch_StellarGetPublicKey) diff --git a/src/apps/stellar/get_public_key.py b/src/apps/stellar/get_public_key.py new file mode 100644 index 0000000000..4ced0caa70 --- /dev/null +++ b/src/apps/stellar/get_public_key.py @@ -0,0 +1,74 @@ +from apps.common import seed +from apps.common.confirm import confirm +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 +from trezor.crypto import base32 +import ustruct + +STELLAR_CURVE = 'ed25519' + + +async def get_public_key(ctx, msg: StellarGetPublicKey): + node = await seed.derive_node(ctx, msg.address_n, STELLAR_CURVE) + pubkey = seed.remove_ed25519_public_key_prefix(node.public_key()) # todo better? + + while True: + if await _show(ctx, _address_from_public_key(pubkey)): + break + + return StellarPublicKey(public_key=pubkey) + + +async def _show(ctx, address: str): + lines = _split_address(address) + content = Text('Export Stellar ID', ui.ICON_RECEIVE, + ui.NORMAL, 'Share public account ID?', + ui.MONO, *lines, + icon_color=ui.GREEN) + + return await confirm( + ctx, + content, + code=ButtonRequestType.Address, + cancel_style=ui.BTN_KEY) + + +def _split_address(address: str): + return chunks(address, 17) + + +def _address_from_public_key(pubkey: bytes): + """Returns the base32-encoded version of public key bytes (G...)""" + + address = bytearray() + address.append(6 << 3) # version -> 'G' + address.extend(pubkey) + address.extend(ustruct.pack("> (7 - i) & 1) == 1) + c15 = ((crc >> 15 & 1) == 1) + crc <<= 1 + if c15 ^ bit: + crc ^= polynomial + + return crc & 0xffff diff --git a/src/main.py b/src/main.py index 8b61f99674..ce5f72abe9 100644 --- a/src/main.py +++ b/src/main.py @@ -15,6 +15,7 @@ import apps.wallet import apps.ethereum import apps.lisk import apps.nem +import apps.stellar if __debug__: import apps.debug @@ -28,6 +29,7 @@ apps.wallet.boot() apps.ethereum.boot() apps.lisk.boot() apps.nem.boot() +apps.stellar.boot() if __debug__: apps.debug.boot() else: