Skip to content

Commit 56f457c

Browse files
committed
DRY up OffersMessage::InvoiceRequest handling
1 parent 9598b07 commit 56f457c

File tree

1 file changed

+58
-66
lines changed

1 file changed

+58
-66
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 58 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -9106,77 +9106,69 @@ where
91069106
return Some(OffersMessage::InvoiceError(error.into()));
91079107
},
91089108
};
9109-
let relative_expiry = DEFAULT_RELATIVE_EXPIRY.as_secs() as u32;
91109109

9111-
match self.create_inbound_payment(Some(amount_msats), relative_expiry, None) {
9112-
Ok((payment_hash, payment_secret)) if invoice_request.keys.is_some() => {
9113-
let payment_paths = match self.create_blinded_payment_paths(
9114-
amount_msats, payment_secret
9115-
) {
9116-
Ok(payment_paths) => payment_paths,
9117-
Err(()) => {
9118-
let error = Bolt12SemanticError::MissingPaths;
9119-
return Some(OffersMessage::InvoiceError(error.into()));
9120-
},
9121-
};
9122-
#[cfg(not(feature = "no-std"))]
9123-
let builder = invoice_request.respond_using_derived_keys(
9124-
payment_paths, payment_hash
9125-
);
9126-
#[cfg(feature = "no-std")]
9127-
let created_at = Duration::from_secs(
9128-
self.highest_seen_timestamp.load(Ordering::Acquire) as u64
9129-
);
9130-
#[cfg(feature = "no-std")]
9131-
let builder = invoice_request.respond_using_derived_keys_no_std(
9132-
payment_paths, payment_hash, created_at
9133-
);
9134-
match builder.and_then(|b| b.allow_mpp().build_and_sign(secp_ctx)) {
9135-
Ok(invoice) => Some(OffersMessage::Invoice(invoice)),
9136-
Err(error) => Some(OffersMessage::InvoiceError(error.into())),
9137-
}
9110+
let relative_expiry = DEFAULT_RELATIVE_EXPIRY.as_secs() as u32;
9111+
let (payment_hash, payment_secret) = match self.create_inbound_payment(
9112+
Some(amount_msats), relative_expiry, None
9113+
) {
9114+
Ok((payment_hash, payment_secret)) => (payment_hash, payment_secret),
9115+
Err(()) => {
9116+
let error = Bolt12SemanticError::InvalidAmount;
9117+
return Some(OffersMessage::InvoiceError(error.into()));
91389118
},
9139-
Ok((payment_hash, payment_secret)) => {
9140-
let payment_paths = match self.create_blinded_payment_paths(
9141-
amount_msats, payment_secret
9142-
) {
9143-
Ok(payment_paths) => payment_paths,
9144-
Err(()) => {
9145-
let error = Bolt12SemanticError::MissingPaths;
9146-
return Some(OffersMessage::InvoiceError(error.into()));
9147-
},
9148-
};
9119+
};
91499120

9150-
#[cfg(not(feature = "no-std"))]
9151-
let builder = invoice_request.respond_with(payment_paths, payment_hash);
9152-
#[cfg(feature = "no-std")]
9153-
let created_at = Duration::from_secs(
9154-
self.highest_seen_timestamp.load(Ordering::Acquire) as u64
9155-
);
9156-
#[cfg(feature = "no-std")]
9157-
let builder = invoice_request.respond_with_no_std(
9158-
payment_paths, payment_hash, created_at
9159-
);
9160-
let response = builder.and_then(|builder| builder.allow_mpp().build())
9161-
.map_err(|e| OffersMessage::InvoiceError(e.into()))
9162-
.and_then(|invoice|
9163-
match invoice.sign(|invoice| self.node_signer.sign_bolt12_invoice(invoice)) {
9164-
Ok(invoice) => Ok(OffersMessage::Invoice(invoice)),
9165-
Err(SignError::Signing(())) => Err(OffersMessage::InvoiceError(
9166-
InvoiceError::from_string("Failed signing invoice".to_string())
9167-
)),
9168-
Err(SignError::Verification(_)) => Err(OffersMessage::InvoiceError(
9169-
InvoiceError::from_string("Failed invoice signature verification".to_string())
9170-
)),
9171-
});
9172-
match response {
9173-
Ok(invoice) => Some(invoice),
9174-
Err(error) => Some(error),
9175-
}
9176-
},
9121+
let payment_paths = match self.create_blinded_payment_paths(
9122+
amount_msats, payment_secret
9123+
) {
9124+
Ok(payment_paths) => payment_paths,
91779125
Err(()) => {
9178-
Some(OffersMessage::InvoiceError(Bolt12SemanticError::InvalidAmount.into()))
9126+
let error = Bolt12SemanticError::MissingPaths;
9127+
return Some(OffersMessage::InvoiceError(error.into()));
91799128
},
9129+
};
9130+
9131+
#[cfg(feature = "no-std")]
9132+
let created_at = Duration::from_secs(
9133+
self.highest_seen_timestamp.load(Ordering::Acquire) as u64
9134+
);
9135+
9136+
if invoice_request.keys.is_some() {
9137+
#[cfg(not(feature = "no-std"))]
9138+
let builder = invoice_request.respond_using_derived_keys(
9139+
payment_paths, payment_hash
9140+
);
9141+
#[cfg(feature = "no-std")]
9142+
let builder = invoice_request.respond_using_derived_keys_no_std(
9143+
payment_paths, payment_hash, created_at
9144+
);
9145+
match builder.and_then(|b| b.allow_mpp().build_and_sign(secp_ctx)) {
9146+
Ok(invoice) => Some(OffersMessage::Invoice(invoice)),
9147+
Err(error) => Some(OffersMessage::InvoiceError(error.into())),
9148+
}
9149+
} else {
9150+
#[cfg(not(feature = "no-std"))]
9151+
let builder = invoice_request.respond_with(payment_paths, payment_hash);
9152+
#[cfg(feature = "no-std")]
9153+
let builder = invoice_request.respond_with_no_std(
9154+
payment_paths, payment_hash, created_at
9155+
);
9156+
let response = builder.and_then(|builder| builder.allow_mpp().build())
9157+
.map_err(|e| OffersMessage::InvoiceError(e.into()))
9158+
.and_then(|invoice|
9159+
match invoice.sign(|invoice| self.node_signer.sign_bolt12_invoice(invoice)) {
9160+
Ok(invoice) => Ok(OffersMessage::Invoice(invoice)),
9161+
Err(SignError::Signing(())) => Err(OffersMessage::InvoiceError(
9162+
InvoiceError::from_string("Failed signing invoice".to_string())
9163+
)),
9164+
Err(SignError::Verification(_)) => Err(OffersMessage::InvoiceError(
9165+
InvoiceError::from_string("Failed invoice signature verification".to_string())
9166+
)),
9167+
});
9168+
match response {
9169+
Ok(invoice) => Some(invoice),
9170+
Err(error) => Some(error),
9171+
}
91809172
}
91819173
},
91829174
OffersMessage::Invoice(invoice) => {

0 commit comments

Comments
 (0)