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
89 changes: 89 additions & 0 deletions packages/rs-platform-wallet-ffi/src/dashpay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,75 @@ pub unsafe extern "C" fn platform_wallet_fetch_sent_contact_requests(
// Send payment
// ---------------------------------------------------------------------------

/// Reserve a fresh contact Core address for a Platform or shielded withdrawal.
/// The address is durably consumed before success; callers must not reuse it.
/// This does not submit a payment or record payment history.
///
/// # Safety
/// - Identity pointers must each reference 32 readable bytes.
/// - `core_signer_handle` must remain valid throughout this synchronous call.
/// - `out_address` must point to writable pointer storage. On success, free
/// the returned string with `platform_wallet_string_free`.
#[no_mangle]
pub unsafe extern "C" fn platform_wallet_reserve_dashpay_payment_address(
wallet_handle: Handle,
from_identity_id: *const u8,
to_contact_identity_id: *const u8,
core_signer_handle: *mut MnemonicResolverHandle,
out_address: *mut *mut c_char,
) -> PlatformWalletFFIResult {
check_ptr!(out_address);
// Sentinel first, before any other fallible check, so every early return
// leaves `*out_address` null for a cleanup-on-error caller.
*out_address = std::ptr::null_mut();
check_ptr!(core_signer_handle);
let from_id = unwrap_result_or_return!(read_identifier(from_identity_id));
let to_id = unwrap_result_or_return!(read_identifier(to_contact_identity_id));
let signer_addr = core_signer_handle as usize;
// Look the identity up under the registry guard, but wait outside it:
// the reservation waits on the manager write lock, the contact-payment
// gate, and the host store + flush, and a registry read guard held
// across those would stall `platform_wallet_destroy` and, through
// parking_lot's writer preference, every other registry reader.
let option = PLATFORM_WALLET_STORAGE.with_item(wallet_handle, |wallet| {
Comment thread
PastaPastaPasta marked this conversation as resolved.
Resolved
(
wallet.identity().clone(),
wallet.wallet_id(),
wallet.network(),
)
});
let (identity, wallet_id, network) = unwrap_option_or_return!(option);
// SAFETY: `signer_addr` came from `core_signer_handle`, which the caller
// pins alive for the duration of this synchronous call; the provider is
// dropped when the worker task completes, before this call returns.
let provider = unsafe {
resolver_contact_crypto_provider(
signer_addr as *mut MnemonicResolverHandle,
wallet_id,
network,
)
};
let result = block_on_worker(async move {
identity
.dashpay()
.reserve_payment_address(&from_id, &to_id, &provider)
.await
});
let address = match result {
Ok(address) => address,
Err(e @ platform_wallet::PlatformWalletError::SeedMismatch { .. }) => {
return PlatformWalletFFIResult::err(
PlatformWalletFFIResultCode::ErrorInvalidParameter,
e.to_string(),
);
}
Err(e) => return e.into(),
};
let c_str = unwrap_result_or_return!(std::ffi::CString::new(address.to_string()));
*out_address = c_str.into_raw();
PlatformWalletFFIResult::ok()
}

/// Send a Dash payment from `from_identity_id` to `to_contact_identity_id`.
///
/// The funding inputs are signed through the supplied
Expand Down Expand Up @@ -1373,6 +1442,26 @@ mod tests {
);
}

/// A null `core_signer_handle` is rejected with `ErrorNullPointer`, and
/// `*out_address` is already nulled when it is, so a cleanup-on-error
/// caller that did not pre-initialize the slot never frees garbage.
#[test]
fn reserve_dashpay_payment_address_null_signer_nulls_out_address() {
let id = [0u8; 32];
let mut out_address: *mut c_char = std::ptr::dangling_mut();
let r = unsafe {
platform_wallet_reserve_dashpay_payment_address(
1,
id.as_ptr(),
id.as_ptr(),
std::ptr::null_mut(),
&mut out_address,
)
};
assert_eq!(r.code, PlatformWalletFFIResultCode::ErrorNullPointer);
assert!(out_address.is_null());
}

/// A null `out_count` is rejected with `ErrorNullPointer` (the `check_ptr!`
/// contract) before any wallet lookup.
#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@ impl EventHandler for DashPayPaymentHandler {
let event = event.clone();
self.tasks.spawn(async move {
let wallet_id = event.wallet_id();
// A fresh handle carries its own contact-payment gate, so it must
// only write incoming-payment history, never the external-account
// pools that `WalletPersister::lock_contact_payments` orders.
let wallet_persister =
crate::wallet::persister::WalletPersister::new(wallet_id, persister);
run_dashpay_payment_hooks(&wallet_manager, &wallet_id, &wallet_persister, &event).await;
Expand Down
Loading
Loading