]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Drop error type parameter from SignError
authorJeffrey Czyz <jkczyz@gmail.com>
Thu, 7 Mar 2024 22:59:09 +0000 (16:59 -0600)
committerJeffrey Czyz <jkczyz@gmail.com>
Fri, 8 Mar 2024 16:39:23 +0000 (10:39 -0600)
SignError allows implementors of SignFunction to return a custom error
type. Drop this as an unconstrained type causes problems with bindings
and isn't useful unless the caller can take some sort of action based on
different errors.

fuzz/src/invoice_request_deser.rs
fuzz/src/offer_deser.rs
fuzz/src/refund_deser.rs
lightning/src/ln/channelmanager.rs
lightning/src/offers/invoice.rs
lightning/src/offers/invoice_request.rs
lightning/src/offers/merkle.rs
lightning/src/offers/test_utils.rs

index 27178deacf017361b0a9fd9aed1d60d240126047..414ce1cdd1cd2c17431d68ea9c1015495b9e074a 100644 (file)
@@ -9,7 +9,7 @@
 
 use bitcoin::secp256k1::{KeyPair, Parity, PublicKey, Secp256k1, SecretKey, self};
 use crate::utils::test_logger;
-use core::convert::{Infallible, TryFrom};
+use core::convert::TryFrom;
 use lightning::blinded_path::BlindedPath;
 use lightning::sign::EntropySource;
 use lightning::ln::PaymentHash;
@@ -37,17 +37,17 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
                        let even_pubkey = x_only_pubkey.public_key(Parity::Even);
                        if signing_pubkey == odd_pubkey || signing_pubkey == even_pubkey {
                                unsigned_invoice
-                                       .sign(|message: &UnsignedBolt12Invoice| -> Result<_, Infallible> {
+                                       .sign(|message: &UnsignedBolt12Invoice|
                                                Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &keys))
-                                       })
+                                       )
                                        .unwrap()
                                        .write(&mut buffer)
                                        .unwrap();
                        } else {
                                unsigned_invoice
-                                       .sign(|message: &UnsignedBolt12Invoice| -> Result<_, Infallible> {
+                                       .sign(|message: &UnsignedBolt12Invoice|
                                                Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &keys))
-                                       })
+                                       )
                                        .unwrap_err();
                        }
                }
index 1b3289b3322b5125208d96d54aed5aeae825800f..5aad4642e461f6477a55591cf2239507882cd86a 100644 (file)
@@ -9,7 +9,7 @@
 
 use bitcoin::secp256k1::{KeyPair, PublicKey, Secp256k1, SecretKey};
 use crate::utils::test_logger;
-use core::convert::{Infallible, TryFrom};
+use core::convert::TryFrom;
 use lightning::offers::invoice_request::UnsignedInvoiceRequest;
 use lightning::offers::offer::{Amount, Offer, Quantity};
 use lightning::offers::parse::Bolt12SemanticError;
@@ -29,9 +29,9 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
 
                if let Ok(invoice_request) = build_response(&offer, pubkey) {
                        invoice_request
-                               .sign(|message: &UnsignedInvoiceRequest| -> Result<_, Infallible> {
+                               .sign(|message: &UnsignedInvoiceRequest|
                                        Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &keys))
-                               })
+                               )
                                .unwrap()
                                .write(&mut buffer)
                                .unwrap();
index 9c231aa5234104158603097cdf7cd05d966fc8ad..14b136ec35b6d06837c09e997523fc5d093944c1 100644 (file)
@@ -9,7 +9,7 @@
 
 use bitcoin::secp256k1::{KeyPair, PublicKey, Secp256k1, SecretKey, self};
 use crate::utils::test_logger;
-use core::convert::{Infallible, TryFrom};
+use core::convert::TryFrom;
 use lightning::blinded_path::BlindedPath;
 use lightning::sign::EntropySource;
 use lightning::ln::PaymentHash;
@@ -33,9 +33,9 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
 
                if let Ok(invoice) = build_response(&refund, pubkey, &secp_ctx) {
                        invoice
-                               .sign(|message: &UnsignedBolt12Invoice| -> Result<_, Infallible> {
+                               .sign(|message: &UnsignedBolt12Invoice|
                                        Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &keys))
-                               })
+                               )
                                .unwrap()
                                .write(&mut buffer)
                                .unwrap();
