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
|
|
|
|
|
2021-01-21 09:32:23 +00:00
|
|
|
from trezorlib import cardano, device, messages
|
2020-08-28 19:12:25 +00:00
|
|
|
from trezorlib.exceptions import TrezorFailure
|
|
|
|
|
|
|
|
from ...common import parametrize_using_common_fixtures
|
|
|
|
|
|
|
|
pytestmark = [
|
|
|
|
pytest.mark.altcoin,
|
|
|
|
pytest.mark.cardano,
|
|
|
|
pytest.mark.skip_t1,
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
@parametrize_using_common_fixtures(
|
2020-09-29 18:23:25 +00:00
|
|
|
"cardano/sign_tx_stake_pool_registration.json",
|
|
|
|
"cardano/sign_tx.json",
|
|
|
|
"cardano/sign_tx.slip39.json",
|
2020-08-28 19:12:25 +00:00
|
|
|
)
|
|
|
|
def test_cardano_sign_tx(client, parameters, result):
|
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"])
|
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:
|
|
|
|
response = cardano.sign_tx(
|
|
|
|
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"],
|
2021-01-25 16:12:32 +00:00
|
|
|
ttl=parameters.get("ttl"),
|
|
|
|
validity_interval_start=parameters.get("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,
|
2020-08-28 19:12:25 +00:00
|
|
|
)
|
2021-06-30 12:19:06 +00:00
|
|
|
assert response == _transform_expected_result(result)
|
2020-08-28 19:12:25 +00:00
|
|
|
|
|
|
|
|
2020-09-29 18:23:25 +00:00
|
|
|
@parametrize_using_common_fixtures(
|
|
|
|
"cardano/sign_tx.failed.json", "cardano/sign_tx_stake_pool_registration.failed.json"
|
|
|
|
)
|
2020-08-28 19:12:25 +00:00
|
|
|
def test_cardano_sign_tx_failed(client, parameters, result):
|
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"])
|
2020-08-28 19:12:25 +00:00
|
|
|
|
|
|
|
with client:
|
|
|
|
with pytest.raises(TrezorFailure, match=result["error_message"]):
|
|
|
|
cardano.sign_tx(
|
|
|
|
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"],
|
2021-01-25 16:12:32 +00:00
|
|
|
ttl=parameters.get("ttl"),
|
|
|
|
validity_interval_start=parameters.get("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,
|
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"]),
|
|
|
|
"chain_code": bytes.fromhex(witness["chain_code"])
|
|
|
|
if witness["chain_code"]
|
|
|
|
else None,
|
|
|
|
}
|
|
|
|
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"]),
|
|
|
|
}
|
|
|
|
if catalyst_signature := supplement.get("catalyst_signature"):
|
|
|
|
transformed_result["auxiliary_data_supplement"][
|
|
|
|
"catalyst_signature"
|
|
|
|
] = bytes.fromhex(catalyst_signature)
|
|
|
|
return transformed_result
|