Skip to content

Commit e793eb7

Browse files
committed
DirectedChannelInfo to PaymentRelay mapping
1 parent 9cbf752 commit e793eb7

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
@@ -13,6 +13,7 @@ use crate::ln::features::BlindedHopFeatures;
1313
use crate::ln::msgs::DecodeError;
1414
use crate::offers::invoice::BlindedPayInfo;
1515
use crate::prelude::*;
16+
use crate::routing::gossip::DirectedChannelInfo;
1617
use crate::util::ser::{Readable, Writeable, Writer};
1718

1819
use 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+
100114
impl 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

lightning/src/routing/gossip.rs

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

10271027
/// Returns information for the direction.
10281028
#[inline]
1029-
pub(super) fn direction(&self) -> &'a ChannelUpdateInfo { self.direction }
1029+
pub(crate) fn direction(&self) -> &'a ChannelUpdateInfo { self.direction }
10301030

10311031
/// Returns the `node_id` of the source hop.
10321032
///

0 commit comments

Comments
 (0)