1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-15 18:00:59 +00:00

nem: correct mosaic quantities

This commit is contained in:
Tomas Susanka 2018-04-27 16:28:56 +01:00 committed by Jan Pochyla
parent 616d117648
commit 0093d21bb6
3 changed files with 32 additions and 8 deletions

View File

@ -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)

View File

@ -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",

View File

@ -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: