diff --git a/payjoin-ffi/src/send/error.rs b/payjoin-ffi/src/send/error.rs index ed5438cde..da1336ac0 100644 --- a/payjoin-ffi/src/send/error.rs +++ b/payjoin-ffi/src/send/error.rs @@ -8,18 +8,36 @@ use crate::error::{FfiValidationError, ImplementationError}; /// Error building a Sender from a SenderBuilder. /// /// This error is unrecoverable. +/// +/// When the error is caused by an invalid original PSBT input, the +/// [`input_index`](BuildSenderError::input_index) method returns the +/// zero-based position of the offending input so that FFI consumers can +/// surface precise diagnostics. #[derive(Debug, PartialEq, Eq, thiserror::Error, uniffi::Object)] #[error("Error initializing the sender: {msg}")] pub struct BuildSenderError { msg: String, + input_index: Option, +} + +#[uniffi::export] +impl BuildSenderError { + /// Returns the zero-based index of the PSBT input that failed + /// validation, if this error was caused by an invalid original input. + pub fn input_index(&self) -> Option { self.input_index } } impl From for BuildSenderError { - fn from(value: PsbtParseError) -> Self { BuildSenderError { msg: value.to_string() } } + fn from(value: PsbtParseError) -> Self { + BuildSenderError { msg: value.to_string(), input_index: None } + } } impl From for BuildSenderError { - fn from(value: send::BuildSenderError) -> Self { BuildSenderError { msg: value.to_string() } } + fn from(value: send::BuildSenderError) -> Self { + let input_index = value.input_index().map(|i| i as u64); + BuildSenderError { msg: value.to_string(), input_index } + } } /// FFI-visible PSBT parsing error surfaced at the sender boundary. @@ -195,3 +213,31 @@ where SenderPersistedError::Unexpected } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn ffi_build_sender_error_with_input_index() { + let err = BuildSenderError { msg: "invalid PSBT input #5".into(), input_index: Some(5) }; + assert_eq!(err.input_index(), Some(5)); + assert!(err.to_string().contains("#5")); + } + + #[test] + fn ffi_build_sender_error_without_input_index() { + let err = BuildSenderError { + msg: "the original transaction has no inputs".into(), + input_index: None, + }; + assert_eq!(err.input_index(), None); + } + + #[test] + fn ffi_build_sender_error_from_psbt_parse_error() { + let parse_err = PsbtParseError::InvalidPsbt("bad psbt".into()); + let err = BuildSenderError::from(parse_err); + assert_eq!(err.input_index(), None); + } +} diff --git a/payjoin/src/core/psbt/mod.rs b/payjoin/src/core/psbt/mod.rs index c02eaef59..6ba1008ed 100644 --- a/payjoin/src/core/psbt/mod.rs +++ b/payjoin/src/core/psbt/mod.rs @@ -353,6 +353,16 @@ impl fmt::Display for PsbtInputsError { } } +impl PsbtInputsError { + /// Returns the index of the PSBT input that failed validation. + pub fn index(&self) -> usize { self.index } + + #[cfg(test)] + pub(crate) fn new_test(index: usize, error: InternalPsbtInputError) -> Self { + Self { index, error } + } +} + impl std::error::Error for PsbtInputsError { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.error) } } @@ -430,7 +440,10 @@ mod test { use bitcoin::{Psbt, ScriptBuf, Transaction}; use payjoin_test_utils::PARSED_ORIGINAL_PSBT; - use crate::psbt::{InputWeightError, InternalInputPair, InternalPsbtInputError, PsbtExt}; + use crate::psbt::{ + InputWeightError, InternalInputPair, InternalPsbtInputError, PrevTxOutError, PsbtExt, + PsbtInputsError, + }; #[test] fn validate_input_utxos() { @@ -541,4 +554,14 @@ mod test { let weight = pair.expected_input_weight(); assert_eq!(weight.unwrap_err(), InputWeightError::NoRedeemScript) } + + #[test] + fn psbt_inputs_error_exposes_index() { + let error = PsbtInputsError { + index: 7, + error: InternalPsbtInputError::PrevTxOut(PrevTxOutError::MissingUtxoInformation), + }; + assert_eq!(error.index(), 7); + assert!(error.to_string().contains("#7")); + } } diff --git a/payjoin/src/core/send/error.rs b/payjoin/src/core/send/error.rs index 7e70286b5..b8a56a5a3 100644 --- a/payjoin/src/core/send/error.rs +++ b/payjoin/src/core/send/error.rs @@ -30,6 +30,17 @@ pub(crate) enum InternalBuildSenderError { AddressType(crate::psbt::AddressTypeError), } +impl BuildSenderError { + /// Returns the index of the invalid PSBT input, if this error was + /// caused by an invalid original input. + pub fn input_index(&self) -> Option { + match &self.0 { + InternalBuildSenderError::InvalidOriginalInput(e) => Some(e.index()), + _ => None, + } + } +} + impl From for BuildSenderError { fn from(value: InternalBuildSenderError) -> Self { BuildSenderError(value) } } @@ -435,4 +446,22 @@ mod tests { ResponseError::Validation(_) )); } + + #[test] + fn build_sender_error_exposes_input_index() { + use crate::psbt::{InternalPsbtInputError, PrevTxOutError, PsbtInputsError}; + + let psbt_err = PsbtInputsError::new_test( + 3, + InternalPsbtInputError::PrevTxOut(PrevTxOutError::MissingUtxoInformation), + ); + let err = BuildSenderError::from(InternalBuildSenderError::InvalidOriginalInput(psbt_err)); + assert_eq!(err.input_index(), Some(3)); + } + + #[test] + fn build_sender_error_no_index_for_other_variants() { + let err = BuildSenderError::from(InternalBuildSenderError::NoInputs); + assert_eq!(err.input_index(), None); + } }