ergo/
lib.rs

1//! C bindings for ergo-lib
2
3// Coding conventions
4#![deny(non_upper_case_globals)]
5#![deny(non_camel_case_types)]
6#![deny(non_snake_case)]
7#![deny(unused_mut)]
8#![deny(dead_code)]
9#![deny(unused_imports)]
10#![deny(clippy::unwrap_used)]
11#![deny(clippy::expect_used)]
12// #![deny(missing_docs)]
13#![allow(clippy::missing_safety_doc)]
14
15#[macro_use]
16mod macros;
17mod address;
18mod batchmerkleproof;
19mod block_header;
20mod box_builder;
21mod box_selector;
22mod byte_array;
23mod constant;
24mod context_extension;
25mod contract;
26mod data_input;
27mod ergo_box;
28mod ergo_state_ctx;
29mod ergo_tree;
30mod ext_secret_key;
31mod header;
32mod input;
33mod merkleproof;
34mod nipopow;
35mod parameters;
36
37mod reduced;
38
39#[cfg(feature = "rest")]
40mod rest;
41
42mod derivation_path;
43mod ext_pub_key;
44mod mnemonic;
45mod secret_key;
46mod token;
47mod transaction;
48mod tx_builder;
49mod wallet;
50
51pub use crate::address::*;
52pub use crate::batchmerkleproof::*;
53pub use crate::block_header::*;
54pub use crate::box_builder::*;
55pub use crate::box_selector::*;
56pub use crate::byte_array::*;
57pub use crate::context_extension::*;
58pub use crate::contract::*;
59pub use crate::data_input::*;
60pub use crate::ergo_box::*;
61pub use crate::ergo_state_ctx::*;
62pub use crate::ergo_tree::*;
63pub use crate::header::*;
64pub use crate::input::*;
65pub use crate::merkleproof::*;
66pub use crate::nipopow::*;
67pub use crate::parameters::*;
68pub use crate::reduced::*;
69pub use crate::secret_key::*;
70pub use crate::token::*;
71pub use crate::transaction::*;
72pub use crate::tx_builder::*;
73pub use crate::wallet::*;
74pub use ergo_lib_c_core::{
75    address::{Address, AddressTypePrefix, NetworkPrefix},
76    Error,
77};
78use std::{ffi::CString, os::raw::c_char};
79
80pub type ErrorPtr = *mut Error;
81
82#[no_mangle]
83pub unsafe extern "C" fn ergo_lib_delete_string(ptr: *mut c_char) {
84    if !ptr.is_null() {
85        let cstring = CString::from_raw(ptr);
86        std::mem::drop(cstring)
87    }
88}
89
90#[no_mangle]
91pub unsafe extern "C" fn ergo_lib_delete_error(error: ErrorPtr) {
92    if !error.is_null() {
93        let boxed = Box::from_raw(error);
94        std::mem::drop(boxed);
95    }
96}
97
98#[no_mangle]
99pub unsafe extern "C" fn ergo_lib_error_to_string(error: ErrorPtr) -> *mut c_char {
100    #[allow(clippy::unwrap_used)]
101    if let Some(error) = error.as_ref() {
102        CString::new(error.to_string()).unwrap().into_raw()
103    } else {
104        CString::new(b"success".to_vec()).unwrap().into_raw()
105    }
106}
107
108/// Convenience type to allow us to pass Rust enums with `u8` representation through FFI to the C
109/// side.
110#[repr(C)]
111pub struct ReturnNum<T: IntegerType> {
112    /// Returned value. Note that it's only valid if the error field is null!
113    value: T,
114    error: ErrorPtr,
115}
116
117/// Convenience type to allow us to pass Rust bools (with possible error) through FFI to the C side.
118#[repr(C)]
119pub struct ReturnBool {
120    /// Returned value. Note that it's only valid if the error field is null!
121    value: bool,
122    error: ErrorPtr,
123}
124
125/// Convenience type to allow us to pass Rust `Option<_>` types through FFI to C side.
126#[repr(C)]
127pub struct ReturnOption {
128    is_some: bool,
129    error: ErrorPtr,
130}
131
132/// Convenience type to allow us to pass Rust CStrings through FFI to C side (as pointers).
133/// Note it is the responsibility of the caller to free the Rust CString.
134/// For an example, see MnemonicGenerator in the Swift bindings.
135#[repr(C)]
136pub struct ReturnString {
137    value: *mut c_char,
138    error: ErrorPtr,
139}
140
141pub unsafe fn delete_ptr<T>(ptr: *mut T) {
142    if !ptr.is_null() {
143        let boxed = Box::from_raw(ptr);
144        std::mem::drop(boxed);
145    }
146}
147pub trait IntegerType {}
148
149impl IntegerType for u8 {}
150impl IntegerType for i16 {}
151impl IntegerType for i32 {}
152impl IntegerType for u32 {}
153impl IntegerType for i64 {}
154impl IntegerType for usize {}