# 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 . import pytest from trezorlib.cardano import ( create_address_parameters, get_address, get_public_key, parse_optional_bytes, ) from trezorlib.exceptions import TrezorFailure from trezorlib.messages import CardanoAddressType from trezorlib.tools import parse_path from ...common import parametrize_using_common_fixtures pytestmark = [ pytest.mark.altcoin, pytest.mark.cardano, pytest.mark.skip_t1, ] @parametrize_using_common_fixtures( "cardano/get_address_byron.json", "cardano/get_address_byron.slip39.json", "cardano/get_base_address.json", "cardano/get_base_address_with_staking_key_hash.json", "cardano/get_base_address_with_script_hashes.json", "cardano/get_enterprise_address.json", "cardano/get_pointer_address.json", "cardano/get_reward_address.json", ) def test_cardano_get_address(client, parameters, result): client.init_device(new_session=True, derive_cardano=True) address = get_address( client, address_parameters=create_address_parameters( address_type=getattr( CardanoAddressType, parameters["address_type"].upper() ), address_n=parse_path(parameters.get("path")) if "path" in parameters else None, address_n_staking=parse_path(parameters.get("staking_path")) if "staking_path" in parameters else None, staking_key_hash=parse_optional_bytes(parameters.get("staking_key_hash")), block_index=parameters.get("block_index"), tx_index=parameters.get("tx_index"), certificate_index=parameters.get("certificate_index"), script_payment_hash=parse_optional_bytes( parameters.get("script_payment_hash") ), script_staking_hash=parse_optional_bytes( parameters.get("script_staking_hash") ), ), protocol_magic=parameters["protocol_magic"], network_id=parameters["network_id"], show_display=True, ) assert address == result["expected_address"] @parametrize_using_common_fixtures( "cardano/get_public_key.json", "cardano/get_public_key.slip39.json", ) def test_cardano_get_public_key(client, parameters, result): client.init_device(new_session=True, derive_cardano=True) key = get_public_key(client, parse_path(parameters["path"])) assert key.node.public_key.hex() == result["public_key"] assert key.node.chain_code.hex() == result["chain_code"] assert key.xpub == result["public_key"] + result["chain_code"] def test_bad_session(client): client.init_device(new_session=True) with pytest.raises(TrezorFailure, match="not enabled"): get_public_key(client, parse_path("m/1852'/1815'/0'")) client.init_device(new_session=True, derive_cardano=False) with pytest.raises(TrezorFailure, match="not enabled"): get_public_key(client, parse_path("m/1852'/1815'/0'"))