Skip to content

Commit e395951

Browse files
Mshehu5spacebear21
authored andcommitted
Deduplicate payjoin URI creation logic
Create a helper function `create_pj_uri` in the v2 module that centralizes the logic for creating Payjoin URIs from a SessionContext. This eliminates code duplication between `SessionHistory::pj_uri` and `Receiver<WithContext>::pj_uri`. The helper function takes a SessionContext reference and an OutputSubstitution parameter, making it flexible for both use cases while maintaining the same behavior as the original implementations. This change: - Improves maintainability by centralizing URI creation logic - Reduces code duplication - Preserves the existing API and behavior
1 parent 2f3f46a commit e395951

File tree

2 files changed

+19
-23
lines changed

2 files changed

+19
-23
lines changed

payjoin/src/receive/v2/mod.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -390,14 +390,7 @@ impl Receiver<Initialized> {
390390

391391
/// Build a V2 Payjoin URI from the receiver's context
392392
pub fn pj_uri<'a>(&self) -> crate::PjUri<'a> {
393-
use crate::uri::{PayjoinExtras, UrlExt};
394-
let mut pj = subdir(&self.context.directory, &self.context.id()).clone();
395-
pj.set_receiver_pubkey(self.context.s.public_key().clone());
396-
pj.set_ohttp(self.context.ohttp_keys.clone());
397-
pj.set_exp(self.context.expiry);
398-
let extras =
399-
PayjoinExtras { endpoint: pj, output_substitution: OutputSubstitution::Enabled };
400-
bitcoin_uri::Uri::with_extras(self.context.address.clone(), extras)
393+
pj_uri(&self.context, OutputSubstitution::Enabled)
401394
}
402395

403396
pub(crate) fn apply_unchecked_from_payload(
@@ -908,6 +901,21 @@ fn subdir(directory: &Url, id: &ShortId) -> Url {
908901
url
909902
}
910903

904+
/// Gets the Payjoin URI from a session context
905+
pub(crate) fn pj_uri<'a>(
906+
session_context: &SessionContext,
907+
output_substitution: OutputSubstitution,
908+
) -> crate::PjUri<'a> {
909+
use crate::uri::{PayjoinExtras, UrlExt};
910+
let id = session_context.id();
911+
let mut pj = subdir(&session_context.directory, &id).clone();
912+
pj.set_receiver_pubkey(session_context.s.public_key().clone());
913+
pj.set_ohttp(session_context.ohttp_keys.clone());
914+
pj.set_exp(session_context.expiry);
915+
let extras = PayjoinExtras { endpoint: pj, output_substitution };
916+
bitcoin_uri::Uri::with_extras(session_context.address.clone(), extras)
917+
}
918+
911919
#[cfg(test)]
912920
pub mod test {
913921
use std::str::FromStr;

payjoin/src/receive/v2/session.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};
55
use super::{Receiver, ReceiverTypeState, SessionContext, UninitializedReceiver};
66
use crate::output_substitution::OutputSubstitution;
77
use crate::persist::SessionPersister;
8-
use crate::receive::v2::{extract_err_req, subdir, SessionError};
8+
use crate::receive::v2::{extract_err_req, SessionError};
99
use crate::receive::{v1, JsonReply};
1010
use crate::{ImplementationError, IntoUrl, PjUri, Request};
1111

@@ -82,20 +82,8 @@ impl SessionHistory {
8282
/// Receiver session Payjoin URI
8383
pub fn pj_uri<'a>(&self) -> Option<PjUri<'a>> {
8484
self.events.iter().find_map(|event| match event {
85-
SessionEvent::Created(session_context) => {
86-
// TODO this code was copied from ReceiverInitialized::pj_uri. Should be deduped
87-
use crate::uri::{PayjoinExtras, UrlExt};
88-
let id = session_context.id();
89-
let mut pj = subdir(&session_context.directory, &id).clone();
90-
pj.set_receiver_pubkey(session_context.s.public_key().clone());
91-
pj.set_ohttp(session_context.ohttp_keys.clone());
92-
pj.set_exp(session_context.expiry);
93-
let extras = PayjoinExtras {
94-
endpoint: pj,
95-
output_substitution: OutputSubstitution::Disabled,
96-
};
97-
Some(bitcoin_uri::Uri::with_extras(session_context.address.clone(), extras))
98-
}
85+
SessionEvent::Created(session_context) =>
86+
Some(crate::receive::v2::pj_uri(session_context, OutputSubstitution::Disabled)),
9987
_ => None,
10088
})
10189
}

0 commit comments

Comments
 (0)