Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
chcmedeiros committed Sep 12, 2023
1 parent 080fb78 commit 755d4a2
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 24 deletions.
12 changes: 9 additions & 3 deletions app/src/apdu_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ __Z_INLINE void handle_getversion(__Z_UNUSED volatile uint32_t *flags, volatile
}

__Z_INLINE uint8_t extractHRP(uint32_t rx, uint32_t offset) {
uint8_t hrp_len = 0;
if (rx < offset + 1) {
THROW(APDU_CODE_DATA_INVALID);
}
Expand All @@ -77,10 +78,15 @@ __Z_INLINE uint8_t extractHRP(uint32_t rx, uint32_t offset) {
memcpy(bech32_hrp, G_io_apdu_buffer + offset + 1, bech32_hrp_len);
bech32_hrp[bech32_hrp_len] = 0; // zero terminate

return bech32_hrp_len;
hrp_len = bech32_hrp_len;
return hrp_len;
}

__Z_INLINE void extractHDPath(uint32_t rx, uint32_t offset) {
if (rx < offset + 1) {
THROW(APDU_CODE_DATA_INVALID);
}

if ((rx - offset) < sizeof(uint32_t) * HDPATH_LEN_DEFAULT) {
THROW(APDU_CODE_WRONG_LENGTH);
}
Expand Down Expand Up @@ -110,8 +116,8 @@ static void extractHDPath_HRP(uint32_t rx, uint32_t offset) {

// Check if HRP was sent
if ((rx - offset) > sizeof(uint32_t) * HDPATH_LEN_DEFAULT) {
extractHRP(rx, offset + sizeof(uint32_t) * HDPATH_LEN_DEFAULT);
encoding = checkChainConfig(hdPath[1], bech32_hrp, bech32_hrp_len);
uint8_t hrp_bech32_len = extractHRP(rx, offset + sizeof(uint32_t) * HDPATH_LEN_DEFAULT);
encoding = checkChainConfig(hdPath[1], bech32_hrp, hrp_bech32_len);
if (encoding == UNSUPPORTED) {
ZEMU_LOGF(50, "Chain config not supported for: %s\n", bech32_hrp)
THROW(APDU_CODE_COMMAND_NOT_ALLOWED);
Expand Down
7 changes: 7 additions & 0 deletions app/src/cbor/cbor_parser_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ parser_error_t parser_mapCborError(CborError err) {
}

static parser_error_t cbor_check_optFields(CborValue *data, Cbor_container *container) {
if (data == NULL || container == NULL) {
return parser_unexpected_value;
}
int key;
for (size_t i = 0; i < container->n_field; i++) {

Expand Down Expand Up @@ -67,6 +70,10 @@ static parser_error_t cbor_check_optFields(CborValue *data, Cbor_container *cont
}

static parser_error_t cbor_check_screen(CborValue *data, Cbor_container *container) {
if (data == NULL || container == NULL) {
return parser_unexpected_value;
}

int screen_key;
//check title Key
PARSER_ASSERT_OR_ERROR(cbor_value_is_integer(data), parser_unexpected_type)
Expand Down
12 changes: 2 additions & 10 deletions app/src/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ uint32_t hdPath[HDPATH_LEN_DEFAULT];

uint8_t bech32_hrp_len;
char bech32_hrp[MAX_BECH32_HRP_LEN + 1];
address_encoding_e encoding;
address_encoding_e encoding = BECH32_COSMOS;

#include "cx.h"

Expand Down Expand Up @@ -73,15 +73,7 @@ static zxerr_t crypto_extractUncompressedPublicKey(uint8_t *pubKey, uint16_t pub
__Z_INLINE zxerr_t compressPubkey(const uint8_t *pubkey, uint16_t pubkeyLen, uint8_t *output, uint16_t outputLen) {
if (pubkey == NULL || output == NULL ||
pubkeyLen != PK_LEN_SECP256K1_UNCOMPRESSED || outputLen < PK_LEN_SECP256K1) {
return zxerr_unknown;
}

// Format pubkey
for (int i = 0; i < 32; i++) {
output[i] = pubkey[64 - i];
}
if ((pubkey[32] & 1) != 0) {
output[31] |= 0x80;
return zxerr_invalid_crypto_settings;
}

MEMCPY(output, pubkey, PK_LEN_SECP256K1);
Expand Down
2 changes: 0 additions & 2 deletions app/src/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ extern char bech32_hrp[MAX_BECH32_HRP_LEN + 1];
extern uint8_t bech32_hrp_len;
extern address_encoding_e encoding;

void crypto_set_hrp(char *p);

zxerr_t crypto_fillAddress(uint8_t *buffer, uint16_t bufferLen, uint16_t *addrResponseLen);

zxerr_t crypto_sign(uint8_t *signature, uint16_t signatureMaxlen, uint16_t *signatureLen);
Expand Down
2 changes: 1 addition & 1 deletion app/src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ __Z_INLINE parser_error_t parser_getJsonItem(const parser_context_t *ctx,
return parser_unexpected_number_items;
}

if (displayIdx < 0 || displayIdx >= numItems) {
if (displayIdx >= numItems) {
return parser_display_idx_out_of_range;
}

Expand Down
12 changes: 6 additions & 6 deletions tests/testcases.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
std::vector<testcase_t> GetJsonTestCases(const std::string &jsonFile) {
auto answer = std::vector<testcase_t>();

Json::CharReaderBuilder builder;
std::shared_ptr<Json::Value> obj(new Json::Value());
const Json::CharReaderBuilder builder;
const std::shared_ptr<Json::Value> obj(new Json::Value());


std::string fullPathJsonFile = std::string(TESTVECTORS_DIR) + jsonFile;
const std::string fullPathJsonFile = std::string(TESTVECTORS_DIR) + jsonFile;

std::ifstream inFile(fullPathJsonFile);
if (!inFile.is_open()) {
Expand All @@ -44,7 +44,7 @@ std::vector<testcase_t> GetJsonTestCases(const std::string &jsonFile) {
Json::StreamWriterBuilder wbuilder;
wbuilder["commentStyle"] = "None";
wbuilder["indentation"] = "";
std::string txStr = Json::writeString(wbuilder, v["tx"]);
const std::string txStr = Json::writeString(wbuilder, v["tx"]);

auto expected = std::vector<std::string>();
for (const auto j : v["expected"]) {
Expand Down Expand Up @@ -73,10 +73,10 @@ std::vector<testcase_t> GetJsonTestCases(const std::string &jsonFile) {
std::vector<testcase_t> GetJsonTextualTestCases(const std::string &jsonFile) {
auto answer = std::vector<testcase_t>();

Json::CharReaderBuilder builder;
const Json::CharReaderBuilder builder;
Json::Value obj;

std::string fullPathJsonFile = std::string(TESTVECTORS_DIR) + jsonFile;
const std::string fullPathJsonFile = std::string(TESTVECTORS_DIR) + jsonFile;

std::ifstream inFile(fullPathJsonFile);
if (!inFile.is_open()) {
Expand Down
4 changes: 2 additions & 2 deletions tests/tx_parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace {
#pragma ide diagnostic ignored "ConstantParameter"
parser_error_t tx_traverse(int16_t root_token_index, uint8_t *numChunks) {
uint16_t ret_value_token_index = 0;
parser_error_t err = tx_traverse_find(root_token_index, &ret_value_token_index);
const parser_error_t err = tx_traverse_find(root_token_index, &ret_value_token_index);

if (err != parser_ok){
return err;
Expand Down Expand Up @@ -145,7 +145,7 @@ namespace {

parser_tx_obj.tx_json.tx = transaction;
parser_tx_obj.tx_json.flags.cache_valid = false;
parser_error_t err = JSON_PARSE(&parser_tx_obj.tx_json.json, parser_tx_obj.tx_json.tx);
const parser_error_t err = JSON_PARSE(&parser_tx_obj.tx_json.json, parser_tx_obj.tx_json.tx);
EXPECT_EQ(err, parser_ok);

uint8_t numItems;
Expand Down

0 comments on commit 755d4a2

Please sign in to comment.