From 4ea00db3265041facd3e2654d1338154eabb5a39 Mon Sep 17 00:00:00 2001 From: Shing Him Ng Date: Fri, 13 Dec 2024 08:55:46 -0600 Subject: [PATCH] Move url_ext-specific errors to url_ext.rs --- payjoin/src/send/error.rs | 2 +- payjoin/src/send/mod.rs | 2 +- payjoin/src/uri/error.rs | 80 ------------------------------------- payjoin/src/uri/url_ext.rs | 82 +++++++++++++++++++++++++++++++++++++- 4 files changed, 83 insertions(+), 83 deletions(-) diff --git a/payjoin/src/send/error.rs b/payjoin/src/send/error.rs index 78d20765..68c3f420 100644 --- a/payjoin/src/send/error.rs +++ b/payjoin/src/send/error.rs @@ -5,7 +5,7 @@ use bitcoin::transaction::Version; use bitcoin::{AddressType, Sequence}; #[cfg(feature = "v2")] -use crate::uri::error::ParseReceiverPubkeyParamError; +use crate::uri::url_ext::ParseReceiverPubkeyParamError; /// Error that may occur when the response from receiver is malformed. /// diff --git a/payjoin/src/send/mod.rs b/payjoin/src/send/mod.rs index e3d3ed34..6e2eb4a7 100644 --- a/payjoin/src/send/mod.rs +++ b/payjoin/src/send/mod.rs @@ -331,7 +331,7 @@ impl Sender { #[cfg(feature = "v2")] fn extract_rs_pubkey( &self, - ) -> Result { + ) -> Result { use crate::uri::UrlExt; self.endpoint.receiver_pubkey() } diff --git a/payjoin/src/uri/error.rs b/payjoin/src/uri/error.rs index 20e4d999..f1f6ed9c 100644 --- a/payjoin/src/uri/error.rs +++ b/payjoin/src/uri/error.rs @@ -11,86 +11,6 @@ pub(crate) enum InternalPjParseError { UnsecureEndpoint, } -#[cfg(feature = "v2")] -#[derive(Debug)] -pub(crate) enum ParseOhttpKeysParamError { - MissingOhttpKeys, - InvalidOhttpKeys(crate::ohttp::ParseOhttpKeysError), -} - -#[cfg(feature = "v2")] -#[derive(Debug)] -pub(crate) enum ParseExpParamError { - MissingExp, - InvalidHrp(bitcoin::bech32::Hrp), - DecodeBech32(bitcoin::bech32::primitives::decode::CheckedHrpstringError), - InvalidExp(bitcoin::consensus::encode::Error), -} - -#[cfg(feature = "v2")] -impl std::fmt::Display for ParseExpParamError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - use ParseExpParamError::*; - - match &self { - MissingExp => write!(f, "exp is missing"), - InvalidHrp(h) => write!(f, "incorrect hrp for exp: {}", h), - DecodeBech32(d) => write!(f, "exp is not valid bech32: {}", d), - InvalidExp(i) => write!(f, "exp param does not contain a bitcoin consensus encoded u32: {}", i), - } - } -} - -#[cfg(feature = "v2")] -impl std::fmt::Display for ParseOhttpKeysParamError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - use ParseOhttpKeysParamError::*; - - match &self { - MissingOhttpKeys => write!(f, "ohttp keys are missing"), - InvalidOhttpKeys(o) => write!(f, "invalid ohttp keys: {}", o), - } - } -} - -#[cfg(feature = "v2")] -#[derive(Debug)] -pub(crate) enum ParseReceiverPubkeyParamError { - MissingPubkey, - InvalidHrp(bitcoin::bech32::Hrp), - DecodeBech32(bitcoin::bech32::primitives::decode::CheckedHrpstringError), - InvalidPubkey(crate::hpke::HpkeError), -} - -#[cfg(feature = "v2")] -impl std::fmt::Display for ParseReceiverPubkeyParamError { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - use ParseReceiverPubkeyParamError::*; - - match &self { - MissingPubkey => write!(f, "receiver public key is missing"), - InvalidHrp(h) => write!(f, "incorrect hrp for receiver key: {}", h), - DecodeBech32(e) => write!(f, "receiver public is not valid base64: {}", e), - InvalidPubkey(e) => - write!(f, "receiver public key does not represent a valid pubkey: {}", e), - } - } -} - -#[cfg(feature = "v2")] -impl std::error::Error for ParseReceiverPubkeyParamError { - fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { - use ParseReceiverPubkeyParamError::*; - - match &self { - MissingPubkey => None, - InvalidHrp(_) => None, - DecodeBech32(error) => Some(error), - InvalidPubkey(error) => Some(error), - } - } -} - impl From for PjParseError { fn from(value: InternalPjParseError) -> Self { PjParseError(value) } } diff --git a/payjoin/src/uri/url_ext.rs b/payjoin/src/uri/url_ext.rs index 1551fc90..b5047ee5 100644 --- a/payjoin/src/uri/url_ext.rs +++ b/payjoin/src/uri/url_ext.rs @@ -5,7 +5,6 @@ use bitcoin::consensus::encode::Decodable; use bitcoin::consensus::Encodable; use url::Url; -use super::error::{ParseExpParamError, ParseOhttpKeysParamError, ParseReceiverPubkeyParamError}; use crate::hpke::HpkePublicKey; use crate::ohttp::OhttpKeys; @@ -131,6 +130,87 @@ fn set_param(url: &mut Url, prefix: &str, param: &str) { url.set_fragment(if fragment.is_empty() { None } else { Some(&fragment) }); } +#[cfg(feature = "v2")] +#[derive(Debug)] +pub(crate) enum ParseOhttpKeysParamError { + MissingOhttpKeys, + InvalidOhttpKeys(crate::ohttp::ParseOhttpKeysError), +} + +#[cfg(feature = "v2")] +impl std::fmt::Display for ParseOhttpKeysParamError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + use ParseOhttpKeysParamError::*; + + match &self { + MissingOhttpKeys => write!(f, "ohttp keys are missing"), + InvalidOhttpKeys(o) => write!(f, "invalid ohttp keys: {}", o), + } + } +} + +#[cfg(feature = "v2")] +#[derive(Debug)] +pub(crate) enum ParseExpParamError { + MissingExp, + InvalidHrp(bitcoin::bech32::Hrp), + DecodeBech32(bitcoin::bech32::primitives::decode::CheckedHrpstringError), + InvalidExp(bitcoin::consensus::encode::Error), +} + +#[cfg(feature = "v2")] +impl std::fmt::Display for ParseExpParamError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + use ParseExpParamError::*; + + match &self { + MissingExp => write!(f, "exp is missing"), + InvalidHrp(h) => write!(f, "incorrect hrp for exp: {}", h), + DecodeBech32(d) => write!(f, "exp is not valid bech32: {}", d), + InvalidExp(i) => + write!(f, "exp param does not contain a bitcoin consensus encoded u32: {}", i), + } + } +} + +#[cfg(feature = "v2")] +#[derive(Debug)] +pub(crate) enum ParseReceiverPubkeyParamError { + MissingPubkey, + InvalidHrp(bitcoin::bech32::Hrp), + DecodeBech32(bitcoin::bech32::primitives::decode::CheckedHrpstringError), + InvalidPubkey(crate::hpke::HpkeError), +} + +#[cfg(feature = "v2")] +impl std::fmt::Display for ParseReceiverPubkeyParamError { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + use ParseReceiverPubkeyParamError::*; + + match &self { + MissingPubkey => write!(f, "receiver public key is missing"), + InvalidHrp(h) => write!(f, "incorrect hrp for receiver key: {}", h), + DecodeBech32(e) => write!(f, "receiver public is not valid base64: {}", e), + InvalidPubkey(e) => + write!(f, "receiver public key does not represent a valid pubkey: {}", e), + } + } +} + +#[cfg(feature = "v2")] +impl std::error::Error for ParseReceiverPubkeyParamError { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + use ParseReceiverPubkeyParamError::*; + + match &self { + MissingPubkey => None, + InvalidHrp(_) => None, + DecodeBech32(error) => Some(error), + InvalidPubkey(error) => Some(error), + } + } +} + #[cfg(test)] mod tests { use super::*;