Skip to content

Commit

Permalink
wip - sign hash
Browse files Browse the repository at this point in the history
  • Loading branch information
0xPxt committed Aug 27, 2024
1 parent 34ae0a2 commit 2e81b33
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 110 deletions.
13 changes: 7 additions & 6 deletions app/src/apdu_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "view.h"
#include "view_internal.h"
#include "zxmacros.h"
#include "parser_txdef.h"

static bool tx_initialized = false;

Expand Down Expand Up @@ -108,13 +109,13 @@ __Z_INLINE void handleGetAddr(volatile uint32_t *flags, volatile uint32_t *tx, u
THROW(APDU_CODE_OK);
}

__Z_INLINE void handleSign(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx) {
zemu_log("handleSign\n");
__Z_INLINE void handleSignJson(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx) {
zemu_log("handleSignJson\n");
if (!process_chunk(tx, rx)) {
THROW(APDU_CODE_OK);
}

const char *error_msg = tx_parse();
const char *error_msg = tx_parse(tx_type_json);
CHECK_APP_CANARY()
if (error_msg != NULL) {
const int error_msg_length = strnlen(error_msg, sizeof(G_io_apdu_buffer));
Expand All @@ -134,8 +135,7 @@ __Z_INLINE void handleSignHash(volatile uint32_t *flags, volatile uint32_t *tx,
THROW(APDU_CODE_OK);
}

// TODO: call parser for hash
const char *error_msg = tx_parse();
const char *error_msg = tx_parse(tx_type_hash);
CHECK_APP_CANARY()
if (error_msg != NULL) {
const int error_msg_length = strnlen(error_msg, sizeof(G_io_apdu_buffer));
Expand All @@ -149,6 +149,7 @@ __Z_INLINE void handleSignHash(volatile uint32_t *flags, volatile uint32_t *tx,
*flags |= IO_ASYNCH_REPLY;
}


__Z_INLINE void handleSignTransaction(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx) {
zemu_log("handleSignTransaction\n");
// if (!process_chunk(tx, rx)) {
Expand Down Expand Up @@ -226,7 +227,7 @@ void handleApdu(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx) {

case INS_SIGN_KDA: {
CHECK_PIN_VALIDATED()
handleSign(flags, tx, rx);
handleSignJson(flags, tx, rx);
break;
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/common/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const char *parser_getErrorDescription(parser_error_t err);
const char *parser_getMsgPackTypeDescription(uint8_t type);

//// parses a tx buffer
parser_error_t parser_parse(parser_context_t *ctx, const uint8_t *data, size_t dataLen, tx_json_t *tx_obj);
parser_error_t parser_parse(parser_context_t *ctx, const uint8_t *data, size_t dataLen, tx_type_t tx_type);

//// verifies tx fields
parser_error_t parser_validate(parser_context_t *ctx);
Expand Down
9 changes: 2 additions & 7 deletions app/src/common/tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ storage_t NV_CONST N_appdata_impl __attribute__((aligned(64)));
#define N_appdata (*(NV_VOLATILE storage_t *)PIC(&N_appdata_impl))
#endif

static tx_json_t tx_obj;
static parser_context_t ctx_parsed_tx;

void tx_initialize() {
Expand All @@ -59,10 +58,8 @@ uint32_t tx_get_buffer_length() { return buffering_get_buffer()->pos; }

uint8_t *tx_get_buffer() { return buffering_get_buffer()->data; }

const char *tx_parse() {
MEMZERO(&tx_obj, sizeof(tx_obj));

uint8_t err = parser_parse(&ctx_parsed_tx, tx_get_buffer(), tx_get_buffer_length(), &tx_obj);
const char *tx_parse(tx_type_t tx_type) {
uint8_t err = parser_parse(&ctx_parsed_tx, tx_get_buffer(), tx_get_buffer_length(), tx_type);

CHECK_APP_CANARY()

Expand All @@ -80,8 +77,6 @@ const char *tx_parse() {
return NULL;
}

void tx_parse_reset() { MEMZERO(&tx_obj, sizeof(tx_obj)); }

zxerr_t tx_getNumItems(uint8_t *num_items) {
parser_error_t err = parser_getNumItems(&ctx_parsed_tx, num_items);

Expand Down
3 changes: 2 additions & 1 deletion app/src/common/tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "coin.h"
#include "os.h"
#include "zxerror.h"
#include "parser_txdef.h"

void tx_initialize();

Expand All @@ -42,7 +43,7 @@ uint8_t *tx_get_buffer();
/// Parse message stored in transaction buffer
/// This function should be called as soon as full buffer data is loaded.
/// \return It returns NULL if data is valid or error message otherwise.
const char *tx_parse();
const char *tx_parse(tx_type_t tx_type);

/// Return the number of items in the transaction
zxerr_t tx_getNumItems(uint8_t *num_items);
Expand Down
77 changes: 46 additions & 31 deletions app/src/items.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ static items_error_t items_storeCaution();
static items_error_t items_storeChainId();
static items_error_t items_storeUsingGas();
static items_error_t items_checkTxLengths();
static items_error_t items_computeHash(tx_type_t tx_type);
static items_error_t items_storeHash();
static items_error_t items_storeSignForAddr();
static items_error_t items_storeGasItem(uint16_t json_token_index);
Expand Down Expand Up @@ -69,30 +70,34 @@ items_error_t items_initItems() {

item_array_t *items_getItemArray() { return &item_array; }

items_error_t items_storeItems() {
CHECK_ITEMS_ERROR(items_storeSigningTransaction());
items_error_t items_storeItems(tx_type_t tx_type) {
if (tx_type == tx_type_json) {
CHECK_ITEMS_ERROR(items_storeSigningTransaction());

CHECK_ITEMS_ERROR(items_storeNetwork());
CHECK_ITEMS_ERROR(items_storeNetwork());

CHECK_ITEMS_ERROR(items_storeRequiringCapabilities());
CHECK_ITEMS_ERROR(items_storeRequiringCapabilities());

CHECK_ITEMS_ERROR(items_storeKey());
CHECK_ITEMS_ERROR(items_storeKey());

CHECK_ITEMS_ERROR(items_validateSigners());
CHECK_ITEMS_ERROR(items_validateSigners());

CHECK_ITEMS_ERROR(items_storePayingGas());
CHECK_ITEMS_ERROR(items_storePayingGas());

CHECK_ITEMS_ERROR(items_storeAllTransfers());
CHECK_ITEMS_ERROR(items_storeAllTransfers());

if (parser_validateMetaField() != parser_ok) {
CHECK_ITEMS_ERROR(items_storeCaution());
} else {
CHECK_ITEMS_ERROR(items_storeChainId());
if (parser_validateMetaField() != parser_ok) {
CHECK_ITEMS_ERROR(items_storeCaution());
} else {
CHECK_ITEMS_ERROR(items_storeChainId());

CHECK_ITEMS_ERROR(items_storeUsingGas());
}

CHECK_ITEMS_ERROR(items_storeUsingGas());
CHECK_ITEMS_ERROR(items_checkTxLengths());
}

CHECK_ITEMS_ERROR(items_checkTxLengths());
CHECK_ITEMS_ERROR(items_computeHash(tx_type));

CHECK_ITEMS_ERROR(items_storeHash());

Expand All @@ -116,7 +121,7 @@ static items_error_t items_storeSigningTransaction() {
static items_error_t items_storeNetwork() {
uint16_t *curr_token_idx = &item_array.items[item_array.numOfItems].json_token_index;
item_t *item = &item_array.items[item_array.numOfItems];
parsed_json_t *json_all = &(parser_getParserTxObj()->json);
parsed_json_t *json_all = &(parser_getParserJsonObj()->json);

PARSER_TO_ITEMS_ERROR(object_get_value(json_all, *curr_token_idx, JSON_NETWORK_ID, curr_token_idx));

Expand All @@ -139,7 +144,7 @@ static items_error_t items_storeRequiringCapabilities() {
}

static items_error_t items_storeKey() {
parsed_json_t *json_all = &(parser_getParserTxObj()->json);
parsed_json_t *json_all = &(parser_getParserJsonObj()->json);
uint16_t *curr_token_idx = &item_array.items[item_array.numOfItems].json_token_index;
item_t *item = &item_array.items[item_array.numOfItems];

Expand All @@ -159,7 +164,7 @@ static items_error_t items_storeKey() {
}

static items_error_t items_validateSigners() {
parsed_json_t *json_all = &(parser_getParserTxObj()->json);
parsed_json_t *json_all = &(parser_getParserJsonObj()->json);
uint16_t *curr_token_idx = &item_array.items[item_array.numOfItems].json_token_index;
item_t *item = &item_array.items[item_array.numOfItems];
item_t *ofKey_item = &item_array.items[item_array.numOfItems - 1];
Expand Down Expand Up @@ -196,7 +201,7 @@ static items_error_t items_validateSigners() {
}

static items_error_t items_storePayingGas() {
parsed_json_t *json_all = &(parser_getParserTxObj()->json);
parsed_json_t *json_all = &(parser_getParserJsonObj()->json);
uint16_t *curr_token_idx = &item_array.items[item_array.numOfItems].json_token_index;
uint16_t token_index = 0;
uint16_t name_token_index = 0;
Expand Down Expand Up @@ -235,7 +240,7 @@ static items_error_t items_storePayingGas() {
}

static items_error_t items_storeAllTransfers() {
parsed_json_t *json_all = &(parser_getParserTxObj()->json);
parsed_json_t *json_all = &(parser_getParserJsonObj()->json);
uint16_t *curr_token_idx = &item_array.items[item_array.numOfItems].json_token_index;
uint16_t token_index = 0;
uint8_t num_of_transfers = 1;
Expand Down Expand Up @@ -296,7 +301,7 @@ static items_error_t items_storeCaution() {
static items_error_t items_storeChainId() {
uint16_t *curr_token_idx = &item_array.items[item_array.numOfItems].json_token_index;
item_t *item = &item_array.items[item_array.numOfItems];
parsed_json_t *json_all = &(parser_getParserTxObj()->json);
parsed_json_t *json_all = &(parser_getParserJsonObj()->json);

PARSER_TO_ITEMS_ERROR(object_get_value(json_all, 0, JSON_META, curr_token_idx));

Expand All @@ -315,7 +320,7 @@ static items_error_t items_storeChainId() {
static items_error_t items_storeUsingGas() {
uint16_t *curr_token_idx = &item_array.items[item_array.numOfItems].json_token_index;
item_t *item = &item_array.items[item_array.numOfItems];
parsed_json_t *json_all = &(parser_getParserTxObj()->json);
parsed_json_t *json_all = &(parser_getParserJsonObj()->json);

PARSER_TO_ITEMS_ERROR(object_get_value(json_all, 0, JSON_META, curr_token_idx));

Expand Down Expand Up @@ -345,12 +350,14 @@ static items_error_t items_checkTxLengths() {
return items_ok;
}

static items_error_t items_storeHash() {
item_t *item = &item_array.items[item_array.numOfItems];

strcpy(item->key, "Transaction hash");
static items_error_t items_computeHash(tx_type_t tx_type) {
if (tx_type == tx_type_hash) {
tx_hash_t *hash_obj = parser_getParserHashObj();
MEMCPY(base64_hash, hash_obj->tx, hash_obj->hash_len);
return items_ok;
}

if (blake2b_hash((uint8_t *)parser_getParserTxObj()->json.buffer, parser_getParserTxObj()->json.bufferLen, hash) !=
if (blake2b_hash((uint8_t *)parser_getParserJsonObj()->json.buffer, parser_getParserJsonObj()->json.bufferLen, hash) !=
zxerr_ok) {
return items_error;
}
Expand All @@ -366,6 +373,14 @@ static items_error_t items_storeHash() {
}
}

return items_ok;
}

static items_error_t items_storeHash() {
item_t *item = &item_array.items[item_array.numOfItems];

strcpy(item->key, "Transaction hash");

item_array.toString[item_array.numOfItems] = items_hashToDisplayString;
INCREMENT_NUM_ITEMS()

Expand All @@ -386,7 +401,7 @@ static items_error_t items_storeSignForAddr() {
static items_error_t items_storeGasItem(uint16_t json_token_index) {
uint16_t token_index = 0;
uint16_t args_count = 0;
parsed_json_t *json_all = &(parser_getParserTxObj()->json);
parsed_json_t *json_all = &(parser_getParserJsonObj()->json);
item_t *item = &item_array.items[item_array.numOfItems];

PARSER_TO_ITEMS_ERROR(object_get_value(json_all, json_token_index, "args", &token_index));
Expand All @@ -407,7 +422,7 @@ static items_error_t items_storeTxItem(uint16_t transfer_token_index, uint8_t *n
uint16_t token_index = 0;
uint16_t num_of_args = 0;
item_t *item = &item_array.items[item_array.numOfItems];
parsed_json_t *json_all = &(parser_getParserTxObj()->json);
parsed_json_t *json_all = &(parser_getParserJsonObj()->json);

PARSER_TO_ITEMS_ERROR(object_get_value(json_all, transfer_token_index, "args", &token_index));

Expand All @@ -429,7 +444,7 @@ static items_error_t items_storeTxCrossItem(uint16_t transfer_token_index, uint8
uint16_t token_index = 0;
uint16_t num_of_args = 0;
item_t *item = &item_array.items[item_array.numOfItems];
parsed_json_t *json_all = &(parser_getParserTxObj()->json);
parsed_json_t *json_all = &(parser_getParserJsonObj()->json);

PARSER_TO_ITEMS_ERROR(object_get_value(json_all, transfer_token_index, "args", &token_index));

Expand All @@ -451,7 +466,7 @@ static items_error_t items_storeTxRotateItem(uint16_t transfer_token_index) {
uint16_t token_index = 0;
uint16_t num_of_args = 0;
item_t *item = &item_array.items[item_array.numOfItems];
parsed_json_t *json_all = &(parser_getParserTxObj()->json);
parsed_json_t *json_all = &(parser_getParserJsonObj()->json);

PARSER_TO_ITEMS_ERROR(object_get_value(json_all, transfer_token_index, "args", &token_index));

Expand All @@ -470,7 +485,7 @@ static items_error_t items_storeTxRotateItem(uint16_t transfer_token_index) {

static items_error_t items_storeUnknownItem(uint16_t num_of_args, uint16_t transfer_token_index) {
item_t *item = &item_array.items[item_array.numOfItems];
parsed_json_t *json_all = &(parser_getParserTxObj()->json);
parsed_json_t *json_all = &(parser_getParserJsonObj()->json);

snprintf(item->key, sizeof(item->key), "Unknown Capability %d", item_array.numOfUnknownCapabilities);
item_array.numOfUnknownCapabilities++;
Expand Down
3 changes: 2 additions & 1 deletion app/src/items.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
#include "json_parser.h"
#include "parser_common.h"
#include "zxtypes.h"
#include "parser_txdef.h"

items_error_t items_initItems();
items_error_t items_storeItems();
items_error_t items_storeItems(tx_type_t tx_type);
uint16_t items_getTotalItems();
item_array_t *items_getItemArray();
12 changes: 6 additions & 6 deletions app/src/items_format.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
extern char base64_hash[45];

items_error_t items_stdToDisplayString(item_t item, char *outVal, uint16_t outValLen) {
const parsed_json_t *json_all = &(parser_getParserTxObj()->json);
const parsed_json_t *json_all = &(parser_getParserJsonObj()->json);
const jsmntok_t *token = &(json_all->tokens[item.json_token_index]);
const uint16_t len = token->end - token->start;

Expand Down Expand Up @@ -102,7 +102,7 @@ items_error_t items_transferToDisplayString(item_t item, char *outVal, uint16_t
uint8_t to_len = 0;
uint8_t from_len = 0;
uint16_t token_index = 0;
parsed_json_t *json_all = &(parser_getParserTxObj()->json);
parsed_json_t *json_all = &(parser_getParserJsonObj()->json);
uint16_t item_token_index = item.json_token_index;

PARSER_TO_ITEMS_ERROR(object_get_value(json_all, item_token_index, "args", &token_index));
Expand Down Expand Up @@ -130,7 +130,7 @@ items_error_t items_crossTransferToDisplayString(item_t item, char *outVal, uint
uint8_t from_len = 0;
uint8_t chain_len = 0;
uint16_t token_index = 0;
parsed_json_t *json_all = &(parser_getParserTxObj()->json);
parsed_json_t *json_all = &(parser_getParserJsonObj()->json);
uint16_t item_token_index = item.json_token_index;

PARSER_TO_ITEMS_ERROR(object_get_value(json_all, item_token_index, "args", &token_index));
Expand All @@ -154,7 +154,7 @@ 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()->json);
parsed_json_t *json_all = &(parser_getParserJsonObj()->json);
jsmntok_t *token;

PARSER_TO_ITEMS_ERROR(object_get_value(json_all, item_token_index, "args", &token_index));
Expand All @@ -172,7 +172,7 @@ items_error_t items_gasToDisplayString(__Z_UNUSED item_t item, char *outVal, uin
const char *gasPrice;
uint8_t gasLimit_len;
uint8_t gasPrice_len;
parsed_json_t *json_all = &(parser_getParserTxObj()->json);
parsed_json_t *json_all = &(parser_getParserJsonObj()->json);
uint16_t item_token_index = item.json_token_index;
uint16_t meta_token_index = item.json_token_index;
jsmntok_t *token;
Expand Down Expand Up @@ -212,7 +212,7 @@ items_error_t items_unknownCapabilityToDisplayString(item_t item, char *outVal,
uint16_t token_index = 0;
uint16_t args_count = 0;
uint8_t outVal_idx = 0;
parsed_json_t *json_all = &(parser_getParserTxObj()->json);
parsed_json_t *json_all = &(parser_getParserJsonObj()->json);
uint16_t item_token_index = item.json_token_index;
jsmntok_t *token;
uint16_t len = 0;
Expand Down
Loading

1 comment on commit 2e81b33

@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: 5 file(s) not formatted
  • app/src/parser_impl.c
  • app/src/apdu_handler.c
  • app/src/items.h
  • app/src/parser_txdef.h
  • app/src/common/tx.h

Have any feedback or feature suggestions? Share it here.

Please sign in to comment.