Skip to content

Commit

Permalink
Move ui_stx_display_output_state and ui_stx_display_tx_state to stx_u…
Browse files Browse the repository at this point in the history
…i_common.c
  • Loading branch information
steel97 committed Oct 17, 2024
1 parent 7f336f5 commit dace382
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 110 deletions.
113 changes: 113 additions & 0 deletions src/commands/signtx/stx_ui_common.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include <os.h>
#include <string.h>
#include <format.h>
#include <base58.h>

#include "stx_ui_common.h"

#include "../../ergo/address.h"

uint16_t ui_stx_display_output_state(uint8_t screen, char* title, char* text, void* context) {
sign_transaction_ui_output_confirm_ctx_t* ctx =
(sign_transaction_ui_output_confirm_ctx_t*) context;
uint8_t title_len = MEMBER_SIZE(sign_transaction_ui_output_confirm_ctx_t, title);
uint8_t text_len = MEMBER_SIZE(sign_transaction_ui_output_confirm_ctx_t, text);
memset(title, 0, title_len);
memset(text, 0, text_len);

switch (screen) {
case 0: // Output Address Info
return output_info_print_address(ctx->output,
ctx->network_id,
title,
title_len,
text,
text_len);
case 1: { // Output Value
strncpy(title, "Output Value", title_len);
if (!format_erg_amount(ctx->output->value, text, text_len)) {
return SW_BUFFER_ERROR;
}
break;
}
default: { // Tokens
screen -= 2; // Decrease index for info screens
uint8_t token_idx = stx_output_info_used_token_index(ctx->output, screen / 2);
if (!IS_ELEMENT_FOUND(token_idx)) { // error. bad index state
return SW_BAD_TOKEN_INDEX;
}
if (screen % 2 == 0) { // Token ID
snprintf(title, title_len, "Token [%d]", (int) (screen / 2) + 1);
if (!format_hex_id(ctx->output->tokens_table->tokens[token_idx],
ERGO_ID_LEN,
text,
text_len)) {
return SW_ADDRESS_FORMATTING_FAILED;
}
} else { // Token Value
snprintf(title, title_len, "Token [%d] Value", (int) (screen / 2) + 1);
format_u64(text, text_len, ctx->output->tokens[token_idx]);
}
break;
}
}
return SW_OK;
}

// Callback for TX UI rendering
uint16_t ui_stx_display_tx_state(uint8_t screen, char* title, char* text, void* context) {
sign_transaction_ui_sign_confirm_ctx_t* ctx = (sign_transaction_ui_sign_confirm_ctx_t*) context;
uint8_t title_len = MEMBER_SIZE(sign_transaction_ui_sign_confirm_ctx_t, title);
uint8_t text_len = MEMBER_SIZE(sign_transaction_ui_sign_confirm_ctx_t, text);
memset(title, 0, title_len);
memset(text, 0, text_len);

if (screen < ctx->op_screen_count) { // Showing operation screen
return ctx->op_screen_cb(screen, title, title_len, text, text_len, ctx->op_cb_context);
}
screen -= ctx->op_screen_count;
switch (screen) {
case 0: { // TX Value
strncpy(title, "Transaction Amount", title_len);
if (!format_erg_amount(ctx->amounts->value, text, text_len)) {
return SW_BUFFER_ERROR;
}
break;
}
case 1: { // TX Fee
strncpy(title, "Transaction Fee", title_len);
if (!format_erg_amount(ctx->amounts->fee, text, text_len)) {
return SW_BUFFER_ERROR;
}
break;
}
default: { // Tokens
screen -= 2; // Decrease index for info screens
uint8_t token_idx = stx_amounts_non_zero_token_index(ctx->amounts, screen / 2);
if (!IS_ELEMENT_FOUND(token_idx)) { // error. bad index state
return SW_BAD_TOKEN_INDEX;
}
if (screen % 2 == 0) { // Token ID
snprintf(title, title_len, "Token [%d]", (int) (screen / 2) + 1);
if (!format_hex_id(ctx->amounts->tokens_table.tokens[token_idx],
ERGO_ID_LEN,
text,
text_len)) {
return SW_ADDRESS_FORMATTING_FAILED;
}
} else { // Token Value
snprintf(title, title_len, "Token [%d] Value", (int) (screen / 2) + 1);
int64_t value = ctx->amounts->tokens[token_idx];
if (value < 0) { // output > inputs
STRING_ADD_STATIC_TEXT(text, text_len, "Minting: ");
format_u64(text, text_len, -value);
} else { // inputs > outputs
STRING_ADD_STATIC_TEXT(text, text_len, "Burning: ");
format_u64(text, text_len, value);
}
}
break;
}
}
return SW_OK;
}
114 changes: 4 additions & 110 deletions src/commands/signtx/stx_ui_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
#include <format.h>
#include <base58.h>

#include "stx_ui.h"

