2323//! use bitcoin::hashes::Hash;
2424//! use bitcoin::secp256k1::{KeyPair, PublicKey, Secp256k1, SecretKey};
2525//! use core::convert::{Infallible, TryFrom};
26+ //! use lightning::offers::invoice::UnsignedBolt12Invoice;
2627//! use lightning::offers::invoice_request::InvoiceRequest;
2728//! use lightning::offers::refund::Refund;
2829//! use lightning::util::ser::Writeable;
5758//! .allow_mpp()
5859//! .fallback_v0_p2wpkh(&wpubkey_hash)
5960//! .build()?
60- //! .sign:: <_, Infallible>(
61- //! |message| Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &keys))
62- //! )
61+ //! .sign(|message: &UnsignedBolt12Invoice| -> Result <_, Infallible> {
62+ //! Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &keys))
63+ //! } )
6364//! .expect("failed verifying signature")
6465//! .write(&mut buffer)
6566//! .unwrap();
9091//! .allow_mpp()
9192//! .fallback_v0_p2wpkh(&wpubkey_hash)
9293//! .build()?
93- //! .sign:: <_, Infallible>(
94- //! |message| Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &keys))
95- //! )
94+ //! .sign(|message: &UnsignedBolt12Invoice| -> Result <_, Infallible> {
95+ //! Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &keys))
96+ //! } )
9697//! .expect("failed verifying signature")
9798//! .write(&mut buffer)
9899//! .unwrap();
@@ -119,7 +120,7 @@ use crate::ln::features::{BlindedHopFeatures, Bolt12InvoiceFeatures, InvoiceRequ
119120use crate :: ln:: inbound_payment:: ExpandedKey ;
120121use crate :: ln:: msgs:: DecodeError ;
121122use crate :: offers:: invoice_request:: { INVOICE_REQUEST_PAYER_ID_TYPE , INVOICE_REQUEST_TYPES , IV_BYTES as INVOICE_REQUEST_IV_BYTES , InvoiceRequest , InvoiceRequestContents , InvoiceRequestTlvStream , InvoiceRequestTlvStreamRef } ;
122- use crate :: offers:: merkle:: { SignError , SignatureTlvStream , SignatureTlvStreamRef , TaggedHash , TlvStream , WithoutSignatures , self } ;
123+ use crate :: offers:: merkle:: { SignError , SignFn , SignatureTlvStream , SignatureTlvStreamRef , TaggedHash , TlvStream , WithoutSignatures , self } ;
123124use crate :: offers:: offer:: { Amount , OFFER_TYPES , OfferTlvStream , OfferTlvStreamRef , Quantity } ;
124125use crate :: offers:: parse:: { Bolt12ParseError , Bolt12SemanticError , ParsedMessage } ;
125126use crate :: offers:: payer:: { PAYER_METADATA_TYPE , PayerTlvStream , PayerTlvStreamRef } ;
@@ -324,9 +325,9 @@ macro_rules! invoice_derived_signing_pubkey_builder_methods { ($self: ident, $se
324325 let mut unsigned_invoice = UnsignedBolt12Invoice :: new( invreq_bytes, invoice. clone( ) ) ;
325326
326327 let invoice = unsigned_invoice
327- . sign:: <_, Infallible >(
328- |message| Ok ( secp_ctx. sign_schnorr_no_aux_rand( message. as_ref( ) . as_digest( ) , & keys) )
329- )
328+ . sign( |message : & UnsignedBolt12Invoice | -> Result <_, Infallible > {
329+ Ok ( secp_ctx. sign_schnorr_no_aux_rand( message. as_ref( ) . as_digest( ) , & keys) )
330+ } )
330331 . unwrap( ) ;
331332 Ok ( invoice)
332333 }
@@ -507,6 +508,37 @@ pub struct UnsignedBolt12Invoice {
507508 tagged_hash : TaggedHash ,
508509}
509510
511+ /// A function for signing an [`UnsignedBolt12Invoice`].
512+ pub trait SignBolt12InvoiceFn {
513+ /// Error type returned by the function.
514+ type Error ;
515+
516+ /// Signs a [`TaggedHash`] computed over the merkle root of `message`'s TLV stream.
517+ fn sign_invoice ( & self , message : & UnsignedBolt12Invoice ) -> Result < Signature , Self :: Error > ;
518+ }
519+
520+ impl < F , E > SignBolt12InvoiceFn for F
521+ where
522+ F : Fn ( & UnsignedBolt12Invoice ) -> Result < Signature , E > ,
523+ {
524+ type Error = E ;
525+
526+ fn sign_invoice ( & self , message : & UnsignedBolt12Invoice ) -> Result < Signature , E > {
527+ self ( message)
528+ }
529+ }
530+
531+ impl < F , E > SignFn < UnsignedBolt12Invoice > for F
532+ where
533+ F : SignBolt12InvoiceFn < Error = E > ,
534+ {
535+ type Error = E ;
536+
537+ fn sign ( & self , message : & UnsignedBolt12Invoice ) -> Result < Signature , Self :: Error > {
538+ self . sign_invoice ( message)
539+ }
540+ }
541+
510542impl UnsignedBolt12Invoice {
511543 fn new ( invreq_bytes : & [ u8 ] , contents : InvoiceContents ) -> Self {
512544 // Use the invoice_request bytes instead of the invoice_request TLV stream as the latter may
@@ -534,12 +566,9 @@ macro_rules! unsigned_invoice_sign_method { ($self: ident, $self_type: ty $(, $s
534566 /// Signs the [`TaggedHash`] of the invoice using the given function.
535567 ///
536568 /// Note: The hash computation may have included unknown, odd TLV records.
537- ///
538- /// This is not exported to bindings users as functions aren't currently mapped.
539- pub fn sign<F , E >( $( $self_mut) * $self: $self_type, sign: F ) -> Result <Bolt12Invoice , SignError <E >>
540- where
541- F : FnOnce ( & Self ) -> Result <Signature , E >
542- {
569+ pub fn sign<F : SignBolt12InvoiceFn >(
570+ $( $self_mut) * $self: $self_type, sign: F
571+ ) -> Result <Bolt12Invoice , SignError <F :: Error >> {
543572 let pubkey = $self. contents. fields( ) . signing_pubkey;
544573 let signature = merkle:: sign_message( sign, & $self, pubkey) ?;
545574
@@ -2013,7 +2042,7 @@ mod tests {
20132042 . sign ( payer_sign) . unwrap ( )
20142043 . respond_with_no_std ( payment_paths ( ) , payment_hash ( ) , now ( ) ) . unwrap ( )
20152044 . build ( ) . unwrap ( )
2016- . sign ( |_| Err ( ( ) ) )
2045+ . sign ( fail_sign )
20172046 {
20182047 Ok ( _) => panic ! ( "expected error" ) ,
20192048 Err ( e) => assert_eq ! ( e, SignError :: Signing ( ( ) ) ) ,
0 commit comments