From: Jeffrey Czyz Date: Thu, 7 Mar 2024 22:59:09 +0000 (-0600) Subject: Drop error type parameter from SignError X-Git-Tag: v0.0.123-beta~46^2 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=9277166a8b10bc0eff18fadc8fcff8a4a6a5673e;p=rust-lightning Drop error type parameter from SignError 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. --- diff --git a/fuzz/src/invoice_request_deser.rs b/fuzz/src/invoice_request_deser.rs index 27178deac..414ce1cdd 100644 --- a/fuzz/src/invoice_request_deser.rs +++ b/fuzz/src/invoice_request_deser.rs @@ -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(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(); } } diff --git a/fuzz/src/offer_deser.rs b/fuzz/src/offer_deser.rs index 1b3289b33..5aad4642e 100644 --- a/fuzz/src/offer_deser.rs +++ b/fuzz/src/offer_deser.rs @@ -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(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(); diff --git a/fuzz/src/refund_deser.rs b/fuzz/src/refund_deser.rs index 9c231aa52..14b136ec3 100644 --- a/fuzz/src/refund_deser.rs +++ b/fuzz/src/refund_deser.rs @@ -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(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(); diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index 364150645..00e281adf 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -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( diff --git a/lightning/src/offers/invoice.rs b/lightning/src/offers/invoice.rs index 50406738f..fc8371023 100644 --- a/lightning/src/offers/invoice.rs +++ b/lightning/src/offers/invoice.rs @@ -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; + fn sign_invoice(&self, message: &UnsignedBolt12Invoice) -> Result; } -impl SignBolt12InvoiceFn for F +impl SignBolt12InvoiceFn for F where - F: Fn(&UnsignedBolt12Invoice) -> Result, + F: Fn(&UnsignedBolt12Invoice) -> Result, { - type Error = E; - - fn sign_invoice(&self, message: &UnsignedBolt12Invoice) -> Result { + fn sign_invoice(&self, message: &UnsignedBolt12Invoice) -> Result { self(message) } } -impl SignFn for F +impl SignFn for F where - F: SignBolt12InvoiceFn, + F: SignBolt12InvoiceFn, { - type Error = E; - - fn sign(&self, message: &UnsignedBolt12Invoice) -> Result { + fn sign(&self, message: &UnsignedBolt12Invoice) -> Result { 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( $($self_mut)* $self: $self_type, sign: F - ) -> Result> { + ) -> Result { 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()) diff --git a/lightning/src/offers/invoice_request.rs b/lightning/src/offers/invoice_request.rs index 4eed9fb7e..f282075d9 100644 --- a/lightning/src/offers/invoice_request.rs +++ b/lightning/src/offers/invoice_request.rs @@ -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; + fn sign_invoice_request(&self, message: &UnsignedInvoiceRequest) -> Result; } -impl SignInvoiceRequestFn for F +impl SignInvoiceRequestFn for F where - F: Fn(&UnsignedInvoiceRequest) -> Result, + F: Fn(&UnsignedInvoiceRequest) -> Result, { - type Error = E; - - fn sign_invoice_request(&self, message: &UnsignedInvoiceRequest) -> Result { + fn sign_invoice_request(&self, message: &UnsignedInvoiceRequest) -> Result { self(message) } } -impl SignFn for F +impl SignFn for F where - F: SignInvoiceRequestFn, + F: SignInvoiceRequestFn, { - type Error = E; - - fn sign(&self, message: &UnsignedInvoiceRequest) -> Result { + fn sign(&self, message: &UnsignedInvoiceRequest) -> Result { 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( $($self_mut)* $self: $self_type, sign: F - ) -> Result> { + ) -> Result { 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(); diff --git a/lightning/src/offers/merkle.rs b/lightning/src/offers/merkle.rs index 5a9424500..941bf1967 100644 --- a/lightning/src/offers/merkle.rs +++ b/lightning/src/offers/merkle.rs @@ -76,29 +76,24 @@ impl AsRef for TaggedHash { /// Error when signing messages. #[derive(Debug, PartialEq)] -pub enum SignError { +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> { - /// 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; + fn sign(&self, message: &T) -> Result; } -impl SignFn for F +impl SignFn for F where - F: Fn(&TaggedHash) -> Result, + F: Fn(&TaggedHash) -> Result, { - type Error = E; - - fn sign(&self, message: &TaggedHash) -> Result { + fn sign(&self, message: &TaggedHash) -> Result { self(message) } } @@ -112,14 +107,14 @@ where /// /// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice /// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest -pub(super) fn sign_message( +pub(super) fn sign_message( f: F, message: &T, pubkey: PublicKey, -) -> Result> +) -> Result where - F: SignFn, + F: SignFn, T: AsRef, { - 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) diff --git a/lightning/src/offers/test_utils.rs b/lightning/src/offers/test_utils.rs index 701eb69c3..29bed53d8 100644 --- a/lightning/src/offers/test_utils.rs +++ b/lightning/src/offers/test_utils.rs @@ -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>(message: &T) -> Result { +pub(crate) fn payer_sign>(message: &T) -> Result { 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>(message: &T) -> Result { +pub(crate) fn recipient_sign>(message: &T) -> Result { 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))