Skip to content

Commit 7e085c5

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 59778da commit 7e085c5

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};
@@ -10356,7 +10355,7 @@ where
1035610355
self.highest_seen_timestamp.load(Ordering::Acquire) as u64
1035710356
);
1035810357

10359-
if invoice_request.keys.is_some() {
10358+
let response = if invoice_request.keys.is_some() {
1036010359
#[cfg(feature = "std")]
1036110360
let builder = invoice_request.respond_using_derived_keys(
1036210361
payment_paths, payment_hash
@@ -10365,42 +10364,35 @@ where
1036510364
let builder = invoice_request.respond_using_derived_keys_no_std(
1036610365
payment_paths, payment_hash, created_at
1036710366
);
10368-
let builder: Result<InvoiceBuilder<DerivedSigningPubkey>, _> =
10369-
builder.map(|b| b.into());
10370-
match builder.and_then(|b| b.allow_mpp().build_and_sign(secp_ctx)) {
10371-
Ok(invoice) => Some(OffersMessage::Invoice(invoice)),
10372-
Err(error) => Some(OffersMessage::InvoiceError(error.into())),
10373-
}
10367+
builder
10368+
.map(InvoiceBuilder::<DerivedSigningPubkey>::from)
10369+
.and_then(|builder| builder.allow_mpp().build_and_sign(secp_ctx))
10370+
.map_err(InvoiceError::from)
1037410371
} else {
1037510372
#[cfg(feature = "std")]
1037610373
let builder = invoice_request.respond_with(payment_paths, payment_hash);
1037710374
#[cfg(not(feature = "std"))]
1037810375
let builder = invoice_request.respond_with_no_std(
1037910376
payment_paths, payment_hash, created_at
1038010377
);
10381-
let builder: Result<InvoiceBuilder<ExplicitSigningPubkey>, _> =
10382-
builder.map(|b| b.into());
10383-
let response = builder.and_then(|builder| builder.allow_mpp().build())
10384-
.map_err(|e| OffersMessage::InvoiceError(e.into()))
10378+
builder
10379+
.map(InvoiceBuilder::<ExplicitSigningPubkey>::from)
10380+
.and_then(|builder| builder.allow_mpp().build())
10381+
.map_err(InvoiceError::from)
1038510382
.and_then(|invoice| {
1038610383
#[cfg(c_bindings)]
1038710384
let mut invoice = invoice;
10388-
match invoice.sign(|invoice: &UnsignedBolt12Invoice|
10389-
self.node_signer.sign_bolt12_invoice(invoice)
10390-
) {
10391-
Ok(invoice) => Ok(OffersMessage::Invoice(invoice)),
10392-
Err(SignError::Signing) => Err(OffersMessage::InvoiceError(
10393-
InvoiceError::from_string("Failed signing invoice".to_string())
10394-
)),
10395-
Err(SignError::Verification(_)) => Err(OffersMessage::InvoiceError(
10396-
InvoiceError::from_string("Failed invoice signature verification".to_string())
10397-
)),
10398-
}
10399-
});
10400-
match response {
10401-
Ok(invoice) => Some(invoice),
10402-
Err(error) => Some(error),
10403-
}
10385+
invoice
10386+
.sign(|invoice: &UnsignedBolt12Invoice|
10387+
self.node_signer.sign_bolt12_invoice(invoice)
10388+
)
10389+
.map_err(InvoiceError::from)
10390+
})
10391+
};
10392+
10393+
match response {
10394+
Ok(invoice) => Some(OffersMessage::Invoice(invoice)),
10395+
Err(error) => Some(OffersMessage::InvoiceError(error.into())),
1040410396
}
1040510397
},
1040610398
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;
@@ -113,6 +114,19 @@ impl From<Bolt12SemanticError> for InvoiceError {
113114
}
114115
}
115116

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

0 commit comments

Comments
 (0)