Skip to content

Commit

Permalink
added changes for issue #11
Browse files Browse the repository at this point in the history
  • Loading branch information
ypopovych committed May 4, 2024
1 parent 47d236c commit d5abacf
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/commands/deriveaddress/da_ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ int ui_display_address(derive_address_ctx_t* ctx,
bip32_path_len,
"Path",
ctx->bip32_path,
MEMBER_SIZE(derive_address_ctx_t, bip32_path));
MEMBER_SIZE(derive_address_ctx_t, bip32_path),
NULL,
NULL);
if (b32_screen == NULL) {
return send_error(SW_BIP32_FORMATTING_FAILED);
}
Expand Down
4 changes: 3 additions & 1 deletion src/commands/extpubkey/epk_ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ int ui_display_account(extended_public_key_ctx_t* ctx,
bip32_path_len,
"Path",
ctx->bip32_path,
MEMBER_SIZE(extended_public_key_ctx_t, bip32_path));
MEMBER_SIZE(extended_public_key_ctx_t, bip32_path),
NULL,
NULL);
if (b32_step == NULL) {
app_set_current_command(CMD_NONE);
return res_error(SW_BIP32_FORMATTING_FAILED);
Expand Down
27 changes: 26 additions & 1 deletion src/commands/signtx/operations/stx_op_p2pk.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,33 @@ bool stx_operation_p2pk_should_show_output_confirm_screen(
SIGN_TRANSACTION_OUTPUT_INFO_TYPE_MINERS_FEE) {
return stx_output_info_used_tokens_count(&ctx->transaction.ui.output) > 0;
}
// Change address
if (stx_output_info_type(&ctx->transaction.ui.output) ==
SIGN_TRANSACTION_OUTPUT_INFO_TYPE_BIP32) {
// if path length is not 5 or wrong type then we should ask to confirm
if (!bip32_path_validate(ctx->transaction.ui.output.bip32_path.path,
ctx->transaction.ui.output.bip32_path.len,
BIP32_HARDENED(44),
BIP32_HARDENED(BIP32_ERGO_COIN),
BIP32_PATH_VALIDATE_ADDRESS_E5)) return true;
// if change index is < 20 then we approve it automatically
if (ctx->transaction.ui.output.bip32_path.path[4] < 20) return false;
// Check was it already approved then approve automatically
if (memcmp(&ctx->transaction.ui.output.bip32_path,
&ctx->transaction.last_approved_change,
sizeof(sign_transaction_bip32_path_t)) == 0) return false;
}
return true;
}

// ===========================
// UI

static NOINLINE void ui_stx_operation_p2pk_approve_action(void* context) {
sign_transaction_ui_aprove_ctx_t* ctx = (sign_transaction_ui_aprove_ctx_t*) context;
ui_stx_operation_approve_reject(true, ctx);
}

