From 7d52d2ceed0040c296bd9b8e32e79912458383cc Mon Sep 17 00:00:00 2001 From: 0xPxt Date: Thu, 29 Aug 2024 18:07:16 +0200 Subject: [PATCH] fix json template generation --- app/src/parser.c | 5 ++-- app/src/parser_impl.c | 63 ++++++++++++++++++++++++++----------------- 2 files changed, 41 insertions(+), 27 deletions(-) diff --git a/app/src/parser.c b/app/src/parser.c index 5acb6bb..a43cab1 100644 --- a/app/src/parser.c +++ b/app/src/parser.c @@ -32,7 +32,7 @@ tx_json_t tx_obj_json; tx_hash_t tx_obj_hash; -char jsonTemplate[650]; +char jsonTemplate[900] = {0}; uint16_t jsonTemplateLen; parser_error_t parser_init_context(parser_context_t *ctx, const uint8_t *buffer, uint16_t bufferSize) { @@ -62,7 +62,7 @@ parser_error_t parser_parse(parser_context_t *ctx, const uint8_t *data, size_t d } else if (tx_type == tx_type_transaction) { parser_createJsonTemplate(ctx, jsonTemplate, sizeof(jsonTemplate), &jsonTemplateLen); ctx->json = &tx_obj_json; - ctx->buffer = (const uint8_t *)&jsonTemplate; + ctx->buffer = (const uint8_t *) jsonTemplate; ctx->bufferLen = jsonTemplateLen; CHECK_ERROR(_read_json_tx(ctx)); @@ -70,7 +70,6 @@ parser_error_t parser_parse(parser_context_t *ctx, const uint8_t *data, size_t d ITEMS_TO_PARSER_ERROR(items_initItems()) ITEMS_TO_PARSER_ERROR(items_storeItems(tx_type)) - return parser_ok; } diff --git a/app/src/parser_impl.c b/app/src/parser_impl.c index 65e88c0..073b314 100644 --- a/app/src/parser_impl.c +++ b/app/src/parser_impl.c @@ -18,13 +18,15 @@ #include "crypto_helper.h" #include "items.h" +#include "crypto.h" +#include "zxformat.h" #define TRANSFER_FORMAT \ "{\"networkId\":\"%.*s\",\"payload\":{\"exec\":{\"data\":{},\"code\":\"(coin.transfer " \ "\\\"k:%.*s\\\" \\\"k:%.*s\\\" %.*s)\"}},\"signers\":[{\"pubKey\":\"%.*s\",\"clist\":[" \ "{\"args\":[\"k:%.*s\",\"k:%.*s\",%.*s],\"name\":\"coin.TRANSFER\"}," \ - "{\"args\":[],\"name\":\"coin.GAS\"}]}],\"meta\":{\"creationTime\":\"%.*s\"," \ - "\"ttl\":\"%.*s\",\"gasLimit\":\"%.*s\",\"chainId\":\"%.*s\",\"gasPrice\":\"%.*s\"," \ + "{\"args\":[],\"name\":\"coin.GAS\"}]}],\"meta\":{\"creationTime\":%.*s," \ + "\"ttl\":%.*s,\"gasLimit\":%.*s,\"chainId\":\"%.*s\",\"gasPrice\":%.*s," \ "\"sender\":\"k:%.*s\"},\"nonce\":\"%.*s\"}" #define TRANSFER_CREATE_FORMAT \ @@ -32,8 +34,8 @@ "\"keys\":[\"%.*s\"]}},\"code\":\"(coin.transfer-create \\\"k:%.*s\\\" \\\"k:%.*s\\\" " \ "(read-keyset \\\"ks\\\") %.*s)\"}},\"signers\":[{\"pubKey\":\"%.*s\",\"clist\":[" \ "{\"args\":[\"k:%.*s\",\"k:%.*s\",%.*s],\"name\":\"coin.TRANSFER\"}," \ - "{\"args\":[],\"name\":\"coin.GAS\"}]}],\"meta\":{\"creationTime\":\"%.*s\"," \ - "\"ttl\":\"%.*s\",\"gasLimit\":\"%.*s\",\"chainId\":\"%.*s\",\"gasPrice\":\"%.*s\"," \ + "{\"args\":[],\"name\":\"coin.GAS\"}]}],\"meta\":{\"creationTime\":%.*s," \ + "\"ttl\":%.*s,\"gasLimit\":%.*s,\"chainId\":\"%.*s\",\"gasPrice\":%.*s," \ "\"sender\":\"k:%.*s\"},\"nonce\":\"%.*s\"}" #define TRANSFER_CROSSCHAIN_FORMAT \ @@ -41,8 +43,8 @@ "\"keys\":[\"%.*s\"]}},\"code\":\"(coin.transfer-crosschain \\\"k:%.*s\\\" \\\"k:%.*s\\\" " \ "(read-keyset \\\"ks\\\") \\\"%.*s\\\" %.*s)\"}},\"signers\":[{\"pubKey\":\"%.*s\",\"clist\":[" \ "{\"args\":[\"k:%.*s\",\"k:%.*s\",%.*s,\"%.*s\"],\"name\":\"coin.TRANSFER_XCHAIN\"}," \ - "{\"args\":[],\"name\":\"coin.GAS\"}]}],\"meta\":{\"creationTime\":\"%.*s\"," \ - "\"ttl\":\"%.*s\",\"gasLimit\":\"%.*s\",\"chainId\":\"%.*s\",\"gasPrice\":\"%.*s\"," \ + "{\"args\":[],\"name\":\"coin.GAS\"}]}],\"meta\":{\"creationTime\":%.*s," \ + "\"ttl\":%.*s,\"gasLimit\":%.*s,\"chainId\":\"%.*s\",\"gasPrice\":%.*s," \ "\"sender\":\"k:%.*s\"},\"nonce\":\"%.*s\"}" #define RECIPIENT_POS 0 @@ -183,6 +185,8 @@ parser_error_t parser_validateMetaField() { MEMZERO(meta_curr_key, sizeof(meta_curr_key)); } + } else { + return parser_no_data; } return parser_ok; @@ -259,15 +263,26 @@ parser_error_t parser_createJsonTemplate(parser_context_t *ctx, char *jsonTempla for (int i = 0; i < 12; i++) { parser_readSingleByte(ctx, &chunks[i].len); - parser_readBytes(ctx, (uint8_t **)&chunks[i].data, chunks[i].len); + if (chunks[i].len > 0) { + parser_readBytes(ctx, (uint8_t **)&chunks[i].data, chunks[i].len); + } else { + chunks[i].data = ""; + } } - char pubkey[PUB_KEY_LENGTH] = {0}; - strncpy(pubkey, "foo", strlen("foo")); + char address[65] = {0}; #if defined(TARGET_NANOS) || defined(TARGET_NANOX) || defined(TARGET_NANOS2) || defined(TARGET_STAX) || defined(TARGET_FLEX) + char pubkey[PUB_KEY_LENGTH] = {0}; + uint16_t pubkey_len = 0; + if (crypto_fillAddress(pubkey, sizeof(pubkey), &pubkey_len) != zxerr_ok) { - return items_error; + return parser_unexpected_error; } + + uint32_t address_len = array_to_hexstr(address, sizeof(address), pubkey, PUB_KEY_LENGTH); +#else + // Dummy address for cpp_test + uint32_t address_len = snprintf(address, sizeof(address), "%s", "1234567890123456789012345678901234567890123456789012345678901234"); #endif switch (tx_type) { @@ -278,11 +293,11 @@ parser_error_t parser_createJsonTemplate(parser_context_t *ctx, char *jsonTempla snprintf(jsonTemplate, jsonTemplateSize, TRANSFER_FORMAT, chunks[NETWORK_POS].len, chunks[NETWORK_POS].data, - PUB_KEY_LENGTH, pubkey, + address_len, address, chunks[RECIPIENT_POS].len, chunks[RECIPIENT_POS].data, chunks[AMOUNT_POS].len, chunks[AMOUNT_POS].data, - PUB_KEY_LENGTH, pubkey, - PUB_KEY_LENGTH, pubkey, + address_len, address, + address_len, address, chunks[RECIPIENT_POS].len, chunks[RECIPIENT_POS].data, chunks[AMOUNT_POS].len, chunks[AMOUNT_POS].data, chunks[CREATION_TIME_POS].len, chunks[CREATION_TIME_POS].data, @@ -290,7 +305,7 @@ parser_error_t parser_createJsonTemplate(parser_context_t *ctx, char *jsonTempla chunks[GAS_LIMIT_POS].len, chunks[GAS_LIMIT_POS].data, chunks[CHAIN_ID_POS].len, chunks[CHAIN_ID_POS].data, chunks[GAS_PRICE_POS].len, chunks[GAS_PRICE_POS].data, - PUB_KEY_LENGTH, pubkey, + address_len, address, chunks[NONCE_POS].len, chunks[NONCE_POS].data ); @@ -304,12 +319,12 @@ parser_error_t parser_createJsonTemplate(parser_context_t *ctx, char *jsonTempla snprintf(jsonTemplate, jsonTemplateSize, TRANSFER_CREATE_FORMAT, chunks[NETWORK_POS].len, chunks[NETWORK_POS].data, - PUB_KEY_LENGTH, pubkey, - PUB_KEY_LENGTH, pubkey, + address_len, address, + address_len, address, chunks[RECIPIENT_POS].len, chunks[RECIPIENT_POS].data, chunks[AMOUNT_POS].len, chunks[AMOUNT_POS].data, - PUB_KEY_LENGTH, pubkey, - PUB_KEY_LENGTH, pubkey, + address_len, address, + address_len, address, chunks[RECIPIENT_POS].len, chunks[RECIPIENT_POS].data, chunks[AMOUNT_POS].len, chunks[AMOUNT_POS].data, chunks[CREATION_TIME_POS].len, chunks[CREATION_TIME_POS].data, @@ -317,7 +332,7 @@ parser_error_t parser_createJsonTemplate(parser_context_t *ctx, char *jsonTempla chunks[GAS_LIMIT_POS].len, chunks[GAS_LIMIT_POS].data, chunks[CHAIN_ID_POS].len, chunks[CHAIN_ID_POS].data, chunks[GAS_PRICE_POS].len, chunks[GAS_PRICE_POS].data, - PUB_KEY_LENGTH, pubkey, + address_len, address, chunks[NONCE_POS].len, chunks[NONCE_POS].data ); @@ -331,13 +346,13 @@ parser_error_t parser_createJsonTemplate(parser_context_t *ctx, char *jsonTempla snprintf(jsonTemplate, jsonTemplateSize, TRANSFER_CROSSCHAIN_FORMAT, chunks[NETWORK_POS].len, chunks[NETWORK_POS].data, - PUB_KEY_LENGTH, pubkey, - PUB_KEY_LENGTH, pubkey, + address_len, address, + address_len, address, chunks[RECIPIENT_POS].len, chunks[RECIPIENT_POS].data, chunks[RECIPIENT_CHAIN_POS].len, chunks[RECIPIENT_CHAIN_POS].data, chunks[AMOUNT_POS].len, chunks[AMOUNT_POS].data, - PUB_KEY_LENGTH, pubkey, - PUB_KEY_LENGTH, pubkey, + address_len, address, + address_len, address, chunks[RECIPIENT_POS].len, chunks[RECIPIENT_POS].data, chunks[AMOUNT_POS].len, chunks[AMOUNT_POS].data, chunks[RECIPIENT_CHAIN_POS].len, chunks[RECIPIENT_CHAIN_POS].data, @@ -346,7 +361,7 @@ parser_error_t parser_createJsonTemplate(parser_context_t *ctx, char *jsonTempla chunks[GAS_LIMIT_POS].len, chunks[GAS_LIMIT_POS].data, chunks[CHAIN_ID_POS].len, chunks[CHAIN_ID_POS].data, chunks[GAS_PRICE_POS].len, chunks[GAS_PRICE_POS].data, - PUB_KEY_LENGTH, pubkey, + address_len, address, chunks[NONCE_POS].len, chunks[NONCE_POS].data );