Skip to content

Commit f6f3f03

Browse files
committed
Require a PaymentContext in payment::ReceiveTlvs
UnknownPaymentContext is used when payment::ReceiveTlvs doesn't contain a PaymentContext. This is only needed for a legacy BlindedPaymentPath. Since these paths a short-lived, UnknownPaymentContext is no longer needed. Remove it and require that payment::ReceiveTlvs always contains a PaymentContext. Any such path would fail authentication since the payment::ReceiveTlvs would be missing an HMAC and Nonce, so this is a good time to remove UnknownPaymentContext.
1 parent c0d94a9 commit f6f3f03

File tree

4 files changed

+17
-42
lines changed

4 files changed

+17
-42
lines changed

lightning/src/blinded_path/payment.rs

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,6 @@ pub struct PaymentConstraints {
344344
/// [`PaymentPurpose`]: crate::events::PaymentPurpose
345345
#[derive(Clone, Debug, Eq, PartialEq)]
346346
pub enum PaymentContext {
347-
/// The payment context was unknown.
348-
Unknown(UnknownPaymentContext),
349-
350347
/// The payment was made for an invoice requested from a BOLT 12 [`Offer`].
351348
///
352349
/// [`Offer`]: crate::offers::offer::Offer
@@ -364,10 +361,6 @@ pub(crate) enum PaymentContextRef<'a> {
364361
Bolt12Refund(&'a Bolt12RefundContext),
365362
}
366363

367-
/// An unknown payment context.
368-
#[derive(Clone, Debug, Eq, PartialEq)]
369-
pub struct UnknownPaymentContext(());
370-
371364
/// The context of a payment made for an invoice requested from a BOLT 12 [`Offer`].
372365
///
373366
/// [`Offer`]: crate::offers::offer::Offer
@@ -391,12 +384,6 @@ pub struct Bolt12OfferContext {
391384
#[derive(Clone, Debug, Eq, PartialEq)]
392385
pub struct Bolt12RefundContext {}
393386

394-
impl PaymentContext {
395-
pub(crate) fn unknown() -> Self {
396-
PaymentContext::Unknown(UnknownPaymentContext(()))
397-
}
398-
}
399-
400387
impl TryFrom<CounterpartyForwardingInfo> for PaymentRelay {
401388
type Error = ();
402389

@@ -477,7 +464,7 @@ impl Readable for BlindedPaymentTlvs {
477464
(12, payment_constraints, required),
478465
(14, features, (option, encoding: (BlindedHopFeatures, WithoutLength))),
479466
(65536, payment_secret, option),
480-
(65537, payment_context, (default_value, PaymentContext::unknown())),
467+
(65537, payment_context, option),
481468
(65539, authentication, option),
482469
});
483470
let _padding: Option<utils::Padding> = _padding;
@@ -499,7 +486,7 @@ impl Readable for BlindedPaymentTlvs {
499486
tlvs: UnauthenticatedReceiveTlvs {
500487
payment_secret: payment_secret.ok_or(DecodeError::InvalidValue)?,
501488
payment_constraints: payment_constraints.0.unwrap(),
502-
payment_context: payment_context.0.unwrap(),
489+
payment_context: payment_context.ok_or(DecodeError::InvalidValue)?,
503490
},
504491
authentication: authentication.ok_or(DecodeError::InvalidValue)?,
505492
}))
@@ -637,7 +624,7 @@ impl Readable for PaymentConstraints {
637624

638625
impl_writeable_tlv_based_enum_legacy!(PaymentContext,
639626
;
640-
(0, Unknown),
627+
// 0 for Unknown removed in version 0.1.
641628
(1, Bolt12Offer),
642629
(2, Bolt12Refund),
643630
);
@@ -659,18 +646,6 @@ impl<'a> Writeable for PaymentContextRef<'a> {
659646
}
660647
}
661648

662-
impl Writeable for UnknownPaymentContext {
663-
fn write<W: Writer>(&self, _w: &mut W) -> Result<(), io::Error> {
664-
Ok(())
665-
}
666-
}
667-
668-
impl Readable for UnknownPaymentContext {
669-
fn read<R: io::Read>(_r: &mut R) -> Result<Self, DecodeError> {
670-
Ok(UnknownPaymentContext(()))
671-
}
672-
}
673-
674649
impl_writeable_tlv_based!(Bolt12OfferContext, {
675650
(0, offer_id, required),
676651
(2, invoice_request, required),
@@ -681,7 +656,7 @@ impl_writeable_tlv_based!(Bolt12RefundContext, {});
681656
#[cfg(test)]
682657
mod tests {
683658
use bitcoin::secp256k1::PublicKey;
684-
use crate::blinded_path::payment::{PaymentForwardNode, ForwardTlvs, PaymentConstraints, PaymentContext, PaymentRelay, UnauthenticatedReceiveTlvs};
659+
use crate::blinded_path::payment::{Bolt12RefundContext, PaymentForwardNode, ForwardTlvs, PaymentConstraints, PaymentContext, PaymentRelay, UnauthenticatedReceiveTlvs};
685660
use crate::types::payment::PaymentSecret;
686661
use crate::types::features::BlindedHopFeatures;
687662
use crate::ln::functional_test_utils::TEST_FINAL_CLTV;
@@ -732,7 +707,7 @@ mod tests {
732707
max_cltv_expiry: 0,
733708
htlc_minimum_msat: 1,
734709
},
735-
payment_context: PaymentContext::unknown(),
710+
payment_context: PaymentContext::Bolt12Refund(Bolt12RefundContext {}),
736711
};
737712
let htlc_maximum_msat = 100_000;
738713
let blinded_payinfo = super::compute_payinfo(&intermediate_nodes[..], &recv_tlvs, htlc_maximum_msat, 12).unwrap();
@@ -751,7 +726,7 @@ mod tests {
751726
max_cltv_expiry: 0,
752727
htlc_minimum_msat: 1,
753728
},
754-
payment_context: PaymentContext::unknown(),
729+
payment_context: PaymentContext::Bolt12Refund(Bolt12RefundContext {}),
755730
};
756731
let blinded_payinfo = super::compute_payinfo(&[], &recv_tlvs, 4242, TEST_FINAL_CLTV as u16).unwrap();
757732
assert_eq!(blinded_payinfo.fee_base_msat, 0);
@@ -807,7 +782,7 @@ mod tests {
807782
max_cltv_expiry: 0,
808783
htlc_minimum_msat: 3,
809784
},
810-
payment_context: PaymentContext::unknown(),
785+
payment_context: PaymentContext::Bolt12Refund(Bolt12RefundContext {}),
811786
};
812787
let htlc_maximum_msat = 100_000;
813788
let blinded_payinfo = super::compute_payinfo(&intermediate_nodes[..], &recv_tlvs, htlc_maximum_msat, TEST_FINAL_CLTV as u16).unwrap();
@@ -860,7 +835,7 @@ mod tests {
860835
max_cltv_expiry: 0,
861836
htlc_minimum_msat: 1,
862837
},
863-
payment_context: PaymentContext::unknown(),
838+
payment_context: PaymentContext::Bolt12Refund(Bolt12RefundContext {}),
864839
};
865840
let htlc_minimum_msat = 3798;
866841
assert!(super::compute_payinfo(&intermediate_nodes[..], &recv_tlvs, htlc_minimum_msat - 1, TEST_FINAL_CLTV as u16).is_err());
@@ -917,7 +892,7 @@ mod tests {
917892
max_cltv_expiry: 0,
918893
htlc_minimum_msat: 1,
919894
},
920-
payment_context: PaymentContext::unknown(),
895+
payment_context: PaymentContext::Bolt12Refund(Bolt12RefundContext {}),
921896
};
922897

