Support signing BOLT 12 invoices in NodeSigner
[rust-lightning] / lightning / src / offers / invoice.rs
index 05960642efd111e2f748a805ab2df628e1d24682..75a844cd117abe5d956ddcbdf7efde5bff44a769 100644 (file)
@@ -397,6 +397,11 @@ impl UnsignedBolt12Invoice {
                Self { bytes, contents, tagged_hash }
        }
 
+       /// Returns the [`TaggedHash`] of the invoice to sign.
+       pub fn tagged_hash(&self) -> &TaggedHash {
+               &self.tagged_hash
+       }
+
        /// Signs the [`TaggedHash`] of the invoice using the given function.
        ///
        /// Note: The hash computation may have included unknown, odd TLV records.
@@ -1184,8 +1189,9 @@ impl TryFrom<ParsedMessage<FullInvoiceTlvStream>> for Bolt12Invoice {
                        None => return Err(Bolt12ParseError::InvalidSemantics(Bolt12SemanticError::MissingSignature)),
                        Some(signature) => signature,
                };
+               let message = TaggedHash::new(SIGNATURE_TAG, &bytes);
                let pubkey = contents.fields().signing_pubkey;
-               merkle::verify_signature(&signature, SIGNATURE_TAG, &bytes, pubkey)?;
+               merkle::verify_signature(&signature, message, pubkey)?;
 
                Ok(Bolt12Invoice { bytes, contents, signature })
        }
@@ -1288,7 +1294,7 @@ mod tests {
        use crate::ln::inbound_payment::ExpandedKey;
        use crate::ln::msgs::DecodeError;
        use crate::offers::invoice_request::InvoiceRequestTlvStreamRef;
-       use crate::offers::merkle::{SignError, SignatureTlvStreamRef, self};
+       use crate::offers::merkle::{SignError, SignatureTlvStreamRef, TaggedHash, self};
        use crate::offers::offer::{Amount, OfferBuilder, OfferTlvStreamRef, Quantity};
        use crate::offers::parse::{Bolt12ParseError, Bolt12SemanticError};
        use crate::offers::payer::PayerTlvStreamRef;
@@ -1400,11 +1406,9 @@ mod tests {
                assert_eq!(invoice.fallbacks(), vec![]);
                assert_eq!(invoice.invoice_features(), &Bolt12InvoiceFeatures::empty());
                assert_eq!(invoice.signing_pubkey(), recipient_pubkey());
-               assert!(
-                       merkle::verify_signature(
-                               &invoice.signature, SIGNATURE_TAG, &invoice.bytes, recipient_pubkey()
-                       ).is_ok()
-               );
+
+               let message = TaggedHash::new(SIGNATURE_TAG, &invoice.bytes);
+               assert!(merkle::verify_signature(&invoice.signature, message, recipient_pubkey()).is_ok());
 
                let digest = Message::from_slice(&invoice.signable_hash()).unwrap();
                let pubkey = recipient_pubkey().into();
@@ -1499,11 +1503,9 @@ mod tests {
                assert_eq!(invoice.fallbacks(), vec![]);
                assert_eq!(invoice.invoice_features(), &Bolt12InvoiceFeatures::empty());
                assert_eq!(invoice.signing_pubkey(), recipient_pubkey());
-               assert!(
-                       merkle::verify_signature(
-                               &invoice.signature, SIGNATURE_TAG, &invoice.bytes, recipient_pubkey()
-                       ).is_ok()
-               );
+
+               let message = TaggedHash::new(SIGNATURE_TAG, &invoice.bytes);
+               assert!(merkle::verify_signature(&invoice.signature, message, recipient_pubkey()).is_ok());
 
                assert_eq!(
                        invoice.as_tlv_stream(),