diff --git a/dash-spv-ffi/Cargo.toml b/dash-spv-ffi/Cargo.toml index 2e6028e3e..d44969dae 100644 --- a/dash-spv-ffi/Cargo.toml +++ b/dash-spv-ffi/Cargo.toml @@ -13,7 +13,7 @@ crate-type = ["cdylib", "staticlib", "rlib"] [dependencies] dash-spv = { path = "../dash-spv" } -dashcore = { path = "../dash", package = "dashcore" } +dashcore = { path = "../dash", package = "dashcore", features= ["ffi"] } tokio = { version = "1", features = ["full"] } tokio-util = "0.7" hex = "0.4" diff --git a/dash-spv-ffi/cbindgen.toml b/dash-spv-ffi/cbindgen.toml index d7f277efe..41e113a0b 100644 --- a/dash-spv-ffi/cbindgen.toml +++ b/dash-spv-ffi/cbindgen.toml @@ -4,7 +4,7 @@ include_guard = "DASH_SPV_FFI_H" autogen_warning = "/* Warning: This file is auto-generated by cbindgen. Do not modify manually. */" include_version = true cpp_compat = true -includes = ["../key-wallet-ffi/key-wallet-ffi.h"] +includes = ["../key-wallet-ffi/key-wallet-ffi.h", "../dashcore/dashcore.h"] [export] include = ["FFI"] diff --git a/dash-spv-ffi/src/bin/ffi_cli.rs b/dash-spv-ffi/src/bin/ffi_cli.rs index 4a9ee51b2..fc521242d 100644 --- a/dash-spv-ffi/src/bin/ffi_cli.rs +++ b/dash-spv-ffi/src/bin/ffi_cli.rs @@ -5,10 +5,11 @@ use std::ptr; use clap::{Arg, ArgAction, Command}; use dash_spv_ffi::*; +use dashcore::ffi::FFINetwork; use key_wallet_ffi::managed_account::FFITransactionRecord; use key_wallet_ffi::types::FFITransactionContext; use key_wallet_ffi::wallet_manager::wallet_manager_add_wallet_from_mnemonic; -use key_wallet_ffi::{FFIError, FFINetwork}; +use key_wallet_ffi::FFIError; fn ffi_string_to_rust(s: *const c_char) -> String { if s.is_null() { diff --git a/dash-spv-ffi/src/config.rs b/dash-spv-ffi/src/config.rs index 2970bd631..e60f65138 100644 --- a/dash-spv-ffi/src/config.rs +++ b/dash-spv-ffi/src/config.rs @@ -1,6 +1,7 @@ use crate::{null_check, set_last_error, FFIErrorCode, FFIMempoolStrategy}; use dash_spv::{ClientConfig, ValidationMode}; -use key_wallet_ffi::FFINetwork; +use dashcore::ffi::FFINetwork; + use std::ffi::CStr; use std::net::{IpAddr, SocketAddr, ToSocketAddrs}; use std::os::raw::c_char; diff --git a/dash-spv-ffi/tests/dashd_sync/context.rs b/dash-spv-ffi/tests/dashd_sync/context.rs index a93103281..497ed3046 100644 --- a/dash-spv-ffi/tests/dashd_sync/context.rs +++ b/dash-spv-ffi/tests/dashd_sync/context.rs @@ -21,6 +21,7 @@ use dash_spv_ffi::config::{ }; use dash_spv_ffi::types::FFIWalletManager as FFIWalletManagerOpaque; use dash_spv_ffi::FFIEventCallbacks; +use dashcore::ffi::FFINetwork; use dashcore::hashes::Hash; use dashcore::{Address, Txid}; use key_wallet_ffi::managed_account::{ @@ -38,8 +39,7 @@ use key_wallet_ffi::wallet_manager::{ }; use key_wallet_ffi::{ wallet_manager_free_string, wallet_manager_free_wallet_ids, wallet_manager_get_wallet, - wallet_manager_get_wallet_balance, wallet_manager_get_wallet_ids, FFIError, FFINetwork, - FFIWalletManager, + wallet_manager_get_wallet_balance, wallet_manager_get_wallet_ids, FFIError, FFIWalletManager, }; use tempfile::TempDir; diff --git a/dash-spv-ffi/tests/test_client.rs b/dash-spv-ffi/tests/test_client.rs index af4387a7f..f3615a17b 100644 --- a/dash-spv-ffi/tests/test_client.rs +++ b/dash-spv-ffi/tests/test_client.rs @@ -1,7 +1,7 @@ #[cfg(test)] mod tests { use dash_spv_ffi::*; - use key_wallet_ffi::FFINetwork; + use dashcore::ffi::FFINetwork; use serial_test::serial; use std::ffi::CString; use tempfile::TempDir; diff --git a/dash-spv-ffi/tests/test_config.rs b/dash-spv-ffi/tests/test_config.rs index 7fc9a5817..caf7013ee 100644 --- a/dash-spv-ffi/tests/test_config.rs +++ b/dash-spv-ffi/tests/test_config.rs @@ -1,7 +1,7 @@ #[cfg(test)] mod tests { use dash_spv_ffi::*; - use key_wallet_ffi::FFINetwork; + use dashcore::ffi::FFINetwork; use serial_test::serial; use std::ffi::CString; diff --git a/dash-spv-ffi/tests/test_types.rs b/dash-spv-ffi/tests/test_types.rs index 71aa803bb..417f87d3c 100644 --- a/dash-spv-ffi/tests/test_types.rs +++ b/dash-spv-ffi/tests/test_types.rs @@ -5,7 +5,7 @@ mod tests { FiltersProgress, InstantSendProgress, MasternodesProgress, SyncProgress, SyncState, }; use dash_spv_ffi::*; - use key_wallet_ffi::FFINetwork; + use dashcore::ffi::FFINetwork; #[test] fn test_ffi_string_new_and_destroy() { diff --git a/dash-spv-ffi/tests/test_utils.rs b/dash-spv-ffi/tests/test_utils.rs index fcb58b0dc..2e27bf316 100644 --- a/dash-spv-ffi/tests/test_utils.rs +++ b/dash-spv-ffi/tests/test_utils.rs @@ -1,8 +1,6 @@ #[cfg(test)] mod tests { use dash_spv_ffi::*; - use key_wallet_ffi::types::ffi_network_get_name; - use key_wallet_ffi::FFINetwork; use serial_test::serial; use std::ffi::{CStr, CString}; @@ -38,29 +36,4 @@ mod tests { assert!(version.contains(".")); } } - - #[test] - fn test_network_names() { - unsafe { - let name = ffi_network_get_name(FFINetwork::Mainnet); - assert!(!name.is_null()); - let name_str = CStr::from_ptr(name).to_str().unwrap(); - assert_eq!(name_str, "mainnet"); - - let name = ffi_network_get_name(FFINetwork::Testnet); - assert!(!name.is_null()); - let name_str = CStr::from_ptr(name).to_str().unwrap(); - assert_eq!(name_str, "testnet"); - - let name = ffi_network_get_name(FFINetwork::Regtest); - assert!(!name.is_null()); - let name_str = CStr::from_ptr(name).to_str().unwrap(); - assert_eq!(name_str, "regtest"); - - let name = ffi_network_get_name(FFINetwork::Devnet); - assert!(!name.is_null()); - let name_str = CStr::from_ptr(name).to_str().unwrap(); - assert_eq!(name_str, "devnet"); - } - } } diff --git a/dash-spv-ffi/tests/test_wallet_manager.rs b/dash-spv-ffi/tests/test_wallet_manager.rs index 5c4d6710f..4b1fd1fa7 100644 --- a/dash-spv-ffi/tests/test_wallet_manager.rs +++ b/dash-spv-ffi/tests/test_wallet_manager.rs @@ -1,6 +1,7 @@ #[cfg(test)] mod tests { use dash_spv_ffi::*; + use dashcore::ffi::FFINetwork; use key_wallet::wallet::initialization::WalletAccountCreationOptions; use key_wallet::wallet::managed_wallet_info::ManagedWalletInfo; use key_wallet_ffi::{ @@ -8,7 +9,7 @@ mod tests { wallet_manager_free_wallet_ids, wallet_manager_get_wallet_ids, wallet_manager_import_wallet_from_bytes, wallet_manager_wallet_count, }, - FFIError, FFINetwork, FFIWalletManager, + FFIError, FFIWalletManager, }; use key_wallet_manager::WalletManager; use std::ffi::{CStr, CString}; diff --git a/dash-spv-ffi/tests/unit/test_async_operations.rs b/dash-spv-ffi/tests/unit/test_async_operations.rs index c18726178..4df5240c6 100644 --- a/dash-spv-ffi/tests/unit/test_async_operations.rs +++ b/dash-spv-ffi/tests/unit/test_async_operations.rs @@ -1,7 +1,7 @@ #[cfg(test)] mod tests { use crate::*; - use key_wallet_ffi::FFINetwork; + use dashcore::ffi::FFINetwork; use serial_test::serial; use std::ffi::CString; use std::os::raw::{c_char, c_void}; diff --git a/dash-spv-ffi/tests/unit/test_client_lifecycle.rs b/dash-spv-ffi/tests/unit/test_client_lifecycle.rs index e0f642b9e..ee2544af6 100644 --- a/dash-spv-ffi/tests/unit/test_client_lifecycle.rs +++ b/dash-spv-ffi/tests/unit/test_client_lifecycle.rs @@ -6,7 +6,7 @@ #[cfg(test)] mod tests { use crate::*; - use key_wallet_ffi::FFINetwork; + use dashcore::ffi::FFINetwork; use serial_test::serial; use std::ffi::CString; use std::sync::mpsc; diff --git a/dash-spv-ffi/tests/unit/test_configuration.rs b/dash-spv-ffi/tests/unit/test_configuration.rs index f4f502532..f8bc76239 100644 --- a/dash-spv-ffi/tests/unit/test_configuration.rs +++ b/dash-spv-ffi/tests/unit/test_configuration.rs @@ -1,7 +1,7 @@ #[cfg(test)] mod tests { use crate::*; - use key_wallet_ffi::FFINetwork; + use dashcore::ffi::FFINetwork; use serial_test::serial; use std::ffi::CString; diff --git a/dash-spv-ffi/tests/unit/test_error_handling.rs b/dash-spv-ffi/tests/unit/test_error_handling.rs index dae68c3f9..61b24fe70 100644 --- a/dash-spv-ffi/tests/unit/test_error_handling.rs +++ b/dash-spv-ffi/tests/unit/test_error_handling.rs @@ -1,7 +1,7 @@ #[cfg(test)] mod tests { use crate::*; - use key_wallet_ffi::FFINetwork; + use dashcore::ffi::FFINetwork; use serial_test::serial; use std::ffi::CStr; use std::sync::{Arc, Barrier}; diff --git a/dash-spv-ffi/tests/unit/test_memory_management.rs b/dash-spv-ffi/tests/unit/test_memory_management.rs index a3722df87..9acd7ab2a 100644 --- a/dash-spv-ffi/tests/unit/test_memory_management.rs +++ b/dash-spv-ffi/tests/unit/test_memory_management.rs @@ -1,7 +1,7 @@ #[cfg(test)] mod tests { use crate::*; - use key_wallet_ffi::FFINetwork; + use dashcore::ffi::FFINetwork; use serial_test::serial; use std::ffi::{CStr, CString}; use std::os::raw::{c_char, c_void}; diff --git a/dash-spv-ffi/tests/unit/test_type_conversions.rs b/dash-spv-ffi/tests/unit/test_type_conversions.rs index 90cc7952d..b5e17b432 100644 --- a/dash-spv-ffi/tests/unit/test_type_conversions.rs +++ b/dash-spv-ffi/tests/unit/test_type_conversions.rs @@ -1,7 +1,8 @@ #[cfg(test)] mod tests { + use dashcore::ffi::FFINetwork; + use crate::*; - use key_wallet_ffi::FFINetwork; #[test] fn test_ffi_string_utf8_edge_cases() { diff --git a/dash/Cargo.toml b/dash/Cargo.toml index 2a7d3ae06..ff25a7334 100644 --- a/dash/Cargo.toml +++ b/dash/Cargo.toml @@ -33,6 +33,7 @@ eddsa = ["ed25519-dalek"] quorum_validation = ["bls"] message_verification = ["bls"] bincode = [ "dep:bincode", "dep:bincode_derive", "dashcore_hashes/bincode" ] +ffi = [] test-utils = [] [package.metadata.docs.rs] @@ -72,6 +73,8 @@ dashcore = { path = ".", features = ["core-block-hash-use-x11", "message_verific criterion = "0.5" key-wallet = { path = "../key-wallet" } +[build-dependencies] +cbindgen = "0.29" [[example]] name = "handshake" diff --git a/dash/build.rs b/dash/build.rs index 4621b7f12..fb94f768d 100644 --- a/dash/build.rs +++ b/dash/build.rs @@ -1,4 +1,10 @@ +use std::{env, fs, path::Path}; + fn main() { + if std::env::var("CARGO_FEATURE_FFI").is_ok() { + generate_bindings(); + } + let rustc = std::env::var_os("RUSTC").unwrap_or_else(|| "rustc".into()); let output = std::process::Command::new(rustc) .arg("--version") @@ -29,3 +35,33 @@ fn main() { } } } + +fn generate_bindings() { + let crate_name = env::var("CARGO_PKG_NAME").unwrap(); + let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); + let out_dir = env::var("OUT_DIR").unwrap(); + + println!("cargo:rerun-if-changed=cbindgen.toml"); + println!("cargo:rerun-if-changed=src/"); + + let target_dir = Path::new(&out_dir) + .ancestors() + .nth(3) // This line moves up to the target/ directory + .expect("Failed to find target dir"); + + let include_dir = target_dir.join("include").join(&crate_name); + + fs::create_dir_all(&include_dir).unwrap(); + + let output_path = include_dir.join(format!("{}.h", &crate_name)); + + let config_path = Path::new(&crate_dir).join("cbindgen.toml"); + let config = cbindgen::Config::from_file(&config_path).expect("Failed to read cbindgen.toml"); + + cbindgen::Builder::new() + .with_crate(&crate_dir) + .with_config(config) + .generate() + .expect("Unable to generate bindings") + .write_to_file(&output_path); +} diff --git a/dash/cbindgen.toml b/dash/cbindgen.toml new file mode 100644 index 000000000..f29c577fd --- /dev/null +++ b/dash/cbindgen.toml @@ -0,0 +1,11 @@ +language = "C" +header = "/* dashcore C bindings - Auto-generated by cbindgen */" +include_guard = "DASHCORE_H" +autogen_warning = "/* Warning: This file is auto-generated by cbindgen. Do not modify manually. */" +include_version = true + +[parse.expand] +features = ["ffi"] + +[enum] +prefix_with_name = true diff --git a/dash/src/ffi/mod.rs b/dash/src/ffi/mod.rs new file mode 100644 index 000000000..48f80d4b7 --- /dev/null +++ b/dash/src/ffi/mod.rs @@ -0,0 +1,3 @@ +mod network; + +pub use network::FFINetwork; diff --git a/dash/src/ffi/network.rs b/dash/src/ffi/network.rs new file mode 100644 index 000000000..4eb4c311d --- /dev/null +++ b/dash/src/ffi/network.rs @@ -0,0 +1,77 @@ +use std::ffi; + +use crate::Network; + +/// FFI-compatible variant of [`Network`]. Converts to/from [`Network`] via [`From`]/[`Into`]. +#[repr(C)] +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum FFINetwork { + Mainnet = 0, + Testnet = 1, + Devnet = 2, + Regtest = 3, +} + +impl From for FFINetwork { + fn from(network: Network) -> Self { + match network { + Network::Mainnet => FFINetwork::Mainnet, + Network::Testnet => FFINetwork::Testnet, + Network::Devnet => FFINetwork::Devnet, + Network::Regtest => FFINetwork::Regtest, + } + } +} + +impl From for Network { + fn from(network: FFINetwork) -> Self { + match network { + FFINetwork::Mainnet => Network::Mainnet, + FFINetwork::Testnet => Network::Testnet, + FFINetwork::Devnet => Network::Devnet, + FFINetwork::Regtest => Network::Regtest, + } + } +} + +#[unsafe(no_mangle)] +pub extern "C" fn dashcore_network_get_name(network: FFINetwork) -> *const ffi::c_char { + match network { + FFINetwork::Mainnet => c"mainnet".as_ptr() as *const ffi::c_char, + FFINetwork::Testnet => c"testnet".as_ptr() as *const ffi::c_char, + FFINetwork::Regtest => c"regtest".as_ptr() as *const ffi::c_char, + FFINetwork::Devnet => c"devnet".as_ptr() as *const ffi::c_char, + } +} + +#[cfg(test)] +mod tests { + use std::ffi::CStr; + + use super::*; + + #[test] + fn test_network_names() { + unsafe { + let name = dashcore_network_get_name(FFINetwork::Mainnet); + assert!(!name.is_null()); + let name_str = CStr::from_ptr(name).to_str().unwrap(); + assert_eq!(name_str, "mainnet"); + + let name = dashcore_network_get_name(FFINetwork::Testnet); + assert!(!name.is_null()); + let name_str = CStr::from_ptr(name).to_str().unwrap(); + assert_eq!(name_str, "testnet"); + + let name = dashcore_network_get_name(FFINetwork::Regtest); + assert!(!name.is_null()); + let name_str = CStr::from_ptr(name).to_str().unwrap(); + assert_eq!(name_str, "regtest"); + + let name = dashcore_network_get_name(FFINetwork::Devnet); + assert!(!name.is_null()); + let name_str = CStr::from_ptr(name).to_str().unwrap(); + assert_eq!(name_str, "devnet"); + } + } +} diff --git a/dash/src/lib.rs b/dash/src/lib.rs index 665066659..902db191f 100644 --- a/dash/src/lib.rs +++ b/dash/src/lib.rs @@ -90,29 +90,40 @@ mod parse; #[cfg(feature = "serde")] pub mod serde_utils; +/// cbindgen:ignore #[macro_use] pub mod network; pub mod address; +/// cbindgen:ignore pub mod amount; pub mod base58; pub mod bip152; pub mod bip158; +/// cbindgen:ignore pub mod blockdata; +/// cbindgen:ignore pub mod bloom; +/// cbindgen:ignore pub mod consensus; // Private until we either make this a crate or flatten it - still to be decided. pub mod bls_sig_utils; pub mod crypto; pub mod ephemerealdata; pub mod error; +#[cfg(feature = "ffi")] +pub mod ffi; pub mod hash_types; pub mod merkle_tree; +/// cbindgen:ignore pub mod policy; +/// cbindgen:ignore pub mod pow; pub mod sign_message; pub mod signer; +/// cbindgen:ignore pub mod sml; pub mod string; +/// cbindgen:ignore pub mod taproot; pub mod util; diff --git a/ffi-c-tests/header-tests/spv_and_key_wallet.c b/ffi-c-tests/header-tests/all.c similarity index 77% rename from ffi-c-tests/header-tests/spv_and_key_wallet.c rename to ffi-c-tests/header-tests/all.c index aef34a3f3..bf656c5c9 100644 --- a/ffi-c-tests/header-tests/spv_and_key_wallet.c +++ b/ffi-c-tests/header-tests/all.c @@ -1,4 +1,5 @@ #include "dash-spv-ffi/dash-spv-ffi.h" #include "key-wallet-ffi/key-wallet-ffi.h" +#include "dashcore/dashcore.h" int main() { return 0; } diff --git a/ffi-c-tests/header-tests/dashcore.c b/ffi-c-tests/header-tests/dashcore.c new file mode 100644 index 000000000..1352b44ec --- /dev/null +++ b/ffi-c-tests/header-tests/dashcore.c @@ -0,0 +1,3 @@ +#include "dashcore/dashcore.h" + +int main() { return 0; } diff --git a/key-wallet-ffi/Cargo.toml b/key-wallet-ffi/Cargo.toml index e787a170d..8dcd6a084 100644 --- a/key-wallet-ffi/Cargo.toml +++ b/key-wallet-ffi/Cargo.toml @@ -22,7 +22,7 @@ bls = ["dashcore/bls", "key-wallet/bls"] [dependencies] key-wallet = { path = "../key-wallet" } key-wallet-manager = { path = "../key-wallet-manager" } -dashcore = { path = "../dash" } +dashcore = { path = "../dash", features = ["ffi"] } secp256k1 = { version = "0.30.0", features = ["global-context"] } tokio = { version = "1.32", features = ["rt-multi-thread", "sync"] } libc = "0.2" diff --git a/key-wallet-ffi/FFI_API.md b/key-wallet-ffi/FFI_API.md index 0c538adf5..a90ff908b 100644 --- a/key-wallet-ffi/FFI_API.md +++ b/key-wallet-ffi/FFI_API.md @@ -4,7 +4,7 @@ This document provides a comprehensive reference for all FFI (Foreign Function I **Auto-generated**: This documentation is automatically generated from the source code. Do not edit manually. -**Total Functions**: 260 +**Total Functions**: 259 ## Table of Contents @@ -328,7 +328,7 @@ Functions: 6 ### Utility Functions -Functions: 18 +Functions: 17 | Function | Description | Module | |----------|-------------|--------| @@ -346,7 +346,6 @@ Functions: 18 | `derivation_xpub_fingerprint` | Get fingerprint from extended public key (4 bytes) # Safety - `xpub` must... | derivation | | `derivation_xpub_free` | Free extended public key # Safety - `xpub` must be a valid pointer to an... | derivation | | `derivation_xpub_to_string` | Get extended public key as string # Safety - `xpub` must be a valid... | derivation | -| `ffi_network_get_name` | No description | types | | `free_u32_array` | Free a u32 array allocated by this library # Safety - `array` must be a... | account_collection | | `script_p2pkh` | Create a P2PKH script pubkey # Safety - `pubkey_hash` must be a valid... | transaction | | `string_free` | Free a string # Safety - `s` must be a valid pointer created by C string... | utils | @@ -4319,16 +4318,6 @@ Get extended public key as string # Safety - `xpub` must be a valid pointer to --- -#### `ffi_network_get_name` - -```c -ffi_network_get_name(network: FFINetwork) -> *const c_char -``` - -**Module:** `types` - ---- - #### `free_u32_array` ```c diff --git a/key-wallet-ffi/cbindgen.toml b/key-wallet-ffi/cbindgen.toml index 06e40a313..bf453808c 100644 --- a/key-wallet-ffi/cbindgen.toml +++ b/key-wallet-ffi/cbindgen.toml @@ -14,6 +14,7 @@ autogen_warning = "/* Warning: This file is auto-generated by cbindgen. Do not m include_version = true usize_is_size_t = true no_includes = false +includes = ["../dashcore/dashcore.h"] sys_includes = ["stdint.h", "stddef.h", "stdbool.h"] # Style options diff --git a/key-wallet-ffi/src/account.rs b/key-wallet-ffi/src/account.rs index 2138bf3ee..4fa847d87 100644 --- a/key-wallet-ffi/src/account.rs +++ b/key-wallet-ffi/src/account.rs @@ -1,14 +1,14 @@ //! Account management functions -use std::os::raw::c_uint; -use std::sync::Arc; - use crate::error::{FFIError, FFIErrorCode}; -use crate::types::{FFIAccountResult, FFIAccountType, FFINetwork, FFIWallet}; +use crate::types::{FFIAccountResult, FFIAccountType, FFIWallet}; +use dashcore::ffi::FFINetwork; #[cfg(feature = "bls")] use key_wallet::account::BLSAccount; #[cfg(feature = "eddsa")] use key_wallet::account::EdDSAAccount; +use std::os::raw::c_uint; +use std::sync::Arc; /// Opaque account handle pub struct FFIAccount { diff --git a/key-wallet-ffi/src/account_collection.rs b/key-wallet-ffi/src/account_collection.rs index 29f55b233..b608e2149 100644 --- a/key-wallet-ffi/src/account_collection.rs +++ b/key-wallet-ffi/src/account_collection.rs @@ -1061,6 +1061,8 @@ pub unsafe extern "C" fn account_collection_summary_free( #[cfg(test)] mod tests { + use dashcore::ffi::FFINetwork; + use super::*; use crate::wallet::wallet_create_from_mnemonic_with_options; use std::ffi::CString; @@ -1074,7 +1076,7 @@ mod tests { let wallet = wallet_create_from_mnemonic_with_options( mnemonic.as_ptr(), ptr::null(), - crate::types::FFINetwork::Testnet, + FFINetwork::Testnet, ptr::null(), ptr::null_mut(), ); @@ -1128,7 +1130,7 @@ mod tests { let wallet = wallet_create_from_mnemonic_with_options( mnemonic.as_ptr(), ptr::null(), - crate::types::FFINetwork::Testnet, + FFINetwork::Testnet, &options, ptr::null_mut(), ); @@ -1174,7 +1176,7 @@ mod tests { let wallet = wallet_create_from_mnemonic_with_options( mnemonic.as_ptr(), ptr::null(), - crate::types::FFINetwork::Testnet, + FFINetwork::Testnet, &options, ptr::null_mut(), ); @@ -1244,7 +1246,7 @@ mod tests { let wallet = wallet_create_from_mnemonic_with_options( mnemonic.as_ptr(), ptr::null(), - crate::types::FFINetwork::Testnet, + FFINetwork::Testnet, &options, ptr::null_mut(), ); @@ -1296,7 +1298,7 @@ mod tests { let wallet = wallet_create_from_mnemonic_with_options( mnemonic.as_ptr(), ptr::null(), - crate::types::FFINetwork::Testnet, + FFINetwork::Testnet, &options, ptr::null_mut(), ); @@ -1380,7 +1382,7 @@ mod tests { let wallet = wallet_create_from_mnemonic_with_options( mnemonic.as_ptr(), ptr::null(), - crate::types::FFINetwork::Testnet, + FFINetwork::Testnet, &options, ptr::null_mut(), ); @@ -1466,7 +1468,7 @@ mod tests { let wallet = wallet_create_from_mnemonic_with_options( mnemonic.as_ptr(), ptr::null(), - crate::types::FFINetwork::Testnet, + FFINetwork::Testnet, &options, ptr::null_mut(), ); @@ -1536,7 +1538,7 @@ mod tests { let wallet = wallet_create_from_mnemonic_with_options( mnemonic.as_ptr(), ptr::null(), - crate::types::FFINetwork::Testnet, + FFINetwork::Testnet, ptr::null(), ptr::null_mut(), ); diff --git a/key-wallet-ffi/src/account_derivation_tests.rs b/key-wallet-ffi/src/account_derivation_tests.rs index 448a52c0d..95e2d1a22 100644 --- a/key-wallet-ffi/src/account_derivation_tests.rs +++ b/key-wallet-ffi/src/account_derivation_tests.rs @@ -2,6 +2,8 @@ #[cfg(test)] mod tests { + use dashcore::ffi::FFINetwork; + use crate::account::account_free; use crate::account_derivation::*; use crate::derivation::*; @@ -9,7 +11,6 @@ mod tests { use crate::keys::{extended_private_key_free, private_key_free}; use crate::types::FFIAccountType; use crate::wallet; - use crate::FFINetwork; const MNEMONIC: &str = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"; @@ -54,12 +55,7 @@ mod tests { assert!(ok); let master_xpriv = unsafe { - derivation_new_master_key( - seed.as_ptr(), - seed.len(), - crate::FFINetwork::Testnet, - &mut error, - ) + derivation_new_master_key(seed.as_ptr(), seed.len(), FFINetwork::Testnet, &mut error) }; assert!(!master_xpriv.is_null()); @@ -158,12 +154,7 @@ mod tests { }; assert!(ok); let master_xpriv = unsafe { - derivation_new_master_key( - seed.as_ptr(), - seed.len(), - crate::FFINetwork::Testnet, - &mut error, - ) + derivation_new_master_key(seed.as_ptr(), seed.len(), FFINetwork::Testnet, &mut error) }; assert!(!master_xpriv.is_null()); diff --git a/key-wallet-ffi/src/account_tests.rs b/key-wallet-ffi/src/account_tests.rs index e7d11cf13..eb445288c 100644 --- a/key-wallet-ffi/src/account_tests.rs +++ b/key-wallet-ffi/src/account_tests.rs @@ -5,7 +5,6 @@ mod tests { use crate::error::{FFIError, FFIErrorCode}; use crate::types::FFIAccountType; use crate::wallet; - use crate::FFINetwork; use std::ffi::CString; use std::ptr; @@ -205,7 +204,7 @@ mod tests { assert!(xpub.is_null()); let network = account_get_network(ptr::null()); - assert_eq!(network, crate::FFINetwork::Mainnet); + assert_eq!(network, FFINetwork::Mainnet); let wallet_id = account_get_parent_wallet_id(ptr::null()); assert!(wallet_id.is_null()); diff --git a/key-wallet-ffi/src/address.rs b/key-wallet-ffi/src/address.rs index 94e81b72d..9eec0f5a6 100644 --- a/key-wallet-ffi/src/address.rs +++ b/key-wallet-ffi/src/address.rs @@ -7,8 +7,9 @@ mod tests; use std::ffi::{CStr, CString}; use std::os::raw::{c_char, c_uchar}; +use dashcore::ffi::FFINetwork; + use crate::error::{FFIError, FFIErrorCode}; -use crate::types::FFINetwork; /// Free address string /// diff --git a/key-wallet-ffi/src/address_pool.rs b/key-wallet-ffi/src/address_pool.rs index ef222a85e..0effc9ba5 100644 --- a/key-wallet-ffi/src/address_pool.rs +++ b/key-wallet-ffi/src/address_pool.rs @@ -995,8 +995,9 @@ pub unsafe extern "C" fn address_info_array_free(infos: *mut *mut FFIAddressInfo #[cfg(test)] mod tests { + use dashcore::ffi::FFINetwork; + use super::*; - use crate::FFINetwork; #[test] fn test_address_pool_type_values() { diff --git a/key-wallet-ffi/src/address_tests.rs b/key-wallet-ffi/src/address_tests.rs index 0ec9b6777..1d9e43dfa 100644 --- a/key-wallet-ffi/src/address_tests.rs +++ b/key-wallet-ffi/src/address_tests.rs @@ -2,9 +2,10 @@ #[cfg(test)] mod address_tests { + use dashcore::ffi::FFINetwork; + use crate::address::{address_array_free, address_free, address_get_type, address_validate}; use crate::error::{FFIError, FFIErrorCode}; - use crate::FFINetwork; use std::ffi::CString; use std::ptr; diff --git a/key-wallet-ffi/src/derivation.rs b/key-wallet-ffi/src/derivation.rs index d3cb06c6d..b5b0e9b77 100644 --- a/key-wallet-ffi/src/derivation.rs +++ b/key-wallet-ffi/src/derivation.rs @@ -3,7 +3,7 @@ use crate::error::{FFIError, FFIErrorCode}; use crate::keys::FFIExtendedPrivKey; use crate::keys::FFIExtendedPubKey; -use crate::types::FFINetwork; +use dashcore::ffi::FFINetwork; use dashcore::Network; use key_wallet::{ExtendedPrivKey, ExtendedPubKey}; use secp256k1::Secp256k1; diff --git a/key-wallet-ffi/src/keys.rs b/key-wallet-ffi/src/keys.rs index 608c0bd62..68f9fa3d3 100644 --- a/key-wallet-ffi/src/keys.rs +++ b/key-wallet-ffi/src/keys.rs @@ -1,7 +1,9 @@ //! Key derivation and management +use dashcore::ffi::FFINetwork; + use crate::error::{FFIError, FFIErrorCode}; -use crate::types::{FFINetwork, FFIWallet}; +use crate::types::FFIWallet; use std::ffi::{CStr, CString}; use std::os::raw::{c_char, c_uint}; use std::ptr; diff --git a/key-wallet-ffi/src/keys_tests.rs b/key-wallet-ffi/src/keys_tests.rs index c21c1896b..484351460 100644 --- a/key-wallet-ffi/src/keys_tests.rs +++ b/key-wallet-ffi/src/keys_tests.rs @@ -3,9 +3,10 @@ #[cfg(test)] #[allow(clippy::module_inception)] mod tests { + use dashcore::ffi::FFINetwork; + use crate::error::{FFIError, FFIErrorCode}; use crate::keys::*; - use crate::types::FFINetwork; use crate::wallet; use std::ffi::{CStr, CString}; use std::ptr; diff --git a/key-wallet-ffi/src/lib.rs b/key-wallet-ffi/src/lib.rs index d79ec1870..ec406b33d 100644 --- a/key-wallet-ffi/src/lib.rs +++ b/key-wallet-ffi/src/lib.rs @@ -31,7 +31,7 @@ pub mod bip38; // Re-export main types for convenience pub use error::{FFIError, FFIErrorCode}; -pub use types::{FFIBalance, FFINetwork, FFIWallet}; +pub use types::{FFIBalance, FFIWallet}; pub use utxo::FFIUTXO; pub use wallet_manager::{ wallet_manager_create, wallet_manager_describe, wallet_manager_free, diff --git a/key-wallet-ffi/src/managed_account.rs b/key-wallet-ffi/src/managed_account.rs index 03d89912f..ab5f8de19 100644 --- a/key-wallet-ffi/src/managed_account.rs +++ b/key-wallet-ffi/src/managed_account.rs @@ -7,6 +7,7 @@ use std::os::raw::{c_char, c_uint}; use std::sync::Arc; +use dashcore::ffi::FFINetwork; use dashcore::hashes::Hash; use crate::address_pool::{FFIAddressPool, FFIAddressPoolType}; @@ -16,7 +17,6 @@ use crate::types::{ FFITransactionDirection, FFITransactionType, }; use crate::wallet_manager::FFIWalletManager; -use crate::FFINetwork; use key_wallet::account::account_collection::{DashpayAccountKey, PlatformPaymentAccountKey}; use key_wallet::managed_account::address_pool::AddressPool; use key_wallet::managed_account::managed_platform_account::ManagedPlatformAccount; @@ -1471,7 +1471,6 @@ mod tests { wallet_manager_add_wallet_from_mnemonic_with_options, wallet_manager_create, wallet_manager_free, wallet_manager_free_wallet_ids, wallet_manager_get_wallet_ids, }; - use crate::FFINetwork; use std::ffi::CString; use std::ptr; diff --git a/key-wallet-ffi/src/managed_wallet.rs b/key-wallet-ffi/src/managed_wallet.rs index f94d41f25..5b9e51586 100644 --- a/key-wallet-ffi/src/managed_wallet.rs +++ b/key-wallet-ffi/src/managed_wallet.rs @@ -653,7 +653,7 @@ mod tests { use crate::error::{FFIError, FFIErrorCode}; use crate::managed_wallet::*; use crate::wallet; - use crate::FFINetwork; + use dashcore::ffi::FFINetwork; use key_wallet::managed_account::managed_account_type::ManagedAccountType; use std::ffi::{CStr, CString}; use std::ptr; diff --git a/key-wallet-ffi/src/managed_wallet_tests.rs b/key-wallet-ffi/src/managed_wallet_tests.rs index 08e3b397b..1d972554c 100644 --- a/key-wallet-ffi/src/managed_wallet_tests.rs +++ b/key-wallet-ffi/src/managed_wallet_tests.rs @@ -2,10 +2,12 @@ #[cfg(test)] mod tests { + use dashcore::ffi::FFINetwork; + use crate::address_pool::managed_wallet_mark_address_used; use crate::error::{FFIError, FFIErrorCode}; use crate::managed_wallet::*; - use crate::types::{FFINetwork, FFIWallet}; + use crate::types::FFIWallet; use crate::wallet; use crate::wallet_manager::FFIWalletManager; use crate::wallet_manager::{ diff --git a/key-wallet-ffi/src/transaction.rs b/key-wallet-ffi/src/transaction.rs index 8c51b3636..20439a2da 100644 --- a/key-wallet-ffi/src/transaction.rs +++ b/key-wallet-ffi/src/transaction.rs @@ -5,6 +5,7 @@ use std::os::raw::c_char; use std::ptr; use std::slice; +use dashcore::ffi::FFINetwork; use dashcore::{ consensus, hashes::Hash, sighash::SighashCache, EcdsaSighashType, Network, OutPoint, Script, ScriptBuf, Transaction, TxIn, TxOut, Txid, @@ -19,7 +20,7 @@ use secp256k1::{Message, Secp256k1, SecretKey}; use crate::error::{FFIError, FFIErrorCode}; use crate::types::{ - transaction_context_from_ffi, FFIBlockInfo, FFINetwork, FFITransactionContextType, FFIWallet, + transaction_context_from_ffi, FFIBlockInfo, FFITransactionContextType, FFIWallet, }; use crate::FFIWalletManager; diff --git a/key-wallet-ffi/src/types.rs b/key-wallet-ffi/src/types.rs index 28da9fb50..314e5b69c 100644 --- a/key-wallet-ffi/src/types.rs +++ b/key-wallet-ffi/src/types.rs @@ -5,7 +5,7 @@ use dashcore::hashes::Hash; use key_wallet::managed_account::transaction_record::{OutputRole, TransactionDirection}; use key_wallet::transaction_checking::transaction_router::TransactionType; use key_wallet::transaction_checking::{BlockInfo, TransactionContext}; -use key_wallet::{Network, Wallet}; +use key_wallet::Wallet; use std::os::raw::c_char; use std::sync::Arc; @@ -88,49 +88,6 @@ pub(crate) fn transaction_context_from_ffi( } } -/// FFI Network type (single network) -#[repr(C)] -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum FFINetwork { - Mainnet = 0, - Testnet = 1, - Regtest = 2, - Devnet = 3, -} - -#[no_mangle] -pub extern "C" fn ffi_network_get_name(network: FFINetwork) -> *const c_char { - match network { - FFINetwork::Mainnet => c"mainnet".as_ptr() as *const c_char, - FFINetwork::Testnet => c"testnet".as_ptr() as *const c_char, - FFINetwork::Regtest => c"regtest".as_ptr() as *const c_char, - FFINetwork::Devnet => c"devnet".as_ptr() as *const c_char, - } -} - -impl From for Network { - fn from(net: FFINetwork) -> Self { - match net { - FFINetwork::Mainnet => Network::Mainnet, - FFINetwork::Testnet => Network::Testnet, - FFINetwork::Regtest => Network::Regtest, - FFINetwork::Devnet => Network::Devnet, - } - } -} - -impl From for FFINetwork { - fn from(net: Network) -> Self { - match net { - Network::Mainnet => FFINetwork::Mainnet, - Network::Testnet => FFINetwork::Testnet, - Network::Regtest => FFINetwork::Regtest, - Network::Devnet => FFINetwork::Devnet, - _ => FFINetwork::Mainnet, - } - } -} - /// FFI Balance type for representing wallet balances #[repr(C)] #[derive(Debug, Clone, Copy, Default)] diff --git a/key-wallet-ffi/src/wallet.rs b/key-wallet-ffi/src/wallet.rs index 5c082e470..8eb9deb5b 100644 --- a/key-wallet-ffi/src/wallet.rs +++ b/key-wallet-ffi/src/wallet.rs @@ -10,12 +10,12 @@ use std::ptr; use std::slice; use crate::types::FFIAccountResult; +use dashcore::ffi::FFINetwork; use key_wallet::wallet::initialization::WalletAccountCreationOptions; use key_wallet::{Mnemonic, Seed, Wallet}; use crate::error::{FFIError, FFIErrorCode}; use crate::types::{FFIWallet, FFIWalletAccountCreationOptions}; -use crate::FFINetwork; use key_wallet::Network; /// Create a new wallet from mnemonic with options diff --git a/key-wallet-ffi/src/wallet_manager.rs b/key-wallet-ffi/src/wallet_manager.rs index cd5e29734..50f2548d7 100644 --- a/key-wallet-ffi/src/wallet_manager.rs +++ b/key-wallet-ffi/src/wallet_manager.rs @@ -8,6 +8,7 @@ mod tests; #[path = "wallet_manager_serialization_tests.rs"] mod serialization_tests; +use dashcore::ffi::FFINetwork; use std::ffi::{CStr, CString}; use std::os::raw::{c_char, c_uint}; use std::ptr; @@ -15,7 +16,6 @@ use std::sync::Arc; use tokio::sync::RwLock; use crate::error::{FFIError, FFIErrorCode}; -use crate::FFINetwork; use key_wallet::wallet::managed_wallet_info::ManagedWalletInfo; use key_wallet_manager::WalletInterface; use key_wallet_manager::WalletManager; diff --git a/key-wallet-ffi/src/wallet_manager_serialization_tests.rs b/key-wallet-ffi/src/wallet_manager_serialization_tests.rs index 7b5f33f6e..0979d9abc 100644 --- a/key-wallet-ffi/src/wallet_manager_serialization_tests.rs +++ b/key-wallet-ffi/src/wallet_manager_serialization_tests.rs @@ -2,10 +2,11 @@ #[cfg(all(test, feature = "bincode"))] mod tests { + use dashcore::ffi::FFINetwork; + use crate::error::{FFIError, FFIErrorCode}; use crate::types::FFIWalletAccountCreationOptions; use crate::wallet_manager; - use crate::FFINetwork; use std::ffi::CString; use std::ptr; diff --git a/key-wallet-ffi/src/wallet_manager_tests.rs b/key-wallet-ffi/src/wallet_manager_tests.rs index 2acaa98d0..07e074236 100644 --- a/key-wallet-ffi/src/wallet_manager_tests.rs +++ b/key-wallet-ffi/src/wallet_manager_tests.rs @@ -4,7 +4,8 @@ #[allow(clippy::module_inception)] mod tests { use crate::error::{FFIError, FFIErrorCode}; - use crate::{wallet, wallet_manager, FFINetwork}; + use crate::{wallet, wallet_manager}; + use dashcore::ffi::FFINetwork; use key_wallet_manager::WalletInterface; use std::ffi::{CStr, CString}; use std::ptr; diff --git a/key-wallet-ffi/src/wallet_tests.rs b/key-wallet-ffi/src/wallet_tests.rs index 3771b15fc..0f360b2b0 100644 --- a/key-wallet-ffi/src/wallet_tests.rs +++ b/key-wallet-ffi/src/wallet_tests.rs @@ -2,11 +2,12 @@ #[cfg(test)] mod wallet_tests { + use dashcore::ffi::FFINetwork; + use crate::account::account_free; use crate::error::{FFIError, FFIErrorCode}; use crate::types::FFIAccountType; use crate::wallet; - use crate::FFINetwork; use std::ffi::CString; use std::ptr; diff --git a/key-wallet-ffi/tests/debug_wallet_add.rs b/key-wallet-ffi/tests/debug_wallet_add.rs index 0be13be10..abb940568 100644 --- a/key-wallet-ffi/tests/debug_wallet_add.rs +++ b/key-wallet-ffi/tests/debug_wallet_add.rs @@ -1,8 +1,9 @@ +use dashcore::ffi::FFINetwork; + #[test] fn test_debug_wallet_add() { use key_wallet_ffi::error::FFIError; use key_wallet_ffi::wallet_manager; - use key_wallet_ffi::FFINetwork; use std::ffi::CString; let mut error = FFIError::success(); diff --git a/key-wallet-ffi/tests/integration_test.rs b/key-wallet-ffi/tests/integration_test.rs index 0136acd48..c7fdb1adb 100644 --- a/key-wallet-ffi/tests/integration_test.rs +++ b/key-wallet-ffi/tests/integration_test.rs @@ -2,8 +2,8 @@ //! //! These tests verify the interaction between different FFI modules +use dashcore::ffi::FFINetwork; use key_wallet_ffi::error::{FFIError, FFIErrorCode}; -use key_wallet_ffi::FFINetwork; use std::ffi::CString; use std::ptr; diff --git a/key-wallet-ffi/tests/test_account_collection.rs b/key-wallet-ffi/tests/test_account_collection.rs index aea299932..7e18d9688 100644 --- a/key-wallet-ffi/tests/test_account_collection.rs +++ b/key-wallet-ffi/tests/test_account_collection.rs @@ -1,10 +1,10 @@ //! Integration tests for account collection FFI functions +use dashcore::ffi::FFINetwork; use key_wallet_ffi::account::account_free; use key_wallet_ffi::account_collection::*; use key_wallet_ffi::types::{FFIAccountCreationOptionType, FFIWalletAccountCreationOptions}; use key_wallet_ffi::wallet::{wallet_create_from_mnemonic_with_options, wallet_free}; -use key_wallet_ffi::FFINetwork; use std::ffi::CString; use std::ptr; diff --git a/key-wallet-ffi/tests/test_addr_simple.rs b/key-wallet-ffi/tests/test_addr_simple.rs index 8710179a0..1804267f4 100644 --- a/key-wallet-ffi/tests/test_addr_simple.rs +++ b/key-wallet-ffi/tests/test_addr_simple.rs @@ -1,7 +1,8 @@ +use dashcore::ffi::FFINetwork; + #[test] fn test_address_simple() { use key_wallet_ffi::error::FFIError; - use key_wallet_ffi::FFINetwork; let mut error = FFIError::success(); let error = &mut error as *mut FFIError; diff --git a/key-wallet-ffi/tests/test_import_wallet.rs b/key-wallet-ffi/tests/test_import_wallet.rs index ddf0f4042..a91bf454d 100644 --- a/key-wallet-ffi/tests/test_import_wallet.rs +++ b/key-wallet-ffi/tests/test_import_wallet.rs @@ -3,10 +3,10 @@ #[cfg(feature = "bincode")] #[cfg(test)] mod tests { + use dashcore::ffi::FFINetwork; use key_wallet_ffi::error::{FFIError, FFIErrorCode}; use key_wallet_ffi::wallet::wallet_free_const; use key_wallet_ffi::wallet_manager::*; - use key_wallet_ffi::FFINetwork; use std::os::raw::c_char; use std::ptr; diff --git a/key-wallet-ffi/tests/test_managed_account_collection.rs b/key-wallet-ffi/tests/test_managed_account_collection.rs index 44f3d9195..fdbcee635 100644 --- a/key-wallet-ffi/tests/test_managed_account_collection.rs +++ b/key-wallet-ffi/tests/test_managed_account_collection.rs @@ -1,5 +1,6 @@ //! Tests for managed account collection FFI bindings +use dashcore::ffi::FFINetwork; use key_wallet_ffi::error::{FFIError, FFIErrorCode}; use key_wallet_ffi::managed_account_collection::*; use key_wallet_ffi::types::{FFIAccountCreationOptionType, FFIWalletAccountCreationOptions}; @@ -7,7 +8,6 @@ use key_wallet_ffi::wallet_manager::{ wallet_manager_add_wallet_from_mnemonic_with_options, wallet_manager_create, wallet_manager_free, wallet_manager_free_wallet_ids, wallet_manager_get_wallet_ids, }; -use key_wallet_ffi::FFINetwork; use std::ffi::CString; use std::ptr; diff --git a/key-wallet-ffi/tests/test_passphrase_wallets.rs b/key-wallet-ffi/tests/test_passphrase_wallets.rs index e6cb13822..3faddf146 100644 --- a/key-wallet-ffi/tests/test_passphrase_wallets.rs +++ b/key-wallet-ffi/tests/test_passphrase_wallets.rs @@ -1,8 +1,8 @@ //! Tests for wallet creation with passphrase through FFI //! These tests demonstrate current issues with passphrase handling in the FFI layer +use dashcore::ffi::FFINetwork; use key_wallet_ffi::error::{FFIError, FFIErrorCode}; -use key_wallet_ffi::FFINetwork; use std::ffi::CString; #[test]