+
+ /// Creates a new [`RecipientOnionFields`] from an existing one, adding custom TLVs. Each
+ /// TLV is provided as a `(u64, Vec<u8>)` for the type number and serialized value
+ /// respectively. TLV type numbers must be unique and within the range
+ /// reserved for custom types, i.e. >= 2^16, otherwise this method will return `Err(())`.
+ ///
+ /// This method will also error for types in the experimental range which have been
+ /// standardized within the protocol, which only includes 5482373484 (keysend) for now.
+ ///
+ /// See [`Self::custom_tlvs`] for more info.
+ pub fn with_custom_tlvs(mut self, mut custom_tlvs: Vec<(u64, Vec<u8>)>) -> Result<Self, ()> {
+ custom_tlvs.sort_unstable_by_key(|(typ, _)| *typ);
+ let mut prev_type = None;
+ for (typ, _) in custom_tlvs.iter() {
+ if *typ < 1 << 16 { return Err(()); }
+ if *typ == 5482373484 { return Err(()); } // keysend
+ match prev_type {
+ Some(prev) if prev >= *typ => return Err(()),
+ _ => {},
+ }
+ prev_type = Some(*typ);
+ }
+ self.custom_tlvs = custom_tlvs;
+ Ok(self)
+ }
+
+ /// Gets the custom TLVs that will be sent or have been received.
+ ///
+ /// Custom TLVs allow sending extra application-specific data with a payment. They provide
+ /// additional flexibility on top of payment metadata, as while other implementations may
+ /// require `payment_metadata` to reflect metadata provided in an invoice, custom TLVs
+ /// do not have this restriction.
+ ///
+ /// Note that if this field is non-empty, it will contain strictly increasing TLVs, each
+ /// represented by a `(u64, Vec<u8>)` for its type number and serialized value respectively.
+ /// This is validated when setting this field using [`Self::with_custom_tlvs`].
+ pub fn custom_tlvs(&self) -> &Vec<(u64, Vec<u8>)> {
+ &self.custom_tlvs
+ }
+
+ /// When we have received some HTLC(s) towards an MPP payment, as we receive further HTLC(s) we
+ /// have to make sure that some fields match exactly across the parts. For those that aren't
+ /// required to match, if they don't match we should remove them so as to not expose data
+ /// that's dependent on the HTLC receive order to users.
+ ///
+ /// Here we implement this, first checking compatibility then mutating two objects and then
+ /// dropping any remaining non-matching fields from both.
+ pub(super) fn check_merge(&mut self, further_htlc_fields: &mut Self) -> Result<(), ()> {
+ if self.payment_secret != further_htlc_fields.payment_secret { return Err(()); }
+ if self.payment_metadata != further_htlc_fields.payment_metadata { return Err(()); }
+
+ let tlvs = &mut self.custom_tlvs;
+ let further_tlvs = &mut further_htlc_fields.custom_tlvs;
+
+ let even_tlvs = tlvs.iter().filter(|(typ, _)| *typ % 2 == 0);
+ let further_even_tlvs = further_tlvs.iter().filter(|(typ, _)| *typ % 2 == 0);
+ if even_tlvs.ne(further_even_tlvs) { return Err(()) }
+
+ tlvs.retain(|tlv| further_tlvs.iter().any(|further_tlv| tlv == further_tlv));
+ further_tlvs.retain(|further_tlv| tlvs.iter().any(|tlv| tlv == further_tlv));
+
+ Ok(())
+ }
+}
+
+/// Arguments for [`super::channelmanager::ChannelManager::send_payment_along_path`].
+pub(super) struct SendAlongPathArgs<'a> {
+ pub path: &'a Path,
+ pub payment_hash: &'a PaymentHash,
+ pub recipient_onion: RecipientOnionFields,
+ pub total_value: u64,
+ pub cur_height: u32,
+ pub payment_id: PaymentId,
+ pub keysend_preimage: &'a Option<PaymentPreimage>,
+ pub session_priv_bytes: [u8; 32],