Skip to content

Commit

Permalink
Make CreateRequestError v2-only
Browse files Browse the repository at this point in the history
Its variants only apply to extracting v2 requests.
  • Loading branch information
DanGould committed Jan 2, 2025
1 parent 30c2a07 commit d15d695
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 22 deletions.
20 changes: 5 additions & 15 deletions payjoin/src/send/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,64 +90,54 @@ impl std::error::Error for BuildSenderError {
/// `unwrap()`ing it is thus considered OK in Rust but you may achieve nicer message by displaying
/// it.
#[derive(Debug)]
#[cfg(feature = "v2")]
pub struct CreateRequestError(InternalCreateRequestError);

#[derive(Debug)]
#[cfg(feature = "v2")]
pub(crate) enum InternalCreateRequestError {
Url(url::ParseError),
#[cfg(feature = "v2")]
Hpke(crate::hpke::HpkeError),
#[cfg(feature = "v2")]
OhttpEncapsulation(crate::ohttp::OhttpEncapsulationError),
#[cfg(feature = "v2")]
ParseReceiverPubkey(ParseReceiverPubkeyParamError),
#[cfg(feature = "v2")]
MissingOhttpConfig,
#[cfg(feature = "v2")]
Expired(std::time::SystemTime),
}

#[cfg(feature = "v2")]
impl fmt::Display for CreateRequestError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use InternalCreateRequestError::*;

match &self.0 {
Url(e) => write!(f, "cannot parse url: {:#?}", e),
#[cfg(feature = "v2")]
Hpke(e) => write!(f, "v2 error: {}", e),
#[cfg(feature = "v2")]
OhttpEncapsulation(e) => write!(f, "v2 error: {}", e),
#[cfg(feature = "v2")]
ParseReceiverPubkey(e) => write!(f, "cannot parse receiver public key: {}", e),
#[cfg(feature = "v2")]
MissingOhttpConfig =>
write!(f, "no ohttp configuration with which to make a v2 request available"),
#[cfg(feature = "v2")]
Expired(expiry) => write!(f, "session expired at {:?}", expiry),
}
}
}

#[cfg(feature = "v2")]
impl std::error::Error for CreateRequestError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use InternalCreateRequestError::*;

match &self.0 {
Url(error) => Some(error),
#[cfg(feature = "v2")]
Hpke(error) => Some(error),
#[cfg(feature = "v2")]
OhttpEncapsulation(error) => Some(error),
#[cfg(feature = "v2")]
ParseReceiverPubkey(error) => Some(error),
#[cfg(feature = "v2")]
MissingOhttpConfig => None,
#[cfg(feature = "v2")]
Expired(_) => None,
}
}
}

#[cfg(feature = "v2")]
impl From<InternalCreateRequestError> for CreateRequestError {
fn from(value: InternalCreateRequestError) -> Self { CreateRequestError(value) }
}
Expand Down
15 changes: 8 additions & 7 deletions payjoin/src/send/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ use std::str::FromStr;
use bitcoin::hashes::{sha256, Hash};
use bitcoin::psbt::Psbt;
use bitcoin::{Amount, FeeRate, Script, ScriptBuf, TxOut, Weight};
pub use error::{BuildSenderError, CreateRequestError, ResponseError, ValidationError};
pub(crate) use error::{
InternalBuildSenderError, InternalCreateRequestError, InternalValidationError,
};
#[cfg(feature = "v2")]
pub use error::CreateRequestError;
#[cfg(feature = "v2")]
pub(crate) use error::InternalCreateRequestError;
pub use error::{BuildSenderError, ResponseError, ValidationError};
pub(crate) use error::{InternalBuildSenderError, InternalValidationError};
#[cfg(feature = "v2")]
use serde::{Deserialize, Serialize};
use url::Url;
Expand Down Expand Up @@ -255,15 +257,14 @@ pub struct Sender {

impl Sender {
/// Extract serialized V1 Request and Context from a Payjoin Proposal
pub fn extract_v1(&self) -> Result<(Request, V1Context), CreateRequestError> {
pub fn extract_v1(&self) -> Result<(Request, V1Context), url::ParseError> {
let url = serialize_url(
self.endpoint.clone(),
self.disable_output_substitution,
self.fee_contribution,
self.min_fee_rate,
"1", // payjoin version
)
.map_err(InternalCreateRequestError::Url)?;
)?;
let body = self.psbt.to_string().as_bytes().to_vec();
Ok((
Request::new_v1(url, body),
Expand Down

0 comments on commit d15d695

Please sign in to comment.