From bd6bcf7dfaa55d27075d8a56fe6a0f45cf26b086 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Du=C5=A1an=20Plav=C3=A1k?= Date: Thu, 6 Sep 2018 16:25:55 +0100 Subject: [PATCH] Add network param to cardano sign tx (#306) --- trezorctl | 5 +- trezorlib/cardano.py | 6 +- .../test_msg_cardano_sign_transaction.py | 110 +++++++++++++++++- 3 files changed, 116 insertions(+), 5 deletions(-) diff --git a/trezorctl b/trezorctl index 6cbfc885a8..52c60b8891 100755 --- a/trezorctl +++ b/trezorctl @@ -1095,8 +1095,9 @@ def ethereum_sign_tx( required=True, help="Transaction in JSON format", ) +@click.option("-N", "--network", type=int, default=1) @click.pass_obj -def cardano_sign_tx(connect, file): +def cardano_sign_tx(connect, file, network): client = connect() transaction = json.load(file) @@ -1105,7 +1106,7 @@ def cardano_sign_tx(connect, file): outputs = [cardano.create_output(output) for output in transaction["outputs"]] transactions = transaction["transactions"] - signed_transaction = cardano.sign_tx(client, inputs, outputs, transactions) + signed_transaction = cardano.sign_tx(client, inputs, outputs, transactions, network) return { "tx_hash": binascii.hexlify(signed_transaction.tx_hash).decode(), diff --git a/trezorlib/cardano.py b/trezorlib/cardano.py index aa7d0df42c..8f95e58321 100644 --- a/trezorlib/cardano.py +++ b/trezorlib/cardano.py @@ -42,10 +42,14 @@ def sign_tx( inputs: List[messages.CardanoTxInputType], outputs: List[messages.CardanoTxOutputType], transactions: List[bytes], + network, ): response = client.call( messages.CardanoSignTx( - inputs=inputs, outputs=outputs, transactions_count=len(transactions) + inputs=inputs, + outputs=outputs, + transactions_count=len(transactions), + network=network, ) ) diff --git a/trezorlib/tests/device_tests/test_msg_cardano_sign_transaction.py b/trezorlib/tests/device_tests/test_msg_cardano_sign_transaction.py index 56565f55f0..200d9b5846 100644 --- a/trezorlib/tests/device_tests/test_msg_cardano_sign_transaction.py +++ b/trezorlib/tests/device_tests/test_msg_cardano_sign_transaction.py @@ -30,7 +30,7 @@ from .conftest import TREZOR_VERSION @pytest.mark.skip_t1 # T1 support is not planned @pytest.mark.xfail(TREZOR_VERSION == 2, reason="T2 support is not yet finished") class TestMsgCardanoSignTx(TrezorTest): - def test_cardano_sign_tx(self): + def test_cardano_sign_tx_mainnet(self): self.setup_mnemonic_allallall() transaction = { @@ -59,7 +59,10 @@ class TestMsgCardanoSignTx(TrezorTest): self.client.transport.write( messages.CardanoSignTx( - inputs=inputs, outputs=outputs, transactions_count=len(transactions) + inputs=inputs, + outputs=outputs, + transactions_count=len(transactions), + network=2, ) ) response = self.client.transport.read() @@ -81,6 +84,15 @@ class TestMsgCardanoSignTx(TrezorTest): self.client.transport.write(messages.ButtonAck()) time.sleep(1) + # Confirm Network + response = self.client.transport.read() + assert isinstance(response, messages.ButtonRequest) + assert response.code == messages.ButtonRequestType.Other + + self.client.debug.press_yes() + self.client.transport.write(messages.ButtonAck()) + time.sleep(1) + # Confirm Output response = self.client.transport.read() assert isinstance(response, messages.ButtonRequest) @@ -111,3 +123,97 @@ class TestMsgCardanoSignTx(TrezorTest): binascii.hexlify(response.tx_body) == b"82839f8200d81858248258201af8fa0b754ff99253d983894e63a2b09cbb56c833ba18c3384210163f63dcfc00ff9f8282d818582183581c9e1c71de652ec8b85fec296f0685ca3988781c94a2e1a5d89d92f45fa0001a0d0c25611a002dd2e8ffa0818200d818588582584089053545a6c254b0d9b1464e48d2b5fcf91d4e25c128afb1fcfc61d0843338ea26308151516f3b0e02bb1638142747863c520273ce9bd3e5cd91e1d46fe2a6355840312c01c27317415b0b8acc86aa789da877fe7e15c65b7ea4c4565d8739117f5f6d9d38bf5d058f7be809b2b9b06c1d79fc6b20f9a4d76d8c89bae333edf5680c" ) + + def test_cardano_sign_tx_testnet(self): + self.setup_mnemonic_allallall() + + transaction = { + "inputs": [ + { + "path": "m/44'/1815'/0'/0/1", + "prev_hash": "1af8fa0b754ff99253d983894e63a2b09cbb56c833ba18c3384210163f63dcfc", + "prev_index": 0, + "type": 0, + } + ], + "outputs": [ + { + "address": "Ae2tdPwUPEZCanmBz5g2GEwFqKTKpNJcGYPKfDxoNeKZ8bRHr8366kseiK2", + "amount": "3003112", + } + ], + "transactions": [ + "839f8200d818582482582008abb575fac4c39d5bf80683f7f0c37e48f4e3d96e37d1f6611919a7241b456600ff9f8282d818582183581cda4da43db3fca93695e71dab839e72271204d28b9d964d306b8800a8a0001a7a6916a51a00305becffa0" + ], + } + + inputs = [create_input(input) for input in transaction["inputs"]] + outputs = [create_output(output) for output in transaction["outputs"]] + transactions = transaction["transactions"] + + self.client.transport.write( + messages.CardanoSignTx( + inputs=inputs, + outputs=outputs, + transactions_count=len(transactions), + network=1, + ) + ) + response = self.client.transport.read() + + assert isinstance(response, messages.CardanoTxRequest) + assert response.tx_index == 0 + + # Upload first transaction + transaction_data = binascii.unhexlify(transactions[0]) + ack_message = messages.CardanoTxAck(transaction=transaction_data) + self.client.transport.write(ack_message) + + # Confirm fee + response = self.client.transport.read() + assert isinstance(response, messages.ButtonRequest) + assert response.code == messages.ButtonRequestType.Other + + self.client.debug.press_yes() + self.client.transport.write(messages.ButtonAck()) + time.sleep(1) + + # Confirm Network + response = self.client.transport.read() + assert isinstance(response, messages.ButtonRequest) + assert response.code == messages.ButtonRequestType.Other + + self.client.debug.press_yes() + self.client.transport.write(messages.ButtonAck()) + time.sleep(1) + + # Confirm Output + response = self.client.transport.read() + assert isinstance(response, messages.ButtonRequest) + assert response.code == messages.ButtonRequestType.Other + + self.client.debug.press_yes() + self.client.transport.write(messages.ButtonAck()) + time.sleep(1) + self.client.debug.swipe_down() + time.sleep(1) + + # Confirm amount + response = self.client.transport.read() + assert isinstance(response, messages.ButtonRequest) + assert response.code == messages.ButtonRequestType.Other + + self.client.debug.press_yes() + self.client.transport.write(messages.ButtonAck()) + + response = self.client.transport.read() + assert isinstance(response, messages.CardanoSignedTx) + + assert ( + binascii.hexlify(response.tx_hash) + == b"799c65e8a2c0b1dc4232611728c09d3f3eb0d811c077f8e9798f84605ef1b23d" + ) + assert ( + binascii.hexlify(response.tx_body) + == b"82839f8200d81858248258201af8fa0b754ff99253d983894e63a2b09cbb56c833ba18c3384210163f63dcfc00ff9f8282d818582183581c9e1c71de652ec8b85fec296f0685ca3988781c94a2e1a5d89d92f45fa0001a0d0c25611a002dd2e8ffa0818200d818588582584089053545a6c254b0d9b1464e48d2b5fcf91d4e25c128afb1fcfc61d0843338ea26308151516f3b0e02bb1638142747863c520273ce9bd3e5cd91e1d46fe2a63558403594ee7e2bfe4c84f886a8336cecb7c42983ce9a057345ebb6294a436087d8db93ca78cf514c7c48edff4c8435f690a5817951e2b55d2db729875ee7cc0f7d08" + )