@@ -6,12 +6,13 @@ use error::{
66use serde:: { Deserialize , Serialize } ;
77use url:: Url ;
88
9- use super :: v2:: { self , EncapsulationError , HpkeContext } ;
9+ use super :: v2:: { self , extract_request , EncapsulationError , HpkeContext } ;
1010use super :: { serialize_url, AdditionalFeeContribution , BuildSenderError , InternalResult } ;
1111use crate :: hpke:: decrypt_message_b;
1212use crate :: ohttp:: ohttp_decapsulate;
1313use crate :: receive:: ImplementationError ;
1414use crate :: send:: v2:: V2PostContext ;
15+ use crate :: uri:: UrlExt ;
1516use crate :: { PjUri , Request } ;
1617
1718mod error;
@@ -36,55 +37,43 @@ impl Sender {
3637 & self ,
3738 ohttp_relay : Url ,
3839 ) -> Result < ( Request , PostContext ) , CreateRequestError > {
39- use crate :: hpke:: encrypt_message_a;
40- use crate :: ohttp:: ohttp_encapsulate;
41- use crate :: send:: PsbtContext ;
42- use crate :: uri:: UrlExt ;
43- let url = self . 0 . endpoint ( ) . clone ( ) ;
44- if let Ok ( expiry) = url. exp ( ) {
45- if std:: time:: SystemTime :: now ( ) > expiry {
46- return Err ( InternalCreateRequestError :: Expired ( expiry) . into ( ) ) ;
47- }
48- }
4940 let rs = self
5041 . 0
5142 . extract_rs_pubkey ( )
5243 . map_err ( InternalCreateRequestError :: ParseReceiverPubkeyParam ) ?;
44+ let mut ohttp_keys = self
45+ . 0
46+ . endpoint ( )
47+ . ohttp ( )
48+ . map_err ( |_| InternalCreateRequestError :: MissingOhttpConfig ) ?;
5349 let body = serialize_v2_body (
5450 & self . 0 . v1 . psbt ,
5551 self . 0 . v1 . disable_output_substitution ,
5652 self . 0 . v1 . fee_contribution ,
5753 self . 0 . v1 . min_fee_rate ,
5854 ) ?;
59- let hpke_ctx = HpkeContext :: new ( rs, & self . 0 . reply_key ) ;
60- let body = encrypt_message_a (
55+ let ( request, ohttp_ctx) = extract_request (
56+ ohttp_relay,
57+ self . 0 . reply_key . clone ( ) ,
6158 body,
62- & hpke_ctx. reply_pair . public_key ( ) . clone ( ) ,
63- & hpke_ctx. receiver . clone ( ) ,
59+ self . 0 . endpoint ( ) . clone ( ) ,
60+ rs. clone ( ) ,
61+ & mut ohttp_keys,
6462 )
65- . map_err ( InternalCreateRequestError :: Hpke ) ?;
66- let mut ohttp = self
67- . 0
68- . v1
69- . endpoint
70- . ohttp ( )
71- . map_err ( |_| InternalCreateRequestError :: MissingOhttpConfig ) ?;
72- let ( body, ohttp_ctx) = ohttp_encapsulate ( & mut ohttp, "POST" , url. as_str ( ) , Some ( & body) )
73- . map_err ( InternalCreateRequestError :: OhttpEncapsulation ) ?;
74-
63+ . map_err ( InternalCreateRequestError :: V2CreateRequest ) ?;
7564 let v2_post_ctx = V2PostContext {
7665 endpoint : self . 0 . endpoint ( ) . clone ( ) ,
77- psbt_ctx : PsbtContext {
66+ psbt_ctx : crate :: send :: PsbtContext {
7867 original_psbt : self . 0 . v1 . psbt . clone ( ) ,
7968 disable_output_substitution : self . 0 . v1 . disable_output_substitution ,
8069 fee_contribution : self . 0 . v1 . fee_contribution ,
8170 payee : self . 0 . v1 . payee . clone ( ) ,
8271 min_fee_rate : self . 0 . v1 . min_fee_rate ,
8372 } ,
84- hpke_ctx,
73+ hpke_ctx : HpkeContext :: new ( rs , & self . 0 . reply_key ) ,
8574 ohttp_ctx,
8675 } ;
87- Ok ( ( Request :: new_v2 ( & ohttp_relay , & body ) , PostContext ( v2_post_ctx) ) )
76+ Ok ( ( request , PostContext ( v2_post_ctx) ) )
8877 }
8978}
9079
@@ -143,8 +132,8 @@ impl GetContext {
143132 let response_array: & [ u8 ; crate :: directory:: ENCAPSULATED_MESSAGE_BYTES ] =
144133 response. try_into ( ) . map_err ( |_| InternalFinalizedError :: InvalidSize ) ?;
145134
146- let response = ohttp_decapsulate ( ohttp_ctx , response_array )
147- . map_err ( InternalFinalizedError :: Encapsulation ) ?;
135+ let response =
136+ ohttp_decapsulate ( ohttp_ctx , response_array ) . map_err ( InternalFinalizedError :: Ohttp ) ?;
148137 let body = match response. status ( ) {
149138 http:: StatusCode :: OK => Some ( response. body ( ) . to_vec ( ) ) ,
150139 http:: StatusCode :: ACCEPTED => None ,
@@ -185,35 +174,22 @@ impl FinalizeContext {
185174 pub fn extract_req (
186175 & self ,
187176 ohttp_relay : Url ,
188- ) -> Result < ( Request , ohttp:: ClientResponse ) , FinalizedError > {
189- use crate :: hpke:: encrypt_message_a;
190- use crate :: ohttp:: ohttp_encapsulate;
191- use crate :: uri:: UrlExt ;
192-
193- let hpke_ctx = self . hpke_ctx . clone ( ) ;
194- let directory_url = self . directory_url . clone ( ) ;
195- // TODO: check if request is expired off the directory url
196-
197- // The query params are not needed for the final request
198- // The reciever will ignore them. PSBT is all that is needed.
199- let body = serialize_v2_body (
200- & self . psbt ,
201- false , // disable output substitution
202- None , // fee contribution
203- FeeRate :: BROADCAST_MIN ,
204- )
205- . map_err ( InternalFinalizedError :: CreateRequest ) ?;
206- let body = encrypt_message_a (
177+ ) -> Result < ( Request , ohttp:: ClientResponse ) , CreateRequestError > {
178+ let reply_key = self . hpke_ctx . reply_pair . secret_key ( ) ;
179+ let body = serialize_v2_body ( & self . psbt , false , None , FeeRate :: BROADCAST_MIN ) ?;
180+ let mut ohttp_keys = self
181+ . directory_url
182+ . ohttp ( )
183+ . map_err ( |_| InternalCreateRequestError :: MissingOhttpConfig ) ?;
184+ let ( request, ohttp_ctx) = extract_request (
185+ ohttp_relay,
186+ reply_key. clone ( ) ,
207187 body,
208- & hpke_ctx. reply_pair . public_key ( ) . clone ( ) ,
209- & hpke_ctx. receiver . clone ( ) ,
188+ self . directory_url . clone ( ) ,
189+ self . hpke_ctx . receiver . clone ( ) ,
190+ & mut ohttp_keys,
210191 )
211- . map_err ( InternalFinalizedError :: Hpke ) ?;
212- let mut ohttp = directory_url. ohttp ( ) . map_err ( InternalFinalizedError :: ParseOhttp ) ?;
213- let ( body, ohttp_ctx) =
214- ohttp_encapsulate ( & mut ohttp, "POST" , directory_url. as_str ( ) , Some ( & body) )
215- . map_err ( InternalFinalizedError :: Encapsulation ) ?;
216- let request = Request :: new_v2 ( & ohttp_relay, & body) ;
192+ . map_err ( InternalCreateRequestError :: V2CreateRequest ) ?;
217193 Ok ( ( request, ohttp_ctx) )
218194 }
219195
0 commit comments