Skip to content

Commit

Permalink
refactor: remove settings (hash signing and custom contracts) (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
overcat authored Jul 22, 2024
1 parent 0ab3f55 commit cbfd6ac
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 362 deletions.
2 changes: 0 additions & 2 deletions docs/COMMANDS.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@
| 0x6985 | `SW_DENY` | Rejected by user |
| 0x6A87 | `SW_WRONG_DATA_LENGTH` | `Lc` or minimum APDU lenght is incorrect |
| 0x6B00 | `SW_WRONG_P1P2` | Either `P1` or `P2` is incorrect |
| 0x6C66 | `SW_HASH_SIGNING_MODE_NOT_ENABLED` | Hash signing model not enabled |
| 0x6C77 | `SW_UNVERIFIED_CONTRACTS_MODE_NOT_ENABLED` | Unverified contracts (custom contracts) model not enabled |
| 0x6D00 | `SW_INS_NOT_SUPPORTED` | No command exists with `INS` |
| 0x6E00 | `SW_CLA_NOT_SUPPORTED` | Bad `CLA` used for this application |
| 0xB002 | `SW_DISPLAY_ADDRESS_FAIL` | Failed to display address |
Expand Down
10 changes: 6 additions & 4 deletions src/handler/get_app_configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
#include "constants.h"
#include "sw.h"
#include "types.h"
#include "settings.h"

// For compatibility with old clients.
// We no longer need to enable it in the settings.
#define HASH_SIGNING_ENABLED 1

int handler_get_app_configuration() {
PRINTF("handler_get_app_configuration invoked\n");
Expand All @@ -43,13 +46,12 @@ int handler_get_app_configuration() {
_Static_assert(RAW_DATA_MAX_SIZE >= 0 && RAW_DATA_MAX_SIZE <= UINT16_MAX,
"RAW_DATA_MAX_SIZE must be between 0 and 65535!");

uint8_t config[] = {HAS_SETTING(S_HASH_SIGNING_ENABLED),
uint8_t config[] = {HASH_SIGNING_ENABLED,
MAJOR_VERSION,
MINOR_VERSION,
PATCH_VERSION,
RAW_DATA_MAX_SIZE >> 8,
RAW_DATA_MAX_SIZE & 0xFF,
HAS_SETTING(S_UNVERIFIED_CONTRACTS_ENABLED)};
RAW_DATA_MAX_SIZE & 0xFF};

return io_send_response_pointer(config, sizeof(config), SW_OK);
}
6 changes: 0 additions & 6 deletions src/handler/sign_auth.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#include "sw.h"
#include "globals.h"
#include "plugin.h"
#include "settings.h"
#include "ui/display.h"
#include "crypto.h"
#include "helper/send_response.h"
Expand Down Expand Up @@ -97,11 +96,6 @@ int handler_sign_auth(buffer_t *cdata, bool is_first_chunk, bool more) {
}

G_context.unverified_contracts = check_include_custom_contract();
PRINTF("G_context.unverified_contracts: %d\n", G_context.unverified_contracts);
if (G_context.unverified_contracts && !HAS_SETTING(S_UNVERIFIED_CONTRACTS_ENABLED)) {
return io_send_sw(SW_UNVERIFIED_CONTRACTS_MODE_NOT_ENABLED);
}

return ui_display_auth();
};

Expand Down
5 changes: 0 additions & 5 deletions src/handler/sign_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,10 @@
#include "types.h"
#include "sw.h"
#include "crypto.h"
#include "settings.h"
#include "ui/display.h"
#include "helper/send_response.h"

int handler_sign_hash(buffer_t *cdata) {
if (!HAS_SETTING(S_HASH_SIGNING_ENABLED)) {
return io_send_sw(SW_HASH_SIGNING_MODE_NOT_ENABLED);
}

explicit_bzero(&G_context, sizeof(G_context));
G_context.req_type = CONFIRM_HASH;
G_context.state = STATE_NONE;
Expand Down
6 changes: 0 additions & 6 deletions src/handler/sign_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#include "sw.h"
#include "globals.h"
#include "plugin.h"
#include "settings.h"
#include "ui/display.h"
#include "crypto.h"
#include "helper/send_response.h"
Expand Down Expand Up @@ -132,11 +131,6 @@ int handler_sign_tx(buffer_t *cdata, bool is_first_chunk, bool more) {
}

G_context.unverified_contracts = check_include_custom_contract();
PRINTF("G_context.unverified_contracts: %d\n", G_context.unverified_contracts);
if (G_context.unverified_contracts && !HAS_SETTING(S_UNVERIFIED_CONTRACTS_ENABLED)) {
return io_send_sw(SW_UNVERIFIED_CONTRACTS_MODE_NOT_ENABLED);
}

return ui_display_transaction();
}
};
Expand Down
4 changes: 1 addition & 3 deletions src/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ extern const internal_storage_t N_storage_real;
// check a setting item
#define HAS_SETTING(k) ((N_settings & (1 << (k))) >> (k))

