Skip to content

Commit

Permalink
Merge pull request #246 from Fingolfin69/bn-add-coin-config-to-swap-p…
Browse files Browse the repository at this point in the history
…arams

Add coin config to swap params
  • Loading branch information
yogh333 authored Feb 26, 2025
2 parents 774f60c + 0680588 commit 17fa220
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 13 deletions.
2 changes: 1 addition & 1 deletion ledger_device_sdk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ledger_device_sdk"
version = "1.21.2"
version = "1.21.3"
authors = ["yhql", "yogh333", "agrojean-ledger", "kingofpayne"]
edition = "2021"
license.workspace = true
Expand Down
81 changes: 69 additions & 12 deletions ledger_device_sdk/src/libcall/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,23 @@ use ledger_secure_sdk_sys::{
const DPATH_STAGE_SIZE: usize = 16;
const ADDRESS_BUF_SIZE: usize = 64;
const AMOUNT_BUF_SIZE: usize = 16;
const DEFAULT_COIN_CONFIG_BUF_SIZE: usize = 16;

pub struct CheckAddressParams {
pub struct CheckAddressParams<const COIN_CONFIG_BUF_SIZE: usize = DEFAULT_COIN_CONFIG_BUF_SIZE> {
pub coin_config: [u8; COIN_CONFIG_BUF_SIZE],
pub coin_config_len: usize,
pub dpath: [u8; DPATH_STAGE_SIZE * 4],
pub dpath_len: usize,
pub ref_address: [u8; ADDRESS_BUF_SIZE],
pub ref_address_len: usize,
pub result: *mut i32,
}

impl Default for CheckAddressParams {
impl<const COIN_CONFIG_BUF_SIZE: usize> Default for CheckAddressParams<COIN_CONFIG_BUF_SIZE> {
fn default() -> Self {
CheckAddressParams {
coin_config: [0; COIN_CONFIG_BUF_SIZE],
coin_config_len: 0,
dpath: [0; DPATH_STAGE_SIZE * 4],
dpath_len: 0,
ref_address: [0; ADDRESS_BUF_SIZE],
Expand All @@ -30,16 +35,20 @@ impl Default for CheckAddressParams {
}
}

pub struct PrintableAmountParams {
pub struct PrintableAmountParams<const COIN_CONFIG_BUF_SIZE: usize = DEFAULT_COIN_CONFIG_BUF_SIZE> {
pub coin_config: [u8; COIN_CONFIG_BUF_SIZE],
pub coin_config_len: usize,
pub amount: [u8; AMOUNT_BUF_SIZE],
pub amount_len: usize,
pub amount_str: *mut i8,
pub is_fee: bool,
}

impl Default for PrintableAmountParams {
impl<const COIN_CONFIG_BUF_SIZE: usize> Default for PrintableAmountParams<COIN_CONFIG_BUF_SIZE> {
fn default() -> Self {
PrintableAmountParams {
coin_config: [0; COIN_CONFIG_BUF_SIZE],
coin_config_len: 0,
amount: [0; AMOUNT_BUF_SIZE],
amount_len: 0,
amount_str: core::ptr::null_mut(),
Expand All @@ -48,7 +57,9 @@ impl Default for PrintableAmountParams {
}
}

pub struct CreateTxParams {
pub struct CreateTxParams<const COIN_CONFIG_BUF_SIZE: usize = DEFAULT_COIN_CONFIG_BUF_SIZE> {
pub coin_config: [u8; COIN_CONFIG_BUF_SIZE],
pub coin_config_len: usize,
pub amount: [u8; AMOUNT_BUF_SIZE],
pub amount_len: usize,
pub fee_amount: [u8; AMOUNT_BUF_SIZE],
Expand All @@ -58,9 +69,11 @@ pub struct CreateTxParams {
pub result: *mut u8,
}

impl Default for CreateTxParams {
impl<const COIN_CONFIG_BUF_SIZE: usize> Default for CreateTxParams<COIN_CONFIG_BUF_SIZE> {
fn default() -> Self {
CreateTxParams {
coin_config: [0; COIN_CONFIG_BUF_SIZE],
coin_config_len: 0,
amount: [0; AMOUNT_BUF_SIZE],
amount_len: 0,
fee_amount: [0; AMOUNT_BUF_SIZE],
Expand All @@ -72,7 +85,9 @@ impl Default for CreateTxParams {
}
}

pub fn get_check_address_params(arg0: u32) -> CheckAddressParams {
pub fn get_check_address_params<const COIN_CONFIG_BUF_SIZE: usize>(
arg0: u32,
) -> CheckAddressParams<COIN_CONFIG_BUF_SIZE> {
debug_print("=> get_check_address_params\n");

let mut libarg: libargs_t = libargs_t::default();
Expand All @@ -88,7 +103,20 @@ pub fn get_check_address_params(arg0: u32) -> CheckAddressParams {
let params: check_address_parameters_t =
unsafe { *(libarg.__bindgen_anon_1.check_address as *const check_address_parameters_t) };

let mut check_address_params: CheckAddressParams = Default::default();
let mut check_address_params: CheckAddressParams<COIN_CONFIG_BUF_SIZE> = Default::default();

debug_print("==> GET_COIN_CONFIG_LENGTH\n");
check_address_params.coin_config_len = params.coin_configuration_length as usize;

debug_print("==> GET_COIN_CONFIG \n");
unsafe {
params.coin_configuration.copy_to_nonoverlapping(
check_address_params.coin_config.as_mut_ptr(),
check_address_params
.coin_config_len
.min(COIN_CONFIG_BUF_SIZE),
);
}

debug_print("==> GET_DPATH_LENGTH\n");
check_address_params.dpath_len =
Expand Down Expand Up @@ -117,7 +145,9 @@ pub fn get_check_address_params(arg0: u32) -> CheckAddressParams {
check_address_params
}

pub fn get_printable_amount_params(arg0: u32) -> PrintableAmountParams {
pub fn get_printable_amount_params<const COIN_CONFIG_BUF_SIZE: usize>(
arg0: u32,
) -> PrintableAmountParams<COIN_CONFIG_BUF_SIZE> {
debug_print("=> get_printable_amount_params\n");

let mut libarg: libargs_t = libargs_t::default();
Expand All @@ -134,7 +164,21 @@ pub fn get_printable_amount_params(arg0: u32) -> PrintableAmountParams {
*(libarg.__bindgen_anon_1.get_printable_amount as *const get_printable_amount_parameters_t)
};

let mut printable_amount_params: PrintableAmountParams = Default::default();
let mut printable_amount_params: PrintableAmountParams<COIN_CONFIG_BUF_SIZE> =
Default::default();

debug_print("==> GET_COIN_CONFIG_LENGTH\n");
printable_amount_params.coin_config_len = params.coin_configuration_length as usize;

debug_print("==> GET_COIN_CONFIG \n");
unsafe {
params.coin_configuration.copy_to_nonoverlapping(
printable_amount_params.coin_config.as_mut_ptr(),
printable_amount_params
.coin_config_len
.min(COIN_CONFIG_BUF_SIZE),
);
}

debug_print("==> GET_IS_FEE\n");
printable_amount_params.is_fee = params.is_fee == true;
Expand Down Expand Up @@ -162,7 +206,9 @@ extern "C" {
fn c_boot_std();
}

pub fn sign_tx_params(arg0: u32) -> CreateTxParams {
pub fn sign_tx_params<const COIN_CONFIG_BUF_SIZE: usize>(
arg0: u32,
) -> CreateTxParams<COIN_CONFIG_BUF_SIZE> {
debug_print("=> sign_tx_params\n");

let mut libarg: libargs_t = libargs_t::default();
Expand All @@ -179,7 +225,18 @@ pub fn sign_tx_params(arg0: u32) -> CreateTxParams {
*(libarg.__bindgen_anon_1.create_transaction as *const create_transaction_parameters_t)
};

let mut create_tx_params: CreateTxParams = Default::default();
let mut create_tx_params: CreateTxParams<COIN_CONFIG_BUF_SIZE> = Default::default();

debug_print("==> GET_COIN_CONFIG_LENGTH\n");
create_tx_params.coin_config_len = params.coin_configuration_length as usize;

debug_print("==> GET_COIN_CONFIG \n");
unsafe {
params.coin_configuration.copy_to_nonoverlapping(
create_tx_params.coin_config.as_mut_ptr(),
create_tx_params.coin_config_len.min(COIN_CONFIG_BUF_SIZE),
);
}

debug_print("==> GET_AMOUNT\n");
create_tx_params.amount_len = AMOUNT_BUF_SIZE.min(params.amount_length as usize);
Expand Down

0 comments on commit 17fa220

Please sign in to comment.