2020-08-28 19:12:25 +00:00
|
|
|
# This file is part of the Trezor project.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2012-2019 SatoshiLabs and contributors
|
|
|
|
#
|
|
|
|
# This library is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser General Public License version 3
|
|
|
|
# as published by the Free Software Foundation.
|
|
|
|
#
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the License along with this library.
|
|
|
|
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2024-03-09 21:05:05 +00:00
|
|
|
from trezorlib import cardano, device, messages, models
|
2022-01-31 12:25:30 +00:00
|
|
|
from trezorlib.debuglink import TrezorClientDebugLink as Client
|
2020-08-28 19:12:25 +00:00
|
|
|
from trezorlib.exceptions import TrezorFailure
|
|
|
|
|
|
|
|
from ...common import parametrize_using_common_fixtures
|
2024-05-27 22:19:01 +00:00
|
|
|
from ...input_flows import InputFlowConfirmAllWarnings
|
2020-08-28 19:12:25 +00:00
|
|
|
|
|
|
|
pytestmark = [
|
|
|
|
pytest.mark.altcoin,
|
|
|
|
pytest.mark.cardano,
|
2024-03-11 15:20:08 +00:00
|
|
|
pytest.mark.skip_t1b1,
|
2020-08-28 19:12:25 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2022-06-30 08:42:41 +00:00
|
|
|
def show_details_input_flow(client: Client):
|
|
|
|
yield
|
|
|
|
client.debug.wait_layout()
|
2024-03-11 15:20:08 +00:00
|
|
|
# Touch screen click vs pressing right for T2B1
|
2024-03-09 21:05:05 +00:00
|
|
|
if client.model in (models.T2T1, models.T3T1):
|
2023-05-12 09:19:35 +00:00
|
|
|
SHOW_ALL_BUTTON_POSITION = (143, 167)
|
|
|
|
client.debug.click(SHOW_ALL_BUTTON_POSITION)
|
2024-03-09 21:05:05 +00:00
|
|
|
elif client.model is models.T2B1:
|
2023-05-12 09:19:35 +00:00
|
|
|
client.debug.press_yes()
|
2024-03-09 21:05:05 +00:00
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
2022-06-30 08:42:41 +00:00
|
|
|
# reset ui flow to continue "automatically"
|
|
|
|
client.ui.input_flow = None
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
2020-08-28 19:12:25 +00:00
|
|
|
@parametrize_using_common_fixtures(
|
2020-09-29 18:23:25 +00:00
|
|
|
"cardano/sign_tx_stake_pool_registration.json",
|
|
|
|
"cardano/sign_tx.json",
|
2021-07-23 12:54:32 +00:00
|
|
|
"cardano/sign_tx.multisig.json",
|
2021-10-21 17:43:10 +00:00
|
|
|
"cardano/sign_tx.plutus.json",
|
2020-09-29 18:23:25 +00:00
|
|
|
"cardano/sign_tx.slip39.json",
|
2020-08-28 19:12:25 +00:00
|
|
|
)
|
2022-01-31 12:25:30 +00:00
|
|
|
def test_cardano_sign_tx(client: Client, parameters, result):
|
2024-05-27 22:19:01 +00:00
|
|
|
response = call_sign_tx(
|
|
|
|
client,
|
|
|
|
parameters,
|
|
|
|
input_flow=lambda client: InputFlowConfirmAllWarnings(client).get(),
|
|
|
|
)
|
2022-06-30 08:42:41 +00:00
|
|
|
assert response == _transform_expected_result(result)
|
|
|
|
|
|
|
|
|
2024-05-31 00:39:13 +00:00
|
|
|
@pytest.mark.skip_t3t1(reason="Not yet implemented in new UI")
|
2022-06-30 08:42:41 +00:00
|
|
|
@parametrize_using_common_fixtures("cardano/sign_tx.show_details.json")
|
|
|
|
def test_cardano_sign_tx_show_details(client: Client, parameters, result):
|
2023-09-14 10:22:18 +00:00
|
|
|
response = call_sign_tx(client, parameters, show_details_input_flow, chunkify=True)
|
2022-06-30 08:42:41 +00:00
|
|
|
assert response == _transform_expected_result(result)
|
|
|
|
|
|
|
|
|
|
|
|
@parametrize_using_common_fixtures(
|
|
|
|
"cardano/sign_tx_stake_pool_registration.failed.json",
|
|
|
|
"cardano/sign_tx.failed.json",
|
|
|
|
"cardano/sign_tx.multisig.failed.json",
|
|
|
|
"cardano/sign_tx.plutus.failed.json",
|
|
|
|
)
|
|
|
|
def test_cardano_sign_tx_failed(client: Client, parameters, result):
|
|
|
|
with pytest.raises(TrezorFailure, match=result["error_message"]):
|
|
|
|
call_sign_tx(client, parameters, None)
|
|
|
|
|
|
|
|
|
2023-09-14 10:22:18 +00:00
|
|
|
def call_sign_tx(client: Client, parameters, input_flow=None, chunkify: bool = False):
|
2021-10-20 11:16:58 +00:00
|
|
|
client.init_device(new_session=True, derive_cardano=True)
|
|
|
|
|
2021-06-30 12:20:08 +00:00
|
|
|
signing_mode = messages.CardanoTxSigningMode.__members__[parameters["signing_mode"]]
|
2021-04-08 05:06:10 +00:00
|
|
|
inputs = [cardano.parse_input(i) for i in parameters["inputs"]]
|
|
|
|
outputs = [cardano.parse_output(o) for o in parameters["outputs"]]
|
|
|
|
certificates = [cardano.parse_certificate(c) for c in parameters["certificates"]]
|
|
|
|
withdrawals = [cardano.parse_withdrawal(w) for w in parameters["withdrawals"]]
|
|
|
|
auxiliary_data = cardano.parse_auxiliary_data(parameters["auxiliary_data"])
|
2021-07-23 12:54:32 +00:00
|
|
|
mint = cardano.parse_mint(parameters["mint"])
|
2021-10-12 11:33:24 +00:00
|
|
|
script_data_hash = cardano.parse_script_data_hash(parameters["script_data_hash"])
|
2021-10-21 17:43:10 +00:00
|
|
|
collateral_inputs = [
|
|
|
|
cardano.parse_collateral_input(i) for i in parameters["collateral_inputs"]
|
|
|
|
]
|
|
|
|
required_signers = [
|
|
|
|
cardano.parse_required_signer(s) for s in parameters["required_signers"]
|
|
|
|
]
|
2022-06-07 15:14:30 +00:00
|
|
|
collateral_return = (
|
|
|
|
cardano.parse_output(parameters["collateral_return"])
|
|
|
|
if parameters["collateral_return"] is not None
|
|
|
|
else None
|
|
|
|
)
|
|
|
|
reference_inputs = [
|
|
|
|
cardano.parse_reference_input(i) for i in parameters["reference_inputs"]
|
|
|
|
]
|
2021-07-23 12:54:32 +00:00
|
|
|
additional_witness_requests = [
|
|
|
|
cardano.parse_additional_witness_request(p)
|
|
|
|
for p in parameters["additional_witness_requests"]
|
|
|
|
]
|
2020-08-28 19:12:25 +00:00
|
|
|
|
2021-01-21 09:32:23 +00:00
|
|
|
if parameters.get("security_checks") == "prompt":
|
|
|
|
device.apply_settings(
|
|
|
|
client, safety_checks=messages.SafetyCheckLevel.PromptTemporarily
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
device.apply_settings(client, safety_checks=messages.SafetyCheckLevel.Strict)
|
2020-08-28 19:12:25 +00:00
|
|
|
|
|
|
|
with client:
|
2022-06-30 08:42:41 +00:00
|
|
|
if input_flow is not None:
|
|
|
|
client.watch_layout()
|
|
|
|
client.set_input_flow(input_flow(client))
|
|
|
|
|
|
|
|
return cardano.sign_tx(
|
2020-08-28 19:12:25 +00:00
|
|
|
client=client,
|
2021-06-30 12:19:06 +00:00
|
|
|
signing_mode=signing_mode,
|
2020-08-28 19:12:25 +00:00
|
|
|
inputs=inputs,
|
|
|
|
outputs=outputs,
|
|
|
|
fee=parameters["fee"],
|
2022-06-07 15:14:30 +00:00
|
|
|
ttl=parameters["ttl"],
|
|
|
|
validity_interval_start=parameters["validity_interval_start"],
|
2020-08-28 19:12:25 +00:00
|
|
|
certificates=certificates,
|
|
|
|
withdrawals=withdrawals,
|
|
|
|
protocol_magic=parameters["protocol_magic"],
|
|
|
|
network_id=parameters["network_id"],
|
2021-03-26 07:46:14 +00:00
|
|
|
auxiliary_data=auxiliary_data,
|
2021-07-23 12:54:32 +00:00
|
|
|
mint=mint,
|
2021-10-12 11:33:24 +00:00
|
|
|
script_data_hash=script_data_hash,
|
2021-10-21 17:43:10 +00:00
|
|
|
collateral_inputs=collateral_inputs,
|
|
|
|
required_signers=required_signers,
|
2022-06-07 15:14:30 +00:00
|
|
|
collateral_return=collateral_return,
|
|
|
|
total_collateral=parameters["total_collateral"],
|
|
|
|
reference_inputs=reference_inputs,
|
2021-07-23 12:54:32 +00:00
|
|
|
additional_witness_requests=additional_witness_requests,
|
2022-01-12 14:45:37 +00:00
|
|
|
include_network_id=parameters["include_network_id"],
|
2023-09-14 10:22:18 +00:00
|
|
|
chunkify=chunkify,
|
2023-12-08 12:48:53 +00:00
|
|
|
tag_cbor_sets=parameters["tag_cbor_sets"],
|
2020-08-28 19:12:25 +00:00
|
|
|
)
|
2021-01-21 09:32:23 +00:00
|
|
|
|
|
|
|
|
2021-06-30 12:19:06 +00:00
|
|
|
def _transform_expected_result(result):
|
|
|
|
"""Transform the JSON representation of the expected result into the format which is returned by trezorlib.
|
2021-02-19 21:13:39 +00:00
|
|
|
|
2021-06-30 12:19:06 +00:00
|
|
|
This involves converting the hex strings into real binary values."""
|
|
|
|
transformed_result = {
|
|
|
|
"tx_hash": bytes.fromhex(result["tx_hash"]),
|
|
|
|
"witnesses": [
|
|
|
|
{
|
|
|
|
"type": witness["type"],
|
|
|
|
"pub_key": bytes.fromhex(witness["pub_key"]),
|
|
|
|
"signature": bytes.fromhex(witness["signature"]),
|
2024-02-28 14:11:47 +00:00
|
|
|
"chain_code": (
|
|
|
|
bytes.fromhex(witness["chain_code"])
|
|
|
|
if witness["chain_code"]
|
|
|
|
else None
|
|
|
|
),
|
2021-06-30 12:19:06 +00:00
|
|
|
}
|
|
|
|
for witness in result["witnesses"]
|
|
|
|
],
|
|
|
|
}
|
|
|
|
if supplement := result.get("auxiliary_data_supplement"):
|
|
|
|
transformed_result["auxiliary_data_supplement"] = {
|
|
|
|
"type": supplement["type"],
|
|
|
|
"auxiliary_data_hash": bytes.fromhex(supplement["auxiliary_data_hash"]),
|
|
|
|
}
|
2023-02-13 16:05:20 +00:00
|
|
|
if cvote_registration_signature := supplement.get(
|
|
|
|
"cvote_registration_signature"
|
|
|
|
):
|
2021-06-30 12:19:06 +00:00
|
|
|
transformed_result["auxiliary_data_supplement"][
|
2023-02-13 16:05:20 +00:00
|
|
|
"cvote_registration_signature"
|
|
|
|
] = bytes.fromhex(cvote_registration_signature)
|
2021-06-30 12:19:06 +00:00
|
|
|
return transformed_result
|