Skip to content

Commit

Permalink
Swap start signing tx usecase
Browse files Browse the repository at this point in the history
  • Loading branch information
yogh333 committed Dec 10, 2024
1 parent e35734b commit ed340e8
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
89 changes: 89 additions & 0 deletions ledger_device_sdk/src/libcall/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use ledger_secure_sdk_sys::{
libargs_s__bindgen_ty_1, libargs_t,
};

use super::string::CustomString;

pub struct CheckAddressParams {
pub dpath: [u8; 64],
pub dpath_len: usize,
Expand Down Expand Up @@ -40,6 +42,30 @@ impl Default for PrintableAmountParams {
}
}

pub struct CreateTxParams {
pub amount: [u8; 16],
pub amount_len: usize,
pub fee_amount: [u8; 16],
pub fee_amount_len: usize,
pub dest_address: [u8; 64],
pub dest_address_len: usize,
pub result: *mut u8,
}

impl Default for CreateTxParams {
fn default() -> Self {
CreateTxParams {
amount: [0; 16],
amount_len: 0,
fee_amount: [0; 16],
fee_amount_len: 0,
dest_address: [0; 64],
dest_address_len: 0,
result: core::ptr::null_mut(),
}
}
}

pub fn get_check_address_params(arg0: u32) -> CheckAddressParams {
unsafe {
debug_print("GET_CHECK_ADDRESS_PARAMS\n");
Expand Down Expand Up @@ -107,6 +133,11 @@ pub fn get_printable_amount_params(arg0: u32) -> PrintableAmountParams {
debug_print("GET_AMOUNT_LENGTH\n");
printable_amount_params.amount_len = params.amount_length as usize;

let s = CustomString::<2>::from(printable_amount_params.amount_len as u8);
debug_print("AMOUNT LENGTH: \n");
debug_print(s.as_str());
debug_print("\n");

debug_print("GET_AMOUNT\n");
for i in 0..printable_amount_params.amount_len {
printable_amount_params.amount[16 - printable_amount_params.amount_len + i] =
Expand All @@ -121,3 +152,61 @@ pub fn get_printable_amount_params(arg0: u32) -> PrintableAmountParams {
printable_amount_params
}
}

pub fn sign_tx_params(arg0: u32) -> CreateTxParams {
unsafe {
debug_print("SIGN_TX_PARAMS\n");

let mut libarg: libargs_t = libargs_t::default();

let arg = arg0 as *const u32;

libarg.id = *arg;
libarg.command = *arg.add(1);
libarg.unused = *arg.add(2);

libarg.__bindgen_anon_1 = *(arg.add(3) as *const libargs_s__bindgen_ty_1);

let params: create_transaction_parameters_t =
*(libarg.__bindgen_anon_1.create_transaction as *const create_transaction_parameters_t);

let mut create_tx_params: CreateTxParams = Default::default();

debug_print("GET_AMOUNT_LENGTH\n");
create_tx_params.amount_len = params.amount_length as usize;

let s = CustomString::<2>::from(create_tx_params.amount_len as u8);
debug_print("AMOUNT LENGTH: \n");
debug_print(s.as_str());
debug_print("\n");

debug_print("GET_AMOUNT\n");
for i in 0..create_tx_params.amount_len {
create_tx_params.amount[16 - create_tx_params.amount_len + i] = *(params.amount.add(i));
}

debug_print("GET_FEE_AMOUNT_LENGTH\n");
create_tx_params.fee_amount_len = params.fee_amount_length as usize;

debug_print("GET_FEE_AMOUNT\n");
for i in 0..create_tx_params.fee_amount_len {
create_tx_params.fee_amount[16 - create_tx_params.fee_amount_len + i] =
*(params.fee_amount.add(i));
}

debug_print("GET_DEST_ADDRESS\n");
let mut dest_address_length = 0usize;
while *(params.destination_address.wrapping_add(dest_address_length)) != '\0' as i8 {
create_tx_params.dest_address[dest_address_length] =
*(params.destination_address.wrapping_add(dest_address_length)) as u8;
dest_address_length += 1;
}
create_tx_params.dest_address_len = dest_address_length;

create_tx_params.result = &(*(libarg.__bindgen_anon_1.create_transaction
as *mut create_transaction_parameters_t))
.result as *const u8 as *mut u8;

create_tx_params
}
}
1 change: 1 addition & 0 deletions ledger_secure_sdk_sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ impl SDKBuilder {
.include(self.bolos_sdk.join("lib_cxng/include"))
.include(self.bolos_sdk.join("lib_stusb"))
.include(self.bolos_sdk.join("lib_stusb_impl"))
.include(self.bolos_sdk.join("lib_standard_app"))
.include(
self.bolos_sdk
.join("lib_stusb/STM32_USB_Device_Library/Core/Inc"),
Expand Down
7 changes: 5 additions & 2 deletions ledger_secure_sdk_sys/src/c/src.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "os_nvm.h"
#include "os_pic.h"
#include "checks.h"
#include "swap_lib_calls.h"
#ifdef HAVE_BLE
#include "ledger_ble.h"
bolos_ux_asynch_callback_t G_io_asynch_ux_callback;
Expand Down Expand Up @@ -281,7 +282,9 @@ int c_main(int arg0) {

link_pass_ram(data_len, sidata_src, data);

if (arg0 == 0) {
libargs_t *args = (libargs_t *) arg0;

if (args == NULL || args->command == SIGN_TRANSACTION) {
size_t bss_len;
SYMBOL_ABSOLUTE_VALUE(bss_len, _bss_len);
struct SectionDst* bss;
Expand All @@ -295,7 +298,7 @@ int c_main(int arg0) {
for(;;) {
BEGIN_TRY {
TRY {
if (arg0 == 0) {
if (args == NULL || args->command == SIGN_TRANSACTION) {
// below is a 'manual' implementation of `io_seproxyhal_init`
#ifdef HAVE_MCU_PROTECT
unsigned char c[4];
Expand Down

0 comments on commit ed340e8

Please sign in to comment.