Skip to content

Commit

Permalink
Remove context parameter being passed everywhere
Browse files Browse the repository at this point in the history
Remove useless features like send function and handler callbacks
Factorize validation globals in global context
  • Loading branch information
fbeutin-ledger committed Apr 20, 2023
1 parent e258d77 commit 8708faa
Show file tree
Hide file tree
Showing 44 changed files with 855 additions and 953 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/reusable_swap_functional_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ on:
type: string
branch_for_solana:
required: false
default: "wip/fbeutin/swap_rebase_post_refacto"
default: "develop"
type: string

jobs:
Expand Down
118 changes: 55 additions & 63 deletions src/check_asset_in.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
#include "swap_errors.h"
#include "globals.h"
#include "currency_lib_calls.h"
#include "reply_error.h"
#include "io.h"
#include "parse_check_address_message.h"
#include "parse_coin_config.h"
#include "printable_amount.h"
#include "validate_transaction.h"
#include "menu.h"
#include "pb_structs.h"

int check_asset_in(swap_app_context_t *ctx, const command_t *cmd, SendFunction send) {
int check_asset_in(const command_t *cmd) {
static buf_t config;
static buf_t der;
static buf_t address_parameters;
Expand All @@ -22,14 +23,14 @@ int check_asset_in(swap_app_context_t *ctx, const command_t *cmd, SendFunction s
if (parse_check_address_message(cmd, &config, &der, &address_parameters) == 0) {
PRINTF("Error: Can't parse CHECK_ASSET_IN command\n");

return reply_error(ctx, INCORRECT_COMMAND_DATA, send);
return reply_error(INCORRECT_COMMAND_DATA);
}

static unsigned char hash[CURVE_SIZE_BYTES];
uint8_t hash[CURVE_SIZE_BYTES];

cx_hash_sha256(config.bytes, config.size, hash, CURVE_SIZE_BYTES);

if (cx_ecdsa_verify(&ctx->ledger_public_key,
if (cx_ecdsa_verify(&G_swap_ctx.ledger_public_key,
CX_LAST,
CX_SHA256,
hash,
Expand All @@ -38,110 +39,101 @@ int check_asset_in(swap_app_context_t *ctx, const command_t *cmd, SendFunction s
der.size) == 0) {
PRINTF("Error: Fail to verify signature of coin config\n");

return reply_error(ctx, SIGN_VERIFICATION_FAIL, send);
return reply_error(SIGN_VERIFICATION_FAIL);
}

if (parse_coin_config(&config, &ticker, &application_name, &ctx->payin_coin_config) == 0) {
if (parse_coin_config(&config, &ticker, &application_name, &G_swap_ctx.payin_coin_config) ==
0) {
PRINTF("Error: Can't parse CRYPTO coin config command\n");

return reply_error(ctx, INCORRECT_COMMAND_DATA, send);
return reply_error(INCORRECT_COMMAND_DATA);
}

// Check that given ticker match current context
char *in_currency = (ctx->subcommand == SELL ? ctx->sell_transaction.in_currency
: ctx->fund_transaction.in_currency);
char *in_currency = (G_swap_ctx.subcommand == SELL ? G_swap_ctx.sell_transaction.in_currency
: G_swap_ctx.fund_transaction.in_currency);

if (strlen(in_currency) != ticker.size ||
strncmp(in_currency, (const char *) ticker.bytes, ticker.size) != 0) {
PRINTF("Error: currency ticker doesn't match configuration ticker\n");

return reply_error(ctx, INCORRECT_COMMAND_DATA, send);
return reply_error(INCORRECT_COMMAND_DATA);
}

PRINTF("Coin configuration parsed: OK\n");

// creating 0-terminated application name
memset(ctx->payin_binary_name, 0, sizeof(ctx->payin_binary_name));
memcpy(ctx->payin_binary_name, application_name.bytes, application_name.size);
memset(G_swap_ctx.payin_binary_name, 0, sizeof(G_swap_ctx.payin_binary_name));
memcpy(G_swap_ctx.payin_binary_name, application_name.bytes, application_name.size);

PRINTF("PATH inside the SWAP = %.*H\n", address_parameters.size, address_parameters.bytes);

static char in_printable_amount[MAX_PRINTABLE_AMOUNT_SIZE];

const pb_bytes_array_16_t *in_amount;
if (ctx->subcommand == SELL) {
in_amount = (pb_bytes_array_16_t *) &ctx->sell_transaction.in_amount;
pb_bytes_array_16_t *in_amount;
if (G_swap_ctx.subcommand == SELL) {
in_amount = (pb_bytes_array_16_t *) &G_swap_ctx.sell_transaction.in_amount;
} else {
in_amount = (pb_bytes_array_16_t *) &ctx->fund_transaction.in_amount;
in_amount = (pb_bytes_array_16_t *) &G_swap_ctx.fund_transaction.in_amount;
}

// getting printable amount
if (get_printable_amount(&ctx->payin_coin_config,
ctx->payin_binary_name,
in_amount->bytes,
if (get_printable_amount(&G_swap_ctx.payin_coin_config,
G_swap_ctx.payin_binary_name,
(uint8_t *) in_amount->bytes,
in_amount->size,
in_printable_amount,
sizeof(in_printable_amount),
G_swap_ctx.printable_send_amount,
sizeof(G_swap_ctx.printable_send_amount),
false) < 0) {
PRINTF("Error: Failed to get CRYPTO currency printable amount\n");

return reply_error(ctx, INTERNAL_ERROR, send);
return reply_error(INTERNAL_ERROR);
}

PRINTF("Amount = %s\n", in_printable_amount);

static char printable_fees_amount[MAX_PRINTABLE_AMOUNT_SIZE];
memset(printable_fees_amount, 0, sizeof(printable_fees_amount));
PRINTF("Amount = %s\n", G_swap_ctx.printable_send_amount);

if (get_printable_amount(&ctx->payin_coin_config,
ctx->payin_binary_name,
ctx->transaction_fee,
ctx->transaction_fee_length,
printable_fees_amount,
sizeof(printable_fees_amount),
if (get_printable_amount(&G_swap_ctx.payin_coin_config,
G_swap_ctx.payin_binary_name,
(uint8_t *) G_swap_ctx.transaction_fee,
G_swap_ctx.transaction_fee_length,
G_swap_ctx.printable_fees_amount,
sizeof(G_swap_ctx.printable_fees_amount),
true) < 0) {
PRINTF("Error: Failed to get CRYPTO currency fees amount");
return reply_error(ctx, INTERNAL_ERROR, send);
return reply_error(INTERNAL_ERROR);
}

if (cmd->subcommand == SELL) {
size_t len = strlen(ctx->sell_transaction.out_currency);
if (len + 1 >= sizeof(ctx->printable_get_amount)) {
return reply_error(ctx, INTERNAL_ERROR, send);
size_t len = strlen(G_swap_ctx.sell_transaction.out_currency);
if (len + 1 >= sizeof(G_swap_ctx.printable_get_amount)) {
return reply_error(INTERNAL_ERROR);
}

strncpy(ctx->printable_get_amount,
ctx->sell_transaction.out_currency,
sizeof(ctx->printable_get_amount));
ctx->printable_get_amount[len] = ' ';
ctx->printable_get_amount[len + 1] = '\x00';

if (get_fiat_printable_amount(ctx->sell_transaction.out_amount.coefficient.bytes,
ctx->sell_transaction.out_amount.coefficient.size,
ctx->sell_transaction.out_amount.exponent,
ctx->printable_get_amount + len + 1,
sizeof(ctx->printable_get_amount) - (len + 1)) < 0) {
strncpy(G_swap_ctx.printable_get_amount,
G_swap_ctx.sell_transaction.out_currency,
sizeof(G_swap_ctx.printable_get_amount));
G_swap_ctx.printable_get_amount[len] = ' ';
G_swap_ctx.printable_get_amount[len + 1] = '\x00';

if (get_fiat_printable_amount(G_swap_ctx.sell_transaction.out_amount.coefficient.bytes,
G_swap_ctx.sell_transaction.out_amount.coefficient.size,
G_swap_ctx.sell_transaction.out_amount.exponent,
G_swap_ctx.printable_get_amount + len + 1,
sizeof(G_swap_ctx.printable_get_amount) - (len + 1)) < 0) {
PRINTF("Error: Failed to get source currency printable amount\n");
return reply_error(ctx, INTERNAL_ERROR, send);
return reply_error(INTERNAL_ERROR);
}

PRINTF("%s\n", ctx->printable_get_amount);
PRINTF("%s\n", G_swap_ctx.printable_get_amount);
} else {
// Prepare message for account funding
strncpy(ctx->printable_get_amount,
ctx->fund_transaction.account_name,
sizeof(ctx->printable_get_amount));
ctx->printable_get_amount[sizeof(ctx->printable_get_amount) - 1] = '\x00';
strncpy(G_swap_ctx.printable_get_amount,
G_swap_ctx.fund_transaction.account_name,
sizeof(G_swap_ctx.printable_get_amount));
G_swap_ctx.printable_get_amount[sizeof(G_swap_ctx.printable_get_amount) - 1] = '\x00';
}

ctx->state = WAITING_USER_VALIDATION;
G_swap_ctx.state = WAITING_USER_VALIDATION;

ui_validate_amounts(cmd->rate, //
cmd->subcommand,
ctx, //
in_printable_amount, //
printable_fees_amount, //
send);
ui_validate_amounts();

return 0;
}
9 changes: 2 additions & 7 deletions src/check_asset_in.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
#ifndef _CHECK_ASSET_IN_H_
#define _CHECK_ASSET_IN_H_
#pragma once

#include "swap_app_context.h"
#include "send_function.h"
#include "commands.h"

int check_asset_in(swap_app_context_t *ctx, const command_t *cmd, SendFunction send);

#endif // _CHECK_ASSET_IN_H_
int check_asset_in(const command_t *cmd);
19 changes: 8 additions & 11 deletions src/check_partner.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,36 @@
#include "check_partner.h"
#include "globals.h"
#include "swap_errors.h"
#include "reply_error.h"
#include "io.h"

// This function receive signature of
// Input should be in the form of DER serialized signature
// the length should be in [MIN_DER_SIGNATURE_LENGTH, MAX_DER_SIGNATURE_LENGTH]
int check_partner(swap_app_context_t *ctx, const command_t *cmd, SendFunction send) {
int check_partner(const command_t *cmd) {
if (cmd->data.size < MIN_DER_SIGNATURE_LENGTH || cmd->data.size > MAX_DER_SIGNATURE_LENGTH) {
PRINTF("Error: Input buffer length don't correspond to DER length\n");

return reply_error(ctx, INCORRECT_COMMAND_DATA, send);
return reply_error(INCORRECT_COMMAND_DATA);
}

if (cx_ecdsa_verify(&(ctx->ledger_public_key),
if (cx_ecdsa_verify(&(G_swap_ctx.ledger_public_key),
CX_LAST,
CX_SHA256,
ctx->sha256_digest,
G_swap_ctx.sha256_digest,
CURVE_SIZE_BYTES,
cmd->data.bytes,
cmd->data.size) == 0) {
PRINTF("Error: Failed to verify signature of partner data\n");

return reply_error(ctx, SIGN_VERIFICATION_FAIL, send);
return reply_error(SIGN_VERIFICATION_FAIL);
}

unsigned char output_buffer[2] = {0x90, 0x00};

if (send(output_buffer, 2) < 0) {
if (reply_success() < 0) {
PRINTF("Error: send error\n");

return -1;
}

ctx->state = PROVIDER_CHECKED;
G_swap_ctx.state = PROVIDER_CHECKED;

return 0;
}
9 changes: 2 additions & 7 deletions src/check_partner.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
#ifndef _CHECK_PARTNER_H_
#define _CHECK_PARTNER_H_
#pragma once

#include "swap_app_context.h"
#include "send_function.h"
#include "commands.h"

int check_partner(swap_app_context_t *ctx, const command_t *cmd, SendFunction send);

#endif // _CHECK_PARTNER_H_
int check_partner(const command_t *cmd);
Loading

0 comments on commit 8708faa

Please sign in to comment.