Skip to content

Commit 83fe0b8

Browse files
committed
Fix space and execss indentation in channel_keys.rs
1 parent 74078c4 commit 83fe0b8

File tree

1 file changed

+109
-110
lines changed

1 file changed

+109
-110
lines changed

lightning/src/ln/channel_keys.rs

Lines changed: 109 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -25,69 +25,69 @@ use bitcoin::secp256k1::PublicKey;
2525
use bitcoin::hashes::sha256::Hash as Sha256;
2626

2727
macro_rules! doc_comment {
28-
($x:expr, $($tt:tt)*) => {
29-
#[doc = $x]
30-
$($tt)*
31-
};
28+
($x:expr, $($tt:tt)*) => {
29+
#[doc = $x]
30+
$($tt)*
31+
};
3232
}
3333
macro_rules! basepoint_impl {
34-
($BasepointT:ty) => {
35-
impl $BasepointT {
36-
/// Get inner Public Key
37-
pub fn to_public_key(&self) -> PublicKey {
38-
self.0
39-
}
40-
}
41-
42-
impl From<PublicKey> for $BasepointT {
43-
fn from(value: PublicKey) -> Self {
44-
Self(value)
45-
}
46-
}
47-
48-
}
34+
($BasepointT:ty) => {
35+
impl $BasepointT {
36+
/// Get inner Public Key
37+
pub fn to_public_key(&self) -> PublicKey {
38+
self.0
39+
}
40+
}
41+
42+
impl From<PublicKey> for $BasepointT {
43+
fn from(value: PublicKey) -> Self {
44+
Self(value)
45+
}
46+
}
47+
48+
}
4949
}
5050
macro_rules! key_impl {
51-
($BasepointT:ty, $KeyName:expr) => {
52-
doc_comment! {
53-
concat!("Generate ", $KeyName, " using per_commitment_point"),
54-
pub fn from_basepoint<T: secp256k1::Signing>(
55-
secp_ctx: &Secp256k1<T>,
56-
basepoint: &$BasepointT,
57-
per_commitment_point: &PublicKey,
58-
) -> Self {
59-
Self(derive_public_key(secp_ctx, per_commitment_point, &basepoint.0))
60-
}
61-
}
62-
63-
doc_comment! {
64-
concat!("Generate ", $KeyName, " from privkey"),
65-
pub fn from_secret_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, sk: &SecretKey) -> Self {
66-
Self(PublicKey::from_secret_key(&secp_ctx, &sk))
67-
}
68-
}
69-
70-
/// Get inner Public Key
71-
pub fn to_public_key(&self) -> PublicKey {
72-
self.0
73-
}
74-
}
51+
($BasepointT:ty, $KeyName:expr) => {
52+
doc_comment! {
53+
concat!("Generate ", $KeyName, " using per_commitment_point"),
54+
pub fn from_basepoint<T: secp256k1::Signing>(
55+
secp_ctx: &Secp256k1<T>,
56+
basepoint: &$BasepointT,
57+
per_commitment_point: &PublicKey,
58+
) -> Self {
59+
Self(derive_public_key(secp_ctx, per_commitment_point, &basepoint.0))
60+
}
61+
}
62+
63+
doc_comment! {
64+
concat!("Generate ", $KeyName, " from privkey"),
65+
pub fn from_secret_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, sk: &SecretKey) -> Self {
66+
Self(PublicKey::from_secret_key(&secp_ctx, &sk))
67+
}
68+
}
69+
70+
/// Get inner Public Key
71+
pub fn to_public_key(&self) -> PublicKey {
72+
self.0
73+
}
74+
}
7575
}
7676
macro_rules! key_read_write {
77-
($SelfT:ty) => {
78-
impl Writeable for $SelfT {
79-
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
80-
self.0.serialize().write(w)
81-
}
82-
}
83-
84-
impl Readable for $SelfT {
85-
fn read<R: io::Read>(r: &mut R) -> Result<Self, DecodeError> {
86-
let key: PublicKey = Readable::read(r)?;
87-
Ok(Self(key))
88-
}
89-
}
90-
}
77+
($SelfT:ty) => {
78+
impl Writeable for $SelfT {
79+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
80+
self.0.serialize().write(w)
81+
}
82+
}
83+
84+
impl Readable for $SelfT {
85+
fn read<R: io::Read>(r: &mut R) -> Result<Self, DecodeError> {
86+
let key: PublicKey = Readable::read(r)?;
87+
Ok(Self(key))
88+
}
89+
}
90+
}
9191
}
9292

