use crate::{delete_ptr, ErrorPtr};
use ergo_lib_c_core::derivation_path::{
derivation_path_depth, derivation_path_from_str, derivation_path_new, derivation_path_next,
derivation_path_to_str, ConstDerivationPathPtr, DerivationPathPtr,
};
use ergo_lib_c_core::Error;
use std::ffi::{CStr, CString};
use std::os::raw::c_char;
#[no_mangle]
pub unsafe extern "C" fn ergo_lib_derivation_path_new(
account: u32,
address_indices: *const u32,
len: usize,
derivation_path_out: *mut DerivationPathPtr,
) -> ErrorPtr {
let address_indices = std::slice::from_raw_parts(address_indices, len);
let res = derivation_path_new(account, address_indices, derivation_path_out);
Error::c_api_from(res)
}
#[no_mangle]
pub unsafe extern "C" fn ergo_lib_derivation_path_from_str(
derivation_path_str: *const c_char,
derivation_path_out: *mut DerivationPathPtr,
) -> ErrorPtr {
let derivation_path_str = CStr::from_ptr(derivation_path_str).to_string_lossy();
let res = derivation_path_from_str(&derivation_path_str, derivation_path_out);
Error::c_api_from(res)
}
#[no_mangle]
pub unsafe extern "C" fn ergo_lib_derivation_path_to_str(
derivation_path_ptr: ConstDerivationPathPtr,
_derivation_path_str: *mut *const c_char,
) {
#[allow(clippy::unwrap_used)]
{
let s = derivation_path_to_str(derivation_path_ptr).unwrap();
*_derivation_path_str = CString::new(s).unwrap().into_raw();
}
}
#[no_mangle]
pub unsafe extern "C" fn ergo_lib_derivation_path_depth(
derivation_path_ptr: ConstDerivationPathPtr,
) -> usize {
#[allow(clippy::unwrap_used)]
derivation_path_depth(derivation_path_ptr).unwrap()
}
#[no_mangle]
pub unsafe extern "C" fn ergo_lib_derivation_path_next(
derivation_path_ptr: ConstDerivationPathPtr,
derivation_path_out: *mut DerivationPathPtr,
) -> ErrorPtr {
let res = derivation_path_next(derivation_path_ptr, derivation_path_out);
Error::c_api_from(res)
}
#[no_mangle]
pub extern "C" fn ergo_lib_derivation_path_delete(ptr: DerivationPathPtr) {
unsafe { delete_ptr(ptr) }
}