From b5cd3c9b4f14374faa7726dc9dacc850799056b9 Mon Sep 17 00:00:00 2001 From: GroM Date: Fri, 13 Dec 2024 11:55:16 +0100 Subject: [PATCH] Add swap_return() function to wrap unsafe calls --- ledger_device_sdk/src/libcall/swap.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/ledger_device_sdk/src/libcall/swap.rs b/ledger_device_sdk/src/libcall/swap.rs index 05e1d522..76be11aa 100644 --- a/ledger_device_sdk/src/libcall/swap.rs +++ b/ledger_device_sdk/src/libcall/swap.rs @@ -206,3 +206,29 @@ pub fn sign_tx_params(arg0: u32) -> CreateTxParams { create_tx_params } } + +pub enum SwapResult<'a> { + CheckAddressResult(&'a mut CheckAddressParams, i32), + PrintableAmountResult(&'a mut PrintableAmountParams, &'a str), + CreateTxResult(&'a mut CreateTxParams, u8), +} + +pub fn swap_return(res: SwapResult) { + unsafe { + match res { + SwapResult::CheckAddressResult(&mut ref p, r) => { + *(p.result) = r; + } + SwapResult::PrintableAmountResult(&mut ref p, s) => { + for (i, c) in s.chars().enumerate() { + *(p.amount_str.add(i)) = c as i8; + } + *(p.amount_str.add(s.len())) = '\0' as i8; + } + SwapResult::CreateTxResult(&mut ref p, r) => { + *(p.result) = r; + } + } + ledger_secure_sdk_sys::os_lib_end(); + } +}