@@ -31,26 +31,30 @@ macro_rules! doc_comment {
31
31
} ;
32
32
}
33
33
macro_rules! basepoint_impl {
34
- ( $BasepointT: ty) => {
34
+ ( $BasepointT: ty $ ( , $KeyName : expr ) ? ) => {
35
35
impl $BasepointT {
36
36
/// Get inner Public Key
37
37
pub fn to_public_key( & self ) -> PublicKey {
38
38
self . 0
39
39
}
40
40
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
+ ) ?
54
58
}
55
59
56
60
impl From <PublicKey > for $BasepointT {
@@ -110,7 +114,7 @@ macro_rules! key_read_write {
110
114
/// state broadcasted was previously revoked.
111
115
#[ derive( PartialEq , Eq , Clone , Copy , Debug , Hash ) ]
112
116
pub struct DelayedPaymentBasepoint ( pub PublicKey ) ;
113
- basepoint_impl ! ( DelayedPaymentBasepoint ) ;
117
+ basepoint_impl ! ( DelayedPaymentBasepoint , "DelayedPaymentKey" ) ;
114
118
key_read_write ! ( DelayedPaymentBasepoint ) ;
115
119
116
120
/// A derived key built from a [`DelayedPaymentBasepoint`] and `per_commitment_point`.
@@ -137,7 +141,7 @@ key_read_write!(DelayedPaymentKey);
137
141
/// Thus, both channel counterparties' HTLC keys will appears in each HTLC output's script.
138
142
#[ derive( PartialEq , Eq , Clone , Copy , Debug , Hash ) ]
139
143
pub struct HtlcBasepoint ( pub PublicKey ) ;
140
- basepoint_impl ! ( HtlcBasepoint ) ;
144
+ basepoint_impl ! ( HtlcBasepoint , "HtlcKey" ) ;
141
145
key_read_write ! ( HtlcBasepoint ) ;
142
146
143
147
/// A derived key built from a [`HtlcBasepoint`] and `per_commitment_point`.
@@ -166,18 +170,20 @@ fn derive_public_key<T: secp256k1::Signing>(
166
170
let mut sha = Sha256 :: engine ( ) ;
167
171
sha. input ( & per_commitment_point. serialize ( ) ) ;
168
172
sha. input ( & base_point. serialize ( ) ) ;
169
- let res = Sha256 :: from_engine ( sha) . to_byte_array ( ) ;
173
+ let res = Sha256 :: from_engine ( sha) ;
170
174
171
175
add_public_key_tweak ( secp_ctx, base_point, & res)
172
176
}
173
177
174
178
/// 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.
175
181
pub 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 ,
177
183
) -> PublicKey {
178
184
let hashkey = PublicKey :: from_secret_key (
179
185
& secp_ctx,
180
- & SecretKey :: from_slice ( tweak)
186
+ & SecretKey :: from_slice ( tweak. as_byte_array ( ) )
181
187
. expect ( "Hashes should always be valid keys unless SHA-256 is broken" ) ,
182
188
) ;
183
189
base_point. combine ( & hashkey)
0 commit comments