@@ -23,6 +23,7 @@ use crate::ln::msgs::DecodeError;
2323use crate :: offers:: invoice:: BlindedPayInfo ;
2424use crate :: offers:: invoice_request:: InvoiceRequestFields ;
2525use crate :: offers:: offer:: OfferId ;
26+ use crate :: routing:: gossip:: DirectedChannelInfo ;
2627use crate :: util:: ser:: { HighZeroBytesDroppedBigSize , Readable , Writeable , Writer } ;
2728
2829#[ allow( unused_imports) ]
@@ -170,6 +171,19 @@ impl PaymentContext {
170171 }
171172}
172173
174+ impl PaymentRelay {
175+ fn normalize_cltv_expiry_delta ( cltv_expiry_delta : u16 ) -> Result < u16 , ( ) > {
176+ // Avoid exposing esoteric CLTV expiry deltas, which could de-anonymize the path.
177+ match cltv_expiry_delta {
178+ 0 ..=40 => Ok ( 40 ) ,
179+ 41 ..=80 => Ok ( 80 ) ,
180+ 81 ..=144 => Ok ( 144 ) ,
181+ 145 ..=216 => Ok ( 216 ) ,
182+ _ => Err ( ( ) ) ,
183+ }
184+ }
185+ }
186+
173187impl TryFrom < CounterpartyForwardingInfo > for PaymentRelay {
174188 type Error = ( ) ;
175189
@@ -178,16 +192,25 @@ impl TryFrom<CounterpartyForwardingInfo> for PaymentRelay {
178192 fee_base_msat, fee_proportional_millionths, cltv_expiry_delta
179193 } = info;
180194
181- // Avoid exposing esoteric CLTV expiry deltas
182- let cltv_expiry_delta = match cltv_expiry_delta {
183- 0 ..=40 => 40 ,
184- 41 ..=80 => 80 ,
185- 81 ..=144 => 144 ,
186- 145 ..=216 => 216 ,
187- _ => return Err ( ( ) ) ,
188- } ;
195+ Ok ( Self {
196+ cltv_expiry_delta : Self :: normalize_cltv_expiry_delta ( cltv_expiry_delta) ?,
197+ fee_proportional_millionths,
198+ fee_base_msat
199+ } )
200+ }
201+ }
202+
203+ impl < ' a > TryFrom < DirectedChannelInfo < ' a > > for PaymentRelay {
204+ type Error = ( ) ;
205+
206+ fn try_from ( info : DirectedChannelInfo < ' a > ) -> Result < Self , ( ) > {
207+ let direction = info. direction ( ) ;
189208
190- Ok ( Self { cltv_expiry_delta, fee_proportional_millionths, fee_base_msat } )
209+ Ok ( Self {
210+ cltv_expiry_delta : Self :: normalize_cltv_expiry_delta ( direction. cltv_expiry_delta ) ?,
211+ fee_proportional_millionths : direction. fees . proportional_millionths ,
212+ fee_base_msat : direction. fees . base_msat ,
213+ } )
191214 }
192215}
193216
0 commit comments