Skip to content
Draft
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
50 changes: 48 additions & 2 deletions payjoin-ffi/src/send/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64>,
}

#[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<u64> { self.input_index }
}

impl From<PsbtParseError> 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<send::BuildSenderError> 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.
Expand Down Expand Up @@ -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);
}
}
25 changes: 24 additions & 1 deletion payjoin/src/core/psbt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
}
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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"));
}
}
29 changes: 29 additions & 0 deletions payjoin/src/core/send/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize> {
match &self.0 {
InternalBuildSenderError::InvalidOriginalInput(e) => Some(e.index()),
_ => None,
}
}
}

impl From<InternalBuildSenderError> for BuildSenderError {
fn from(value: InternalBuildSenderError) -> Self { BuildSenderError(value) }
}
Expand Down Expand Up @@ -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);
}
}
Loading