Skip to content

Commit

Permalink
wip - transfer create
Browse files Browse the repository at this point in the history
  • Loading branch information
0xPxt committed Aug 28, 2024
1 parent d041fc9 commit 3975090
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/src/apdu_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
11 changes: 10 additions & 1 deletion app/src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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))
Expand Down
130 changes: 130 additions & 0 deletions app/src/parser_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions app/src/parser_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down

1 comment on commit 3975090

@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: 8 file(s) not formatted
  • app/src/items_format.h
  • app/src/items.h
  • app/src/parser_txdef.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.