Skip to content

Commit fcd8a11

Browse files
committed
DRY up OffersMessage::InvoiceRequest handling
1 parent 98763ef commit fcd8a11

File tree

1 file changed

+58
-66
lines changed

1 file changed

+58
-66
lines changed

lightning/src/ln/channelmanager.rs

+58-66
Original file line numberDiff line numberDiff line change
@@ -9091,77 +9091,69 @@ where
90919091
return Some(OffersMessage::InvoiceError(error.into()));
90929092
},
90939093
};
9094-
let relative_expiry = DEFAULT_RELATIVE_EXPIRY.as_secs() as u32;
90959094

9096-
match self.create_inbound_payment(Some(amount_msats), relative_expiry, None) {
9097-
Ok((payment_hash, payment_secret)) if invoice_request.keys.is_some() => {
9098-
let payment_paths = match self.create_blinded_payment_paths(
9099-
amount_msats, payment_secret
9100-
) {
9101-
Ok(payment_paths) => payment_paths,
9102-
Err(()) => {
9103-
let error = Bolt12SemanticError::MissingPaths;
9104-
return Some(OffersMessage::InvoiceError(error.into()));
9105-
},
9106-
};
9107-
#[cfg(not(feature = "no-std"))]
9108-
let builder = invoice_request.respond_using_derived_keys(
9109-
payment_paths, payment_hash
9110-
);
9111-
#[cfg(feature = "no-std")]
9112-
let created_at = Duration::from_secs(
9113-
self.highest_seen_timestamp.load(Ordering::Acquire) as u64
9114-
);
9115-
#[cfg(feature = "no-std")]
9116-
let builder = invoice_request.respond_using_derived_keys_no_std(
9117-
payment_paths, payment_hash, created_at
9118-
);
9119-
match builder.and_then(|b| b.allow_mpp().build_and_sign(secp_ctx)) {
9120-
Ok(invoice) => Some(OffersMessage::Invoice(invoice)),
9121-
Err(error) => Some(OffersMessage::InvoiceError(error.into())),
9122-
}
9095+
let relative_expiry = DEFAULT_RELATIVE_EXPIRY.as_secs() as u32;
9096+
let (payment_hash, payment_secret) = match self.create_inbound_payment(
9097+
Some(amount_msats), relative_expiry, None
9098+
) {
9099+
Ok((payment_hash, payment_secret)) => (payment_hash, payment_secret),
9100+
Err(()) => {
9101+
let error = Bolt12SemanticError::InvalidAmount;
9102+
return Some(OffersMessage::InvoiceError(error.into()));
91239103
},
9124-
Ok((payment_hash, payment_secret)) => {
9125-
let payment_paths = match self.create_blinded_payment_paths(
9126-
amount_msats, payment_secret
9127-
) {
9128-
Ok(payment_paths) => payment_paths,
9129-
Err(()) => {
9130-
let error = Bolt12SemanticError::MissingPaths;
9131-
return Some(OffersMessage::InvoiceError(error.into()));
9132-
},
9133-
};
9104+
};
91349105

9135-
#[cfg(not(feature = "no-std"))]
9136-
let builder = invoice_request.respond_with(payment_paths, payment_hash);
9137-
#[cfg(feature = "no-std")]
9138-
let created_at = Duration::from_secs(
9139-
self.highest_seen_timestamp.load(Ordering::Acquire) as u64
9140-
);
9141-
#[cfg(feature = "no-std")]
9142-
let builder = invoice_request.respond_with_no_std(
9143-
payment_paths, payment_hash, created_at
9144-
);
9145-
let response = builder.and_then(|builder| builder.allow_mpp().build())
9146-
.map_err(|e| OffersMessage::InvoiceError(e.into()))
9147-
.and_then(|invoice|
9148-
match invoice.sign(|invoice| self.node_signer.sign_bolt12_invoice(invoice)) {
9149-
Ok(invoice) => Ok(OffersMessage::Invoice(invoice)),
9150-
Err(SignError::Signing(())) => Err(OffersMessage::InvoiceError(
9151-
InvoiceError::from_string("Failed signing invoice".to_string())
9152-
)),
9153-
Err(SignError::Verification(_)) => Err(OffersMessage::InvoiceError(
9154-
InvoiceError::from_string("Failed invoice signature verification".to_string())
9155-
)),
9156-
});
9157-
match response {
9158-
Ok(invoice) => Some(invoice),
9159-
Err(error) => Some(error),
9160-
}
9161-
},
9106+
let payment_paths = match self.create_blinded_payment_paths(
9107+
amount_msats, payment_secret
9108+
) {
9109+
Ok(payment_paths) => payment_paths,
91629110
Err(()) => {
9163-
Some(OffersMessage::InvoiceError(Bolt12SemanticError::InvalidAmount.into()))
9111+
let error = Bolt12SemanticError::MissingPaths;
9112+
return Some(OffersMessage::InvoiceError(error.into()));
91649113
},
9114+
};
9115+
9116+
#[cfg(feature = "no-std")]
9117+
let created_at = Duration::from_secs(
9118+
self.highest_seen_timestamp.load(Ordering::Acquire) as u64
9119+
);
9120+
9121+
if invoice_request.keys.is_some() {
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 builder = invoice_request.respond_using_derived_keys_no_std(
9128+
payment_paths, payment_hash, created_at
9129+
);
9130+
match builder.and_then(|b| b.allow_mpp().build_and_sign(secp_ctx)) {
9131+
Ok(invoice) => Some(OffersMessage::Invoice(invoice)),
9132+
Err(error) => Some(OffersMessage::InvoiceError(error.into())),
9133+
}
9134+
} else {
9135+
#[cfg(not(feature = "no-std"))]
9136+
let builder = invoice_request.respond_with(payment_paths, payment_hash);
9137+
#[cfg(feature = "no-std")]
9138+
let builder = invoice_request.respond_with_no_std(
9139+
payment_paths, payment_hash, created_at
9140+
);
9141+
let response = builder.and_then(|builder| builder.allow_mpp().build())
9142+
.map_err(|e| OffersMessage::InvoiceError(e.into()))
9143+
.and_then(|invoice|
9144+
match invoice.sign(|invoice| self.node_signer.sign_bolt12_invoice(invoice)) {
9145+
Ok(invoice) => Ok(OffersMessage::Invoice(invoice)),
9146+
Err(SignError::Signing(())) => Err(OffersMessage::InvoiceError(
9147+
InvoiceError::from_string("Failed signing invoice".to_string())
9148+
)),
9149+
Err(SignError::Verification(_)) => Err(OffersMessage::InvoiceError(
9150+
InvoiceError::from_string("Failed invoice signature verification".to_string())
9151+
)),
9152+
});
9153+
match response {
9154+
Ok(invoice) => Some(invoice),
9155+
Err(error) => Some(error),
9156+
}
91659157
}
91669158
},
91679159
OffersMessage::Invoice(invoice) => {

0 commit comments

Comments
 (0)