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
5 changes: 5 additions & 0 deletions packages/rs-platform-wallet-ffi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,11 @@ impl From<PlatformWalletError> for PlatformWalletFFIResult {
PlatformWalletError::AssetLockNotTracked(..) => {
PlatformWalletFFIResultCode::ErrorAssetLockNotTracked
}
// A DashPay invitation link for the other network: definitive, so
// hosts can say so instead of treating it as undetermined.
PlatformWalletError::InvitationNetworkMismatch { .. } => {
PlatformWalletFFIResultCode::ErrorInvalidNetwork
}
PlatformWalletError::AssetLockAlreadyConsumed(..) => {
PlatformWalletFFIResultCode::ErrorAssetLockAlreadyConsumed
}
Expand Down
153 changes: 69 additions & 84 deletions packages/rs-platform-wallet-ffi/src/invitation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use std::os::raw::c_char;

use dpp::identity::accessors::IdentityGettersV0;
use platform_wallet::wallet::identity::crypto::{
parse_invitation_uri, wif_network_matches, InviterInfo,
parse_invitation_uri, InviterInfo, ParsedInvitation,
};
use rs_sdk_ffi::{MnemonicResolverCoreSigner, MnemonicResolverHandle, SignerHandle, VTableSigner};

Expand Down Expand Up @@ -336,6 +336,19 @@ pub unsafe extern "C" fn platform_wallet_claim_invitation(
PlatformWalletFFIResult::ok()
}

/// Parse a link for one of the read-only invitation queries. A malformed link
/// is definitive, not undetermined: there is no invitation to claim, so it is
/// its own code (`ErrorInvalidParameter`) and the caller can say so instead of
/// falling through the generic arm into "proceed anyway".
fn parse_invitation_for_query(uri: &str) -> Result<ParsedInvitation, PlatformWalletFFIResult> {
parse_invitation_uri(uri).map_err(|e| {
PlatformWalletFFIResult::err(
PlatformWalletFFIResultCode::ErrorInvalidParameter,
format!("invitation link is malformed and cannot be claimed: {e}"),
)
})
}

/// The identity id this invitation WOULD create — a read-only probe that lets
/// the UI reject an already-claimed voucher up front.
///
Expand Down Expand Up @@ -369,8 +382,7 @@ pub unsafe extern "C" fn platform_wallet_claim_invitation(
///
/// * `ErrorInvalidParameter` — the URI is malformed, so there is no invitation.
/// * `ErrorInvalidNetwork` — the voucher key belongs to the other network;
/// [`platform_wallet_claim_invitation`] applies the same guard and will
/// refuse it too.
/// [`platform_wallet_claim_invitation`] refuses it with the same code.
///
/// Every other failure (funding-tx not yet propagated, transport error) leaves
/// usability genuinely undetermined, and only those should be treated as
Expand All @@ -385,58 +397,31 @@ pub unsafe extern "C" fn platform_wallet_invitation_prospective_identity_id(
uri: *const c_char,
out_identity_id: *mut [u8; 32],
) -> PlatformWalletFFIResult {
check_ptr!(uri);
check_ptr!(out_identity_id);
// Sentinel before any fallible work, matching the claim/parse siblings.
unsafe {
*out_identity_id = [0u8; 32];
}
check_ptr!(uri);

let uri = unwrap_result_or_return!(unsafe { CStr::from_ptr(uri) }.to_str());
// A malformed link is definitive, not undetermined: there is no invitation
// to claim. Surfaced as its own code so the caller can say so instead of
// falling through the generic arm into "proceed anyway".
let invitation = match parse_invitation_uri(uri) {
let invitation = match parse_invitation_for_query(uri) {
Ok(invitation) => invitation,
Err(e) => {
return PlatformWalletFFIResult::err(
PlatformWalletFFIResultCode::ErrorInvalidParameter,
format!("invitation link is malformed and cannot be claimed: {e}"),
);
}
Err(e) => return e,
};

let option = PLATFORM_WALLET_STORAGE.with_item(
wallet_handle,
|wallet| -> Result<dash_sdk::platform::Identifier, PlatformWalletFFIResult> {
// Also definitive: the claim applies the same guard, so a link for the
// other network can never be claimed through this wallet. Checked here
// (as the withdrawal FFI does) to give it a distinguishable code rather
// than flattening into the catch-all the library error maps to.
if !wif_network_matches(invitation.voucher_key_network, wallet.network()) {
return Err(PlatformWalletFFIResult::err(
PlatformWalletFFIResultCode::ErrorInvalidNetwork,
format!(
"invitation is for the {:?} network but this wallet is on {:?}",
invitation.voucher_key_network,
wallet.network()
),
));
}
let identity_wallet = wallet.identity().clone();
block_on_worker(async move {
identity_wallet
.invitation_prospective_identity_id(&invitation)
.await
})
.map_err(PlatformWalletFFIResult::from)
},
);
// A link for the other network comes back as the library's typed
// mismatch, which maps to `ErrorInvalidNetwork`.
let option = PLATFORM_WALLET_STORAGE.with_item(wallet_handle, |wallet| {
let identity_wallet = wallet.identity().clone();
block_on_worker(async move {
identity_wallet
.invitation_prospective_identity_id(&invitation)
.await
})
});
let result = unwrap_option_or_return!(option);
let identifier = match result {
Ok(identifier) => identifier,
Err(e) => return e,
};
let identifier = unwrap_result_or_return!(result);
unsafe {
*out_identity_id = identifier.to_buffer();
}
Expand Down Expand Up @@ -488,7 +473,9 @@ impl From<InvitationClaimStatus> for InvitationClaimStatusFFI {

/// What an invitation is worth and whether it was already claimed, without
/// claiming it: one funding-tx fetch (with the claim's propagation retry) and
/// one identity fetch.
/// one identity fetch. A ChainLock-only link whose funding tx is not
/// chain-locked yet is reported (`is_instant` and `is_chain_locked` both
/// false) rather than refused.
///
/// Same error contract as
/// [`platform_wallet_invitation_prospective_identity_id`]:
Expand All @@ -509,48 +496,24 @@ pub unsafe extern "C" fn platform_wallet_invitation_claim_status(
uri: *const c_char,
out_status: *mut InvitationClaimStatusFFI,
) -> PlatformWalletFFIResult {
check_ptr!(uri);
check_ptr!(out_status);
unsafe {
*out_status = InvitationClaimStatusFFI::zeroed();
}
check_ptr!(uri);

let uri = unwrap_result_or_return!(unsafe { CStr::from_ptr(uri) }.to_str());
let invitation = match parse_invitation_uri(uri) {
let invitation = match parse_invitation_for_query(uri) {
Ok(invitation) => invitation,
Err(e) => {
return PlatformWalletFFIResult::err(
PlatformWalletFFIResultCode::ErrorInvalidParameter,
format!("invitation link is malformed and cannot be claimed: {e}"),
);
}
Err(e) => return e,
};

let option = PLATFORM_WALLET_STORAGE.with_item(
wallet_handle,
|wallet| -> Result<InvitationClaimStatus, PlatformWalletFFIResult> {
if !wif_network_matches(invitation.voucher_key_network, wallet.network()) {
return Err(PlatformWalletFFIResult::err(
PlatformWalletFFIResultCode::ErrorInvalidNetwork,
format!(
"invitation is for the {:?} network but this wallet is on {:?}",
invitation.voucher_key_network,
wallet.network()
),
));
}
let identity_wallet = wallet.identity().clone();
block_on_worker(
async move { identity_wallet.invitation_claim_status(&invitation).await },
)
.map_err(PlatformWalletFFIResult::from)
},
);
let option = PLATFORM_WALLET_STORAGE.with_item(wallet_handle, |wallet| {
let identity_wallet = wallet.identity().clone();
block_on_worker(async move { identity_wallet.invitation_claim_status(&invitation).await })
});
let result = unwrap_option_or_return!(option);
let status = match result {
Ok(status) => status,
Err(e) => return e,
};
let status = unwrap_result_or_return!(result);
unsafe {
*out_status = status.into();
}
Expand Down Expand Up @@ -587,7 +550,8 @@ pub struct InvitationPreviewFFI {
/// [`crate::platform_wallet_string_free`].
pub inviter_username: *mut c_char,
/// Amount locked in the voucher (duffs) — always 0: unknown pre-fetch (the
/// link carries the funding txid, not the proof), resolved at claim time.
/// link carries the funding txid, not the proof). Read it with
/// [`platform_wallet_invitation_claim_status`].
pub amount_duffs: u64,
/// Advisory expiry (unix seconds) — always 0: the legacy link carries no
/// expiry field.
Expand Down Expand Up @@ -637,8 +601,9 @@ fn optional_c_string(value: Option<&String>) -> *mut c_char {
/// the UI can render a clean "invalid invitation" state; only a null / non-UTF-8
/// `uri` argument returns an error result.
///
/// When `out_preview.inviter_username` is non-null it is a heap C string the
/// caller frees with [`crate::platform_wallet_string_free`]. It can be null
/// `out_preview.inviter_username`, `inviter_display_name` and
/// `inviter_avatar_url` are each either null or a heap C string the caller
/// frees with [`crate::platform_wallet_string_free`]. The username can be null
/// even when `has_inviter` is set (a metadata-only link) — see the field docs.
///
/// # Safety
Expand Down Expand Up @@ -959,7 +924,7 @@ mod tests {
/// The inviter's display name and avatar URL travel through the preview
/// (percent-decoded), and both are null for a link that carries neither.
#[test]
fn parse_invitation_surfaces_inviter_display_name_and_avatar() {
fn should_surface_inviter_display_name_and_avatar_in_the_preview() {
let key = SecretKey::from_slice(&[0x11u8; 32]).unwrap();
let wif = PrivateKey::new(key, Network::Testnet).to_wif();
let txid = "ab".repeat(32);
Expand Down Expand Up @@ -1006,7 +971,7 @@ mod tests {
/// A malformed link is a definitive `ErrorInvalidParameter` from the status
/// call — never a zeroed "not claimed" the caller could proceed on.
#[test]
fn claim_status_malformed_uri_is_invalid_parameter() {
fn should_refuse_a_malformed_link_in_claim_status_as_invalid_parameter() {
let bad = std::ffi::CString::new("https://not-an-invite").unwrap();
let mut status = InvitationClaimStatusFFI::zeroed();
status.amount_duffs = 7;
Expand All @@ -1015,13 +980,33 @@ mod tests {
assert_eq!(status.amount_duffs, 0, "the out-param is zeroed first");
}

/// A null `uri` is rejected with `ErrorNullPointer`.
/// A null `uri` is rejected with `ErrorNullPointer`, after the out-param
/// is zeroed.
#[test]
fn claim_status_null_uri_is_null_pointer() {
fn should_refuse_a_null_uri_in_claim_status_and_still_zero_the_status() {
let mut status = InvitationClaimStatusFFI::zeroed();
status.already_claimed = true;
let r =
unsafe { platform_wallet_invitation_claim_status(0, std::ptr::null(), &mut status) };
assert_eq!(r.code, PlatformWalletFFIResultCode::ErrorNullPointer);
assert!(!status.already_claimed, "the out-param is zeroed first");
}

/// A link for the other network is refused by the library with a typed
/// error that must reach hosts as the definitive `ErrorInvalidNetwork`,
/// not the undetermined catch-all.
#[test]
fn should_map_an_invitation_network_mismatch_to_invalid_network() {
let result = PlatformWalletFFIResult::from(
platform_wallet::PlatformWalletError::InvitationNetworkMismatch {
invitation: Network::Testnet,
wallet: Network::Mainnet,
},
);
assert_eq!(
result.code,
PlatformWalletFFIResultCode::ErrorInvalidNetwork
);
}

/// The amount-bound getters hand back the library constants verbatim. This
Expand Down
28 changes: 9 additions & 19 deletions packages/rs-platform-wallet/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ pub enum PlatformWalletError {
#[error("Invalid identity data: {0}")]
InvalidIdentityData(String),

/// A DashPay invitation link's voucher key is for the other network (a
/// testnet link opened in a mainnet wallet, or the reverse). Definitive:
/// the claim refuses the link before any network work.
#[error("invitation is for the {invitation:?} network but this wallet is on {wallet:?}")]
InvitationNetworkMismatch {
invitation: Network,
wallet: Network,
},

#[error("Failed to persist state: {0}")]
/// A persister `store(...)` round failed. Returned (not swallowed) by
/// user-initiated writes whose loss leaves a silent, non-self-healing
Expand Down Expand Up @@ -1247,25 +1256,6 @@ pub fn is_asset_lock_already_consumed(
)
}

/// Platform refused an identity create in a way that means the same create
/// was already accepted: the identical transition is still in the mempool
/// ("tx already exists in cache" — a broadcast that timed out after the node
/// took it, then retried elsewhere), or its funding outpoint is consumed.
///
/// Neither says by whom. A caller may treat the create as done only after
/// fetching the identity and checking it carries the keys it submitted.
pub fn is_identity_create_already_landed(error: &dash_sdk::Error) -> bool {
use dpp::consensus::basic::BasicError;

matches!(error, dash_sdk::Error::AlreadyExists(_))
|| matches!(
consensus_error_of(error),
Some(ConsensusError::BasicError(
BasicError::IdentityAssetLockTransactionOutPointAlreadyConsumedError(_)
))
)
}

/// Promote a document-trade consensus rejection to its typed
/// [`PlatformWalletError`] so callers get structured data instead of a
/// stringified verdict:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

use dashcore::secp256k1::{PublicKey, Secp256k1, SecretKey};
use dashcore::transaction::special_transaction::TransactionPayload;
use dashcore::{Network, PrivateKey, ScriptBuf, Transaction};
use dashcore::{Network, PrivateKey, ScriptBuf, Transaction, TxOut};
use dpp::prelude::AssetLockProof;

use crate::error::PlatformWalletError;
Expand Down Expand Up @@ -514,6 +514,15 @@ pub fn voucher_output_index(
transaction: &Transaction,
voucher_key: &SecretKey,
) -> Result<u32, PlatformWalletError> {
voucher_credit_output(transaction, voucher_key).map(|(index, _)| index)
}

/// [`voucher_output_index`] plus the selected credit output itself, so a
/// caller that needs the voucher's value reads it from the same match.
pub fn voucher_credit_output<'a>(
transaction: &'a Transaction,
voucher_key: &SecretKey,
) -> Result<(u32, &'a TxOut), PlatformWalletError> {
let Some(TransactionPayload::AssetLockPayloadType(payload)) =
&transaction.special_transaction_payload
else {
Expand All @@ -525,8 +534,9 @@ pub fn voucher_output_index(
payload
.credit_outputs
.iter()
.position(|out| out.script_pubkey == expected)
.map(|idx| idx as u32)
.enumerate()
.find(|(_, out)| out.script_pubkey == expected)
.map(|(index, out)| (index as u32, out))
.ok_or_else(|| {
invalid("voucher key does not control any credit output of the funding transaction")
})
Expand Down Expand Up @@ -768,6 +778,18 @@ mod tests {
assert_eq!(voucher_output_index(&tx, &key).unwrap(), 2);
}

/// The credit output comes back with its index, so its value is the
/// voucher's, not a decoy's.
#[test]
fn should_return_the_voucher_credit_output_with_its_index() {
let key = voucher();
let tx = asset_lock_tx_paying_voucher_at(&key, 2);
let (index, output) = voucher_credit_output(&tx, &key).unwrap();
assert_eq!(index, 2);
assert_eq!(output.value, 100_000, "the decoys before it carry 50_000");
assert_eq!(output.script_pubkey, voucher_credit_script(&key));
}

#[test]
fn voucher_output_index_rejects_no_match() {
let key = voucher();
Expand Down
4 changes: 2 additions & 2 deletions packages/rs-platform-wallet/src/wallet/identity/crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub use dip14::{
derive_contact_xpub, unmask_account_reference, ContactXpubData, DEFAULT_CONTACT_GAP_LIMIT,
};
pub use invitation::{
encode_invitation_uri, parse_invitation_uri, voucher_output_index, wif_network_matches,
InviterInfo, ParsedInvitation,
encode_invitation_uri, parse_invitation_uri, voucher_credit_output, voucher_output_index,
wif_network_matches, InviterInfo, ParsedInvitation,
};
pub use validation::pubkey_binds_expected_key_data;
Loading
Loading