#define S_HASH_SIGNING_ENABLED 0
#define S_SEQUENCE_NUMBER_ENABLED 1
#define S_UNVERIFIED_CONTRACTS_ENABLED 2
#define S_SEQUENCE_NUMBER_ENABLED 0

#define S_INITIALIZED 7
8 changes: 0 additions & 8 deletions src/sw.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,6 @@
* Status word for incorrect P1 or P2.
*/
#define SW_WRONG_P1P2 0x6B00
/**
* Status word for hash signing model not enabled.
*/
#define SW_HASH_SIGNING_MODE_NOT_ENABLED 0x6C66
/**
* Status word for unverified contracts model not enabled.
*/
#define SW_UNVERIFIED_CONTRACTS_MODE_NOT_ENABLED 0x6C77
/**
* Status word for unknown command with this INS.
*/
Expand Down
62 changes: 4 additions & 58 deletions src/ui/bagl_menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,63 +22,29 @@

static void ui_idle(void);
static void display_settings(const ux_flow_step_t* const start_step);
static void switch_settings_hash_signing();
static void switch_settings_sequence_number();
static void switch_settings_unverified_contracts();

// FLOW for the settings menu:
// #1 screen: enable hash signing
// #1 screen: enable sequence number
// #2 screen: quit
#if defined(TARGET_NANOS)

UX_STEP_CB(ux_settings_unverified_contracts_step,
bnnn_paging,
switch_settings_unverified_contracts(),
{
.title = "Custom contracts",
.text = G.ui.detail_value,
});
UX_STEP_CB(ux_settings_hash_signing_step,
bnnn_paging,
switch_settings_hash_signing(),
{
.title = "Hash signing",
.text = G.ui.detail_value + 12,
});
UX_STEP_CB(ux_settings_sequence_number_step,
bnnn_paging,
switch_settings_sequence_number(),
{
.title = "Sequence Number",
.text = G.ui.detail_value + 24,
.text = G.ui.detail_value,
});
#else
UX_STEP_CB(ux_settings_unverified_contracts_step,
bnnn,
switch_settings_unverified_contracts(),
{
"Custom contracts",
"Allow unverified",
"contracts",
G.ui.detail_value,
});
UX_STEP_CB(ux_settings_hash_signing_step,
bnnn,
switch_settings_hash_signing(),
{
"Hash signing",
"Enable hash",
"signing",
G.ui.detail_value + 12,
});
UX_STEP_CB(ux_settings_sequence_number_step,
bnnn,
switch_settings_sequence_number(),
{
"Sequence Number",
"Display sequence",
"in transactions",
G.ui.detail_value + 24,
G.ui.detail_value,
});

#endif
Expand All @@ -89,11 +55,7 @@ UX_STEP_CB(ux_settings_exit_step,
&C_icon_back_x,
"Back",
});
UX_FLOW(ux_settings_flow,
&ux_settings_unverified_contracts_step,
&ux_settings_hash_signing_step,
&ux_settings_sequence_number_step,
&ux_settings_exit_step);
UX_FLOW(ux_settings_flow, &ux_settings_sequence_number_step, &ux_settings_exit_step);

