Skip to content

Commit

Permalink
Doc: documents pubkey files
Browse files Browse the repository at this point in the history
  • Loading branch information
spalmer25 committed Mar 6, 2024
1 parent 8fe5a49 commit 0b77b6e
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 65 deletions.
33 changes: 15 additions & 18 deletions src/apdu_pubkey.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
#include "apdu_pubkey.h"

#include "apdu.h"
#include "cx.h"
#include "globals.h"
#include "keys.h"
#include "protocol.h"
#include "to_string.h"
#include "ui.h"
#include "baking_auth.h"
#include "globals.h"
#include "ui_pubkey.h"

#include <string.h>

/**
* @brief Sends apdu response with the public key
*
* @return true
*/
static bool pubkey_ok(void) {
cx_ecfp_public_key_t public_key = {0};
generate_public_key(&public_key,
Expand All @@ -20,21 +19,19 @@ static bool pubkey_ok(void) {
return true;
}

/**
* @brief Authorizes the public key
*
* Sends apdu response with the public key
*
* @return true
*/
static bool baking_ok(void) {
authorize_baking(global.path_with_curve.derivation_type, &global.path_with_curve.bip32_path);
pubkey_ok();
return true;
}

char const *const *get_baking_prompts() {
static const char *const baking_prompts[] = {
PROMPT("Authorize Baking"),
PROMPT("Public Key Hash"),
NULL,
};
return baking_prompts;
}

size_t handle_apdu_get_public_key(uint8_t instruction, volatile uint32_t *flags) {
uint8_t *dataBuffer = G_io_apdu_buffer + OFFSET_CDATA;

Expand Down Expand Up @@ -74,7 +71,7 @@ size_t handle_apdu_get_public_key(uint8_t instruction, volatile uint32_t *flags)
cb = pubkey_ok;
bake = false;
}
prompt_address(bake, cb, delay_reject);
prompt_pubkey(bake, cb, delay_reject);
*flags = IO_ASYNCH_REPLY;
return 0;
}
Expand Down
12 changes: 9 additions & 3 deletions src/apdu_pubkey.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
#pragma once

#include "apdu.h"
#include <stddef.h>
#include <stdint.h>

/**
* @brief Handles AUTHORIZE_BAKING, GET_PUBLIC_KEY and PROMPT_PUBLIC_KEY instructions
*
* @param instruction: apdu instruction
* @param flags: io flags
* @return size_t: offset of the apdu response
*/
size_t handle_apdu_get_public_key(uint8_t instruction, volatile uint32_t* flags);

void prompt_address(bool baking, ui_callback_t ok_cb, ui_callback_t cxl_cb);
8 changes: 0 additions & 8 deletions src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,6 @@ typedef struct {
#define PROMPT_WIDTH 16
#define VALUE_WIDTH PROTOCOL_HASH_BASE58_STRING_SIZE

// Macros to wrap a static prompt and value strings and ensure they aren't too long.
#define PROMPT(str) \
({ \
_Static_assert(sizeof(str) <= PROMPT_WIDTH + 1 /*null byte*/, \
str " won't fit in the UI prompt."); \
str; \
})

// Operations
#define PROTOCOL_HASH_SIZE 32

Expand Down
17 changes: 17 additions & 0 deletions src/ui_pubkey.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include "types.h"

/**
* @brief Draws public key confirmation pages flow
*
* - Initial screen
* - Values:
* - Address
* - Confirmation screens
*
* @param authorize: if ask for authorize the public key
* @param ok_cb: accept callback
* @param cxl_cb: reject callback
*/
void prompt_pubkey(bool authorize, ui_callback_t const ok_cb, ui_callback_t const cxl_cb);
17 changes: 4 additions & 13 deletions src/ui_pubkey_bagl.c
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
#ifdef HAVE_BAGL
#include "apdu_pubkey.h"

#include "apdu.h"
#include "cx.h"
#include "ui_pubkey.h"

#include "globals.h"
#include "keys.h"
#include "protocol.h"
#include "to_string.h"
#include "ui.h"
#include "baking_auth.h"

#include <string.h>

__attribute__((noreturn)) void prompt_address(bool baking,
ui_callback_t ok_cb,
ui_callback_t cxl_cb) {
void prompt_pubkey(bool authorize, ui_callback_t ok_cb, ui_callback_t cxl_cb) {
init_screen_stack();

if (baking) {
if (authorize) {
push_ui_callback("Authorize Baking", copy_string, "With Public Key?");
push_ui_callback("Public Key Hash",
bip32_path_with_curve_to_pkh_string,
Expand All @@ -30,6 +22,5 @@ __attribute__((noreturn)) void prompt_address(bool baking,
}

ux_confirm_screen(ok_cb, cxl_cb);
__builtin_unreachable();
}
#endif // HAVE_BAGL
66 changes: 43 additions & 23 deletions src/ui_pubkey_nbgl.c
Original file line number Diff line number Diff line change
@@ -1,38 +1,54 @@
#ifdef HAVE_NBGL
#include "nbgl_use_case.h"
#include "apdu_pubkey.h"

#include "apdu.h"
#include "cx.h"
#include "ui_pubkey.h"

#include "globals.h"
#include "keys.h"
#include "protocol.h"
#include "to_string.h"
#include "ui.h"
#include "baking_auth.h"

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

#define G global.apdu.u.baking
#define BUFFER_SIZE sizeof(cx_ecfp_public_key_t)

/**
* @brief This structure represents a context needed for address screens navigation
*
*/
typedef struct {
char buffer[sizeof(cx_ecfp_public_key_t)];
ui_callback_t ok_cb;
ui_callback_t cxl_cb;
} TransactionContext_t;
ui_callback_t ok_cb; /// accept callback
ui_callback_t cxl_cb; /// cancel callback
char buffer[BUFFER_SIZE]; /// value buffer
} AddressContext_t;

static TransactionContext_t transactionContext;
/**
* @brief Current address context
*
*/
static AddressContext_t address_context;

/**
* @brief Callback called when address is rejected
*
*/
static void cancel_callback(void) {
transactionContext.cxl_cb();
address_context.cxl_cb();
nbgl_useCaseStatus("Address rejected", false, ui_initial_screen);
}

/**
* @brief Callback called when address is approved
*
*/
static void approve_callback(void) {
transactionContext.ok_cb();
address_context.ok_cb();
nbgl_useCaseStatus("ADDRESS\nVERIFIED", true, ui_initial_screen);
}

/**
* @brief Callback called when address is approved or cancelled
*
* @param confirm: true if approved, false if cancelled
*/
static void confirmation_callback(bool confirm) {
if (confirm) {
approve_callback();
Expand All @@ -41,20 +57,24 @@ static void confirmation_callback(bool confirm) {
}
}

/**
* @brief Draws an address confirmation page
*
*/
static void verify_address(void) {
nbgl_useCaseAddressConfirmation(transactionContext.buffer, confirmation_callback);
nbgl_useCaseAddressConfirmation(address_context.buffer, confirmation_callback);
}

void prompt_address(bool baking, ui_callback_t ok_cb, ui_callback_t cxl_cb) {
transactionContext.ok_cb = ok_cb;
transactionContext.cxl_cb = cxl_cb;
void prompt_pubkey(bool authorize, ui_callback_t ok_cb, ui_callback_t cxl_cb) {
address_context.ok_cb = ok_cb;
address_context.cxl_cb = cxl_cb;

bip32_path_with_curve_to_pkh_string(transactionContext.buffer,
sizeof(transactionContext.buffer),
bip32_path_with_curve_to_pkh_string(address_context.buffer,
BUFFER_SIZE,
&global.path_with_curve);

const char* text;
if (baking) {
if (authorize) {
text = "Authorize Tezos\nBaking address";
} else {
text = "Verify Tezos\naddress";
Expand Down

0 comments on commit 0b77b6e

Please sign in to comment.