Skip to content

Commit

Permalink
format files and update snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
abenso committed Sep 4, 2024
1 parent eb2dc64 commit 7884404
Show file tree
Hide file tree
Showing 374 changed files with 179 additions and 149 deletions.
65 changes: 37 additions & 28 deletions app/src/apdu_handler_legacy.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "actions.h"
#include "addr.h"
#include "view_internal.h"
#include "zxformat.h"

static bool tx_initialized = false;
static uint32_t payload_length = 0;
Expand All @@ -31,7 +30,7 @@ static uint8_t items = 0;
static uint8_t item_len = 0;
static bool check_item_len = false;

__Z_INLINE void legacy_app_sign() {
void legacy_app_sign() {
const uint8_t *message = tx_get_buffer();
const uint16_t messageLength = tx_get_buffer_length() - hdpath_length;

Expand All @@ -46,6 +45,28 @@ __Z_INLINE void legacy_app_sign() {
}
}

void legacy_app_sign_transference() {
const uint8_t *message = (uint8_t *)tx_get_json_template_buffer();
const uint16_t messageLength = tx_get_json_template_buffer_len();

// get pubkey
zxerr_t zxerr = app_fill_address();
if (zxerr != zxerr_ok) {
THROW(APDU_CODE_DATA_INVALID);
}
MEMMOVE(G_io_apdu_buffer + SK_LEN_25519, G_io_apdu_buffer, action_addrResponseLen);

zxerr = crypto_sign(G_io_apdu_buffer, IO_APDU_BUFFER_SIZE - 3, message, messageLength, tx_type);

if (zxerr != zxerr_ok) {
set_code(G_io_apdu_buffer, 0, APDU_CODE_SIGN_VERIFY_ERROR);
io_exchange(CHANNEL_APDU | IO_RETURN_AFTER_TX, 2);
} else {
set_code(G_io_apdu_buffer, SK_LEN_25519 + action_addrResponseLen, APDU_CODE_OK);
io_exchange(CHANNEL_APDU | IO_RETURN_AFTER_TX, SK_LEN_25519 + action_addrResponseLen + 2);
}
}

void legacy_app_reply_address() {
if (action_addrResponseLen + 1 > sizeof(G_io_apdu_buffer)) {
THROW(APDU_CODE_OUTPUT_BUFFER_TOO_SMALL);
Expand All @@ -60,7 +81,7 @@ void legacy_app_reply_address() {
io_exchange(CHANNEL_APDU | IO_RETURN_AFTER_TX, action_addrResponseLen + 2);
}

void legacy_extractHDPath(uint8_t *buffer, uint32_t rx, uint32_t offset, bool check_len) {
void legacy_extractHDPath(uint8_t *buffer, uint32_t rx, uint32_t offset, uint8_t check_len) {
if (rx < offset) {
THROW(APDU_CODE_WRONG_LENGTH);
}
Expand All @@ -69,22 +90,14 @@ void legacy_extractHDPath(uint8_t *buffer, uint32_t rx, uint32_t offset, bool ch
uint8_t hdPathLen = hdPathQty * sizeof(uint32_t);
uint32_t offset_hdpath_data = offset + 1;

ZEMU_LOGF(50, "hdPathQty: %d\n", hdPathQty);
ZEMU_LOGF(50, "hdPathLen: %d\n", hdPathLen);
ZEMU_LOGF(50, "offset_hdpath_data: %d\n", offset_hdpath_data);

if (check_len) {
if (rx - offset_hdpath_data != hdPathLen) {
THROW(APDU_CODE_WRONG_LENGTH);
}
if ((check_len == 1) && (rx - offset_hdpath_data != hdPathLen)) {
THROW(APDU_CODE_WRONG_LENGTH);
}

MEMCPY(hdPath, buffer + offset_hdpath_data, hdPathLen);

const bool mainnet = hdPath[0] == HDPATH_0_DEFAULT && hdPath[1] == HDPATH_1_DEFAULT;

ZEMU_LOGF(50, "mainnet: %d\n", mainnet);

if (!mainnet) {
THROW(APDU_CODE_DATA_INVALID);
}
Expand All @@ -108,7 +121,6 @@ bool legacy_check_end_of_chunk() {
}

uint32_t legacy_check_request(volatile uint32_t *tx) {

// check buffer length
uint32_t tx_buffer_length = tx_get_buffer_length();
uint8_t *tx_buffer = tx_get_buffer();
Expand All @@ -121,7 +133,7 @@ uint32_t legacy_check_request(volatile uint32_t *tx) {
}

// get hdpath
legacy_extractHDPath(tx_buffer, tx_buffer_length, payload_length, true);
legacy_extractHDPath(tx_buffer, tx_buffer_length, payload_length, 1);

// verify sizes
if (tx_buffer_length != hdpath_length + payload_length) {
Expand All @@ -137,10 +149,6 @@ uint32_t legacy_check_request(volatile uint32_t *tx) {
void legacy_append_data(uint8_t *buffer, uint32_t len) {
uint32_t added = tx_append(buffer, len);

char print[200] = {0};
array_to_hexstr(print, sizeof(print), buffer, len);
ZEMU_LOGF(200, "legacy_append_data: %s\n", print);

if (added != len) {
tx_reset();
tx_initialized = false;
Expand All @@ -149,7 +157,7 @@ void legacy_append_data(uint8_t *buffer, uint32_t len) {
}

void legacy_handleGetAddr(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx, const uint8_t requireConfirmation) {
legacy_extractHDPath(G_io_apdu_buffer, rx, LEGACY_OFFSET_HDPATH_SIZE, true);
legacy_extractHDPath(G_io_apdu_buffer, rx, LEGACY_OFFSET_HDPATH_SIZE, 1);

zxerr_t zxerr = app_fill_address();
if (zxerr != zxerr_ok) {
Expand Down Expand Up @@ -184,7 +192,7 @@ bool legacy_process_chunk(__Z_UNUSED volatile uint32_t *tx, uint32_t rx, bool ha
// read length of data
if (has_len) {
payload_length = (uint32_t)G_io_apdu_buffer[5] | ((uint32_t)G_io_apdu_buffer[6] << 8) |
((uint32_t)G_io_apdu_buffer[7] << 16) | ((uint32_t)G_io_apdu_buffer[8] << 24);
((uint32_t)G_io_apdu_buffer[7] << 16) | ((uint32_t)G_io_apdu_buffer[8] << 24);

offset = LEGACY_PAYLOAD_LEN_BYTES + LEGACY_HEADER_LENGTH;
payload_size = rx - offset;
Expand All @@ -199,7 +207,7 @@ bool legacy_process_chunk(__Z_UNUSED volatile uint32_t *tx, uint32_t rx, bool ha
tx_reset();
tx_initialized = true;
}

// TODO: use legacy_append_data
uint32_t added = tx_append(&(G_io_apdu_buffer[offset]), payload_size);
if (added != payload_size) {
Expand All @@ -218,7 +226,7 @@ bool legacy_process_chunk(__Z_UNUSED volatile uint32_t *tx, uint32_t rx, bool ha
}

static uint32_t legacy_initialize_transfer() {
legacy_extractHDPath(G_io_apdu_buffer, LEGACY_OFFSET_HDPATH_SIZE + 1, LEGACY_OFFSET_HDPATH_SIZE, false);
legacy_extractHDPath(G_io_apdu_buffer, LEGACY_OFFSET_HDPATH_SIZE + 1, LEGACY_OFFSET_HDPATH_SIZE, 0);

tx_initialize();
tx_reset();
Expand Down Expand Up @@ -300,7 +308,7 @@ bool legacy_process_transfer_chunk(uint32_t rx) {
}

legacy_append_data(&G_io_apdu_buffer[offset], payload_size + 1);

if (++items > LEGACY_TRANSFER_NUM_ITEMS) {
THROW(APDU_CODE_DATA_INVALID);
}
Expand Down Expand Up @@ -365,16 +373,17 @@ void legacy_handleSignHash(volatile uint32_t *flags, volatile uint32_t *tx, uint
*flags |= IO_ASYNCH_REPLY;
}

// bytes: | 1 | 1 | 1 | 1 | 1 | 4*hdpath_qty | n |
// data: | CLA | INS | P1 | P2 | hdpath_qty | hdpath_data | payload |
// clang-format off
// bytes: | 1 | 1 | 1 | 1 | 1 | 4*hdpath_qty | 1 | param_1_len | 1 | param_2_len | ... | 1 | param_12_len |
// data: | CLA | INS | P1 | P2 | hdpath_qty | hdpath_data | param_1_len | param_1_data | param_2_len | param_2_data | ... | param_12_len | param_12_data |
// clang-format on
void legacy_handleSignTransferTx(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx) {
zemu_log("handleSignLegacyTransferTx\n");
if (!legacy_process_transfer_chunk(rx)) {
THROW(APDU_CODE_OK);
}

uint32_t buffer_length = tx_get_buffer_length();
ZEMU_LOGF(50, "buffer_length: %d\n", buffer_length);

const char *error_msg = tx_parse(buffer_length, tx_type_transaction);
tx_type = tx_type_transaction;
Expand All @@ -385,7 +394,7 @@ void legacy_handleSignTransferTx(volatile uint32_t *flags, volatile uint32_t *tx
THROW(APDU_CODE_LEGACY_PARSER_ERROR);
}

view_review_init(tx_getItem, tx_getNumItems, legacy_app_sign);
view_review_init(tx_getItem, tx_getNumItems, legacy_app_sign_transference);
view_review_show(REVIEW_TXN);
*flags |= IO_ASYNCH_REPLY;
}
2 changes: 1 addition & 1 deletion app/src/json/json_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ parser_error_t json_parse(parsed_json_t *parsed_json, const char *buffer, uint16
parsed_json->buffer = buffer;
parsed_json->bufferLen = bufferLen;

int32_t num_tokens =
int16_t num_tokens =
jsmn_parse(&parser, parsed_json->buffer, parsed_json->bufferLen, parsed_json->tokens, MAX_NUMBER_OF_TOKENS);

ZEMU_LOGF(35, "num_tokens: %d\n", num_tokens);
Expand Down
6 changes: 3 additions & 3 deletions tests_zemu/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"format:check": "FORCE_COLOR=1 prettier --check .",
"lint": "eslint .",
"lint:fix": "eslint --fix .",
"test": "jest tests/legacy.test.ts",
"test": "jest",
"try": "node try.mjs",
"try_legacy": "node try_legacy.mjs",
"upgrade": "bunx npm-check-updates -i"
Expand All @@ -46,13 +46,13 @@
"eslint-plugin-promise": "^7.0.0",
"eslint-plugin-tsdoc": "^0.3.0",
"eslint-plugin-unused-imports": "^4.0.1",
"hw-app-kda": "git+https://github.com/obsidiansystems/hw-app-kda",
"jest": "29.7.0",
"jssha": "^3.3.1",
"prettier": "^3.3.3",
"sort-package-json": "^1.52.0",
"ts-jest": "^29.2.3",
"ts-node": "^10.9.2",
"typescript": "^5.5.4",
"hw-app-kda": "git+https://github.com/obsidiansystems/hw-app-kda"
"typescript": "^5.5.4"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/s-show_address-legacy/00000.png
Binary file added tests_zemu/snapshots/s-show_address-legacy/00001.png
Binary file added tests_zemu/snapshots/s-show_address-legacy/00002.png
Binary file added tests_zemu/snapshots/s-show_address-legacy/00003.png
Binary file added tests_zemu/snapshots/s-sign_hash_1_legacy/00000.png
Binary file added tests_zemu/snapshots/s-sign_hash_1_legacy/00001.png
Binary file added tests_zemu/snapshots/s-sign_hash_1_legacy/00002.png
Binary file added tests_zemu/snapshots/s-sign_hash_1_legacy/00003.png
Binary file added tests_zemu/snapshots/s-sign_hash_1_legacy/00004.png
Binary file added tests_zemu/snapshots/s-sign_hash_1_legacy/00005.png
Binary file added tests_zemu/snapshots/s-sign_hash_1_legacy/00006.png
Binary file added tests_zemu/snapshots/s-sign_hash_1_legacy/00007.png
Binary file added tests_zemu/snapshots/s-sign_hash_1_legacy/00008.png
Binary file added tests_zemu/snapshots/s-sign_hash_1_legacy/00009.png
Binary file added tests_zemu/snapshots/s-sign_hash_2_legacy/00000.png
Binary file added tests_zemu/snapshots/s-sign_hash_2_legacy/00001.png
Binary file added tests_zemu/snapshots/s-sign_hash_2_legacy/00002.png
Binary file added tests_zemu/snapshots/s-sign_hash_2_legacy/00003.png
Binary file added tests_zemu/snapshots/s-sign_hash_2_legacy/00004.png
Binary file added tests_zemu/snapshots/s-sign_hash_2_legacy/00005.png
Binary file added tests_zemu/snapshots/s-sign_hash_2_legacy/00006.png
Binary file added tests_zemu/snapshots/s-sign_hash_2_legacy/00007.png
Binary file added tests_zemu/snapshots/s-sign_hash_2_legacy/00008.png
Binary file added tests_zemu/snapshots/s-sign_hash_2_legacy/00009.png
Binary file added tests_zemu/snapshots/sp-sign_hash_1_legacy/00000.png
Binary file added tests_zemu/snapshots/sp-sign_hash_1_legacy/00001.png
Binary file added tests_zemu/snapshots/sp-sign_hash_1_legacy/00002.png
Binary file added tests_zemu/snapshots/sp-sign_hash_1_legacy/00003.png
Binary file added tests_zemu/snapshots/sp-sign_hash_1_legacy/00004.png
Binary file added tests_zemu/snapshots/sp-sign_hash_1_legacy/00005.png
Binary file added tests_zemu/snapshots/sp-sign_hash_1_legacy/00006.png
Binary file added tests_zemu/snapshots/sp-sign_hash_1_legacy/00007.png
Binary file added tests_zemu/snapshots/sp-sign_hash_1_legacy/00008.png
Binary file added tests_zemu/snapshots/sp-sign_hash_2_legacy/00000.png
Binary file added tests_zemu/snapshots/sp-sign_hash_2_legacy/00001.png
Binary file added tests_zemu/snapshots/sp-sign_hash_2_legacy/00002.png
Binary file added tests_zemu/snapshots/sp-sign_hash_2_legacy/00003.png
Binary file added tests_zemu/snapshots/sp-sign_hash_2_legacy/00004.png
Binary file added tests_zemu/snapshots/sp-sign_hash_2_legacy/00005.png
Binary file added tests_zemu/snapshots/sp-sign_hash_2_legacy/00006.png
Binary file added tests_zemu/snapshots/sp-sign_hash_2_legacy/00007.png
Binary file added tests_zemu/snapshots/sp-sign_hash_2_legacy/00008.png
Binary file modified tests_zemu/snapshots/sp-sign_simple_transfer_legacy/00012.png
Binary file added tests_zemu/snapshots/sp-sign_transfer_1/00016.png
Binary file added tests_zemu/snapshots/x-show_address-legacy/00000.png
Binary file added tests_zemu/snapshots/x-show_address-legacy/00001.png
Binary file added tests_zemu/snapshots/x-show_address-legacy/00002.png
Binary file added tests_zemu/snapshots/x-show_address-legacy/00003.png
Binary file added tests_zemu/snapshots/x-show_address-legacy/00004.png
Binary file added tests_zemu/snapshots/x-sign_hash_1_legacy/00000.png
Binary file added tests_zemu/snapshots/x-sign_hash_1_legacy/00001.png
Binary file added tests_zemu/snapshots/x-sign_hash_1_legacy/00002.png
Binary file added tests_zemu/snapshots/x-sign_hash_1_legacy/00003.png
Loading

0 comments on commit 7884404

Please sign in to comment.