Skip to content

Commit

Permalink
Relay v1 parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
DanGould committed Nov 30, 2023
1 parent 82d361c commit 81d8b9e
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions payjoin-relay/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -86,6 +88,7 @@ async fn handle_ohttp_gateway(
ohttp: Arc<Mutex<ohttp::Server>>,
) -> Result<Response<Body>> {
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();
Expand All @@ -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());
Expand Down Expand Up @@ -214,14 +217,29 @@ async fn post_enroll(body: Body) -> Result<Response<Body>, HandlerError> {

async fn post_fallback_v1(
id: &str,
query: String,
body: Body,
pool: DbPool,
) -> Result<Response<Body>, HandlerError> {
trace!("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(
Expand Down

0 comments on commit 81d8b9e

Please sign in to comment.