uint16_t ui_stx_operation_p2pk_show_token_and_path(sign_transaction_operation_p2pk_ctx_t *ctx,
uint32_t app_access_token,
bool is_known_application,
Expand All @@ -311,7 +333,9 @@ uint16_t ui_stx_operation_p2pk_show_token_and_path(sign_transaction_operation_p2
ctx->bip32.len,
"P2PK Signing",
ctx->ui_approve.bip32_path,
MEMBER_SIZE(sign_transaction_operation_p2pk_ui_approve_data_ctx_t, bip32_path));
MEMBER_SIZE(sign_transaction_operation_p2pk_ui_approve_data_ctx_t, bip32_path),
ui_stx_operation_p2pk_approve_action,
&ctx->ui_approve.ui_approve);
if (b32_step == NULL) {
return SW_BIP32_FORMATTING_FAILED;
}
Expand Down Expand Up @@ -339,6 +363,7 @@ uint16_t ui_stx_operation_p2pk_show_output_confirm_screen(
if (!ui_stx_add_output_screens(&ctx->transaction.ui.ui,
&screen,
&ctx->transaction.ui.output,
&ctx->transaction.last_approved_change,
ctx->network_id)) {
return SW_SCREENS_BUFFER_OVERFLOW;
}
Expand Down
1 change: 1 addition & 0 deletions src/commands/signtx/operations/stx_op_p2pk.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ typedef enum {
typedef struct {
ergo_tx_serializer_full_context_t tx;
sign_transaction_operation_p2pk_ui_output_info_ctx_t ui;
sign_transaction_bip32_path_t last_approved_change;
} sign_transaction_operation_p2pk_transaction_ctx_t;

typedef struct {
Expand Down
1 change: 1 addition & 0 deletions src/commands/signtx/stx_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ typedef struct {
char text[70]; // dynamic screen text
uint8_t network_id;
const sign_transaction_output_info_ctx_t *output;
sign_transaction_bip32_path_t *last_approved_change;
} sign_transaction_ui_output_confirm_ctx_t;

// Show screen callback: (index, title, title_len, text, text_len, cb_context)
Expand Down
24 changes: 20 additions & 4 deletions src/commands/signtx/stx_ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ static inline bool format_erg_amount(uint64_t amount, char* out, size_t out_len)

// ----- OPERATION APPROVE / REJECT FLOW

static NOINLINE void ui_stx_operation_approve_action(bool approved, void* context) {
sign_transaction_ui_aprove_ctx_t* ctx = (sign_transaction_ui_aprove_ctx_t*) context;
void ui_stx_operation_approve_reject(bool approved, sign_transaction_ui_aprove_ctx_t* ctx) {
sign_transaction_ctx_t* sign_tx = (sign_transaction_ctx_t*) ctx->sign_tx_context;

app_set_ui_busy(false);
Expand All @@ -84,6 +83,11 @@ static NOINLINE void ui_stx_operation_approve_action(bool approved, void* contex
ui_menu_main();
}

static NOINLINE void ui_stx_operation_approve_action(bool approved, void* context) {
sign_transaction_ui_aprove_ctx_t* ctx = (sign_transaction_ui_aprove_ctx_t*) context;
ui_stx_operation_approve_reject(approved, ctx);
}

bool ui_stx_add_operation_approve_screens(sign_transaction_ui_aprove_ctx_t* ctx,
uint8_t* screen,
uint32_t app_access_token,
Expand Down Expand Up @@ -214,10 +218,19 @@ static NOINLINE uint16_t ui_stx_display_output_state(uint8_t screen,
}

static NOINLINE void ui_stx_operation_output_confirm_action(bool approved, void* context) {
UNUSED(context);
sign_transaction_ui_output_confirm_ctx_t* ctx =
(sign_transaction_ui_output_confirm_ctx_t*) context;
app_set_ui_busy(false);

explicit_bzero(ctx->last_approved_change, sizeof(sign_transaction_bip32_path_t));

if (approved) {
// store last approved change address
if (stx_output_info_type(ctx->output) == SIGN_TRANSACTION_OUTPUT_INFO_TYPE_BIP32) {
memmove(ctx->last_approved_change,
&ctx->output->bip32_path,
sizeof(sign_transaction_bip32_path_t));
}
res_ok();
} else {
app_set_current_command(CMD_NONE);
Expand All @@ -230,10 +243,12 @@ static NOINLINE void ui_stx_operation_output_confirm_action(bool approved, void*
bool ui_stx_add_output_screens(sign_transaction_ui_output_confirm_ctx_t* ctx,
uint8_t* screen,
const sign_transaction_output_info_ctx_t* output,
sign_transaction_bip32_path_t* last_approved_change,
uint8_t network_id) {
if (MAX_NUMBER_OF_SCREENS - *screen < 6) return false;

memset(ctx, 0, sizeof(sign_transaction_ui_output_confirm_ctx_t));
memset(last_approved_change, 0, sizeof(sign_transaction_bip32_path_t));

ui_add_screen(&ux_stx_display_output_confirm_step, screen);

Expand All @@ -254,12 +269,13 @@ bool ui_stx_add_output_screens(sign_transaction_ui_output_confirm_ctx_t* ctx,
if (MAX_NUMBER_OF_SCREENS - *screen < 2) return false;

ui_approve_reject_screens(ui_stx_operation_output_confirm_action,
NULL,
(void*) ctx,
ui_next_sreen_ptr(screen),
ui_next_sreen_ptr(screen));

ctx->network_id = network_id;
ctx->output = output;
ctx->last_approved_change = last_approved_change;

return true;
}
Expand Down
9 changes: 8 additions & 1 deletion src/commands/signtx/stx_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ bool ui_stx_add_operation_approve_screens(sign_transaction_ui_aprove_ctx_t* ctx,
bool ui_stx_add_output_screens(sign_transaction_ui_output_confirm_ctx_t* ctx,
uint8_t* screen,
const sign_transaction_output_info_ctx_t* output,
sign_transaction_bip32_path_t* last_approved_change,
uint8_t network_id);

/**
Expand All @@ -51,4 +52,10 @@ bool ui_stx_add_transaction_screens(sign_transaction_ui_sign_confirm_ctx_t* ctx,
* @return true if success, false if screens buffer is full.
*
*/
bool ui_stx_display_screens(uint8_t screen_count);
bool ui_stx_display_screens(uint8_t screen_count);

/**
* Approve or reject operation programatically.

Check failure on line 58 in src/commands/signtx/stx_ui.h

View workflow job for this annotation

GitHub Actions / Check misspellings

programatically ==> programmatically
*
*/
void ui_stx_operation_approve_reject(bool approved, sign_transaction_ui_aprove_ctx_t* ctx);
27 changes: 25 additions & 2 deletions src/ui/ui_bip32_path.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,47 @@
#include "../common/bip32_ext.h"

static ux_layout_bnnn_paging_params_t G_ui_path_params[1];
static ui_bip32_approve_callback G_ui_bip32_approve_callback;
static void* G_ui_bip32_approve_callback_context;

void ux_bip32_path_validate_init(unsigned int stack_slot) {
UNUSED(stack_slot);
if (G_ui_bip32_approve_callback != NULL) {
G_ui_bip32_approve_callback(G_ui_bip32_approve_callback_context);
} else {
ux_flow_next();
}
}

const ux_flow_step_t ux_bip32_path_validate_step = {
ux_bip32_path_validate_init, NULL, NULL, NULL
};

const ux_flow_step_t *const ux_bip32_path_validate[] = {
&ux_bip32_path_validate_step, FLOW_END_STEP
};

const ux_flow_step_t ux_bip32_path_step = {
ux_layout_bnnn_paging_init,
G_ui_path_params,
NULL,
ux_bip32_path_validate,
NULL,
};

const ux_flow_step_t* ui_bip32_path_screen(uint32_t* path,
uint8_t path_len,
const char* title,
char* buffer,
uint8_t buffer_len) {
uint8_t buffer_len,
ui_bip32_approve_callback cb,
void* cb_context) {
G_ui_path_params[0].title = title;
G_ui_path_params[0].text = buffer;
memset(buffer, 0, buffer_len);
if (!bip32_path_format(path, path_len, buffer, buffer_len)) {
return NULL;
}
G_ui_bip32_approve_callback = cb;
G_ui_bip32_approve_callback_context = cb_context;
return &ux_bip32_path_step;
}
6 changes: 5 additions & 1 deletion src/ui/ui_bip32_path.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
#include <ux.h>
#include <stdint.h>

typedef void (*ui_bip32_approve_callback)(void*);

const ux_flow_step_t* ui_bip32_path_screen(uint32_t* path,
uint8_t path_len,
const char* title,
char* buffer,
uint8_t buffer_len);
uint8_t buffer_len,
ui_bip32_approve_callback cb,
void* cb_context);

0 comments on commit d5abacf

Please sign in to comment.