923898
let blinded_payinfo = super::compute_payinfo(&intermediate_nodes[..], &recv_tlvs, 10_000, TEST_FINAL_CLTV as u16).unwrap();

lightning/src/events/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ impl PaymentPurpose {
183183
payment_context: Option<PaymentContext>,
184184
) -> Self {
185185
match payment_context {
186-
Some(PaymentContext::Unknown(_)) | None => {
186+
None => {
187187
PaymentPurpose::Bolt11InvoicePayment {
188188
payment_preimage,
189189
payment_secret,

lightning/src/ln/blinded_payment_tests.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use bitcoin::secp256k1::{PublicKey, Scalar, Secp256k1, SecretKey, schnorr};
1212
use bitcoin::secp256k1::ecdh::SharedSecret;
1313
use bitcoin::secp256k1::ecdsa::{RecoverableSignature, Signature};
1414
use crate::blinded_path;
15-
use crate::blinded_path::payment::{BlindedPaymentPath, PaymentForwardNode, ForwardTlvs, PaymentConstraints, PaymentContext, PaymentRelay, UnauthenticatedReceiveTlvs};
15+
use crate::blinded_path::payment::{BlindedPaymentPath, Bolt12RefundContext, PaymentForwardNode, ForwardTlvs, PaymentConstraints, PaymentContext, PaymentRelay, UnauthenticatedReceiveTlvs};
1616
use crate::events::{Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider, PaymentFailureReason};
1717
use crate::ln::types::ChannelId;
1818
use crate::types::payment::{PaymentHash, PaymentSecret};
@@ -79,7 +79,7 @@ fn blinded_payment_path(
7979
htlc_minimum_msat:
8080
intro_node_min_htlc_opt.unwrap_or_else(|| channel_upds.last().unwrap().htlc_minimum_msat),
8181
},
82-
payment_context: PaymentContext::unknown(),
82+
payment_context: PaymentContext::Bolt12Refund(Bolt12RefundContext {}),
8383
};
8484

8585
let nonce = Nonce([42u8; 16]);
@@ -130,7 +130,7 @@ fn do_one_hop_blinded_path(success: bool) {
130130
max_cltv_expiry: u32::max_value(),
131131
htlc_minimum_msat: chan_upd.htlc_minimum_msat,
132132
},
133-
payment_context: PaymentContext::unknown(),
133+
payment_context: PaymentContext::Bolt12Refund(Bolt12RefundContext {}),
134134
};
135135
let nonce = Nonce([42u8; 16]);
136136
let expanded_key = chanmon_cfgs[1].keys_manager.get_inbound_payment_key();
@@ -178,7 +178,7 @@ fn mpp_to_one_hop_blinded_path() {
178178
max_cltv_expiry: u32::max_value(),
179179
htlc_minimum_msat: chan_upd_1_3.htlc_minimum_msat,
180180
},
181-
payment_context: PaymentContext::unknown(),
181+
payment_context: PaymentContext::Bolt12Refund(Bolt12RefundContext {}),
182182
};
183183
let nonce = Nonce([42u8; 16]);
184184
let expanded_key = chanmon_cfgs[3].keys_manager.get_inbound_payment_key();
@@ -1386,7 +1386,7 @@ fn custom_tlvs_to_blinded_path() {
13861386
max_cltv_expiry: u32::max_value(),
13871387
htlc_minimum_msat: chan_upd.htlc_minimum_msat,
13881388
},
1389-
payment_context: PaymentContext::unknown(),
1389+
payment_context: PaymentContext::Bolt12Refund(Bolt12RefundContext {}),
13901390
};
13911391
let nonce = Nonce([42u8; 16]);
13921392
let expanded_key = chanmon_cfgs[1].keys_manager.get_inbound_payment_key();

lightning/src/ln/max_payment_path_len_tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
1313
use bitcoin::secp256k1::{Secp256k1, PublicKey};
1414
use crate::blinded_path::BlindedHop;
15-
use crate::blinded_path::payment::{BlindedPayInfo, BlindedPaymentPath, PaymentConstraints, PaymentContext, UnauthenticatedReceiveTlvs};
15+
use crate::blinded_path::payment::{BlindedPayInfo, BlindedPaymentPath, Bolt12RefundContext, PaymentConstraints, PaymentContext, UnauthenticatedReceiveTlvs};
1616
use crate::events::{Event, MessageSendEventsProvider};
1717
use crate::types::payment::PaymentSecret;
1818
use crate::ln::blinded_payment_tests::get_blinded_route_parameters;
@@ -165,7 +165,7 @@ fn one_hop_blinded_path_with_custom_tlv() {
165165
max_cltv_expiry: u32::max_value(),
166166
htlc_minimum_msat: chan_upd_1_2.htlc_minimum_msat,
167167
},
168-
payment_context: PaymentContext::unknown(),
168+
payment_context: PaymentContext::Bolt12Refund(Bolt12RefundContext {}),
169169
};
170170
let nonce = Nonce([42u8; 16]);
171171
let expanded_key = chanmon_cfgs[2].keys_manager.get_inbound_payment_key();

0 commit comments

Comments
 (0)