Skip to content

Commit 0f8f48a

Browse files
committed
Refactor handling of InvoiceRequest
In order to generate and InvoiceSent event, it would be cleaner to have one location where a Bolt12Invoice is successfully generated. Refactor the handling code to this end and clean-up line length by making some of the type conversions more streamlined.
1 parent 670b41a commit 0f8f48a

File tree

2 files changed

+34
-28
lines changed

2 files changed

+34
-28
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ use crate::ln::wire::Encode;
6161
use crate::offers::invoice::{BlindedPayInfo, Bolt12Invoice, DEFAULT_RELATIVE_EXPIRY, DerivedSigningPubkey, ExplicitSigningPubkey, InvoiceBuilder, UnsignedBolt12Invoice};
6262
use crate::offers::invoice_error::InvoiceError;
6363
use crate::offers::invoice_request::{DerivedPayerId, InvoiceRequestBuilder};
64-
use crate::offers::merkle::SignError;
6564
use crate::offers::offer::{Offer, OfferBuilder};
6665
use crate::offers::parse::Bolt12SemanticError;
6766
use crate::offers::refund::{Refund, RefundBuilder};
@@ -9451,7 +9450,7 @@ where
94519450
self.highest_seen_timestamp.load(Ordering::Acquire) as u64
94529451
);
94539452

9454-
if invoice_request.keys.is_some() {
9453+
let response = if invoice_request.keys.is_some() {
94559454
#[cfg(feature = "std")]
94569455
let builder = invoice_request.respond_using_derived_keys(
94579456
payment_paths, payment_hash
@@ -9460,42 +9459,35 @@ where
94609459
let builder = invoice_request.respond_using_derived_keys_no_std(
94619460
payment_paths, payment_hash, created_at
94629461
);
9463-
let builder: Result<InvoiceBuilder<DerivedSigningPubkey>, _> =
9464-
builder.map(|b| b.into());
9465-
match builder.and_then(|b| b.allow_mpp().build_and_sign(secp_ctx)) {
9466-
Ok(invoice) => Some(OffersMessage::Invoice(invoice)),
9467-
Err(error) => Some(OffersMessage::InvoiceError(error.into())),
9468-
}
9462+
builder
9463+
.map(InvoiceBuilder::<DerivedSigningPubkey>::from)
9464+
.and_then(|builder| builder.allow_mpp().build_and_sign(secp_ctx))
9465+
.map_err(InvoiceError::from)
94699466
} else {
94709467
#[cfg(feature = "std")]
94719468
let builder = invoice_request.respond_with(payment_paths, payment_hash);
94729469
#[cfg(not(feature = "std"))]
94739470
let builder = invoice_request.respond_with_no_std(
94749471
payment_paths, payment_hash, created_at
94759472
);
9476-
let builder: Result<InvoiceBuilder<ExplicitSigningPubkey>, _> =
9477-
builder.map(|b| b.into());
9478-
let response = builder.and_then(|builder| builder.allow_mpp().build())
9479-
.map_err(|e| OffersMessage::InvoiceError(e.into()))
9473+
builder
9474+
.map(InvoiceBuilder::<ExplicitSigningPubkey>::from)
9475+
.and_then(|builder| builder.allow_mpp().build())
9476+
.map_err(InvoiceError::from)
94809477
.and_then(|invoice| {
94819478
#[cfg(c_bindings)]
94829479
let mut invoice = invoice;
9483-
match invoice.sign(|invoice: &UnsignedBolt12Invoice|
9484-
self.node_signer.sign_bolt12_invoice(invoice)
9485-
) {
9486-
Ok(invoice) => Ok(OffersMessage::Invoice(invoice)),
9487-
Err(SignError::Signing) => Err(OffersMessage::InvoiceError(
9488-
InvoiceError::from_string("Failed signing invoice".to_string())
9489-
)),
9490-
Err(SignError::Verification(_)) => Err(OffersMessage::InvoiceError(
9491-
InvoiceError::from_string("Failed invoice signature verification".to_string())
9492-
)),
9493-
}
9494-
});
9495-
match response {
9496-
Ok(invoice) => Some(invoice),
9497-
Err(error) => Some(error),
9498-
}
9480+
invoice
9481+
.sign(|invoice: &UnsignedBolt12Invoice|
9482+
self.node_signer.sign_bolt12_invoice(invoice)
9483+
)
9484+
.map_err(InvoiceError::from)
9485+
})
9486+
};
9487+
9488+
match response {
9489+
Ok(invoice) => Some(OffersMessage::Invoice(invoice)),
9490+
Err(error) => Some(OffersMessage::InvoiceError(error.into())),
94999491
}
95009492
},
95019493
OffersMessage::Invoice(invoice) => {

lightning/src/offers/invoice_error.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
1212
use crate::io;
1313
use crate::ln::msgs::DecodeError;
14+
use crate::offers::merkle::SignError;
1415
use crate::offers::parse::Bolt12SemanticError;
1516
use crate::util::ser::{HighZeroBytesDroppedBigSize, Readable, WithoutLength, Writeable, Writer};
1617
use crate::util::string::UntrustedString;
@@ -112,6 +113,19 @@ impl From<Bolt12SemanticError> for InvoiceError {
112113
}
113114
}
114115

116+
impl From<SignError> for InvoiceError {
117+
fn from(error: SignError) -> Self {
118+
let message = match error {
119+
SignError::Signing => "Failed signing invoice",
120+
SignError::Verification(_) => "Failed invoice signature verification",
121+
};
122+
InvoiceError {
123+
erroneous_field: None,
124+
message: UntrustedString(message.to_string()),
125+
}
126+
}
127+
}
128+
115129
#[cfg(test)]
116130
mod tests {
117131
use super::{ErroneousField, InvoiceError};

0 commit comments

Comments
 (0)