Skip to content

Commit

Permalink
Add a settings page with facility to turn HWM on/off
Browse files Browse the repository at this point in the history
- Add HWM to N_data persistent storage
- Add HWM to settings in nano home layout
- Add HWM switch to stax settings page
  • Loading branch information
ajinkyaraj-23 committed Mar 27, 2024
1 parent b4ebe68 commit ef8946e
Show file tree
Hide file tree
Showing 211 changed files with 181 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ compile_commands.json
# Documentation files
doc/html
doc/latex
# Pytest temporary snapshots
test/snapshots-tmp
4 changes: 4 additions & 0 deletions src/globals.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ void init_globals(void) {
memset(&global, 0, sizeof(global));
}

void toggle_hwm(void) {
UPDATE_NVRAM(ram, { ram->hwm_disabled = !ram->hwm_disabled; });
}

// DO NOT TRY TO INIT THIS. This can only be written via an system call.
// The "N_" is *significant*. It tells the linker to put this in NVRAM.
nvram_data const N_data_real;
Expand Down
8 changes: 8 additions & 0 deletions src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ void clear_apdu_globals(void);
*/
void init_globals(void);

/**
* @brief Toggle high watermark tracking by Ledger.
*
* if its off, the responsibility to track watermark for blocks/attestation signed falls on the
* signer being used.
*/
void toggle_hwm(void);

/// Maximum number of bytes in a single APDU
#define MAX_APDU_SIZE 235u

Expand Down
8 changes: 8 additions & 0 deletions src/to_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,14 @@ int hwm_to_string(char *dest, size_t dest_size, high_watermark_t const *const hw
}
}

int hwm_status_to_string(char *dest, size_t dest_size, bool const *hwm_disabled) {
if (dest == NULL) {
return -1;
}
memcpy(dest, *hwm_disabled ? "Disabled" : "Enabled", dest_size);
return 0;
}

