Skip to content

Commit 63394d1

Browse files
committed
Add SignFunction trait to document parameters
1 parent 4d3dd26 commit 63394d1

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

lightning/src/offers/invoice.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ use crate::ln::features::{BlindedHopFeatures, Bolt12InvoiceFeatures};
110110
use crate::ln::inbound_payment::ExpandedKey;
111111
use crate::ln::msgs::DecodeError;
112112
use crate::offers::invoice_request::{INVOICE_REQUEST_PAYER_ID_TYPE, INVOICE_REQUEST_TYPES, IV_BYTES as INVOICE_REQUEST_IV_BYTES, InvoiceRequest, InvoiceRequestContents, InvoiceRequestTlvStream, InvoiceRequestTlvStreamRef};
113-
use crate::offers::merkle::{SignError, SignatureTlvStream, SignatureTlvStreamRef, TaggedHash, TlvStream, WithoutSignatures, self};
113+
use crate::offers::merkle::{SignError, SignFunction, SignatureTlvStream, SignatureTlvStreamRef, TaggedHash, TlvStream, WithoutSignatures, self};
114114
use crate::offers::offer::{Amount, OFFER_TYPES, OfferTlvStream, OfferTlvStreamRef};
115115
use crate::offers::parse::{Bolt12ParseError, Bolt12SemanticError, ParsedMessage};
116116
use crate::offers::payer::{PAYER_METADATA_TYPE, PayerTlvStream, PayerTlvStreamRef};
@@ -383,10 +383,7 @@ impl<'a> UnsignedBolt12Invoice<'a> {
383383
/// Signs the invoice using the given function.
384384
///
385385
/// This is not exported to bindings users as functions aren't currently mapped.
386-
pub fn sign<F, E>(self, sign: F) -> Result<Bolt12Invoice, SignError<E>>
387-
where
388-
F: FnOnce(&TaggedHash, &[u8]) -> Result<Signature, E>
389-
{
386+
pub fn sign<F: SignFunction<E>, E>(self, sign: F) -> Result<Bolt12Invoice, SignError<E>> {
390387
// Use the invoice_request bytes instead of the invoice_request TLV stream as the latter may
391388
// have contained unknown TLV records, which are not stored in `InvoiceRequestContents` or
392389
// `RefundContents`.

lightning/src/offers/invoice_request.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ use crate::ln::features::InvoiceRequestFeatures;
6666
use crate::ln::inbound_payment::{ExpandedKey, IV_LEN, Nonce};
6767
use crate::ln::msgs::DecodeError;
6868
use crate::offers::invoice::{BlindedPayInfo, DerivedSigningPubkey, ExplicitSigningPubkey, InvoiceBuilder};
69-
use crate::offers::merkle::{SignError, SignatureTlvStream, SignatureTlvStreamRef, TaggedHash, self};
69+
use crate::offers::merkle::{SignError, SignFunction, SignatureTlvStream, SignatureTlvStreamRef, TaggedHash, self};
7070
use crate::offers::offer::{Offer, OfferContents, OfferTlvStream, OfferTlvStreamRef};
7171
use crate::offers::parse::{Bolt12ParseError, ParsedMessage, Bolt12SemanticError};
7272
use crate::offers::payer::{PayerContents, PayerTlvStream, PayerTlvStreamRef};
@@ -353,10 +353,7 @@ impl<'a> UnsignedInvoiceRequest<'a> {
353353
/// Signs the invoice request using the given function.
354354
///
355355
/// This is not exported to bindings users as functions are not yet mapped.
356-
pub fn sign<F, E>(self, sign: F) -> Result<InvoiceRequest, SignError<E>>
357-
where
358-
F: FnOnce(&TaggedHash, &[u8]) -> Result<Signature, E>
359-
{
356+
pub fn sign<F: SignFunction<E>, E>(self, sign: F) -> Result<InvoiceRequest, SignError<E>> {
360357
// Use the offer bytes instead of the offer TLV stream as the offer may have contained
361358
// unknown TLV records, which are not stored in `OfferContents`.
362359
let (payer_tlv_stream, _offer_tlv_stream, invoice_request_tlv_stream) =

lightning/src/offers/merkle.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,23 @@ tlv_stream!(SignatureTlvStream, SignatureTlvStreamRef, SIGNATURE_TYPES, {
2525
(240, signature: Signature),
2626
});
2727

28+
/// A function for signing a [`Message`] digest produced from the given [`TaggedHash`]. See
29+
/// [`TaggedHash`] for details.
30+
///
31+
/// The second parameter is either the payer or offer metadata, depending on the TLV stream
32+
/// represented by the first parameter, and may be useful in order to derive the signing keys.
33+
pub trait SignFunction<E>: FnOnce(&TaggedHash, &[u8]) -> Result<Signature, E> {}
34+
35+
impl<F, E> SignFunction<E> for F where F: FnOnce(&TaggedHash, &[u8]) -> Result<Signature, E> {}
36+
2837
/// A hash for use in a specific context by tweaking with a context-dependent tag as per [BIP 340]
2938
/// and computed over the merkle root of a TLV stream to sign as defined in [BOLT 12].
3039
///
3140
/// The hash is computed lazily from the TLV stream.
3241
///
3342
/// [BIP 340]: https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki
3443
/// [BOLT 12]: https://github.com/rustyrussell/lightning-rfc/blob/guilt/offers/12-offer-encoding.md#signature-calculation
44+
/// Bytes associated with a tag, which are used to produced a [`Message`] digest to sign.
3545
pub struct TaggedHash<'a> {
3646
/// Tag used for the hash function as defined in BOLT 12. Typically, either
3747
/// [`invoice_request::SIGNATURE_TAG`] or [`invoice::SIGNATURE_TAG`].
@@ -76,12 +86,9 @@ pub enum SignError<E> {
7686
///
7787
/// Panics if the underlying TLV stream of `message` is not a well-formed TLV stream containing at
7888
/// least one TLV record.
79-
pub(super) fn sign_message<F, E>(
89+
pub(super) fn sign_message<F: SignFunction<E>, E>(
8090
sign: F, message: TaggedHash, metadata: &[u8], pubkey: PublicKey,
81-
) -> Result<Signature, SignError<E>>
82-
where
83-
F: FnOnce(&TaggedHash, &[u8]) -> Result<Signature, E>
84-
{
91+
) -> Result<Signature, SignError<E>> {
8592
let signature = sign(&message, metadata).map_err(|e| SignError::Signing(e))?;
8693

8794
let pubkey = pubkey.into();

0 commit comments

Comments
 (0)