Skip to content

Commit 3db675d

Browse files
committed
Add SignFunction trait to document parameters
1 parent 71d7a3c commit 3db675d

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::{ParseError, ParsedMessage, SemanticError};
116116
use crate::offers::payer::{PAYER_METADATA_TYPE, PayerTlvStream, PayerTlvStreamRef};
@@ -381,10 +381,7 @@ impl<'a> UnsignedInvoice<'a> {
381381
/// Signs the invoice using the given function.
382382
///
383383
/// This is not exported to bindings users as functions aren't currently mapped.
384-
pub fn sign<F, E>(self, sign: F) -> Result<Invoice, SignError<E>>
385-
where
386-
F: FnOnce(&TaggedHash, &[u8]) -> Result<Signature, E>
387-
{
384+
pub fn sign<F: SignFunction<E>, E>(self, sign: F) -> Result<Invoice, SignError<E>> {
388385
// Use the invoice_request bytes instead of the invoice_request TLV stream as the latter may
389386
// have contained unknown TLV records, which are not stored in `InvoiceRequestContents` or
390387
// `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::{ParseError, ParsedMessage, SemanticError};
7272
use crate::offers::payer::{PayerContents, PayerTlvStream, PayerTlvStreamRef};
@@ -352,10 +352,7 @@ impl<'a> UnsignedInvoiceRequest<'a> {
352352
/// Signs the invoice request using the given function.
353353
///
354354
/// This is not exported to bindings users as functions are not yet mapped.
355-
pub fn sign<F, E>(self, sign: F) -> Result<InvoiceRequest, SignError<E>>
356-
where
357-
F: FnOnce(&TaggedHash, &[u8]) -> Result<Signature, E>
358-
{
355+
pub fn sign<F: SignFunction<E>, E>(self, sign: F) -> Result<InvoiceRequest, SignError<E>> {
359356
// Use the offer bytes instead of the offer TLV stream as the offer may have contained
360357
// unknown TLV records, which are not stored in `OfferContents`.
361358
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: &'a str,
3747
tlv_stream: &'a [u8],
@@ -67,12 +77,9 @@ pub enum SignError<E> {
6777
///
6878
/// Panics if the underlying TLV stream of `message` is not a well-formed TLV stream containing at
6979
/// least one TLV record.
70-
pub(super) fn sign_message<F, E>(
80+
pub(super) fn sign_message<F: SignFunction<E>, E>(
7181
sign: F, message: TaggedHash, metadata: &[u8], pubkey: PublicKey,
72-
) -> Result<Signature, SignError<E>>
73-
where
74-
F: FnOnce(&TaggedHash, &[u8]) -> Result<Signature, E>
75-
{
82+
) -> Result<Signature, SignError<E>> {
7683
let signature = sign(&message, metadata).map_err(|e| SignError::Signing(e))?;
7784

7885
let pubkey = pubkey.into();

0 commit comments

Comments
 (0)