From 39750902a8977b8bac66b5793d6ff9f54f7bc539 Mon Sep 17 00:00:00 2001 From: 0xPxt Date: Wed, 28 Aug 2024 10:12:52 +0200 Subject: [PATCH] wip - transfer create --- app/src/apdu_handler.c | 1 + app/src/parser.c | 11 +++- app/src/parser_impl.c | 130 +++++++++++++++++++++++++++++++++++++++++ app/src/parser_impl.h | 5 ++ 4 files changed, 146 insertions(+), 1 deletion(-) diff --git a/app/src/apdu_handler.c b/app/src/apdu_handler.c index 8e7e5b6..89dd071 100644 --- a/app/src/apdu_handler.c +++ b/app/src/apdu_handler.c @@ -170,6 +170,7 @@ __Z_INLINE void handleSignTransaction(volatile uint32_t *flags, volatile uint32_ view_review_show(REVIEW_TXN); *flags |= IO_ASYNCH_REPLY; } + __Z_INLINE void handle_getversion(__Z_UNUSED volatile uint32_t *flags, volatile uint32_t *tx) { G_io_apdu_buffer[0] = 0; diff --git a/app/src/parser.c b/app/src/parser.c index 28b505d..85ea9d5 100644 --- a/app/src/parser.c +++ b/app/src/parser.c @@ -32,6 +32,9 @@ tx_json_t tx_obj_json; tx_hash_t tx_obj_hash; +uint8_t jsonTemplate[500]; +uint16_t jsonTemplateLen; + parser_error_t parser_init_context(parser_context_t *ctx, const uint8_t *buffer, uint16_t bufferSize) { ctx->offset = 0; @@ -56,7 +59,13 @@ parser_error_t parser_parse(parser_context_t *ctx, const uint8_t *data, size_t d } else if (tx_type == tx_type_hash) { ctx->hash = &tx_obj_hash; CHECK_ERROR(_read_hash_tx(ctx)); - } + } else if (tx_type == tx_type_transaction) { + parser_createJsonTemplate(ctx, jsonTemplate, jsonTemplateLen); + ctx->buffer = &jsonTemplate; + ctx->bufferLen = &jsonTemplateLen; + + CHECK_ERROR(_read_json_tx(ctx)); + } ITEMS_TO_PARSER_ERROR(items_initItems()) ITEMS_TO_PARSER_ERROR(items_storeItems(tx_type)) diff --git a/app/src/parser_impl.c b/app/src/parser_impl.c index ece84c0..a0027a3 100644 --- a/app/src/parser_impl.c +++ b/app/src/parser_impl.c @@ -19,6 +19,9 @@ #include "crypto_helper.h" #include "items.h" +static parser_error_t parser_readSingleByte(parser_context_t *ctx, uint8_t *byte); +static parser_error_t parser_readBytes(parser_context_t *ctx, uint8_t *bytes, uint16_t len); + tx_json_t *parser_json_obj; tx_hash_t *parser_hash_obj; @@ -201,6 +204,133 @@ bool items_isNullField(uint16_t json_token_index) { return (MEMCMP("null", json_all->buffer + token->start, token->end - token->start) == 0); } + +parser_error_t parser_createJsonTemplate(parser_context_t *ctx, uint8_t *jsonTemplate, uint16_t jsonTemplateLen) { + // read tx_type + uint8_t tx_type = 0; + parser_readSingleByte(ctx, &tx_type); + + // read recipient_len + uint8_t recipient_len = 0; + parser_readSingleByte(ctx, &recipient_len); + // read recipient + char *recipient = NULL; + parser_readBytes(ctx, (uint8_t *)recipient, recipient_len); + + // read recipient_chain_len + uint8_t recipient_chain_len = 0; + parser_readSingleByte(ctx, &recipient_chain_len); + // read recipient_chain + char *recipient_chain = NULL; + parser_readBytes(ctx, (uint8_t *)recipient_chain, recipient_chain_len); + + // read network_len + uint8_t network_len = 0; + parser_readSingleByte(ctx, &network_len); + // read network + char *network = NULL; + parser_readBytes(ctx, (uint8_t *)network, network_len); + + // read amount_len + uint8_t amount_len = 0; + parser_readSingleByte(ctx, &amount_len); + // read amount + char *amount = NULL; + parser_readBytes(ctx, (uint8_t *)amount, amount_len); + + // read namespace_len + uint8_t namespace_len = 0; + parser_readSingleByte(ctx, &namespace_len); + // read namespace + char *namespace = NULL; + parser_readBytes(ctx, (uint8_t *)namespace, namespace_len); + + // read module_len + uint8_t module_len = 0; + parser_readSingleByte(ctx, &module_len); + // read module + char *module = NULL; + parser_readBytes(ctx, (uint8_t *)module, module_len); + + // read gas_price_len + uint8_t gas_price_len = 0; + parser_readSingleByte(ctx, &gas_price_len); + // read gas_price + char *gas_price = NULL; + parser_readBytes(ctx, (uint8_t *)gas_price, gas_price_len); + + // read gas_limit_len + uint8_t gas_limit_len = 0; + parser_readSingleByte(ctx, &gas_limit_len); + // read gas_limit + char *gas_limit = NULL; + parser_readBytes(ctx, (uint8_t *)gas_limit, gas_limit_len); + + // read creation_time_len + uint8_t creation_time_len = 0; + parser_readSingleByte(ctx, &creation_time_len); + // read creation_time + char *creation_time = NULL; + parser_readBytes(ctx, (uint8_t *)creation_time, creation_time_len); + + // read chain_id_len + uint8_t chain_id_len = 0; + parser_readSingleByte(ctx, &chain_id_len); + // read chain_id + char *chain_id = NULL; + parser_readBytes(ctx, (uint8_t *)chain_id, chain_id_len); + + // read nonce_len + uint8_t nonce_len = 0; + parser_readSingleByte(ctx, &nonce_len); + // read nonce + char *nonce = NULL; + parser_readBytes(ctx, (uint8_t *)nonce, nonce_len); + + // read ttl_len + uint8_t ttl_len = 0; + parser_readSingleByte(ctx, &ttl_len); + // read ttl + char *ttl = NULL; + parser_readBytes(ctx, (uint8_t *)ttl, ttl_len); + + switch (tx_type) { + case TX_TYPE_TRANSFER: + // "{\"networkId\":\"$NETWORK\",\"payload\":{\"exec\":{\"data\":{},\"code\":\"(coin.transfer \\\"k:$PUBKEY\\\" \\\"k:$RECIPIENT\\\" $AMOUNT)\"}},\"signers\":[{\"pubKey\":\"$PUBKEY\",\"clist\":[{\"args\":[\"k:$PUBKEY\",\"k:$RECIPIENT\",$AMOUNT],\"name\":\"coin.TRANSFER\"},{\"args\":[],\"name\":\"coin.GAS\"}]}],\"meta\":{\"creationTime\":$CREATION_TIME,\"ttl\":$TTL,\"gasLimit\":$GAS_LIMIT,\"chainId\":\"$CHAIN_ID\",\"gasPrice\":$GAS_PRICE,\"sender\":\"k:$PUBKEY\"},\"nonce\":\"$NONCE\"}" + break; + case TX_TYPE_TRANSFER_CREATE: + // "{\"networkId\":\"$NETWORK\",\"payload\":{\"exec\":{\"data\":{\"ks\":{\"pred\":\"keys-all\",\"keys\":[\"$PUBKEY\"]}},\"code\":\"(coin.transfer-create \\\"k:$PUBKEY\\\" \\\"k:$RECIPIENT\\\" (read-keyset \\\"ks\\\") $AMOUNT)\"}},\"signers\":[{\"pubKey\":\"$PUBKEY\",\"clist\":[{\"args\":[\"k:$PUBKEY\",\"k:$RECIPIENT\",$AMOUNT],\"name\":\"coin.TRANSFER\"},{\"args\":[],\"name\":\"coin.GAS\"}]}],\"meta\":{\"creationTime\":$CREATION_TIME,\"ttl\":$TTL,\"gasLimit\":$GAS_LIMIT,\"chainId\":\"$CHAIN_ID\",\"gasPrice\":$GAS_PRICE,\"sender\":\"k:$PUBKEY\"},\"nonce\":\"$NONCE\"}" + break; + case TX_TYPE_TRANSFER_CROSSCHAIN: + // "{\"networkId\":\"$NETWORK\",\"payload\":{\"exec\":{\"data\":{\"ks\":{\"pred\":\"keys-all\",\"keys\":[\"$PUBKEY\"]}},\"code\":\"(coin.transfer-crosschain \\\"k:$PUBKEY\\\" \\\"k:$RECIPIENT\\\" (read-keyset \\\"ks\\\") \\\"$RECIPIENT_CHAIN\\\" $AMOUNT)\"}},\"signers\":[{\"pubKey\":\"$PUBKEY\",\"clist\":[{\"args\":[\"k:$PUBKEY\",\"k:$RECIPIENT\",$AMOUNT,\"$RECIPIENT_CHAIN\"],\"name\":\"coin.TRANSFER_XCHAIN\"},{\"args\":[],\"name\":\"coin.GAS\"}]}],\"meta\":{\"creationTime\":$CREATION_TIME,\"ttl\":$TTL,\"gasLimit\":$GAS_LIMIT,\"chainId\":\"$CHAIN_ID\",\"gasPrice\":$GAS_PRICE,\"sender\":\"k:$PUBKEY\"},\"nonce\":\"$NONCE\"}" + break; + default: + return parser_no_data; + } + + + return parser_ok; +} + +static parser_error_t parser_readSingleByte(parser_context_t *ctx, uint8_t *byte) { + if (ctx->offset >= ctx->bufferLen) { + return parser_unexpected_buffer_end; + } + + *byte = ctx->buffer[ctx->offset]; + ctx->offset++; + return parser_ok; +} + +static parser_error_t parser_readBytes(parser_context_t *ctx, uint8_t *bytes, uint16_t len) { + if (ctx->offset + len > ctx->bufferLen) { + return parser_unexpected_buffer_end; + } + + bytes = ctx->buffer + ctx->offset; + ctx->offset += len; + return parser_ok; +} const char *parser_getErrorDescription(parser_error_t err) { switch (err) { diff --git a/app/src/parser_impl.h b/app/src/parser_impl.h index daef177..b7f8964 100644 --- a/app/src/parser_impl.h +++ b/app/src/parser_impl.h @@ -25,6 +25,10 @@ extern "C" { #endif +#define TX_TYPE_TRANSFER 0 +#define TX_TYPE_TRANSFER_CREATE 1 +#define TX_TYPE_TRANSFER_CROSSCHAIN 2 + #define JSON_NETWORK_ID "networkId" #define JSON_META "meta" #define JSON_SIGNERS "signers" @@ -61,6 +65,7 @@ parser_error_t parser_validateMetaField(); parser_error_t parser_getTxName(uint16_t token_index); parser_error_t parser_getValidClist(uint16_t *clist_token_index, uint16_t *num_args); bool items_isNullField(uint16_t json_token_index); +parser_error_t parser_createJsonTemplate(parser_context_t *ctx, uint8_t *jsonTemplate, uint16_t jsonTemplateLen); #ifdef __cplusplus }