Skip to content

Commit

Permalink
take into account PR review remarks
Browse files Browse the repository at this point in the history
  • Loading branch information
tdejoigny-ledger committed Nov 6, 2024
1 parent 6f9f50c commit 25081bb
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 20 deletions.
1 change: 0 additions & 1 deletion src/common/base64.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ int base64url_decode(const char *src, size_t src_len, uint8_t *dest, size_t dest
return -1;
}
*(dest++) = (pr2six[(uint8_t) src[2]] << 6 | pr2six[(uint8_t) src[3]] >> 0);
dest_len--;
}

return dest - original_dest;
Expand Down
2 changes: 1 addition & 1 deletion src/common/bip32_check.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "bip32_check.h"

bool check_bip32_path(uint8_t bip32_path_len, uint32_t bip32_path[MAX_BIP32_PATH]) {
bool check_bip32_path(uint8_t bip32_path_len, const uint32_t bip32_path[MAX_BIP32_PATH]) {
if (bip32_path_len <= 2) return false;

return bip32_path[0] == 0x8000002c && bip32_path[1] == 0x8000025f;
Expand Down
2 changes: 1 addition & 1 deletion src/common/bip32_check.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* @return true if the BIP32 path is valid, false otherwise.
*
*/
bool check_bip32_path(uint8_t bip32_path_len, uint32_t bip32_path[MAX_BIP32_PATH]);
bool check_bip32_path(uint8_t bip32_path_len, const uint32_t bip32_path[MAX_BIP32_PATH]);

/**
* Check the bip32 path stored in G_context.
Expand Down
2 changes: 0 additions & 2 deletions src/handler/sign_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ int handler_sign_tx(buffer_t *cdata, bool first, bool more) {

if (swap_check_validity()) {
PRINTF("Swap response validated\n");
// Unreachable due to io_send_sw instant replying and quitting to Exchange in Swap mode
// Failsafe
ui_action_validate_transaction(true);
} else {
// Unreachable due to io_send_sw instant replying and quitting to Exchange in Swap mode
Expand Down
34 changes: 22 additions & 12 deletions src/swap/handle_check_address.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
#include "constants.h"
#include "crc16.h"

#define BASE_CHAIN 0x00
#define MASTER_CHAIN 0xFF
#define BASE_CHAIN 0x00
#define MASTER_CHAIN 0xFF
#define ADDRESS_BASE64_LENGTH 48
#define ADDRESS_DECODED_LENGTH 36

/* Set params.result to 0 on error, 1 otherwise */
void swap_handle_check_address(check_address_parameters_t *params) {
Expand All @@ -34,8 +36,10 @@ void swap_handle_check_address(check_address_parameters_t *params) {
return;
}
PRINTF("Address to check %s\n", params->address_to_check);
if (strlen(params->address_to_check) != 48) {
PRINTF("Address to check expected length 48, not %d\n", strlen(params->address_to_check));
if (strlen(params->address_to_check) != ADDRESS_BASE64_LENGTH) {
PRINTF("Address to check expected length %d, not %d\n",
ADDRESS_BASE64_LENGTH,
strlen(params->address_to_check));
return;
}

Expand Down Expand Up @@ -66,24 +70,30 @@ void swap_handle_check_address(check_address_parameters_t *params) {
}
PRINTF("hash %.*H\n", HASH_LEN, hash);

char address_base64url[48];
if (base64_to_base64url(params->address_to_check, 48, address_base64url, 48) < 0) {
char address_base64url[ADDRESS_BASE64_LENGTH];
if (base64_to_base64url(params->address_to_check,
ADDRESS_BASE64_LENGTH,
address_base64url,
ADDRESS_BASE64_LENGTH) < 0) {
PRINTF("Failed to convert to base64url\n");
return;
}
uint8_t address_decoded[36];
int ret = base64url_decode(address_base64url, 48, address_decoded, 36);
if (ret != 36) {
uint8_t address_decoded[ADDRESS_DECODED_LENGTH];
int ret = base64url_decode(address_base64url,
ADDRESS_BASE64_LENGTH,
address_decoded,
ADDRESS_DECODED_LENGTH);
if (ret != ADDRESS_DECODED_LENGTH) {
PRINTF("%d\n", ret);
PRINTF("Failed to decode\n");
return;
}
PRINTF("address_decoded = %.*H\n", 36, address_decoded);
PRINTF("address_decoded = %.*H\n", ADDRESS_DECODED_LENGTH, address_decoded);

uint8_t flag = address_decoded[0];
uint8_t workchain_id = address_decoded[1];
uint8_t *account_id = &address_decoded[2];
uint16_t address_verification = U2BE(address_decoded, 34);
uint16_t address_verification = U2BE(address_decoded, ADDRESS_DECODED_LENGTH - 2);

if (flag & NON_BOUNCEABLE) {
PRINTF("Setting mode NON_BOUNCEABLE\n");
Expand Down Expand Up @@ -121,7 +131,7 @@ void swap_handle_check_address(check_address_parameters_t *params) {
return;
}

uint16_t calculated_address_verification = crc16(address_decoded, 34);
uint16_t calculated_address_verification = crc16(address_decoded, ADDRESS_DECODED_LENGTH - 2);
if (calculated_address_verification != address_verification) {
PRINTF("Wrong address verification: calculated %d, received %d\n",
calculated_address_verification,
Expand Down
3 changes: 2 additions & 1 deletion src/swap/handle_get_printable_amount.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#ifdef HAVE_SWAP

#include <string.h> // memset, explicit_bzero
#include "handle_swap_sign_transaction.h"
#include "swap.h"
#include "os.h"
Expand Down Expand Up @@ -47,7 +48,7 @@ void swap_handle_get_printable_amount(get_printable_amount_parameters_t* params)
return;

error:
memset(params->printable_amount, '\0', sizeof(params->printable_amount));
explicit_bzero(params->printable_amount, sizeof(params->printable_amount));
}

#endif // HAVE_SWAP
33 changes: 31 additions & 2 deletions tests/test_name_version.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,44 @@
import re
from pathlib import Path
from application_client.ton_command_sender import BoilerplateCommandSender
from application_client.ton_response_unpacker import unpack_get_app_and_version_response

def check_version(root_path: Path, target_version: str):
"""Extract and check if the version in the Makefile matches the target version."""
version_re = re.compile(r"^APPVERSION_(?P<part>[MNP])\s*=\s*(?P<val>\d+)", re.I)
vers_dict = {}
makefile = f"{root_path.parent.resolve()}/Makefile"

# Read the file and extract the version
with open(makefile, "r", encoding="utf-8") as f:
for line in f:
match = version_re.match(line)
if match:
part = match.group("part")
vers_dict[part] = int(match.group("val"))

# Ensure all parts (M, N, P) are present
try:
major = vers_dict['M']
minor = vers_dict['N']
patch = vers_dict['P']
except KeyError:
raise ValueError("The version in the Makefile is incomplete.")

extracted_version = f"{major}.{minor}.{patch}"

print(f"Makefile version: {extracted_version}, Target version: {target_version}")
assert extracted_version == target_version

# Test a specific APDU asking BOLOS (and not the app) the name and version of the current app
def test_get_app_and_version(backend, backend_name):
def test_get_app_and_version(backend, default_screenshot_path):
# Use the app interface instead of raw interface
client = BoilerplateCommandSender(backend)
# Send the special instruction to BOLOS
response = client.get_app_and_version()
# Use an helper to parse the response, assert the values
app_name, version = unpack_get_app_and_version_response(response.data)

check_version(default_screenshot_path, version)
assert app_name == "TON"
assert version == "2.4.0"

0 comments on commit 25081bb

Please sign in to comment.