Skip to content

Commit

Permalink
Drop error type parameter from SignError
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jkczyz committed Mar 8, 2024
1 parent 6b7050e commit 9277166
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 86 deletions.
10 changes: 5 additions & 5 deletions fuzz/src/invoice_request_deser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
Expand Down
6 changes: 3 additions & 3 deletions fuzz/src/offer_deser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions fuzz/src/refund_deser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
41 changes: 17 additions & 24 deletions lightning/src/offers/invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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;
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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)?;

Expand Down Expand Up @@ -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())
Expand Down
42 changes: 17 additions & 25 deletions lightning/src/offers/invoice_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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;
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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)?;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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();
Expand Down
Loading

0 comments on commit 9277166

Please sign in to comment.