Skip to content

Commit

Permalink
Set version header correctly for v2 senders
Browse files Browse the repository at this point in the history
Closes payjoin#311. Set `v=2` in HTTP query string parameters when serializing
v2 sender requests.
  • Loading branch information
spacebear21 committed Jul 11, 2024
1 parent c036ec1 commit 4f22f0b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion payjoin/src/receive/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ impl ActiveSession {
/// transaction with extract_tx_to_schedule_broadcast() and schedule, followed by checking
/// that the transaction can be broadcast with check_broadcast_suitability. Otherwise it is safe to
/// call assume_interactive_receive to proceed with validation.
#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct UncheckedProposal {
inner: super::UncheckedProposal,
context: SessionContext,
Expand Down
13 changes: 8 additions & 5 deletions payjoin/src/send/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,11 @@ impl RequestContext {
/// Extract serialized V1 Request and Context froma Payjoin Proposal
pub fn extract_v1(self) -> Result<(Request, ContextV1), CreateRequestError> {
let url = serialize_url(
self.endpoint.into(),
self.endpoint.as_str(),
self.disable_output_substitution,
self.fee_contribution,
self.min_fee_rate,
"1", // payjoin version
)
.map_err(InternalCreateRequestError::Url)?;
let body = self.psbt.to_string().as_bytes().to_vec();
Expand Down Expand Up @@ -974,10 +975,11 @@ fn serialize_v2_body(
) -> Result<Vec<u8>, CreateRequestError> {
// Grug say localhost base be discarded anyway. no big brain needed.
let placeholder_url = serialize_url(
"http:/localhost".to_string(),
"http://localhost",
disable_output_substitution,
fee_contribution,
min_feerate,
"2", // payjoin version
)
.map_err(InternalCreateRequestError::Url)?;
let query_params = placeholder_url.query().unwrap_or_default();
Expand All @@ -986,13 +988,14 @@ fn serialize_v2_body(
}

fn serialize_url(
endpoint: String,
endpoint: &str,
disable_output_substitution: bool,
fee_contribution: Option<(bitcoin::Amount, usize)>,
min_fee_rate: FeeRate,
version: &str,
) -> Result<Url, url::ParseError> {
let mut url = Url::parse(&endpoint)?;
url.query_pairs_mut().append_pair("v", "1");
let mut url = Url::parse(endpoint)?;
url.query_pairs_mut().append_pair("v", version);
if disable_output_substitution {
url.query_pairs_mut().append_pair("disableoutputsubstitution", "1");
}
Expand Down
3 changes: 3 additions & 0 deletions payjoin/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ mod integration {
)
.unwrap();
let proposal = handle_proposal(proposal, receiver);
assert!(!proposal.is_output_substitution_disabled());
let psbt = proposal.psbt();
debug!("Receiver's Payjoin proposal PSBT: {:#?}", &psbt);
psbt.to_string()
Expand Down Expand Up @@ -436,6 +437,7 @@ mod integration {
let proposal =
session.process_res(response.bytes().await?.to_vec().as_slice(), ctx)?.unwrap();
let mut payjoin_proposal = handle_directory_proposal(receiver, proposal);
assert!(!payjoin_proposal.is_output_substitution_disabled());
let (req, ctx) = payjoin_proposal.extract_v2_req()?;
let response = agent.post(req.url).body(req.body).send().await?;
let res = response.bytes().await?.to_vec();
Expand Down Expand Up @@ -546,6 +548,7 @@ mod integration {
};
let proposal = session.process_res(response.as_slice(), ctx).unwrap().unwrap();
let mut payjoin_proposal = handle_directory_proposal(receiver, proposal);
assert!(payjoin_proposal.is_output_substitution_disabled());
// Respond with payjoin psbt within the time window the sender is willing to wait
// this response would be returned as http response to the sender
let (req, ctx) = payjoin_proposal.extract_v2_req().unwrap();
Expand Down

0 comments on commit 4f22f0b

Please sign in to comment.