Skip to content

Commit

Permalink
Merge pull request #7 from Zondax/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
chcmedeiros committed Mar 1, 2024
2 parents 732871b + 8ee520e commit ffc9ce3
Show file tree
Hide file tree
Showing 156 changed files with 289 additions and 191 deletions.
2 changes: 1 addition & 1 deletion app/Makefile.version
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ APPVERSION_M=0
# This is the minor version
APPVERSION_N=0
# This is the patch version
APPVERSION_P=8
APPVERSION_P=9
3 changes: 3 additions & 0 deletions app/src/addr.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>

#include "zxerror.h"

// Return the number of items in the address view
zxerr_t addr_getNumItems(uint8_t *num_items);
Expand Down
6 changes: 2 additions & 4 deletions app/src/apdu_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,10 @@ void extractHDPath(uint32_t rx, uint32_t offset) {

memcpy(hdPath, G_io_apdu_buffer + offset, sizeof(uint32_t) * HDPATH_LEN_DEFAULT);

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

hdPath_len = HDPATH_LEN_DEFAULT;
if (!mainnet) {
if (hdPath[0] != HDPATH_0_DEFAULT || ((hdPath[1] != HDPATH_1_DEFAULT) && (hdPath[1] != HDPATH_ETH_1_DEFAULT))) {
THROW(APDU_CODE_DATA_INVALID);
}
hdPath_len = HDPATH_LEN_DEFAULT;
}

uint8_t extractHRP(uint32_t rx, uint32_t offset) {
Expand Down
3 changes: 3 additions & 0 deletions app/src/eth_addr.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>

#include "zxerror.h"

/// Return the number of items in the address view
zxerr_t eth_addr_getNumItems(uint8_t *num_items);
Expand Down
35 changes: 17 additions & 18 deletions app/src/eth_erc20.h
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
/*******************************************************************************
* (c) 2018 - 2023 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.
********************************************************************************/
* (c) 2018 - 2023 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

#include <stdint.h>

#include "coin.h"
#include "parser_common.h"
#include "rlp.h"
#include "coin.h"


#ifdef __cplusplus
extern "C" {
Expand All @@ -34,8 +34,7 @@ typedef struct {

bool validateERC20(rlp_t data);
parser_error_t getERC20Token(const rlp_t *data, char tokenSymbol[MAX_SYMBOL_LEN], uint8_t *decimals);
parser_error_t printERC20Value(const rlp_t *data, char *outVal, uint16_t outValLen,
uint8_t pageIdx, uint8_t *pageCount);
parser_error_t printERC20Value(const rlp_t *data, char *outVal, uint16_t outValLen, uint8_t pageIdx, uint8_t *pageCount);

#ifdef __cplusplus
}
Expand Down
45 changes: 21 additions & 24 deletions app/src/eth_utils.h
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
/*******************************************************************************
* (c) 2018 - 2023 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.
********************************************************************************/
* (c) 2018 - 2023 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

#include <stdio.h>
#include <zxmacros.h>

#include "parser_common.h"
#include "rlp.h"

Expand All @@ -26,12 +27,11 @@ extern "C" {
#endif

typedef enum RlpError {
rlp_ok = 0,
rlp_no_data,
rlp_invalid_data,
rlp_ok = 0,
rlp_no_data,
rlp_invalid_data,
} rlp_error_t;


// Add two numbers returning UINT64_MAX if overflows
uint64_t saturating_add(uint64_t a, uint64_t b);

Expand All @@ -42,8 +42,7 @@ uint32_t saturating_add_u32(uint32_t a, uint32_t b);
// Gets the number of bytes read and the number of bytes to read
//
// Returns false if there is a error in the rlp encoded data, true otherwise.
rlp_error_t get_tx_rlp_len(const uint8_t *buffer, uint32_t len, uint64_t *read,
uint64_t *to_read);
rlp_error_t get_tx_rlp_len(const uint8_t *buffer, uint32_t len, uint64_t *read, uint64_t *to_read);

// Use to decode rlp data pointed by data.
// sets itemOffset to point to encoded data like item = data[itemOffset], and sets its len.
Expand All @@ -54,11 +53,9 @@ rlp_error_t parse_rlp_item(const uint8_t *data, uint32_t dataLen, uint32_t *read
// returns 0 on success, a negative number otherwise
parser_error_t be_bytes_to_u64(const uint8_t *bytes, uint8_t len, uint64_t *num);

parser_error_t printRLPNumber(const rlp_t *num, char* outVal, uint16_t outValLen,
uint8_t pageIdx, uint8_t *pageCount);
parser_error_t printRLPNumber(const rlp_t *num, char *outVal, uint16_t outValLen, uint8_t pageIdx, uint8_t *pageCount);

parser_error_t printEVMAddress(const rlp_t *address, char* outVal, uint16_t outValLen,
uint8_t pageIdx, uint8_t *pageCount);
parser_error_t printEVMAddress(const rlp_t *address, char *outVal, uint16_t outValLen, uint8_t pageIdx, uint8_t *pageCount);
#ifdef __cplusplus
}
#endif
2 changes: 2 additions & 0 deletions app/src/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>

#include "zxerror.h"

const char *hash_parse();
Expand Down
9 changes: 3 additions & 6 deletions app/src/parser_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,20 +159,17 @@ parser_error_t getNumItems(const parser_context_t *ctx, uint8_t *numItems) {
switch (ctx->tx_obj->tx_type) {
case p_export_tx:
// Tx + fee + Amounts(= n_outs) + Addresses
*numItems = 2 + ctx->tx_obj->tx.p_export_tx.secp_outs.n_addrs +
parser_get_renderable_outputs_number(ctx->tx_obj->tx.p_export_tx.secp_outs.out_render_mask) +
*numItems = 2 + ctx->tx_obj->tx.p_export_tx.secp_outs.n_addrs + ctx->tx_obj->tx.p_export_tx.secp_outs.n_outs +
expertModeHashField;
break;
case p_import_tx:
// Tx + fee + Amounts(= n_outs) + Addresses
*numItems = 2 + ctx->tx_obj->tx.p_import_tx.base_secp_outs.n_addrs +
parser_get_renderable_outputs_number(ctx->tx_obj->tx.p_import_tx.base_secp_outs.out_render_mask) +
expertModeHashField;
ctx->tx_obj->tx.p_import_tx.base_secp_outs.n_outs + expertModeHashField;
break;
case c_export_tx:
// Tx + fee + Amounts(= n_outs) + Addresses
*numItems = 2 + ctx->tx_obj->tx.c_export_tx.secp_outs.n_addrs +
parser_get_renderable_outputs_number(ctx->tx_obj->tx.c_export_tx.secp_outs.out_render_mask) +
*numItems = 2 + ctx->tx_obj->tx.c_export_tx.secp_outs.n_addrs + ctx->tx_obj->tx.c_export_tx.secp_outs.n_outs +
expertModeHashField;
break;
case c_import_tx:
Expand Down
97 changes: 26 additions & 71 deletions app/src/parser_impl_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,36 +193,11 @@ parser_error_t parse_evm_inputs(parser_context_t *c, evm_inputs_t *evm) {
return parser_ok;
}

bool parser_output_contains_change_address(parser_context_t *c) {
bool contains = false;
// verify address is renderable compare with CHANGE ADDRESS
#if defined(TARGET_NANOS) || defined(TARGET_NANOS2) || defined(TARGET_NANOX) || defined(TARGET_STAX)
CTX_CHECK_AVAIL(c, ADDRESS_LEN)
if (MEMCMP(c->buffer + c->offset, change_address, ADDRESS_LEN) == 0) {
contains = false;
} else {
contains = true;
}
#else
uint8_t test_change_address[ADDRESS_LEN] = {0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa};

CTX_CHECK_AVAIL(c, ADDRESS_LEN)
if (MEMCMP(c->buffer + c->offset, test_change_address, ADDRESS_LEN) == 0) {
contains = false;
} else {
contains = true;
}
#endif
return contains;
}

parser_error_t parse_transferable_secp_output(parser_context_t *c, transferable_out_secp_t *outputs, bool verify_locktime) {
if (outputs == NULL) {
return parser_unexpected_error;
}
outputs->out_sum = 0;
bool is_renderable = false;

for (uint32_t i = 0; i < outputs->n_outs; i++) {
// skip assetId
Expand Down Expand Up @@ -259,19 +234,9 @@ parser_error_t parse_transferable_secp_output(parser_context_t *c, transferable_
return parser_unexpected_threshold;
}

// Following AVAX logic 99.99% of tx have only one address, so we should test if it matches the change_address, in
// case the output has more then one address then the output should be rendered with all its addresses
if (tmp_n_adresses == 1) {
is_renderable = parser_output_contains_change_address(c);
outputs->out_render_mask |= (is_renderable ? 1 : 0) << i;
for (uint32_t j = 0; j < tmp_n_adresses; j++) {
verifyBytes(c, ADDRESS_LEN);
outputs->n_addrs = is_renderable ? (outputs->n_addrs + 1) : outputs->n_addrs;
} else {
outputs->out_render_mask |= 1 << i;
for (uint32_t j = 0; j < tmp_n_adresses; j++) {
verifyBytes(c, ADDRESS_LEN);
outputs->n_addrs++;
}
outputs->n_addrs++;
}
}

Expand All @@ -283,11 +248,9 @@ parser_error_t parse_evm_output(parser_context_t *c, evm_outs_t *outputs) {
return parser_unexpected_error;
}
outputs->out_sum = 0;
uint8_t is_renderable = 0;

for (uint32_t i = 0; i < outputs->n_outs; i++) {
// Check address is renderable
is_renderable = parser_output_contains_change_address(c);
outputs->out_render_mask |= (is_renderable ? 1 : 0) << i;
verifyBytes(c, ADDRESS_LEN);

// Save amount to total
Expand Down Expand Up @@ -399,31 +362,27 @@ parser_error_t parser_get_secp_output_for_index(parser_context_t *out_ctx, trans
uint64_t count = 0;
uint32_t out_n_addr = 0;
for (uint32_t i = 0; i < secp_outs.n_outs; i++) {
if ((secp_outs.out_render_mask & (1 << i)) == 0) {
CHECK_ERROR(parser_go_to_next_transferable_output(out_ctx));
} else {
// read amount and check if its the index we are looking for return element 0 for amount print
CHECK_ERROR(verifyBytes(out_ctx, AMOUNT_OFFSET));
CHECK_ERROR(read_u64(out_ctx, amount));
// read amount and check if its the index we are looking for return element 0 for amount print
CHECK_ERROR(verifyBytes(out_ctx, AMOUNT_OFFSET));
CHECK_ERROR(read_u64(out_ctx, amount));
if (count == inner_displayIdx) {
*element_idx = 0;
return parser_ok;
}

CHECK_ERROR(verifyBytes(out_ctx, N_ADDRESS_OFFSET));
CHECK_ERROR(read_u32(out_ctx, &out_n_addr));
// Go through output addresses and check if its the index we are looking for return element >0 for address print
for (uint32_t j = 1; j <= out_n_addr; j++) {
CHECK_ERROR(readBytes(out_ctx, address, ADDRESS_LEN));
count++;
if (count == inner_displayIdx) {
*element_idx = 0;
*element_idx = j;
return parser_ok;
}

CHECK_ERROR(verifyBytes(out_ctx, N_ADDRESS_OFFSET));
CHECK_ERROR(read_u32(out_ctx, &out_n_addr));
// Go through output addresses and check if its the index we are looking for return element >0 for address print
for (uint32_t j = 1; j <= out_n_addr; j++) {
CHECK_ERROR(readBytes(out_ctx, address, ADDRESS_LEN));
count++;
if (count == inner_displayIdx) {
*element_idx = j;
return parser_ok;
}
}
// We did not find the index in the address add one for the next amoount we are about to read
count++;
}
// We did not find the index in the address add one for the next amoount we are about to read
count++;
}
return parser_unexpected_number_items;
}
Expand All @@ -436,18 +395,14 @@ parser_error_t parser_get_evm_output_index(parser_context_t *out_ctx, evm_outs_t
uint64_t count = 0;

for (uint32_t i = 0; i < evm_outs.n_outs; i++) {
if ((evm_outs.out_render_mask & (1 << i)) == 0) {
CHECK_ERROR(parser_go_to_next_transferable_output(out_ctx));
} else {
CHECK_ERROR(readBytes(out_ctx, address, ADDRESS_LEN));
CHECK_ERROR(read_u64(out_ctx, amount));
CHECK_ERROR(verifyBytes(out_ctx, ASSET_ID_LEN));
CHECK_ERROR(readBytes(out_ctx, address, ADDRESS_LEN));
CHECK_ERROR(read_u64(out_ctx, amount));
CHECK_ERROR(verifyBytes(out_ctx, ASSET_ID_LEN));

if (count == out_index) {
return parser_ok;
}
count++;
if (count == out_index) {
return parser_ok;
}
count++;
}
return parser_unexpected_number_items;
}
Expand Down
4 changes: 3 additions & 1 deletion app/src/parser_impl_eth.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

eth_tx_t eth_tx_obj;
#define FLARE_MAINNET_CHAINID 14
#define SONG_BIRD_CHAINID 19
#define COSTON2_CHAINID 114

static parser_error_t readChainID(parser_context_t *ctx, rlp_t *chainId) {
if (ctx == NULL || chainId == NULL) {
Expand All @@ -49,7 +51,7 @@ static parser_error_t readChainID(parser_context_t *ctx, rlp_t *chainId) {
}

// Check allowed values for chain id
if (tmpChainId != FLARE_MAINNET_CHAINID) {
if (tmpChainId != FLARE_MAINNET_CHAINID && tmpChainId != SONG_BIRD_CHAINID && tmpChainId != COSTON2_CHAINID) {
return parser_invalid_chain_id;
}

Expand Down
2 changes: 0 additions & 2 deletions app/src/parser_txdef.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ typedef struct {
const uint8_t *outs;
uint16_t outs_offset;
uint64_t out_sum;
uint64_t out_render_mask;
uint32_t n_addrs;
} transferable_out_secp_t;

Expand All @@ -132,7 +131,6 @@ typedef struct {
const uint8_t *outs;
uint16_t outs_offset;
uint64_t out_sum;
uint64_t out_render_mask;
} evm_outs_t;

typedef struct {
Expand Down

0 comments on commit ffc9ce3

Please sign in to comment.