Skip to content

Commit

Permalink
feature(send): enable optional opt in to optimistic query param
Browse files Browse the repository at this point in the history
  • Loading branch information
0xBEEFCAF3 committed Jan 7, 2025
1 parent 83ba223 commit d78288f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
4 changes: 4 additions & 0 deletions payjoin/src/send/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ fn serialize_url(
fee_contribution: Option<OutputAmountAndIndex>,
min_fee_rate: FeeRate,
version: &str,
opt_in_to_optimistic_merge: bool,
) -> Result<Url, url::ParseError> {
let mut url = endpoint;
url.query_pairs_mut().append_pair("v", version);
Expand All @@ -455,6 +456,9 @@ fn serialize_url(
.append_pair("additionalfeeoutputindex", &index.to_string())
.append_pair("maxadditionalfeecontribution", &amount.to_sat().to_string());
}
if opt_in_to_optimistic_merge {
url.query_pairs_mut().append_pair("optimisticmerge", "1");
}
if min_fee_rate > FeeRate::ZERO {
// TODO serialize in rust-bitcoin <https://github.com/rust-bitcoin/rust-bitcoin/pull/1787/files#diff-c2ea40075e93ccd068673873166cfa3312ec7439d6bc5a4cbc03e972c7e045c4>
let float_fee_rate = min_fee_rate.to_sat_per_kwu() as f32 / 250.0_f32;
Expand Down
1 change: 1 addition & 0 deletions payjoin/src/send/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ impl Sender {
self.fee_contribution,
self.min_fee_rate,
"1", // payjoin version
false, // Opt into optimistic multiparty payjoin
)?;
let body = self.psbt.to_string().as_bytes().to_vec();
Ok((
Expand Down
9 changes: 9 additions & 0 deletions payjoin/src/send/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ impl<'a> SenderBuilder<'a> {
Ok(Sender {
v1: self.0.build_recommended(min_fee_rate)?,
reply_key: HpkeKeyPair::gen_keypair().0,
opt_in_to_optimistic_merge: false,
})
}

Expand Down Expand Up @@ -107,6 +108,7 @@ impl<'a> SenderBuilder<'a> {
min_fee_rate,
clamp_fee_contribution,
)?,
opt_in_to_optimistic_merge: false,
reply_key: HpkeKeyPair::gen_keypair().0,
})
}
Expand All @@ -122,6 +124,7 @@ impl<'a> SenderBuilder<'a> {
Ok(Sender {
v1: self.0.build_non_incentivizing(min_fee_rate)?,
reply_key: HpkeKeyPair::gen_keypair().0,
opt_in_to_optimistic_merge: false,
})
}
}
Expand All @@ -132,6 +135,8 @@ pub struct Sender {
v1: v1::Sender,
/// The secret key to decrypt the receiver's reply.
reply_key: HpkeSecretKey,
/// Allow for optimistic merge
opt_in_to_optimistic_merge: bool,
}

impl Sender {
Expand Down Expand Up @@ -164,6 +169,7 @@ impl Sender {
self.v1.disable_output_substitution,
self.v1.fee_contribution,
self.v1.min_fee_rate,
self.opt_in_to_optimistic_merge,
)?;
let hpke_ctx = HpkeContext::new(rs, &self.reply_key);
let body = encrypt_message_a(
Expand Down Expand Up @@ -210,6 +216,7 @@ fn serialize_v2_body(
disable_output_substitution: bool,
fee_contribution: Option<(bitcoin::Amount, usize)>,
min_feerate: FeeRate,
opt_in_to_optimistic_merge: bool,
) -> Result<Vec<u8>, CreateRequestError> {
// Grug say localhost base be discarded anyway. no big brain needed.
let placeholder_url = serialize_url(
Expand All @@ -218,6 +225,7 @@ fn serialize_v2_body(
fee_contribution,
min_feerate,
"2", // payjoin version
opt_in_to_optimistic_merge,
)
.map_err(InternalCreateRequestError::Url)?;
let query_params = placeholder_url.query().unwrap_or_default();
Expand Down Expand Up @@ -349,6 +357,7 @@ mod test {
payee: ScriptBuf::from(vec![0x00]),
},
reply_key: HpkeKeyPair::gen_keypair().0,
opt_in_to_optimistic_merge: false,
};
let serialized = serde_json::to_string(&req_ctx).unwrap();
let deserialized = serde_json::from_str(&serialized).unwrap();
Expand Down
4 changes: 3 additions & 1 deletion payjoin/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ mod integration {
use http::StatusCode;
use payjoin::send::v2::SenderBuilder;
use payjoin::receive::v2::{
MultiPartyProposal, PayjoinProposal, Receiver, UnMergedMultiPartyProposal,
PayjoinProposal, Receiver, UnMergedMultiPartyProposal,
UncheckedProposal,
};
use payjoin::{HpkeKeyPair, OhttpKeys, PjUri, UriExt};
Expand Down Expand Up @@ -409,6 +409,7 @@ mod integration {
.build_with_multiple_senders()?;
let (Request { url, body, content_type, .. }, send_post_ctx_1) =
sender_ctx_1.extract_v2(directory.to_owned())?;

let response = agent
.post(url.clone())
.header("Content-Type", content_type)
Expand All @@ -419,6 +420,7 @@ mod integration {
assert!(response.status().is_success());
let sender_get_ctx_1 = send_post_ctx_1
.process_response(&mut response.bytes().await?.to_vec().as_slice())?;

//**********************
// Inside Sender 2
// Sender 2 will POST a different psbt to the same subdir id
Expand Down

0 comments on commit d78288f

Please sign in to comment.