@@ -13,6 +13,7 @@ use crate::ln::features::BlindedHopFeatures;
1313use crate :: ln:: msgs:: DecodeError ;
1414use crate :: offers:: invoice:: BlindedPayInfo ;
1515use crate :: prelude:: * ;
16+ use crate :: routing:: gossip:: DirectedChannelInfo ;
1617use crate :: util:: ser:: { Readable , Writeable , Writer } ;
1718
1819use core:: convert:: TryFrom ;
@@ -97,6 +98,19 @@ pub struct PaymentConstraints {
9798 pub htlc_minimum_msat : u64 ,
9899}
99100
101+ impl PaymentRelay {
102+ fn normalize_cltv_expiry_delta ( cltv_expiry_delta : u16 ) -> Result < u16 , ( ) > {
103+ // Avoid exposing esoteric CLTV expiry deltas, which could de-anonymize the path.
104+ match cltv_expiry_delta {
105+ 0 ..=40 => Ok ( 40 ) ,
106+ 41 ..=80 => Ok ( 80 ) ,
107+ 81 ..=144 => Ok ( 144 ) ,
108+ 145 ..=216 => Ok ( 216 ) ,
109+ _ => Err ( ( ) ) ,
110+ }
111+ }
112+ }
113+
100114impl TryFrom < CounterpartyForwardingInfo > for PaymentRelay {
101115 type Error = ( ) ;
102116
@@ -105,16 +119,25 @@ impl TryFrom<CounterpartyForwardingInfo> for PaymentRelay {
105119 fee_base_msat, fee_proportional_millionths, cltv_expiry_delta
106120 } = info;
107121
108- // Avoid exposing esoteric CLTV expiry deltas
109- let cltv_expiry_delta = match cltv_expiry_delta {
110- 0 ..=40 => 40 ,
111- 41 ..=80 => 80 ,
112- 81 ..=144 => 144 ,
113- 145 ..=216 => 216 ,
114- _ => return Err ( ( ) ) ,
115- } ;
122+ Ok ( Self {
123+ cltv_expiry_delta : Self :: normalize_cltv_expiry_delta ( cltv_expiry_delta) ?,
124+ fee_proportional_millionths,
125+ fee_base_msat
126+ } )
127+ }
128+ }
129+
130+ impl < ' a > TryFrom < DirectedChannelInfo < ' a > > for PaymentRelay {
131+ type Error = ( ) ;
132+
133+ fn try_from ( info : DirectedChannelInfo < ' a > ) -> Result < Self , ( ) > {
134+ let direction = info. direction ( ) ;
116135
117- Ok ( Self { cltv_expiry_delta, fee_proportional_millionths, fee_base_msat } )
136+ Ok ( Self {
137+ cltv_expiry_delta : Self :: normalize_cltv_expiry_delta ( direction. cltv_expiry_delta ) ?,
138+ fee_proportional_millionths : direction. fees . proportional_millionths ,
139+ fee_base_msat : direction. fees . base_msat ,
140+ } )
118141 }
119142}
120143
0 commit comments