@@ -25,7 +25,8 @@ use bitcoin::hashes::sha256d::Hash as Sha256dHash;
2525use bitcoin:: hash_types:: WPubkeyHash ;
2626
2727use bitcoin:: secp256k1:: key:: { SecretKey , PublicKey } ;
28- use bitcoin:: secp256k1:: { Secp256k1 , Signature , Signing } ;
28+ use bitcoin:: secp256k1:: { Message , Secp256k1 , Signature , Signing } ;
29+ use bitcoin:: secp256k1:: recovery:: RecoverableSignature ;
2930use bitcoin:: secp256k1;
3031
3132use util:: { byte_utils, transaction_utils} ;
@@ -391,6 +392,12 @@ pub trait KeysInterface: Send + Sync {
391392 /// contain no versioning scheme. You may wish to include your own version prefix and ensure
392393 /// you've read all of the provided bytes to ensure no corruption occurred.
393394 fn read_chan_signer ( & self , reader : & [ u8 ] ) -> Result < Self :: Signer , DecodeError > ;
395+
396+ /// Sign an invoice's preimage (note that this is the preimage of the invoice, not the HTLC's
397+ /// preimage). By parameterizing by the preimage instead of the hash, we allow implementors of
398+ /// this trait to parse the invoice and make sure they're signing what they expect, rather than
399+ /// blindly signing the hash.
400+ fn sign_invoice ( & self , invoice_preimage : Vec < u8 > ) -> Result < RecoverableSignature , ( ) > ;
394401}
395402
396403#[ derive( Clone ) ]
@@ -1047,6 +1054,15 @@ impl KeysInterface for KeysManager {
10471054 fn read_chan_signer ( & self , reader : & [ u8 ] ) -> Result < Self :: Signer , DecodeError > {
10481055 InMemorySigner :: read ( & mut std:: io:: Cursor :: new ( reader) )
10491056 }
1057+
1058+ fn sign_invoice ( & self , invoice_preimage : Vec < u8 > ) -> Result < RecoverableSignature , ( ) > {
1059+ let secp_ctx = Secp256k1 :: new ( ) ;
1060+ let mut raw_hash: [ u8 ; 32 ] = Default :: default ( ) ;
1061+ raw_hash. copy_from_slice ( & Sha256 :: hash ( & invoice_preimage) [ ..] ) ;
1062+ let msg_hash = Message :: from_slice ( & raw_hash[ ..] )
1063+ . expect ( "Hash is 32 bytes long, same as MESSAGE_SIZE" ) ;
1064+ Ok ( secp_ctx. sign_recoverable ( & msg_hash, & self . get_node_secret ( ) ) )
1065+ }
10501066}
10511067
10521068// Ensure that BaseSign can have a vtable
0 commit comments