@@ -15,9 +15,11 @@ use hyper_util::rt::TokioIo;
1515use payjoin:: directory:: { ShortId , ShortIdError , ENCAPSULATED_MESSAGE_BYTES } ;
1616use tokio:: net:: TcpListener ;
1717use tokio:: sync:: Mutex ;
18- use tracing:: { debug, error, info , trace} ;
18+ use tracing:: { debug, error, trace} ;
1919
2020use crate :: db:: DbPool ;
21+ pub mod key_config;
22+ pub use crate :: key_config:: * ;
2123
2224pub const DEFAULT_DIR_PORT : u16 = 8080 ;
2325pub const DEFAULT_DB_HOST : & str = "localhost:6379" ;
@@ -43,11 +45,13 @@ pub async fn listen_tcp_with_tls_on_free_port(
4345 db_host : String ,
4446 timeout : Duration ,
4547 cert_key : ( Vec < u8 > , Vec < u8 > ) ,
48+ ohttp : ohttp:: Server ,
4649) -> Result < ( u16 , tokio:: task:: JoinHandle < Result < ( ) , BoxError > > ) , BoxError > {
4750 let listener = tokio:: net:: TcpListener :: bind ( "[::]:0" ) . await ?;
4851 let port = listener. local_addr ( ) ?. port ( ) ;
4952 println ! ( "Directory server binding to port {}" , listener. local_addr( ) ?) ;
50- let handle = listen_tcp_with_tls_on_listener ( listener, db_host, timeout, cert_key) . await ?;
53+ let handle =
54+ listen_tcp_with_tls_on_listener ( listener, db_host, timeout, cert_key, ohttp) . await ?;
5155 Ok ( ( port, handle) )
5256}
5357
@@ -58,9 +62,10 @@ async fn listen_tcp_with_tls_on_listener(
5862 db_host : String ,
5963 timeout : Duration ,
6064 tls_config : ( Vec < u8 > , Vec < u8 > ) ,
65+ ohttp : ohttp:: Server ,
6166) -> Result < tokio:: task:: JoinHandle < Result < ( ) , BoxError > > , BoxError > {
6267 let pool = DbPool :: new ( timeout, db_host) . await ?;
63- let ohttp = Arc :: new ( Mutex :: new ( init_ohttp ( ) ? ) ) ;
68+ let ohttp = Arc :: new ( Mutex :: new ( ohttp ) ) ;
6469 let tls_acceptor = init_tls_acceptor ( tls_config) ?;
6570 // Spawn the connection handling loop in a separate task
6671 let handle = tokio:: spawn ( async move {
@@ -100,9 +105,10 @@ pub async fn listen_tcp(
100105 port : u16 ,
101106 db_host : String ,
102107 timeout : Duration ,
108+ ohttp : ohttp:: Server ,
103109) -> Result < ( ) , Box < dyn std:: error:: Error > > {
104110 let pool = DbPool :: new ( timeout, db_host) . await ?;
105- let ohttp = Arc :: new ( Mutex :: new ( init_ohttp ( ) ? ) ) ;
111+ let ohttp = Arc :: new ( Mutex :: new ( ohttp ) ) ;
106112 let bind_addr = SocketAddr :: new ( IpAddr :: V6 ( Ipv6Addr :: UNSPECIFIED ) , port) ;
107113 let listener = TcpListener :: bind ( bind_addr) . await ?;
108114 while let Ok ( ( stream, _) ) = listener. accept ( ) . await {
@@ -134,10 +140,11 @@ pub async fn listen_tcp_with_tls(
134140 db_host : String ,
135141 timeout : Duration ,
136142 cert_key : ( Vec < u8 > , Vec < u8 > ) ,
143+ ohttp : ohttp:: Server ,
137144) -> Result < tokio:: task:: JoinHandle < Result < ( ) , BoxError > > , BoxError > {
138145 let addr = format ! ( "0.0.0.0:{}" , port) ;
139146 let listener = tokio:: net:: TcpListener :: bind ( & addr) . await ?;
140- listen_tcp_with_tls_on_listener ( listener, db_host, timeout, cert_key) . await
147+ listen_tcp_with_tls_on_listener ( listener, db_host, timeout, cert_key, ohttp ) . await
141148}
142149
143150#[ cfg( feature = "_danger-local-https" ) ]
@@ -158,21 +165,6 @@ fn init_tls_acceptor(cert_key: (Vec<u8>, Vec<u8>)) -> Result<tokio_rustls::TlsAc
158165 Ok ( TlsAcceptor :: from ( Arc :: new ( server_config) ) )
159166}
160167
161- fn init_ohttp ( ) -> Result < ohttp:: Server > {
162- use ohttp:: hpke:: { Aead , Kdf , Kem } ;
163- use ohttp:: { KeyId , SymmetricSuite } ;
164-
165- const KEY_ID : KeyId = 1 ;
166- const KEM : Kem = Kem :: K256Sha256 ;
167- const SYMMETRIC : & [ SymmetricSuite ] =
168- & [ SymmetricSuite :: new ( Kdf :: HkdfSha256 , Aead :: ChaCha20Poly1305 ) ] ;
169-
170- // create or read from file
171- let server_config = ohttp:: KeyConfig :: new ( KEY_ID , KEM , Vec :: from ( SYMMETRIC ) ) ?;
172- info ! ( "Initialized a new OHTTP Key Configuration. GET /ohttp-keys to fetch it." ) ;
173- Ok ( ohttp:: Server :: new ( server_config) ?)
174- }
175-
176168async fn serve_payjoin_directory (
177169 req : Request < Incoming > ,
178170 pool : DbPool ,
0 commit comments