예제 #1
0
def test_cardano_sign_tx(client, protocol_magic, inputs, outputs, transactions,
                         tx_hash, tx_body):
    inputs = [cardano.create_input(i) for i in inputs]
    outputs = [cardano.create_output(o) for o in outputs]

    expected_responses = [
        messages.PassphraseRequest(),
        messages.PassphraseStateRequest(),
    ]
    expected_responses += [
        messages.CardanoTxRequest(tx_index=i) for i in range(len(transactions))
    ]
    expected_responses += [
        messages.ButtonRequest(code=messages.ButtonRequestType.Other),
        messages.ButtonRequest(code=messages.ButtonRequestType.Other),
        messages.CardanoSignedTx(),
    ]

    def input_flow():
        yield
        client.debug.swipe_down()
        client.debug.press_yes()
        yield
        client.debug.swipe_down()
        client.debug.press_yes()

    client.set_passphrase("TREZOR")
    with client:
        client.set_expected_responses(expected_responses)
        client.set_input_flow(input_flow)
        response = cardano.sign_tx(client, inputs, outputs, transactions,
                                   protocol_magic)
        assert response.tx_hash.hex() == tx_hash
        assert response.tx_body.hex() == tx_body
예제 #2
0
def test_cardano_sign_tx_failed(client, parameters, result):
    inputs = [cardano.create_input(i) for i in parameters["inputs"]]
    outputs = [cardano.create_output(o) for o in parameters["outputs"]]
    certificates = [
        cardano.create_certificate(c) for c in parameters["certificates"]
    ]
    withdrawals = [
        cardano.create_withdrawal(w) for w in parameters["withdrawals"]
    ]

    expected_responses = [messages.PassphraseRequest(), messages.Failure()]

    with client:
        client.set_expected_responses(expected_responses)

        with pytest.raises(TrezorFailure, match=result["error_message"]):
            cardano.sign_tx(
                client=client,
                inputs=inputs,
                outputs=outputs,
                fee=parameters["fee"],
                ttl=parameters["ttl"],
                certificates=certificates,
                withdrawals=withdrawals,
                metadata=bytes.fromhex(parameters["metadata"]),
                protocol_magic=parameters["protocol_magic"],
                network_id=parameters["network_id"],
            )
예제 #3
0
def test_cardano_sign_tx_failed(client, parameters, result):
    inputs = [cardano.create_input(i) for i in parameters["inputs"]]
    outputs = [cardano.create_output(o) for o in parameters["outputs"]]
    certificates = [
        cardano.create_certificate(c) for c in parameters["certificates"]
    ]
    withdrawals = [
        cardano.create_withdrawal(w) for w in parameters["withdrawals"]
    ]

    input_flow = parameters.get("input_flow", ())

    with client:
        client.set_input_flow(_to_device_actions(client, input_flow))

        with pytest.raises(TrezorFailure, match=result["error_message"]):
            cardano.sign_tx(
                client=client,
                inputs=inputs,
                outputs=outputs,
                fee=parameters["fee"],
                ttl=parameters.get("ttl"),
                validity_interval_start=parameters.get(
                    "validity_interval_start"),
                certificates=certificates,
                withdrawals=withdrawals,
                metadata=bytes.fromhex(parameters["metadata"]),
                protocol_magic=parameters["protocol_magic"],
                network_id=parameters["network_id"],
            )
예제 #4
0
def test_cardano_sign_tx(client, network, inputs, outputs, transactions,
                         tx_hash, tx_body):
    inputs = [cardano.create_input(i) for i in inputs]
    outputs = [cardano.create_output(o) for o in outputs]

    expected_responses = [
        messages.CardanoTxRequest(tx_index=i) for i in range(len(transactions))
    ]
    expected_responses += [
        messages.ButtonRequest(code=messages.ButtonRequestType.Other),
        messages.ButtonRequest(code=messages.ButtonRequestType.Other),
        messages.ButtonRequest(code=messages.ButtonRequestType.Other),
        messages.ButtonRequest(code=messages.ButtonRequestType.Other),
        messages.CardanoSignedTx(),
    ]

    with client:
        client.set_expected_responses(expected_responses)
        response = cardano.sign_tx(client,
                                   inputs,
                                   outputs,
                                   transactions,
                                   network=network)
        assert response.tx_hash.hex() == tx_hash
        assert response.tx_body.hex() == tx_body
