Skip to content

Commit

Permalink
Merge pull request #424 from Cypherock/fix/delete-after-wallet-create…
Browse files Browse the repository at this point in the history
…-fail/PRF-6377

fix(core): Handle wallet deletion failure for creation failure
  • Loading branch information
amanCypherock authored Oct 20, 2023
2 parents 456058b + 9c5cec4 commit dccaa58
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 12 deletions.
14 changes: 14 additions & 0 deletions common/core/core_error.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,20 @@ void handle_core_errors() {
return;
}

bool show_errors_if_p0_not_occured() {
/* Check P0 events */
p0_evt_t evt = {0};
p0_get_evt(&evt);

// Display errors if there's no p0 event, else it'll be handled by
// handle_core_errors
if (false == evt.flag) {
display_core_error();
}

return !evt.flag;
}

void ignore_p0_event() {
p0_reset_evt();
}
Expand Down
8 changes: 8 additions & 0 deletions common/core/core_error.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@
*/
void mark_core_error_screen(const char *error_msg, bool ring_buzzer);

/**
* @brief The function checks for a p0 event and displays core errors if it has
* not occurred.
*
* @return returns true p0 event flag is false and vice versa
*/
bool show_errors_if_p0_not_occured();

/**
* @brief This function clears the message buffer of the core error screen
*
Expand Down
36 changes: 34 additions & 2 deletions src/menu/create_wallet_menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,14 @@
#include "create_wallet_menu.h"

#include "constant_texts.h"
#include "core_error.h"
#include "core_error_priv.h"
#include "create_new_wallet_flow.h"
#include "delete_wallet_flow.h"
#include "menu_priv.h"
#include "restore_seed_phrase_flow.h"
#include "ui_screens.h"
#include "ui_state_machine.h"

/*****************************************************************************
* EXTERN VARIABLES
Expand Down Expand Up @@ -124,6 +127,15 @@ static void create_wallet_menu_handler(engine_ctx_t *ctx,
static void ignore_p0_handler(engine_ctx_t *ctx,
p0_evt_t p0_evt,
const void *data_ptr);

/**
* @brief This function handles the failure of wallet creation by deleting the
* wallet if it was written on flash and cards.
*
* @param flash_wallet pointer to the ram instance of the wallet that failed
* during creation.
*/
void handle_wallet_creation_failure(Flash_Wallet *falsh_wallet);
/*****************************************************************************
* STATIC VARIABLES
*****************************************************************************/
Expand Down Expand Up @@ -161,14 +173,16 @@ static void create_wallet_menu_initialize(engine_ctx_t *ctx,
static void create_wallet_menu_handler(engine_ctx_t *ctx,
ui_event_t ui_event,
const void *data_ptr) {
Flash_Wallet *flash_wallet = NULL; // default value

if (UI_EVENT_LIST_CHOICE == ui_event.event_type) {
switch (ui_event.list_selection) {
case GENERATE_NEW_WALLET: {
create_new_wallet_flow();
flash_wallet = create_new_wallet_flow();
break;
}
case RESTORE_FROM_SEED: {
restore_seed_phrase_flow();
flash_wallet = restore_seed_phrase_flow();
break;
}
default: {
Expand All @@ -179,13 +193,31 @@ static void create_wallet_menu_handler(engine_ctx_t *ctx,
// UI_EVENT_LIST_REJECTION handled below already
}

if (NULL != flash_wallet && ((VALID_WALLET != flash_wallet->state) ||
(0x0F != flash_wallet->cards_states))) {
handle_wallet_creation_failure(flash_wallet);
}

/* Return to the previous menu irrespective if UI_EVENT_REJECTION was
* detected, or a create wallet flow was executed */
engine_delete_current_flow_step(ctx);

return;
}

void handle_wallet_creation_failure(Flash_Wallet *flash_wallet) {
if (!show_errors_if_p0_not_occured()) {
return;
}

message_scr_init(ui_text_creation_failed_delete_wallet);
if (0 != get_state_on_confirm_scr(0, 1, 2)) {
return;
}

delete_wallet_flow(flash_wallet);
}

/*****************************************************************************
* GLOBAL FUNCTIONS
*****************************************************************************/
Expand Down
8 changes: 5 additions & 3 deletions src/wallet/create_new_wallet_flow.c
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,6 @@ new_wallet_state_e new_wallet_state_handler(new_wallet_state_e current_state) {
}

case COMPLETED_WITH_ERRORS: {
mark_core_error_screen(ui_text_creation_failed_delete_wallet, false);
next_state = EXIT;
break;
}
Expand All @@ -415,7 +414,7 @@ new_wallet_state_e new_wallet_state_handler(new_wallet_state_e current_state) {
/*****************************************************************************
* GLOBAL FUNCTIONS
*****************************************************************************/
void create_new_wallet_flow(void) {
Flash_Wallet *create_new_wallet_flow() {
new_wallet_state_e current_state = NAME_INPUT;

// Ensure that atleast 4 cards are paired
Expand All @@ -442,6 +441,9 @@ void create_new_wallet_flow(void) {
current_state = next_state;
}

Flash_Wallet *flash_wallet = NULL;
get_flash_wallet_by_name((const char *)wallet.wallet_name, &flash_wallet);

clear_wallet_data();
return;
return flash_wallet;
}
7 changes: 5 additions & 2 deletions src/wallet/create_new_wallet_flow.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/*****************************************************************************
* INCLUDES
*****************************************************************************/

#include "flash_struct.h"
/*****************************************************************************
* MACROS AND DEFINES
*****************************************************************************/
Expand All @@ -34,7 +34,10 @@
* @details This function generate random mnemonics, takes user inputs for
* wallet configuration, writes the wallet shares on the X1 vault flash and X1
* cards and verifies each share
*
* @return NULL if wallet was not created, else pointer to the ram instance of
* wallet created.
*/
void create_new_wallet_flow(void);
Flash_Wallet *create_new_wallet_flow();

#endif /* CREATE_NEW_WALLET_FLOW_H */
8 changes: 5 additions & 3 deletions src/wallet/restore_seed_phrase_flow.c
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,6 @@ restore_wallet_state_e restore_wallet_state_handler(
}

case COMPLETED_WITH_ERRORS: {
mark_core_error_screen(ui_text_creation_failed_delete_wallet, false);
next_state = EXIT;
break;
}
Expand All @@ -528,7 +527,7 @@ restore_wallet_state_e restore_wallet_state_handler(
return next_state;
}

void restore_seed_phrase_flow(void) {
Flash_Wallet *restore_seed_phrase_flow() {
restore_wallet_state_e current_state = NAME_INPUT;

// Ensure that atleast 4 cards are paired
Expand Down Expand Up @@ -556,6 +555,9 @@ void restore_seed_phrase_flow(void) {
current_state = next_state;
}

Flash_Wallet *flash_wallet = NULL;
get_flash_wallet_by_name((const char *)wallet.wallet_name, &flash_wallet);

clear_wallet_data();
return;
return flash_wallet;
}
7 changes: 5 additions & 2 deletions src/wallet/restore_seed_phrase_flow.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/*****************************************************************************
* INCLUDES
*****************************************************************************/

#include "flash_struct.h"
/*****************************************************************************
* MACROS AND DEFINES
*****************************************************************************/
Expand All @@ -34,7 +34,10 @@
* @details This function inputs seed phrase from user, takes user inputs for
* wallet configuration, writes the wallet shares on the X1 vault flash and X1
* cards and verifies each share
*
* @return NULL if wallet was not created, else pointer to the ram instance of
* wallet created.
*/
void restore_seed_phrase_flow(void);
Flash_Wallet *restore_seed_phrase_flow();

#endif /* RESTORE_SEED_PHRASE_FLOW_H */

0 comments on commit dccaa58

Please sign in to comment.