Merge pull request #2868 from orbitalturtle/export-send-onion-path
[rust-lightning] / lightning / src / blinded_path / payment.rs
index 39f16a91692cb30fb1be087191b4c32a506de830..5d332a76faf57fed3b2fe844b91d5c5d04ed8a81 100644 (file)
@@ -8,6 +8,7 @@ use crate::blinded_path::BlindedHop;
 use crate::blinded_path::utils;
 use crate::io;
 use crate::ln::PaymentSecret;
+use crate::ln::channelmanager::CounterpartyForwardingInfo;
 use crate::ln::features::BlindedHopFeatures;
 use crate::ln::msgs::DecodeError;
 use crate::offers::invoice::BlindedPayInfo;
@@ -89,14 +90,34 @@ pub struct PaymentRelay {
 /// [`BlindedHop`]: crate::blinded_path::BlindedHop
 #[derive(Clone, Debug)]
 pub struct PaymentConstraints {
-       /// The maximum total CLTV delta that is acceptable when relaying a payment over this
-       /// [`BlindedHop`].
+       /// The maximum total CLTV that is acceptable when relaying a payment over this [`BlindedHop`].
        pub max_cltv_expiry: u32,
        /// The minimum value, in msat, that may be accepted by the node corresponding to this
        /// [`BlindedHop`].
        pub htlc_minimum_msat: u64,
 }
 
+impl TryFrom<CounterpartyForwardingInfo> for PaymentRelay {
+       type Error = ();
+
+       fn try_from(info: CounterpartyForwardingInfo) -> Result<Self, ()> {
+               let CounterpartyForwardingInfo {
+                       fee_base_msat, fee_proportional_millionths, cltv_expiry_delta
+               } = info;
+
+               // Avoid exposing esoteric CLTV expiry deltas
+               let cltv_expiry_delta = match cltv_expiry_delta {
+                       0..=40 => 40,
+                       41..=80 => 80,
+                       81..=144 => 144,
+                       145..=216 => 216,
+                       _ => return Err(()),
+               };
+
+               Ok(Self { cltv_expiry_delta, fee_proportional_millionths, fee_base_msat })
+       }
+}
+
 impl Writeable for ForwardTlvs {
        fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
                encode_tlv_stream!(w, {
@@ -119,21 +140,6 @@ impl Writeable for ReceiveTlvs {
        }
 }
 
-// This will be removed once we support forwarding blinded HTLCs, because we'll always read a
-// `BlindedPaymentTlvs` instead.
-impl Readable for ReceiveTlvs {
-       fn read<R: io::Read>(r: &mut R) -> Result<Self, DecodeError> {
-               _init_and_read_tlv_stream!(r, {
-                       (12, payment_constraints, required),
-                       (65536, payment_secret, required),
-               });
-               Ok(Self {
-                       payment_secret: payment_secret.0.unwrap(),
-                       payment_constraints: payment_constraints.0.unwrap()
-               })
-       }
-}
-
 impl<'a> Writeable for BlindedPaymentTlvsRef<'a> {
        fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
                // TODO: write padding
@@ -188,7 +194,7 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
 }
 
 /// `None` if underflow occurs.
-fn amt_to_forward_msat(inbound_amt_msat: u64, payment_relay: &PaymentRelay) -> Option<u64> {
+pub(crate) fn amt_to_forward_msat(inbound_amt_msat: u64, payment_relay: &PaymentRelay) -> Option<u64> {
        let inbound_amt = inbound_amt_msat as u128;
        let base = payment_relay.fee_base_msat as u128;
        let prop = payment_relay.fee_proportional_millionths as u128;