Skip to content
Open
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
15 changes: 15 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 22 additions & 45 deletions packages/rs-platform-wallet-ffi/src/platform_wallet_info.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use crate::error::*;
use crate::handle::*;
use crate::types::*;
use crate::types::Network;
use key_wallet::wallet::initialization::WalletAccountCreationOptions;
use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface;
use platform_wallet::platform_wallet_info::PlatformWalletInfo;
use std::os::raw::{c_char, c_uchar};

/// Create a new PlatformWalletInfo from seed bytes
#[no_mangle]
pub extern "C" fn platform_wallet_info_create_from_seed(
network: Network,
seed_bytes: *const c_uchar,
seed_len: usize,
out_handle: *mut Handle,
Expand Down Expand Up @@ -44,10 +46,10 @@ pub extern "C" fn platform_wallet_info_create_from_seed(
let mut seed_array = [0u8; 64];
seed_array.copy_from_slice(seed_slice);

// Create wallet from seed - use empty network list, accounts can be added later
// Create wallet from seed
let wallet = match key_wallet::Wallet::from_seed_bytes(
seed_array,
&[], // No networks initially
network,
WalletAccountCreationOptions::None, // No accounts initially
) {
Ok(w) => w,
Expand All @@ -64,14 +66,8 @@ pub extern "C" fn platform_wallet_info_create_from_seed(
}
};

// Create ManagedWalletInfo from the wallet
let wallet_info = key_wallet::wallet::ManagedWalletInfo::from_wallet(&wallet);

// Create PlatformWalletInfo wrapping the ManagedWalletInfo
let platform_wallet = PlatformWalletInfo {
wallet_info,
identity_managers: std::collections::BTreeMap::new(),
};
// Create PlatformWalletInfo from the wallet
let platform_wallet = PlatformWalletInfo::from_wallet(&wallet);

// Store in handle storage
let handle = WALLET_INFO_STORAGE.insert(platform_wallet);
Expand All @@ -83,6 +79,7 @@ pub extern "C" fn platform_wallet_info_create_from_seed(
/// Create a new PlatformWalletInfo from mnemonic
#[no_mangle]
pub extern "C" fn platform_wallet_info_create_from_mnemonic(
network: Network,
mnemonic: *const c_char,
passphrase: *const c_char,
out_handle: *mut Handle,
Expand Down Expand Up @@ -155,7 +152,7 @@ pub extern "C" fn platform_wallet_info_create_from_mnemonic(
match key_wallet::Wallet::from_mnemonic_with_passphrase(
mnemonic_obj,
pass.to_string(),
&[], // No networks initially
network,
WalletAccountCreationOptions::None, // No accounts initially
) {
Ok(w) => w,
Expand All @@ -177,7 +174,7 @@ pub extern "C" fn platform_wallet_info_create_from_mnemonic(
} else {
match key_wallet::Wallet::from_mnemonic(
mnemonic_obj,
&[], // No networks initially
network,
WalletAccountCreationOptions::None, // No accounts initially
) {
Ok(w) => w,
Expand All @@ -195,14 +192,8 @@ pub extern "C" fn platform_wallet_info_create_from_mnemonic(
}
};

// Create ManagedWalletInfo from the wallet
let wallet_info = key_wallet::wallet::ManagedWalletInfo::from_wallet(&wallet);

// Create PlatformWalletInfo wrapping the ManagedWalletInfo
let platform_wallet = PlatformWalletInfo {
wallet_info,
identity_managers: std::collections::BTreeMap::new(),
};
// Create PlatformWalletInfo from the wallet
let platform_wallet = PlatformWalletInfo::from_wallet(&wallet);

// Store in handle storage
let handle = WALLET_INFO_STORAGE.insert(platform_wallet);
Expand All @@ -211,11 +202,10 @@ pub extern "C" fn platform_wallet_info_create_from_mnemonic(
PlatformWalletFFIResult::Success
}

/// Get the identity manager for a specific network
/// Get the identity manager
#[no_mangle]
pub extern "C" fn platform_wallet_info_get_identity_manager(
wallet_handle: Handle,
network: NetworkType,
out_handle: *mut Handle,
out_error: *mut PlatformWalletFFIError,
) -> PlatformWalletFFIResult {
Expand All @@ -233,23 +223,9 @@ pub extern "C" fn platform_wallet_info_get_identity_manager(

WALLET_INFO_STORAGE
.with_item(wallet_handle, |wallet_info| {
let dash_network = network.to_dash_network();

if let Some(manager) = wallet_info.identity_managers.get(&dash_network) {
let handle = IDENTITY_MANAGER_STORAGE.insert(manager.clone());
unsafe { *out_handle = handle };
PlatformWalletFFIResult::Success
} else {
if !out_error.is_null() {
unsafe {
*out_error = PlatformWalletFFIError::new(
PlatformWalletFFIResult::ErrorInvalidNetwork,
format!("No identity manager for network: {:?}", network),
);
}
}
PlatformWalletFFIResult::ErrorInvalidNetwork
}
let handle = IDENTITY_MANAGER_STORAGE.insert(wallet_info.identity_manager.clone());
unsafe { *out_handle = handle };
PlatformWalletFFIResult::Success
})
.unwrap_or_else(|| {
if !out_error.is_null() {
Expand All @@ -264,11 +240,10 @@ pub extern "C" fn platform_wallet_info_get_identity_manager(
})
}

/// Add or update identity manager for a network
/// Set the identity manager
#[no_mangle]
pub extern "C" fn platform_wallet_info_set_identity_manager(
wallet_handle: Handle,
network: NetworkType,
manager_handle: Handle,
out_error: *mut PlatformWalletFFIError,
) -> PlatformWalletFFIResult {
Expand All @@ -292,8 +267,7 @@ pub extern "C" fn platform_wallet_info_set_identity_manager(

WALLET_INFO_STORAGE
.with_item_mut(wallet_handle, |wallet_info| {
let dash_network = network.to_dash_network();
wallet_info.identity_managers.insert(dash_network, manager);
wallet_info.identity_manager = manager;
PlatformWalletFFIResult::Success
})
.unwrap_or_else(|| {
Expand Down Expand Up @@ -353,6 +327,7 @@ pub extern "C" fn platform_wallet_info_destroy(wallet_handle: Handle) -> Platfor

#[cfg(test)]
mod tests {
use crate::platform_wallet_string_free;
use super::*;

#[test]
Expand All @@ -362,6 +337,7 @@ mod tests {
let mut error = PlatformWalletFFIError::success();

let result = platform_wallet_info_create_from_seed(
Network::Testnet,
seed.as_ptr(),
seed.len(),
&mut handle,
Expand All @@ -385,6 +361,7 @@ mod tests {
let mut error = PlatformWalletFFIError::success();

let result = platform_wallet_info_create_from_mnemonic(
Network::Testnet,
mnemonic.as_ptr(),
std::ptr::null(),
&mut handle,
Expand All @@ -405,7 +382,7 @@ mod tests {
let mut handle: Handle = NULL_HANDLE;
let mut error = PlatformWalletFFIError::success();

platform_wallet_info_create_from_seed(seed.as_ptr(), seed.len(), &mut handle, &mut error);
platform_wallet_info_create_from_seed(Network::Testnet, seed.as_ptr(), seed.len(), &mut handle, &mut error);

let mut json_ptr: *mut c_char = std::ptr::null_mut();
let result = platform_wallet_info_to_json(handle, &mut json_ptr, &mut error);
Expand Down
47 changes: 1 addition & 46 deletions packages/rs-platform-wallet-ffi/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,6 @@
use std::os::raw::{c_char, c_uchar};

/// Network types
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NetworkType {
Mainnet = 0,
Testnet = 1,
Devnet = 2,
Regtest = 3,
}

impl NetworkType {
pub fn to_dash_network(&self) -> dashcore::Network {
match self {
NetworkType::Mainnet => dashcore::Network::Dash,
NetworkType::Testnet => dashcore::Network::Testnet,
NetworkType::Devnet => dashcore::Network::Devnet,
NetworkType::Regtest => dashcore::Network::Regtest,
}
}

pub fn from_dash_network(network: dashcore::Network) -> Self {
match network {
dashcore::Network::Dash => NetworkType::Mainnet,
dashcore::Network::Testnet => NetworkType::Testnet,
dashcore::Network::Devnet => NetworkType::Devnet,
dashcore::Network::Regtest => NetworkType::Regtest,
_ => NetworkType::Mainnet,
}
}
}
pub use dashcore::Network;

/// Identifier (32 bytes)
#[repr(C)]
Expand Down Expand Up @@ -155,22 +126,6 @@ pub extern "C" fn platform_wallet_string_free(s: *mut c_char) {
mod tests {
use super::*;

#[test]
fn test_network_type_conversion() {
assert_eq!(
NetworkType::Mainnet.to_dash_network(),
dashcore::Network::Dash
);
assert_eq!(
NetworkType::Testnet.to_dash_network(),
dashcore::Network::Testnet
);
assert_eq!(
NetworkType::from_dash_network(dashcore::Network::Testnet),
NetworkType::Testnet
);
}

#[test]
fn test_identifier_bytes_from_slice() {
let bytes = [0u8; 32];
Expand Down
10 changes: 5 additions & 5 deletions packages/rs-platform-wallet-ffi/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn test_wallet_creation_and_destruction() {
let mut error = PlatformWalletFFIError::success();

let result =
platform_wallet_info_create_from_seed(seed.as_ptr(), seed.len(), &mut handle, &mut error);
platform_wallet_info_create_from_seed(Network::Testnet, seed.as_ptr(), seed.len(), &mut handle, &mut error);

assert_eq!(result, PlatformWalletFFIResult::Success);
assert_ne!(handle, NULL_HANDLE);
Expand All @@ -43,6 +43,7 @@ fn test_wallet_from_mnemonic() {
let mut error = PlatformWalletFFIError::success();

let result = platform_wallet_info_create_from_mnemonic(
Network::Testnet,
mnemonic.as_ptr(),
std::ptr::null(),
&mut handle,
Expand Down Expand Up @@ -179,7 +180,7 @@ fn test_serialization() {
let mut handle: Handle = NULL_HANDLE;
let mut error = PlatformWalletFFIError::success();

platform_wallet_info_create_from_seed(seed.as_ptr(), seed.len(), &mut handle, &mut error);
platform_wallet_info_create_from_seed(Network::Testnet, seed.as_ptr(), seed.len(), &mut handle, &mut error);

// Serialize to JSON - function not yet implemented
// let mut json_ptr: *mut std::os::raw::c_char = std::ptr::null_mut();
Expand Down Expand Up @@ -237,6 +238,7 @@ fn test_error_handling() {

// Try to create wallet with null pointer
let result = platform_wallet_info_create_from_seed(
Network::Testnet,
std::ptr::null(),
0,
std::ptr::null_mut(),
Expand All @@ -260,6 +262,7 @@ fn test_full_workflow() {

let mut wallet_handle: Handle = NULL_HANDLE;
let result = platform_wallet_info_create_from_mnemonic(
Network::Testnet,
mnemonic.as_ptr(),
std::ptr::null(),
&mut wallet_handle,
Expand Down Expand Up @@ -290,10 +293,8 @@ fn test_full_workflow() {
identity_manager_set_primary_identity(manager_handle, id_bytes, &mut error);

// Set identity manager on wallet
let network = NetworkType::Testnet;
let result = platform_wallet_info_set_identity_manager(
wallet_handle,
network,
manager_handle,
&mut error,
);
Expand All @@ -303,7 +304,6 @@ fn test_full_workflow() {
let mut retrieved_manager_handle: Handle = NULL_HANDLE;
let result = platform_wallet_info_get_identity_manager(
wallet_handle,
network,
&mut retrieved_manager_handle,
&mut error,
);
Expand Down
2 changes: 1 addition & 1 deletion packages/rs-platform-wallet/examples/basic_usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn main() -> Result<(), PlatformWalletError> {
println!(
"Total identities on {:?}: {}",
network,
platform_wallet.identities(network).len()
platform_wallet.identities().len()
);

// The platform wallet can be used with WalletManager (requires "manager" feature)
Expand Down
5 changes: 1 addition & 4 deletions packages/rs-platform-wallet/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ pub enum PlatformWalletError {

#[error("Contact request not found: {0}")]
ContactRequestNotFound(Identifier),

#[error("No accounts found for network: {0:?}")]
NoAccountsForNetwork(Network),


#[error(
"DashPay receiving account already exists for identity {identity} with contact {contact} on network {network:?} (account index {account_index})"
)]
Expand Down
12 changes: 12 additions & 0 deletions packages/rs-platform-wallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,15 @@ pub mod contact_request;
pub mod crypto;
pub mod error;
pub mod established_contact;
pub mod identity_manager;
pub mod managed_identity;
pub mod platform_wallet_info;

// Re-export main types at crate root
pub use block_time::BlockTime;
pub use contact_request::ContactRequest;
pub use error::PlatformWalletError;
pub use established_contact::EstablishedContact;
pub use identity_manager::IdentityManager;
pub use managed_identity::ManagedIdentity;
pub use platform_wallet_info::PlatformWalletInfo;
Loading
Loading