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

Fix the swap on the second Polakdot implem #234

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
19 changes: 19 additions & 0 deletions .github/workflows/swap-ci-workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Swap functional tests

on:
workflow_dispatch:
push:
branches:
- master
- main
- develop
- swap
pull_request:

jobs:
job_functional_tests:
uses: LedgerHQ/app-exchange/.github/workflows/reusable_swap_functional_tests.yml@develop
with:
branch_for_polkadot: ${{ github.ref }}
repo_for_polkadot: ${{ github.repository }}
test_filter: '"DOT or dot or Polkadot or polkadot"'
21 changes: 12 additions & 9 deletions app/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,18 @@ MY_DIR := $(dir $(lastword $(MAKEFILE_LIST)))

include $(BOLOS_SDK)/Makefile.defines

# Coin definition
ifndef COIN
COIN=DOT
endif

include $(CURDIR)/Makefile.version
$(info COIN = [$(COIN)])

# # Set the default value for HAVE_SWAP to undefined if not already defined
# HAVE_SWAP ?=
ifeq ($(COIN),DOT)
HAVE_SWAP ?= 1
endif

# Set the default value for PRODUCTION_BUILD to 0 if not already defined
PRODUCTION_BUILD ?= 1
Expand All @@ -41,7 +51,7 @@ else
endif

# Display whether swap functionality is enabled or not
ifdef HAVE_SWAP
ifeq ($(HAVE_SWAP), 1)
$(info ************ HAVE_SWAP = [ENABLED])
DEFINES += HAVE_SWAP=$(HAVE_SWAP)
else
Expand All @@ -52,13 +62,6 @@ endif
DEFINES += PRODUCTION_BUILD=$(PRODUCTION_BUILD)
include $(CURDIR)/../deps/ledger-zxlib/makefiles/Makefile.app_testing

ifndef COIN
COIN=DOT
endif

include $(CURDIR)/Makefile.version
$(info COIN = [$(COIN)])

APPPATH = "44'/354'"

ifeq ($(COIN),DOT)
Expand Down
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=100
# This is the `spec_version` field of `Runtime`
APPVERSION_N=0
# This is the patch version of this release
APPVERSION_P=8
APPVERSION_P=10
17 changes: 15 additions & 2 deletions app/src/apdu_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
#include "view.h"
#include "zxmacros.h"

#ifdef HAVE_SWAP
#include "swap.h"
#endif

static bool tx_initialized = false;
uint16_t blobLen = 0;

Expand Down Expand Up @@ -175,6 +179,17 @@ __Z_INLINE void handleSign(volatile uint32_t *flags, volatile uint32_t *tx, uint
THROW(APDU_CODE_DATA_INVALID);
}

#ifdef HAVE_SWAP
if (G_swap_state.called_from_swap && G_swap_state.should_exit && error_msg == NULL) {
// Call app_sign_ed25519 without going through UI display, the UI validation was done in
// Exchange app already
app_sign_ed25519();
// Go back to Exchange and report our success to display the modal
finalize_exchange_sign_transaction(true);
// Unreachable
}
#endif

view_review_init(tx_getItem, tx_getNumItems, app_sign_ed25519);
view_review_show(REVIEW_TXN);
*flags |= IO_ASYNCH_REPLY;
Expand Down Expand Up @@ -260,7 +275,6 @@ void handleApdu(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx) {
*tx += 2;
}
FINALLY {
#if 0
#ifdef HAVE_SWAP
if (G_swap_state.called_from_swap && G_swap_state.should_exit) {
// Swap checking failed, send reply now and exit, don't wait next cycle
Expand All @@ -270,7 +284,6 @@ void handleApdu(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx) {
// Go back to exchange and report our status
finalize_exchange_sign_transaction(sw == 0);
}
#endif
#endif
}
}
Expand Down
23 changes: 17 additions & 6 deletions app/src/common/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,20 @@
#include "nbgl_use_case.h"
#endif

#if 0
#ifdef HAVE_SWAP
#include "lib_standard_app/swap_lib_calls.h"
#include "swap.h"
#endif

#ifdef HAVE_SWAP
// Helper to quit the application in a limited THROW context
static void app_exit(void) {
BEGIN_TRY_L(exit) {
TRY_L(exit) { os_sched_exit(-1); }
FINALLY_L(exit) {}
TRY_L(exit) {
os_sched_exit(-1);
}
FINALLY_L(exit) {
}
}
END_TRY_L(exit);
}
Expand Down Expand Up @@ -69,8 +77,11 @@ static void library_main(libargs_t *args) {
break;
}
}
CATCH_OTHER(e) {}
FINALLY { os_lib_end(); }
CATCH_OTHER(e) {
}
FINALLY {
os_lib_end();
}
}
END_TRY;
}
Expand All @@ -83,7 +94,7 @@ __attribute__((section(".boot"))) int main(int arg0) {
os_boot();

if (arg0 != 0) {
#if 0
#if HAVE_SWAP
// The app has been started in library mode
libargs_t *args = (libargs_t *)arg0;
if (args->id != 0x100) {
Expand Down
8 changes: 6 additions & 2 deletions app/src/common/tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
#include "zxformat.h"
#include "zxmacros.h"

#ifdef HAVE_SWAP
#include "swap.h"
#endif

extern uint16_t blobLen;

#if defined(TARGET_NANOX) || defined(TARGET_NANOS2) || defined(TARGET_STAX) || defined(TARGET_FLEX)
Expand Down Expand Up @@ -111,7 +115,7 @@ const char *tx_parse() {
return parser_getErrorDescription(err);
}

#if 0
#ifdef HAVE_SWAP
// If in swap mode, compare swap tx parameters with stored info.
if (G_swap_state.called_from_swap) {
if (G_swap_state.should_exit == 1) {
Expand All @@ -123,7 +127,7 @@ const char *tx_parse() {
// We will quit the app after this transaction, whether it succeeds or fails
G_swap_state.should_exit = 1;
}
err = check_swap_conditions(&ctx_parsed_tx);
err = check_swap_conditions(&tx_obj);
CHECK_APP_CANARY()
if (err != parser_ok) {
return parser_getErrorDescription(err);
Expand Down
11 changes: 7 additions & 4 deletions app/src/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "coin.h"
#include "crypto_helper.h"
#include "cx.h"
#include "swap_utils.h"
#include "zxmacros.h"

#define PREFIX_SIGNATURE_TYPE_ED25519 0
Expand Down Expand Up @@ -129,10 +130,13 @@ zxerr_t crypto_fillAddress(uint8_t *buffer, uint16_t bufferLen, uint16_t *addrRe
return crypto_fillAddress_helper(buffer, bufferLen, addrResponseLen, ss58prefix, hdPath);
}

#if 0
// fill a crypto address using a locally computed hdpath
zxerr_t crypto_fillAddress_standalone(uint8_t *params, uint8_t paramsSize, uint8_t *buffer, uint16_t bufferLen,
uint16_t *addrResponseLen, const uint16_t ss58prefix) {
zxerr_t crypto_fillAddress_standalone(uint8_t *params,
uint8_t paramsSize,
uint8_t *buffer,
uint16_t bufferLen,
uint16_t *addrResponseLen,
const uint16_t ss58prefix) {
uint32_t local_hdPath[HDPATH_LEN_DEFAULT];

if (paramsSize != (sizeof(uint32_t) * HDPATH_LEN_DEFAULT)) {
Expand All @@ -145,4 +149,3 @@ zxerr_t crypto_fillAddress_standalone(uint8_t *params, uint8_t paramsSize, uint8

return crypto_fillAddress_helper(buffer, bufferLen, addrResponseLen, ss58prefix, local_hdPath);
}
#endif
58 changes: 58 additions & 0 deletions app/src/swap/handle_check_address.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*******************************************************************************
* (c) 2016 Ledger
* (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.
********************************************************************************/
#include "crypto.h"
#include "lib_standard_app/swap_lib_calls.h"
#include "swap.h"
#include "zxformat.h"

#define DOT_SS58_PREFIX 0
#define ED25519_ADD_KIND 0

void handle_check_address(check_address_parameters_t *params) {
if (params == NULL || params->address_to_check == NULL || params->address_parameters == NULL || params->address_parameters_length < 2) {
return;
}
params->result = 0;
uint8_t buffer[100] = {0};
uint16_t replyLen = 0;

// address parameters have the following structure
// address kind (1 byte) | path length (1 byte) | bip44 path (4 * pathLength bytes)
// address kind won't be used anymore since Generic Polkadot app only works for ED25519
const uint8_t add_kind = *params->address_parameters;
if (add_kind != ED25519_ADD_KIND) {
return;
}

zxerr_t err = crypto_fillAddress_standalone((uint8_t *)params->address_parameters + 2,
(uint8_t)params->address_parameters_length - 2, buffer, sizeof(buffer),
&replyLen, DOT_SS58_PREFIX);
if (err != zxerr_ok || replyLen <= PK_LEN_25519) {
MEMZERO(buffer, sizeof(buffer));
return;
}

const uint8_t *address = buffer + PK_LEN_25519;
const uint8_t addressLen = replyLen - PK_LEN_25519;

// Exchange guarantees that the input string is '\0' terminated
uint8_t address_to_check_len = strlen(params->address_to_check);

if (addressLen == address_to_check_len && memcmp(address, params->address_to_check, addressLen) == 0) {
params->result = 1;
}
}
46 changes: 46 additions & 0 deletions app/src/swap/handle_get_printable_amount.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*******************************************************************************
* (c) 2016 Ledger
* (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.
********************************************************************************/
#include "bignum.h"
#include "crypto.h"
#include "lib_standard_app/swap_lib_calls.h"
#include "swap.h"
#include "swap_utils.h"
#include "zxformat.h"

void handle_get_printable_amount(get_printable_amount_parameters_t *params) {
if (params == NULL) {
return;
}
uint8_t amount[16];

MEMZERO(amount, sizeof(amount));
MEMZERO(params->printable_amount, sizeof(params->printable_amount));

if (params->amount_length > 16) {
return;
}

memcpy(amount + 16 - params->amount_length, params->amount, params->amount_length);

char tmp_amount[100] = {0};
const zxerr_t zxerr = bytesAmountToStringBalance(amount, sizeof(amount), tmp_amount, sizeof(tmp_amount));

if (zxerr != zxerr_ok || strnlen(tmp_amount, sizeof(tmp_amount)) > sizeof(params->printable_amount)) {
return;
}
strncpy(params->printable_amount, tmp_amount, sizeof(params->printable_amount) - 1);
}
Loading