Skip to content

Commit

Permalink
fix(cardano): display tADA in testnet transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmisiak committed Mar 11, 2022
1 parent 7513776 commit 4eb1db1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
26 changes: 16 additions & 10 deletions core/src/apps/cardano/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

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,
Expand Down Expand Up @@ -85,8 +85,9 @@
}


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:
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -459,14 +461,15 @@ 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,
is_network_id_verifiable: bool,
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:
Expand Down Expand Up @@ -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
Expand All @@ -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,
),
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand Down
17 changes: 11 additions & 6 deletions core/src/apps/cardano/sign_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -1174,15 +1177,17 @@ 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
# certificate, and validate_certificate ensures that the structure is valid
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)

Expand Down

0 comments on commit 4eb1db1

Please sign in to comment.