int copy_string(char *const dest, size_t const dest_size, char const *const src) {
if ((dest == NULL) || (src == NULL)) {
return false;
Expand Down
9 changes: 9 additions & 0 deletions src/to_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ int microtez_to_string(char *const dest, size_t dest_size, uint64_t number);
*/
int hwm_to_string(char *dest, size_t dest_size, high_watermark_t const *const hwm);

/**
*
* @param dest output buffer
* @param dest_size output size
* @param hwm_disabled High watermark tracking enabled/disabled on ledger
* @return
*/
int hwm_status_to_string(char *dest, size_t dest_size, bool const *hwm_disabled);

/**
* @brief Copies a string in a buffer
*
Expand Down
16 changes: 11 additions & 5 deletions src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ typedef enum {
DERIVATION_TYPE_ED25519 = 3,
DERIVATION_TYPE_BIP32_ED25519 = 4
} derivation_type_t;

typedef enum {
SIGNATURE_TYPE_UNSET = 0,
SIGNATURE_TYPE_SECP256K1 = 1,
Expand Down Expand Up @@ -197,12 +198,17 @@ typedef struct {
high_watermark_t main; ///< HWM of main
high_watermark_t test; ///< HWM of test
} hwm;

bip32_path_with_curve_t baking_key; ///< authorized key
bool hwm_disabled; /**< Set HWM setting on/off,
e.g. if you are using signer assisted HWM,
no need to track HWM using Ledger.*/
} nvram_data;

#define SIGN_HASH_SIZE 32u // TODO: Rename or use a different constant.

#define PKH_STRING_SIZE 40u // includes null byte // TODO: use sizeof for this.
#define HWM_STATUS_SIZE 9u // HWM status takes values Enabled and Disabled.
#define PROTOCOL_HASH_BASE58_STRING_SIZE \
sizeof("ProtoBetaBetaBetaBetaBetaBetaBetaBetaBet11111a5ug96")

Expand All @@ -226,11 +232,11 @@ typedef struct {
*
*/
typedef struct parsed_contract {
uint8_t originated; ///< a lightweight bool
signature_type_t
signature_type; ///< 0 in originated case
///< An implicit contract with signature_type of 0 means not present
uint8_t hash[HASH_SIZE]; ///< hash of the contract
uint8_t originated; ///< a lightweight bool
signature_type_t signature_type; /**< 0 in originated case
An implicit contract with signature_type of 0
means not present*/
uint8_t hash[HASH_SIZE]; ///< hash of the contract
} parsed_contract_t;

/**
Expand Down
36 changes: 35 additions & 1 deletion src/ui_bagl.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,15 @@ typedef struct {
char chain_id[CHAIN_ID_BASE58_STRING_SIZE];
char authorized_key[PKH_STRING_SIZE];
char hwm[MAX_INT_DIGITS + 1u + MAX_INT_DIGITS + 1u];
char hwm_status[HWM_STATUS_SIZE];
} HomeContext_t;

/// Current home context
static HomeContext_t home_context;

void ui_settings(void); ///> Initialize settings page
void ui_toggle_hwm(void); ///> Toggle HWM settings
void ui_menu_init(void);
/**
* @brief Idle flow
*
Expand All @@ -66,6 +70,7 @@ UX_STEP_NOCB(ux_app_is_ready_step, nn, {"Application", "is ready"});
UX_STEP_NOCB(ux_version_step, bnnn_paging, {"Tezos Baking", APPVERSION});
UX_STEP_NOCB(ux_chain_id_step, bnnn_paging, {"Chain", home_context.chain_id});
UX_STEP_NOCB(ux_authorized_key_step, bnnn_paging, {"Public Key Hash", home_context.authorized_key});
UX_STEP_CB(ux_settings_step, pb, ui_settings(), {&C_icon_coggle, "Settings"});
UX_STEP_NOCB(ux_hwm_step, bnnn_paging, {"High Watermark", home_context.hwm});
UX_STEP_CB(ux_idle_quit_step, pb, exit_app(), {&C_icon_dashboard_x, "Quit"});

Expand All @@ -74,10 +79,35 @@ UX_FLOW(ux_idle_flow,
&ux_version_step,
&ux_chain_id_step,
&ux_authorized_key_step,
&ux_settings_step,
&ux_hwm_step,
&ux_idle_quit_step,
FLOW_LOOP);

void ui_menu_init(void) {
ux_flow_init(0, ux_idle_flow, NULL);
}

UX_STEP_CB(ux_hwm_info, bn, ui_toggle_hwm(), {"High Watermark", home_context.hwm_status});
UX_STEP_CB(ux_menu_back_step, pb, ui_menu_init(), {&C_icon_back, "Back"});

// FLOW for the about submenu:
// #1 screen: app info
// #2 screen: back button to main menu
UX_FLOW(ux_settings_flow, &ux_hwm_info, &ux_menu_back_step, FLOW_LOOP);

void ui_settings(void) {
memcpy(home_context.hwm_status,
N_data.hwm_disabled ? "Disabled" : "Enabled",
sizeof(home_context.hwm_status));
ux_flow_init(0, ux_settings_flow, NULL);
}

void ui_toggle_hwm(void) {
toggle_hwm();
ui_settings();
}

/**
* @brief Calculates baking values for the idle screens
*
Expand Down Expand Up @@ -106,6 +136,10 @@ static bool calculate_baking_idle_screens_data(void) {

TZ_ASSERT(hwm_to_string(home_context.hwm, sizeof(home_context.hwm), &N_data.hwm.main) >= 0,
EXC_WRONG_LENGTH);
TZ_ASSERT(hwm_status_to_string(home_context.hwm_status,
sizeof(home_context.hwm_status),
&N_data.hwm_disabled) >= 0,
EXC_UNKNOWN_CX_ERR);

return true;

Expand All @@ -121,7 +155,7 @@ void ui_initial_screen(void) {
}

if (calculate_baking_idle_screens_data()) {
ux_flow_init(0, ux_idle_flow, NULL);
ui_menu_init();
}
}

Expand Down
89 changes: 68 additions & 21 deletions src/ui_nbgl.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

#ifdef HAVE_NBGL
// #ifdef HAVE_NBGL
#include "bolos_target.h"

#include "ui.h"
Expand All @@ -38,7 +38,7 @@
#include "nbgl_use_case.h"

static const char* const infoTypes[] = {"Version", "Developer", "Copyright"};
static const char* const infoContents[] = {APPVERSION, "Ledger", "(c) 2023 Ledger"};
static const char* const infoContents[] = {APPVERSION, "Trilitech", "(c) Trilitech, (c) Functori"};

#define MAX_LENGTH 200
static char* bakeInfoContents[3];
Expand All @@ -50,6 +50,16 @@ static const char* const bakeInfoTypes[] = {
"High Watermark",
};

enum {
HWM_ENABLED_TOKEN = FIRST_USER_TOKEN
};
enum {
HWM_ENABLED_TOKEN_ID = 0,
SETTINGS_SWITCHES_NB
};

static nbgl_layoutSwitch_t switches[SETTINGS_SWITCHES_NB] = {0};

/**
* @brief Callback to fill the settings page content
*
Expand All @@ -58,7 +68,7 @@ static const char* const bakeInfoTypes[] = {
* @return bool: if the page is not out of bounds
*/
static bool navigation_cb_baking(uint8_t page, nbgl_pageContent_t* content) {
if (page > 1u) {
if (page > 2u) {
return false;
}

Expand All @@ -67,6 +77,7 @@ static bool navigation_cb_baking(uint8_t page, nbgl_pageContent_t* content) {
bakeInfoContents[0] = buffer[0];
bakeInfoContents[1] = buffer[1];
bakeInfoContents[2] = buffer[2];
const bool hwm_disabled = N_data.hwm_disabled;

TZ_ASSERT(
chain_id_to_string_with_aliases(buffer[0], sizeof(buffer[0]), &N_data.main_chain_id) >= 0,
Expand All @@ -81,41 +92,77 @@ static bool navigation_cb_baking(uint8_t page, nbgl_pageContent_t* content) {

TZ_ASSERT(hwm_to_string(buffer[2], sizeof(buffer[2]), &N_data.hwm.main) >= 0, EXC_WRONG_LENGTH);

if (page == 0u) {
content->type = INFOS_LIST;
content->infosList.nbInfos = 3;
content->infosList.infoTypes = bakeInfoTypes;
content->infosList.infoContents = bakeInfoContents;
} else {
content->type = INFOS_LIST;
content->infosList.nbInfos = 3;
content->infosList.infoTypes = infoTypes;
content->infosList.infoContents = infoContents;
switch (page) {
case 0:
content->type = INFOS_LIST;
content->infosList.nbInfos = 3;
content->infosList.infoTypes = bakeInfoTypes;
content->infosList.infoContents = bakeInfoContents;
break;
case 1:
content->type = INFOS_LIST;
content->infosList.nbInfos = 3;
content->infosList.infoTypes = infoTypes;
content->infosList.infoContents = infoContents;
break;
case 2:
switches[HWM_ENABLED_TOKEN_ID].initState = (nbgl_state_t) (!hwm_disabled);
switches[HWM_ENABLED_TOKEN_ID].text = "High Watermark";
switches[HWM_ENABLED_TOKEN_ID].subText = "Track high watermark\n in Ledger";
switches[HWM_ENABLED_TOKEN_ID].token = HWM_ENABLED_TOKEN;
switches[HWM_ENABLED_TOKEN_ID].tuneId = TUNE_TAP_CASUAL;
content->type = SWITCHES_LIST;
content->switchesList.nbSwitches = SETTINGS_SWITCHES_NB;
content->switchesList.switches = (nbgl_layoutSwitch_t*) switches;
break;
default:
return false;
}

return true;

end:
TZ_EXC_PRINT(exc);
return true;
}

static void controls_callback(int token, uint8_t index) {
UNUSED(index);
bool switch_value;
if (token == HWM_ENABLED_TOKEN) {
// HWM switch touched
// toggle the switch value
switch_value = !N_data.hwm_disabled;
// store the new setting value in NVM
nvm_write((void*) &N_data.hwm_disabled, &switch_value, 1);
}
}

#define TOTAL_SETTINGS_PAGE (3)
#define INIT_SETTINGS_PAGE (0)
#define DISABLE_SUB_SETTINGS false

/**
* @brief Draws settings pages
*
*/
static void ui_menu_about_baking(void) {
nbgl_useCaseSettings("Tezos baking",
0,
2,
false,
INIT_SETTINGS_PAGE,
TOTAL_SETTINGS_PAGE,
DISABLE_SUB_SETTINGS,
ui_initial_screen,
navigation_cb_baking,
NULL);
controls_callback);
}

#define SETTINGS_BUTTON_ENABLED (true)

void ui_initial_screen(void) {
nbgl_useCaseHome("Tezos Baking", &C_tezos, NULL, false, ui_menu_about_baking, exit_app);
nbgl_useCaseHome("Tezos Baking",
&C_tezos,
NULL,
SETTINGS_BUTTON_ENABLED,
ui_menu_about_baking,
exit_app);
}

#endif // HAVE_NBGL
// #endif // HAVE_NBGL
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/snapshots/nanos/test_review_home/back.png
Binary file added test/snapshots/nanos/test_review_home/settings.png
Binary file added test/snapshots/nanosp/test_review_home/back.png
Binary file added test/snapshots/nanosp/test_review_home/settings.png
Binary file added test/snapshots/nanox/test_review_home/back.png
Binary file added test/snapshots/nanox/test_review_home/settings.png
Binary file modified test/snapshots/stax/test_review_home/app_context.png
Binary file modified test/snapshots/stax/test_review_home/description.png
Binary file modified test/snapshots/stax/test_review_home/home_screen.png
Loading

0 comments on commit ef8946e

Please sign in to comment.