Skip to content

Commit

Permalink
Send v1 to v1 receivers if v2 unsupported
Browse files Browse the repository at this point in the history
Fix #144
  • Loading branch information
DanGould committed Jul 30, 2024
1 parent 8d4196f commit b0170e4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
25 changes: 24 additions & 1 deletion payjoin-cli/src/app/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use payjoin::bitcoin::consensus::encode::serialize_hex;
use payjoin::bitcoin::psbt::Psbt;
use payjoin::bitcoin::Amount;
use payjoin::receive::v2::ActiveSession;
use payjoin::send::RequestContext;
use payjoin::send::{RequestContext, ResponseError};
use payjoin::{bitcoin, Error, Uri};
use tokio::signal;
use tokio::sync::watch;
Expand Down Expand Up @@ -213,6 +213,7 @@ impl App {
}

async fn long_poll_post(&self, req_ctx: &mut payjoin::send::RequestContext) -> Result<Psbt> {
use payjoin::send::WellKnownError;
loop {
let (req, ctx) = req_ctx.extract_v2(self.config.ohttp_relay.clone())?;
println!("Polling send request...");
Expand All @@ -232,6 +233,28 @@ impl App {
println!("No response yet.");
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
}
Err(ResponseError::WellKnown(WellKnownError::VersionUnsupported {
message,
supported,
})) => {
if supported.contains(&1) {
let (req, ctx) = req_ctx.clone().extract_v1()?;
println!("Polling send request...");
let http = http_agent()?;
let response = http
.post(req.url)
.header("Content-Type", "text/plain")
.body(req.body)
.send()
.await
.map_err(map_reqwest_err)?;

return Ok(
ctx.process_response(&mut response.bytes().await?.to_vec().as_slice())?
);
}
return Err(anyhow!("Response error: {}", message));
}
Err(re) => {
println!("{}", re);
log::debug!("{:?}", re);
Expand Down
3 changes: 2 additions & 1 deletion payjoin/src/send/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use bitcoin::secp256k1::rand;
#[cfg(feature = "v2")]
use bitcoin::secp256k1::PublicKey;
use bitcoin::{FeeRate, Script, ScriptBuf, Sequence, TxOut, Weight};
pub use error::{CreateRequestError, ResponseError, ValidationError};
pub use error::{CreateRequestError, ResponseError, ValidationError, WellKnownError};
pub(crate) use error::{InternalCreateRequestError, InternalValidationError};
#[cfg(feature = "v2")]
use serde::{
Expand Down Expand Up @@ -557,6 +557,7 @@ impl ContextV2 {
let mut body = match response.status() {
http::StatusCode::OK => response.body().to_vec(),
http::StatusCode::ACCEPTED => return Ok(None),
http::StatusCode::BAD_REQUEST => return Ok(None), // TODO return properl
_ => return Err(InternalValidationError::UnexpectedStatusCode)?,
};
let psbt = crate::v2::decrypt_message_b(&mut body, self.e)
Expand Down

0 comments on commit b0170e4

Please sign in to comment.