From a5187b5cbc1001da06177cde832102c01a827fad Mon Sep 17 00:00:00 2001 From: neithanmo Date: Mon, 9 Dec 2024 16:38:29 +0100 Subject: [PATCH] Implement correctly the spend plan ui --- app/src/constants.h | 6 ++++- app/src/plan/spend_plan.c | 54 +++++++++++++++++++++++++++++---------- app/src/plan/spend_plan.h | 2 ++ 3 files changed, 48 insertions(+), 14 deletions(-) diff --git a/app/src/constants.h b/app/src/constants.h index 6f4f75d..7ec5d33 100644 --- a/app/src/constants.h +++ b/app/src/constants.h @@ -75,4 +75,8 @@ // Constant to use to allocate a buffer on the stack to hold the formatting of an output action #define OUTPUT_DISPLAY_MAX_LEN \ - (VALUE_DISPLAY_MAX_LEN + SHORT_ADDRESS_VISIBLE_CHARS + sizeof(ELLIPSIS) + sizeof(ADDR_BECH32_PREFIX) + 6) // = 202 \ No newline at end of file + (VALUE_DISPLAY_MAX_LEN + SHORT_ADDRESS_VISIBLE_CHARS + sizeof(ELLIPSIS) + sizeof(ADDR_BECH32_PREFIX) + 6) // = 202 + +// Constant to use to allocate a buffer on the stack to hold the formatting of an spend action +#define SPEND_DISPLAY_MAX_LEN \ + (VALUE_DISPLAY_MAX_LEN + SHORT_ADDRESS_VISIBLE_CHARS + sizeof(ELLIPSIS) + sizeof(ADDR_BECH32_PREFIX) + 10) // = 202 diff --git a/app/src/plan/spend_plan.c b/app/src/plan/spend_plan.c index b593cee..4199ef2 100644 --- a/app/src/plan/spend_plan.c +++ b/app/src/plan/spend_plan.c @@ -23,6 +23,7 @@ #include "zxformat.h" #include "known_assets.h" #include "note.h" +#include "ui_utils.h" parser_error_t decode_spend_plan(const bytes_t *data, spend_plan_t *output) { penumbra_core_component_shielded_pool_v1_SpendPlan spend_plan = @@ -62,12 +63,43 @@ parser_error_t decode_spend_plan(const bytes_t *data, spend_plan_t *output) { return parser_ok; } + +parser_error_t spend_printValue(const parser_context_t *ctx, const spend_plan_t *spend, char *outVal, uint16_t outValLen) { + if (ctx == NULL || spend == NULL || outVal == NULL) { + return parser_no_data; + } + + if (outValLen < SPEND_DISPLAY_MAX_LEN) { + return parser_unexpected_buffer_end; + } + + MEMZERO(outVal, outValLen); + + // example: Spend 100 USDC to penumbra1k0zzug62gpz60sejdvu9q7mq… + + // add action title + uint16_t written_local = snprintf(outVal, outValLen, "Spend "); + + // add value + CHECK_ERROR(printValue(ctx, &spend->note.value, &ctx->tx_obj->parameters_plan.chain_id, outVal + written_local, outValLen - written_local)); + uint16_t written_value = strlen(outVal); + + // add "from" + written_local = snprintf(outVal + written_value, outValLen - written_value, " from "); + + // add address + CHECK_ERROR(printTxAddress(&spend->note.address.inner, outVal + written_local + written_value, outValLen - written_local - written_value)); + + return parser_ok; +} + parser_error_t spend_getNumItems(const parser_context_t *ctx, uint8_t *num_items) { UNUSED(ctx); // from spends we display only two items: // - Spend 100 USDC // - From Main Account - *num_items = 2; + // all concatenated in a single string + *num_items = 1; return parser_ok; } @@ -82,18 +114,14 @@ parser_error_t spend_getItem(const parser_context_t *ctx, const spend_plan_t *sp } - switch ( displayIdx ) { - case 0: - snprintf(outKey, outKeyLen, "Spend"); - return printValue(ctx, &spend->note.value, &spend->note.value.asset_id.inner, outVal, outValLen); - break; - case 1: - snprintf(outKey, outKeyLen, "From"); - snprintf(outVal, outValLen, "Main Account"); - break; - default: - return parser_no_data; + char bufferUI[OUTPUT_DISPLAY_MAX_LEN] = {0}; + if (displayIdx == 0) { + snprintf(outKey, outKeyLen, "Action"); + CHECK_ERROR(spend_printValue(ctx, spend, bufferUI, sizeof(bufferUI))); + pageString(outVal, outValLen, bufferUI, pageIdx, pageCount); + + return parser_ok; } - return parser_ok; + return parser_no_data; } diff --git a/app/src/plan/spend_plan.h b/app/src/plan/spend_plan.h index 182c7dc..a372a0b 100644 --- a/app/src/plan/spend_plan.h +++ b/app/src/plan/spend_plan.h @@ -32,6 +32,8 @@ extern "C" { parser_error_t decode_spend_plan(const bytes_t *input, spend_plan_t *spend_plan); +parser_error_t spend_printValue(const parser_context_t *ctx, const spend_plan_t *spend, char *outVal, uint16_t outValLen); + parser_error_t spend_getNumItems(const parser_context_t *ctx, uint8_t *num_items); parser_error_t spend_getItem(const parser_context_t *ctx, const spend_plan_t *spend, const address_index_t *addr_index, uint8_t displayIdx, char *outKey, uint16_t outKeyLen,