Skip to content

Commit

Permalink
fix json template generation
Browse files Browse the repository at this point in the history
  • Loading branch information
0xPxt committed Aug 29, 2024
1 parent 3404d8d commit 7d52d2c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 27 deletions.
5 changes: 2 additions & 3 deletions app/src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -62,15 +62,14 @@ 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));
}

ITEMS_TO_PARSER_ERROR(items_initItems())
ITEMS_TO_PARSER_ERROR(items_storeItems(tx_type))

return parser_ok;
}

Expand Down
63 changes: 39 additions & 24 deletions app/src/parser_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,33 @@

#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 \
"{\"networkId\":\"%.*s\",\"payload\":{\"exec\":{\"data\":{\"ks\":{\"pred\":\"keys-all\"," \
"\"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 \
"{\"networkId\":\"%.*s\",\"payload\":{\"exec\":{\"data\":{\"ks\":{\"pred\":\"keys-all\"," \
"\"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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -278,19 +293,19 @@ 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,
chunks[TTL_POS].len, chunks[TTL_POS].data,
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
);

Expand All @@ -304,20 +319,20 @@ 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,
chunks[TTL_POS].len, chunks[TTL_POS].data,
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
);

Expand All @@ -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,
Expand All @@ -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
);

Expand Down

1 comment on commit 7d52d2c

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cpp-Linter Report ⚠️

Some files did not pass the configured checks!

clang-format reports: 9 file(s) not formatted
  • app/src/items_format.h
  • app/src/items.h
  • app/src/parser_txdef.h
  • app/src/parser_impl.h
  • app/src/common/tx.h
  • app/src/parser_impl.c
  • app/src/items.c
  • app/src/apdu_handler.c
  • app/src/parser.c

Have any feedback or feature suggestions? Share it here.

Please sign in to comment.