@@ -25,13 +25,23 @@ tlv_stream!(SignatureTlvStream, SignatureTlvStreamRef, SIGNATURE_TYPES, {
25
25
( 240 , signature: Signature ) ,
26
26
} ) ;
27
27
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
+
28
37
/// A hash for use in a specific context by tweaking with a context-dependent tag as per [BIP 340]
29
38
/// and computed over the merkle root of a TLV stream to sign as defined in [BOLT 12].
30
39
///
31
40
/// The hash is computed lazily from the TLV stream.
32
41
///
33
42
/// [BIP 340]: https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki
34
43
/// [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.
35
45
pub struct TaggedHash < ' a > {
36
46
/// Tag used for the hash function as defined in BOLT 12. Typically, either
37
47
/// [`invoice_request::SIGNATURE_TAG`] or [`invoice::SIGNATURE_TAG`].
@@ -76,12 +86,9 @@ pub enum SignError<E> {
76
86
///
77
87
/// Panics if the underlying TLV stream of `message` is not a well-formed TLV stream containing at
78
88
/// least one TLV record.
79
- pub ( super ) fn sign_message < F , E > (
89
+ pub ( super ) fn sign_message < F : SignFunction < E > , E > (
80
90
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 > > {
85
92
let signature = sign ( & message, metadata) . map_err ( |e| SignError :: Signing ( e) ) ?;
86
93
87
94
let pubkey = pubkey. into ( ) ;
0 commit comments