Skip to content

Commit 16ee240

Browse files
committed
DRY up OffersMessage::InvoiceRequest handling
1 parent 827833c commit 16ee240

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
@@ -9174,77 +9174,69 @@ where
91749174
return Some(OffersMessage::InvoiceError(error.into()));
91759175
},
91769176
};
9177-
let relative_expiry = DEFAULT_RELATIVE_EXPIRY.as_secs() as u32;
91789177

9179-
match self.create_inbound_payment(Some(amount_msats), relative_expiry, None) {
9180-
Ok((payment_hash, payment_secret)) if invoice_request.keys.is_some() => {
9181-
let payment_paths = match self.create_blinded_payment_paths(
9182-
amount_msats, payment_secret
9183-
) {
9184-
Ok(payment_paths) => payment_paths,
9185-
Err(()) => {
9186-
let error = Bolt12SemanticError::MissingPaths;
9187-
return Some(OffersMessage::InvoiceError(error.into()));
9188-
},
9189-
};
9190-
#[cfg(not(feature = "no-std"))]
9191-
let builder = invoice_request.respond_using_derived_keys(
9192-
payment_paths, payment_hash
9193-
);
9194-
#[cfg(feature = "no-std")]
9195-
let created_at = Duration::from_secs(
9196-
self.highest_seen_timestamp.load(Ordering::Acquire) as u64
9197-
);
9198-
#[cfg(feature = "no-std")]
9199-
let builder = invoice_request.respond_using_derived_keys_no_std(
9200-
payment_paths, payment_hash, created_at
9201-
);
9202-
match builder.and_then(|b| b.allow_mpp().build_and_sign(secp_ctx)) {
9203-
Ok(invoice) => Some(OffersMessage::Invoice(invoice)),
9204-
Err(error) => Some(OffersMessage::InvoiceError(error.into())),
9205-
}
9178+
let relative_expiry = DEFAULT_RELATIVE_EXPIRY.as_secs() as u32;
9179+
let (payment_hash, payment_secret) = match self.create_inbound_payment(
9180+
Some(amount_msats), relative_expiry, None
9181+
) {
9182+
Ok((payment_hash, payment_secret)) => (payment_hash, payment_secret),
9183+
Err(()) => {
9184+
let error = Bolt12SemanticError::InvalidAmount;
9185+
return Some(OffersMessage::InvoiceError(error.into()));
92069186
},
9207-
Ok((payment_hash, payment_secret)) => {
9208-
let payment_paths = match self.create_blinded_payment_paths(
9209-
amount_msats, payment_secret
9210-
) {
9211-
Ok(payment_paths) => payment_paths,
9212-
Err(()) => {
9213-
let error = Bolt12SemanticError::MissingPaths;
9214-
return Some(OffersMessage::InvoiceError(error.into()));
9215-
},
9216-
};
9187+
};
92179188

9218-
#[cfg(not(feature = "no-std"))]
9219-
let builder = invoice_request.respond_with(payment_paths, payment_hash);
9220-
#[cfg(feature = "no-std")]
9221-
let created_at = Duration::from_secs(
9222-
self.highest_seen_timestamp.load(Ordering::Acquire) as u64
9223-
);
9224-
#[cfg(feature = "no-std")]
9225-
let builder = invoice_request.respond_with_no_std(
9226-
payment_paths, payment_hash, created_at
9227-
);
9228-
let response = builder.and_then(|builder| builder.allow_mpp().build())
9229-
.map_err(|e| OffersMessage::InvoiceError(e.into()))
9230-
.and_then(|invoice|
9231-
match invoice.sign(|invoice| self.node_signer.sign_bolt12_invoice(invoice)) {
9232-
Ok(invoice) => Ok(OffersMessage::Invoice(invoice)),
9233-
Err(SignError::Signing(())) => Err(OffersMessage::InvoiceError(
9234-
InvoiceError::from_string("Failed signing invoice".to_string())
9235-
)),
9236-
Err(SignError::Verification(_)) => Err(OffersMessage::InvoiceError(
9237-
InvoiceError::from_string("Failed invoice signature verification".to_string())
9238-
)),
9239-
});
9240-
match response {
9241-
Ok(invoice) => Some(invoice),
9242-
Err(error) => Some(error),
9243-
}
9244-
},
9189+
let payment_paths = match self.create_blinded_payment_paths(
9190+
amount_msats, payment_secret
9191+
) {
9192+
Ok(payment_paths) => payment_paths,
92459193
Err(()) => {
9246-
Some(OffersMessage::InvoiceError(Bolt12SemanticError::InvalidAmount.into()))
9194+
let error = Bolt12SemanticError::MissingPaths;
9195+
return Some(OffersMessage::InvoiceError(error.into()));
92479196
},
9197+
};
9198+
9199+
#[cfg(feature = "no-std")]
9200+
let created_at = Duration::from_secs(
9201+
self.highest_seen_timestamp.load(Ordering::Acquire) as u64
9202+
);
9203+
9204+
if invoice_request.keys.is_some() {
9205+
#[cfg(not(feature = "no-std"))]
9206+
let builder = invoice_request.respond_using_derived_keys(
9207+
payment_paths, payment_hash
9208+
);
9209+
#[cfg(feature = "no-std")]
9210+
let builder = invoice_request.respond_using_derived_keys_no_std(
9211+
payment_paths, payment_hash, created_at
9212+
);
9213+
match builder.and_then(|b| b.allow_mpp().build_and_sign(secp_ctx)) {
9214+
Ok(invoice) => Some(OffersMessage::Invoice(invoice)),
9215+
Err(error) => Some(OffersMessage::InvoiceError(error.into())),
9216+
}
9217+
} else {
9218+
#[cfg(not(feature = "no-std"))]
9219+
let builder = invoice_request.respond_with(payment_paths, payment_hash);
9220+
#[cfg(feature = "no-std")]
9221+
let builder = invoice_request.respond_with_no_std(
9222+
payment_paths, payment_hash, created_at
9223+
);
9224+
let response = builder.and_then(|builder| builder.allow_mpp().build())
9225+
.map_err(|e| OffersMessage::InvoiceError(e.into()))
9226+
.and_then(|invoice|
9227+
match invoice.sign(|invoice| self.node_signer.sign_bolt12_invoice(invoice)) {
9228+
Ok(invoice) => Ok(OffersMessage::Invoice(invoice)),
9229+
Err(SignError::Signing(())) => Err(OffersMessage::InvoiceError(
9230+
InvoiceError::from_string("Failed signing invoice".to_string())
9231+
)),
9232+
Err(SignError::Verification(_)) => Err(OffersMessage::InvoiceError(
9233+
InvoiceError::from_string("Failed invoice signature verification".to_string())
9234+
)),
9235+
});
9236+
match response {
9237+
Ok(invoice) => Some(invoice),
9238+
Err(error) => Some(error),
9239+
}
92489240
}
92499241
},
92509242
OffersMessage::Invoice(invoice) => {

0 commit comments

Comments
 (0)