From f8b9cda05d11f16b2b2811de22956376b4a4b42a Mon Sep 17 00:00:00 2001 From: matejcik Date: Fri, 22 Oct 2021 16:04:43 +0200 Subject: [PATCH] feat(python): support Cardano derivation type arguments --- python/docs/OPTIONS.rst | 7 ++-- python/src/trezorlib/cardano.py | 18 ++++++++-- python/src/trezorlib/cli/cardano.py | 51 +++++++++++++++++++++++++---- 3 files changed, 65 insertions(+), 11 deletions(-) diff --git a/python/docs/OPTIONS.rst b/python/docs/OPTIONS.rst index 78e2eb338..01fb9144c 100644 --- a/python/docs/OPTIONS.rst +++ b/python/docs/OPTIONS.rst @@ -123,9 +123,10 @@ Cardano commands. --help Show this message and exit. Commands: - get-address Get Cardano address. - get-public-key Get Cardano public key. - sign-tx Sign Cardano transaction. + get-address Get Cardano address. + get-native-script-hash Get Cardano native script hash. + get-public-key Get Cardano public key. + sign-tx Sign Cardano transaction. CoSi (Cothority / collective signing) commands. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/python/src/trezorlib/cardano.py b/python/src/trezorlib/cardano.py index 14eb23411..1b43e25e9 100644 --- a/python/src/trezorlib/cardano.py +++ b/python/src/trezorlib/cardano.py @@ -611,6 +611,7 @@ def get_address( protocol_magic: int = PROTOCOL_MAGICS["mainnet"], network_id: int = NETWORK_IDS["mainnet"], show_display: bool = False, + derivation_type: messages.CardanoDerivationType = messages.CardanoDerivationType.ICARUS, ) -> messages.CardanoAddress: return client.call( messages.CardanoGetAddress( @@ -618,13 +619,22 @@ def get_address( protocol_magic=protocol_magic, network_id=network_id, show_display=show_display, + derivation_type=derivation_type, ) ) @expect(messages.CardanoPublicKey) -def get_public_key(client, address_n: List[int]) -> messages.CardanoPublicKey: - return client.call(messages.CardanoGetPublicKey(address_n=address_n)) +def get_public_key( + client, + address_n: List[int], + derivation_type: messages.CardanoDerivationType = messages.CardanoDerivationType.ICARUS, +) -> messages.CardanoPublicKey: + return client.call( + messages.CardanoGetPublicKey( + address_n=address_n, derivation_type=derivation_type + ) + ) @expect(messages.CardanoNativeScriptHash) @@ -632,11 +642,13 @@ def get_native_script_hash( client, native_script: messages.CardanoNativeScript, display_format: messages.CardanoNativeScriptHashDisplayFormat = messages.CardanoNativeScriptHashDisplayFormat.HIDE, + derivation_type: messages.CardanoDerivationType = messages.CardanoDerivationType.ICARUS, ) -> messages.CardanoNativeScriptHash: return client.call( messages.CardanoGetNativeScriptHash( script=native_script, display_format=display_format, + derivation_type=derivation_type, ) ) @@ -656,6 +668,7 @@ def sign_tx( auxiliary_data: messages.CardanoTxAuxiliaryData = None, mint: List[AssetGroupWithTokens] = (), additional_witness_requests: List[Path] = (), + derivation_type: messages.CardanoDerivationType = messages.CardanoDerivationType.ICARUS, ) -> SignTxResponse: UNEXPECTED_RESPONSE_ERROR = exceptions.TrezorException("Unexpected response") @@ -678,6 +691,7 @@ def sign_tx( has_auxiliary_data=auxiliary_data is not None, minting_asset_groups_count=len(mint), witness_requests_count=len(witness_requests), + derivation_type=derivation_type, ) ) if not isinstance(response, messages.CardanoTxItemAck): diff --git a/python/src/trezorlib/cli/cardano.py b/python/src/trezorlib/cli/cardano.py index 99b81774f..eb2fce271 100644 --- a/python/src/trezorlib/cli/cardano.py +++ b/python/src/trezorlib/cli/cardano.py @@ -43,8 +43,16 @@ def cli(): ) @click.option("-N", "--network-id", type=int, default=cardano.NETWORK_IDS["mainnet"]) @click.option("-t", "--testnet", is_flag=True) +@click.option( + "-D", + "--derivation-type", + type=ChoiceType({m.name: m for m in messages.CardanoDerivationType}), + default=messages.CardanoDerivationType.ICARUS, +) @with_client -def sign_tx(client, file, signing_mode, protocol_magic, network_id, testnet): +def sign_tx( + client, file, signing_mode, protocol_magic, network_id, testnet, derivation_type +): """Sign Cardano transaction.""" transaction = json.load(file) @@ -72,6 +80,7 @@ def sign_tx(client, file, signing_mode, protocol_magic, network_id, testnet): for p in transaction["additional_witness_requests"] ] + client.init_device(derive_cardano=True) sign_tx_response = cardano.sign_tx( client, signing_mode, @@ -87,6 +96,7 @@ def sign_tx(client, file, signing_mode, protocol_magic, network_id, testnet): auxiliary_data, mint, additional_witness_requests, + derivation_type=derivation_type, ) sign_tx_response["tx_hash"] = sign_tx_response["tx_hash"].hex() @@ -134,6 +144,12 @@ def sign_tx(client, file, signing_mode, protocol_magic, network_id, testnet): ) @click.option("-N", "--network-id", type=int, default=cardano.NETWORK_IDS["mainnet"]) @click.option("-e", "--testnet", is_flag=True) +@click.option( + "-D", + "--derivation-type", + type=ChoiceType({m.name: m for m in messages.CardanoDerivationType}), + default=messages.CardanoDerivationType.ICARUS, +) @with_client def get_address( client, @@ -150,6 +166,7 @@ def get_address( network_id, show_display, testnet, + derivation_type, ): """ Get Cardano address. @@ -185,18 +202,31 @@ def get_address( script_staking_hash_bytes, ) + client.init_device(derive_cardano=True) return cardano.get_address( - client, address_parameters, protocol_magic, network_id, show_display + client, + address_parameters, + protocol_magic, + network_id, + show_display, + derivation_type=derivation_type, ) @cli.command() @click.option("-n", "--address", required=True, help=PATH_HELP) +@click.option( + "-D", + "--derivation-type", + type=ChoiceType({m.name: m for m in messages.CardanoDerivationType}), + default=messages.CardanoDerivationType.ICARUS, +) @with_client -def get_public_key(client, address): +def get_public_key(client, address, derivation_type): """Get Cardano public key.""" address_n = tools.parse_path(address) - return cardano.get_public_key(client, address_n) + client.init_device(derive_cardano=True) + return cardano.get_public_key(client, address_n, derivation_type=derivation_type) @cli.command() @@ -207,10 +237,19 @@ def get_public_key(client, address): type=ChoiceType({m.name: m for m in messages.CardanoNativeScriptHashDisplayFormat}), default="HIDE", ) +@click.option( + "-D", + "--derivation-type", + type=ChoiceType({m.name: m for m in messages.CardanoDerivationType}), + default=messages.CardanoDerivationType.ICARUS, +) @with_client -def get_native_script_hash(client, file, display_format): +def get_native_script_hash(client, file, display_format, derivation_type): """Get Cardano native script hash.""" native_script_json = json.load(file) native_script = cardano.parse_native_script(native_script_json) - return cardano.get_native_script_hash(client, native_script, display_format) + client.init_device(derive_cardano=True) + return cardano.get_native_script_hash( + client, native_script, display_format, derivation_type=derivation_type + )