Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dash-spv-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion dash-spv-ffi/cbindgen.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
3 changes: 2 additions & 1 deletion dash-spv-ffi/src/bin/ffi_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
3 changes: 2 additions & 1 deletion dash-spv-ffi/src/config.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 2 additions & 2 deletions dash-spv-ffi/tests/dashd_sync/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion dash-spv-ffi/tests/test_client.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion dash-spv-ffi/tests/test_config.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
2 changes: 1 addition & 1 deletion dash-spv-ffi/tests/test_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
27 changes: 0 additions & 27 deletions dash-spv-ffi/tests/test_utils.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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");
}
}
}
3 changes: 2 additions & 1 deletion dash-spv-ffi/tests/test_wallet_manager.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#[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::{
wallet_manager::{
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};
Expand Down
2 changes: 1 addition & 1 deletion dash-spv-ffi/tests/unit/test_async_operations.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
2 changes: 1 addition & 1 deletion dash-spv-ffi/tests/unit/test_client_lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion dash-spv-ffi/tests/unit/test_configuration.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
2 changes: 1 addition & 1 deletion dash-spv-ffi/tests/unit/test_error_handling.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
2 changes: 1 addition & 1 deletion dash-spv-ffi/tests/unit/test_memory_management.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
3 changes: 2 additions & 1 deletion dash-spv-ffi/tests/unit/test_type_conversions.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
3 changes: 3 additions & 0 deletions dash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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"
Expand Down
36 changes: 36 additions & 0 deletions dash/build.rs
Original file line number Diff line number Diff line change
@@ -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")
Expand Down Expand Up @@ -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/<PROFILE> 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);
}
11 changes: 11 additions & 0 deletions dash/cbindgen.toml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions dash/src/ffi/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod network;

pub use network::FFINetwork;
77 changes: 77 additions & 0 deletions dash/src/ffi/network.rs
Original file line number Diff line number Diff line change
@@ -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<Network> 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<FFINetwork> 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,
}
Comment thread
ZocoLini marked this conversation as resolved.
}

#[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");
}
Comment thread
ZocoLini marked this conversation as resolved.
}
}
11 changes: 11 additions & 0 deletions dash/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Comment thread
xdustinface marked this conversation as resolved.
/// 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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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; }
Loading
Loading