#include "../../ergo/address.h"

#include "stx_context.h"

#define ERGO_ID_UI_CHARACTERS_HALF 26
#define STRING_ADD_STATIC_TEXT(str, slen, text) \
strncpy(str, text, slen); \
Expand Down Expand Up @@ -102,113 +102,7 @@ static inline uint16_t output_info_print_address(const sign_transaction_output_i
return SW_OK;
}

static NOINLINE uint16_t ui_stx_display_output_state(uint8_t screen,
char* title,
char* text,
void* context) {
sign_transaction_ui_output_confirm_ctx_t* ctx =
(sign_transaction_ui_output_confirm_ctx_t*) context;
uint8_t title_len = MEMBER_SIZE(sign_transaction_ui_output_confirm_ctx_t, title);
uint8_t text_len = MEMBER_SIZE(sign_transaction_ui_output_confirm_ctx_t, text);
memset(title, 0, title_len);
memset(text, 0, text_len);

switch (screen) {
case 0: // Output Address Info
return output_info_print_address(ctx->output,
ctx->network_id,
title,
title_len,
text,
text_len);
case 1: { // Output Value
strncpy(title, "Output Value", title_len);
if (!format_erg_amount(ctx->output->value, text, text_len)) {
return SW_BUFFER_ERROR;
}
break;
}
default: { // Tokens
screen -= 2; // Decrease index for info screens
uint8_t token_idx = stx_output_info_used_token_index(ctx->output, screen / 2);
if (!IS_ELEMENT_FOUND(token_idx)) { // error. bad index state
return SW_BAD_TOKEN_INDEX;
}
if (screen % 2 == 0) { // Token ID
snprintf(title, title_len, "Token [%d]", (int) (screen / 2) + 1);
if (!format_hex_id(ctx->output->tokens_table->tokens[token_idx],
ERGO_ID_LEN,
text,
text_len)) {
return SW_ADDRESS_FORMATTING_FAILED;
}
} else { // Token Value
snprintf(title, title_len, "Token [%d] Value", (int) (screen / 2) + 1);
format_u64(text, text_len, ctx->output->tokens[token_idx]);
}
break;
}
}
return SW_OK;
}
uint16_t ui_stx_display_output_state(uint8_t screen, char* title, char* text, void* context);

// Callback for TX UI rendering
static NOINLINE uint16_t ui_stx_display_tx_state(uint8_t screen,
char* title,
char* text,
void* context) {
sign_transaction_ui_sign_confirm_ctx_t* ctx = (sign_transaction_ui_sign_confirm_ctx_t*) context;
uint8_t title_len = MEMBER_SIZE(sign_transaction_ui_sign_confirm_ctx_t, title);
uint8_t text_len = MEMBER_SIZE(sign_transaction_ui_sign_confirm_ctx_t, text);
memset(title, 0, title_len);
memset(text, 0, text_len);

if (screen < ctx->op_screen_count) { // Showing operation screen
return ctx->op_screen_cb(screen, title, title_len, text, text_len, ctx->op_cb_context);
}
screen -= ctx->op_screen_count;
switch (screen) {
case 0: { // TX Value
strncpy(title, "Transaction Amount", title_len);
if (!format_erg_amount(ctx->amounts->value, text, text_len)) {
return SW_BUFFER_ERROR;
}
break;
}
case 1: { // TX Fee
strncpy(title, "Transaction Fee", title_len);
if (!format_erg_amount(ctx->amounts->fee, text, text_len)) {
return SW_BUFFER_ERROR;
}
break;
}
default: { // Tokens
screen -= 2; // Decrease index for info screens
uint8_t token_idx = stx_amounts_non_zero_token_index(ctx->amounts, screen / 2);
if (!IS_ELEMENT_FOUND(token_idx)) { // error. bad index state
return SW_BAD_TOKEN_INDEX;
}
if (screen % 2 == 0) { // Token ID
snprintf(title, title_len, "Token [%d]", (int) (screen / 2) + 1);
if (!format_hex_id(ctx->amounts->tokens_table.tokens[token_idx],
ERGO_ID_LEN,
text,
text_len)) {
return SW_ADDRESS_FORMATTING_FAILED;
}
} else { // Token Value
snprintf(title, title_len, "Token [%d] Value", (int) (screen / 2) + 1);
int64_t value = ctx->amounts->tokens[token_idx];
if (value < 0) { // output > inputs
STRING_ADD_STATIC_TEXT(text, text_len, "Minting: ");
format_u64(text, text_len, -value);
} else { // inputs > outputs
STRING_ADD_STATIC_TEXT(text, text_len, "Burning: ");
format_u64(text, text_len, value);
}
}
break;
}
}
return SW_OK;
}
uint16_t ui_stx_display_tx_state(uint8_t screen, char* title, char* text, void* context);

0 comments on commit dace382

Please sign in to comment.