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 2002139
Show file tree
Hide file tree
Showing 16 changed files with 4,216 additions and 3,199 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/misspellings_checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ jobs:
with:
builtin: clear,rare
check_filenames: true
skip: ./deps/jsmn/test/tests.c
skip: ./deps/jsmn/test/tests.c, ./transaction_metadata/manifest.testnet.json, ./transaction_metadata/manifest.mainnet.json
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
4 changes: 2 additions & 2 deletions src/common/app_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ process_chunk_response_t process_chunk(__Z_UNUSED volatile uint32_t *tx, uint32_
}
switch (p2) {
case 0x01:
zemu_log("Transaction witout metadata.\n");
zemu_log("Transaction without metadata.\n");
return PROCESS_CHUNK_FINISHED_NO_METADATA;
case 0x02:
zemu_log("Transaction to match nft1 script.\n");
Expand All @@ -90,7 +90,7 @@ process_chunk_response_t process_chunk(__Z_UNUSED volatile uint32_t *tx, uint32_
zemu_log("Transaction to match nft2 script.\n");
return PROCESS_CHUNK_FINISHED_NFT2;
default:
zemu_log("Incalid P2 transaction type.\n");
zemu_log("Invalid P2 transaction type.\n");
THROW(APDU_CODE_INVALIDP1P2);
}
case 0x03:
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
Loading

0 comments on commit 2002139

Please sign in to comment.