Files
app-ethereum/src_nbgl/ui_approve_tx.c

246 lines
7.9 KiB
C
Raw Permalink Normal View History

2023-06-05 17:54:19 +02:00
#include <ctype.h>
2024-06-21 18:12:26 +02:00
#include "nbgl_page.h"
2023-02-21 11:01:18 +01:00
#include "shared_context.h"
#include "ui_callbacks.h"
#include "ui_nbgl.h"
#include "ui_signing.h"
2023-02-21 11:01:18 +01:00
#include "plugins.h"
2023-05-05 11:54:39 +02:00
#include "domain_name.h"
#include "caller_api.h"
#include "network_icons.h"
2024-01-15 18:48:48 +01:00
#include "network.h"
#include "ledger_assert.h"
2023-02-21 11:01:18 +01:00
// 1 more than actually displayed on screen, because of calculations in StaticReview
#define MAX_PLUGIN_ITEMS 8
#define TAG_MAX_LEN 43
#define VALUE_MAX_LEN 79
#define MAX_PAIRS 12 // Max 10 for plugins + 2 (Network and fees)
2023-02-21 11:01:18 +01:00
static nbgl_contentTagValue_t pairs[MAX_PAIRS];
static nbgl_contentTagValueList_t pairsList;
2023-02-21 11:01:18 +01:00
// these buffers are used as circular
static char title_buffer[MAX_PLUGIN_ITEMS][TAG_MAX_LEN];
static char msg_buffer[MAX_PLUGIN_ITEMS][VALUE_MAX_LEN];
2023-02-21 11:01:18 +01:00
struct tx_approval_context_t {
bool fromPlugin;
bool displayNetwork;
2023-05-05 11:54:39 +02:00
#ifdef HAVE_DOMAIN_NAME
bool domain_name_match;
#endif
2023-02-21 11:01:18 +01:00
};
static struct tx_approval_context_t tx_approval_context;
static void reviewReject(void) {
io_seproxyhal_touch_tx_cancel(NULL);
2023-05-05 11:54:39 +02:00
memset(&tx_approval_context, 0, sizeof(tx_approval_context));
2023-02-21 11:01:18 +01:00
}
static void confirmTransation(void) {
io_seproxyhal_touch_tx_ok(NULL);
2023-05-05 11:54:39 +02:00
memset(&tx_approval_context, 0, sizeof(tx_approval_context));
2023-02-21 11:01:18 +01:00
}
static void reviewChoice(bool confirm) {
if (confirm) {
nbgl_useCaseReviewStatus(STATUS_TYPE_TRANSACTION_SIGNED, confirmTransation);
2023-02-21 11:01:18 +01:00
} else {
nbgl_useCaseReviewStatus(STATUS_TYPE_TRANSACTION_REJECTED, reviewReject);
2023-02-21 11:01:18 +01:00
}
}
const nbgl_icon_details_t *get_tx_icon(void) {
const nbgl_icon_details_t *icon = NULL;
if (tx_approval_context.fromPlugin && (pluginType == EXTERNAL)) {
if ((caller_app != NULL) && (caller_app->name != NULL)) {
if (strcmp(strings.common.toAddress, caller_app->name) == 0) {
icon = get_app_icon(true);
}
}
// icon is NULL in this case
// Check with Alex if this is expected or a bug
} else if ((caller_app != NULL) && !tx_approval_context.fromPlugin) {
// Clone case
icon = get_app_icon(true);
} else {
uint64_t chain_id = get_tx_chain_id();
if (chain_id == chainConfig->chainId) {
icon = get_app_icon(false);
} else {
icon = get_network_icon_from_chain_id(&chain_id);
}
}
return icon;
}
// Force operation to be lowercase
static void get_lowercase_operation(char *dst, size_t dst_len) {
const char *src = strings.common.fullAmount;
size_t idx;
for (idx = 0; (idx < dst_len - 1) && (src[idx] != '\0'); ++idx) {
dst[idx] = (char) tolower((int) src[idx]);
}
dst[idx] = '\0';
}
static uint8_t setTagValuePairs(void) {
2023-02-21 11:01:18 +01:00
uint8_t nbPairs = 0;
uint8_t pairIndex = 0;
uint8_t counter = 0;
explicit_bzero(pairs, sizeof(pairs));
2023-02-21 11:01:18 +01:00
// Setup data to display
2023-02-21 11:01:18 +01:00
if (tx_approval_context.fromPlugin) {
2024-06-19 11:24:13 +02:00
if (pluginType != EXTERNAL) {
if (strings.common.fromAddress[0] != 0) {
pairs[nbPairs].item = "From";
pairs[nbPairs].value = strings.common.fromAddress;
nbPairs++;
}
}
for (pairIndex = 0; pairIndex < dataContext.tokenContext.pluginUiMaxItems; pairIndex++) {
// for the next dataContext.tokenContext.pluginUiMaxItems items, get tag/value from
// plugin_ui_get_item_internal()
dataContext.tokenContext.pluginUiCurrentItem = pairIndex;
plugin_ui_get_item_internal((uint8_t *) title_buffer[counter],
TAG_MAX_LEN,
(uint8_t *) msg_buffer[counter],
VALUE_MAX_LEN);
pairs[nbPairs].item = title_buffer[counter];
pairs[nbPairs].value = msg_buffer[counter];
2023-02-21 11:01:18 +01:00
nbPairs++;
LEDGER_ASSERT((++counter < MAX_PLUGIN_ITEMS), "Too many items for plugin\n");
2023-02-21 11:01:18 +01:00
}
// for the last 1 (or 2), tags are fixed
if (tx_approval_context.displayNetwork) {
pairs[nbPairs].item = "Network";
pairs[nbPairs].value = strings.common.network_name;
nbPairs++;
}
pairs[nbPairs].item = "Max fees";
pairs[nbPairs].value = strings.common.maxFee;
nbPairs++;
} else {
2024-03-11 16:14:31 +01:00
if (strings.common.fromAddress[0] != 0) {
pairs[nbPairs].item = "From";
pairs[nbPairs].value = strings.common.fromAddress;
nbPairs++;
}
2024-06-18 11:57:00 +02:00
pairs[nbPairs].item = "Amount";
pairs[nbPairs].value = strings.common.fullAmount;
nbPairs++;
2023-05-05 11:54:39 +02:00
#ifdef HAVE_DOMAIN_NAME
uint64_t chain_id = get_tx_chain_id();
tx_approval_context.domain_name_match =
has_domain_name(&chain_id, tmpContent.txContent.destination);
if (tx_approval_context.domain_name_match) {
2024-06-18 11:57:00 +02:00
pairs[nbPairs].item = "To (domain)";
pairs[nbPairs].value = g_domain_name;
nbPairs++;
2023-05-05 11:54:39 +02:00
}
if (!tx_approval_context.domain_name_match || N_storage.verbose_domain_name) {
#endif
2024-03-11 16:14:31 +01:00
pairs[nbPairs].item = "To";
pairs[nbPairs].value = strings.common.toAddress;
nbPairs++;
#ifdef HAVE_DOMAIN_NAME
}
#endif
if (N_storage.displayNonce) {
pairs[nbPairs].item = "Nonce";
pairs[nbPairs].value = strings.common.nonce;
nbPairs++;
}
2024-06-19 11:24:13 +02:00
pairs[nbPairs].item = "Max fees";
pairs[nbPairs].value = strings.common.maxFee;
nbPairs++;
if (tx_approval_context.displayNetwork) {
pairs[nbPairs].item = "Network";
pairs[nbPairs].value = strings.common.network_name;
nbPairs++;
}
}
return nbPairs;
2023-02-21 11:01:18 +01:00
}
static void reviewCommon(void) {
explicit_bzero(&pairsList, sizeof(pairsList));
2023-06-05 17:54:19 +02:00
pairsList.nbPairs = setTagValuePairs();
pairsList.pairs = pairs;
2024-06-21 18:12:26 +02:00
nbgl_operationType_t op = TYPE_TRANSACTION;
2023-06-05 17:54:19 +02:00
2024-06-25 13:55:27 +02:00
#if API_LEVEL >= 19
2024-06-21 18:12:26 +02:00
if (tmpContent.txContent.dataPresent) {
op |= BLIND_OPERATION;
}
2024-06-25 13:55:27 +02:00
#endif
2023-02-21 11:01:18 +01:00
if (tx_approval_context.fromPlugin) {
uint32_t buf_size = SHARED_BUFFER_SIZE / 2;
2023-06-05 17:54:19 +02:00
char op_name[sizeof(strings.common.fullAmount)];
2023-02-21 11:01:18 +01:00
plugin_ui_get_id();
2023-06-05 17:54:19 +02:00
get_lowercase_operation(op_name, sizeof(op_name));
snprintf(g_stax_shared_buffer,
buf_size,
"Review transaction\nto %s\n%s%s",
op_name,
(pluginType == EXTERNAL ? "on " : ""),
2024-03-11 16:14:31 +01:00
strings.common.toAddress);
// Finish text: replace "Review" by "Sign" and add questionmark
snprintf(g_stax_shared_buffer + buf_size,
buf_size,
"Sign transaction\nto %s\n%s%s",
op_name,
(pluginType == EXTERNAL ? "on " : ""),
2024-03-11 16:14:31 +01:00
strings.common.toAddress);
2024-06-21 18:12:26 +02:00
nbgl_useCaseReview(op,
&pairsList,
get_tx_icon(),
g_stax_shared_buffer,
NULL,
g_stax_shared_buffer + buf_size,
reviewChoice);
} else {
2024-06-21 18:12:26 +02:00
nbgl_useCaseReview(op,
&pairsList,
get_tx_icon(),
REVIEW("transaction"),
NULL,
SIGN("transaction"),
reviewChoice);
}
}
void blind_confirm_cb(bool confirm) {
if (confirm) {
reviewCommon();
2023-02-21 11:01:18 +01:00
} else {
reviewReject();
2023-02-21 11:01:18 +01:00
}
}
void ux_approve_tx(bool fromPlugin) {
memset(&tx_approval_context, 0, sizeof(tx_approval_context));
2023-02-21 11:01:18 +01:00
tx_approval_context.fromPlugin = fromPlugin;
tx_approval_context.displayNetwork = false;
uint64_t chain_id = get_tx_chain_id();
if (chainConfig->chainId == ETHEREUM_MAINNET_CHAINID && chain_id != chainConfig->chainId) {
tx_approval_context.displayNetwork = true;
}
2024-06-21 18:12:26 +02:00
reviewCommon();
2023-02-21 11:01:18 +01:00
}