diff --git a/src/apdu_pubkey.c b/src/apdu_pubkey.c index 80f7336a..6320697c 100644 --- a/src/apdu_pubkey.c +++ b/src/apdu_pubkey.c @@ -1,16 +1,15 @@ #include "apdu_pubkey.h" #include "apdu.h" -#include "cx.h" -#include "globals.h" -#include "keys.h" -#include "protocol.h" -#include "to_string.h" -#include "ui.h" #include "baking_auth.h" +#include "globals.h" +#include "ui_pubkey.h" -#include - +/** + * @brief Sends apdu response with the public key + * + * @return true + */ static bool pubkey_ok(void) { cx_ecfp_public_key_t public_key = {0}; generate_public_key(&public_key, @@ -20,21 +19,19 @@ static bool pubkey_ok(void) { return true; } +/** + * @brief Authorizes the public key + * + * Sends apdu response with the public key + * + * @return true + */ static bool baking_ok(void) { authorize_baking(global.path_with_curve.derivation_type, &global.path_with_curve.bip32_path); pubkey_ok(); return true; } -char const *const *get_baking_prompts() { - static const char *const baking_prompts[] = { - PROMPT("Authorize Baking"), - PROMPT("Public Key Hash"), - NULL, - }; - return baking_prompts; -} - size_t handle_apdu_get_public_key(uint8_t instruction, volatile uint32_t *flags) { uint8_t *dataBuffer = G_io_apdu_buffer + OFFSET_CDATA; @@ -74,7 +71,7 @@ size_t handle_apdu_get_public_key(uint8_t instruction, volatile uint32_t *flags) cb = pubkey_ok; bake = false; } - prompt_address(bake, cb, delay_reject); + prompt_pubkey(bake, cb, delay_reject); *flags = IO_ASYNCH_REPLY; return 0; } diff --git a/src/apdu_pubkey.h b/src/apdu_pubkey.h index e2514daa..bbf6095d 100644 --- a/src/apdu_pubkey.h +++ b/src/apdu_pubkey.h @@ -1,7 +1,13 @@ #pragma once -#include "apdu.h" +#include +#include +/** + * @brief Handles AUTHORIZE_BAKING, GET_PUBLIC_KEY and PROMPT_PUBLIC_KEY instructions + * + * @param instruction: apdu instruction + * @param flags: io flags + * @return size_t: offset of the apdu response + */ size_t handle_apdu_get_public_key(uint8_t instruction, volatile uint32_t* flags); - -void prompt_address(bool baking, ui_callback_t ok_cb, ui_callback_t cxl_cb); diff --git a/src/types.h b/src/types.h index 6b9cdc54..87627d74 100644 --- a/src/types.h +++ b/src/types.h @@ -144,14 +144,6 @@ typedef struct { #define PROMPT_WIDTH 16 #define VALUE_WIDTH PROTOCOL_HASH_BASE58_STRING_SIZE -// Macros to wrap a static prompt and value strings and ensure they aren't too long. -#define PROMPT(str) \ - ({ \ - _Static_assert(sizeof(str) <= PROMPT_WIDTH + 1 /*null byte*/, \ - str " won't fit in the UI prompt."); \ - str; \ - }) - // Operations #define PROTOCOL_HASH_SIZE 32 diff --git a/src/ui_pubkey.h b/src/ui_pubkey.h new file mode 100644 index 00000000..f1a75fdc --- /dev/null +++ b/src/ui_pubkey.h @@ -0,0 +1,17 @@ +#pragma once + +#include "types.h" + +/** + * @brief Draws public key confirmation pages flow + * + * - Initial screen + * - Values: + * - Address + * - Confirmation screens + * + * @param authorize: if ask for authorize the public key + * @param ok_cb: accept callback + * @param cxl_cb: reject callback + */ +void prompt_pubkey(bool authorize, ui_callback_t const ok_cb, ui_callback_t const cxl_cb); diff --git a/src/ui_pubkey_bagl.c b/src/ui_pubkey_bagl.c index a7c3e66a..99d74f87 100644 --- a/src/ui_pubkey_bagl.c +++ b/src/ui_pubkey_bagl.c @@ -1,23 +1,15 @@ #ifdef HAVE_BAGL -#include "apdu_pubkey.h" -#include "apdu.h" -#include "cx.h" +#include "ui_pubkey.h" + #include "globals.h" -#include "keys.h" -#include "protocol.h" #include "to_string.h" #include "ui.h" -#include "baking_auth.h" - -#include -__attribute__((noreturn)) void prompt_address(bool baking, - ui_callback_t ok_cb, - ui_callback_t cxl_cb) { +void prompt_pubkey(bool authorize, ui_callback_t ok_cb, ui_callback_t cxl_cb) { init_screen_stack(); - if (baking) { + if (authorize) { push_ui_callback("Authorize Baking", copy_string, "With Public Key?"); push_ui_callback("Public Key Hash", bip32_path_with_curve_to_pkh_string, @@ -30,6 +22,5 @@ __attribute__((noreturn)) void prompt_address(bool baking, } ux_confirm_screen(ok_cb, cxl_cb); - __builtin_unreachable(); } #endif // HAVE_BAGL diff --git a/src/ui_pubkey_nbgl.c b/src/ui_pubkey_nbgl.c index 3ad7e0c3..86c516da 100644 --- a/src/ui_pubkey_nbgl.c +++ b/src/ui_pubkey_nbgl.c @@ -1,38 +1,54 @@ #ifdef HAVE_NBGL -#include "nbgl_use_case.h" -#include "apdu_pubkey.h" -#include "apdu.h" -#include "cx.h" +#include "ui_pubkey.h" + #include "globals.h" -#include "keys.h" -#include "protocol.h" #include "to_string.h" #include "ui.h" -#include "baking_auth.h" -#include +#include "nbgl_use_case.h" -#define G global.apdu.u.baking +#define BUFFER_SIZE sizeof(cx_ecfp_public_key_t) +/** + * @brief This structure represents a context needed for address screens navigation + * + */ typedef struct { - char buffer[sizeof(cx_ecfp_public_key_t)]; - ui_callback_t ok_cb; - ui_callback_t cxl_cb; -} TransactionContext_t; + ui_callback_t ok_cb; /// accept callback + ui_callback_t cxl_cb; /// cancel callback + char buffer[BUFFER_SIZE]; /// value buffer +} AddressContext_t; -static TransactionContext_t transactionContext; +/** + * @brief Current address context + * + */ +static AddressContext_t address_context; +/** + * @brief Callback called when address is rejected + * + */ static void cancel_callback(void) { - transactionContext.cxl_cb(); + address_context.cxl_cb(); nbgl_useCaseStatus("Address rejected", false, ui_initial_screen); } +/** + * @brief Callback called when address is approved + * + */ static void approve_callback(void) { - transactionContext.ok_cb(); + address_context.ok_cb(); nbgl_useCaseStatus("ADDRESS\nVERIFIED", true, ui_initial_screen); } +/** + * @brief Callback called when address is approved or cancelled + * + * @param confirm: true if approved, false if cancelled + */ static void confirmation_callback(bool confirm) { if (confirm) { approve_callback(); @@ -41,20 +57,24 @@ static void confirmation_callback(bool confirm) { } } +/** + * @brief Draws an address confirmation page + * + */ static void verify_address(void) { - nbgl_useCaseAddressConfirmation(transactionContext.buffer, confirmation_callback); + nbgl_useCaseAddressConfirmation(address_context.buffer, confirmation_callback); } -void prompt_address(bool baking, ui_callback_t ok_cb, ui_callback_t cxl_cb) { - transactionContext.ok_cb = ok_cb; - transactionContext.cxl_cb = cxl_cb; +void prompt_pubkey(bool authorize, ui_callback_t ok_cb, ui_callback_t cxl_cb) { + address_context.ok_cb = ok_cb; + address_context.cxl_cb = cxl_cb; - bip32_path_with_curve_to_pkh_string(transactionContext.buffer, - sizeof(transactionContext.buffer), + bip32_path_with_curve_to_pkh_string(address_context.buffer, + BUFFER_SIZE, &global.path_with_curve); const char* text; - if (baking) { + if (authorize) { text = "Authorize Tezos\nBaking address"; } else { text = "Verify Tezos\naddress";