@@ -94,6 +94,19 @@ impl BlockedAddresses {
9494 }
9595}
9696
97+ /// V1 protocol configuration.
98+ ///
99+ /// Its presence in [`Service`] enables the V1 fallback path;
100+ /// its contents carry optional blocklist screening.
101+ #[ derive( Clone , Default ) ]
102+ pub struct V1 {
103+ blocked_addresses : Option < BlockedAddresses > ,
104+ }
105+
106+ impl V1 {
107+ pub fn new ( blocked_addresses : Option < BlockedAddresses > ) -> Self { Self { blocked_addresses } }
108+ }
109+
97110fn parse_address_lines ( text : & str ) -> std:: collections:: HashSet < bitcoin:: ScriptBuf > {
98111 text. lines ( )
99112 . filter_map ( |l| {
@@ -117,8 +130,7 @@ pub struct Service<D: Db> {
117130 db : D ,
118131 ohttp : ohttp:: Server ,
119132 sentinel_tag : SentinelTag ,
120- enable_v1 : bool ,
121- blocked_addresses : Option < BlockedAddresses > ,
133+ v1 : Option < V1 > ,
122134}
123135
124136impl < D : Db , B > tower:: Service < Request < B > > for Service < D >
@@ -142,13 +154,8 @@ where
142154}
143155
144156impl < D : Db > Service < D > {
145- pub fn new ( db : D , ohttp : ohttp:: Server , sentinel_tag : SentinelTag , enable_v1 : bool ) -> Self {
146- Self { db, ohttp, sentinel_tag, enable_v1, blocked_addresses : None }
147- }
148-
149- pub fn with_blocked_addresses ( mut self , addrs : BlockedAddresses ) -> Self {
150- self . blocked_addresses = Some ( addrs) ;
151- self
157+ pub fn new ( db : D , ohttp : ohttp:: Server , sentinel_tag : SentinelTag , v1 : Option < V1 > ) -> Self {
158+ Self { db, ohttp, sentinel_tag, v1 }
152159 }
153160
154161 #[ cfg( feature = "_manual-tls" ) ]
@@ -294,7 +301,7 @@ impl<D: Db> Service<D> {
294301 B : Body < Data = Bytes > + Send + ' static ,
295302 B :: Error : Into < BoxError > ,
296303 {
297- if self . enable_v1 {
304+ if self . v1 . is_some ( ) {
298305 self . post_fallback_v1 ( id, query, body) . await
299306 } else {
300307 let _ = ( id, query, body) ;
@@ -382,7 +389,7 @@ impl<D: Db> Service<D> {
382389 match ( parts. method , path_segments. as_slice ( ) ) {
383390 ( Method :: POST , & [ "" , id] ) => self . post_mailbox ( id, body) . await ,
384391 ( Method :: GET , & [ "" , id] ) => self . get_mailbox ( id) . await ,
385- ( Method :: PUT , & [ "" , id] ) if self . enable_v1 => self . put_payjoin_v1 ( id, body) . await ,
392+ ( Method :: PUT , & [ "" , id] ) if self . v1 . is_some ( ) => self . put_payjoin_v1 ( id, body) . await ,
386393 _ => Ok ( not_found ( ) ) ,
387394 }
388395 }
@@ -472,7 +479,7 @@ impl<D: Db> Service<D> {
472479 Err ( _) => return Ok ( bad_request_body_res) ,
473480 } ;
474481
475- if let Some ( blocked) = & self . blocked_addresses {
482+ if let Some ( blocked) = self . v1 . as_ref ( ) . and_then ( |v| v . blocked_addresses . as_ref ( ) ) {
476483 let scripts = blocked. 0 . read ( ) . await ;
477484 if !scripts. is_empty ( ) {
478485 match screen_v1_addresses ( & body_str, & scripts) {
@@ -763,12 +770,12 @@ mod tests {
763770
764771 use super :: * ;
765772
766- async fn test_service ( enable_v1 : bool ) -> Service < FilesDb > {
773+ async fn test_service ( v1 : Option < V1 > ) -> Service < FilesDb > {
767774 let dir = tempfile:: tempdir ( ) . expect ( "tempdir" ) ;
768775 let db = FilesDb :: init ( Duration :: from_millis ( 100 ) , dir. keep ( ) ) . await . expect ( "db init" ) ;
769776 let ohttp: ohttp:: Server =
770777 key_config:: gen_ohttp_server_config ( ) . expect ( "ohttp config" ) . into ( ) ;
771- Service :: new ( db, ohttp, SentinelTag :: new ( [ 0u8 ; 32 ] ) , enable_v1 )
778+ Service :: new ( db, ohttp, SentinelTag :: new ( [ 0u8 ; 32 ] ) , v1 )
772779 }
773780
774781 /// A valid ShortId encoded as bech32 for use in URL paths.
@@ -785,7 +792,7 @@ mod tests {
785792
786793 #[ tokio:: test]
787794 async fn post_v1_when_disabled_returns_version_unsupported ( ) {
788- let mut svc = test_service ( false ) . await ;
795+ let mut svc = test_service ( None ) . await ;
789796 let id = valid_short_id_path ( ) ;
790797 let req = Request :: builder ( )
791798 . method ( Method :: POST )
@@ -802,7 +809,7 @@ mod tests {
802809
803810 #[ tokio:: test]
804811 async fn post_v1_with_invalid_body_returns_reject ( ) {
805- let mut svc = test_service ( true ) . await ;
812+ let mut svc = test_service ( Some ( V1 :: new ( None ) ) ) . await ;
806813 let id = valid_short_id_path ( ) ;
807814 let req = Request :: builder ( )
808815 . method ( Method :: POST )
@@ -819,7 +826,7 @@ mod tests {
819826
820827 #[ tokio:: test]
821828 async fn post_v1_with_no_receiver_returns_unavailable ( ) {
822- let mut svc = test_service ( true ) . await ;
829+ let mut svc = test_service ( Some ( V1 :: new ( None ) ) ) . await ;
823830 let id = valid_short_id_path ( ) ;
824831 let req = Request :: builder ( )
825832 . method ( Method :: POST )
0 commit comments