1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-18 04:18:10 +00:00

feat(python): support Cardano derivation type arguments

This commit is contained in:
matejcik 2021-10-22 16:04:43 +02:00 committed by matejcik
parent 72924a016f
commit f8b9cda05d
3 changed files with 65 additions and 11 deletions

View File

@ -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.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -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):

View File

@ -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
)