Skip to content

Commit

Permalink
compute address from path
Browse files Browse the repository at this point in the history
  • Loading branch information
chcmedeiros committed Nov 7, 2024
1 parent a7c2e55 commit 13c46e8
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 59 deletions.
7 changes: 7 additions & 0 deletions app/src/apdu_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ __Z_INLINE void handleSign(volatile uint32_t *flags, volatile uint32_t *tx, uint
THROW(APDU_CODE_OK);
}

// Get address for the received path
zxerr_t zxerr = crypto_get_change_address();
if (zxerr != zxerr_ok) {
*tx = 0;
THROW(APDU_CODE_DATA_INVALID);
}

const char *error_msg = tx_parse();
CHECK_APP_CANARY()
if (error_msg != NULL) {
Expand Down
10 changes: 10 additions & 0 deletions app/src/common/actions.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,13 @@ __Z_INLINE void app_reply_error() {
set_code(G_io_apdu_buffer, 0, APDU_CODE_DATA_INVALID);
io_exchange(CHANNEL_APDU | IO_RETURN_AFTER_TX, 2);
}

__Z_INLINE zxerr_t app_get_address() {
zxerr_t err = crypto_get_address();

if (err != zxerr_ok) {
THROW(APDU_CODE_EXECUTION_ERROR);
}

return zxerr_ok;
}
11 changes: 11 additions & 0 deletions app/src/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "zxmacros.h"

uint32_t hdPath[HDPATH_LEN_DEFAULT];
uint8_t change_address[32];

static zxerr_t computeKeys(keys_t *saplingKeys) {
if (saplingKeys == NULL) {
Expand Down Expand Up @@ -179,3 +180,13 @@ zxerr_t crypto_fillKeys(uint8_t *buffer, uint16_t bufferLen, key_kind_e requeste

return zxerr_ok;
}

zxerr_t crypto_get_change_address(void) {
if (change_address == NULL || sizeof(change_address) != KEY_LENGTH) {
return zxerr_unknown;
}

MEMZERO(change_address, sizeof(change_address));
CHECK_ZXERR(crypto_generateSaplingKeys(change_address, sizeof(change_address), PublicAddress));
return zxerr_ok;
}
3 changes: 2 additions & 1 deletion app/src/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ extern "C" {
#include "zxerror.h"

extern uint32_t hdPath[HDPATH_LEN_DEFAULT];
extern uint8_t change_address[32];

zxerr_t crypto_fillKeys(uint8_t *buffer, uint16_t bufferLen, key_kind_e requestedKey, uint16_t *cmdResponseLen);
zxerr_t crypto_sign(const uint8_t publickeyRandomness[32], const uint8_t txnHash[32], uint8_t *output, uint16_t outputLen);

zxerr_t crypto_get_change_address(void);
#if defined(LEDGER_SPECIFIC)
zxerr_t crypto_generateSaplingKeys(uint8_t *output, uint16_t outputLen, key_kind_e requestedKey);
#endif
Expand Down
27 changes: 21 additions & 6 deletions app/src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ parser_error_t parser_validate(parser_context_t *ctx) {
parser_error_t parser_getNumItems(const parser_context_t *ctx, uint8_t *num_items) {
UNUSED(ctx);

// Txversion + (owner + amount ) * output_with_valid_asset_id + (owner + amount + asset id) * output_with_raw_asset_id +
// fee + expiration
// Txversion + From + (owner + amount ) * output_with_valid_asset_id + (owner + amount + asset id) *
// output_with_raw_asset_id + fee + expiration
*num_items =
1 + ((ctx->tx_obj->outputs.elements - ctx->tx_obj->n_raw_asset_id) * 2) + (ctx->tx_obj->n_raw_asset_id * 3) + 2;
2 + ((ctx->tx_obj->outputs.elements - ctx->tx_obj->n_raw_asset_id) * 2) + (ctx->tx_obj->n_raw_asset_id * 3) + 2;

if (*num_items == 0) {
return parser_unexpected_number_items;
Expand Down Expand Up @@ -154,18 +154,33 @@ parser_error_t parser_getItem(const parser_context_t *ctx, uint8_t displayIdx, c
CHECK_ERROR(checkSanity(numItems, displayIdx));
cleanOutput(outKey, outKeyLen, outVal, outValLen);

// Calculate total output elements
uint8_t tmp_idx = displayIdx;
uint8_t asset_id_idx = 0;
bool known_asset_id = false;
char buf[70] = {0};

if (displayIdx == 0) {
snprintf(outKey, outKeyLen, "Tx Version");
snprintf(outVal, outValLen, "V%d", (uint8_t)ctx->tx_obj->transactionVersion);
return parser_ok;
}
char buf[70] = {0};
tmp_idx -= 1; // Adjust for the transaction version

if (displayIdx == 1) {
snprintf(outKey, outKeyLen, "From");
#if defined(LEDGER_SPECIFIC)
array_to_hexstr(buf, sizeof(buf), change_address, 32);
pageString(outVal, outValLen, buf, pageIdx, pageCount);
#else
uint8_t test_change_address[32] = {0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa};
array_to_hexstr(buf, sizeof(buf), test_change_address, 32);
pageString(outVal, outValLen, buf, pageIdx, pageCount);
#endif
return parser_ok;
}

tmp_idx -= 2; // Adjust for the transaction version
cumulative_display_count = 0; // Reset cumulative display count for fresh calculation

for (out_idx = 1; out_idx <= ctx->tx_obj->outputs.elements; out_idx++) {
Expand Down
Loading

0 comments on commit 13c46e8

Please sign in to comment.