index 364150645e52b118422f9ea31db888d411c267f8..00e281adf438512305b9796a24a052c2b0d52866 100644 (file)
@@ -9306,7 +9306,7 @@ where
                                                                self.node_signer.sign_bolt12_invoice(invoice)
                                                        ) {
                                                                Ok(invoice) => Ok(OffersMessage::Invoice(invoice)),
-                                                               Err(SignError::Signing(())) => Err(OffersMessage::InvoiceError(
+                                                               Err(SignError::Signing) => Err(OffersMessage::InvoiceError(
                                                                                InvoiceError::from_string("Failed signing invoice".to_string())
                                                                )),
                                                                Err(SignError::Verification(_)) => Err(OffersMessage::InvoiceError(
index 50406738f36ff9d3e8d8bd783fd7196bf78d335d..fc8371023484d8ade7a2708340a0e2402685d710 100644 (file)
@@ -22,7 +22,7 @@
 //!
 //! use bitcoin::hashes::Hash;
 //! use bitcoin::secp256k1::{KeyPair, PublicKey, Secp256k1, SecretKey};
-//! use core::convert::{Infallible, TryFrom};
+//! use core::convert::TryFrom;
 //! use lightning::offers::invoice::UnsignedBolt12Invoice;
 //! use lightning::offers::invoice_request::InvoiceRequest;
 //! use lightning::offers::refund::Refund;
@@ -58,9 +58,9 @@
 //!     .allow_mpp()
 //!     .fallback_v0_p2wpkh(&wpubkey_hash)
 //!     .build()?
-//!     .sign(|message: &UnsignedBolt12Invoice| -> Result<_, Infallible> {
+//!     .sign(|message: &UnsignedBolt12Invoice|
 //!         Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &keys))
-//!     })
+//!     )
 //!     .expect("failed verifying signature")
 //!     .write(&mut buffer)
 //!     .unwrap();
@@ -91,9 +91,9 @@
 //!     .allow_mpp()
 //!     .fallback_v0_p2wpkh(&wpubkey_hash)
 //!     .build()?
-//!     .sign(|message: &UnsignedBolt12Invoice| -> Result<_, Infallible> {
+//!     .sign(|message: &UnsignedBolt12Invoice|
 //!         Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &keys))
-//!     })
+//!     )
 //!     .expect("failed verifying signature")
 //!     .write(&mut buffer)
 //!     .unwrap();
@@ -110,7 +110,7 @@ use bitcoin::secp256k1::{KeyPair, PublicKey, Secp256k1, self};
 use bitcoin::secp256k1::schnorr::Signature;
 use bitcoin::address::{Address, Payload, WitnessProgram, WitnessVersion};
 use bitcoin::key::TweakedPublicKey;
-use core::convert::{AsRef, Infallible, TryFrom};
+use core::convert::{AsRef, TryFrom};
 use core::time::Duration;
 use crate::io;
 use crate::blinded_path::BlindedPath;
@@ -325,9 +325,9 @@ macro_rules! invoice_derived_signing_pubkey_builder_methods { ($self: ident, $se
                let mut unsigned_invoice = UnsignedBolt12Invoice::new(invreq_bytes, invoice.clone());
 
                let invoice = unsigned_invoice
-                       .sign(|message: &UnsignedBolt12Invoice| -> Result<_, Infallible> {
+                       .sign(|message: &UnsignedBolt12Invoice|
                                Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &keys))
-                       })
+                       )
                        .unwrap();
                Ok(invoice)
        }
@@ -510,31 +510,24 @@ pub struct UnsignedBolt12Invoice {
 
 /// A function for signing an [`UnsignedBolt12Invoice`].
 pub trait SignBolt12InvoiceFn {
-       /// Error type returned by the function.
-       type Error;
-
        /// Signs a [`TaggedHash`] computed over the merkle root of `message`'s TLV stream.
-       fn sign_invoice(&self, message: &UnsignedBolt12Invoice) -> Result<Signature, Self::Error>;
+       fn sign_invoice(&self, message: &UnsignedBolt12Invoice) -> Result<Signature, ()>;
 }
 
-impl<F, E> SignBolt12InvoiceFn for F
+impl<F> SignBolt12InvoiceFn for F
 where
-       F: Fn(&UnsignedBolt12Invoice) -> Result<Signature, E>,
+       F: Fn(&UnsignedBolt12Invoice) -> Result<Signature, ()>,
 {
-       type Error = E;
-
-       fn sign_invoice(&self, message: &UnsignedBolt12Invoice) -> Result<Signature, E> {
+       fn sign_invoice(&self, message: &UnsignedBolt12Invoice) -> Result<Signature, ()> {
                self(message)
        }
 }
 
-impl<F, E> SignFn<UnsignedBolt12Invoice> for F
+impl<F> SignFn<UnsignedBolt12Invoice> for F
 where
-       F: SignBolt12InvoiceFn<Error = E>,
+       F: SignBolt12InvoiceFn,
 {
-       type Error = E;
-
-       fn sign(&self, message: &UnsignedBolt12Invoice) -> Result<Signature, Self::Error> {
+       fn sign(&self, message: &UnsignedBolt12Invoice) -> Result<Signature, ()> {
                self.sign_invoice(message)
        }
 }
@@ -568,7 +561,7 @@ macro_rules! unsigned_invoice_sign_method { ($self: ident, $self_type: ty $(, $s
        /// Note: The hash computation may have included unknown, odd TLV records.
        pub fn sign<F: SignBolt12InvoiceFn>(
                $($self_mut)* $self: $self_type, sign: F
-       ) -> Result<Bolt12Invoice, SignError<F::Error>> {
+       ) -> Result<Bolt12Invoice, SignError> {
                let pubkey = $self.contents.fields().signing_pubkey;
                let signature = merkle::sign_message(sign, &$self, pubkey)?;
 
@@ -2045,7 +2038,7 @@ mod tests {
                        .sign(fail_sign)
                {
                        Ok(_) => panic!("expected error"),
-                       Err(e) => assert_eq!(e, SignError::Signing(())),
+                       Err(e) => assert_eq!(e, SignError::Signing),
                }
 
                match OfferBuilder::new("foo".into(), recipient_pubkey())
index 4eed9fb7ee68f1ef51621c00173998167b56dc1f..f282075d9333cf1f9002154519474518036b9336 100644 (file)
@@ -25,7 +25,6 @@
 //!
 //! use bitcoin::network::constants::Network;
 //! use bitcoin::secp256k1::{KeyPair, PublicKey, Secp256k1, SecretKey};
-//! use core::convert::Infallible;
 //! use lightning::ln::features::OfferFeatures;
 //! use lightning::offers::invoice_request::UnsignedInvoiceRequest;
 //! use lightning::offers::offer::Offer;
@@ -48,9 +47,9 @@
 //!     .quantity(5)?
 //!     .payer_note("foo".to_string())
 //!     .build()?
-//!     .sign(|message: &UnsignedInvoiceRequest| -> Result<_, Infallible> {
+//!     .sign(|message: &UnsignedInvoiceRequest|
 //!         Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &keys))
-//!     })
+//!     )
 //!     .expect("failed verifying signature")
 //!     .write(&mut buffer)
 //!     .unwrap();
@@ -62,7 +61,7 @@ use bitcoin::blockdata::constants::ChainHash;
 use bitcoin::network::constants::Network;
 use bitcoin::secp256k1::{KeyPair, PublicKey, Secp256k1, self};
 use bitcoin::secp256k1::schnorr::Signature;
-use core::convert::{AsRef, Infallible, TryFrom};
+use core::convert::{AsRef, TryFrom};
 use core::ops::Deref;
 use crate::sign::EntropySource;
 use crate::io;
@@ -228,9 +227,9 @@ macro_rules! invoice_request_derived_payer_id_builder_methods { (
                let secp_ctx = secp_ctx.unwrap();
                let keys = keys.unwrap();
                let invoice_request = unsigned_invoice_request
-                       .sign(|message: &UnsignedInvoiceRequest| -> Result<_, Infallible> {
+                       .sign(|message: &UnsignedInvoiceRequest|
                                Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &keys))
-                       })
+                       )
                        .unwrap();
                Ok(invoice_request)
        }
@@ -496,31 +495,24 @@ pub struct UnsignedInvoiceRequest {
 
 /// A function for signing an [`UnsignedInvoiceRequest`].
 pub trait SignInvoiceRequestFn {
-       /// Error type returned by the function.
-       type Error;
-
        /// Signs a [`TaggedHash`] computed over the merkle root of `message`'s TLV stream.
-       fn sign_invoice_request(&self, message: &UnsignedInvoiceRequest) -> Result<Signature, Self::Error>;
+       fn sign_invoice_request(&self, message: &UnsignedInvoiceRequest) -> Result<Signature, ()>;
 }
 
-impl<F, E> SignInvoiceRequestFn for F
+impl<F> SignInvoiceRequestFn for F
 where
-       F: Fn(&UnsignedInvoiceRequest) -> Result<Signature, E>,
+       F: Fn(&UnsignedInvoiceRequest) -> Result<Signature, ()>,
 {
-       type Error = E;
-
-       fn sign_invoice_request(&self, message: &UnsignedInvoiceRequest) -> Result<Signature, E> {
+       fn sign_invoice_request(&self, message: &UnsignedInvoiceRequest) -> Result<Signature, ()> {
                self(message)
        }
 }
 
-impl<F, E> SignFn<UnsignedInvoiceRequest> for F
+impl<F> SignFn<UnsignedInvoiceRequest> for F
 where
-       F: SignInvoiceRequestFn<Error = E>,
+       F: SignInvoiceRequestFn,
 {
-       type Error = E;
-
-       fn sign(&self, message: &UnsignedInvoiceRequest) -> Result<Signature, Self::Error> {
+       fn sign(&self, message: &UnsignedInvoiceRequest) -> Result<Signature, ()> {
                self.sign_invoice_request(message)
        }
 }
@@ -556,7 +548,7 @@ macro_rules! unsigned_invoice_request_sign_method { (
        /// Note: The hash computation may have included unknown, odd TLV records.
        pub fn sign<F: SignInvoiceRequestFn>(
                $($self_mut)* $self: $self_type, sign: F
-       ) -> Result<InvoiceRequest, SignError<F::Error>> {
+       ) -> Result<InvoiceRequest, SignError> {
                let pubkey = $self.contents.payer_id;
                let signature = merkle::sign_message(sign, &$self, pubkey)?;
 
@@ -1111,7 +1103,7 @@ mod tests {
        use bitcoin::blockdata::constants::ChainHash;
        use bitcoin::network::constants::Network;
        use bitcoin::secp256k1::{KeyPair, Secp256k1, SecretKey, self};
-       use core::convert::{Infallible, TryFrom};
+       use core::convert::TryFrom;
        use core::num::NonZeroU64;
        #[cfg(feature = "std")]
        use core::time::Duration;
@@ -1744,7 +1736,7 @@ mod tests {
                        .sign(fail_sign)
                {
                        Ok(_) => panic!("expected error"),
-                       Err(e) => assert_eq!(e, SignError::Signing(())),
+                       Err(e) => assert_eq!(e, SignError::Signing),
                }
 
                match OfferBuilder::new("foo".into(), recipient_pubkey())
@@ -2155,9 +2147,9 @@ mod tests {
                        .build().unwrap()
                        .request_invoice(vec![1; 32], keys.public_key()).unwrap()
                        .build().unwrap()
-                       .sign(|message: &UnsignedInvoiceRequest| -> Result<_, Infallible> {
+                       .sign(|message: &UnsignedInvoiceRequest|
                                Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &keys))
-                       })
+                       )
                        .unwrap();
 
                let mut encoded_invoice_request = Vec::new();
index 5a9424500b37c8e037f22b0e9ea2ee7cd561c911..941bf196716d8b5ee2d2e51d2c4db95716bdf869 100644 (file)
@@ -76,29 +76,24 @@ impl AsRef<TaggedHash> for TaggedHash {
 
 /// Error when signing messages.
 #[derive(Debug, PartialEq)]
-pub enum SignError<E = ()> {
+pub enum SignError {
        /// User-defined error when signing the message.
-       Signing(E),
+       Signing,
        /// Error when verifying the produced signature using the given pubkey.
        Verification(secp256k1::Error),
 }
 
 /// A function for signing a [`TaggedHash`].
 pub(super) trait SignFn<T: AsRef<TaggedHash>> {
-       /// Error type returned by the function.
-       type Error;
-
        /// Signs a [`TaggedHash`] computed over the merkle root of `message`'s TLV stream.
-       fn sign(&self, message: &T) -> Result<Signature, Self::Error>;
+       fn sign(&self, message: &T) -> Result<Signature, ()>;
 }
 
-impl<F, E> SignFn<TaggedHash> for F
+impl<F> SignFn<TaggedHash> for F
 where
-       F: Fn(&TaggedHash) -> Result<Signature, E>,
+       F: Fn(&TaggedHash) -> Result<Signature, ()>,
 {
-       type Error = E;
-
-       fn sign(&self, message: &TaggedHash) -> Result<Signature, E> {
+       fn sign(&self, message: &TaggedHash) -> Result<Signature, ()> {
                self(message)
        }
 }
@@ -112,14 +107,14 @@ where
 ///
 /// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
 /// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
-pub(super) fn sign_message<F, T, E>(
+pub(super) fn sign_message<F, T>(
        f: F, message: &T, pubkey: PublicKey,
-) -> Result<Signature, SignError<E>>
+) -> Result<Signature, SignError>
 where
-       F: SignFn<T, Error = E>,
+       F: SignFn<T>,
        T: AsRef<TaggedHash>,
 {
-       let signature = f.sign(message).map_err(|e| SignError::Signing(e))?;
+       let signature = f.sign(message).map_err(|()| SignError::Signing)?;
 
        let digest = message.as_ref().as_digest();
        let pubkey = pubkey.into();
@@ -296,7 +291,6 @@ mod tests {
        use bitcoin::hashes::hex::FromHex;
        use bitcoin::secp256k1::{KeyPair, Message, Secp256k1, SecretKey};
        use bitcoin::secp256k1::schnorr::Signature;
-       use core::convert::Infallible;
        use crate::offers::offer::{Amount, OfferBuilder};
        use crate::offers::invoice_request::{InvoiceRequest, UnsignedInvoiceRequest};
        use crate::offers::parse::Bech32Encode;
@@ -341,9 +335,9 @@ mod tests {
                        .build_unchecked()
                        .request_invoice(vec![0; 8], payer_keys.public_key()).unwrap()
                        .build_unchecked()
-                       .sign(|message: &UnsignedInvoiceRequest| -> Result<_, Infallible> {
+                       .sign(|message: &UnsignedInvoiceRequest|
                                Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &payer_keys))
-                       })
+                       )
                        .unwrap();
                assert_eq!(
                        invoice_request.to_string(),
@@ -395,9 +389,9 @@ mod tests {
                        .build_unchecked()
                        .request_invoice(vec![0; 8], payer_keys.public_key()).unwrap()
                        .build_unchecked()
-                       .sign(|message: &UnsignedInvoiceRequest| -> Result<_, Infallible> {
+                       .sign(|message: &UnsignedInvoiceRequest|
                                Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &payer_keys))
-                       })
+                       )
                        .unwrap();
 
                let mut bytes_without_signature = Vec::new();
@@ -427,9 +421,9 @@ mod tests {
                        .build_unchecked()
                        .request_invoice(vec![0; 8], payer_keys.public_key()).unwrap()
                        .build_unchecked()
-                       .sign(|message: &UnsignedInvoiceRequest| -> Result<_, Infallible> {
+                       .sign(|message: &UnsignedInvoiceRequest|
                                Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &payer_keys))
-                       })
+                       )
                        .unwrap();
 
                let tlv_stream = TlvStream::new(&invoice_request.bytes).range(0..1)
index 701eb69c3e15b6a96db097859dde46d0072fa953..29bed53d83f561f030e4966373c4403020ac6ffc 100644 (file)
@@ -11,7 +11,7 @@
 
 use bitcoin::secp256k1::{KeyPair, PublicKey, Secp256k1, SecretKey};
 use bitcoin::secp256k1::schnorr::Signature;
-use core::convert::{AsRef, Infallible};
+use core::convert::AsRef; 
 use core::time::Duration;
 use crate::blinded_path::{BlindedHop, BlindedPath};
 use crate::sign::EntropySource;
@@ -29,7 +29,7 @@ pub(crate) fn payer_keys() -> KeyPair {
        KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap())
 }
 
-pub(crate) fn payer_sign<T: AsRef<TaggedHash>>(message: &T) -> Result<Signature, Infallible> {
+pub(crate) fn payer_sign<T: AsRef<TaggedHash>>(message: &T) -> Result<Signature, ()> {
        let secp_ctx = Secp256k1::new();
        let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
        Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &keys))
@@ -44,7 +44,7 @@ pub(crate) fn recipient_keys() -> KeyPair {
        KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[43; 32]).unwrap())
 }
 
-pub(crate) fn recipient_sign<T: AsRef<TaggedHash>>(message: &T) -> Result<Signature, Infallible> {
+pub(crate) fn recipient_sign<T: AsRef<TaggedHash>>(message: &T) -> Result<Signature, ()> {
        let secp_ctx = Secp256k1::new();
        let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[43; 32]).unwrap());
        Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &keys))