diff --git a/payjoin/src/receive/error.rs b/payjoin/src/receive/error.rs index 4783bbdb..d6271c7f 100644 --- a/payjoin/src/receive/error.rs +++ b/payjoin/src/receive/error.rs @@ -123,7 +123,7 @@ impl fmt::Display for RequestError { format!("Content length too large: {}.", length), ), InternalRequestError::SenderParams(e) => match e { - super::optional_parameters::Error::UnknownVersion => { + super::optional_parameters::Error::UnknownVersion { supported_versions } => { write!( f, r#"{{ @@ -131,8 +131,7 @@ impl fmt::Display for RequestError { "supported": "{}", "message": "This version of payjoin is not supported." }}"#, - serde_json::to_string(&super::optional_parameters::SUPPORTED_VERSIONS) - .map_err(|_| fmt::Error)? + serde_json::to_string(supported_versions).map_err(|_| fmt::Error)? ) } _ => write_error(f, "sender-params-error", e), diff --git a/payjoin/src/receive/optional_parameters.rs b/payjoin/src/receive/optional_parameters.rs index 2db96f29..dabcd2ee 100644 --- a/payjoin/src/receive/optional_parameters.rs +++ b/payjoin/src/receive/optional_parameters.rs @@ -4,11 +4,6 @@ use std::fmt; use bitcoin::FeeRate; use log::warn; -#[cfg(feature = "v2")] -pub(crate) const SUPPORTED_VERSIONS: [usize; 2] = [1, 2]; -#[cfg(not(feature = "v2"))] -pub(crate) const SUPPORTED_VERSIONS: [usize; 1] = [1]; - #[derive(Debug, Clone)] pub(crate) struct Params { // version @@ -33,8 +28,10 @@ impl Default for Params { } impl Params { - #[cfg(feature = "receive")] - pub fn from_query_pairs(pairs: I) -> Result + pub fn from_query_pairs( + pairs: I, + supported_versions: &'static [usize], + ) -> Result where I: Iterator, K: Borrow + Into, @@ -49,8 +46,8 @@ impl Params { match (key.borrow(), v.borrow()) { ("v", version) => params.v = match version.parse::() { - Ok(version) if SUPPORTED_VERSIONS.contains(&version) => version, - _ => return Err(Error::UnknownVersion), + Ok(version) if supported_versions.contains(&version) => version, + _ => return Err(Error::UnknownVersion { supported_versions }), }, ("additionalfeeoutputindex", index) => additional_fee_output_index = match index.parse::() { @@ -107,14 +104,14 @@ impl Params { #[derive(Debug)] pub(crate) enum Error { - UnknownVersion, + UnknownVersion { supported_versions: &'static [usize] }, FeeRate, } impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { - Error::UnknownVersion => write!(f, "unknown version"), + Error::UnknownVersion { .. } => write!(f, "unknown version"), Error::FeeRate => write!(f, "could not parse feerate"), } } diff --git a/payjoin/src/receive/v1.rs b/payjoin/src/receive/v1.rs index 5e9afa60..ec16412f 100644 --- a/payjoin/src/receive/v1.rs +++ b/payjoin/src/receive/v1.rs @@ -43,6 +43,8 @@ use super::{ }; use crate::psbt::PsbtExt; +const SUPPORTED_VERSIONS: &[usize] = &[1]; + pub trait Headers { fn get_header(&self, key: &str) -> Option<&str>; } @@ -104,7 +106,8 @@ impl UncheckedProposal { log::debug!("Received original psbt: {:?}", psbt); let pairs = url::form_urlencoded::parse(query.as_bytes()); - let params = Params::from_query_pairs(pairs).map_err(InternalRequestError::SenderParams)?; + let params = Params::from_query_pairs(pairs, SUPPORTED_VERSIONS) + .map_err(InternalRequestError::SenderParams)?; log::debug!("Received request with params: {:?}", params); // TODO check that params are valid for the request's Original PSBT diff --git a/payjoin/src/receive/v2/mod.rs b/payjoin/src/receive/v2/mod.rs index b520f48c..a8c8023a 100644 --- a/payjoin/src/receive/v2/mod.rs +++ b/payjoin/src/receive/v2/mod.rs @@ -23,6 +23,8 @@ use crate::Request; pub(crate) mod error; +const SUPPORTED_VERSIONS: &[usize] = &[1, 2]; + static TWENTY_FOUR_HOURS_DEFAULT_EXPIRY: Duration = Duration::from_secs(60 * 60 * 24); #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] @@ -165,8 +167,11 @@ impl Receiver { let unchecked_psbt = Psbt::from_str(base64).map_err(InternalRequestError::ParsePsbt)?; let psbt = unchecked_psbt.validate().map_err(InternalRequestError::InconsistentPsbt)?; log::debug!("Received original psbt: {:?}", psbt); - let mut params = Params::from_query_pairs(url::form_urlencoded::parse(query.as_bytes())) - .map_err(InternalRequestError::SenderParams)?; + let mut params = Params::from_query_pairs( + url::form_urlencoded::parse(query.as_bytes()), + SUPPORTED_VERSIONS, + ) + .map_err(InternalRequestError::SenderParams)?; // Output substitution must be disabled for V1 sessions in V2 contexts. //