From: Jeffrey Czyz Date: Sun, 25 Feb 2024 17:32:02 +0000 (-0600) Subject: Merge similar InvoiceBuilder impl blocks X-Git-Tag: v0.0.123-beta~46^2~5 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=3a92c7b08a96167355da85f4daac76cd1251684e;p=rust-lightning Merge similar InvoiceBuilder impl blocks This avoids needing to create additional macros when adding c_bindings support. --- diff --git a/lightning/src/offers/invoice.rs b/lightning/src/offers/invoice.rs index da88be154..9e5922692 100644 --- a/lightning/src/offers/invoice.rs +++ b/lightning/src/offers/invoice.rs @@ -200,6 +200,25 @@ impl<'a> InvoiceBuilder<'a, ExplicitSigningPubkey> { Self::new(&refund.bytes, contents, ExplicitSigningPubkey {}) } + + /// Builds an unsigned [`Bolt12Invoice`] after checking for valid semantics. It can be signed by + /// [`UnsignedBolt12Invoice::sign`]. + pub fn build(self) -> Result { + #[cfg(feature = "std")] { + if self.invoice.is_offer_or_refund_expired() { + return Err(Bolt12SemanticError::AlreadyExpired); + } + } + + #[cfg(not(feature = "std"))] { + if self.invoice.is_offer_or_refund_expired_no_std(self.invoice.created_at()) { + return Err(Bolt12SemanticError::AlreadyExpired); + } + } + + let InvoiceBuilder { invreq_bytes, invoice, .. } = self; + Ok(UnsignedBolt12Invoice::new(invreq_bytes, invoice)) + } } impl<'a> InvoiceBuilder<'a, DerivedSigningPubkey> { @@ -234,6 +253,35 @@ impl<'a> InvoiceBuilder<'a, DerivedSigningPubkey> { Self::new(&refund.bytes, contents, DerivedSigningPubkey(keys)) } + + /// Builds a signed [`Bolt12Invoice`] after checking for valid semantics. + pub fn build_and_sign( + self, secp_ctx: &Secp256k1 + ) -> Result { + #[cfg(feature = "std")] { + if self.invoice.is_offer_or_refund_expired() { + return Err(Bolt12SemanticError::AlreadyExpired); + } + } + + #[cfg(not(feature = "std"))] { + if self.invoice.is_offer_or_refund_expired_no_std(self.invoice.created_at()) { + return Err(Bolt12SemanticError::AlreadyExpired); + } + } + + let InvoiceBuilder { + invreq_bytes, invoice, signing_pubkey_strategy: DerivedSigningPubkey(keys) + } = self; + let unsigned_invoice = UnsignedBolt12Invoice::new(invreq_bytes, invoice); + + let invoice = unsigned_invoice + .sign::<_, Infallible>( + |message| Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &keys)) + ) + .unwrap(); + Ok(invoice) + } } impl<'a, S: SigningPubkeyStrategy> InvoiceBuilder<'a, S> { @@ -331,58 +379,6 @@ impl<'a, S: SigningPubkeyStrategy> InvoiceBuilder<'a, S> { } } -impl<'a> InvoiceBuilder<'a, ExplicitSigningPubkey> { - /// Builds an unsigned [`Bolt12Invoice`] after checking for valid semantics. It can be signed by - /// [`UnsignedBolt12Invoice::sign`]. - pub fn build(self) -> Result { - #[cfg(feature = "std")] { - if self.invoice.is_offer_or_refund_expired() { - return Err(Bolt12SemanticError::AlreadyExpired); - } - } - - #[cfg(not(feature = "std"))] { - if self.invoice.is_offer_or_refund_expired_no_std(self.invoice.created_at()) { - return Err(Bolt12SemanticError::AlreadyExpired); - } - } - - let InvoiceBuilder { invreq_bytes, invoice, .. } = self; - Ok(UnsignedBolt12Invoice::new(invreq_bytes, invoice)) - } -} - -impl<'a> InvoiceBuilder<'a, DerivedSigningPubkey> { - /// Builds a signed [`Bolt12Invoice`] after checking for valid semantics. - pub fn build_and_sign( - self, secp_ctx: &Secp256k1 - ) -> Result { - #[cfg(feature = "std")] { - if self.invoice.is_offer_or_refund_expired() { - return Err(Bolt12SemanticError::AlreadyExpired); - } - } - - #[cfg(not(feature = "std"))] { - if self.invoice.is_offer_or_refund_expired_no_std(self.invoice.created_at()) { - return Err(Bolt12SemanticError::AlreadyExpired); - } - } - - let InvoiceBuilder { - invreq_bytes, invoice, signing_pubkey_strategy: DerivedSigningPubkey(keys) - } = self; - let unsigned_invoice = UnsignedBolt12Invoice::new(invreq_bytes, invoice); - - let invoice = unsigned_invoice - .sign::<_, Infallible>( - |message| Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &keys)) - ) - .unwrap(); - Ok(invoice) - } -} - /// A semantically valid [`Bolt12Invoice`] that hasn't been signed. /// /// # Serialization