From a2a861a688000a17f249d047e0c7a738548ed76f Mon Sep 17 00:00:00 2001 From: Aleksey Popov Date: Mon, 16 Apr 2018 23:03:04 +0300 Subject: [PATCH] app.lisk: Add lisk_get_address and helpers --- src/apps/lisk/__init__.py | 11 +++++++++++ src/apps/lisk/get_address.py | 25 +++++++++++++++++++++++++ src/apps/lisk/helpers.py | 33 +++++++++++++++++++++++++++++++++ src/main.py | 2 ++ 4 files changed, 71 insertions(+) create mode 100644 src/apps/lisk/__init__.py create mode 100644 src/apps/lisk/get_address.py create mode 100644 src/apps/lisk/helpers.py diff --git a/src/apps/lisk/__init__.py b/src/apps/lisk/__init__.py new file mode 100644 index 0000000000..e9630e57e9 --- /dev/null +++ b/src/apps/lisk/__init__.py @@ -0,0 +1,11 @@ +from trezor.wire import register, protobuf_workflow +from trezor.messages.wire_types import \ + LiskGetAddress + + +def dispatch_LiskGetAddress(*args, **kwargs): + from .get_address import layout_lisk_get_address + return layout_lisk_get_address(*args, **kwargs) + +def boot(): + register(LiskGetAddress, protobuf_workflow, dispatch_LiskGetAddress) diff --git a/src/apps/lisk/get_address.py b/src/apps/lisk/get_address.py new file mode 100644 index 0000000000..ae3da52c7b --- /dev/null +++ b/src/apps/lisk/get_address.py @@ -0,0 +1,25 @@ +from apps.wallet.get_address import _show_address, _show_qr +from .helpers import LISK_CURVE, get_address_from_public_key + + +async def layout_lisk_get_address(ctx, msg): + from trezor.messages.LiskAddress import LiskAddress + from trezor.crypto.curve import ed25519 + from ..common import seed + + address_n = msg.address_n or () + + node = await seed.derive_node(ctx, address_n, LISK_CURVE) + + seckey = node.private_key() + public_key = ed25519.publickey(seckey) + address = get_address_from_public_key(public_key) + + if msg.show_display: + while True: + if await _show_address(ctx, address): + break + if await _show_qr(ctx, address): + break + + return LiskAddress(address=address) diff --git a/src/apps/lisk/helpers.py b/src/apps/lisk/helpers.py new file mode 100644 index 0000000000..8ba96587ee --- /dev/null +++ b/src/apps/lisk/helpers.py @@ -0,0 +1,33 @@ + +LISK_CURVE = 'ed25519' + +def get_address_from_public_key(public_key): + from trezor.crypto.hashlib import sha256 + + # logic from lisk-js library + # https://github.com/LiskHQ/lisk-js/blob/115e0e771e8456dc6c1d4acaabba93d975655cfe/lib/transactions/crypto/convert.js#L30 + publicKeyHash = list(sha256(public_key).digest()) + addressData = [] + for i in range(8): + addressData.append(publicKeyHash[7 - i]) + + address = int.from_bytes(bytes(addressData), 'big') + return str(address) + "L" + +def get_votes_count(votes): + plus, minus = [], [] + for vote in votes: + plus.append(vote) if vote.startswith('+') else minus.append(vote) + return len(plus), len(minus) + +def get_vote_tx_text(votes): + plus, minus = get_votes_count(votes) + text = [] + if plus > 0: + text.append(_text_with_plural('Add', plus)) + if minus > 0: + text.append(_text_with_plural('Remove', minus)) + return text + +def _text_with_plural(txt, value): + return '%s %s %s' % (txt, value, ('votes' if value != 1 else 'vote')) diff --git a/src/main.py b/src/main.py index d41b0ecc56..5b1fe06c24 100644 --- a/src/main.py +++ b/src/main.py @@ -11,6 +11,7 @@ import apps.homescreen import apps.management import apps.wallet import apps.ethereum +import apps.lisk if __debug__: import apps.debug else: @@ -21,6 +22,7 @@ apps.homescreen.boot() apps.management.boot() apps.wallet.boot() apps.ethereum.boot() +apps.lisk.boot() if __debug__: apps.debug.boot() else: