Skip to content

Commit

Permalink
add Swap
Browse files Browse the repository at this point in the history
  • Loading branch information
ftheirs committed Jul 10, 2024
1 parent 9988e6a commit 05c7ffd
Show file tree
Hide file tree
Showing 11 changed files with 442 additions and 23 deletions.
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
6 changes: 4 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 @@ -260,7 +264,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 +273,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)
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_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

0 comments on commit 05c7ffd

Please sign in to comment.