@@ -31,26 +31,30 @@ macro_rules! doc_comment {
3131 } ;
3232}
3333macro_rules! basepoint_impl {
34- ( $BasepointT: ty) => {
34+ ( $BasepointT: ty $ ( , $KeyName : expr ) ? ) => {
3535 impl $BasepointT {
3636 /// Get inner Public Key
3737 pub fn to_public_key( & self ) -> PublicKey {
3838 self . 0
3939 }
4040
41- /// Derives a per-commitment-transaction (eg an htlc key or delayed_payment key) private key addition tweak
42- /// from a basepoint and a per_commitment_point:
43- /// `privkey = basepoint_secret + SHA256(per_commitment_point || basepoint)`
44- /// This calculates the hash part in the tweak derivation process, which is used to ensure
45- /// that each key is unique and cannot be guessed by an external party. It is equivalent
46- /// to the `from_basepoint` method, but without the addition operation, providing just the
47- /// tweak from the hash of the per_commitment_point and the basepoint.
48- pub fn derive_add_tweak( & self , per_commitment_point: & PublicKey ) -> [ u8 ; 32 ] {
49- let mut sha = Sha256 :: engine( ) ;
50- sha. input( & per_commitment_point. serialize( ) ) ;
51- sha. input( & self . to_public_key( ) . serialize( ) ) ;
52- Sha256 :: from_engine( sha) . to_byte_array( )
53- }
41+ $( doc_comment!(
42+ concat!(
43+ "Derives the \" tweak\" used in calculate [`" , $KeyName, "::from_basepoint`].\n " ,
44+ "\n " ,
45+ "[`" , $KeyName, "::from_basepoint`] calculates a private key as:\n " ,
46+ "`privkey = basepoint_secret + SHA256(per_commitment_point || basepoint)`\n " ,
47+ "\n " ,
48+ "This calculates the hash part in the tweak derivation process, which is used to\n " ,
49+ "ensure that each key is unique and cannot be guessed by an external party."
50+ ) ,
51+ pub fn derive_add_tweak( & self , per_commitment_point: & PublicKey ) -> Sha256 {
52+ let mut sha = Sha256 :: engine( ) ;
53+ sha. input( & per_commitment_point. serialize( ) ) ;
54+ sha. input( & self . to_public_key( ) . serialize( ) ) ;
55+ Sha256 :: from_engine( sha)
56+ } ) ;
57+ ) ?
5458 }
5559
5660 impl From <PublicKey > for $BasepointT {
@@ -110,7 +114,7 @@ macro_rules! key_read_write {
110114/// state broadcasted was previously revoked.
111115#[ derive( PartialEq , Eq , Clone , Copy , Debug , Hash ) ]
112116pub struct DelayedPaymentBasepoint ( pub PublicKey ) ;
113- basepoint_impl ! ( DelayedPaymentBasepoint ) ;
117+ basepoint_impl ! ( DelayedPaymentBasepoint , "DelayedPaymentKey" ) ;
114118key_read_write ! ( DelayedPaymentBasepoint ) ;
115119
116120/// A derived key built from a [`DelayedPaymentBasepoint`] and `per_commitment_point`.
@@ -137,7 +141,7 @@ key_read_write!(DelayedPaymentKey);
137141/// Thus, both channel counterparties' HTLC keys will appears in each HTLC output's script.
138142#[ derive( PartialEq , Eq , Clone , Copy , Debug , Hash ) ]
139143pub struct HtlcBasepoint ( pub PublicKey ) ;
140- basepoint_impl ! ( HtlcBasepoint ) ;
144+ basepoint_impl ! ( HtlcBasepoint , "HtlcKey" ) ;
141145key_read_write ! ( HtlcBasepoint ) ;
142146
143147/// A derived key built from a [`HtlcBasepoint`] and `per_commitment_point`.
@@ -166,18 +170,20 @@ fn derive_public_key<T: secp256k1::Signing>(
166170 let mut sha = Sha256 :: engine ( ) ;
167171 sha. input ( & per_commitment_point. serialize ( ) ) ;
168172 sha. input ( & base_point. serialize ( ) ) ;
169- let res = Sha256 :: from_engine ( sha) . to_byte_array ( ) ;
173+ let res = Sha256 :: from_engine ( sha) ;
170174
171175 add_public_key_tweak ( secp_ctx, base_point, & res)
172176}
173177
174178/// Adds a tweak to a public key to derive a new public key.
179+ ///
180+ /// May panic if `tweak` is not the output of a SHA-256 hash.
175181pub fn add_public_key_tweak < T : secp256k1:: Signing > (
176- secp_ctx : & Secp256k1 < T > , base_point : & PublicKey , tweak : & [ u8 ; 32 ] ,
182+ secp_ctx : & Secp256k1 < T > , base_point : & PublicKey , tweak : & Sha256 ,
177183) -> PublicKey {
178184 let hashkey = PublicKey :: from_secret_key (
179185 & secp_ctx,
180- & SecretKey :: from_slice ( tweak)
186+ & SecretKey :: from_slice ( tweak. as_byte_array ( ) )
181187 . expect ( "Hashes should always be valid keys unless SHA-256 is broken" ) ,
182188 ) ;
183189 base_point. combine ( & hashkey)
0 commit comments