Skip to content

Commit

Permalink
Rename ParseReceiverPubkeyError to ParseReceiverPubkeyParamError
Browse files Browse the repository at this point in the history
This change will differentiate between parsing a receiver pubkey in the
context of a URL and other formats
  • Loading branch information
shinghim committed Dec 5, 2024
1 parent fe6ecad commit c9509fd
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
8 changes: 4 additions & 4 deletions payjoin/src/send/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bitcoin::transaction::Version;
use bitcoin::{AddressType, Sequence};

#[cfg(feature = "v2")]
use crate::uri::error::ParseReceiverPubkeyError;
use crate::uri::error::ParseReceiverPubkeyParamError;

/// Error that may occur when the response from receiver is malformed.
///
Expand Down Expand Up @@ -203,7 +203,7 @@ pub(crate) enum InternalCreateRequestError {
#[cfg(feature = "v2")]
OhttpEncapsulation(crate::ohttp::OhttpEncapsulationError),
#[cfg(feature = "v2")]
ParseReceiverPubkey(ParseReceiverPubkeyError),
ParseReceiverPubkey(ParseReceiverPubkeyParamError),
#[cfg(feature = "v2")]
MissingOhttpConfig,
#[cfg(feature = "v2")]
Expand Down Expand Up @@ -287,8 +287,8 @@ impl From<crate::psbt::AddressTypeError> for CreateRequestError {
}

#[cfg(feature = "v2")]
impl From<ParseReceiverPubkeyError> for CreateRequestError {
fn from(value: ParseReceiverPubkeyError) -> Self {
impl From<ParseReceiverPubkeyParamError> for CreateRequestError {
fn from(value: ParseReceiverPubkeyParamError) -> Self {
CreateRequestError(InternalCreateRequestError::ParseReceiverPubkey(value))
}
}
Expand Down
2 changes: 1 addition & 1 deletion payjoin/src/send/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ impl Sender {
#[cfg(feature = "v2")]
fn extract_rs_pubkey(
&self,
) -> Result<HpkePublicKey, crate::uri::error::ParseReceiverPubkeyError> {
) -> Result<HpkePublicKey, crate::uri::error::ParseReceiverPubkeyParamError> {
use crate::uri::UrlExt;
self.endpoint.receiver_pubkey()
}
Expand Down
10 changes: 5 additions & 5 deletions payjoin/src/uri/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ impl std::fmt::Display for ParseOhttpKeysParamError {

#[cfg(feature = "v2")]
#[derive(Debug)]
pub(crate) enum ParseReceiverPubkeyError {
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 ParseReceiverPubkeyError {
impl std::fmt::Display for ParseReceiverPubkeyParamError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
use ParseReceiverPubkeyError::*;
use ParseReceiverPubkeyParamError::*;

match &self {
MissingPubkey => write!(f, "receiver public key is missing"),
Expand All @@ -55,9 +55,9 @@ impl std::fmt::Display for ParseReceiverPubkeyError {
}

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

match &self {
MissingPubkey => None,
Expand Down
14 changes: 7 additions & 7 deletions payjoin/src/uri/url_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ use bitcoin::consensus::encode::Decodable;
use bitcoin::consensus::Encodable;
use url::Url;

use super::error::{ParseOhttpKeysParamError, ParseReceiverPubkeyError};
use super::error::{ParseOhttpKeysParamError, ParseReceiverPubkeyParamError};
use crate::hpke::HpkePublicKey;
use crate::ohttp::OhttpKeys;

/// Parse and set fragment parameters from `&pj=` URI parameter URLs
pub(crate) trait UrlExt {
fn receiver_pubkey(&self) -> Result<HpkePublicKey, ParseReceiverPubkeyError>;
fn receiver_pubkey(&self) -> Result<HpkePublicKey, ParseReceiverPubkeyParamError>;
fn set_receiver_pubkey(&mut self, exp: HpkePublicKey);
fn ohttp(&self) -> Result<OhttpKeys, ParseOhttpKeysParamError>;
fn set_ohttp(&mut self, ohttp: OhttpKeys);
Expand All @@ -21,20 +21,20 @@ pub(crate) trait UrlExt {

impl UrlExt for Url {
/// Retrieve the receiver's public key from the URL fragment
fn receiver_pubkey(&self) -> Result<HpkePublicKey, ParseReceiverPubkeyError> {
fn receiver_pubkey(&self) -> Result<HpkePublicKey, ParseReceiverPubkeyParamError> {
let value = get_param(self, "RK1", |v| Some(v.to_owned()))
.ok_or(ParseReceiverPubkeyError::MissingPubkey)?;
.ok_or(ParseReceiverPubkeyParamError::MissingPubkey)?;

let (hrp, bytes) = crate::bech32::nochecksum::decode(&value)
.map_err(ParseReceiverPubkeyError::DecodeBech32)?;
.map_err(ParseReceiverPubkeyParamError::DecodeBech32)?;

let rk_hrp: Hrp = Hrp::parse("RK").unwrap();
if hrp != rk_hrp {
return Err(ParseReceiverPubkeyError::InvalidHrp(hrp));
return Err(ParseReceiverPubkeyParamError::InvalidHrp(hrp));
}

HpkePublicKey::from_compressed_bytes(&bytes[..])
.map_err(ParseReceiverPubkeyError::InvalidPubkey)
.map_err(ParseReceiverPubkeyParamError::InvalidPubkey)
}

/// Set the receiver's public key in the URL fragment
Expand Down

0 comments on commit c9509fd

Please sign in to comment.