From 81d8b9e10f18bd5292d5cfa6511bee1a2dcea63d Mon Sep 17 00:00:00 2001 From: DanGould Date: Mon, 13 Nov 2023 19:00:29 -0500 Subject: [PATCH] Relay v1 parameters --- payjoin-relay/src/main.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/payjoin-relay/src/main.rs b/payjoin-relay/src/main.rs index 4a0602a0..4f506bdf 100644 --- a/payjoin-relay/src/main.rs +++ b/payjoin-relay/src/main.rs @@ -16,6 +16,8 @@ const DEFAULT_RELAY_PORT: &str = "8080"; const DEFAULT_DB_HOST: &str = "localhost:5432"; const DEFAULT_TIMEOUT_SECS: u64 = 30; const MAX_BUFFER_SIZE: usize = 65536; +const V1_REJECT_RES_JSON: &str = + r#"{{"errorCode": "original-psbt-rejected ", "message": "Body is not a string"}}"#; const V1_UNAVAILABLE_RES_JSON: &str = r#"{{"errorCode": "unavailable", "message": "V2 receiver offline. V1 sends require synchronous communications."}}"#; mod db; @@ -86,6 +88,7 @@ async fn handle_ohttp_gateway( ohttp: Arc>, ) -> Result> { let path = req.uri().path().to_string(); + let query = req.uri().query().unwrap_or_default().to_string(); let (parts, body) = req.into_parts(); let path_segments: Vec<&str> = path.split('/').collect(); @@ -94,7 +97,7 @@ async fn handle_ohttp_gateway( (Method::POST, ["", ""]) => handle_ohttp(body, pool, ohttp).await, (Method::GET, ["", "ohttp-config"]) => Ok(get_ohttp_config(ohttp_config(&ohttp).await?).await), - (Method::POST, ["", id]) => post_fallback_v1(id, body, pool).await, + (Method::POST, ["", id]) => post_fallback_v1(id, query, body, pool).await, _ => Ok(not_found()), } .unwrap_or_else(|e| e.to_response()); @@ -214,6 +217,7 @@ async fn post_enroll(body: Body) -> Result, HandlerError> { async fn post_fallback_v1( id: &str, + query: String, body: Body, pool: DbPool, ) -> Result, HandlerError> { @@ -221,7 +225,21 @@ async fn post_fallback_v1( let none_response = Response::builder() .status(StatusCode::SERVICE_UNAVAILABLE) .body(Body::from(V1_UNAVAILABLE_RES_JSON))?; - post_fallback(id, body, pool, none_response).await + let bad_request_body_res = + Response::builder().status(StatusCode::BAD_REQUEST).body(Body::from(V1_REJECT_RES_JSON))?; + + let body_bytes = match hyper::body::to_bytes(body).await { + Ok(bytes) => bytes.to_vec(), + Err(_) => return Ok(bad_request_body_res), + }; + + let body_str = match String::from_utf8(body_bytes) { + Ok(body_str) => body_str, + Err(_) => return Ok(bad_request_body_res), + }; + + let v2_compat_body = Body::from(format!("{}\n{}", body_str, query)); + post_fallback(id, v2_compat_body, pool, none_response).await } async fn post_fallback_v2(