diff --git a/src_features/signMessageEIP712_common/common_712.c b/src_features/signMessageEIP712_common/common_712.c index abc1fd5..084a528 100644 --- a/src_features/signMessageEIP712_common/common_712.c +++ b/src_features/signMessageEIP712_common/common_712.c @@ -7,7 +7,7 @@ static const uint8_t EIP_712_MAGIC[] = {0x19, 0x01}; -unsigned int ui_712_approve_cb() { +unsigned int ui_712_approve_cb(void) { uint8_t privateKeyData[INT256_LENGTH]; uint8_t hash[INT256_LENGTH]; uint8_t signature[100]; @@ -75,7 +75,7 @@ unsigned int ui_712_approve_cb() { return 0; // do not redraw the widget } -unsigned int ui_712_reject_cb() { +unsigned int ui_712_reject_cb(void) { reset_app_context(); G_io_apdu_buffer[0] = 0x69; G_io_apdu_buffer[1] = 0x85; diff --git a/src_nbgl/ui_712_common.c b/src_nbgl/ui_712_common.c deleted file mode 100644 index a1b13e3..0000000 --- a/src_nbgl/ui_712_common.c +++ /dev/null @@ -1,48 +0,0 @@ -#include "ui_nbgl.h" -#include "ui_712_common.h" -#include "common_712.h" - -static void (*g_resume_func)(void) = NULL; - -void nbgl_712_review_approve(void) { - ui_712_approve_cb(NULL); -} - -void nbgl_712_review_reject(void) { - ui_712_reject_cb(NULL); -} - -void nbgl_712_confirm_rejection_cb(bool confirm) { - if (confirm) { - nbgl_useCaseStatus("Message signing\ncancelled", false, nbgl_712_review_reject); - } else { - (*g_resume_func)(); - } -} - -void nbgl_712_confirm_rejection(void) { - nbgl_useCaseChoice(&C_warning64px, - "Reject message?", - NULL, - "Yes, reject", - "Go back to message", - nbgl_712_confirm_rejection_cb); -} - -void nbgl_712_review_choice(bool confirm) { - if (confirm) { - nbgl_useCaseStatus("MESSAGE\nSIGNED", true, nbgl_712_review_approve); - } else { - nbgl_712_confirm_rejection(); - } -} - -void nbgl_712_start(void (*resume_func)(void), const char *title) { - g_resume_func = resume_func; - nbgl_useCaseReviewStart(&C_Message_64px, - title, - NULL, - "Reject", - resume_func, - nbgl_712_confirm_rejection); -} diff --git a/src_nbgl/ui_712_common.h b/src_nbgl/ui_712_common.h deleted file mode 100644 index ab8bd4f..0000000 --- a/src_nbgl/ui_712_common.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef UI_712_COMMON_H_ -#define UI_712_COMMON_H_ - -#include - -void nbgl_712_approve(void); -void nbgl_712_reject(void); -void nbgl_712_confirm_rejection_cb(bool confirm); -void nbgl_712_confirm_rejection(void); -void nbgl_712_review_choice(bool confirm); -void nbgl_712_start(void (*resume_func)(void), const char *title); - -#endif // UI_712_COMMON_H_ diff --git a/src_nbgl/ui_message_signing.c b/src_nbgl/ui_message_signing.c new file mode 100644 index 0000000..609dd81 --- /dev/null +++ b/src_nbgl/ui_message_signing.c @@ -0,0 +1,57 @@ +#include "ui_nbgl.h" +#include "common_712.h" +#include "ui_message_signing.h" +#include "glyphs.h" + +static void (*g_resume_func)(void) = NULL; +static void (*g_approved_func)(void) = NULL; +static void (*g_rejected_func)(void) = NULL; + +static void ui_message_rejection_handler(bool confirm) { + if (confirm) { + nbgl_useCaseStatus("Message signing\ncancelled", false, g_rejected_func); + } else { + (*g_resume_func)(); + } +} + +static void ui_message_confirm_rejection(void) { + nbgl_useCaseChoice(&C_warning64px, + "Reject message?", + NULL, + "Yes, reject", + "Go back to message", + ui_message_rejection_handler); +} + +void ui_message_review_choice(bool confirm) { + if (confirm) { + nbgl_useCaseStatus("MESSAGE\nSIGNED", true, g_approved_func); + } else { + ui_message_confirm_rejection(); + } +} + +void ui_message_start(const char *title, + void (*start_func)(void), + void (*resume_func)(void), + void (*approved_func)(void), + void (*rejected_func)(void)) { + g_resume_func = resume_func; + g_approved_func = approved_func; + g_rejected_func = rejected_func; + nbgl_useCaseReviewStart(&C_Message_64px, + title, + NULL, + "Reject", + start_func, + ui_message_confirm_rejection); +} + +void ui_message_712_approved(void) { + ui_712_approve_cb(); +} + +void ui_message_712_rejected(void) { + ui_712_reject_cb(); +} diff --git a/src_nbgl/ui_message_signing.h b/src_nbgl/ui_message_signing.h new file mode 100644 index 0000000..03f6367 --- /dev/null +++ b/src_nbgl/ui_message_signing.h @@ -0,0 +1,16 @@ +#ifndef UI_MESSAGE_SIGNING_H_ +#define UI_MESSAGE_SIGNING_H_ + +#include + +void ui_message_review_choice(bool confirm); +void ui_message_start(const char *title, + void (*start_func)(void), + void (*resume_func)(void), + void (*approved_func)(void), + void (*rejected_func)(void)); + +void ui_message_712_approved(void); +void ui_message_712_rejected(void); + +#endif // UI_MESSAGE_SIGNING_H_ diff --git a/src_nbgl/ui_sign_712.c b/src_nbgl/ui_sign_712.c index bfe53fd..fa63a34 100644 --- a/src_nbgl/ui_sign_712.c +++ b/src_nbgl/ui_sign_712.c @@ -6,7 +6,7 @@ #include "common_712.h" #include "nbgl_use_case.h" #include "network.h" -#include "ui_712_common.h" +#include "ui_message_signing.h" static nbgl_layoutTagValue_t pair; @@ -62,11 +62,15 @@ static bool display_review_page(uint8_t page, nbgl_pageContent_t *content) { } static void handle_display(nbgl_navCallback_t cb) { - nbgl_useCaseRegularReview(0, 0, "Reject", NULL, cb, nbgl_712_review_choice); + nbgl_useCaseRegularReview(0, 0, "Reject", NULL, cb, ui_message_review_choice); } void ui_712_start(void) { - nbgl_712_start(&ui_712_switch_to_message, "Review typed message"); + ui_message_start("Review typed message", + NULL, + &ui_712_switch_to_message, + &ui_message_712_approved, + &ui_message_712_rejected); } void ui_712_switch_to_message(void) { diff --git a/src_nbgl/ui_sign_712_v0.c b/src_nbgl/ui_sign_712_v0.c index e5dc92e..7d6eddb 100644 --- a/src_nbgl/ui_sign_712_v0.c +++ b/src_nbgl/ui_sign_712_v0.c @@ -3,7 +3,7 @@ #include "common_712.h" #include "network.h" #include "ethUtils.h" -#include "ui_712_common.h" +#include "ui_message_signing.h" static nbgl_layoutTagValue_t pairs[2]; @@ -43,9 +43,13 @@ static bool display_review_page(uint8_t page, nbgl_pageContent_t *content) { } static void start_review(void) { - nbgl_useCaseRegularReview(0, 2, "Reject", NULL, display_review_page, nbgl_712_review_choice); + nbgl_useCaseRegularReview(0, 2, "Reject", NULL, display_review_page, ui_message_review_choice); } void ui_sign_712_v0(void) { - nbgl_712_start(&start_review, "Sign typed message"); + ui_message_start("Sign typed message", + NULL, + &start_review, + &ui_message_712_approved, + &ui_message_712_rejected); } diff --git a/src_nbgl/ui_sign_message.c b/src_nbgl/ui_sign_message.c index 5ea2c89..766d9c7 100644 --- a/src_nbgl/ui_sign_message.c +++ b/src_nbgl/ui_sign_message.c @@ -6,17 +6,17 @@ #include "glyphs.h" #include "nbgl_use_case.h" #include "common_ui.h" +#include "ui_message_signing.h" +#include "ui_signing.h" typedef enum { - UI_191_NBGL_START_REVIEW_DISPLAYED = 0, - UI_191_NBGL_GO_TO_NEXT_CONTENT, - UI_191_NBGL_BACK_FROM_REJECT_CANCEL, - UI_191_NBGL_GO_TO_SIGN, - UI_191_NBGL_SIGN_DISPLAYED, -} e_ui_nbgl_191_state; + UI_191_ACTION_IDLE = 0, + UI_191_ACTION_ADVANCE_IN_MESSAGE, + UI_191_ACTION_GO_TO_SIGN +} e_ui_191_action; + +static e_ui_191_action g_action; -static e_ui_nbgl_191_state state; -static e_ui_nbgl_191_state state_before_reject_cancel; static bool skip_message; static nbgl_layoutTagValue_t pair; @@ -37,7 +37,7 @@ static bool display_message(nbgl_pageContent_t *content) { uint16_t len = 0; bool reached; - if (state != UI_191_NBGL_BACK_FROM_REJECT_CANCEL) { + if (g_action == UI_191_ACTION_ADVANCE_IN_MESSAGE) { strncpy(staxSharedBuffer + eip191MessageIdx, strings.tmp.tmp + stringsTmpTmpIdx, SHARED_BUFFER_SIZE - eip191MessageIdx); @@ -55,7 +55,7 @@ static bool display_message(nbgl_pageContent_t *content) { stringsTmpTmpIdx = 0; question_switcher(); - if (state != UI_191_NBGL_GO_TO_SIGN) { + if (g_action != UI_191_ACTION_GO_TO_SIGN) { return false; } } else if (reached || eip191MessageIdx == SHARED_BUFFER_SIZE) { @@ -72,13 +72,7 @@ static bool display_message(nbgl_pageContent_t *content) { content->tagValueList.nbMaxLinesForValue = 9; content->tagValueList.wrapping = false; - if (state == UI_191_NBGL_BACK_FROM_REJECT_CANCEL) { - // We come back from Reject screen. - // The previously displayed content must be redisplayed. - // Do not call question_switcher() to avoid replacing - // string.tmp.tmp content. - state = state_before_reject_cancel; - } else if (stringsTmpTmpIdx >= strlen(strings.tmp.tmp)) { + if ((g_action != UI_191_ACTION_IDLE) && (stringsTmpTmpIdx >= strlen(strings.tmp.tmp))) { // Fetch the next content to display into strings.tmp.tmp buffer. stringsTmpTmpIdx = 0; question_switcher(); @@ -88,9 +82,9 @@ static bool display_message(nbgl_pageContent_t *content) { static void display_sign(nbgl_pageContent_t *content) { content->type = INFO_LONG_PRESS, content->infoLongPress.icon = &C_Message_64px; - content->infoLongPress.text = "Sign Message?"; + content->infoLongPress.text = "Sign Message"; content->infoLongPress.longPressText = "Hold to sign"; - state = UI_191_NBGL_SIGN_DISPLAYED; + g_position = UI_SIGNING_POSITION_SIGN; } static bool nav_callback(uint8_t page, nbgl_pageContent_t *content) { @@ -100,7 +94,7 @@ static bool nav_callback(uint8_t page, nbgl_pageContent_t *content) { skip_message = true; skip_rest_of_message(); } - if ((state != UI_191_NBGL_GO_TO_SIGN) && (state != UI_191_NBGL_SIGN_DISPLAYED)) { + if ((g_action != UI_191_ACTION_GO_TO_SIGN) && (g_position != UI_SIGNING_POSITION_SIGN)) { if (skip_message) { // do not refresh when this callback triggers after user validation ret = false; @@ -114,68 +108,48 @@ static bool nav_callback(uint8_t page, nbgl_pageContent_t *content) { return ret; } -static void choice_callback(bool confirm) { - if (confirm) { - nbgl_useCaseStatus("MESSAGE\nSIGNED", true, sign_message); - sign_message(); - } -} - static void continue_review(void) { - nbgl_useCaseForwardOnlyReview("Reject", NULL, nav_callback, choice_callback); + nbgl_useCaseForwardOnlyReview("Reject", NULL, nav_callback, ui_message_review_choice); } -static void confirm_transaction_rejection_choice(bool confirm) { - if (confirm) { - reject_message(); +static void resume_message(void) { + e_ui_191_action action_bak = g_action; + + if (g_position == UI_SIGNING_POSITION_START) { + ui_191_start(); } else { - // Go to previous screen accordingly - if (state == UI_191_NBGL_START_REVIEW_DISPLAYED) { - ui_191_start(); - } else { - if (state != UI_191_NBGL_SIGN_DISPLAYED) { - state_before_reject_cancel = state; - state = UI_191_NBGL_BACK_FROM_REJECT_CANCEL; - } - continue_review(); - } + g_action = UI_191_ACTION_IDLE; + continue_review(); + g_action = action_bak; } } -static void confirm_transaction_rejection() { - nbgl_useCaseChoice(&C_warning64px, - "Reject message?", - NULL, - "Yes, Reject", - "Go back to message", - confirm_transaction_rejection_choice); -} - void ui_191_start(void) { - state = UI_191_NBGL_START_REVIEW_DISPLAYED; + g_position = UI_SIGNING_POSITION_START; + skip_message = false; eip191MessageIdx = 0; stringsTmpTmpIdx = 0; - nbgl_useCaseReviewStart(&C_Message_64px, - "Review message", - NULL, - "Reject", - continue_review, - confirm_transaction_rejection); + ui_message_start("Review message", + &ui_191_switch_to_message, + &resume_message, + &sign_message, + &reject_message); } void ui_191_switch_to_message(void) { + g_position = UI_SIGNING_POSITION_REVIEW; + g_action = UI_191_ACTION_ADVANCE_IN_MESSAGE; // No question mechanism on Stax: // Message is already displayed - state = UI_191_NBGL_GO_TO_NEXT_CONTENT; continue_review(); } void ui_191_switch_to_sign(void) { + g_action = UI_191_ACTION_GO_TO_SIGN; // Next nav_callback callback must display // the hold to approve screen - state = UI_191_NBGL_GO_TO_SIGN; if (skip_message) { continue_review(); // to force screen refresh } diff --git a/src_nbgl/ui_signing.c b/src_nbgl/ui_signing.c new file mode 100644 index 0000000..2becdf0 --- /dev/null +++ b/src_nbgl/ui_signing.c @@ -0,0 +1,3 @@ +#include "ui_signing.h" + +e_ui_signing_position g_position; diff --git a/src_nbgl/ui_signing.h b/src_nbgl/ui_signing.h new file mode 100644 index 0000000..e9248ac --- /dev/null +++ b/src_nbgl/ui_signing.h @@ -0,0 +1,12 @@ +#ifndef UI_SIGNING_H_ +#define UI_SIGNING_H_ + +typedef enum { + UI_SIGNING_POSITION_START = 0, + UI_SIGNING_POSITION_REVIEW, + UI_SIGNING_POSITION_SIGN +} e_ui_signing_position; + +extern e_ui_signing_position g_position; + +#endif // UI_SIGNING_H_