refactor(cardano): shorten identifiers

pull/2374/head
David Misiak 2 years ago committed by matejcik
parent 1ae6b68eaf
commit 2753faa7b0

@ -132,7 +132,7 @@ def cborize_certificate_stake_credential(
raise RuntimeError
def cborize_initial_pool_registration_certificate_fields(
def cborize_pool_registration_certificate_init(
certificate: messages.CardanoTxCertificate,
) -> CborSequence:
assert certificate.type == CardanoCertificateType.STAKE_POOL_REGISTRATION

@ -2,23 +2,21 @@ from trezor import log, messages, wire
from . import seed
from .address import derive_human_readable_address, validate_address_parameters
from .helpers.credential import Credential, should_show_address_credentials
from .helpers.credential import Credential, should_show_credentials
from .helpers.utils import validate_network_info
from .layout import show_address_credentials, show_cardano_address
from .layout import show_cardano_address, show_credentials
@seed.with_keychain
async def get_address(
ctx: wire.Context, msg: messages.CardanoGetAddress, keychain: seed.Keychain
) -> messages.CardanoAddress:
address_parameters = msg.address_parameters
validate_network_info(msg.network_id, msg.protocol_magic)
validate_address_parameters(address_parameters)
validate_address_parameters(msg.address_parameters)
try:
address = derive_human_readable_address(
keychain, address_parameters, msg.protocol_magic, msg.network_id
keychain, msg.address_parameters, msg.protocol_magic, msg.network_id
)
except ValueError as e:
if __debug__:
@ -26,7 +24,7 @@ async def get_address(
raise wire.ProcessError("Deriving address failed")
if msg.show_display:
await _display_address(ctx, address_parameters, address, msg.protocol_magic)
await _display_address(ctx, msg.address_parameters, address, msg.protocol_magic)
return messages.CardanoAddress(address=address)
@ -37,8 +35,8 @@ async def _display_address(
address: str,
protocol_magic: int,
) -> None:
if should_show_address_credentials(address_parameters):
await show_address_credentials(
if should_show_credentials(address_parameters):
await show_credentials(
ctx,
Credential.payment_credential(address_parameters),
Credential.stake_credential(address_parameters),

@ -203,7 +203,7 @@ class Credential:
return []
def should_show_address_credentials(
def should_show_credentials(
address_parameters: messages.CardanoAddressParametersType,
) -> bool:
return not (

@ -179,7 +179,7 @@ async def show_script_hash(
)
async def show_multisig_transaction(ctx: wire.Context) -> None:
async def show_multisig_tx(ctx: wire.Context) -> None:
await confirm_metadata(
ctx,
"confirm_signing_mode",
@ -190,7 +190,7 @@ async def show_multisig_transaction(ctx: wire.Context) -> None:
)
async def show_plutus_transaction(ctx: wire.Context) -> None:
async def show_plutus_tx(ctx: wire.Context) -> None:
await confirm_metadata(
ctx,
"confirm_signing_mode",
@ -258,7 +258,7 @@ async def confirm_sending_token(
)
async def show_address_credentials(
async def show_credentials(
ctx: wire.Context,
payment_credential: Credential,
stake_credential: Credential,
@ -348,11 +348,11 @@ async def _show_credential(
)
async def show_warning_path(ctx: wire.Context, path: list[int], title: str) -> None:
async def warn_path(ctx: wire.Context, path: list[int], title: str) -> None:
await confirm_path_warning(ctx, address_n_to_str(path), path_type=title)
async def show_warning_tx_output_contains_tokens(ctx: wire.Context) -> None:
async def warn_tx_output_contains_tokens(ctx: wire.Context) -> None:
await confirm_metadata(
ctx,
"confirm_tokens",
@ -363,7 +363,7 @@ async def show_warning_tx_output_contains_tokens(ctx: wire.Context) -> None:
)
async def show_warning_tx_contains_mint(ctx: wire.Context) -> None:
async def warn_tx_contains_mint(ctx: wire.Context) -> None:
await confirm_metadata(
ctx,
"confirm_tokens",
@ -374,7 +374,7 @@ async def show_warning_tx_contains_mint(ctx: wire.Context) -> None:
)
async def show_warning_tx_output_contains_datum_hash(
async def warn_tx_output_contains_datum_hash(
ctx: wire.Context, datum_hash: bytes
) -> None:
await confirm_properties(
@ -392,7 +392,7 @@ async def show_warning_tx_output_contains_datum_hash(
)
async def show_warning_tx_output_no_datum_hash(ctx: wire.Context) -> None:
async def warn_tx_output_no_datum_hash(ctx: wire.Context) -> None:
await confirm_metadata(
ctx,
"confirm_no_datum_hash",
@ -402,7 +402,7 @@ async def show_warning_tx_output_no_datum_hash(ctx: wire.Context) -> None:
)
async def show_warning_no_script_data_hash(ctx: wire.Context) -> None:
async def warn_no_script_data_hash(ctx: wire.Context) -> None:
await confirm_metadata(
ctx,
"confirm_no_script_data_hash",
@ -412,7 +412,7 @@ async def show_warning_no_script_data_hash(ctx: wire.Context) -> None:
)
async def show_warning_no_collateral_inputs(ctx: wire.Context) -> None:
async def warn_no_collateral_inputs(ctx: wire.Context) -> None:
await confirm_metadata(
ctx,
"confirm_no_collateral_inputs",
@ -443,7 +443,7 @@ async def confirm_witness_request(
)
async def confirm_transaction(
async def confirm_tx(
ctx: wire.Context,
fee: int,
network_id: int,
@ -633,13 +633,13 @@ async def confirm_stake_pool_registration_final(
async def confirm_withdrawal(
ctx: wire.Context,
withdrawal: messages.CardanoTxWithdrawal,
reward_address_bytes: bytes,
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)
address = encode_human_readable_address(address_bytes)
props: list[PropertyType] = [
(f"Confirm withdrawal for {address_type_name} address:", reward_address),
(f"Confirm withdrawal for {address_type_name} address:", address),
]
if withdrawal.path:
@ -739,7 +739,7 @@ async def confirm_token_minting(
)
async def show_warning_tx_network_unverifiable(ctx: wire.Context) -> None:
async def warn_tx_network_unverifiable(ctx: wire.Context) -> None:
await confirm_metadata(
ctx,
"warning_no_outputs",

@ -32,9 +32,8 @@ def validate_native_script(script: messages.CardanoNativeScript | None) -> None:
if len(script.key_hash) != ADDRESS_KEY_HASH_SIZE:
raise INVALID_NATIVE_SCRIPT
elif script.key_path:
if not is_multisig_path(script.key_path) and not SCHEMA_MINT.match(
script.key_path
):
is_minting = SCHEMA_MINT.match(script.key_path)
if not is_multisig_path(script.key_path) and not is_minting:
raise INVALID_NATIVE_SCRIPT
else:
raise INVALID_NATIVE_SCRIPT

@ -20,17 +20,17 @@ class MultisigSigner(Signer):
) -> None:
super().__init__(ctx, msg, keychain)
def _validate_tx_signing_request(self) -> None:
super()._validate_tx_signing_request()
def _validate_tx_init(self) -> None:
super()._validate_tx_init()
if (
self.msg.collateral_inputs_count != 0
or self.msg.required_signers_count != 0
):
raise wire.ProcessError("Invalid tx signing request")
async def _show_tx_signing_request(self) -> None:
await layout.show_multisig_transaction(self.ctx)
await super()._show_tx_signing_request()
async def _show_tx_init(self) -> None:
await layout.show_multisig_tx(self.ctx)
await super()._show_tx_init()
async def _confirm_tx(self, tx_hash: bytes) -> None:
# super() omitted intentionally
@ -68,10 +68,10 @@ class MultisigSigner(Signer):
) -> None:
super()._validate_witness_request(witness_request)
is_minting = SCHEMA_MINT.match(witness_request.path)
transaction_has_token_minting = self.msg.minting_asset_groups_count > 0
tx_has_token_minting = self.msg.minting_asset_groups_count > 0
if not (
is_multisig_path(witness_request.path)
or (is_minting and transaction_has_token_minting)
or (is_minting and tx_has_token_minting)
):
raise wire.ProcessError("Invalid witness request")

@ -26,8 +26,8 @@ class OrdinarySigner(Signer):
) -> None:
super().__init__(ctx, msg, keychain)
def _validate_tx_signing_request(self) -> None:
super()._validate_tx_signing_request()
def _validate_tx_init(self) -> None:
super()._validate_tx_init()
if (
self.msg.collateral_inputs_count != 0
or self.msg.required_signers_count != 0
@ -65,12 +65,12 @@ class OrdinarySigner(Signer):
) -> None:
super()._validate_witness_request(witness_request)
is_minting = SCHEMA_MINT.match(witness_request.path)
transaction_has_token_minting = self.msg.minting_asset_groups_count > 0
tx_has_token_minting = self.msg.minting_asset_groups_count > 0
if not (
is_byron_path(witness_request.path)
or is_shelley_path(witness_request.path)
or (is_minting and transaction_has_token_minting)
or (is_minting and tx_has_token_minting)
):
raise wire.ProcessError("Invalid witness request")

@ -2,7 +2,7 @@ from trezor import messages, wire
from trezor.enums import CardanoCertificateType
from .. import layout, seed
from ..helpers.credential import Credential, should_show_address_credentials
from ..helpers.credential import Credential, should_show_credentials
from ..helpers.paths import SCHEMA_MINT
from ..seed import is_multisig_path, is_shelley_path
from .signer import Signer
@ -22,22 +22,22 @@ class PlutusSigner(Signer):
) -> None:
super().__init__(ctx, msg, keychain)
async def _show_tx_signing_request(self) -> None:
await layout.show_plutus_transaction(self.ctx)
await super()._show_tx_signing_request()
async def _show_tx_init(self) -> None:
await layout.show_plutus_tx(self.ctx)
await super()._show_tx_init()
# These items should be present if a Plutus script is to be executed.
if self.msg.script_data_hash is None:
await layout.show_warning_no_script_data_hash(self.ctx)
await layout.warn_no_script_data_hash(self.ctx)
if self.msg.collateral_inputs_count == 0:
await layout.show_warning_no_collateral_inputs(self.ctx)
await layout.warn_no_collateral_inputs(self.ctx)
async def _confirm_transaction(self, tx_hash: bytes) -> None:
async def _confirm_tx(self, tx_hash: bytes) -> None:
# super() omitted intentionally
# We display tx hash so that experienced users can compare it to the tx hash
# computed by a trusted device (in case the tx contains many items which are
# tedious to check one by one on the Trezor screen).
is_network_id_verifiable = self._is_network_id_verifiable()
await layout.confirm_transaction(
await layout.confirm_tx(
self.ctx,
self.msg.fee,
self.msg.network_id,
@ -59,7 +59,7 @@ class PlutusSigner(Signer):
# In ordinary txs, change outputs with matching payment and staking paths can be
# hidden, but we need to show them in Plutus txs because of the script
# evaluation. We at least hide the staking path if it matches the payment path.
show_both_credentials = should_show_address_credentials(address_parameters)
show_both_credentials = should_show_credentials(address_parameters)
await layout.show_device_owned_output_credentials(
self.ctx,
Credential.payment_credential(address_parameters),

@ -27,8 +27,8 @@ class PoolOwnerSigner(Signer):
) -> None:
super().__init__(ctx, msg, keychain)
def _validate_tx_signing_request(self) -> None:
super()._validate_tx_signing_request()
def _validate_tx_init(self) -> None:
super()._validate_tx_init()
if (
self.msg.certificates_count != 1
or self.msg.withdrawals_count != 0
@ -45,7 +45,7 @@ class PoolOwnerSigner(Signer):
):
raise wire.ProcessError("Invalid tx signing request")
async def _confirm_transaction(self, tx_hash: bytes) -> None:
async def _confirm_tx(self, tx_hash: bytes) -> None:
# super() omitted intentionally
await layout.confirm_stake_pool_registration_final(
self.ctx,

@ -30,9 +30,9 @@ from ..auxiliary_data import (
from ..certificates import (
assert_certificate_cond,
cborize_certificate,
cborize_initial_pool_registration_certificate_fields,
cborize_pool_metadata,
cborize_pool_owner,
cborize_pool_registration_certificate_init,
cborize_pool_relay,
validate_certificate,
validate_pool_owner,
@ -46,7 +46,7 @@ from ..helpers import (
SCRIPT_DATA_HASH_SIZE,
)
from ..helpers.account_path_check import AccountPathChecker
from ..helpers.credential import Credential, should_show_address_credentials
from ..helpers.credential import Credential, should_show_credentials
from ..helpers.hash_builder_collection import HashBuilderDict, HashBuilderList
from ..helpers.paths import (
CERTIFICATE_PATH_NAME,
@ -115,7 +115,7 @@ class Signer:
self.account_path_checker = AccountPathChecker()
# Inputs, outputs and fee are mandatory, count the number of optional fields present.
tx_body_map_item_count = 3 + sum(
tx_dict_items_count = 3 + sum(
(
msg.ttl is not None,
msg.certificates_count > 0,
@ -130,17 +130,17 @@ class Signer:
)
)
self.tx_dict: HashBuilderDict[int, Any] = HashBuilderDict(
tx_body_map_item_count, wire.ProcessError("Invalid tx signing request")
tx_dict_items_count, wire.ProcessError("Invalid tx signing request")
)
async def sign(self) -> None:
hash_fn = hashlib.blake2b(outlen=32)
self.tx_dict.start(hash_fn)
with self.tx_dict:
await self._processs_tx_signing_request()
await self._processs_tx_init()
tx_hash = hash_fn.digest()
await self._confirm_transaction(tx_hash)
await self._confirm_tx(tx_hash)
response_after_witness_requests = await self._process_witness_requests(tx_hash)
await self.ctx.call(response_after_witness_requests, messages.CardanoTxHostAck)
@ -150,9 +150,9 @@ class Signer:
# signing request
async def _processs_tx_signing_request(self) -> None:
self._validate_tx_signing_request()
await self._show_tx_signing_request()
async def _processs_tx_init(self) -> None:
self._validate_tx_init()
await self._show_tx_init()
inputs_list: HashBuilderList[tuple[bytes, int]] = HashBuilderList(
self.msg.inputs_count
@ -221,14 +221,14 @@ class Signer:
if self.msg.include_network_id:
self.tx_dict.add(TX_BODY_KEY_NETWORK_ID, self.msg.network_id)
def _validate_tx_signing_request(self) -> None:
def _validate_tx_init(self) -> None:
if self.msg.fee > LOVELACE_MAX_SUPPLY:
raise wire.ProcessError("Fee is out of range!")
validate_network_info(self.msg.network_id, self.msg.protocol_magic)
async def _show_tx_signing_request(self) -> None:
async def _show_tx_init(self) -> None:
if not self._is_network_id_verifiable():
await layout.show_warning_tx_network_unverifiable(self.ctx)
await layout.warn_tx_network_unverifiable(self.ctx)
async def _confirm_tx(self, tx_hash: bytes) -> None:
# Final signing confirmation is handled separately in each signing mode.
@ -328,16 +328,14 @@ class Signer:
return
if output.datum_hash is not None:
await layout.show_warning_tx_output_contains_datum_hash(
self.ctx, output.datum_hash
)
await layout.warn_tx_output_contains_datum_hash(self.ctx, output.datum_hash)
address_type = self._get_output_address_type(output)
if output.datum_hash is None and address_type in ADDRESS_TYPES_PAYMENT_SCRIPT:
await layout.show_warning_tx_output_no_datum_hash(self.ctx)
await layout.warn_tx_output_no_datum_hash(self.ctx)
if output.asset_groups_count > 0:
await layout.show_warning_tx_output_contains_tokens(self.ctx)
await layout.warn_tx_output_contains_tokens(self.ctx)
if output.address_parameters is not None:
address = derive_human_readable_address(
@ -384,7 +382,7 @@ class Signer:
return True
if output.address_parameters is not None: # change output
if not should_show_address_credentials(output.address_parameters):
if not should_show_credentials(output.address_parameters):
# We don't need to display simple address outputs.
return False
@ -491,9 +489,7 @@ class Signer:
POOL_REGISTRATION_CERTIFICATE_ITEMS_COUNT
)
with certificates_list.append(pool_items_list):
for item in cborize_initial_pool_registration_certificate_fields(
certificate
):
for item in cborize_pool_registration_certificate_init(certificate):
pool_items_list.append(item)
pool_owners_list: HashBuilderList[bytes] = HashBuilderList(
@ -600,13 +596,11 @@ class Signer:
messages.CardanoTxItemAck(), messages.CardanoTxWithdrawal
)
self._validate_withdrawal(withdrawal)
reward_address_bytes = self._derive_withdrawal_reward_address_bytes(
withdrawal
)
address_bytes = self._derive_withdrawal_address_bytes(withdrawal)
await layout.confirm_withdrawal(
self.ctx, withdrawal, reward_address_bytes, self.msg.network_id
self.ctx, withdrawal, address_bytes, self.msg.network_id
)
withdrawals_dict.add(reward_address_bytes, withdrawal.amount)
withdrawals_dict.add(address_bytes, withdrawal.amount)
def _validate_withdrawal(self, withdrawal: messages.CardanoTxWithdrawal) -> None:
validate_stake_credential(
@ -656,7 +650,7 @@ class Signer:
messages.CardanoTxItemAck(), messages.CardanoTxMint
)
await layout.show_warning_tx_contains_mint(self.ctx)
await layout.warn_tx_contains_mint(self.ctx)
for _ in range(token_minting.asset_groups_count):
asset_group: messages.CardanoAssetGroup = await self.ctx.call(
@ -832,7 +826,7 @@ class Signer:
assert output.address is not None # _validate_output
return get_address_type(get_address_bytes_unsafe(output.address))
def _derive_withdrawal_reward_address_bytes(
def _derive_withdrawal_address_bytes(
self, withdrawal: messages.CardanoTxWithdrawal
) -> bytes:
reward_address_type = (
@ -888,7 +882,7 @@ class Signer:
if safety_checks.is_strict():
raise wire.DataError(f"Invalid {path_name.lower()}")
else:
await layout.show_warning_path(self.ctx, path, path_name)
await layout.warn_path(self.ctx, path, path_name)
def _fail_if_strict_and_unusual(
self, address_parameters: messages.CardanoAddressParametersType

Loading…
Cancel
Save