9393

@@ -107,7 +107,7 @@ key_read_write!(DelayedPaymentBasepoint);
107107
pub struct DelayedPaymentKey(pub PublicKey);
108108

109109
impl DelayedPaymentKey {
110-
key_impl!(DelayedPaymentBasepoint, "delayedpubkey");
110+
key_impl!(DelayedPaymentBasepoint, "delayedpubkey");
111111
}
112112
key_read_write!(DelayedPaymentKey);
113113

@@ -127,7 +127,7 @@ key_read_write!(PaymentBasepoint);
127127
pub struct PaymentKey(pub PublicKey);
128128

129129
impl PaymentKey {
130-
key_impl!(PaymentBasepoint, "localpubkey");
130+
key_impl!(PaymentBasepoint, "localpubkey");
131131
}
132132
key_read_write!(PaymentKey);
133133

@@ -144,7 +144,7 @@ key_read_write!(HtlcBasepoint);
144144
pub struct HtlcKey(pub PublicKey);
145145

146146
impl HtlcKey {
147-
key_impl!(HtlcBasepoint, "htlcpubkey");
147+
key_impl!(HtlcBasepoint, "htlcpubkey");
148148
}
149149
key_read_write!(HtlcKey);
150150

@@ -156,7 +156,6 @@ fn derive_public_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, per_commitm
156156
sha.input(&per_commitment_point.serialize());
157157
sha.input(&base_point.serialize());
158158
let res = Sha256::from_engine(sha).to_byte_array();
159-
160159

161160
let hashkey = PublicKey::from_secret_key(&secp_ctx,
162161
&SecretKey::from_slice(&res).expect("Hashes should always be valid keys unless SHA-256 is broken"));
@@ -178,62 +177,62 @@ key_read_write!(RevocationBasepoint);
178177
pub struct RevocationKey(pub PublicKey);
179178

