1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-20 06:28:09 +00:00

fix(cardano): display tADA in testnet transactions

This commit is contained in:
David Misiak 2022-02-25 14:17:07 +01:00 committed by matejcik
parent 1f20c49536
commit a36fc6cadc
2 changed files with 27 additions and 16 deletions

View File

@ -25,7 +25,7 @@ from apps.common.paths import address_n_to_str
from . import seed
from .address import derive_human_readable_address, encode_human_readable_address
from .helpers import bech32, protocol_magics
from .helpers import bech32, network_ids, protocol_magics
from .helpers.utils import (
format_account_number,
format_asset_fingerprint,
@ -85,8 +85,9 @@ CERTIFICATE_TYPE_NAMES = {
}
def format_coin_amount(amount: int) -> str:
return f"{format_amount(amount, 6)} ADA"
def format_coin_amount(amount: int, network_id: int) -> str:
currency = "ADA" if network_ids.is_mainnet(network_id) else "tADA"
return f"{format_amount(amount, 6)} {currency}"
def is_printable_ascii_bytestring(bytestr: bytes) -> bool:
@ -232,12 +233,13 @@ async def confirm_sending(
ada_amount: int,
to: str,
is_change_output: bool,
network_id: int,
) -> None:
subtitle = "Change amount:" if is_change_output else "Confirm sending:"
await confirm_output(
ctx,
to,
format_coin_amount(ada_amount),
format_coin_amount(ada_amount, network_id),
title="Confirm transaction",
subtitle=subtitle,
font_amount=ui.BOLD,
@ -459,6 +461,7 @@ async def confirm_witness_request(
async def confirm_transaction(
ctx: wire.Context,
fee: int,
network_id: int,
protocol_magic: int,
ttl: int | None,
validity_interval_start: int | None,
@ -466,7 +469,7 @@ async def confirm_transaction(
tx_hash: bytes | None,
) -> None:
props: list[PropertyType] = [
("Transaction fee:", format_coin_amount(fee)),
("Transaction fee:", format_coin_amount(fee, network_id)),
]
if is_network_id_verifiable:
@ -516,7 +519,7 @@ async def confirm_certificate(
async def confirm_stake_pool_parameters(
ctx: wire.Context, pool_parameters: CardanoPoolParametersType
ctx: wire.Context, pool_parameters: CardanoPoolParametersType, network_id: int
) -> None:
margin_percentage = (
100.0 * pool_parameters.margin_numerator / pool_parameters.margin_denominator
@ -533,8 +536,8 @@ async def confirm_stake_pool_parameters(
),
("Pool reward account:", pool_parameters.reward_account),
(
f"Pledge: {format_coin_amount(pool_parameters.pledge)}\n"
+ f"Cost: {format_coin_amount(pool_parameters.cost)}\n"
f"Pledge: {format_coin_amount(pool_parameters.pledge, network_id)}\n"
+ f"Cost: {format_coin_amount(pool_parameters.cost, network_id)}\n"
+ f"Margin: {percentage_formatted}%",
None,
),
@ -641,7 +644,10 @@ async def confirm_stake_pool_registration_final(
async def confirm_withdrawal(
ctx: wire.Context, withdrawal: CardanoTxWithdrawal, reward_address_bytes: bytes
ctx: wire.Context,
withdrawal: CardanoTxWithdrawal,
reward_address_bytes: bytes,
network_id: int,
) -> None:
address_type_name = "script reward" if withdrawal.script_hash else "reward"
reward_address = encode_human_readable_address(reward_address_bytes)
@ -656,7 +662,7 @@ async def confirm_withdrawal(
)
)
props.append(("Amount:", format_coin_amount(withdrawal.amount)))
props.append(("Amount:", format_coin_amount(withdrawal.amount, network_id)))
await confirm_properties(
ctx,

View File

@ -372,6 +372,7 @@ async def _confirm_transaction(
await confirm_transaction(
ctx,
msg.fee,
msg.network_id,
msg.protocol_magic,
msg.ttl,
msg.validity_interval_start,
@ -385,6 +386,7 @@ async def _confirm_transaction(
await confirm_transaction(
ctx,
msg.fee,
msg.network_id,
msg.protocol_magic,
msg.ttl,
msg.validity_interval_start,
@ -547,7 +549,7 @@ async def _process_certificates(
validate_certificate(
certificate, signing_mode, protocol_magic, network_id, account_path_checker
)
await _show_certificate(ctx, certificate, signing_mode)
await _show_certificate(ctx, certificate, signing_mode, network_id)
if certificate.type == CardanoCertificateType.STAKE_POOL_REGISTRATION:
pool_parameters = certificate.pool_parameters
@ -658,7 +660,7 @@ async def _process_withdrawals(
)
previous_reward_address_bytes = reward_address_bytes
await confirm_withdrawal(ctx, withdrawal, reward_address_bytes)
await confirm_withdrawal(ctx, withdrawal, reward_address_bytes, network_id)
withdrawals_dict.add(reward_address_bytes, withdrawal.amount)
@ -999,7 +1001,7 @@ async def _show_output(
assert output.address is not None # _validate_output
address = output.address
await confirm_sending(ctx, output.amount, address, is_change_output)
await confirm_sending(ctx, output.amount, address, is_change_output, network_id)
def _validate_asset_group(
@ -1043,6 +1045,7 @@ async def _show_certificate(
ctx: wire.Context,
certificate: CardanoTxCertificate,
signing_mode: CardanoTxSigningMode,
network_id: int,
) -> None:
if signing_mode == CardanoTxSigningMode.ORDINARY_TRANSACTION:
assert certificate.path # validate_certificate
@ -1056,7 +1059,7 @@ async def _show_certificate(
):
await confirm_certificate(ctx, certificate)
elif signing_mode == CardanoTxSigningMode.POOL_REGISTRATION_AS_OWNER:
await _show_stake_pool_registration_certificate(ctx, certificate)
await _show_stake_pool_registration_certificate(ctx, certificate, network_id)
else:
raise RuntimeError # we didn't cover all signing modes
@ -1174,7 +1177,9 @@ def _sign_tx_hash(
async def _show_stake_pool_registration_certificate(
ctx: wire.Context, stake_pool_registration_certificate: CardanoTxCertificate
ctx: wire.Context,
stake_pool_registration_certificate: CardanoTxCertificate,
network_id: int,
) -> None:
pool_parameters = stake_pool_registration_certificate.pool_parameters
# _validate_stake_pool_registration_tx_structure ensures that there is only one
@ -1182,7 +1187,7 @@ async def _show_stake_pool_registration_certificate(
assert pool_parameters is not None
# display the transaction (certificate) in UI
await confirm_stake_pool_parameters(ctx, pool_parameters)
await confirm_stake_pool_parameters(ctx, pool_parameters, network_id)
await confirm_stake_pool_metadata(ctx, pool_parameters.metadata)