Skip to content

Commit 0869721

Browse files
f reworking PR by allowing passing an wrapper enum
Signed-off-by: Vincenzo Palazzo <[email protected]>
1 parent 99b4167 commit 0869721

File tree

3 files changed

+51
-13
lines changed

3 files changed

+51
-13
lines changed

lightning/src/events/mod.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ pub enum Event {
955955
/// payment hash. A third party can verify that the payment was made by
956956
/// showing the invoice and confirming that the payment hash matches
957957
/// the hash of the payment preimage.
958-
bolt12_invoice: Option<Bolt12Invoice>,
958+
bolt12_invoice: Option<PaidInvoice>,
959959
},
960960
/// Indicates an outbound payment failed. Individual [`Event::PaymentPathFailed`] events
961961
/// provide failure information for each path attempt in the payment, including retries.
@@ -2449,3 +2449,27 @@ impl<T: EventHandler> EventHandler for Arc<T> {
24492449
self.deref().handle_event(event)
24502450
}
24512451
}
2452+
2453+
/// Wrapper time to move the bolt12 invoice and the static
2454+
/// invoice across the same event as a unique type.
2455+
#[derive(Clone, Debug, PartialEq, Eq)]
2456+
pub enum PaidInvoice {
2457+
/// Bolt12 invoice
2458+
Bolt12Invoice(crate::offers::invoice::Bolt12Invoice),
2459+
#[cfg(async_payments)]
2460+
/// Static invoice
2461+
StaticInvoice(crate::offers::static_invoice::StaticInvoice),
2462+
}
2463+
2464+
#[cfg(not(async_payments))]
2465+
impl_writeable_tlv_based_enum_legacy!(PaidInvoice,
2466+
;
2467+
(0, Bolt12Invoice),
2468+
);
2469+
2470+
#[cfg(async_payments)]
2471+
impl_writeable_tlv_based_enum_legacy!(PaidInvoice,
2472+
;
2473+
(0, Bolt12Invoice),
2474+
(2, StaticInvoice)
2475+
);

lightning/src/ln/outbound_payment.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ use bitcoin::secp256k1::{self, Secp256k1, SecretKey};
1515
use lightning_invoice::Bolt11Invoice;
1616

1717
use crate::blinded_path::{IntroductionNode, NodeIdLookUp};
18-
use crate::events::{self, PaymentFailureReason};
18+
use crate::events::{self, PaymentFailureReason, PaidInvoice};
19+
use crate::offers::static_invoice::StaticInvoice;
1920
use crate::types::payment::{PaymentHash, PaymentPreimage, PaymentSecret};
2021
use crate::ln::channel_state::ChannelDetails;
2122
use crate::ln::channelmanager::{EventCompletionAction, HTLCSource, PaymentId};
@@ -34,10 +35,7 @@ use crate::util::time::Instant;
3435
use crate::util::ser::ReadableArgs;
3536

3637
#[cfg(async_payments)]
37-
use {
38-
crate::offers::invoice::{DerivedSigningPubkey, InvoiceBuilder},
39-
crate::offers::static_invoice::StaticInvoice,
40-
};
38+
use crate::offers::invoice::{DerivedSigningPubkey, InvoiceBuilder};
4139

