Skip to content

Commit

Permalink
Transaction signingusing merkle trees
Browse files Browse the repository at this point in the history
First transaction test runs, needs to fix other tests
Some manifest tests may not work at all if script hashes are not in
merkle tree
  • Loading branch information
relatko committed Jan 5, 2024
1 parent 6449710 commit 008b00e
Show file tree
Hide file tree
Showing 12 changed files with 4,203 additions and 3,194 deletions.
8 changes: 5 additions & 3 deletions src/apdu_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,11 @@ __Z_INLINE void handleSign(volatile uint32_t *flags, volatile uint32_t *tx, uint
view_review_show(REVIEW_TXN);
*flags |= IO_ASYNCH_REPLY;
break;
case PROCESS_CHUNK_FINISHED_NO_METADATA:;
zemu_log("Processing chunks finished, no metadata.\n");
const char *error_msg = tx_parse();
case PROCESS_CHUNK_FINISHED_NFT1:
case PROCESS_CHUNK_FINISHED_NFT2:
case PROCESS_CHUNK_FINISHED_NO_METADATA:
case PROCESS_CHUNK_FINISHED_WITH_METADATA:;
const char *error_msg = tx_parse(callType);

if (error_msg != NULL) {
int error_msg_length = strlen(error_msg);
Expand Down
37 changes: 34 additions & 3 deletions src/common/tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "parser_tx.h"
#include <string.h>
#include "zxmacros.h"
#include "app_mode.h"

#if defined(TARGET_NANOX) || defined(TARGET_NANOS2) || defined(TARGET_STAX)
#define RAM_BUFFER_SIZE 8192
Expand Down Expand Up @@ -74,16 +75,46 @@ uint8_t *tx_get_buffer() {
return buffering_get_buffer()->data;
}

const char *tx_parse() {
uint8_t err = parser_parse(&ctx_parsed_tx, tx_get_buffer(), tx_get_buffer_length());
const char *tx_parse(process_chunk_response_t typeOfCall) {
script_parsed_type_t scriptType =
(typeOfCall == PROCESS_CHUNK_FINISHED_NFT1) ? SCRIPT_TYPE_NFT_SETUP_COLLECTION
: (typeOfCall == PROCESS_CHUNK_FINISHED_NFT2) ? SCRIPT_TYPE_NFT_TRANSFER
: SCRIPT_TYPE_UNKNOWN;
// parse tx
uint8_t err = parser_parse(&ctx_parsed_tx, tx_get_buffer(), tx_get_buffer_length(), scriptType);

if (err != PARSER_OK) {
return parser_getErrorDescription(err);
}

err = parser_validate(&ctx_parsed_tx);
// parse metadata
parser_tx_obj.metadataInitialized = false;
switch (typeOfCall) {
case PROCESS_CHUNK_FINISHED_WITH_METADATA:
MEMZERO(&parser_tx_obj.metadata, sizeof(parser_tx_obj.metadata));
err = parseTxMetadata(parser_tx_obj.hash.digest, &parser_tx_obj.metadata);
if (err != PARSER_OK) {
return parser_getErrorDescription(err);
}
parser_tx_obj.metadataInitialized = true;
break;
case PROCESS_CHUNK_FINISHED_NFT1:
case PROCESS_CHUNK_FINISHED_NFT2:
break; // we do not need metadata for these scripts
case PROCESS_CHUNK_FINISHED_NO_METADATA:
if (!app_mode_expert()) { // we do not need metadata for these scripts, but this
// workflow should work only in expert mode
return parser_getErrorDescription(PARSER_UNEXPECTED_SCRIPT);
}
break;
default:
return parser_getErrorDescription(PARSER_UNEXPECTED_ERROR);
}

CHECK_APP_CANARY()

// validate
err = parser_validate(&ctx_parsed_tx);
if (err != PARSER_OK) {
return parser_getErrorDescription(err);
}
Expand Down
3 changes: 2 additions & 1 deletion src/common/tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "os.h"
#include "coin.h"
#include "zxerror.h"
#include "app_helper.h"

void tx_initialize();

Expand Down Expand Up @@ -50,7 +51,7 @@ uint8_t *get_signable();
/// 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(process_chunk_response_t typeOfCall);

/// Release zbuffer memory
void tx_parse_reset();
Expand Down
4 changes: 2 additions & 2 deletions src/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "cx.h"

__Z_INLINE digest_type_e get_hash_type(const uint16_t options) {
const uint8_t hash_type = (uint8_t)(options & 0xFF);
const uint8_t hash_type = (uint8_t) (options & 0xFF);
switch (hash_type) {
case 0x01:
zemu_log_stack("path: sha2_256");
Expand All @@ -41,7 +41,7 @@ __Z_INLINE digest_type_e get_hash_type(const uint16_t options) {
}

__Z_INLINE cx_curve_t get_cx_curve(const uint16_t options) {
const uint8_t curve_code = (uint8_t)((options >> 8) & 0xFF);
const uint8_t curve_code = (uint8_t) ((options >> 8) & 0xFF);
switch (curve_code) {
case 0x02: {
zemu_log_stack("curve: secp256r1");
Expand Down
Loading

0 comments on commit 008b00e

Please sign in to comment.