4444#include "get_merkleized_map.h"
4545#include "get_merkleized_map_value.h"
4646#include "get_preimage.h"
47- #include "handle_swap_sign_transaction.h"
4847#include "handlers.h"
4948#include "menu.h"
5049#include "merkle.h"
@@ -1029,22 +1028,19 @@ execute_swap_checks(dispatcher_context_t *dc, sign_psbt_state_t *st) {
10291028 if (!st -> is_wallet_default ) {
10301029 PRINTF ("Must be a default wallet policy for swap feature\n" );
10311030 SEND_SW_EC (dc , SW_FAIL_SWAP , EC_SWAP_ERROR_WRONG_METHOD_NONDEFAULT_POLICY );
1032- finalize_exchange_sign_transaction (false);
10331031 }
10341032
10351033 // No external inputs allowed
10361034 if (st -> n_external_inputs > 0 ) {
10371035 PRINTF ("External inputs not allowed in swap transactions\n" );
10381036 SEND_SW_EC (dc , SW_FAIL_SWAP , EC_SWAP_ERROR_WRONG_METHOD_EXTERNAL_INPUTS );
1039- finalize_exchange_sign_transaction (false);
10401037 }
10411038
10421039 if (st -> warnings .missing_nonwitnessutxo || st -> warnings .non_default_sighash ) {
10431040 // Do not allow transactions with missing non-witness utxos or non-default sighash flags
10441041 PRINTF (
10451042 "Missing non-witness utxo or non-default sighash flags are not allowed during swaps\n" );
10461043 SEND_SW_EC (dc , SW_FAIL_SWAP , EC_SWAP_ERROR_WRONG_METHOD_MISSING_NONWITNESSUTXO );
1047- finalize_exchange_sign_transaction (false);
10481044 }
10491045
10501046 uint64_t fee = st -> inputs_total_amount - st -> outputs .total_amount ;
@@ -1060,7 +1056,6 @@ execute_swap_checks(dispatcher_context_t *dc, sign_psbt_state_t *st) {
10601056 if (st -> n_external_outputs != 1 ) {
10611057 PRINTF ("Standard swap transaction must have exactly 1 external output\n" );
10621058 SEND_SW_EC (dc , SW_FAIL_SWAP , EC_SWAP_ERROR_WRONG_METHOD_WRONG_N_OF_OUTPUTS );
1063- finalize_exchange_sign_transaction (false);
10641059 }
10651060 } else if (G_swap_state .mode == SWAP_MODE_CROSSCHAIN ) {
10661061 // There must be exactly 2 external outputs; the first is the OP_RETURN
@@ -1070,7 +1065,6 @@ execute_swap_checks(dispatcher_context_t *dc, sign_psbt_state_t *st) {
10701065 if (st -> n_external_outputs != 2 ) {
10711066 PRINTF ("Cross-chain swap transaction must have exactly 2 external outputs\n" );
10721067 SEND_SW_EC (dc , SW_FAIL_SWAP , EC_SWAP_ERROR_WRONG_METHOD_WRONG_N_OF_OUTPUTS );
1073- finalize_exchange_sign_transaction (false);
10741068 }
10751069
10761070 uint8_t * opreturn_script = st -> outputs .output_scripts [0 ];
@@ -1081,12 +1075,11 @@ execute_swap_checks(dispatcher_context_t *dc, sign_psbt_state_t *st) {
10811075 SEND_SW_EC (dc ,
10821076 SW_FAIL_SWAP ,
10831077 EC_SWAP_ERROR_CROSSCHAIN_WRONG_METHOD_INVALID_FIRST_OUTPUT );
1084- finalize_exchange_sign_transaction (false);
10851078 }
10861079
10871080 uint8_t second_byte = opreturn_script [1 ];
1088- size_t push_opcode_size ; // the length of the push opcode (1 or 2 bytes)
1089- size_t data_size ; // the length of the actual data embedded in the OP_RETURN output
1081+ size_t push_opcode_size = 0 ; // the length of the push opcode (1 or 2 bytes)
1082+ size_t data_size = 0 ; // the length of the actual data embedded in the OP_RETURN output
10901083 if (2 <= second_byte && second_byte <= 75 ) {
10911084 push_opcode_size = 1 ;
10921085 data_size = second_byte ;
@@ -1100,21 +1093,18 @@ execute_swap_checks(dispatcher_context_t *dc, sign_psbt_state_t *st) {
11001093 // so we don't bother parsing.
11011094 PRINTF ("Unsupported or invalid OP_RETURN Script in cross-chain swap\n" );
11021095 SEND_SW_EC (dc , SW_FAIL_SWAP , EC_SWAP_ERROR_CROSSCHAIN_WRONG_METHOD );
1103- finalize_exchange_sign_transaction (false);
11041096 }
11051097
11061098 // Make sure there is a singla data push
11071099 if (opreturn_script_len != 1 + push_opcode_size + data_size ) {
11081100 PRINTF ("Invalid OP_RETURN Script length in cross-chain swap\n" );
11091101 SEND_SW_EC (dc , SW_FAIL_SWAP , EC_SWAP_ERROR_CROSSCHAIN_WRONG_METHOD );
1110- finalize_exchange_sign_transaction (false);
11111102 }
11121103
11131104 // Make sure the output's value is 0
11141105 if (opreturn_amount != 0 ) {
11151106 PRINTF ("OP_RETURN with non-zero value during cross-chain swap\n" );
11161107 SEND_SW_EC (dc , SW_FAIL_SWAP , EC_SWAP_ERROR_CROSSCHAIN_WRONG_METHOD_NONZERO_AMOUNT );
1117- finalize_exchange_sign_transaction (false);
11181108 }
11191109
11201110 // verify the hash in the data payload is the expected one
@@ -1125,18 +1115,15 @@ execute_swap_checks(dispatcher_context_t *dc, sign_psbt_state_t *st) {
11251115 sizeof (expected_payin_hash )) != 0 ) {
11261116 PRINTF ("Mismatching payin hash in cross-chain swap\n" );
11271117 SEND_SW_EC (dc , SW_FAIL_SWAP , EC_SWAP_ERROR_CROSSCHAIN_WRONG_HASH );
1128- finalize_exchange_sign_transaction (false);
11291118 }
11301119 } else if (G_swap_state .mode == SWAP_MODE_ERROR ) {
11311120 // an error was detected in handle_swap_sign_transaction.c::copy_transaction_parameters
11321121 // special case only to improve error reporting in debug mode
11331122 PRINTF ("Invalid parameters for swap feature\n" );
11341123 SEND_SW_EC (dc , SW_FAIL_SWAP , EC_SWAP_ERROR_GENERIC_COPY_TRANSACTION_PARAMETERS_FAILED );
1135- finalize_exchange_sign_transaction (false);
11361124 } else {
11371125 PRINTF ("Unknown swap mode: %d\n" , G_swap_state .mode );
11381126 SEND_SW_EC (dc , SW_FAIL_SWAP , EC_SWAP_ERROR_GENERIC_UNKNOWN_MODE );
1139- finalize_exchange_sign_transaction (false);
11401127 }
11411128
11421129 LEDGER_ASSERT (0 <= swap_dest_idx && swap_dest_idx < N_CACHED_EXTERNAL_OUTPUTS ,
@@ -1146,14 +1133,12 @@ execute_swap_checks(dispatcher_context_t *dc, sign_psbt_state_t *st) {
11461133 if (fee != G_swap_state .fees ) {
11471134 PRINTF ("Mismatching fee for swap\n" );
11481135 SEND_SW_EC (dc , SW_FAIL_SWAP , EC_SWAP_ERROR_WRONG_FEES );
1149- finalize_exchange_sign_transaction (false);
11501136 }
11511137
11521138 uint64_t spent_amount = st -> outputs .total_amount - st -> outputs .change_total_amount ;
11531139 if (spent_amount != G_swap_state .amount ) {
11541140 PRINTF ("Mismatching spent amount for swap\n" );
11551141 SEND_SW_EC (dc , SW_FAIL_SWAP , EC_SWAP_ERROR_WRONG_AMOUNT );
1156- finalize_exchange_sign_transaction (false);
11571142 }
11581143
11591144 // Compute this output's address
@@ -1164,7 +1149,6 @@ execute_swap_checks(dispatcher_context_t *dc, sign_psbt_state_t *st) {
11641149 output_description )) {
11651150 PRINTF ("Invalid or unsupported script for external output\n" );
11661151 SEND_SW_EC (dc , SW_FAIL_SWAP , EC_SWAP_ERROR_WRONG_METHOD_WRONG_UNSUPPORTED_OUTPUT );
1167- finalize_exchange_sign_transaction (false);
11681152 }
11691153
11701154 char output_description_len = strlen (output_description );
@@ -1187,7 +1171,6 @@ execute_swap_checks(dispatcher_context_t *dc, sign_psbt_state_t *st) {
11871171 }
11881172 PRINTF ("\n" );
11891173 SEND_SW_EC (dc , SW_FAIL_SWAP , EC_SWAP_ERROR_WRONG_DESTINATION );
1190- finalize_exchange_sign_transaction (false);
11911174 }
11921175
11931176 return true;
@@ -2204,11 +2187,6 @@ void handler_sign_psbt(dispatcher_context_t *dc, uint8_t protocol_version) {
22042187 if (!sign_result ) {
22052188 return ;
22062189 }
2207-
2208- // Only if called from swap, the app should terminate after sending the response
2209- if (G_called_from_swap ) {
2210- G_swap_state .should_exit = true;
2211- }
22122190 }
22132191
22142192 // MuSig2: if there is an active session at the end of round 1, we move it to persistent
0 commit comments