Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose Sender::extract_v2 for bindings #382

Merged
merged 3 commits into from
Nov 11, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions payjoin/src/send/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,10 @@ impl Sender {
}

match self.extract_rs_pubkey() {
Ok(rs) => self.extract_v2(ohttp_relay, rs),
Ok(_rs) => {
let (req, context_v2) = self.extract_v2(ohttp_relay)?;
Ok((req, Context::V2(context_v2)))
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it even makes sense to have this match extract_highest_version since it still requires a match on the resulting Context to handle both processing v1 and v2

It may be simpler to just have the downstream implementor switch on whether or not extract_v2 errors, then try extract_v1 (if it's a version error), and otherwise return an error.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that the try/catch pattern on extract_v2 seems cleaner.

Err(e) => {
log::warn!("Failed to extract `rs` pubkey, falling back to v1: {}", e);
let (req, context_v1) = self.extract_v1()?;
Expand All @@ -302,12 +305,12 @@ impl Sender {
/// This method requires the `rs` pubkey to be extracted from the endpoint
/// and has no fallback to v1.
#[cfg(feature = "v2")]
fn extract_v2(
&mut self,
pub fn extract_v2(
&self,
ohttp_relay: Url,
rs: HpkePublicKey,
) -> Result<(Request, Context), CreateRequestError> {
) -> Result<(Request, V2PostContext), CreateRequestError> {
use crate::uri::UrlExt;
let rs = self.extract_rs_pubkey()?;
let url = self.endpoint.clone();
let body = serialize_v2_body(
&self.psbt,
Expand All @@ -329,7 +332,7 @@ impl Sender {
log::debug!("ohttp_relay_url: {:?}", ohttp_relay);
Ok((
Request::new_v2(ohttp_relay, body),
Context::V2(V2PostContext {
V2PostContext {
endpoint: self.endpoint.clone(),
psbt_ctx: PsbtContext {
original_psbt: self.psbt.clone(),
Expand All @@ -341,7 +344,7 @@ impl Sender {
},
hpke_ctx,
ohttp_ctx,
}),
},
))
}

Expand Down
Loading