Skip to content

Commit

Permalink
fix bool returns and argument validation
Browse files Browse the repository at this point in the history
  • Loading branch information
chcmedeiros committed Sep 15, 2023
1 parent 09dd6bc commit 3c05a83
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 21 deletions.
2 changes: 1 addition & 1 deletion app/src/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ zxerr_t crypto_fillAddress(uint8_t *buffer, uint16_t buffer_len, uint16_t *addrR
case BECH32_COSMOS: {
// Hash it
cx_hash_sha256(buffer, PK_LEN_SECP256K1, hashed1_pk, CX_SHA256_SIZE);
uint8_t hashed2_pk[CX_RIPEMD160_SIZE];
uint8_t hashed2_pk[CX_RIPEMD160_SIZE] = {0};
ripemd160_32(hashed2_pk, hashed1_pk);
CHECK_ZXERR(bech32EncodeFromBytes(addr, buffer_len - PK_LEN_SECP256K1, bech32_hrp, hashed2_pk, CX_RIPEMD160_SIZE, 1, BECH32_ENCODING_BECH32))
break;
Expand Down
44 changes: 24 additions & 20 deletions app/src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,63 +90,67 @@ parser_error_t parser_getNumItems(const parser_context_t *ctx, uint8_t *num_item
return tx_display_numItems(num_items);
}

__Z_INLINE bool_t parser_areEqual(uint16_t tokenIdx, const char *expected) {
__Z_INLINE bool parser_areEqual(uint16_t tokenIdx, const char *expected) {
if (parser_tx_obj.tx_json.json.tokens[tokenIdx].type != JSMN_STRING) {
return bool_false;
return false;
}

int32_t len = parser_tx_obj.tx_json.json.tokens[tokenIdx].end - parser_tx_obj.tx_json.json.tokens[tokenIdx].start;
if (len < 0) {
return bool_false;
return false;
}

if (strlen(expected) != (size_t) len) {
return bool_false;
return false;
}

const char *p = parser_tx_obj.tx_json.tx + parser_tx_obj.tx_json.json.tokens[tokenIdx].start;
for (int32_t i = 0; i < len; i++) {
if (expected[i] != *(p + i)) {
return bool_false;
return false;
}
}

return bool_true;
return true;
}

__Z_INLINE bool_t parser_isAmount(char *key) {
__Z_INLINE bool parser_isAmount(char *key) {
if (strcmp(key, "fee/amount") == 0) {
return bool_true;
return true;
}

if (strcmp(key, "msgs/inputs/coins") == 0) {
return bool_true;
return true;
}

if (strcmp(key, "msgs/outputs/coins") == 0) {
return bool_true;
return true;
}

if (strcmp(key, "msgs/value/inputs/coins") == 0) {
return bool_true;
return true;
}

if (strcmp(key, "msgs/value/outputs/coins") == 0) {
return bool_true;
return true;
}

if (strcmp(key, "msgs/value/amount") == 0) {
return bool_true;
return true;
}

if (strcmp(key, "tip/amount") == 0) {
return bool_true;
return true;
}

return bool_false;
return false;
}

__Z_INLINE parser_error_t is_default_denom_base(const char *denom, uint8_t denom_len, bool *is_default) {
if (is_default == NULL) {
return parser_unexpected_value;
}

bool is_expert_or_default = false;
CHECK_PARSER_ERR(tx_is_expert_mode_or_not_default_chainid(&is_expert_or_default))
if (is_expert_or_default) {
Expand All @@ -155,12 +159,12 @@ __Z_INLINE parser_error_t is_default_denom_base(const char *denom, uint8_t denom
}

if (strlen(COIN_DEFAULT_DENOM_BASE) != denom_len) {
*is_default = bool_false;
*is_default = false;
return parser_ok;
}

if (memcmp(denom, COIN_DEFAULT_DENOM_BASE, denom_len) == 0) {
*is_default = bool_true;
*is_default = true;
return parser_ok;
}

Expand Down Expand Up @@ -205,7 +209,7 @@ __Z_INLINE parser_error_t parser_formatAmountItem(uint16_t amountToken,
MEMZERO(outVal, outValLen);
MEMZERO(bufferUI, sizeof(bufferUI));

if (parser_tx_obj.tx_json.json.tokens[amountToken + 2].start < 0 ||
if (parser_tx_obj.tx_json.json.tokens[amountToken + 2].start < 0 ||
parser_tx_obj.tx_json.json.tokens[amountToken + 4].start < 0) {
return parser_unexpected_buffer_end;
}
Expand Down Expand Up @@ -262,7 +266,7 @@ __Z_INLINE parser_error_t parser_formatAmount(uint16_t amountToken,
}

uint8_t totalPages = 0;
bool_t showItemSet = false;
uint8_t showItemSet = 0;
uint8_t showPageIdx = pageIdx;
uint16_t showItemTokenIdx = 0;

Expand All @@ -283,7 +287,7 @@ __Z_INLINE parser_error_t parser_formatAmount(uint16_t amountToken,

if (!showItemSet) {
if (showPageIdx < subpagesCount) {
showItemSet = true;
showItemSet = 1;
showItemTokenIdx = itemTokenIdx;
ZEMU_LOGF(200, "[formatAmount] [%d] [SET] TokenIdx %d - PageIdx: %d", i, showItemTokenIdx,
showPageIdx)
Expand Down
12 changes: 12 additions & 0 deletions app/src/tx_display.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,13 +320,21 @@ parser_error_t tx_indexRootFields() {
}

__Z_INLINE parser_error_t is_default_chainid(bool *is_default) {
if (is_default == NULL) {
return parser_unexpected_value;
}

CHECK_PARSER_ERR(tx_indexRootFields())
*is_default = display_cache.is_default_chain;

return parser_ok;
}

parser_error_t tx_is_expert_mode_or_not_default_chainid(bool *expert_or_default) {
if (expert_or_default == NULL) {
return parser_unexpected_value;
}

bool is_default = false;
CHECK_PARSER_ERR(is_default_chainid(&is_default))
*expert_or_default = app_mode_expert() || !is_default;
Expand All @@ -335,6 +343,10 @@ parser_error_t tx_is_expert_mode_or_not_default_chainid(bool *expert_or_default)
}

__Z_INLINE parser_error_t get_subitem_count(root_item_e root_item, uint8_t *num_items) {
if (num_items == NULL) {
return parser_unexpected_value;
}

CHECK_PARSER_ERR(tx_indexRootFields())
if (display_cache.total_item_count == 0) {
*num_items = 0;
Expand Down
2 changes: 2 additions & 0 deletions tests_zemu/tests/standard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ describe('Standard', function () {

expect(resp.bech32_address).toEqual('cosmos1wkd9tfm5pqvhhaxq77wv9tvjcsazuaykwsld65')
expect(resp.compressed_pk.length).toEqual(33)
expect(resp.compressed_pk.toString("hex")).toEqual('035c986b9ae5fbfb8e1e9c12c817f5ef8fdb821cdecaa407f1420ec4f8f1d766bf')
} finally {
await sim.close()
}
Expand Down Expand Up @@ -121,6 +122,7 @@ describe('Standard', function () {

expect(resp.bech32_address).toEqual('cosmos1wkd9tfm5pqvhhaxq77wv9tvjcsazuaykwsld65')
expect(resp.compressed_pk.length).toEqual(33)
expect(resp.compressed_pk.toString("hex")).toEqual('035c986b9ae5fbfb8e1e9c12c817f5ef8fdb821cdecaa407f1420ec4f8f1d766bf')
} finally {
await sim.close()
}
Expand Down

0 comments on commit 3c05a83

Please sign in to comment.