def test_cardano_sign_tx(
    client, protocol_magic, inputs, outputs, transactions, tx_hash, tx_body
):
    inputs = [cardano.create_input(i) for i in inputs]
    outputs = [cardano.create_output(o) for o in outputs]

    expected_responses = [
        messages.CardanoTxRequest(tx_index=i) for i in range(len(transactions))
    ]
    expected_responses += [
        messages.ButtonRequest(code=messages.ButtonRequestType.Other),
        messages.ButtonRequest(code=messages.ButtonRequestType.Other),
        messages.CardanoSignedTx(),
    ]

    def input_flow():
        yield
        client.debug.swipe_down()
        client.debug.press_yes()
        yield
        client.debug.swipe_down()
        client.debug.press_yes()

    with client:
        client.set_expected_responses(expected_responses)
        client.set_input_flow(input_flow)
        response = cardano.sign_tx(
            client, inputs, outputs, transactions, protocol_magic
        )
        assert response.tx_hash.hex() == tx_hash
        assert response.tx_body.hex() == tx_body
def test_cardano_sign_tx(
    client,
    protocol_magic,
    network_id,
    inputs,
    outputs,
    fee,
    ttl,
    input_flow_sequences,
    tx_hash,
    serialized_tx,
):
    inputs = [cardano.create_input(i) for i in inputs]
    outputs = [cardano.create_output(o) for o in outputs]
    certificates = []
    withdrawals = []
    metadata = bytes()

    expected_responses = [
        messages.PassphraseRequest(),
    ]
    expected_responses += [
        messages.ButtonRequest(code=messages.ButtonRequestType.Other)
        for i in range(len(input_flow_sequences))
    ]
    expected_responses.append(messages.CardanoSignedTx())

    def input_flow():
        for sequence in input_flow_sequences:
            yield
            for action in sequence:
                if action == InputAction.SWIPE:
                    client.debug.swipe_up()
                elif action == InputAction.YES:
                    client.debug.press_yes()
                else:
                    raise ValueError("Invalid input action")

    client.use_passphrase("TREZOR")
    with client:
        client.set_expected_responses(expected_responses)
        client.set_input_flow(input_flow)
        response = cardano.sign_tx(
            client=client,
            inputs=inputs,
            outputs=outputs,
            fee=fee,
            ttl=ttl,
            certificates=certificates,
            withdrawals=withdrawals,
            metadata=metadata,
            protocol_magic=protocol_magic,
            network_id=network_id,
        )
        assert response.tx_hash.hex() == tx_hash
        assert response.serialized_tx.hex() == serialized_tx
def test_cardano_sign_tx_validation(client, protocol_magic, inputs, outputs,
                                    transactions, expected_error_message):
    inputs = [cardano.create_input(i) for i in inputs]
    outputs = [cardano.create_output(o) for o in outputs]

    expected_responses = [
        messages.CardanoTxRequest(tx_index=i) for i in range(len(transactions))
    ]
    expected_responses += [messages.Failure()]

    with client:
        client.set_expected_responses(expected_responses)

        with pytest.raises(TrezorFailure, match=expected_error_message):
            cardano.sign_tx(client, inputs, outputs, transactions,
                            protocol_magic)
예제 #8
0
def test_cardano_sign_tx(client, parameters, result):
    inputs = [cardano.create_input(i) for i in parameters["inputs"]]
    outputs = [cardano.create_output(o) for o in parameters["outputs"]]
    certificates = [
        cardano.create_certificate(c) for c in parameters["certificates"]
    ]
    withdrawals = [
        cardano.create_withdrawal(w) for w in parameters["withdrawals"]
    ]

    expected_responses = [messages.PassphraseRequest()]
    expected_responses += [
        messages.ButtonRequest(code=messages.ButtonRequestType.Other)
        for i in range(len(parameters["input_flow"]))
    ]
    expected_responses.append(messages.CardanoSignedTx())

    def input_flow():
        for sequence in parameters["input_flow"]:
            yield
            for action in sequence:
                if action == "SWIPE":
                    client.debug.swipe_up()
                elif action == "YES":
                    client.debug.press_yes()
                else:
                    raise ValueError("Invalid input action")

    with client:
        client.set_expected_responses(expected_responses)
        client.set_input_flow(input_flow)
        response = cardano.sign_tx(
            client=client,
            inputs=inputs,
            outputs=outputs,
            fee=parameters["fee"],
            ttl=parameters.get("ttl"),
            validity_interval_start=parameters.get("validity_interval_start"),
            certificates=certificates,
            withdrawals=withdrawals,
            metadata=bytes.fromhex(parameters["metadata"]),
            protocol_magic=parameters["protocol_magic"],
            network_id=parameters["network_id"],
        )
        assert response.tx_hash.hex() == result["tx_hash"]
        assert response.serialized_tx.hex() == result["serialized_tx"]
