Skip to content

Commit f50708b

Browse files
committed
DRY basepoint and docs fix.
1 parent 2e1737e commit f50708b

File tree

3 files changed

+29
-68
lines changed

3 files changed

+29
-68
lines changed

lightning/src/ln/channel_keys.rs

Lines changed: 25 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@ macro_rules! doc_comment {
3131
$($tt)*
3232
};
3333
}
34+
macro_rules! basepoint_impl {
35+
($BasepointT:ty) => {
36+
impl $BasepointT {
37+
/// Get inner Public Key
38+
pub fn to_public_key(&self) -> PublicKey {
39+
self.0
40+
}
41+
}
42+
43+
impl From<PublicKey> for $BasepointT {
44+
fn from(value: PublicKey) -> Self {
45+
Self(value)
46+
}
47+
}
48+
49+
}
50+
}
3451
macro_rules! key_impl {
3552
($BasepointT:ty, $KeyName:expr) => {
3653
doc_comment! {
@@ -80,24 +97,10 @@ macro_rules! key_read_write {
8097

8198

8299

83-
/// [delayed_payment_basepoint](https://github.com/lightning/bolts/blob/master/03-transactions.md#key-derivation)
84-
/// Used to generate local_delayedpubkey for the latest state of a channel.
100+
/// Used to generate [`local_delayedpubkey`](https://github.com/lightning/bolts/blob/master/03-transactions.md#key-derivation) for the latest state of a channel.
85101
#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)]
86102
pub struct DelayedPaymentBasepoint(PublicKey);
87-
88-
impl DelayedPaymentBasepoint {
89-
/// Get inner Public Key
90-
pub fn to_public_key(&self) -> PublicKey {
91-
self.0
92-
}
93-
}
94-
95-
impl From<PublicKey> for DelayedPaymentBasepoint {
96-
fn from(value: PublicKey) -> Self {
97-
Self(value)
98-
}
99-
}
100-
103+
basepoint_impl!(DelayedPaymentBasepoint);
101104
key_read_write!(DelayedPaymentBasepoint);
102105

103106
/// [delayedpubkey](https://github.com/lightning/bolts/blob/master/03-transactions.md#localpubkey-local_htlcpubkey-remote_htlcpubkey-local_delayedpubkey-and-remote_delayedpubkey-derivation)
@@ -110,24 +113,10 @@ impl DelayedPaymentKey {
110113
}
111114
key_read_write!(DelayedPaymentKey);
112115

113-
/// [payment_basepoint](https://github.com/lightning/bolts/blob/master/03-transactions.md#key-derivation)
114-
/// Used to generate localpubkey for the latest state of a channel.
116+
/// Used to generate [localpubkey](https://github.com/lightning/bolts/blob/master/03-transactions.md#key-derivation) for the latest state of a channel.
115117
#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)]
116118
pub struct PaymentBasepoint(PublicKey);
117-
118-
impl PaymentBasepoint {
119-
/// Get inner Public Key
120-
pub fn to_public_key(&self) -> PublicKey {
121-
self.0
122-
}
123-
}
124-
125-
impl From<PublicKey> for PaymentBasepoint {
126-
fn from(value: PublicKey) -> Self {
127-
Self(value)
128-
}
129-
}
130-
119+
basepoint_impl!(PaymentBasepoint);
131120
key_read_write!(PaymentBasepoint);
132121

133122

@@ -141,24 +130,10 @@ impl PaymentKey {
141130
}
142131
key_read_write!(PaymentKey);
143132

144-
/// [htlc_basepoint](https://github.com/lightning/bolts/blob/master/03-transactions.md#key-derivation)
145-
/// Used to generate htlcpubkey for the latest state of a channel.
133+
/// Used to generate [htlcpubkey](https://github.com/lightning/bolts/blob/master/03-transactions.md#key-derivation) for the latest state of a channel.
146134
#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)]
147135
pub struct HtlcBasepoint(PublicKey);
148-
149-
impl HtlcBasepoint {
150-
/// Get inner Public Key
151-
pub fn to_public_key(&self) -> PublicKey {
152-
self.0
153-
}
154-
}
155-
156-
impl From<PublicKey> for HtlcBasepoint {
157-
fn from(value: PublicKey) -> Self {
158-
Self(value)
159-
}
160-
}
161-
136+
basepoint_impl!(HtlcBasepoint);
162137
key_read_write!(HtlcBasepoint);
163138

164139

@@ -187,24 +162,10 @@ fn derive_public_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, per_commitm
187162
.expect("Addition only fails if the tweak is the inverse of the key. This is not possible when the tweak contains the hash of the key.")
188163
}
189164

190-
/// [revocation_basepoint](https://github.com/lightning/bolts/blob/master/03-transactions.md#key-derivation)
191-
/// Used to generate htlcpubkey for the latest state of a channel.
165+
/// Used to generate [htlcpubkey](https://github.com/lightning/bolts/blob/master/03-transactions.md#key-derivation) for the latest state of a channel.
192166
#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)]
193167
pub struct RevocationBasepoint(PublicKey);
194-
195-
impl RevocationBasepoint {
196-
/// Get inner Public Key
197-
pub fn to_public_key(&self) -> PublicKey {
198-
self.0
199-
}
200-
}
201-
202-
impl From<PublicKey> for RevocationBasepoint {
203-
fn from(value: PublicKey) -> Self {
204-
Self(value)
205-
}
206-
}
207-
168+
basepoint_impl!(RevocationBasepoint);
208169
key_read_write!(RevocationBasepoint);
209170

210171

lightning/src/sign/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,7 +1306,7 @@ impl EcdsaChannelSigner for InMemorySigner {
13061306
&secp_ctx, &HtlcBasepoint::from(counterparty_keys.htlc_basepoint), &per_commitment_point,
13071307
);
13081308
let holder_htlcpubkey = HtlcKey::from_basepoint(
1309-
&secp_ctx, &HtlcBasepoint::from(self.pubkeys().htlc_basepoint.to_owned()), &per_commitment_point,
1309+
&secp_ctx, &HtlcBasepoint::from(self.pubkeys().htlc_basepoint), &per_commitment_point,
13101310
);
13111311
let chan_type = self.channel_type_features().expect(MISSING_PARAMS_ERR);
13121312
chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, chan_type, &counterparty_htlcpubkey, &holder_htlcpubkey, &revocation_pubkey)
@@ -1337,10 +1337,10 @@ impl EcdsaChannelSigner for InMemorySigner {
13371337
);
13381338
let counterparty_keys = self.counterparty_pubkeys().expect(MISSING_PARAMS_ERR);
13391339
let counterparty_htlcpubkey = HtlcKey::from_basepoint(
1340-
&secp_ctx, &HtlcBasepoint::from(counterparty_keys.htlc_basepoint.to_owned()), &per_commitment_point,
1340+
&secp_ctx, &HtlcBasepoint::from(counterparty_keys.htlc_basepoint), &per_commitment_point,
13411341
);
13421342
let htlcpubkey = HtlcKey::from_basepoint(
1343-
&secp_ctx, &HtlcBasepoint::from(self.pubkeys().htlc_basepoint.to_owned()), &per_commitment_point,
1343+
&secp_ctx, &HtlcBasepoint::from(self.pubkeys().htlc_basepoint), &per_commitment_point,
13441344
);
13451345
let chan_type = self.channel_type_features().expect(MISSING_PARAMS_ERR);
13461346
let witness_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, chan_type, &counterparty_htlcpubkey, &htlcpubkey, &revocation_pubkey);

lightning/src/util/test_channel_signer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ impl EcdsaChannelSigner for TestChannelSigner {
208208
input, &witness_script, htlc_descriptor.htlc.amount_msat / 1000, sighash_type
209209
).unwrap();
210210
let countersignatory_htlc_key = HtlcKey::from_basepoint(
211-
&secp_ctx,&HtlcBasepoint::from(self.inner.counterparty_pubkeys().unwrap().htlc_basepoint.to_owned()), &htlc_descriptor.per_commitment_point,
211+
&secp_ctx,&HtlcBasepoint::from(self.inner.counterparty_pubkeys().unwrap().htlc_basepoint), &htlc_descriptor.per_commitment_point,
212212
);
213213

214214
secp_ctx.verify_ecdsa(

0 commit comments

Comments
 (0)