Skip to content

Commit

Permalink
Doc: documents delegation files
Browse files Browse the repository at this point in the history
  • Loading branch information
spalmer25 committed Mar 7, 2024
1 parent bae5635 commit 89714bb
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 77 deletions.
2 changes: 1 addition & 1 deletion src/apdu_sign.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ size_t baking_sign_complete(bool const send_hash, volatile uint32_t *flags) {
0) {
ui_callback_t const ok_c =
send_hash ? sign_with_hash_ok : sign_without_hash_ok;
prompt_register_delegate(ok_c, sign_reject);
prompt_delegation(ok_c, sign_reject);
*flags = IO_ASYNCH_REPLY;
result = 0;
} else {
Expand Down
14 changes: 13 additions & 1 deletion src/ui_delegation.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,16 @@

#include "types.h"

void prompt_register_delegate(ui_callback_t const ok_cb, ui_callback_t const cxl_cb);
/**
* @brief Draws delegation confirmation pages flow
*
* - Initial screen
* - Values:
* - Address
* - Fee
* - Confirmation screens
*
* @param ok_cb: accept callback
* @param cxl_cb: reject callback
*/
void prompt_delegation(ui_callback_t const ok_cb, ui_callback_t const cxl_cb);
23 changes: 3 additions & 20 deletions src/ui_delegation_bagl.c
Original file line number Diff line number Diff line change
@@ -1,37 +1,20 @@
#ifdef HAVE_BAGL
#include "apdu_sign.h"

#include "apdu.h"
#include "baking_auth.h"
#include "ui_delegation.h"

#include "globals.h"
#include "keys.h"
#include "memory.h"
#include "protocol.h"
#include "to_string.h"
#include "ui.h"
#include "cx.h"

#include <string.h>

#define G global.apdu.u.sign

#define PARSE_ERROR() THROW(EXC_PARSE_ERROR)

#define B2B_BLOCKBYTES 128

__attribute__((noreturn)) void prompt_register_delegate(ui_callback_t const ok_cb,
ui_callback_t const cxl_cb) {
if (!G.maybe_ops.is_valid) {
THROW(EXC_MEMORY_ERROR);
}

void prompt_delegation(ui_callback_t const ok_cb, ui_callback_t const cxl_cb) {
init_screen_stack();
push_ui_callback("Register", copy_string, "as delegate?");
push_ui_callback("Address", bip32_path_with_curve_to_pkh_string, &global.path_with_curve);
push_ui_callback("Fee", microtez_to_string_indirect, &G.maybe_ops.v.total_fee);

ux_confirm_screen(ok_cb, cxl_cb);
__builtin_unreachable();
}

#endif // HAVE_BAGL
118 changes: 63 additions & 55 deletions src/ui_delegation_nbgl.c
Original file line number Diff line number Diff line change
@@ -1,50 +1,61 @@
#ifdef HAVE_NBGL
#include "nbgl_use_case.h"
#include "apdu_sign.h"

#include "apdu.h"
#include "baking_auth.h"
#include "ui_delegation.h"

#include "globals.h"
#include "keys.h"
#include "memory.h"
#include "protocol.h"
#include "to_string.h"
#include "ui.h"
#include "cx.h"

#include <string.h>
#include "nbgl_use_case.h"

#define G global.apdu.u.sign

#define MAX_LENGTH 100
#define BUFFER_SIZE 100

#define NUMBER_VALUES 2u /// Number of values displayed

#define PARSE_ERROR() THROW(EXC_PARSE_ERROR)

/**
* @brief This structure represents a context needed for delegation screens navigation
*
*/
typedef struct {
ui_callback_t ok_cb;
ui_callback_t cxl_cb;
nbgl_layoutTagValue_t tagValuePair[NUMBER_VALUES];
ui_callback_t ok_cb; /// accept callback
ui_callback_t cxl_cb; /// cancel callback
nbgl_layoutTagValue_t tagValues[NUMBER_VALUES];
nbgl_layoutTagValueList_t tagValueList;
nbgl_pageInfoLongPress_t infoLongPress;
const char* confirmed_status;
const char* cancelled_status;
char buffer[NUMBER_VALUES][MAX_LENGTH];
} TransactionContext_t;

static TransactionContext_t transactionContext;

char buffer[NUMBER_VALUES][BUFFER_SIZE]; /// values buffers;
} DelegationContext_t;

/**
* @brief Current delegation context
*
*/
static DelegationContext_t delegation_context;

/**
* @brief Callback called when delegation is rejected
*
*/
static void cancel_callback(void) {
nbgl_useCaseStatus(transactionContext.cancelled_status, false, ui_initial_screen);
transactionContext.cxl_cb();
nbgl_useCaseStatus("Delegate registration\ncancelled", false, ui_initial_screen);
delegation_context.cxl_cb();
}

/**
* @brief Callback called when delegation is approved
*
*/
static void approve_callback(void) {
nbgl_useCaseStatus(transactionContext.confirmed_status, true, ui_initial_screen);
transactionContext.ok_cb();
nbgl_useCaseStatus("DELEGATE\nCONFIRMED", true, ui_initial_screen);
delegation_context.ok_cb();
}

/**
* @brief Callback called when delegation is accepted or cancelled
*
* @param confirm: true if accepted, false if cancelled
*/
static void confirmation_callback(bool confirm) {
if (confirm) {
approve_callback();
Expand All @@ -53,59 +64,56 @@ static void confirmation_callback(bool confirm) {
}
}

static void continue_light_callback(void) {
transactionContext.infoLongPress.icon = &C_tezos;
transactionContext.infoLongPress.longPressText = "Approve";
transactionContext.infoLongPress.tuneId = TUNE_TAP_CASUAL;
transactionContext.infoLongPress.text = "Confirm delegate\nregistration";

nbgl_useCaseStaticReviewLight(&transactionContext.tagValueList,
&transactionContext.infoLongPress,
/**
* @brief Draws a confirmation page
*
*/
static void confirm_delegation_page(void) {
delegation_context.infoLongPress.icon = &C_tezos;
delegation_context.infoLongPress.longPressText = "Approve";
delegation_context.infoLongPress.tuneId = TUNE_TAP_CASUAL;
delegation_context.infoLongPress.text = "Confirm delegate\nregistration";

nbgl_useCaseStaticReviewLight(&delegation_context.tagValueList,
&delegation_context.infoLongPress,
"Cancel",
confirmation_callback);
}

void prompt_register_delegate(ui_callback_t const ok_cb, ui_callback_t const cxl_cb) {
void prompt_delegation(ui_callback_t const ok_cb, ui_callback_t const cxl_cb) {
uint8_t value_index = 0;

if (!G.maybe_ops.is_valid) {
THROW(EXC_MEMORY_ERROR);
}
delegation_context.ok_cb = ok_cb;
delegation_context.cxl_cb = cxl_cb;

transactionContext.ok_cb = ok_cb;
transactionContext.cxl_cb = cxl_cb;

bip32_path_with_curve_to_pkh_string(transactionContext.buffer[value_index],
sizeof(transactionContext.buffer[value_index]),
bip32_path_with_curve_to_pkh_string(delegation_context.buffer[value_index],
BUFFER_SIZE,
&global.path_with_curve);

transactionContext.tagValuePair[value_index].item = "Address";
transactionContext.tagValuePair[value_index].value = transactionContext.buffer[value_index];
delegation_context.tagValues[value_index].item = "Address";
delegation_context.tagValues[value_index].value = delegation_context.buffer[value_index];
value_index++;

microtez_to_string_indirect(transactionContext.buffer[value_index],
sizeof(transactionContext.buffer[value_index]),
microtez_to_string_indirect(delegation_context.buffer[value_index],
BUFFER_SIZE,
&G.maybe_ops.v.total_fee);

transactionContext.tagValuePair[value_index].item = "Fee";
transactionContext.tagValuePair[value_index].value = transactionContext.buffer[value_index];
delegation_context.tagValues[value_index].item = "Fee";
delegation_context.tagValues[value_index].value = delegation_context.buffer[value_index];
value_index++;

if (value_index != NUMBER_VALUES) {
THROW(EXC_MEMORY_ERROR);
}

transactionContext.tagValueList.nbPairs = NUMBER_VALUES;
transactionContext.tagValueList.pairs = transactionContext.tagValuePair;

transactionContext.confirmed_status = "DELEGATE\nCONFIRMED";
transactionContext.cancelled_status = "Delegate registration\ncancelled";
delegation_context.tagValueList.nbPairs = NUMBER_VALUES;
delegation_context.tagValueList.pairs = delegation_context.tagValues;

nbgl_useCaseReviewStart(&C_tezos,
"Register delegate",
NULL,
"Cancel",
continue_light_callback,
confirm_delegation_page,
cancel_callback);
}

Expand Down

0 comments on commit 89714bb

Please sign in to comment.