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

Add SWAP functionality #19

Closed
wants to merge 13 commits into from
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ VARIANT_PARAM = COIN
VARIANT_VALUES = APTOS

# Enabling DEBUG flag will enable PRINTF and disable optimizations
#DEBUG = 1
DEBUG = 1
TEST_PUBLIC_KEY = 1
TESTING = 1

ENABLE_SWAP = 1

########################################
# Application custom permissions #
Expand Down
95 changes: 76 additions & 19 deletions src/handler/get_public_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,48 +34,103 @@
#include "../ui/display.h"
#include "../helper/send_response.h"

int handler_get_public_key(buffer_t *cdata, bool display) {
explicit_bzero(&G_context, sizeof(G_context));
G_context.req_type = CONFIRM_ADDRESS;
/*****************************************************************************
* Ledger App Aptos.
* (c) 2020 Ledger SAS.
*
* 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.
*****************************************************************************/

cx_ecfp_private_key_t private_key = {0};
cx_ecfp_public_key_t public_key = {0};
#include <stdint.h> // uint*_t
#include <stdbool.h> // bool
#include <stddef.h> // size_t
#include <string.h> // memset, explicit_bzero

if (!buffer_read_u8(cdata, &G_context.bip32_path_len) ||
!buffer_read_bip32_path(cdata, G_context.bip32_path, (size_t) G_context.bip32_path_len)) {
G_context.req_type = REQUEST_UNDEFINED;
#include "os.h"
#include "cx.h"
#include "io.h"
#include "buffer.h"

#include "get_public_key.h"
#include "../globals.h"
#include "../types.h"
#include "../sw.h"
#include "../crypto.h"
#include "../address.h"
#include "../ui/display.h"
#include "../helper/send_response.h"


// Get public key from BIP32 path by deriving it from the private key.
int get_public_key(buffer_t *cdata,
uint8_t *output_bip32_path_len,
uint32_t *output_bip32_path,
uint8_t *output_public_key) {
if (!buffer_read_u8(cdata, output_bip32_path_len)) {
return io_send_sw(SW_WRONG_DATA_LENGTH);
}
if (!buffer_read_bip32_path(cdata, output_bip32_path, (size_t) *output_bip32_path_len)) {
return io_send_sw(SW_WRONG_DATA_LENGTH);
}

if (!validate_aptos_bip32_path(G_context.bip32_path, G_context.bip32_path_len)) {
G_context.req_type = REQUEST_UNDEFINED;
if (!validate_aptos_bip32_path(output_bip32_path, *output_bip32_path_len)) {
return io_send_sw(SW_GET_PUB_KEY_FAIL);
}

// derive private key according to BIP32 path
// TODO(jmartins): review if chain code should come from G_context or needs to be specified
// for the use (ie: SWAP or GET_PUBLIC_KEY)
cx_ecfp_private_key_t private_key = {0};
uint32_t chaincode[32];
cx_err_t error = crypto_derive_private_key(&private_key,
G_context.pk_info.chain_code,
G_context.bip32_path,
G_context.bip32_path_len);
output_bip32_path,
*output_bip32_path_len);
if (error != CX_OK) {
explicit_bzero(&private_key, sizeof(private_key));
PRINTF("crypto_derive_private_key error code: %x.\n", error);
G_context.req_type = REQUEST_UNDEFINED;
return io_send_sw(SW_GET_PUB_KEY_FAIL);
}

// generate corresponding public key
error = crypto_init_public_key(&private_key, &public_key, G_context.pk_info.raw_public_key);
cx_ecfp_private_key_t public_key = {0};
error = crypto_init_public_key(&private_key, &public_key, output_public_key);
// Wipe the private key from memory
explicit_bzero(&private_key, sizeof(private_key));

if (error != CX_OK) {
explicit_bzero(&private_key, sizeof(private_key));
PRINTF("crypto_init_public_key error code: %x.\n", error);
G_context.req_type = REQUEST_UNDEFINED;
return io_send_sw(SW_GET_PUB_KEY_FAIL);
}

// reset private key
explicit_bzero(&private_key, sizeof(private_key));
return 0;
}

// Handler to get public key from BIP32 path, store in G_context and display on screen.
int handler_get_public_key(buffer_t *cdata, bool display) {
explicit_bzero(&G_context, sizeof(G_context));
G_context.req_type = CONFIRM_ADDRESS;

cx_ecfp_public_key_t public_key = {0};

int err = get_public_key(cdata,
&G_context.bip32_path_len,
G_context.bip32_path,
&G_context.pk_info.raw_public_key[0]);

if (err) {
G_context.req_type = REQUEST_UNDEFINED;
return err;
}

if (display) {
int ui_status = ui_display_address();
Expand All @@ -86,3 +141,5 @@
G_context.req_type = REQUEST_UNDEFINED; // all the work is done, reset the context
return helper_send_response_pubkey();
}


5 changes: 5 additions & 0 deletions src/handler/get_public_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@
*
*/
int handler_get_public_key(buffer_t *cdata, bool display);

int get_public_key(buffer_t *cdata,
uint8_t *output_bip32_path_len,
uint32_t output_bip32_path[MAX_BIP32_PATH],
uint8_t *output_public_key);
84 changes: 84 additions & 0 deletions src/swap/handle_check_address.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*****************************************************************************
* Ledger App Aptos.
* (c) 2020 Ledger SAS.
*
* 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.
*****************************************************************************/

#ifdef HAVE_SWAP
#include <string.h>
#include "swap.h"
#include "../handler/get_public_key.h"
#include "os.h"

// The address string lenght is 66, 2 characters for the prefix and 64 for the address

Check failure on line 24 in src/swap/handle_check_address.c

View workflow job for this annotation

GitHub Actions / Check misspellings

lenght ==> length
#define ADDRESS_STRING_LENGTH 66

/* Set params.result to 0 on error, 1 otherwise */
void swap_handle_check_address(check_address_parameters_t *params) {
PRINTF("Inside Aptos swap_handle_check_address\n");
params->result = 0;

// Checking that parameters are correct
if (params->address_parameters == NULL || params->address_parameters_length == 0) {
PRINTF("address_parameters is empty\n");
return;
}
PRINTF("address_parameters_length: %d\n", params->address_parameters_length);
PRINTF("address_parameters: %.*H\n", params->address_parameters_length, params->address_parameters);

if (params->address_to_check == NULL) {
PRINTF("address_to_check is empty\n");
return;
}

if (strlen(params->address_to_check) != ADDRESS_STRING_LENGTH) {
PRINTF("address_to_check length should be %d, not %d\n",
ADDRESS_STRING_LENGTH, strlen(params->address_to_check));
return;
}

// Calculate the public key from the BIP32 path
buffer_t cdata;
cdata.ptr = params->address_parameters;
cdata.size = params->address_parameters_length;
cdata.offset = 0;

uint8_t bip32_path_len;
uint32_t bip32_path[MAX_BIP32_PATH];
uint8_t public_key[32];
if (get_public_key(&cdata, &bip32_path_len, bip32_path, &public_key) != 0) {
PRINTF("get_public_key failed\n");
return;
}
// Calculate the address from the public key, and decode it to readable format
uint8_t address[ADDRESS_LEN] = {0};
if (!address_from_pubkey(public_key, address, sizeof(address))) {
return;
}
char g_address[ADDRESS_STRING_LENGTH + 1];
if (0 > format_prefixed_hex(address, sizeof(address), g_address, sizeof(g_address))) {
return;
}

//PRINTF("address_to_check: %s\n", params->address_to_check);
//PRINTF("g_address: %s\n", g_address);
// Compare the strings
if (strcmp(params->address_to_check, g_address) != 0) {
PRINTF("address_to_check does not match the address in the device\n");
params->result = 1;
} else {
PRINTF("addess_to_check matches within the addresses in the device\n");
}
}
#endif
14 changes: 14 additions & 0 deletions src/swap/handle_get_printable_amount.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifdef HAVE_SWAP

#include <string.h> // memset, explicit_bzero
#include "handle_swap_sign_transaction.h"
#include "swap.h"
#include "os.h"
#include "constants.h"

/* Set empty printable_amount on error, printable amount otherwise */
void swap_handle_get_printable_amount(get_printable_amount_parameters_t* params) {
PRINTF("TODO: swap_handle_get_printable_amount\n");
}

#endif
20 changes: 20 additions & 0 deletions src/swap/handle_swap_sign_transaction.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifdef HAVE_SWAP

#include "handle_swap_sign_transaction.h"
#include "display.h"
#include "swap.h"
#include "string.h"
#include "os_lib.h"
#include "constants.h"
#include "os_utils.h"
#include "globals.h"

bool swap_copy_transaction_parameters(create_transaction_parameters_t* params) {
PRINTF("TODO: swap_copy_transaction_parameters\n");
}

void __attribute__((noreturn)) swap_finalize_exchange_sign_transaction(bool is_success) {
PRINTF("TODO: swap_finalize_exchange_sign_transaction\n");
}

#endif
Empty file.
Loading