예제 #9
0
def test_cardano_sign_tx_with_multiple_chunks(client, parameters, result):
    inputs = [cardano.create_input(i) for i in parameters["inputs"]]
    outputs = [cardano.create_output(o) for o in parameters["outputs"]]
    certificates = [
        cardano.create_certificate(c) for c in parameters["certificates"]
    ]
    withdrawals = [
        cardano.create_withdrawal(w) for w in parameters["withdrawals"]
    ]

    input_flow = parameters.get("input_flow", ())

    expected_responses = [
        messages.PassphraseRequest(),
        messages.ButtonRequest(),
        messages.ButtonRequest(),
    ]
    expected_responses += [
        messages.CardanoSignedTxChunk(
            signed_tx_chunk=bytes.fromhex(signed_tx_chunk))
        for signed_tx_chunk in result["signed_tx_chunks"]
    ]
    expected_responses += [
        messages.CardanoSignedTx(tx_hash=bytes.fromhex(result["tx_hash"]))
    ]

    with client:
        client.set_input_flow(_to_device_actions(client, input_flow))
        client.set_expected_responses(expected_responses)

        response = cardano.sign_tx(
            client=client,
            inputs=inputs,
            outputs=outputs,
            fee=parameters["fee"],
            ttl=parameters.get("ttl"),
            validity_interval_start=parameters.get("validity_interval_start"),
            certificates=certificates,
            withdrawals=withdrawals,
            metadata=bytes.fromhex(parameters["metadata"]),
            protocol_magic=parameters["protocol_magic"],
            network_id=parameters["network_id"],
        )
        assert response.tx_hash.hex() == result["tx_hash"]
        assert response.serialized_tx.hex() == result["serialized_tx"]
def test_cardano_sign_tx_validation(
    client, protocol_magic, inputs, outputs, transactions, expected_error_message
):
    inputs = [cardano.create_input(i) for i in inputs]
    outputs = [cardano.create_output(o) for o in outputs]

    expected_responses = [
        messages.CardanoTxRequest(tx_index=i) for i in range(len(transactions))
    ]
    expected_responses += [messages.Failure()]

    with client:
        client.set_expected_responses(expected_responses)

        with pytest.raises(TrezorFailure) as exc:
            cardano.sign_tx(client, inputs, outputs, transactions, protocol_magic)

        assert exc.value.args[1] == expected_error_message
def test_cardano_sign_tx(
    client,
    protocol_magic,
    network_id,
    inputs,
    outputs,
    fee,
    ttl,
    input_flow_sequences,
    tx_hash,
    serialized_tx,
):
    inputs = [cardano.create_input(i) for i in inputs]
    outputs = [cardano.create_output(o) for o in outputs]

    expected_responses = [
        messages.ButtonRequest(code=messages.ButtonRequestType.Other)
        for i in range(len(input_flow_sequences))
    ]
    expected_responses.append(messages.CardanoSignedTx())

    def input_flow():
        for sequence in input_flow_sequences:
            yield
            for action in sequence:
                if action == InputAction.SWIPE:
                    client.debug.swipe_up()
                elif action == InputAction.YES:
                    client.debug.press_yes()
                else:
                    raise ValueError("Invalid input action")

    with client:
        client.set_expected_responses(expected_responses)
        client.set_input_flow(input_flow)
        response = cardano.sign_tx(
            client, inputs, outputs, fee, ttl, protocol_magic, network_id
        )
        assert response.tx_hash.hex() == tx_hash
        assert response.serialized_tx.hex() == serialized_tx
def test_cardano_sign_tx_validation(
    client,
    protocol_magic,
    network_id,
    inputs,
    outputs,
    fee,
    ttl,
    expected_error_message,
):
    inputs = [cardano.create_input(i) for i in inputs]
    outputs = [cardano.create_output(o) for o in outputs]

    expected_responses = [messages.Failure()]

    with client:
        client.set_expected_responses(expected_responses)

        with pytest.raises(TrezorFailure, match=expected_error_message):
            cardano.sign_tx(
                client, inputs, outputs, fee, ttl, protocol_magic, network_id
            )
