Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/parser support #2

Merged
merged 7 commits into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,14 @@ file(GLOB_RECURSE LIB_SRC
####
${CMAKE_CURRENT_SOURCE_DIR}/app/src/parser.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/parser_impl.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/parser_utils.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/lisk_base32.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/crypto_helper.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/parser_print_items.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/txn_token_module.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/txn_auth_module.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/txn_dpos_module.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/txn_legacy_module.c
${CMAKE_CURRENT_SOURCE_DIR}/deps/picohash/
)

Expand Down
2 changes: 1 addition & 1 deletion app/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ APP_LOAD_PARAMS = --delete $(COMMON_LOAD_PARAMS) --path $(APPPATH)

ifeq ($(TARGET_NAME),TARGET_NANOS)
APP_LOAD_PARAMS += --appFlags 0x000
APP_STACK_SIZE:=3000
APP_STACK_SIZE:=2800
ICONNAME:=$(CURDIR)/nanos_icon.gif
OUTPUT_ELF ?= $(CURDIR)/output/app_s.elf
OUTPUT_INSTALLER := $(CURDIR)/pkg/installer_s.sh
Expand Down
4 changes: 2 additions & 2 deletions app/Makefile.version
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This is the `transaction_version` field of `Runtime`
APPVERSION_M=0
APPVERSION_M=1
ftheirs marked this conversation as resolved.
Show resolved Hide resolved
# This is the `spec_version` field of `Runtime`
APPVERSION_N=0
# This is the patch version of this release
APPVERSION_P=4
APPVERSION_P=0
23 changes: 7 additions & 16 deletions app/src/apdu_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "actions.h"
#include "tx.h"
#include "addr.h"
#include "msg.h"
#include "crypto.h"
#include "coin.h"
#include "zxmacros.h"
Expand All @@ -53,17 +54,16 @@ void extractHDPath(uint32_t rx, uint32_t offset) {
hdPath.pathLength = pathLength;
memcpy(hdPath.path, G_io_apdu_buffer + offset, sizeof(uint32_t) * pathLength);

#if 0
const bool mainnet = hdPath[0] == HDPATH_0_DEFAULT &&
hdPath[1] == HDPATH_1_DEFAULT;
const bool mainnet = hdPath.path[0] == HDPATH_0_DEFAULT &&
hdPath.path[1] == HDPATH_1_DEFAULT;

const bool testnet = hdPath[0] == HDPATH_0_DEFAULT &&
hdPath[1] == HDPATH_1_TESTNET;
const bool testnet = hdPath.path[0] == HDPATH_0_DEFAULT &&
hdPath.path[1] == HDPATH_1_TESTNET;

if (!mainnet && !testnet) {
THROW(APDU_CODE_DATA_INVALID);
}
#endif

}

