Skip to content

Commit 3a92c7b

Browse files
committed
Merge similar InvoiceBuilder impl blocks
This avoids needing to create additional macros when adding c_bindings support.
1 parent af318b6 commit 3a92c7b

File tree

1 file changed

+48
-52
lines changed

1 file changed

+48
-52
lines changed

lightning/src/offers/invoice.rs

Lines changed: 48 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,25 @@ impl<'a> InvoiceBuilder<'a, ExplicitSigningPubkey> {
200200

201201
Self::new(&refund.bytes, contents, ExplicitSigningPubkey {})
202202
}
203+
204+
/// Builds an unsigned [`Bolt12Invoice`] after checking for valid semantics. It can be signed by
205+
/// [`UnsignedBolt12Invoice::sign`].
206+
pub fn build(self) -> Result<UnsignedBolt12Invoice, Bolt12SemanticError> {
207+
#[cfg(feature = "std")] {
208+
if self.invoice.is_offer_or_refund_expired() {
209+
return Err(Bolt12SemanticError::AlreadyExpired);
210+
}
211+
}
212+
213+
#[cfg(not(feature = "std"))] {
214+
if self.invoice.is_offer_or_refund_expired_no_std(self.invoice.created_at()) {
215+
return Err(Bolt12SemanticError::AlreadyExpired);
216+
}
217+
}
218+
219+
let InvoiceBuilder { invreq_bytes, invoice, .. } = self;
220+
Ok(UnsignedBolt12Invoice::new(invreq_bytes, invoice))
221+
}
203222
}
204223

205224
impl<'a> InvoiceBuilder<'a, DerivedSigningPubkey> {
@@ -234,6 +253,35 @@ impl<'a> InvoiceBuilder<'a, DerivedSigningPubkey> {
234253

235254
Self::new(&refund.bytes, contents, DerivedSigningPubkey(keys))
236255
}
256+
257+
/// Builds a signed [`Bolt12Invoice`] after checking for valid semantics.
258+
pub fn build_and_sign<T: secp256k1::Signing>(
259+
self, secp_ctx: &Secp256k1<T>
260+
) -> Result<Bolt12Invoice, Bolt12SemanticError> {
261+
#[cfg(feature = "std")] {
262+
if self.invoice.is_offer_or_refund_expired() {
263+
return Err(Bolt12SemanticError::AlreadyExpired);
264+
}
265+
}
266+
267+
#[cfg(not(feature = "std"))] {
268+
if self.invoice.is_offer_or_refund_expired_no_std(self.invoice.created_at()) {
269+
return Err(Bolt12SemanticError::AlreadyExpired);
270+
}
271+
}
272+
273+
let InvoiceBuilder {
274+
invreq_bytes, invoice, signing_pubkey_strategy: DerivedSigningPubkey(keys)
275+
} = self;
276+
let unsigned_invoice = UnsignedBolt12Invoice::new(invreq_bytes, invoice);
277+
278+
let invoice = unsigned_invoice
279+
.sign::<_, Infallible>(
280+
|message| Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &keys))
281+
)
282+
.unwrap();
283+
Ok(invoice)
284+
}
237285
}
238286

239287
impl<'a, S: SigningPubkeyStrategy> InvoiceBuilder<'a, S> {
@@ -331,58 +379,6 @@ impl<'a, S: SigningPubkeyStrategy> InvoiceBuilder<'a, S> {
331379
}
332380
}
333381

334-
impl<'a> InvoiceBuilder<'a, ExplicitSigningPubkey> {
335-
/// Builds an unsigned [`Bolt12Invoice`] after checking for valid semantics. It can be signed by
336-
/// [`UnsignedBolt12Invoice::sign`].
337-
pub fn build(self) -> Result<UnsignedBolt12Invoice, Bolt12SemanticError> {
338-
#[cfg(feature = "std")] {
339-
if self.invoice.is_offer_or_refund_expired() {
340-
return Err(Bolt12SemanticError::AlreadyExpired);
341-
}
342-
}
343-
344-
#[cfg(not(feature = "std"))] {
345-
if self.invoice.is_offer_or_refund_expired_no_std(self.invoice.created_at()) {
346-
return Err(Bolt12SemanticError::AlreadyExpired);
347-
}
348-
}
349-
350-
let InvoiceBuilder { invreq_bytes, invoice, .. } = self;
351-
Ok(UnsignedBolt12Invoice::new(invreq_bytes, invoice))
352-
}
353-
}
354-
355-
impl<'a> InvoiceBuilder<'a, DerivedSigningPubkey> {
356-
/// Builds a signed [`Bolt12Invoice`] after checking for valid semantics.
357-
pub fn build_and_sign<T: secp256k1::Signing>(
358-
self, secp_ctx: &Secp256k1<T>
359-
) -> Result<Bolt12Invoice, Bolt12SemanticError> {
360-
#[cfg(feature = "std")] {
361-
if self.invoice.is_offer_or_refund_expired() {
362-
return Err(Bolt12SemanticError::AlreadyExpired);
363-
}
364-
}
365-
366-
#[cfg(not(feature = "std"))] {
367-
if self.invoice.is_offer_or_refund_expired_no_std(self.invoice.created_at()) {
368-
return Err(Bolt12SemanticError::AlreadyExpired);
369-
}
370-
}
371-
372-
let InvoiceBuilder {
373-
invreq_bytes, invoice, signing_pubkey_strategy: DerivedSigningPubkey(keys)
374-
} = self;
375-
let unsigned_invoice = UnsignedBolt12Invoice::new(invreq_bytes, invoice);
376-
377-
let invoice = unsigned_invoice
378-
.sign::<_, Infallible>(
379-
|message| Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &keys))
380-
)
381-
.unwrap();
382-
Ok(invoice)
383-
}
384-
}
385-
386382
/// A semantically valid [`Bolt12Invoice`] that hasn't been signed.
387383
///
388384
/// # Serialization

0 commit comments

Comments
 (0)