Skip to content

Commit 57c8275

Browse files
committed
Only include derive_add_tweak for base key types that use it
Specifically `RevocationBasepoint` has a different derivation, so shouldn't have a `derive_add_tweak` at all. We also use this opportunity to link to the `from_basepoint` function in the `derive_add_tweak` docs.
1 parent db31f43 commit 57c8275

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

lightning/src/ln/channel_keys.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,30 @@ macro_rules! doc_comment {
3131
};
3232
}
3333
macro_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 the "tweak" used to calculate the per-commitment private key.
42-
///
43-
/// The per-commitment private key is calculates a private key as:
44-
/// `privkey = basepoint_secret + SHA256(per_commitment_point || basepoint)`
45-
///
46-
/// This calculates the hash part in the tweak derivation process, which is used to
47-
/// ensure that each key is unique and cannot be guessed by an external party.
48-
pub fn derive_add_tweak(&self, per_commitment_point: &PublicKey) -> Sha256 {
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)
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)]
112116
pub struct DelayedPaymentBasepoint(pub PublicKey);
113-
basepoint_impl!(DelayedPaymentBasepoint);
117+
basepoint_impl!(DelayedPaymentBasepoint, "DelayedPaymentKey");
114118
key_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)]
139143
pub struct HtlcBasepoint(pub PublicKey);
140-
basepoint_impl!(HtlcBasepoint);
144+
basepoint_impl!(HtlcBasepoint, "HtlcKey");
141145
key_read_write!(HtlcBasepoint);
142146

143147
/// A derived key built from a [`HtlcBasepoint`] and `per_commitment_point`.

0 commit comments

Comments
 (0)