From 0093d21bb68150f6300645f2a1b562447d92fb42 Mon Sep 17 00:00:00 2001 From: Tomas Susanka Date: Fri, 27 Apr 2018 16:28:56 +0100 Subject: [PATCH] nem: correct mosaic quantities --- src/apps/nem/helpers.py | 1 + src/apps/nem/mosaic/definitions.py | 7 +++++++ src/apps/nem/transfer/layout.py | 32 ++++++++++++++++++++++-------- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/apps/nem/helpers.py b/src/apps/nem/helpers.py index 46f7402df5..dbb549058e 100644 --- a/src/apps/nem/helpers.py +++ b/src/apps/nem/helpers.py @@ -22,6 +22,7 @@ AES_BLOCK_SIZE = const(16) NEM_HASH_ALG = 'keccak' NEM_PUBLIC_KEY_SIZE = const(32) # ed25519 public key NEM_LEVY_PERCENTILE_DIVISOR_ABSOLUTE = const(10000) +NEM_MOSAIC_AMOUNT_DIVISOR = const(1000000) NEM_MAX_PLAIN_PAYLOAD_SIZE = const(1024) NEM_MAX_ENCRYPTED_PAYLOAD_SIZE = const(960) diff --git a/src/apps/nem/mosaic/definitions.py b/src/apps/nem/mosaic/definitions.py index f283f074c0..c1daf9e3c5 100644 --- a/src/apps/nem/mosaic/definitions.py +++ b/src/apps/nem/mosaic/definitions.py @@ -1,5 +1,6 @@ # todo move to common and generate via script + def get_mosaic_definition(namespace_name: str, mosaic_name: str, network: int): for m in mosaics: if namespace_name == m["namespace"] and mosaic_name == m["mosaic"]: @@ -8,6 +9,12 @@ def get_mosaic_definition(namespace_name: str, mosaic_name: str, network: int): return None +def is_nem_xem_mosaic(namespace_name: str, mosaic_name: str): + if namespace_name == "nem" and mosaic_name == "xem": + return True + return False + + mosaics = [ { "name": "XEM", diff --git a/src/apps/nem/transfer/layout.py b/src/apps/nem/transfer/layout.py index f3121645d0..7bf4f948dd 100644 --- a/src/apps/nem/transfer/layout.py +++ b/src/apps/nem/transfer/layout.py @@ -1,11 +1,11 @@ from apps.nem.layout import * +from apps.nem.mosaic.definitions import * from trezor.messages import NEMImportanceTransferMode from trezor.messages import NEMTransfer from trezor.messages import NEMImportanceTransfer from trezor.messages import NEMTransactionCommon from trezor.messages import NEMMosaic from trezor.messages import NEMMosaicLevy -from apps.nem.mosaic.definitions import get_mosaic_definition async def ask_transfer(ctx, common: NEMTransactionCommon, transfer: NEMTransfer, payload, encrypted): @@ -13,27 +13,31 @@ async def ask_transfer(ctx, common: NEMTransactionCommon, transfer: NEMTransfer, await _require_confirm_payload(ctx, transfer.payload, encrypted) for mosaic in transfer.mosaics: - await ask_transfer_mosaic(ctx, mosaic, common.network) + await ask_transfer_mosaic(ctx, common, transfer, mosaic) - await _require_confirm_transfer(ctx, transfer.recipient, transfer.amount) + await _require_confirm_transfer(ctx, transfer.recipient, _get_xem_amount(transfer)) await require_confirm_final(ctx, common.fee) -async def ask_transfer_mosaic(ctx, mosaic: NEMMosaic, network: int): - definition = get_mosaic_definition(mosaic.namespace, mosaic.mosaic, network) +async def ask_transfer_mosaic(ctx, common: NEMTransactionCommon, transfer: NEMTransfer, mosaic: NEMMosaic): + if is_nem_xem_mosaic(mosaic.namespace, mosaic.mosaic): + return + + definition = get_mosaic_definition(mosaic.namespace, mosaic.mosaic, common.network) + mosaic_quantity = mosaic.quantity * transfer.amount / NEM_MOSAIC_AMOUNT_DIVISOR if definition: msg = Text('Confirm mosaic', ui.ICON_SEND, ui.NORMAL, 'Confirm transfer of', - ui.BOLD, format_amount(mosaic.quantity, definition["divisibility"]) + definition["ticker"], + ui.BOLD, format_amount(mosaic_quantity, definition["divisibility"]) + definition["ticker"], ui.NORMAL, 'of', ui.BOLD, definition["name"], icon_color=ui.GREEN) await require_confirm(ctx, msg, ButtonRequestType.ConfirmOutput) if "levy" in definition and "fee" in definition: - levy_msg = _get_levy_msg(definition, mosaic.quantity, network) + levy_msg = _get_levy_msg(definition, mosaic.quantity, common.network) msg = Text('Confirm mosaic', ui.ICON_SEND, ui.NORMAL, 'Confirm mosaic', ui.NORMAL, 'levy fee of', @@ -50,13 +54,25 @@ async def ask_transfer_mosaic(ctx, mosaic: NEMMosaic, network: int): msg = Text('Confirm mosaic', ui.ICON_SEND, ui.NORMAL, 'Confirm transfer of', - ui.BOLD, str(mosaic.quantity) + ' raw units', + ui.BOLD, str(mosaic_quantity) + ' raw units', ui.NORMAL, 'of', ui.BOLD, mosaic.namespace + '.' + mosaic.mosaic, icon_color=ui.GREEN) await require_confirm(ctx, msg, ButtonRequestType.ConfirmOutput) +def _get_xem_amount(transfer: NEMTransfer): + # mosaics are empty the transfer.amount denotes the xem amount + if not len(mosaics): + return transfer.amount + # otherwise xem amount is taken from the nem xem mosaic if present + for mosaic in transfer.mosaics: + if is_nem_xem_mosaic(mosaic.namespace, mosaic.mosaic): + return mosaic.quantity * transfer.amount / NEM_MOSAIC_AMOUNT_DIVISOR + # if there are mosaics but do not include xem, 0 xem is sent + return 0 + + def _get_levy_msg(mosaic_definition, quantity: int, network: int) -> str: levy_definition = get_mosaic_definition(mosaic_definition["levy_namespace"], mosaic_definition["levy_mosaic"], network) if mosaic_definition["levy"] == NEMMosaicLevy.MosaicLevy_Absolute: