@@ -25,6 +25,15 @@ 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 [`TaggedBytes`]. See
29
+ /// [`TaggedBytes::digest`] 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 ( & TaggedBytes , & [ u8 ] ) -> Result < Signature , E > { }
34
+
35
+ impl < F , E > SignFunction < E > for F where F : FnOnce ( & TaggedBytes , & [ u8 ] ) -> Result < Signature , E > { }
36
+
28
37
/// Bytes associated with a tag, which are used to produced a [`Message`] digest to sign.
29
38
pub struct TaggedBytes < ' a > {
30
39
tag : & ' a str ,
@@ -38,7 +47,8 @@ impl<'a> TaggedBytes<'a> {
38
47
Self { tag, bytes, digest : RefCell :: new ( None ) }
39
48
}
40
49
41
- /// Returns the digest to sign.
50
+ /// Returns the digest to sign as defined in the BOLT 12 spec's "Signature Calculation" section.
51
+ /// (i.e., the tagged merkle root of a TLV stream).
42
52
pub fn digest ( & self ) -> Message {
43
53
* self . digest . borrow_mut ( ) . get_or_insert_with ( || message_digest ( self . tag , self . bytes ) )
44
54
}
@@ -64,7 +74,7 @@ pub(super) fn sign_message<F, E>(
64
74
sign : F , message : TaggedBytes , metadata : & [ u8 ] , pubkey : PublicKey ,
65
75
) -> Result < Signature , SignError < E > >
66
76
where
67
- F : FnOnce ( & TaggedBytes , & [ u8 ] ) -> Result < Signature , E >
77
+ F : SignFunction < E >
68
78
{
69
79
let signature = sign ( & message, metadata) . map_err ( |e| SignError :: Signing ( e) ) ?;
70
80
0 commit comments