__Z_INLINE bool process_chunk(__Z_UNUSED volatile uint32_t *tx, uint32_t rx) {
Expand Down Expand Up @@ -152,16 +152,7 @@ __Z_INLINE void handleSignMessage(volatile uint32_t *flags, volatile uint32_t *t
THROW(APDU_CODE_OK);
}

const char *error_msg = tx_parse();
CHECK_APP_CANARY()
if (error_msg != NULL) {
int error_msg_length = strlen(error_msg);
memcpy(G_io_apdu_buffer, error_msg, error_msg_length);
*tx += (error_msg_length);
THROW(APDU_CODE_DATA_INVALID);
}

view_review_init(tx_getItem, tx_getNumItems, app_sign_message);
view_review_init(msg_getItem, msg_getNumItems, app_sign_message);
view_review_show(REVIEW_TXN);
*flags |= IO_ASYNCH_REPLY;
}
Expand Down
3 changes: 2 additions & 1 deletion app/src/coin.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ extern "C" {
#define MAX_SIGN_SIZE 256u
#define BLAKE2B_DIGEST_SIZE 32u

#define COIN_AMOUNT_DECIMAL_PLACES 18
#define COIN_AMOUNT_DECIMAL_PLACES 8
#define COIN_TICKER "LSK "
#define UNVOTE_COIN_TICKER "LSK -"
#define COIN_HRP "lsk"

#define MENU_MAIN_APP_LINE1 "Lisk"
Expand Down
1 change: 1 addition & 0 deletions app/src/common/parser_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ typedef enum {
parser_unexpected_type,
parser_unexpected_method,
parser_unexpected_buffer_end,
parser_unexpected_unparsed_bytes,
parser_unexpected_value,
parser_unexpected_number_items,
parser_unexpected_version,
Expand Down
76 changes: 76 additions & 0 deletions app/src/msg.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*******************************************************************************
* (c) 2018 - 2022 Zondax AG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
********************************************************************************/

#include <stdio.h>
#include "coin.h"
#include "zxerror.h"
#include "zxmacros.h"
#include "zxformat.h"
#include "app_mode.h"
#include "crypto.h"
#include "parser_utils.h"
#include "crypto_helper.h"
#include "tx.h"

#define IS_PRINTABLE(c) ((c >= 0x20 && c <= 0x7e) || (c >= 0x80 && c <= 0xFF))

zxerr_t msg_getNumItems(uint8_t *num_items) {
zemu_log_stack("msg_getNumItems");
*num_items = 1;
return zxerr_ok;
}

zxerr_t msg_getItem(int8_t displayIdx,
char *outKey, uint16_t outKeyLen,
char *outVal, uint16_t outValLen,
uint8_t pageIdx, uint8_t *pageCount) {
ZEMU_LOGF(200, "[msg_getItem] %d/%d\n", displayIdx, pageIdx)

const uint8_t *message = tx_get_buffer();
const uint16_t messageLength = tx_get_buffer_length();

if(messageLength == 0) {
return zxerr_no_data;
}

switch (displayIdx) {
case 0: {
snprintf(outKey, outKeyLen, "Msg hex");
uint8_t npc = 0; //Non Printable Chars Counter

for (uint8_t i=0; i < messageLength; i++) {
npc += IS_PRINTABLE(message[i]) ?
0 /* Printable Char */:
1 /* Non Printable Char */;
}

// msg in hex in case >= than 40% is non printable
// or first char is not printable.
if ((npc*100) / messageLength >= 40 || ! IS_PRINTABLE(message[0])) {
pageStringHex(outVal, outValLen, (const char*)message, messageLength, pageIdx, pageCount);
return zxerr_ok;
}

//print message
snprintf(outKey, outKeyLen, "Msg");
pageString(outVal, outValLen, (const char*)message, pageIdx, pageCount);
return zxerr_ok;
}
default:
return zxerr_no_data;
}

}
34 changes: 34 additions & 0 deletions app/src/msg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*******************************************************************************
* (c) 2018 - 2022 Zondax AG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
********************************************************************************/

#pragma once

#ifdef __cplusplus
extern "C" {
#endif

// Return the number of items in the address view
zxerr_t msg_getNumItems(uint8_t *num_items);

// Gets an specific item from the address view (including paging)
zxerr_t msg_getItem(int8_t displayIdx,
char *outKey, uint16_t outKeyLen,
char *outValue, uint16_t outValueLen,
uint8_t pageIdx, uint8_t *pageCount);

#ifdef __cplusplus
}
#endif
99 changes: 72 additions & 27 deletions app/src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,18 @@
#include "parser_common.h"
#include "parser_impl.h"
#include "parser.h"
#include "parser_print_items.h"

#include "txn_token_module.h"
#include "txn_auth_module.h"
#include "txn_dpos_module.h"
#include "txn_legacy_module.h"

#include "crypto.h"
#include "crypto_helper.h"

#include "app_mode.h"


parser_error_t parser_init_context(parser_context_t *ctx,
const uint8_t *buffer,
Expand Down Expand Up @@ -55,7 +63,6 @@ parser_error_t parser_parse(parser_context_t *ctx,
}

parser_error_t parser_validate(parser_context_t *ctx) {
#if 0
// Iterate through all items to check that all can be shown and are valid
uint8_t numItems = 0;
CHECK_ERROR(parser_getNumItems(ctx, &numItems))
Expand All @@ -67,38 +74,37 @@ parser_error_t parser_validate(parser_context_t *ctx) {
uint8_t pageCount = 0;
CHECK_ERROR(parser_getItem(ctx, idx, tmpKey, sizeof(tmpKey), tmpVal, sizeof(tmpVal), 0, &pageCount))
}
#endif

return parser_ok;
}

parser_error_t parser_getNumItems(const parser_context_t *ctx, uint8_t *num_items) {
// #{TODO} --> function to retrieve num Items
// *num_items = _getNumItems();
*num_items = 1;
*num_items = _getNumItems(ctx);
if(*num_items == 0) {
return parser_unexpected_number_items;
}
return parser_ok;
}

static void cleanOutput(char *outKey, uint16_t outKeyLen,
char *outVal, uint16_t outValLen)
{
char *outVal, uint16_t outValLen) {
MEMZERO(outKey, outKeyLen);
MEMZERO(outVal, outValLen);
snprintf(outKey, outKeyLen, "?");
snprintf(outVal, outValLen, " ");
}

static parser_error_t checkSanity(uint8_t numItems, uint8_t displayIdx)
{
static parser_error_t checkSanity(uint8_t numItems, uint8_t displayIdx) {
if ( displayIdx >= numItems) {
return parser_display_idx_out_of_range;
}
return parser_ok;
}

parser_error_t parser_getTxNumItems(const parser_context_t *ctx, uint8_t *tx_num_items) {
*tx_num_items = _getTxNumItems();
return parser_ok;
}

parser_error_t parser_getItem(const parser_context_t *ctx,
uint8_t displayIdx,
char *outKey, uint16_t outKeyLen,
Expand All @@ -113,24 +119,63 @@ parser_error_t parser_getItem(const parser_context_t *ctx,
CHECK_ERROR(checkSanity(numItems, displayIdx))
cleanOutput(outKey, outKeyLen, outVal, outValLen);

switch (displayIdx)
uint8_t txItems = 0;
CHECK_ERROR(parser_getTxNumItems(ctx, &txItems))

const uint8_t common_items = _getNumCommonItems();

if (displayIdx < common_items) {
return print_common_items(ctx, displayIdx, outKey, outKeyLen,
outVal, outValLen, pageIdx, pageCount);
}

displayIdx-=common_items;
uint8_t txDisplayIdx = 0;

switch (ctx->tx_obj->module_id)
{
case 0: {
uint8_t hash[32] = {0};
char hash_str[100] = {0};
CHECK_ERROR(crypto_hash(ctx->buffer, ctx->bufferLen,
hash, sizeof(hash)))
array_to_hexstr((char*) &hash_str, sizeof(hash_str),
(const uint8_t*) &hash, sizeof(hash));

// Display Item 0
snprintf(outKey, outKeyLen, "Txn hash");
pageString(outVal, outValLen, (const char*) &hash_str, pageIdx, pageCount);
return parser_ok;
}

default:
break;
case TX_MODULE_ID_TOKEN:
switch (ctx->tx_obj->command_id)
{
case TX_COMMAND_ID_TRANSFER:
return print_module_token_transfer(ctx, displayIdx, outKey, outKeyLen,
outVal, outValLen, pageIdx, pageCount);
case TX_COMMAND_ID_CROSSCHAIN_TRANSFER:
return print_module_token_cross(ctx, displayIdx, outKey, outKeyLen,
outVal, outValLen, pageIdx, pageCount);
default:
return parser_unexpected_value;
}
case TX_MODULE_ID_AUTH:
CHECK_ERROR(getItem(displayIdx, &txDisplayIdx))
return print_module_auth_reg(ctx, displayIdx, txDisplayIdx, outKey, outKeyLen,
outVal, outValLen, pageIdx, pageCount);

case TX_MODULE_ID_DPOS:
switch (ctx->tx_obj->command_id)
{
case TX_COMMAND_ID_REGISTER_DELEGATE:
return print_module_dpos_reg_delegate(ctx, displayIdx, outKey, outKeyLen,
outVal, outValLen, pageIdx, pageCount);

case TX_COMMAND_ID_VOTE_DELEGATE:
CHECK_ERROR(getItem(displayIdx, &txDisplayIdx))
return print_module_dpos_vote(ctx, displayIdx, txDisplayIdx, outKey, outKeyLen,
outVal, outValLen, pageIdx, pageCount);

case TX_COMMAND_ID_UNLOCK_TOKEN:
case TX_COMMAND_ID_REPORT_DELEGATE_MISBEHAVIOUR:
default:
return parser_unexpected_value;
}

case TX_MODULE_ID_LEGACY:
return print_module_legacy_reclaim(ctx, displayIdx, outKey, outKeyLen,
outVal, outValLen, pageIdx, pageCount);

default:
return parser_unexpected_value;

}

return parser_display_idx_out_of_range;
Expand Down
Loading