Skip to content

Commit

Permalink
Abstract common Receiver subdir and id functions
Browse files Browse the repository at this point in the history
Only `Receiver.id()` needs to be public.
Subdir can be a private pure function.
  • Loading branch information
DanGould committed Jan 13, 2025
1 parent f2c8b94 commit 22a5281
Showing 1 changed file with 21 additions and 35 deletions.
56 changes: 21 additions & 35 deletions payjoin/src/receive/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl Receiver {
([u8; crate::ohttp::ENCAPSULATED_MESSAGE_BYTES], ohttp::ClientResponse),
OhttpEncapsulationError,
> {
let fallback_target = self.subdir();
let fallback_target = subdir(&self.context.directory, &self.id());
ohttp_encapsulate(&mut self.context.ohttp_keys, "GET", fallback_target.as_str(), None)
}

Expand Down Expand Up @@ -193,30 +193,16 @@ impl Receiver {
/// Build a V2 Payjoin URI from the receiver's context
pub fn pj_uri<'a>(&self) -> crate::PjUri<'a> {
use crate::uri::{PayjoinExtras, UrlExt};
let mut pj = self.subdir().clone();
let mut pj = subdir(&self.context.directory, &self.id()).clone();
pj.set_receiver_pubkey(self.context.s.public_key().clone());
pj.set_ohttp(self.context.ohttp_keys.clone());
pj.set_exp(self.context.expiry);
let extras = PayjoinExtras { endpoint: pj, disable_output_substitution: false };
bitcoin_uri::Uri::with_extras(self.context.address.clone(), extras)
}

/// The subdirectory for this Payjoin receiver session.
/// It consists of a directory URL and the session ShortID in the path.
pub fn subdir(&self) -> Url {
let mut url = self.context.directory.clone();
{
let mut path_segments =
url.path_segments_mut().expect("Payjoin Directory URL cannot be a base");
path_segments.push(&self.id().to_string());
}
url
}

/// The per-session identifier
pub fn id(&self) -> ShortId {
sha256::Hash::hash(&self.context.s.public_key().to_compressed_bytes()).into()
}
pub fn id(&self) -> ShortId { id(&self.context.s) }
}

/// The sender's original PSBT and optional parameters
Expand Down Expand Up @@ -278,7 +264,7 @@ impl UncheckedProposal {
err: Error,
ohttp_relay: &Url,
) -> Result<(Request, ohttp::ClientResponse), SessionError> {
let subdir = self.subdir();
let subdir = subdir(&self.context.directory, &id(&self.context.s));
let (body, ohttp_ctx) = ohttp_encapsulate(
&mut self.context.ohttp_keys,
"POST",
Expand Down Expand Up @@ -311,23 +297,6 @@ impl UncheckedProposal {
))),
}
}

/// The subdirectory for this Payjoin receiver session.
/// It consists of a directory URL and the session ShortID in the path.
pub fn subdir(&self) -> Url {
let mut url = self.context.directory.clone();
{
let mut path_segments =
url.path_segments_mut().expect("Payjoin Directory URL cannot be a base");
path_segments.push(&self.id().to_string());
}
url
}

/// The per-session identifier
pub fn id(&self) -> ShortId {
sha256::Hash::hash(&self.context.s.public_key().to_compressed_bytes()).into()
}
}

/// Typestate to validate that the Original PSBT has no receiver-owned inputs.
Expand Down Expand Up @@ -596,6 +565,23 @@ impl PayjoinProposal {
}
}

/// The subdirectory for this Payjoin receiver session.
/// It consists of a directory URL and the session ShortID in the path.
fn subdir(directory: &Url, id: &ShortId) -> Url {
let mut url = directory.clone();
{
let mut path_segments =
url.path_segments_mut().expect("Payjoin Directory URL cannot be a base");
path_segments.push(&id.to_string());
}
url
}

/// The per-session identifier
fn id(s: &HpkeKeyPair) -> ShortId {
sha256::Hash::hash(&s.public_key().to_compressed_bytes()).into()
}

#[cfg(test)]
mod test {
use super::*;
Expand Down

0 comments on commit 22a5281

Please sign in to comment.