예제 #13
0
def test_cardano_sign_tx(client, parameters, result):
    inputs = [cardano.create_input(i) for i in parameters["inputs"]]
    outputs = [cardano.create_output(o) for o in parameters["outputs"]]
    certificates = [
        cardano.create_certificate(c) for c in parameters["certificates"]
    ]
    withdrawals = [
        cardano.create_withdrawal(w) for w in parameters["withdrawals"]
    ]

    input_flow = parameters.get("input_flow", ())

    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)

    with client:
        client.set_input_flow(_to_device_actions(client, input_flow))

        response = cardano.sign_tx(
            client=client,
            inputs=inputs,
            outputs=outputs,
            fee=parameters["fee"],
            ttl=parameters.get("ttl"),
            validity_interval_start=parameters.get("validity_interval_start"),
            certificates=certificates,
            withdrawals=withdrawals,
            metadata=bytes.fromhex(parameters["metadata"]),
            protocol_magic=parameters["protocol_magic"],
            network_id=parameters["network_id"],
        )
        assert response.tx_hash.hex() == result["tx_hash"]
        assert response.serialized_tx.hex() == result["serialized_tx"]
예제 #14
0
def test_cardano_sign_tx_validation(
    client,
    protocol_magic,
    network_id,
    inputs,
    outputs,
    fee,
    ttl,
    certificates,
    withdrawals,
    metadata,
    expected_error_message,
):
    inputs = [cardano.create_input(i) for i in inputs]
    outputs = [cardano.create_output(o) for o in outputs]
    certificates = [cardano.create_certificate(c) for c in certificates]
    withdrawals = [cardano.create_withdrawal(w) for w in withdrawals]

    expected_responses = [messages.Failure()]

    with client:
        client.set_expected_responses(expected_responses)

        with pytest.raises(TrezorFailure, match=expected_error_message):
            cardano.sign_tx(
                client=client,
                inputs=inputs,
                outputs=outputs,
                fee=fee,
                ttl=ttl,
                certificates=certificates,
                withdrawals=withdrawals,
                metadata=bytes.fromhex(metadata),
                protocol_magic=protocol_magic,
                network_id=network_id,
            )
예제 #15
0
    def test_cardano_sign_tx_mainnet(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=2,
            ))
        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"82839f8200d81858248258201af8fa0b754ff99253d983894e63a2b09cbb56c833ba18c3384210163f63dcfc00ff9f8282d818582183581c9e1c71de652ec8b85fec296f0685ca3988781c94a2e1a5d89d92f45fa0001a0d0c25611a002dd2e8ffa0818200d818588582584089053545a6c254b0d9b1464e48d2b5fcf91d4e25c128afb1fcfc61d0843338ea26308151516f3b0e02bb1638142747863c520273ce9bd3e5cd91e1d46fe2a6355840312c01c27317415b0b8acc86aa789da877fe7e15c65b7ea4c4565d8739117f5f6d9d38bf5d058f7be809b2b9b06c1d79fc6b20f9a4d76d8c89bae333edf5680c"
        )
예제 #16
0
    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 = bytes.fromhex(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 (
            response.tx_hash.hex() ==
            "799c65e8a2c0b1dc4232611728c09d3f3eb0d811c077f8e9798f84605ef1b23d")
        assert (
            response.tx_body.hex() ==
            "82839f8200d81858248258201af8fa0b754ff99253d983894e63a2b09cbb56c833ba18c3384210163f63dcfc00ff9f8282d818582183581c9e1c71de652ec8b85fec296f0685ca3988781c94a2e1a5d89d92f45fa0001a0d0c25611a002dd2e8ffa0818200d818588582584089053545a6c254b0d9b1464e48d2b5fcf91d4e25c128afb1fcfc61d0843338ea26308151516f3b0e02bb1638142747863c520273ce9bd3e5cd91e1d46fe2a63558403594ee7e2bfe4c84f886a8336cecb7c42983ce9a057345ebb6294a436087d8db93ca78cf514c7c48edff4c8435f690a5817951e2b55d2db729875ee7cc0f7d08"
        )