180179
impl RevocationKey {
181-
/// Derives a per-commitment-transaction revocation public key from its constituent parts. This is
182-
/// the public equivalend of derive_private_revocation_key - using only public keys to derive a
183-
/// public key instead of private keys.
184-
///
185-
/// Only the cheating participant owns a valid witness to propagate a revoked
186-
/// commitment transaction, thus per_commitment_point always come from cheater
187-
/// and revocation_base_point always come from punisher, which is the broadcaster
188-
/// of the transaction spending with this key knowledge.
189-
///
190-
/// Note that this is infallible iff we trust that at least one of the two input keys are randomly
191-
/// generated (ie our own).
192-
pub fn from_basepoint<T: secp256k1::Verification>(
193-
secp_ctx: &Secp256k1<T>,
194-
basepoint: &RevocationBasepoint,
195-
per_commitment_point: &PublicKey,
196-
) -> Self {
197-
let rev_append_commit_hash_key = {
198-
let mut sha = Sha256::engine();
199-
sha.input(&basepoint.to_public_key().serialize());
200-
sha.input(&per_commitment_point.serialize());
201-
202-
Sha256::from_engine(sha).to_byte_array()
203-
};
204-
let commit_append_rev_hash_key = {
205-
let mut sha = Sha256::engine();
206-
sha.input(&per_commitment_point.serialize());
207-
sha.input(&basepoint.to_public_key().serialize());
208-
209-
Sha256::from_engine(sha).to_byte_array()
210-
};
211-
212-
let countersignatory_contrib = basepoint.to_public_key().mul_tweak(&secp_ctx, &Scalar::from_be_bytes(rev_append_commit_hash_key).unwrap())
213-
.expect("Multiplying a valid public key by a hash is expected to never fail per secp256k1 docs");
214-
let broadcaster_contrib = (&per_commitment_point).mul_tweak(&secp_ctx, &Scalar::from_be_bytes(commit_append_rev_hash_key).unwrap())
215-
.expect("Multiplying a valid public key by a hash is expected to never fail per secp256k1 docs");
216-
let pk = countersignatory_contrib.combine(&broadcaster_contrib)
217-
.expect("Addition only fails if the tweak is the inverse of the key. This is not possible when the tweak commits to the key.");
218-
Self(pk)
219-
}
220-
221-
/// Get inner Public Key
222-
pub fn to_public_key(&self) -> PublicKey {
223-
self.0
224-
}
180+
/// Derives a per-commitment-transaction revocation public key from its constituent parts. This is
181+
/// the public equivalend of derive_private_revocation_key - using only public keys to derive a
182+
/// public key instead of private keys.
183+
///
184+
/// Only the cheating participant owns a valid witness to propagate a revoked
185+
/// commitment transaction, thus per_commitment_point always come from cheater
186+
/// and revocation_base_point always come from punisher, which is the broadcaster
187+
/// of the transaction spending with this key knowledge.
188+
///
189+
/// Note that this is infallible iff we trust that at least one of the two input keys are randomly
190+
/// generated (ie our own).
191+
pub fn from_basepoint<T: secp256k1::Verification>(
192+
secp_ctx: &Secp256k1<T>,
193+
basepoint: &RevocationBasepoint,
194+
per_commitment_point: &PublicKey,
195+
) -> Self {
196+
let rev_append_commit_hash_key = {
197+
let mut sha = Sha256::engine();
198+
sha.input(&basepoint.to_public_key().serialize());
199+
sha.input(&per_commitment_point.serialize());
200+
201+
Sha256::from_engine(sha).to_byte_array()
202+
};
203+
let commit_append_rev_hash_key = {
204+
let mut sha = Sha256::engine();
205+
sha.input(&per_commitment_point.serialize());
206+
sha.input(&basepoint.to_public_key().serialize());
207+
208+
Sha256::from_engine(sha).to_byte_array()
209+
};
210+
211+
let countersignatory_contrib = basepoint.to_public_key().mul_tweak(&secp_ctx, &Scalar::from_be_bytes(rev_append_commit_hash_key).unwrap())
212+
.expect("Multiplying a valid public key by a hash is expected to never fail per secp256k1 docs");
213+
let broadcaster_contrib = (&per_commitment_point).mul_tweak(&secp_ctx, &Scalar::from_be_bytes(commit_append_rev_hash_key).unwrap())
214+
.expect("Multiplying a valid public key by a hash is expected to never fail per secp256k1 docs");
215+
let pk = countersignatory_contrib.combine(&broadcaster_contrib)
216+
.expect("Addition only fails if the tweak is the inverse of the key. This is not possible when the tweak commits to the key.");
217+
Self(pk)
218+
}
219+
220+
/// Get inner Public Key
221+
pub fn to_public_key(&self) -> PublicKey {
222+
self.0
223+
}
225224
}
226225
key_read_write!(RevocationKey);
227226

228227

229228

230229
#[cfg(test)]
231230
mod test {
232-
use bitcoin::secp256k1::{Secp256k1, SecretKey, PublicKey};
233-
use bitcoin::hashes::hex::FromHex;
234-
use super::derive_public_key;
231+
use bitcoin::secp256k1::{Secp256k1, SecretKey, PublicKey};
232+
use bitcoin::hashes::hex::FromHex;
233+
use super::derive_public_key;
235234

236-
#[test]
235+
#[test]
237236
fn test_key_derivation() {
238237
// Test vectors from BOLT 3 Appendix E:
239238
let secp_ctx = Secp256k1::new();
@@ -248,6 +247,6 @@ mod test {
248247
assert_eq!(per_commitment_point.serialize()[..], <Vec<u8>>::from_hex("025f7117a78150fe2ef97db7cfc83bd57b2e2c0d0dd25eaf467a4a1c2a45ce1486").unwrap()[..]);
249248

250249
assert_eq!(derive_public_key(&secp_ctx, &per_commitment_point, &base_point).serialize()[..],
251-
<Vec<u8>>::from_hex("0235f2dbfaa89b57ec7b055afe29849ef7ddfeb1cefdb9ebdc43f5494984db29e5").unwrap()[..]);
250+
<Vec<u8>>::from_hex("0235f2dbfaa89b57ec7b055afe29849ef7ddfeb1cefdb9ebdc43f5494984db29e5").unwrap()[..]);
252251
}
253252
}

0 commit comments

Comments
 (0)