X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fblinded_path%2Fpayment.rs;h=7f938d6617f71a49ca65b5ddc9637ad9296b5665;hb=ef02b9e6f9caf1ce7a6bf625dc8b4b445d5cc699;hp=4edfb7d8de05bf0e0187941cc91bf4f646fe5557;hpb=b6f3d0a5fa6cf6036d317d3ff47e5252be47bc40;p=rust-lightning diff --git a/lightning/src/blinded_path/payment.rs b/lightning/src/blinded_path/payment.rs index 4edfb7d8..7f938d66 100644 --- a/lightning/src/blinded_path/payment.rs +++ b/lightning/src/blinded_path/payment.rs @@ -8,11 +8,12 @@ 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; use crate::prelude::*; -use crate::util::ser::{Readable, Writeable, Writer}; +use crate::util::ser::{HighZeroBytesDroppedBigSize, Readable, Writeable, Writer}; use core::convert::TryFrom; @@ -96,6 +97,27 @@ pub struct PaymentConstraints { pub htlc_minimum_msat: u64, } +impl TryFrom for PaymentRelay { + type Error = (); + + fn try_from(info: CounterpartyForwardingInfo) -> Result { + 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(&self, w: &mut W) -> Result<(), io::Error> { encode_tlv_stream!(w, { @@ -118,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: &mut R) -> Result { - _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(&self, w: &mut W) -> Result<(), io::Error> { // TODO: write padding @@ -187,7 +194,7 @@ pub(super) fn blinded_hops( } /// `None` if underflow occurs. -fn amt_to_forward_msat(inbound_amt_msat: u64, payment_relay: &PaymentRelay) -> Option { +pub(crate) fn amt_to_forward_msat(inbound_amt_msat: u64, payment_relay: &PaymentRelay) -> Option { 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; @@ -268,16 +275,35 @@ pub(super) fn compute_payinfo( }) } -impl_writeable_msg!(PaymentRelay, { - cltv_expiry_delta, - fee_proportional_millionths, - fee_base_msat -}, {}); +impl Writeable for PaymentRelay { + fn write(&self, w: &mut W) -> Result<(), io::Error> { + self.cltv_expiry_delta.write(w)?; + self.fee_proportional_millionths.write(w)?; + HighZeroBytesDroppedBigSize(self.fee_base_msat).write(w) + } +} +impl Readable for PaymentRelay { + fn read(r: &mut R) -> Result { + let cltv_expiry_delta: u16 = Readable::read(r)?; + let fee_proportional_millionths: u32 = Readable::read(r)?; + let fee_base_msat: HighZeroBytesDroppedBigSize = Readable::read(r)?; + Ok(Self { cltv_expiry_delta, fee_proportional_millionths, fee_base_msat: fee_base_msat.0 }) + } +} -impl_writeable_msg!(PaymentConstraints, { - max_cltv_expiry, - htlc_minimum_msat -}, {}); +impl Writeable for PaymentConstraints { + fn write(&self, w: &mut W) -> Result<(), io::Error> { + self.max_cltv_expiry.write(w)?; + HighZeroBytesDroppedBigSize(self.htlc_minimum_msat).write(w) + } +} +impl Readable for PaymentConstraints { + fn read(r: &mut R) -> Result { + let max_cltv_expiry: u32 = Readable::read(r)?; + let htlc_minimum_msat: HighZeroBytesDroppedBigSize = Readable::read(r)?; + Ok(Self { max_cltv_expiry, htlc_minimum_msat: htlc_minimum_msat.0 }) + } +} #[cfg(test)] mod tests {