Skip to content

Commit bd15da7

Browse files
committed
Make pub PsbtInputError with internal variants
The internal variants aren't yet stable, so we use the InternalError pattern to hide them, and make internal variants pub(crate).
1 parent 241e2ba commit bd15da7

File tree

3 files changed

+43
-17
lines changed

3 files changed

+43
-17
lines changed

payjoin/src/psbt.rs

+32-14
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,13 @@ impl<'a> InternalInputPair<'a> {
133133
}
134134
}
135135

136-
pub fn validate_utxo(&self, treat_missing_as_error: bool) -> Result<(), PsbtInputError> {
136+
pub fn validate_utxo(
137+
&self,
138+
treat_missing_as_error: bool,
139+
) -> Result<(), InternalPsbtInputError> {
137140
match (&self.psbtin.non_witness_utxo, &self.psbtin.witness_utxo) {
138141
(None, None) if treat_missing_as_error =>
139-
Err(PsbtInputError::PrevTxOut(PrevTxOutError::MissingUtxoInformation)),
142+
Err(InternalPsbtInputError::PrevTxOut(PrevTxOutError::MissingUtxoInformation)),
140143
(None, None) => Ok(()),
141144
(Some(tx), None) if tx.compute_txid() == self.txin.previous_output.txid => tx
142145
.output
@@ -154,7 +157,7 @@ impl<'a> InternalInputPair<'a> {
154157
.into()
155158
})
156159
.map(drop),
157-
(Some(_), None) => Err(PsbtInputError::UnequalTxid),
160+
(Some(_), None) => Err(InternalPsbtInputError::UnequalTxid),
158161
(None, Some(_)) => Ok(()),
159162
(Some(tx), Some(witness_txout))
160163
if tx.compute_txid() == self.txin.previous_output.txid =>
@@ -174,10 +177,10 @@ impl<'a> InternalInputPair<'a> {
174177
if witness_txout == non_witness_txout {
175178
Ok(())
176179
} else {
177-
Err(PsbtInputError::SegWitTxOutMismatch)
180+
Err(InternalPsbtInputError::SegWitTxOutMismatch)
178181
}
179182
}
180-
(Some(_), Some(_)) => Err(PsbtInputError::UnequalTxid),
183+
(Some(_), Some(_)) => Err(InternalPsbtInputError::UnequalTxid),
181184
}
182185
}
183186

@@ -227,7 +230,7 @@ impl<'a> InternalInputPair<'a> {
227230
}
228231

229232
#[derive(Debug)]
230-
pub enum PrevTxOutError {
233+
pub(crate) enum PrevTxOutError {
231234
MissingUtxoInformation,
232235
IndexOutOfBounds { output_count: usize, index: u32 },
233236
}
@@ -246,7 +249,7 @@ impl fmt::Display for PrevTxOutError {
246249
impl std::error::Error for PrevTxOutError {}
247250

248251
#[derive(Debug)]
249-
pub enum PsbtInputError {
252+
pub(crate) enum InternalPsbtInputError {
250253
PrevTxOut(PrevTxOutError),
251254
UnequalTxid,
252255
/// TxOut provided in `segwit_utxo` doesn't match the one in `non_segwit_utxo`
@@ -255,7 +258,7 @@ pub enum PsbtInputError {
255258
NoRedeemScript,
256259
}
257260

258-
impl fmt::Display for PsbtInputError {
261+
impl fmt::Display for InternalPsbtInputError {
259262
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
260263
match self {
261264
Self::PrevTxOut(_) => write!(f, "invalid previous transaction output"),
@@ -267,7 +270,7 @@ impl fmt::Display for PsbtInputError {
267270
}
268271
}
269272

270-
impl std::error::Error for PsbtInputError {
273+
impl std::error::Error for InternalPsbtInputError {
271274
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
272275
match self {
273276
Self::PrevTxOut(error) => Some(error),
@@ -279,18 +282,33 @@ impl std::error::Error for PsbtInputError {
279282
}
280283
}
281284

282-
impl From<PrevTxOutError> for PsbtInputError {
283-
fn from(value: PrevTxOutError) -> Self { PsbtInputError::PrevTxOut(value) }
285+
impl From<PrevTxOutError> for InternalPsbtInputError {
286+
fn from(value: PrevTxOutError) -> Self { InternalPsbtInputError::PrevTxOut(value) }
284287
}
285288

286-
impl From<AddressTypeError> for PsbtInputError {
289+
impl From<AddressTypeError> for InternalPsbtInputError {
287290
fn from(value: AddressTypeError) -> Self { Self::AddressType(value) }
288291
}
289292

293+
#[derive(Debug)]
294+
pub struct PsbtInputError(crate::psbt::InternalPsbtInputError);
295+
296+
impl From<crate::psbt::InternalPsbtInputError> for PsbtInputError {
297+
fn from(e: crate::psbt::InternalPsbtInputError) -> Self { PsbtInputError(e) }
298+
}
299+
300+
impl fmt::Display for PsbtInputError {
301+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.0) }
302+
}
303+
304+
impl std::error::Error for PsbtInputError {
305+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.0) }
306+
}
307+
290308
#[derive(Debug)]
291309
pub struct PsbtInputsError {
292310
index: usize,
293-
error: PsbtInputError,
311+
error: InternalPsbtInputError,
294312
}
295313

296314
impl fmt::Display for PsbtInputsError {
@@ -304,7 +322,7 @@ impl std::error::Error for PsbtInputsError {
304322
}
305323

306324
#[derive(Debug)]
307-
pub enum AddressTypeError {
325+
pub(crate) enum AddressTypeError {
308326
PrevTxOut(PrevTxOutError),
309327
InvalidScript(FromScriptError),
310328
UnknownAddressType,

payjoin/src/receive/error.rs

+7
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ impl From<crate::ohttp::OhttpEncapsulationError> for Error {
4545
fn from(e: crate::ohttp::OhttpEncapsulationError) -> Self { Error::Server(Box::new(e)) }
4646
}
4747

48+
#[derive(Debug)]
49+
pub struct PsbtInputError(crate::psbt::InternalPsbtInputError);
50+
51+
impl From<crate::psbt::InternalPsbtInputError> for PsbtInputError {
52+
fn from(e: crate::psbt::InternalPsbtInputError) -> Self { PsbtInputError(e) }
53+
}
54+
4855
/// Error that may occur when the request from sender is malformed.
4956
///
5057
/// This is currently opaque type because we aren't sure which variants will stay.

payjoin/src/receive/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ use error::{
4747
};
4848
use optional_parameters::Params;
4949

50-
use crate::psbt::{InternalInputPair, PsbtExt, PsbtInputError};
50+
pub use crate::psbt::PsbtInputError;
51+
use crate::psbt::{InternalInputPair, InternalPsbtInputError, PsbtExt};
5152

5253
pub trait Headers {
5354
fn get_header(&self, key: &str) -> Option<&str>;
@@ -66,9 +67,9 @@ impl InputPair {
6667
let input_pair = Self { txin, psbtin };
6768
let raw = InternalInputPair::from(&input_pair);
6869
raw.validate_utxo(true)?;
69-
let address_type = raw.address_type()?;
70+
let address_type = raw.address_type().map_err(InternalPsbtInputError::AddressType)?;
7071
if address_type == AddressType::P2sh && input_pair.psbtin.redeem_script.is_none() {
71-
return Err(PsbtInputError::NoRedeemScript);
72+
return Err(InternalPsbtInputError::NoRedeemScript.into());
7273
}
7374
Ok(input_pair)
7475
}

0 commit comments

Comments
 (0)