From b76202bfb868857579f434171417ddca056d4201 Mon Sep 17 00:00:00 2001 From: Francois Beutin Date: Thu, 10 Oct 2024 18:16:29 +0200 Subject: [PATCH] Fix swap implementation --- app/src/apdu_handler.c | 18 +++++++++++++++--- app/src/swap/handle_sign_transaction.c | 6 ++++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/app/src/apdu_handler.c b/app/src/apdu_handler.c index a3804a2c..c67f329a 100644 --- a/app/src/apdu_handler.c +++ b/app/src/apdu_handler.c @@ -179,9 +179,21 @@ __Z_INLINE void handleSign(volatile uint32_t *flags, volatile uint32_t *tx, uint THROW(APDU_CODE_DATA_INVALID); } - view_review_init(tx_getItem, tx_getNumItems, app_sign_ed25519); - view_review_show(REVIEW_TXN); - *flags |= IO_ASYNCH_REPLY; +#ifdef HAVE_SWAP + if (G_swap_state.called_from_swap && G_swap_state.should_exit && error_msg == NULL) { + // Call app_sign_ed25519 without going through UI display, the UI validation was done in + // Exchange app already + app_sign_ed25519(); + // Go back to Exchange and report our success to display the modal + finalize_exchange_sign_transaction(true); + } else { +#endif + view_review_init(tx_getItem, tx_getNumItems, app_sign_ed25519); + view_review_show(REVIEW_TXN); + *flags |= IO_ASYNCH_REPLY; +#ifdef HAVE_SWAP + } +#endif } __Z_INLINE void handleSignRaw(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx) { diff --git a/app/src/swap/handle_sign_transaction.c b/app/src/swap/handle_sign_transaction.c index 00eeee44..9ce139b6 100644 --- a/app/src/swap/handle_sign_transaction.c +++ b/app/src/swap/handle_sign_transaction.c @@ -75,6 +75,7 @@ bool copy_transaction_parameters(create_transaction_parameters_t *sign_transacti return true; } +// Ensure the received transaction matches what was validated in the Exchange app UI parser_error_t check_swap_conditions(parser_tx_t *txObj) { parser_error_t err = parser_unexpected_error; if (txObj == NULL) { @@ -103,6 +104,7 @@ parser_error_t check_swap_conditions(parser_tx_t *txObj) { CHECK_ERROR(parser_getItem(txObj, &uiFields)); if (strncmp(valid_network, tmpValue, strlen(valid_network)) != 0) { ZEMU_LOGF(200, "Swap not enable on %s network.\n", tmpValue); + return parser_swap_tx_wrong_method; } // Check method. @@ -150,12 +152,12 @@ parser_error_t check_swap_conditions(parser_tx_t *txObj) { const size_t strLen = strlen(tmpValue); const size_t amountLen = strlen(tmpAmount); if (zxerr != zxerr_ok || strLen != amountLen || strncmp(tmpValue, tmpAmount, strLen) != 0) { - ZEMU_LOGF(200, "Wrong swap tx amount (%s, should be : %s).\n", tmp_str, tmpAmount); + ZEMU_LOGF(200, "Wrong swap tx amount (%s, should be : %s).\n", tmpValue, tmpAmount); return parser_swap_tx_wrong_amount; } ZEMU_LOGF(50, "Swap parameters verified by current tx\n"); - return err; + return parser_ok; } void __attribute__((noreturn)) finalize_exchange_sign_transaction(bool is_success) {