@@ -37,6 +37,8 @@ const V1_MAX_BUFFER_SIZE: usize = 65536;
3737const V1_REJECT_RES_JSON : & str =
3838 r#"{{"errorCode": "original-psbt-rejected ", "message": "Body is not a string"}}"# ;
3939const V1_UNAVAILABLE_RES_JSON : & str = r#"{{"errorCode": "unavailable", "message": "V2 receiver offline. V1 sends require synchronous communications."}}"# ;
40+ const V1_VERSION_UNSUPPORTED_RES_JSON : & str =
41+ r#"{"errorCode": "version-unsupported", "supported": [2], "message": "V1 is not supported"}"# ;
4042
4143pub ( crate ) mod db;
4244
@@ -68,6 +70,7 @@ pub struct Service<D: Db> {
6870 db : D ,
6971 ohttp : ohttp:: Server ,
7072 sentinel_tag : SentinelTag ,
73+ enable_v1 : bool ,
7174}
7275
7376impl < D : Db , B > tower:: Service < Request < B > > for Service < D >
9194}
9295
9396impl < D : Db > Service < D > {
94- pub fn new ( db : D , ohttp : ohttp:: Server , sentinel_tag : SentinelTag ) -> Self {
95- Self { db, ohttp, sentinel_tag }
97+ pub fn new ( db : D , ohttp : ohttp:: Server , sentinel_tag : SentinelTag , enable_v1 : bool ) -> Self {
98+ Self { db, ohttp, sentinel_tag, enable_v1 }
9699 }
97100
98101 #[ cfg( feature = "_manual-tls" ) ]
@@ -214,7 +217,7 @@ impl<D: Db> Service<D> {
214217 self . handle_ohttp_gateway_get ( & query) . await ,
215218 ( Method :: POST , [ "" , "" ] ) => self . handle_ohttp_gateway ( body) . await ,
216219 ( Method :: GET , [ "" , "ohttp-keys" ] ) => self . get_ohttp_keys ( ) . await ,
217- ( Method :: POST , [ "" , id] ) => self . post_fallback_v1 ( id, query, body) . await ,
220+ ( Method :: POST , [ "" , id] ) => self . handle_post_v1 ( id, query, body) . await ,
218221 ( Method :: GET , [ "" , "health" ] ) => health_check ( ) . await ,
219222 ( Method :: GET , [ "" , "" ] ) => handle_directory_home_path ( ) . await ,
220223 _ => Ok ( not_found ( ) ) ,
@@ -227,6 +230,28 @@ impl<D: Db> Service<D> {
227230 Ok ( response)
228231 }
229232
233+ /// Route POST /{id}: forward to V1 fallback when enabled, otherwise reject.
234+ async fn handle_post_v1 < B > (
235+ & self ,
236+ id : & str ,
237+ query : String ,
238+ body : B ,
239+ ) -> Result < Response < BoxBody < Bytes , hyper:: Error > > , HandlerError >
240+ where
241+ B : Body < Data = Bytes > + Send + ' static ,
242+ B :: Error : Into < BoxError > ,
243+ {
244+ if self . enable_v1 {
245+ self . post_fallback_v1 ( id, query, body) . await
246+ } else {
247+ let _ = ( id, query, body) ;
248+ Ok ( Response :: builder ( )
249+ . status ( StatusCode :: BAD_REQUEST )
250+ . header ( CONTENT_TYPE , "application/json" )
251+ . body ( full ( V1_VERSION_UNSUPPORTED_RES_JSON ) ) ?)
252+ }
253+ }
254+
230255 /// Handle an encapsulated OHTTP request and return an encapsulated response
231256 async fn handle_ohttp_gateway < B > (
232257 & self ,
@@ -304,7 +329,7 @@ impl<D: Db> Service<D> {
304329 match ( parts. method , path_segments. as_slice ( ) ) {
305330 ( Method :: POST , & [ "" , id] ) => self . post_mailbox ( id, body) . await ,
306331 ( Method :: GET , & [ "" , id] ) => self . get_mailbox ( id) . await ,
307- ( Method :: PUT , & [ "" , id] ) => self . put_payjoin_v1 ( id, body) . await ,
332+ ( Method :: PUT , & [ "" , id] ) if self . enable_v1 => self . put_payjoin_v1 ( id, body) . await ,
308333 _ => Ok ( not_found ( ) ) ,
309334 }
310335 }
0 commit comments