Skip to content

Commit

Permalink
Refactor parserGetItem
Browse files Browse the repository at this point in the history
  • Loading branch information
0xPxt committed Aug 9, 2024
1 parent b681832 commit 4e8e117
Show file tree
Hide file tree
Showing 5 changed files with 475 additions and 140 deletions.
10 changes: 1 addition & 9 deletions app/src/common/parser_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,7 @@ typedef enum {
parser_json_zero_tokens,
parser_json_too_many_tokens, // "NOMEM: JSON string contains too many tokens"
parser_json_incomplete_json, // "JSON string is not complete";
// TODO : Clean these if never used
//parser_json_contains_whitespace,
//parser_json_is_not_sorted,
//parser_json_missing_chain_id,
//parser_json_missing_sequence,
//parser_json_missing_fee,
//parser_json_missing_msgs,
//parser_json_missing_account_number,
//parser_json_missing_memo,
parser_json_not_a_transfer,
parser_json_unexpected_error,
} parser_error_t;

Expand Down
110 changes: 9 additions & 101 deletions app/src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include <zxformat.h>
#include <zxmacros.h>
#include <zxtypes.h>
#include <base64.h>

#include "coin.h"
#include "crypto.h"
Expand Down Expand Up @@ -48,6 +47,9 @@ parser_error_t parser_parse(parser_context_t *ctx, const uint8_t *data, size_t d

CHECK_ERROR(_read_json_tx(ctx, tx_obj));

CHECK_ERROR(parser_initItems());
CHECK_ERROR(parser_storeItems(ctx));

return parser_ok;
}

Expand All @@ -74,7 +76,8 @@ parser_error_t parser_getNumItems(const parser_context_t *ctx, uint8_t *num_item
return parser_tx_obj_empty;
}

*num_items = 10;
*num_items = 0;
parser_getTotalItems(num_items);

return parser_ok;
}
Expand All @@ -97,110 +100,15 @@ parser_error_t parser_getItem(const parser_context_t *ctx, uint8_t displayIdx, c
char *outVal, uint16_t outValLen, uint8_t pageIdx, uint8_t *pageCount) {
*pageCount = 1;
uint8_t numItems = 0;
item_array_t *item_array = parser_getItemArray();
CHECK_ERROR(parser_getNumItems(ctx, &numItems))
CHECK_APP_CANARY()

CHECK_ERROR(checkSanity(numItems, displayIdx))
cleanOutput(outKey, outKeyLen, outVal, outValLen);

switch (displayIdx) {
case 0:
snprintf(outKey, outKeyLen, "Signing");
snprintf(outVal, outValLen, "Transaction");
return parser_ok;
case 1:
snprintf(outKey, outKeyLen, "On Network");
char net[9];
uint16_t net_size;
CHECK_ERROR(parser_getJsonValueAsString("networkId", net, &net_size));
snprintf(outVal, net_size + 1, "%s", net);
return parser_ok;
case 2:
snprintf(outKey, outKeyLen, "Requiring");
snprintf(outVal, outValLen, "Capabilities");
return parser_ok;
case 3:
snprintf(outKey, outKeyLen, "Of Key");
char pubKey[64];
uint16_t pubKey_size;
CHECK_ERROR(parser_getJsonValueAsString("sender", pubKey, &pubKey_size));
snprintf(outVal, pubKey_size + 1, "%s", pubKey);
return parser_ok;
case 4:
snprintf(outKey, outKeyLen, "Paying Gas");
snprintf(outVal, outValLen, "");
return parser_ok;
case 5:
// TODO : Iterate over all the transfers
snprintf(outKey, outKeyLen, "Transfer 1");
char to[65];
char from[65];
char amount[10];
uint16_t to_size;
uint16_t from_size;
uint16_t amount_size;
CHECK_ERROR(parser_getTransactionParams(1, amount, &amount_size, from, &from_size, to, &to_size));

snprintf(outVal, amount_size + from_size + to_size + 15, "%s from \"%s\" to \"%s\"", amount, from, to);

return parser_ok;
case 6:
snprintf(outKey, outKeyLen, "On Chain");
char chain[2];
uint16_t chain_size;
CHECK_ERROR(parser_getJsonValueAsString("chainId", chain, &chain_size));
snprintf(outVal, chain_size + 1, "%s", chain);
return parser_ok;
case 7:
snprintf(outKey, outKeyLen, "Using Gas");
char gasLimit[10];
char gasPrice[10];
uint16_t gasLimit_size;
uint16_t gasPrice_size;

CHECK_ERROR(parser_getJsonValueAsString("gasLimit", gasLimit, &gasLimit_size));
CHECK_ERROR(parser_getJsonValueAsString("gasPrice", gasPrice, &gasPrice_size));

snprintf(outVal, 8, "at most");
snprintf(outVal + strlen(outVal), gasLimit_size + 2, " %s", gasLimit);
snprintf(outVal + strlen(outVal), 10, " at price");
snprintf(outVal + strlen(outVal), gasPrice_size + 2, " %s", gasPrice);
return parser_ok;
case 8:
snprintf(outKey, outKeyLen, "Transaction hash");
uint8_t hash[BLAKE2B_OUTPUT_LEN] = {0};
if (blake2b_hash((uint8_t *)ctx->buffer, ctx->bufferLen, hash) != zxerr_ok) {
return parser_unexpected_error;
}

uint8_t base64_hash[44];
base64_encode(base64_hash, 44, hash, sizeof(hash));

// Make it base64 URL safe
for (int i = 0; base64_hash[i] != '\0'; i++) {
if (base64_hash[i] == '+') {
base64_hash[i] = '-';
} else if (base64_hash[i] == '/') {
base64_hash[i] = '_';
}
}

snprintf(outVal, sizeof(base64_hash), "%s", base64_hash);
return parser_ok;
case 9:
snprintf(outKey, outKeyLen, "Sign for Address");
/*
Currently launching cpp tests, so this is not available
uint8_t address[32];
uint16_t address_size;
CHECK_ERROR(crypto_fillAddress(address, sizeof(address), &address_size));
snprintf(outVal, address_size + 1, "%s", address);
*/

return parser_ok;
default:
break;
}
snprintf(outKey, outKeyLen, item_array->items[displayIdx].key);
item_array->items[displayIdx].toString(item_array->items[displayIdx].buf, item_array->items[displayIdx].len, outVal, &outValLen);

return parser_display_idx_out_of_range;
return parser_ok;
}
Loading

0 comments on commit 4e8e117

Please sign in to comment.