diff --git a/app/src/items.c b/app/src/items.c index 68b1e2a..6912ccd 100644 --- a/app/src/items.c +++ b/app/src/items.c @@ -32,7 +32,7 @@ static items_error_t items_storeChainId(uint8_t *items_idx); static items_error_t items_storeUsingGas(uint8_t *items_idx); static items_error_t items_checkTxLengths(uint8_t *items_idx); static items_error_t items_storeHash(uint8_t *items_idx); -static items_error_t items_storeSignature(uint8_t *items_idx); +static items_error_t items_storeSignForAddr(uint8_t *items_idx); static items_error_t items_storeGasItem(uint16_t json_token_index, uint8_t items_idx, uint8_t *unknown_capabitilies); static items_error_t items_storeTransferItem(parsed_json_t *json_all, uint16_t transfer_token_index, uint8_t items_idx, uint8_t *num_of_transfers, uint8_t *unknown_capabitilies); static items_error_t items_storeCrossTransferItem(parsed_json_t *json_all, uint16_t transfer_token_index, uint8_t items_idx, uint8_t *num_of_transfers, uint8_t *unknown_capabitilies); @@ -41,7 +41,7 @@ static items_error_t items_storeUnknownItem(parsed_json_t *json_all, uint16_t tr #define MAX_ITEM_LENGTH_TO_DISPLAY 1000 // TODO : Check other apps to find this number -item_array_t item_array; +item_array_t item_array = {0}; uint8_t hash[BLAKE2B_HASH_SIZE] = {0}; char base64_hash[44]; @@ -61,7 +61,6 @@ item_array_t *items_getItemArray() { void items_storeItems() { uint8_t items_idx = 0; uint8_t unknown_capabitilies = 1; - parsed_json_t json_all = parser_getParserTxObj()->tx_json.json; items_storeSigningTransaction(&items_idx); @@ -89,7 +88,7 @@ void items_storeItems() { items_storeHash(&items_idx); - items_storeSignature(&items_idx); + items_storeSignForAddr(&items_idx); item_array.numOfItems = items_idx; } @@ -324,16 +323,9 @@ static items_error_t items_storeHash(uint8_t *items_idx) { return items_ok; } -static items_error_t items_storeSignature(uint8_t *items_idx) { +static items_error_t items_storeSignForAddr(uint8_t *items_idx) { strcpy(item_array.items[*items_idx].key, "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); - */ - item_array.toString[*items_idx] = items_hashToDisplayString; + item_array.toString[*items_idx] = items_signForAddrToDisplayString; (*items_idx)++; return items_ok; @@ -342,10 +334,10 @@ static items_error_t items_storeSignature(uint8_t *items_idx) { static items_error_t items_storeGasItem(uint16_t json_token_index, uint8_t items_idx, uint8_t *unknown_capabitilies) { uint16_t token_index = 0; uint16_t args_count = 0; - parsed_json_t json_all = parser_getParserTxObj()->tx_json.json; + parsed_json_t *json_all = &(parser_getParserTxObj()->tx_json.json); - object_get_value(&json_all, json_token_index, "args", &token_index); - array_get_element_count(&json_all, token_index, &args_count); + object_get_value(json_all, json_token_index, "args", &token_index); + array_get_element_count(json_all, token_index, &args_count); if (args_count > 0) { snprintf(item_array.items[items_idx].key, sizeof(item_array.items[items_idx].key), "Unknown Capability %d", *unknown_capabitilies); diff --git a/app/src/items.h b/app/src/items.h index c700793..97d6dae 100644 --- a/app/src/items.h +++ b/app/src/items.h @@ -18,8 +18,8 @@ #include #include "zxtypes.h" #include "parser_common.h" -#include "json_parser.h" #include "items_defs.h" +#include "json_parser.h" void items_initItems(); void items_storeItems(); diff --git a/app/src/items_format.c b/app/src/items_format.c index 4024014..8292988 100644 --- a/app/src/items_format.c +++ b/app/src/items_format.c @@ -16,15 +16,16 @@ #include "items_format.h" #include "parser.h" +#include "crypto.h" extern char base64_hash[44]; items_error_t items_stdToDisplayString(item_t item, char *outVal, uint16_t *outValLen) { - parsed_json_t json_all = parser_getParserTxObj()->tx_json.json; + parsed_json_t *json_all = &(parser_getParserTxObj()->tx_json.json); uint16_t item_token_index = item.json_token_index; - *outValLen = json_all.tokens[item_token_index].end - json_all.tokens[item_token_index].start + 1; - snprintf(outVal, *outValLen, "%s", json_all.buffer + json_all.tokens[item_token_index].start); + *outValLen = json_all->tokens[item_token_index].end - json_all->tokens[item_token_index].start + 1; + snprintf(outVal, *outValLen, "%s", json_all->buffer + json_all->tokens[item_token_index].start); return items_ok; } @@ -73,10 +74,10 @@ items_error_t items_transferToDisplayString(item_t item, char *outVal, uint16_t char from[65]; uint8_t from_len = 0; uint16_t token_index = 0; - parsed_json_t json_all = parser_getParserTxObj()->tx_json.json; + parsed_json_t *json_all = &(parser_getParserTxObj()->tx_json.json); uint16_t item_token_index = item.json_token_index; - object_get_value(&json_all, item_token_index, "args", &token_index); + object_get_value(json_all, item_token_index, "args", &token_index); PARSER_TO_ITEMS_ERROR(parser_arrayElementToString(token_index, 0, from, &from_len)); @@ -100,10 +101,10 @@ items_error_t items_crossTransferToDisplayString(item_t item, char *outVal, uint char chain[3]; uint8_t chain_len = 0; uint16_t token_index = 0; - parsed_json_t json_all = parser_getParserTxObj()->tx_json.json; + parsed_json_t *json_all = &(parser_getParserTxObj()->tx_json.json); uint16_t item_token_index = item.json_token_index; - object_get_value(&json_all, item_token_index, "args", &token_index); + object_get_value(json_all, item_token_index, "args", &token_index); PARSER_TO_ITEMS_ERROR(parser_arrayElementToString(token_index, 0, from, &from_len)); @@ -122,13 +123,13 @@ items_error_t items_crossTransferToDisplayString(item_t item, char *outVal, uint items_error_t items_rotateToDisplayString(item_t item, char *outVal, uint16_t *outValLen) { uint16_t token_index = 0; uint16_t item_token_index = item.json_token_index; - parsed_json_t json_all = parser_getParserTxObj()->tx_json.json; + parsed_json_t *json_all = &(parser_getParserTxObj()->tx_json.json); - object_get_value(&json_all, item_token_index, "args", &token_index); - array_get_nth_element(&json_all, token_index, 0, &token_index); + object_get_value(json_all, item_token_index, "args", &token_index); + array_get_nth_element(json_all, token_index, 0, &token_index); - *outValLen = json_all.tokens[token_index].end - json_all.tokens[token_index].start + sizeof("\"\""); - snprintf(outVal, *outValLen, "\"%s\"", json_all.buffer + json_all.tokens[token_index].start); + *outValLen = json_all->tokens[token_index].end - json_all->tokens[token_index].start + sizeof("\"\""); + snprintf(outVal, *outValLen, "\"%s\"", json_all->buffer + json_all->tokens[token_index].start); return items_ok; } @@ -138,18 +139,18 @@ items_error_t items_gasToDisplayString(__Z_UNUSED item_t item, char *outVal, uin uint8_t gasLimit_len = 0; char gasPrice[64]; uint8_t gasPrice_len = 0; - parsed_json_t json_all = parser_getParserTxObj()->tx_json.json; + parsed_json_t *json_all = &(parser_getParserTxObj()->tx_json.json); uint16_t item_token_index = item.json_token_index; uint16_t meta_token_index = item_token_index; parser_getJsonValue(&item_token_index, JSON_GAS_LIMIT); - gasLimit_len = json_all.tokens[item_token_index].end - json_all.tokens[item_token_index].start + 1; - snprintf(gasLimit, gasLimit_len, "%s", json_all.buffer + json_all.tokens[item_token_index].start); + gasLimit_len = json_all->tokens[item_token_index].end - json_all->tokens[item_token_index].start + 1; + snprintf(gasLimit, gasLimit_len, "%s", json_all->buffer + json_all->tokens[item_token_index].start); item_token_index = meta_token_index; parser_getJsonValue(&item_token_index, JSON_GAS_PRICE); - gasPrice_len = json_all.tokens[item_token_index].end - json_all.tokens[item_token_index].start + 1; - snprintf(gasPrice, gasPrice_len, "%s", json_all.buffer + json_all.tokens[item_token_index].start); + gasPrice_len = json_all->tokens[item_token_index].end - json_all->tokens[item_token_index].start + 1; + snprintf(gasPrice, gasPrice_len, "%s", json_all->buffer + json_all->tokens[item_token_index].start); *outValLen = gasLimit_len + gasPrice_len + sizeof("at most ") + sizeof(" at price "); snprintf(outVal, *outValLen, "at most %s at price %s", gasLimit, gasPrice); @@ -163,17 +164,26 @@ items_error_t items_hashToDisplayString(__Z_UNUSED item_t item, char *outVal, ui return items_ok; } +items_error_t items_signForAddrToDisplayString(__Z_UNUSED item_t item, char *outVal, uint16_t *outValLen) { + uint8_t address[65]; + uint16_t address_size; + CHECK_ERROR(crypto_fillAddress(address, sizeof(address), &address_size)); + *outValLen = address_size; + snprintf(outVal, address_size, "%s", address); + return items_ok; +} + items_error_t items_unknownCapabilityToDisplayString(item_t item, char *outVal, uint16_t *outValLen) { uint16_t token_index = 0; uint16_t args_count = 0; uint8_t len = 0; uint8_t outVal_idx= 0; - parsed_json_t json_all = parser_getParserTxObj()->tx_json.json; + parsed_json_t *json_all = &(parser_getParserTxObj()->tx_json.json); uint16_t item_token_index = item.json_token_index; - object_get_value(&json_all, item_token_index, "name", &token_index); - len = json_all.tokens[token_index].end - json_all.tokens[token_index].start + sizeof("name: "); - snprintf(outVal, len, "name: %s", json_all.buffer + json_all.tokens[token_index].start); + object_get_value(json_all, item_token_index, "name", &token_index); + len = json_all->tokens[token_index].end - json_all->tokens[token_index].start + sizeof("name: "); + snprintf(outVal, len, "name: %s", json_all->buffer + json_all->tokens[token_index].start); outVal_idx += len; // Remove null terminator @@ -190,21 +200,21 @@ items_error_t items_unknownCapabilityToDisplayString(item_t item, char *outVal, return items_ok; } - object_get_value(&json_all, item_token_index, "args", &token_index); - array_get_element_count(&json_all, token_index, &args_count); + object_get_value(json_all, item_token_index, "args", &token_index); + array_get_element_count(json_all, token_index, &args_count); if (args_count) { uint16_t args_token_index = 0; for (uint8_t i = 0; i < args_count - 1; i++) { - array_get_nth_element(&json_all, token_index, i, &args_token_index); - if (json_all.tokens[args_token_index].type == JSMN_STRING) { + array_get_nth_element(json_all, token_index, i, &args_token_index); + if (json_all->tokens[args_token_index].type == JSMN_STRING) { // Strings go in between double quotes - len = json_all.tokens[args_token_index].end - json_all.tokens[args_token_index].start + sizeof("arg X: \"\","); - snprintf(outVal + outVal_idx, len, "arg %d: \"%s\",", i + 1, json_all.buffer + json_all.tokens[args_token_index].start); + len = json_all->tokens[args_token_index].end - json_all->tokens[args_token_index].start + sizeof("arg X: \"\","); + snprintf(outVal + outVal_idx, len, "arg %d: \"%s\",", i + 1, json_all->buffer + json_all->tokens[args_token_index].start); } else { - len = json_all.tokens[args_token_index].end - json_all.tokens[args_token_index].start + sizeof("arg X: ,"); - snprintf(outVal + outVal_idx, len, "arg %d: %s,", i + 1, json_all.buffer + json_all.tokens[args_token_index].start); + len = json_all->tokens[args_token_index].end - json_all->tokens[args_token_index].start + sizeof("arg X: ,"); + snprintf(outVal + outVal_idx, len, "arg %d: %s,", i + 1, json_all->buffer + json_all->tokens[args_token_index].start); } outVal_idx += len; @@ -213,13 +223,13 @@ items_error_t items_unknownCapabilityToDisplayString(item_t item, char *outVal, } // Last arg (without comma) - array_get_nth_element(&json_all, token_index, args_count - 1, &args_token_index); - if (json_all.tokens[args_token_index].type == JSMN_STRING) { - len = json_all.tokens[args_token_index].end - json_all.tokens[args_token_index].start + sizeof("arg X: \"\""); - snprintf(outVal + outVal_idx, len, "arg %d: \"%s\"", args_count, json_all.buffer + json_all.tokens[args_token_index].start); + array_get_nth_element(json_all, token_index, args_count - 1, &args_token_index); + if (json_all->tokens[args_token_index].type == JSMN_STRING) { + len = json_all->tokens[args_token_index].end - json_all->tokens[args_token_index].start + sizeof("arg X: \"\""); + snprintf(outVal + outVal_idx, len, "arg %d: \"%s\"", args_count, json_all->buffer + json_all->tokens[args_token_index].start); } else { - len = json_all.tokens[args_token_index].end - json_all.tokens[args_token_index].start + sizeof("arg X: "); - snprintf(outVal + outVal_idx, len, "arg %d: %s", args_count, json_all.buffer + json_all.tokens[args_token_index].start); + len = json_all->tokens[args_token_index].end - json_all->tokens[args_token_index].start + sizeof("arg X: "); + snprintf(outVal + outVal_idx, len, "arg %d: %s", args_count, json_all->buffer + json_all->tokens[args_token_index].start); } outVal_idx += len; } else { diff --git a/app/src/items_format.h b/app/src/items_format.h index 7776c0e..e7b1127 100644 --- a/app/src/items_format.h +++ b/app/src/items_format.h @@ -35,4 +35,5 @@ items_error_t items_crossTransferToDisplayString(item_t item, char *outVal, uint items_error_t items_rotateToDisplayString(item_t item, char *outVal, uint16_t *outValLen); items_error_t items_gasToDisplayString(item_t item, char *outVal, uint16_t *outValLen); items_error_t items_hashToDisplayString(item_t item, char *outVal, uint16_t *outValLen); +items_error_t items_signForAddrToDisplayString(__Z_UNUSED item_t item, char *outVal, uint16_t *outValLen); items_error_t items_unknownCapabilityToDisplayString(item_t item, char *outVal, uint16_t *outValLen); diff --git a/app/src/jsmn/jsmn.c b/app/src/jsmn/jsmn.c index 422139e..b6f4453 100644 --- a/app/src/jsmn/jsmn.c +++ b/app/src/jsmn/jsmn.c @@ -33,10 +33,9 @@ static jsmntok_t *jsmn_alloc_token(jsmn_parser *parser, jsmntok_t *tokens, return NULL; } tok = &tokens[parser->toknext++]; - tok->start = tok->end = -1; - tok->size = 0; + tok->start = tok->end = 0xFFFF; #ifdef JSMN_PARENT_LINKS - tok->parent = -1; + tok->parent = 0xFFFF; #endif return tok; } @@ -49,7 +48,6 @@ static void jsmn_fill_token(jsmntok_t *token, const jsmntype_t type, token->type = type; token->start = start; token->end = end; - token->size = 0; } /** @@ -211,15 +209,14 @@ JSMN_API int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len, if (token == NULL) { return JSMN_ERROR_NOMEM; } - if (parser->toksuper != -1) { - jsmntok_t *t = &tokens[parser->toksuper]; + if (parser->toksuper != 0xFFFF) { #ifdef JSMN_STRICT /* In strict mode an object or array can't become a key */ + jsmntok_t *t = &tokens[parser->toksuper]; if (t->type == JSMN_OBJECT) { return JSMN_ERROR_INVAL; } #endif - t->size++; #ifdef JSMN_PARENT_LINKS token->parent = parser->toksuper; #endif @@ -240,7 +237,7 @@ JSMN_API int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len, } token = &tokens[parser->toknext - 1]; for (;;) { - if (token->start != -1 && token->end == -1) { + if (token->start != 0xFFFF && token->end == 0xFFFF) { if (token->type != type) { return JSMN_ERROR_INVAL; } @@ -248,8 +245,8 @@ JSMN_API int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len, parser->toksuper = token->parent; break; } - if (token->parent == -1) { - if (token->type != type || parser->toksuper == -1) { + if (token->parent == 0xFFFF) { + if (token->type != type || parser->toksuper == 0xFFFF) { return JSMN_ERROR_INVAL; } break; @@ -259,7 +256,7 @@ JSMN_API int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len, #else for (i = parser->toknext - 1; i >= 0; i--) { token = &tokens[i]; - if (token->start != -1 && token->end == -1) { + if (token->start != 0xFFFF && token->end == 0xFFFF) { if (token->type != type) { return JSMN_ERROR_INVAL; } @@ -274,7 +271,7 @@ JSMN_API int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len, } for (; i >= 0; i--) { token = &tokens[i]; - if (token->start != -1 && token->end == -1) { + if (token->start != 0xFFFF && token->end == 0xFFFF) { parser->toksuper = i; break; } @@ -287,9 +284,6 @@ JSMN_API int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len, return r; } count++; - if (parser->toksuper != -1 && tokens != NULL) { - tokens[parser->toksuper].size++; - } break; case '\t': case '\r': @@ -300,7 +294,7 @@ JSMN_API int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len, parser->toksuper = parser->toknext - 1; break; case ',': - if (tokens != NULL && parser->toksuper != -1 && + if (tokens != NULL && parser->toksuper != 0xFFFF && tokens[parser->toksuper].type != JSMN_ARRAY && tokens[parser->toksuper].type != JSMN_OBJECT) { #ifdef JSMN_PARENT_LINKS @@ -308,7 +302,7 @@ JSMN_API int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len, #else for (i = parser->toknext - 1; i >= 0; i--) { if (tokens[i].type == JSMN_ARRAY || tokens[i].type == JSMN_OBJECT) { - if (tokens[i].start != -1 && tokens[i].end == -1) { + if (tokens[i].start != 0xFFFF && tokens[i].end == 0xFFFF) { parser->toksuper = i; break; } @@ -334,10 +328,10 @@ JSMN_API int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len, case 'f': case 'n': /* And they must not be keys of the object */ - if (tokens != NULL && parser->toksuper != -1) { + if (tokens != NULL && parser->toksuper != 0xFFFF) { const jsmntok_t *t = &tokens[parser->toksuper]; if (t->type == JSMN_OBJECT || - (t->type == JSMN_STRING && t->size != 0)) { + (t->type == JSMN_STRING && (t->end - t->start) != 0)) { return JSMN_ERROR_INVAL; } } @@ -350,9 +344,6 @@ JSMN_API int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len, return r; } count++; - if (parser->toksuper != -1 && tokens != NULL) { - tokens[parser->toksuper].size++; - } break; #ifdef JSMN_STRICT @@ -366,7 +357,7 @@ JSMN_API int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len, if (tokens != NULL) { for (i = parser->toknext - 1; i >= 0; i--) { /* Unmatched opened object or array */ - if (tokens[i].start != -1 && tokens[i].end == -1) { + if (tokens[i].start != 0xFFFF && tokens[i].end == 0xFFFF) { return JSMN_ERROR_PART; } } diff --git a/app/src/jsmn/jsmn.h b/app/src/jsmn/jsmn.h index 44dd8a5..f6466aa 100644 --- a/app/src/jsmn/jsmn.h +++ b/app/src/jsmn/jsmn.h @@ -68,9 +68,8 @@ enum jsmnerr { */ typedef struct jsmntok { jsmntype_t type; - int start; - int end; - int size; + unsigned short start; + unsigned short end; #ifdef JSMN_PARENT_LINKS int parent; #endif diff --git a/app/src/json/json_parser.c b/app/src/json/json_parser.c index bffe0f8..804142a 100644 --- a/app/src/json/json_parser.c +++ b/app/src/json/json_parser.c @@ -1,7 +1,22 @@ -#include "json_parser.h" +/******************************************************************************* + * (c) 2018 - 2024 Zondax AG + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ********************************************************************************/ #include #include "../common/parser_common.h" -#include "jsmn.h" +#include "jsmn/jsmn.h" +#include "json_parser.h" #define EQUALS(_P, _Q, _LEN) (MEMCMP( (const void*) PIC(_P), (const void*) PIC(_Q), (_LEN))==0) diff --git a/app/src/json/json_parser.h b/app/src/json/json_parser.h index 2b93e48..953a61b 100644 --- a/app/src/json/json_parser.h +++ b/app/src/json/json_parser.h @@ -27,7 +27,7 @@ // Limit depending on target #if defined(TARGET_NANOS) || defined(TARGET_NANOX) #undef MAX_NUMBER_OF_TOKENS -#define MAX_NUMBER_OF_TOKENS 10 // TODO : Check how many are actually needed, currently made to fit in memory. +#define MAX_NUMBER_OF_TOKENS 120 #endif #if defined(TARGET_STAX) || defined(TARGET_FLEX) diff --git a/app/src/parser.c b/app/src/parser.c index 9759f97..c2a7b7a 100644 --- a/app/src/parser.c +++ b/app/src/parser.c @@ -109,6 +109,5 @@ parser_error_t parser_getItem(const parser_context_t *ctx, uint8_t displayIdx, c ITEMS_TO_PARSER_ERROR(item_array->toString[displayIdx](item_array->items[displayIdx], tempVal, &tempValLen)); pageString(outVal, outValLen, tempVal, pageIdx, pageCount); - return parser_ok; } diff --git a/app/src/parser_impl.c b/app/src/parser_impl.c index 491b231..08f8c68 100644 --- a/app/src/parser_impl.c +++ b/app/src/parser_impl.c @@ -18,28 +18,29 @@ #include "crypto_helper.h" #include +parser_tx_t *parser_tx_obj; -parser_tx_t parser_tx_obj; +parser_error_t _read_json_tx(parser_context_t *c, parser_tx_t *v) { + parser_tx_obj = v; -parser_error_t _read_json_tx(parser_context_t *c, __Z_UNUSED parser_tx_t *v) { - CHECK_ERROR(json_parse(&parser_tx_obj.tx_json.json, (const char *) c->buffer, + CHECK_ERROR(json_parse(&(parser_tx_obj->tx_json.json), (const char *) c->buffer, c->bufferLen)); - parser_tx_obj.tx_json.tx = (const char *) c->buffer; - parser_tx_obj.tx_json.flags.cache_valid = 0; - parser_tx_obj.tx_json.filter_msg_type_count = 0; - parser_tx_obj.tx_json.filter_msg_from_count = 0; + parser_tx_obj->tx_json.tx = (const char *) c->buffer; + parser_tx_obj->tx_json.flags.cache_valid = 0; + parser_tx_obj->tx_json.filter_msg_type_count = 0; + parser_tx_obj->tx_json.filter_msg_from_count = 0; return parser_ok; } parser_tx_t *parser_getParserTxObj() { - return &parser_tx_obj; + return parser_tx_obj; } uint16_t parser_getNumberOfClistElements() { uint16_t number_of_elements = 0; - parsed_json_t *json_all = &parser_tx_obj.tx_json.json; + parsed_json_t *json_all = &parser_tx_obj->tx_json.json; uint16_t token_index = 0; parser_getJsonValue(&token_index, JSON_SIGNERS); @@ -52,7 +53,7 @@ uint16_t parser_getNumberOfClistElements() { } parser_error_t parser_findPubKeyInClist(uint16_t key_token_index) { - parsed_json_t *json_all = &parser_tx_obj.tx_json.json; + parsed_json_t *json_all = &parser_tx_obj->tx_json.json; uint16_t token_index = 0; uint16_t clist_token_index = 0; uint16_t args_token_index = 0; @@ -85,14 +86,12 @@ parser_error_t parser_findPubKeyInClist(uint16_t key_token_index) { } parser_error_t parser_getJsonValue(uint16_t *json_token_index, const char *key) { - parsed_json_t json_obj; + parsed_json_t *json_all = &(parser_tx_obj->tx_json.json); uint16_t token_index = 0; - CHECK_ERROR(object_get_value(&parser_tx_obj.tx_json.json, *json_token_index, key, &token_index)); + CHECK_ERROR(object_get_value(json_all, *json_token_index, key, &token_index)); - CHECK_ERROR(json_parse(&json_obj, parser_tx_obj.tx_json.json.buffer + parser_tx_obj.tx_json.json.tokens[token_index].start, parser_tx_obj.tx_json.json.tokens[token_index].end - parser_tx_obj.tx_json.json.tokens[token_index].start)); - - if (MEMCMP("null", json_obj.buffer, json_obj.bufferLen) == 0) { + if (MEMCMP("null", json_all->buffer + json_all->tokens[token_index].start, json_all->tokens[token_index].end - json_all->tokens[token_index].start) == 0) { return parser_no_data; } @@ -103,11 +102,11 @@ parser_error_t parser_getJsonValue(uint16_t *json_token_index, const char *key) parser_error_t parser_arrayElementToString(uint16_t json_token_index, uint16_t element_idx, char *outVal, uint8_t *outValLen) { uint16_t token_index = 0; - parsed_json_t json_all = parser_tx_obj.tx_json.json; + parsed_json_t *json_all = &(parser_tx_obj->tx_json.json); - CHECK_ERROR(array_get_nth_element(&json_all, json_token_index, element_idx, &token_index)); - strncpy(outVal, json_all.buffer + json_all.tokens[token_index].start, json_all.tokens[token_index].end - json_all.tokens[token_index].start); - *outValLen = json_all.tokens[token_index].end - json_all.tokens[token_index].start; + CHECK_ERROR(array_get_nth_element(json_all, json_token_index, element_idx, &token_index)); + strncpy(outVal, json_all->buffer + json_all->tokens[token_index].start, json_all->tokens[token_index].end - json_all->tokens[token_index].start); + *outValLen = json_all->tokens[token_index].end - json_all->tokens[token_index].start; outVal[*outValLen] = '\0'; return parser_ok; @@ -115,7 +114,7 @@ parser_error_t parser_arrayElementToString(uint16_t json_token_index, uint16_t e parser_error_t parser_getGasObject(uint16_t *json_token_index) { uint16_t token_index = 0; - parsed_json_t *json_all = &parser_tx_obj.tx_json.json; + parsed_json_t *json_all = &parser_tx_obj->tx_json.json; uint16_t name_token_index = 0; for (uint16_t i = 0; i < parser_getNumberOfClistElements(); i++) { @@ -145,15 +144,16 @@ parser_error_t parser_validateMetaField() { uint16_t meta_token_index = 0; uint16_t meta_num_elements = 0; uint16_t key_token_idx = 0; + parsed_json_t *json_all = &(parser_tx_obj->tx_json.json); if (parser_getJsonValue(&meta_token_index, JSON_META) == parser_ok) { - object_get_element_count(&parser_tx_obj.tx_json.json, meta_token_index, &meta_num_elements); + object_get_element_count(json_all, meta_token_index, &meta_num_elements); for (uint16_t i = 0; i < meta_num_elements; i++) { - object_get_nth_key(&parser_tx_obj.tx_json.json, meta_token_index, i, &key_token_idx); + object_get_nth_key(json_all, meta_token_index, i, &key_token_idx); - MEMCPY(meta_curr_key, parser_tx_obj.tx_json.json.buffer + parser_tx_obj.tx_json.json.tokens[key_token_idx].start, - parser_tx_obj.tx_json.json.tokens[key_token_idx].end - parser_tx_obj.tx_json.json.tokens[key_token_idx].start); - meta_curr_key[parser_tx_obj.tx_json.json.tokens[key_token_idx].end - parser_tx_obj.tx_json.json.tokens[key_token_idx].start] = '\0'; + MEMCPY(meta_curr_key, json_all->buffer + json_all->tokens[key_token_idx].start, + json_all->tokens[key_token_idx].end - json_all->tokens[key_token_idx].start); + meta_curr_key[json_all->tokens[key_token_idx].end - json_all->tokens[key_token_idx].start] = '\0'; if (strcmp(keywords[i], meta_curr_key) != 0) { return parser_invalid_meta_field; diff --git a/tests_zemu/tests/common.ts b/tests_zemu/tests/common.ts index 806c641..082de04 100644 --- a/tests_zemu/tests/common.ts +++ b/tests_zemu/tests/common.ts @@ -28,4 +28,4 @@ export const defaultOptions = { } export const txBlobExample = - '{"networkId":"mainnet01","payload":{"exec":{"data":{"ks":{"pred":"keys-all","keys":["368820f80c324bbc7c2b0610688a7da43e39f91d118732671cd9c7500ff43cca"]}},"code":"(coin.transfer-create \\"alice\\" \\"bob\\" (read-keyset \\"ks\\") 100.1)\\n(coin.transfer \\"bob\\" \\"alice\\" 0.1)"}},"signers":[{"pubKey":"6be2f485a7af75fedb4b7f153a903f7e6000ca4aa501179c91a2450b777bd2a7","clist":[{"args":["alice","bob",100.1],"name":"coin.TRANSFER"},{"args":[],"name":"coin.GAS"}]},{"pubKey":"368820f80c324bbc7c2b0610688a7da43e39f91d118732671cd9c7500ff43cca","clist":[{"args":["bob","alice",0.1],"name":"coin.TRANSFER"}]}],"meta":{"creationTime":1580316382,"ttl":7200,"gasLimit":1200,"chainId":"0","gasPrice":1.0e-5,"sender":"alice"},"nonce":"2020-01-29 16:46:22.916695 UTC"}' + '{"networkId":"mainnet01","payload":{"exec":{"data":{},"code":"(coin.transfer \\"83934c0f9b005f378ba3520f9dea952fb0a90e5aa36f1b5ff837d9b30c471790\\" \\"9790d119589a26114e1a42d92598b3f632551c566819ec48e0e8c54dae6ebb42\\" 11.0)"}},"signers":[{"pubKey":"83934c0f9b005f378ba3520f9dea952fb0a90e5aa36f1b5ff837d9b30c471790","clist":[{"args":[],"name":"coin.GAS"},{"args":["83934c0f9b005f378ba3520f9dea952fb0a90e5aa36f1b5ff837d9b30c471790","9790d119589a26114e1a42d92598b3f632551c566819ec48e0e8c54dae6ebb42",11],"name":"coin.TRANSFER"}]}],"meta":{"creationTime":1634009214,"ttl":28800,"gasLimit":600,"chainId":"0","gasPrice":1.0e-5,"sender":"83934c0f9b005f378ba3520f9dea952fb0a90e5aa36f1b5ff837d9b30c471790"},"nonce":"\\"2021-10-12T03:27:53.700Z\\""}' diff --git a/tests_zemu/tests/standard.test.ts b/tests_zemu/tests/standard.test.ts index 92e324d..dd573cd 100644 --- a/tests_zemu/tests/standard.test.ts +++ b/tests_zemu/tests/standard.test.ts @@ -179,35 +179,37 @@ describe('Standard', function () { } }) - // test.concurrent.each(models)('sign tx1 normal', async function (m) { - // const sim = new Zemu(m.path) - // try { - // await sim.start({ ...defaultOptions, model: m.name }) - // const app = new KadenaApp(sim.getTransport()) - - // const txBlob = Buffer.from(txBlobExample) - // const responseAddr = await app.getAddressAndPubKey(accountId) - // const pubKey = responseAddr.publicKey - - // // do not wait here.. we need to navigate - // const signatureRequest = app.sign(accountId, txBlob) - - // // Wait until we are not in the main menu - // await sim.waitUntilScreenIsNot(sim.getMainMenuSnapshot()) - // await sim.compareSnapshotsAndApprove('.', `${m.prefix.toLowerCase()}-sign_asset_freeze`,50000) - - // const signatureResponse = await signatureRequest - // console.log(signatureResponse) - - // expect(signatureResponse.return_code).toEqual(0x9000) - // expect(signatureResponse.error_message).toEqual('No errors') - - // // Now verify the signature - // const prehash = Buffer.concat([Buffer.from('TX'), txBlob]); - // const valid = ed25519.verify(signatureResponse.signature, prehash, pubKey) - // expect(valid).toEqual(true) - // } finally { - // await sim.close() - // } - // }) + /* + test.concurrent.each(models)('sign tx1 normal', async function (m) { + const sim = new Zemu(m.path) + try { + await sim.start({ ...defaultOptions, model: m.name }) + const app = new KadenaApp(sim.getTransport()) + + const txBlob = Buffer.from(txBlobExample) + const responseAddr = await app.getAddressAndPubKey(accountId) + const pubKey = responseAddr.publicKey + + // do not wait here.. we need to navigate + const signatureRequest = app.sign(accountId, txBlob) + + // Wait until we are not in the main menu + await sim.waitUntilScreenIsNot(sim.getMainMenuSnapshot()) + await sim.compareSnapshotsAndApprove('.', `${m.prefix.toLowerCase()}-sign_asset_freeze`,50000) + + const signatureResponse = await signatureRequest + console.log(signatureResponse) + + expect(signatureResponse.return_code).toEqual(0x9000) + expect(signatureResponse.error_message).toEqual('No errors') + + // Now verify the signature + const prehash = Buffer.concat([Buffer.from('TX'), txBlob]); + const valid = ed25519.verify(signatureResponse.signature, prehash, pubKey) + expect(valid).toEqual(true) + } finally { + await sim.close() + } + }) + */ })