// We have a screen with the icon and "Stellar is ready"
UX_STEP_NOCB(ux_menu_ready_step, pnn, {&C_icon_stellar, "Stellar", "is ready"});
Expand Down Expand Up @@ -129,30 +91,14 @@ static void ui_idle(void) {

static void display_settings(const ux_flow_step_t* const start_step) {
strlcpy(G.ui.detail_value,
(HAS_SETTING(S_UNVERIFIED_CONTRACTS_ENABLED) ? "Enabled" : "NOT Enabled"),
12);
strlcpy(G.ui.detail_value + 12,
(HAS_SETTING(S_HASH_SIGNING_ENABLED) ? "Enabled" : "NOT Enabled"),
12);
strlcpy(G.ui.detail_value + 24,
(HAS_SETTING(S_SEQUENCE_NUMBER_ENABLED) ? "Displayed" : "NOT Displayed"),
14);

ux_flow_init(0, ux_settings_flow, start_step);
}

static void switch_settings_hash_signing() {
SETTING_TOGGLE(S_HASH_SIGNING_ENABLED);
display_settings(&ux_settings_hash_signing_step);
}

static void switch_settings_sequence_number() {
SETTING_TOGGLE(S_SEQUENCE_NUMBER_ENABLED);
display_settings(&ux_settings_sequence_number_step);
}

static void switch_settings_unverified_contracts() {
SETTING_TOGGLE(S_UNVERIFIED_CONTRACTS_ENABLED);
display_settings(&ux_settings_unverified_contracts_step);
}
#endif // HAVE_BAGL
100 changes: 4 additions & 96 deletions src/ui/nbgl_menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,16 @@ void app_quit(void) {
// -----------------------------------------------------------
// --------------------- SETTINGS MENU -----------------------
// -----------------------------------------------------------
#define SETTING_INFO_NB 3
#define SETTING_INFO_NB 2
static const char* const INFO_TYPES[SETTING_INFO_NB] = {"Version", "Developer"};
static const char* const INFO_CONTENTS[SETTING_INFO_NB] = {APPVERSION, "overcat"};

// settings switches definitions
enum {
SWITCH_UNVERIFIED_CONTRACTS_SET_TOKEN = FIRST_USER_TOKEN,
SWITCH_HASH_SET_TOKEN,
SWITCH_SEQUENCE_SET_TOKEN,
SWITCH_SEQUENCE_SET_TOKEN = FIRST_USER_TOKEN,
};

enum {
SWITCH_UNVERIFIED_CONTRACTS_SET_ID = 0,
SWITCH_HASH_SET_ID,
SWITCH_SEQUENCE_SET_ID,
SETTINGS_SWITCHES_NB
};
enum { SWITCH_SEQUENCE_SET_ID = 0, SETTINGS_SWITCHES_NB };

static nbgl_contentSwitch_t switches[SETTINGS_SWITCHES_NB] = {0};

Expand All @@ -65,8 +58,6 @@ static const nbgl_contentInfoList_t info_list = {
};

static uint8_t init_setting_page;
static void review_warning_choice_hash_signing(bool confirm);
static void review_warning_choice_unverified_contracts(bool confirm);
static void controls_callback(int token, uint8_t index, int page);

// settings menu definition
Expand All @@ -81,105 +72,22 @@ static const nbgl_genericContents_t setting_contents = {.callbackCallNeeded = fa
.contentsList = contents,
.nbContents = SETTING_CONTENTS_NB};

// callback for setting warning choice
static void review_warning_choice_hash_signing(bool confirm) {
if (confirm) {
switches[SWITCH_HASH_SET_ID].initState = ON_STATE;
// store the new setting value in NVM
SETTING_TOGGLE(S_HASH_SIGNING_ENABLED);
}

// Reset setting menu to the right page
nbgl_useCaseHomeAndSettings(APPNAME,
&C_icon_stellar_64px,
NULL,
init_setting_page,
&setting_contents,
&info_list,
NULL,
app_quit);
}

// callback for setting warning choice
static void review_warning_choice_unverified_contracts(bool confirm) {
if (confirm) {
switches[SWITCH_UNVERIFIED_CONTRACTS_SET_ID].initState = ON_STATE;
// store the new setting value in NVM
SETTING_TOGGLE(S_UNVERIFIED_CONTRACTS_ENABLED);
}

// Reset setting menu to the right page
nbgl_useCaseHomeAndSettings(APPNAME,
&C_icon_stellar_64px,
NULL,
init_setting_page,
&setting_contents,
&info_list,
NULL,
app_quit);
}

static void controls_callback(int token, uint8_t index, int page) {
UNUSED(index);
init_setting_page = page;

if (token == SWITCH_HASH_SET_TOKEN) {
if (HAS_SETTING(S_HASH_SIGNING_ENABLED)) {
switches[SWITCH_HASH_SET_ID].initState = OFF_STATE;
SETTING_TOGGLE(S_HASH_SIGNING_ENABLED);
} else {
// Display the warning message and ask the user to confirm
nbgl_useCaseChoice(
&C_Warning_64px,
"Enable Hash Signing",
"Signing hashes is a dangerous operation that can put your wallet at serious risk. "
"Only enable this feature if you are sure you know what you are doing.",
"I understand, confirm",
"Cancel",
review_warning_choice_hash_signing);
}
} else if (token == SWITCH_SEQUENCE_SET_TOKEN) {
if (token == SWITCH_SEQUENCE_SET_TOKEN) {
// toggle the switch value
switches[SWITCH_SEQUENCE_SET_ID].initState =
(!HAS_SETTING(S_SEQUENCE_NUMBER_ENABLED)) ? ON_STATE : OFF_STATE;
// store the new setting value in NVM
SETTING_TOGGLE(S_SEQUENCE_NUMBER_ENABLED);
} else if (token == SWITCH_UNVERIFIED_CONTRACTS_SET_TOKEN) {
if (HAS_SETTING(S_UNVERIFIED_CONTRACTS_ENABLED)) {
switches[SWITCH_UNVERIFIED_CONTRACTS_SET_ID].initState = OFF_STATE;
SETTING_TOGGLE(S_UNVERIFIED_CONTRACTS_ENABLED);
} else {
// Display the warning message and ask the user to confirm
nbgl_useCaseChoice(
&C_Warning_64px,
"Allow unverified contracts",
"Unverified contracts may not be displayed in a readable form on "
"your Ledger, so you need to examine them very carefully before sign them.",
"I understand, confirm",
"Cancel",
review_warning_choice_unverified_contracts);
}
}
}

// home page definition
void ui_menu_main(void) {
// Initialize switches data

switches[SWITCH_UNVERIFIED_CONTRACTS_SET_ID].initState =
(HAS_SETTING(S_UNVERIFIED_CONTRACTS_ENABLED)) ? ON_STATE : OFF_STATE;
switches[SWITCH_UNVERIFIED_CONTRACTS_SET_ID].text = "Custom contracts";
switches[SWITCH_UNVERIFIED_CONTRACTS_SET_ID].subText = "Allow unverified contract";
switches[SWITCH_UNVERIFIED_CONTRACTS_SET_ID].token = SWITCH_UNVERIFIED_CONTRACTS_SET_TOKEN;
switches[SWITCH_UNVERIFIED_CONTRACTS_SET_ID].tuneId = TUNE_TAP_CASUAL;

switches[SWITCH_HASH_SET_ID].initState =
(HAS_SETTING(S_HASH_SIGNING_ENABLED)) ? ON_STATE : OFF_STATE;
switches[SWITCH_HASH_SET_ID].text = "Hash signing";
switches[SWITCH_HASH_SET_ID].subText = "Enable hash signing";
switches[SWITCH_HASH_SET_ID].token = SWITCH_HASH_SET_TOKEN;
switches[SWITCH_HASH_SET_ID].tuneId = TUNE_TAP_CASUAL;

switches[SWITCH_SEQUENCE_SET_ID].initState =
(HAS_SETTING(S_SEQUENCE_NUMBER_ENABLED)) ? ON_STATE : OFF_STATE;
switches[SWITCH_SEQUENCE_SET_ID].text = "Sequence number";
Expand Down
Loading

0 comments on commit cbfd6ac

Please sign in to comment.