Skip to content

Commit 38fd7e4

Browse files
committed
DirectedChannelInfo to PaymentRelay mapping
1 parent 04b53fe commit 38fd7e4

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

lightning/src/blinded_path/payment.rs

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use crate::ln::msgs::DecodeError;
2323
use crate::offers::invoice::BlindedPayInfo;
2424
use crate::offers::invoice_request::InvoiceRequestFields;
2525
use crate::offers::offer::OfferId;
26+
use crate::routing::gossip::DirectedChannelInfo;
2627
use 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+
173187
impl 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

lightning/src/routing/gossip.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1039,7 +1039,7 @@ impl<'a> DirectedChannelInfo<'a> {
10391039

10401040
/// Returns information for the direction.
10411041
#[inline]
1042-
pub(super) fn direction(&self) -> &'a ChannelUpdateInfo { self.direction }
1042+
pub(crate) fn direction(&self) -> &'a ChannelUpdateInfo { self.direction }
10431043

10441044
/// Returns the `node_id` of the source hop.
10451045
///

0 commit comments

Comments
 (0)