Skip to content

Commit

Permalink
Add settings menu in stax
Browse files Browse the repository at this point in the history
  • Loading branch information
timemarkovqtum committed Oct 16, 2023
1 parent ab515e5 commit e0ba4b3
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 21 deletions.
26 changes: 19 additions & 7 deletions src/common/script.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
#include "../crypto.h"
#endif

#ifndef SKIP_FOR_CMOCKA
#define DELEGATIONS_ADDRESS "\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x86"
#define ADD_DELEGATION_HASH "\x4c\x0e\x96\x8c"
#define REMOVE_DELEGATION_HASH "\x3d\x66\x6e\x8b"

size_t get_push_script_size(uint32_t n) {
if (n <= 16)
return 1; // OP_0 and OP_1 .. OP_16
Expand Down Expand Up @@ -379,8 +384,18 @@ bool is_opsender(const uint8_t script[], size_t script_len) {
return is_opcontract((uint8_t *) script, script_len, OP_SENDER);
}

bool is_contract(const uint8_t script[], size_t script_len) {
return is_opcreate(script, script_len) || is_opcall(script, script_len);
bool is_delegate(const uint8_t script[], size_t script_len) {
char contractaddress[20];
size_t i;
for (i = 0; i < sizeof(contractaddress); i++) {
contractaddress[i] = script[script_len - 21 + i];
}
return strncmp(contractaddress, DELEGATIONS_ADDRESS, sizeof(contractaddress)) == 0;
}

bool is_contract_blind_sign(const uint8_t script[], size_t script_len) {
bool isContract = is_opcreate(script, script_len) || is_opcall(script, script_len);
return isContract && !is_delegate(script, script_len);
}

bool get_script_sender_address(uint8_t *buffer, size_t size, uint8_t *script) {
Expand All @@ -398,11 +413,6 @@ bool get_sender_sig(uint8_t *buffer, size_t size, uint8_t **sig, unsigned int *s
*sigSize > 0;
}

#ifndef SKIP_FOR_CMOCKA
#define DELEGATIONS_ADDRESS "\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x86"
#define ADD_DELEGATION_HASH "\x4c\x0e\x96\x8c"
#define REMOVE_DELEGATION_HASH "\x3d\x66\x6e\x8b"

bool opcall_addr_encode(const uint8_t script[],
size_t script_len,
char *out,
Expand Down Expand Up @@ -459,6 +469,8 @@ bool opcall_addr_encode(const uint8_t script[],
snprintf(out, out_len, "Delegate to %s (fee %d %%)", stakerbase58, delegationfee);
} else if (strncmp(functionhash, REMOVE_DELEGATION_HASH, sizeof(functionhash)) == 0) {
strncpy(out, "Undelegate", out_len);
} else {
return 0;
}
} else {
uint8_t contractaddressstring[41];
Expand Down
2 changes: 1 addition & 1 deletion src/common/script.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ bool is_opcall(const uint8_t script[], size_t script_len);

bool is_opsender(const uint8_t script[], size_t script_len);

bool is_contract(const uint8_t script[], size_t script_len);
bool is_contract_blind_sign(const uint8_t script[], size_t script_len);

/**
* Returns the size in bytes of the minimal push opcode for <n>, where n a uint32_t.
Expand Down
11 changes: 6 additions & 5 deletions src/handler/sign_psbt.c
Original file line number Diff line number Diff line change
Expand Up @@ -1370,7 +1370,6 @@ static bool read_outputs(dispatcher_context_t *dc,
output_info_t output;
memset(&output, 0, sizeof(output));
bool isOpSender = false;
bool isContract = false;

output_keys_callback_data_t callback_data = {.output = &output,
.placeholder_info = placeholder_info};
Expand Down Expand Up @@ -1440,11 +1439,13 @@ static bool read_outputs(dispatcher_context_t *dc,
++external_outputs_count;

// check if output contract data is allowed
isContract = is_contract(output.in_out.scriptPubKey, output.in_out.scriptPubKey_len);
if(isContract && !N_storage.dataAllowed) {
bool isContractBlindSign = is_contract_blind_sign(output.in_out.scriptPubKey, output.in_out.scriptPubKey_len);
if(isContractBlindSign && !N_storage.dataAllowed) {
ui_warn_contract_data(dc);
SEND_SW(dc, SW_SIGNATURE_FAIL);
return false;
if(!N_storage.dataAllowed) {
SEND_SW(dc, SW_SIGNATURE_FAIL);
return false;
}
}

if (!dry_run &&
Expand Down
17 changes: 17 additions & 0 deletions src/ui/display_nbgl.c
Original file line number Diff line number Diff line change
Expand Up @@ -549,4 +549,21 @@ void ui_display_post_processing_confirm_wallet_spend(bool success) {
}
}

static void ui_warning_contract_data_choice(bool confirm) {
if (confirm) {
ux_flow_response_false();
} else {
ui_menu_settings();
}
}

void ui_warning_contract_data(void) {
nbgl_useCaseChoice(&C_round_warning_64px,
"This message cannot\nbe clear-signed",
"Enable blind-signing in\nthe settings to sign\nthis transaction.",
"Exit",
"Go to settings",
ui_warning_contract_data_choice);
}

#endif // HAVE_NBGL
5 changes: 5 additions & 0 deletions src/ui/menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ void ui_menu_main_flow_bitcoin(void);
* Show main menu for Testnet (ready screen, version, about, quit).
*/
void ui_menu_main_flow_bitcoin_testnet(void);

/**
* Show settings submenu
*/
void ui_menu_settings(void);
6 changes: 5 additions & 1 deletion src/ui/menu_bagl.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ UX_STEP_NOCB(ux_menu_ready_step_bitcoin_testnet,

UX_STEP_NOCB(ux_menu_version_step, bn, {"Version", APPVERSION});
UX_STEP_CB(ux_menu_about_step, pb, ui_menu_about(), {&C_icon_certificate, "About"});
UX_STEP_CB(ux_menu_settings_step, pb, display_settings(NULL), {&C_icon_eye,"Settings"});
UX_STEP_CB(ux_menu_settings_step, pb, ui_menu_settings(), {&C_icon_eye,"Settings"});
UX_STEP_VALID(ux_menu_exit_step, pb, os_sched_exit(-1), {&C_icon_dashboard_x, "Quit"});

// FLOW for the main menu (for bitcoin):
Expand Down Expand Up @@ -91,6 +91,10 @@ void ui_menu_about(void) {
ux_flow_init(0, ux_menu_about_flow, NULL);
}

void ui_menu_settings(void) {
display_settings(NULL);
}

#define ENABLED_STR "Enabled"
#define DISABLED_STR "Disabled"
#define BUF_INCREMENT (MAX(strlen(ENABLED_STR), strlen(DISABLED_STR)) + 1)
Expand Down
60 changes: 53 additions & 7 deletions src/ui/menu_nbgl.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,54 @@

#include "../globals.h"
#include "menu.h"
#include "display.h"

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

enum {
BLIND_SIGNING_TOKEN = FIRST_USER_TOKEN,
};

static nbgl_layoutSwitch_t switches[1];

static void controls_call_back(int token, uint8_t index) {
(void) index;
uint8_t value;
switch (token) {
case BLIND_SIGNING_TOKEN:
value = (N_storage.dataAllowed ? 0 : 1);
nvm_write((void*) &N_storage.dataAllowed, (void*) &value, sizeof(uint8_t));
break;
}
}

static bool navigation_cb(uint8_t page, nbgl_pageContent_t* content) {
UNUSED(page);
content->type = INFOS_LIST;
content->infosList.nbInfos = 3;
content->infosList.infoTypes = (const char**) infoTypes;
content->infosList.infoContents = (const char**) infoContents;
uint8_t index = 0;
switch (page) {
case 0:
content->type = INFOS_LIST;
content->infosList.nbInfos = 3;
content->infosList.infoTypes = (const char**) infoTypes;
content->infosList.infoContents = (const char**) infoContents;
break;

case 1:
switches[index++] =
(nbgl_layoutSwitch_t){.initState = N_storage.dataAllowed ? ON_STATE : OFF_STATE,
.text = "Blind signing",
.subText = "Enable transaction blind\nsigning",
.token = BLIND_SIGNING_TOKEN,
.tuneId = TUNE_TAP_CASUAL};
content->type = SWITCHES_LIST;
content->switchesList.nbSwitches = index;
content->switchesList.switches = (nbgl_layoutSwitch_t*) switches;
break;

default:
return false;
break;
}
return true;
}

Expand All @@ -51,10 +89,18 @@ void ui_menu_main_flow_bitcoin_testnet(void) {
}

void ui_menu_about(void) {
nbgl_useCaseSettings("Qtum", 0, 1, false, ui_menu_main, navigation_cb, NULL);
nbgl_useCaseSettings("Qtum", 0, 2, false, ui_menu_main, navigation_cb, controls_call_back);
}

void ui_menu_about_testnet(void) {
nbgl_useCaseSettings("Qtum Test", 0, 1, false, ui_menu_main, navigation_cb, NULL);
nbgl_useCaseSettings("Qtum Test", 0, 2, false, ui_menu_main, navigation_cb, controls_call_back);
}

void settings_call_back(void) {
set_ux_flow_response(N_storage.dataAllowed);
}

void ui_menu_settings(void) {
nbgl_useCaseSettings("Qtum", 1, 2, false, settings_call_back, navigation_cb, controls_call_back);
}
#endif // HAVE_NBGL

0 comments on commit e0ba4b3

Please sign in to comment.