From 89714bba1ee8021ef393fcaa23bb7f868ca9a108 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Palmer?= Date: Thu, 7 Mar 2024 18:28:00 +0100 Subject: [PATCH] Doc: documents delegation files --- src/apdu_sign.c | 2 +- src/ui_delegation.h | 14 ++++- src/ui_delegation_bagl.c | 23 +------- src/ui_delegation_nbgl.c | 118 +++++++++++++++++++++------------------ 4 files changed, 80 insertions(+), 77 deletions(-) diff --git a/src/apdu_sign.c b/src/apdu_sign.c index b90ee744..c6700f95 100644 --- a/src/apdu_sign.c +++ b/src/apdu_sign.c @@ -120,7 +120,7 @@ size_t baking_sign_complete(bool const send_hash, volatile uint32_t *flags) { 0) { ui_callback_t const ok_c = send_hash ? sign_with_hash_ok : sign_without_hash_ok; - prompt_register_delegate(ok_c, sign_reject); + prompt_delegation(ok_c, sign_reject); *flags = IO_ASYNCH_REPLY; result = 0; } else { diff --git a/src/ui_delegation.h b/src/ui_delegation.h index a82a4e58..93aa161c 100644 --- a/src/ui_delegation.h +++ b/src/ui_delegation.h @@ -2,4 +2,16 @@ #include "types.h" -void prompt_register_delegate(ui_callback_t const ok_cb, ui_callback_t const cxl_cb); +/** + * @brief Draws delegation confirmation pages flow + * + * - Initial screen + * - Values: + * - Address + * - Fee + * - Confirmation screens + * + * @param ok_cb: accept callback + * @param cxl_cb: reject callback + */ +void prompt_delegation(ui_callback_t const ok_cb, ui_callback_t const cxl_cb); diff --git a/src/ui_delegation_bagl.c b/src/ui_delegation_bagl.c index 94ec7336..1e1f4366 100644 --- a/src/ui_delegation_bagl.c +++ b/src/ui_delegation_bagl.c @@ -1,37 +1,20 @@ #ifdef HAVE_BAGL -#include "apdu_sign.h" -#include "apdu.h" -#include "baking_auth.h" +#include "ui_delegation.h" + #include "globals.h" -#include "keys.h" -#include "memory.h" -#include "protocol.h" #include "to_string.h" #include "ui.h" -#include "cx.h" - -#include #define G global.apdu.u.sign -#define PARSE_ERROR() THROW(EXC_PARSE_ERROR) - -#define B2B_BLOCKBYTES 128 - -__attribute__((noreturn)) void prompt_register_delegate(ui_callback_t const ok_cb, - ui_callback_t const cxl_cb) { - if (!G.maybe_ops.is_valid) { - THROW(EXC_MEMORY_ERROR); - } - +void prompt_delegation(ui_callback_t const ok_cb, ui_callback_t const cxl_cb) { init_screen_stack(); push_ui_callback("Register", copy_string, "as delegate?"); push_ui_callback("Address", bip32_path_with_curve_to_pkh_string, &global.path_with_curve); push_ui_callback("Fee", microtez_to_string_indirect, &G.maybe_ops.v.total_fee); ux_confirm_screen(ok_cb, cxl_cb); - __builtin_unreachable(); } #endif // HAVE_BAGL diff --git a/src/ui_delegation_nbgl.c b/src/ui_delegation_nbgl.c index cde38397..cb2f9a40 100644 --- a/src/ui_delegation_nbgl.c +++ b/src/ui_delegation_nbgl.c @@ -1,50 +1,61 @@ #ifdef HAVE_NBGL -#include "nbgl_use_case.h" -#include "apdu_sign.h" -#include "apdu.h" -#include "baking_auth.h" +#include "ui_delegation.h" + #include "globals.h" -#include "keys.h" -#include "memory.h" -#include "protocol.h" #include "to_string.h" #include "ui.h" -#include "cx.h" -#include +#include "nbgl_use_case.h" #define G global.apdu.u.sign -#define MAX_LENGTH 100 +#define BUFFER_SIZE 100 #define NUMBER_VALUES 2u /// Number of values displayed -#define PARSE_ERROR() THROW(EXC_PARSE_ERROR) - +/** + * @brief This structure represents a context needed for delegation screens navigation + * + */ typedef struct { - ui_callback_t ok_cb; - ui_callback_t cxl_cb; - nbgl_layoutTagValue_t tagValuePair[NUMBER_VALUES]; + ui_callback_t ok_cb; /// accept callback + ui_callback_t cxl_cb; /// cancel callback + nbgl_layoutTagValue_t tagValues[NUMBER_VALUES]; nbgl_layoutTagValueList_t tagValueList; nbgl_pageInfoLongPress_t infoLongPress; - const char* confirmed_status; - const char* cancelled_status; - char buffer[NUMBER_VALUES][MAX_LENGTH]; -} TransactionContext_t; - -static TransactionContext_t transactionContext; - + char buffer[NUMBER_VALUES][BUFFER_SIZE]; /// values buffers; +} DelegationContext_t; + +/** + * @brief Current delegation context + * + */ +static DelegationContext_t delegation_context; + +/** + * @brief Callback called when delegation is rejected + * + */ static void cancel_callback(void) { - nbgl_useCaseStatus(transactionContext.cancelled_status, false, ui_initial_screen); - transactionContext.cxl_cb(); + nbgl_useCaseStatus("Delegate registration\ncancelled", false, ui_initial_screen); + delegation_context.cxl_cb(); } +/** + * @brief Callback called when delegation is approved + * + */ static void approve_callback(void) { - nbgl_useCaseStatus(transactionContext.confirmed_status, true, ui_initial_screen); - transactionContext.ok_cb(); + nbgl_useCaseStatus("DELEGATE\nCONFIRMED", true, ui_initial_screen); + delegation_context.ok_cb(); } +/** + * @brief Callback called when delegation is accepted or cancelled + * + * @param confirm: true if accepted, false if cancelled + */ static void confirmation_callback(bool confirm) { if (confirm) { approve_callback(); @@ -53,59 +64,56 @@ static void confirmation_callback(bool confirm) { } } -static void continue_light_callback(void) { - transactionContext.infoLongPress.icon = &C_tezos; - transactionContext.infoLongPress.longPressText = "Approve"; - transactionContext.infoLongPress.tuneId = TUNE_TAP_CASUAL; - transactionContext.infoLongPress.text = "Confirm delegate\nregistration"; - - nbgl_useCaseStaticReviewLight(&transactionContext.tagValueList, - &transactionContext.infoLongPress, +/** + * @brief Draws a confirmation page + * + */ +static void confirm_delegation_page(void) { + delegation_context.infoLongPress.icon = &C_tezos; + delegation_context.infoLongPress.longPressText = "Approve"; + delegation_context.infoLongPress.tuneId = TUNE_TAP_CASUAL; + delegation_context.infoLongPress.text = "Confirm delegate\nregistration"; + + nbgl_useCaseStaticReviewLight(&delegation_context.tagValueList, + &delegation_context.infoLongPress, "Cancel", confirmation_callback); } -void prompt_register_delegate(ui_callback_t const ok_cb, ui_callback_t const cxl_cb) { +void prompt_delegation(ui_callback_t const ok_cb, ui_callback_t const cxl_cb) { uint8_t value_index = 0; - if (!G.maybe_ops.is_valid) { - THROW(EXC_MEMORY_ERROR); - } + delegation_context.ok_cb = ok_cb; + delegation_context.cxl_cb = cxl_cb; - transactionContext.ok_cb = ok_cb; - transactionContext.cxl_cb = cxl_cb; - - bip32_path_with_curve_to_pkh_string(transactionContext.buffer[value_index], - sizeof(transactionContext.buffer[value_index]), + bip32_path_with_curve_to_pkh_string(delegation_context.buffer[value_index], + BUFFER_SIZE, &global.path_with_curve); - transactionContext.tagValuePair[value_index].item = "Address"; - transactionContext.tagValuePair[value_index].value = transactionContext.buffer[value_index]; + delegation_context.tagValues[value_index].item = "Address"; + delegation_context.tagValues[value_index].value = delegation_context.buffer[value_index]; value_index++; - microtez_to_string_indirect(transactionContext.buffer[value_index], - sizeof(transactionContext.buffer[value_index]), + microtez_to_string_indirect(delegation_context.buffer[value_index], + BUFFER_SIZE, &G.maybe_ops.v.total_fee); - transactionContext.tagValuePair[value_index].item = "Fee"; - transactionContext.tagValuePair[value_index].value = transactionContext.buffer[value_index]; + delegation_context.tagValues[value_index].item = "Fee"; + delegation_context.tagValues[value_index].value = delegation_context.buffer[value_index]; value_index++; if (value_index != NUMBER_VALUES) { THROW(EXC_MEMORY_ERROR); } - transactionContext.tagValueList.nbPairs = NUMBER_VALUES; - transactionContext.tagValueList.pairs = transactionContext.tagValuePair; - - transactionContext.confirmed_status = "DELEGATE\nCONFIRMED"; - transactionContext.cancelled_status = "Delegate registration\ncancelled"; + delegation_context.tagValueList.nbPairs = NUMBER_VALUES; + delegation_context.tagValueList.pairs = delegation_context.tagValues; nbgl_useCaseReviewStart(&C_tezos, "Register delegate", NULL, "Cancel", - continue_light_callback, + confirm_delegation_page, cancel_callback); }