4240
use core::fmt::{self, Display, Formatter};
4341
use core::ops::Deref;
@@ -108,7 +106,7 @@ pub(crate) enum PendingOutboundPayment {
108106
invoice_request: Option<InvoiceRequest>,
109107
// Storing the bolt12 invoice here to allow Proof of Payment after
110108
// the payment is made.
111-
bolt12_invoice: Option<Bolt12Invoice>,
109+
bolt12_invoice: Option<PaidInvoice>,
112110
custom_tlvs: Vec<(u64, Vec<u8>)>,
113111
pending_amt_msat: u64,
114112
/// Used to track the fee paid. Present iff the payment was serialized on 0.0.103+.
@@ -158,7 +156,7 @@ impl_writeable_tlv_based!(RetryableInvoiceRequest, {
158156
});
159157

160158
impl PendingOutboundPayment {
161-
fn bolt12_invoice(&self) -> Option<&Bolt12Invoice> {
159+
fn bolt12_invoice(&self) -> Option<&PaidInvoice> {
162160
match self {
163161
PendingOutboundPayment::Retryable { bolt12_invoice, .. } => bolt12_invoice.as_ref(),
164162
_ => None,
@@ -967,8 +965,9 @@ impl OutboundPayments {
967965
if let Some(max_fee_msat) = params_config.max_total_routing_fee_msat {
968966
route_params.max_total_routing_fee_msat = Some(max_fee_msat);
969967
}
968+
let invoice = PaidInvoice::Bolt12Invoice(invoice.clone());
970969
self.send_payment_for_bolt12_invoice_internal(
971-
payment_id, payment_hash, None, None, Some(invoice), route_params, retry_strategy, router, first_hops,
970+
payment_id, payment_hash, None, None, Some(&invoice), route_params, retry_strategy, router, first_hops,
972971
inflight_htlcs, entropy_source, node_signer, node_id_lookup, secp_ctx, best_block_height,
973972
logger, pending_events, send_payment_along_path
974973
)
@@ -979,7 +978,7 @@ impl OutboundPayments {
979978
>(
980979
&self, payment_id: PaymentId, payment_hash: PaymentHash,
981980
keysend_preimage: Option<PaymentPreimage>, invoice_request: Option<&InvoiceRequest>,
982-
bolt12_invoice: Option<&Bolt12Invoice>,
981+
bolt12_invoice: Option<&PaidInvoice>,
983982
mut route_params: RouteParameters, retry_strategy: Retry, router: &R,
984983
first_hops: Vec<ChannelDetails>, inflight_htlcs: IH, entropy_source: &ES, node_signer: &NS,
985984
node_id_lookup: &NL, secp_ctx: &Secp256k1<secp256k1::All>, best_block_height: u32, logger: &L,
@@ -1726,7 +1725,7 @@ impl OutboundPayments {
17261725
&self, payment_hash: PaymentHash, recipient_onion: RecipientOnionFields, payment_id: PaymentId,
17271726
keysend_preimage: Option<PaymentPreimage>, route: &Route, retry_strategy: Option<Retry>,
17281727
payment_params: Option<PaymentParameters>, entropy_source: &ES, best_block_height: u32,
1729-
bolt12_invoice: Option<Bolt12Invoice>
1728+
bolt12_invoice: Option<PaidInvoice>
17301729
) -> Result<Vec<[u8; 32]>, PaymentSendFailure> where ES::Target: EntropySource {
17311730
let mut pending_outbounds = self.pending_outbound_payments.lock().unwrap();
17321731
match pending_outbounds.entry(payment_id) {
@@ -1745,7 +1744,7 @@ impl OutboundPayments {
17451744
fn create_pending_payment<ES: Deref>(
17461745
payment_hash: PaymentHash, recipient_onion: RecipientOnionFields,
17471746
keysend_preimage: Option<PaymentPreimage>, invoice_request: Option<InvoiceRequest>,
1748-
bolt12_invoice: Option<Bolt12Invoice>, route: &Route, retry_strategy: Option<Retry>,
1747+
bolt12_invoice: Option<PaidInvoice>, route: &Route, retry_strategy: Option<Retry>,
17491748
payment_params: Option<PaymentParameters>, entropy_source: &ES, best_block_height: u32
17501749
) -> (PendingOutboundPayment, Vec<[u8; 32]>)
17511750
where

lightning/src/offers/static_invoice.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use crate::offers::offer::{
3333
};
3434
use crate::offers::parse::{Bolt12ParseError, Bolt12SemanticError, ParsedMessage};
3535
use crate::types::features::{Bolt12InvoiceFeatures, OfferFeatures};
36-
use crate::util::ser::{CursorReadable, Iterable, WithoutLength, Writeable, Writer};
36+
use crate::util::ser::{CursorReadable, Iterable, Readable, WithoutLength, Writeable, Writer};
3737
use crate::util::string::PrintableString;
3838
use bitcoin::address::Address;
3939
use bitcoin::constants::ChainHash;
@@ -70,6 +70,14 @@ pub struct StaticInvoice {
7070
signature: Signature,
7171
}
7272

73+
impl PartialEq for StaticInvoice {
74+
fn eq(&self, other: &Self) -> bool {
75+
self.bytes.eq(&other.bytes)
76+
}
77+
}
78+
79+
impl Eq for StaticInvoice {}
80+
7381
/// The contents of a [`StaticInvoice`] for responding to an [`Offer`].
7482
///
7583
/// [`Offer`]: crate::offers::offer::Offer
@@ -535,6 +543,13 @@ impl Writeable for StaticInvoice {
535543
}
536544
}
537545

546+
impl Readable for StaticInvoice {
547+
fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
548+
let bytes: WithoutLength<Vec<u8>> = Readable::read(reader)?;
549+
Self::try_from(bytes.0).map_err(|_| DecodeError::InvalidValue)
550+
}
551+
}
552+
538553
impl TryFrom<Vec<u8>> for StaticInvoice {
539554
type Error = Bolt12ParseError;
540555

0 commit comments

Comments
 (0)