Skip to content

Commit 9885ed4

Browse files
committed
f - type-specific sign function traits
1 parent 03700bc commit 9885ed4

File tree

3 files changed

+73
-18
lines changed

3 files changed

+73
-18
lines changed

lightning/src/offers/invoice.rs

+34-6
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ use crate::ln::features::{BlindedHopFeatures, Bolt12InvoiceFeatures, InvoiceRequ
120120
use crate::ln::inbound_payment::ExpandedKey;
121121
use crate::ln::msgs::DecodeError;
122122
use crate::offers::invoice_request::{INVOICE_REQUEST_PAYER_ID_TYPE, INVOICE_REQUEST_TYPES, IV_BYTES as INVOICE_REQUEST_IV_BYTES, InvoiceRequest, InvoiceRequestContents, InvoiceRequestTlvStream, InvoiceRequestTlvStreamRef};
123-
use crate::offers::merkle::{SignError, SignFunction, SignatureTlvStream, SignatureTlvStreamRef, TaggedHash, TlvStream, WithoutSignatures, self};
123+
use crate::offers::merkle::{SignError, SignFn, SignatureTlvStream, SignatureTlvStreamRef, TaggedHash, TlvStream, WithoutSignatures, self};
124124
use crate::offers::offer::{Amount, OFFER_TYPES, OfferTlvStream, OfferTlvStreamRef, Quantity};
125125
use crate::offers::parse::{Bolt12ParseError, Bolt12SemanticError, ParsedMessage};
126126
use crate::offers::payer::{PAYER_METADATA_TYPE, PayerTlvStream, PayerTlvStreamRef};
@@ -508,6 +508,37 @@ pub struct UnsignedBolt12Invoice {
508508
tagged_hash: TaggedHash,
509509
}
510510

511+
/// A function for signing an [`UnsignedBolt12Invoice`].
512+
pub trait SignBolt12InvoiceFn {
513+
/// Error type returned by the function.
514+
type Error;
515+
516+
/// Signs a [`TaggedHash`] computed over the merkle root of `message`'s TLV stream.
517+
fn sign_invoice(&self, message: &UnsignedBolt12Invoice) -> Result<Signature, Self::Error>;
518+
}
519+
520+
impl<F, E> SignBolt12InvoiceFn for F
521+
where
522+
F: Fn(&UnsignedBolt12Invoice) -> Result<Signature, E>,
523+
{
524+
type Error = E;
525+
526+
fn sign_invoice(&self, message: &UnsignedBolt12Invoice) -> Result<Signature, E> {
527+
self(message)
528+
}
529+
}
530+
531+
impl<F, E> SignFn<UnsignedBolt12Invoice> for F
532+
where
533+
F: SignBolt12InvoiceFn<Error = E>,
534+
{
535+
type Error = E;
536+
537+
fn sign(&self, message: &UnsignedBolt12Invoice) -> Result<Signature, Self::Error> {
538+
self.sign_invoice(message)
539+
}
540+
}
541+
511542
impl UnsignedBolt12Invoice {
512543
fn new(invreq_bytes: &[u8], contents: InvoiceContents) -> Self {
513544
// Use the invoice_request bytes instead of the invoice_request TLV stream as the latter may
@@ -535,12 +566,9 @@ macro_rules! unsigned_invoice_sign_method { ($self: ident, $self_type: ty $(, $s
535566
/// Signs the [`TaggedHash`] of the invoice using the given function.
536567
///
537568
/// Note: The hash computation may have included unknown, odd TLV records.
538-
pub fn sign<F>(
569+
pub fn sign<F: SignBolt12InvoiceFn>(
539570
$($self_mut)* $self: $self_type, sign: F
540-
) -> Result<Bolt12Invoice, SignError<F::Error>>
541-
where
542-
F: SignFunction<Self>,
543-
{
571+
) -> Result<Bolt12Invoice, SignError<F::Error>> {
544572
let pubkey = $self.contents.fields().signing_pubkey;
545573
let signature = merkle::sign_message(sign, &$self, pubkey)?;
546574

lightning/src/offers/invoice_request.rs

+34-6
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ use crate::ln::features::InvoiceRequestFeatures;
7373
use crate::ln::inbound_payment::{ExpandedKey, IV_LEN, Nonce};
7474
use crate::ln::msgs::DecodeError;
7575
use crate::offers::invoice::BlindedPayInfo;
76-
use crate::offers::merkle::{SignError, SignFunction, SignatureTlvStream, SignatureTlvStreamRef, TaggedHash, self};
76+
use crate::offers::merkle::{SignError, SignFn, SignatureTlvStream, SignatureTlvStreamRef, TaggedHash, self};
7777
use crate::offers::offer::{Offer, OfferContents, OfferTlvStream, OfferTlvStreamRef};
7878
use crate::offers::parse::{Bolt12ParseError, ParsedMessage, Bolt12SemanticError};
7979
use crate::offers::payer::{PayerContents, PayerTlvStream, PayerTlvStreamRef};
@@ -494,6 +494,37 @@ pub struct UnsignedInvoiceRequest {
494494
tagged_hash: TaggedHash,
495495
}
496496

497+
/// A function for signing an [`UnsignedInvoiceRequest`].
498+
pub trait SignInvoiceRequestFn {
499+
/// Error type returned by the function.
500+
type Error;
501+
502+
/// Signs a [`TaggedHash`] computed over the merkle root of `message`'s TLV stream.
503+
fn sign_invoice_request(&self, message: &UnsignedInvoiceRequest) -> Result<Signature, Self::Error>;
504+
}
505+
506+
impl<F, E> SignInvoiceRequestFn for F
507+
where
508+
F: Fn(&UnsignedInvoiceRequest) -> Result<Signature, E>,
509+
{
510+
type Error = E;
511+
512+
fn sign_invoice_request(&self, message: &UnsignedInvoiceRequest) -> Result<Signature, E> {
513+
self(message)
514+
}
515+
}
516+
517+
impl<F, E> SignFn<UnsignedInvoiceRequest> for F
518+
where
519+
F: SignInvoiceRequestFn<Error = E>,
520+
{
521+
type Error = E;
522+
523+
fn sign(&self, message: &UnsignedInvoiceRequest) -> Result<Signature, Self::Error> {
524+
self.sign_invoice_request(message)
525+
}
526+
}
527+
497528
impl UnsignedInvoiceRequest {
498529
fn new(offer: &Offer, contents: InvoiceRequestContents) -> Self {
499530
// Use the offer bytes instead of the offer TLV stream as the offer may have contained
@@ -523,12 +554,9 @@ macro_rules! unsigned_invoice_request_sign_method { (
523554
/// Signs the [`TaggedHash`] of the invoice request using the given function.
524555
///
525556
/// Note: The hash computation may have included unknown, odd TLV records.
526-
pub fn sign<F>(
557+
pub fn sign<F: SignInvoiceRequestFn>(
527558
$($self_mut)* $self: $self_type, sign: F
528-
) -> Result<InvoiceRequest, SignError<F::Error>>
529-
where
530-
F: SignFunction<Self>,
531-
{
559+
) -> Result<InvoiceRequest, SignError<F::Error>> {
532560
let pubkey = $self.contents.payer_id;
533561
let signature = merkle::sign_message(sign, &$self, pubkey)?;
534562

lightning/src/offers/merkle.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -84,22 +84,21 @@ pub enum SignError<E = ()> {
8484
}
8585

8686
/// A function for signing a [`TaggedHash`].
87-
pub trait SignFunction<T: AsRef<TaggedHash>> {
87+
pub(super) trait SignFn<T: AsRef<TaggedHash>> {
8888
/// Error type returned by the function.
8989
type Error;
9090

9191
/// Signs a [`TaggedHash`] computed over the merkle root of `message`'s TLV stream.
9292
fn sign(&self, message: &T) -> Result<Signature, Self::Error>;
9393
}
9494

95-
impl<F, T, E> SignFunction<T> for F
95+
impl<F, E> SignFn<TaggedHash> for F
9696
where
97-
F: Fn(&T) -> Result<Signature, E>,
98-
T: AsRef<TaggedHash>,
97+
F: Fn(&TaggedHash) -> Result<Signature, E>,
9998
{
10099
type Error = E;
101100

102-
fn sign(&self, message: &T) -> Result<Signature, E> {
101+
fn sign(&self, message: &TaggedHash) -> Result<Signature, E> {
103102
self(message)
104103
}
105104
}
@@ -117,7 +116,7 @@ pub(super) fn sign_message<F, T, E>(
117116
f: F, message: &T, pubkey: PublicKey,
118117
) -> Result<Signature, SignError<E>>
119118
where
120-
F: SignFunction<T, Error = E>,
119+
F: SignFn<T, Error = E>,
121120
T: AsRef<TaggedHash>,
122121
{
123122
let signature = f.sign(message).map_err(|e| SignError::Signing(e))?;

0 commit comments

Comments
 (0)