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
319 changes: 255 additions & 64 deletions packages/rs-platform-wallet-ffi/src/invitation.rs

Large diffs are not rendered by default.

9 changes: 9 additions & 0 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
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