Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add from to ui #39

Merged
merged 3 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_change_address();

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

return zxerr_ok;
}
7 changes: 7 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,9 @@ zxerr_t crypto_fillKeys(uint8_t *buffer, uint16_t bufferLen, key_kind_e requeste

return zxerr_ok;
}

zxerr_t crypto_get_change_address(void) {
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
35 changes: 25 additions & 10 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 All @@ -187,27 +202,27 @@ parser_error_t parser_getItem(const parser_context_t *ctx, uint8_t displayIdx, c
// Generate output based on the local index
switch (local_idx) {
case 0:
snprintf(outKey, outKeyLen, "To %d", out_idx - 1);
snprintf(outKey, outKeyLen, "To");
array_to_hexstr(buf, sizeof(buf), ctx->tx_obj->outputs.decrypted_note.owner, 32);
pageString(outVal, outValLen, buf, pageIdx, pageCount);
return parser_ok;

case 1:
if (known_asset_id) {
snprintf(outKey, outKeyLen, "Amount %d", out_idx - 1);
snprintf(outKey, outKeyLen, "Amount");
CHECK_ERROR(
printAmount64(ctx->tx_obj->outputs.decrypted_note.value, asset_id_lookups[asset_id_idx].decimals,
PIC(asset_id_lookups[asset_id_idx].name), outVal, outValLen, pageIdx, pageCount));
return parser_ok;
} else {
snprintf(outKey, outKeyLen, "Raw amount %d", out_idx - 1);
snprintf(outKey, outKeyLen, "Raw amount");
uint64_to_str(buf, sizeof(buf), ctx->tx_obj->outputs.decrypted_note.value);
pageString(outVal, outValLen, buf, pageIdx, pageCount);
return parser_ok;
}

case 2:
snprintf(outKey, outKeyLen, "Raw Asset ID %d", out_idx - 1);
snprintf(outKey, outKeyLen, "Raw Asset ID");
array_to_hexstr(buf, sizeof(buf), ctx->tx_obj->outputs.decrypted_note.asset_id, 32);
pageString(outVal, outValLen, buf, pageIdx, pageCount);
return parser_ok;
Expand Down
Loading
Loading