Skip to content

Commit

Permalink
f - associated type on SignFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
jkczyz committed Mar 6, 2024
1 parent 0336dd1 commit a8cc5fe
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 27 deletions.
18 changes: 10 additions & 8 deletions lightning/src/offers/invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@
//! .allow_mpp()
//! .fallback_v0_p2wpkh(&wpubkey_hash)
//! .build()?
//! .sign::<_, Infallible>(|message: &UnsignedBolt12Invoice|
//! .sign(|message: &UnsignedBolt12Invoice| -> Result<_, Infallible> {
//! 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::<_, Infallible>(|message: &UnsignedBolt12Invoice|
//! .sign(|message: &UnsignedBolt12Invoice| -> Result<_, Infallible> {
//! 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 @@ -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::<_, Infallible>(|message: &UnsignedBolt12Invoice|
.sign(|message: &UnsignedBolt12Invoice| -> Result<_, Infallible> {
Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &keys))
)
})
.unwrap();
Ok(invoice)
}
Expand Down Expand Up @@ -535,9 +535,11 @@ macro_rules! unsigned_invoice_sign_method { ($self: ident, $self_type: ty $(, $s
/// Signs the [`TaggedHash`] of the invoice using the given function.
///
/// Note: The hash computation may have included unknown, odd TLV records.
pub fn sign<F, E>($($self_mut)* $self: $self_type, sign: F) -> Result<Bolt12Invoice, SignError<E>>
pub fn sign<F>(
$($self_mut)* $self: $self_type, sign: F
) -> Result<Bolt12Invoice, SignError<F::Error>>
where
F: SignFunction<Self, E>,
F: SignFunction<Self>,
{
let pubkey = $self.contents.fields().signing_pubkey;
let signature = merkle::sign_message(sign, &$self, pubkey)?;
Expand Down
18 changes: 10 additions & 8 deletions lightning/src/offers/invoice_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@
//! .quantity(5)?
//! .payer_note("foo".to_string())
//! .build()?
//! .sign::<_, Infallible>(|message: &UnsignedInvoiceRequest|
//! .sign(|message: &UnsignedInvoiceRequest| -> Result<_, Infallible> {
//! 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 @@ -228,9 +228,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::<_, Infallible>(|message: &UnsignedInvoiceRequest|
.sign(|message: &UnsignedInvoiceRequest| -> Result<_, Infallible> {
Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &keys))
)
})
.unwrap();
Ok(invoice_request)
}
Expand Down Expand Up @@ -523,9 +523,11 @@ macro_rules! unsigned_invoice_request_sign_method { (
/// Signs the [`TaggedHash`] of the invoice request using the given function.
///
/// Note: The hash computation may have included unknown, odd TLV records.
pub fn sign<F, E>($($self_mut)* $self: $self_type, sign: F) -> Result<InvoiceRequest, SignError<E>>
pub fn sign<F>(
$($self_mut)* $self: $self_type, sign: F
) -> Result<InvoiceRequest, SignError<F::Error>>
where
F: SignFunction<Self, E>,
F: SignFunction<Self>,
{
let pubkey = $self.contents.payer_id;
let signature = merkle::sign_message(sign, &$self, pubkey)?;
Expand Down Expand Up @@ -2125,9 +2127,9 @@ mod tests {
.build().unwrap()
.request_invoice(vec![1; 32], keys.public_key()).unwrap()
.build().unwrap()
.sign::<_, Infallible>(|message: &UnsignedInvoiceRequest|
.sign(|message: &UnsignedInvoiceRequest| -> Result<_, Infallible> {
Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &keys))
)
})
.unwrap();

let mut encoded_invoice_request = Vec::new();
Expand Down
27 changes: 16 additions & 11 deletions lightning/src/offers/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,24 +76,29 @@ impl AsRef<TaggedHash> for TaggedHash {

/// Error when signing messages.
#[derive(Debug, PartialEq)]
pub enum SignError<E> {
pub enum SignError<E = ()> {
/// User-defined error when signing the message.
Signing(E),
/// Error when verifying the produced signature using the given pubkey.
Verification(secp256k1::Error),
}

/// A function for signing a [`TaggedHash`].
pub trait SignFunction<T: AsRef<TaggedHash>, E> {
pub trait SignFunction<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, E>;
fn sign(&self, message: &T) -> Result<Signature, Self::Error>;
}

impl<F, T, E> SignFunction<T, E> for F
impl<F, T, E> SignFunction<T> for F
where
F: Fn(&T) -> Result<Signature, E>,
T: AsRef<TaggedHash>,
{
type Error = E;

fn sign(&self, message: &T) -> Result<Signature, E> {
self(message)
}
Expand All @@ -112,7 +117,7 @@ pub(super) fn sign_message<F, T, E>(
f: F, message: &T, pubkey: PublicKey,
) -> Result<Signature, SignError<E>>
where
F: SignFunction<T, E>,
F: SignFunction<T, Error = E>,
T: AsRef<TaggedHash>,
{
let signature = f.sign(message).map_err(|e| SignError::Signing(e))?;
Expand Down Expand Up @@ -337,9 +342,9 @@ mod tests {
.build_unchecked()
.request_invoice(vec![0; 8], payer_keys.public_key()).unwrap()
.build_unchecked()
.sign::<_, Infallible>(|message: &UnsignedInvoiceRequest|
.sign(|message: &UnsignedInvoiceRequest| -> Result<_, Infallible> {
Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &payer_keys))
)
})
.unwrap();
assert_eq!(
invoice_request.to_string(),
Expand Down Expand Up @@ -391,9 +396,9 @@ mod tests {
.build_unchecked()
.request_invoice(vec![0; 8], payer_keys.public_key()).unwrap()
.build_unchecked()
.sign::<_, Infallible>(|message: &UnsignedInvoiceRequest|
.sign(|message: &UnsignedInvoiceRequest| -> Result<_, Infallible> {
Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &payer_keys))
)
})
.unwrap();

let mut bytes_without_signature = Vec::new();
Expand Down Expand Up @@ -423,9 +428,9 @@ mod tests {
.build_unchecked()
.request_invoice(vec![0; 8], payer_keys.public_key()).unwrap()
.build_unchecked()
.sign::<_, Infallible>(|message: &UnsignedInvoiceRequest|
.sign(|message: &UnsignedInvoiceRequest| -> Result<_, Infallible> {
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)
Expand Down

0 comments on commit a8cc5fe

Please sign in to comment.