Skip to content

Commit

Permalink
DRY basepoint and docs fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
yellowred committed Nov 13, 2023
1 parent 2ad5127 commit 90e24a5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 68 deletions.
89 changes: 25 additions & 64 deletions lightning/src/ln/channel_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@ macro_rules! doc_comment {
$($tt)*
};
}
macro_rules! basepoint_impl {
($BasepointT:ty) => {
impl $BasepointT {
/// Get inner Public Key
pub fn to_public_key(&self) -> PublicKey {
self.0
}
}

impl From<PublicKey> for $BasepointT {
fn from(value: PublicKey) -> Self {
Self(value)
}
}

}
}
macro_rules! key_impl {
($BasepointT:ty, $KeyName:expr) => {
doc_comment! {
Expand Down Expand Up @@ -80,24 +97,10 @@ macro_rules! key_read_write {



/// [delayed_payment_basepoint](https://github.com/lightning/bolts/blob/master/03-transactions.md#key-derivation)
/// Used to generate local_delayedpubkey for the latest state of a channel.
/// Used to generate [`local_delayedpubkey`](https://github.com/lightning/bolts/blob/master/03-transactions.md#key-derivation) for the latest state of a channel.
#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)]
pub struct DelayedPaymentBasepoint(PublicKey);

impl DelayedPaymentBasepoint {
/// Get inner Public Key
pub fn to_public_key(&self) -> PublicKey {
self.0
}
}

impl From<PublicKey> for DelayedPaymentBasepoint {
fn from(value: PublicKey) -> Self {
Self(value)
}
}

basepoint_impl!(DelayedPaymentBasepoint);
key_read_write!(DelayedPaymentBasepoint);

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

/// [payment_basepoint](https://github.com/lightning/bolts/blob/master/03-transactions.md#key-derivation)
/// Used to generate localpubkey for the latest state of a channel.
/// Used to generate [localpubkey](https://github.com/lightning/bolts/blob/master/03-transactions.md#key-derivation) for the latest state of a channel.
#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)]
pub struct PaymentBasepoint(PublicKey);

impl PaymentBasepoint {
/// Get inner Public Key
pub fn to_public_key(&self) -> PublicKey {
self.0
}
}

impl From<PublicKey> for PaymentBasepoint {
fn from(value: PublicKey) -> Self {
Self(value)
}
}

basepoint_impl!(PaymentBasepoint);
key_read_write!(PaymentBasepoint);


Expand All @@ -141,24 +130,10 @@ impl PaymentKey {
}
key_read_write!(PaymentKey);

/// [htlc_basepoint](https://github.com/lightning/bolts/blob/master/03-transactions.md#key-derivation)
/// Used to generate htlcpubkey for the latest state of a channel.
/// Used to generate [htlcpubkey](https://github.com/lightning/bolts/blob/master/03-transactions.md#key-derivation) for the latest state of a channel.
#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)]
pub struct HtlcBasepoint(PublicKey);

impl HtlcBasepoint {
/// Get inner Public Key
pub fn to_public_key(&self) -> PublicKey {
self.0
}
}

impl From<PublicKey> for HtlcBasepoint {
fn from(value: PublicKey) -> Self {
Self(value)
}
}

basepoint_impl!(HtlcBasepoint);
key_read_write!(HtlcBasepoint);


Expand Down Expand Up @@ -187,24 +162,10 @@ fn derive_public_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, per_commitm
.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.")
}

/// [revocation_basepoint](https://github.com/lightning/bolts/blob/master/03-transactions.md#key-derivation)
/// Used to generate htlcpubkey for the latest state of a channel.
/// Used to generate [htlcpubkey](https://github.com/lightning/bolts/blob/master/03-transactions.md#key-derivation) for the latest state of a channel.
#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)]
pub struct RevocationBasepoint(PublicKey);

impl RevocationBasepoint {
/// Get inner Public Key
pub fn to_public_key(&self) -> PublicKey {
self.0
}
}

impl From<PublicKey> for RevocationBasepoint {
fn from(value: PublicKey) -> Self {
Self(value)
}
}

basepoint_impl!(RevocationBasepoint);
key_read_write!(RevocationBasepoint);


Expand Down
6 changes: 3 additions & 3 deletions lightning/src/sign/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1306,7 +1306,7 @@ impl EcdsaChannelSigner for InMemorySigner {
&secp_ctx, &HtlcBasepoint::from(counterparty_keys.htlc_basepoint), &per_commitment_point,
);
let holder_htlcpubkey = HtlcKey::from_basepoint(
&secp_ctx, &HtlcBasepoint::from(self.pubkeys().htlc_basepoint.to_owned()), &per_commitment_point,
&secp_ctx, &HtlcBasepoint::from(self.pubkeys().htlc_basepoint), &per_commitment_point,
);
let chan_type = self.channel_type_features().expect(MISSING_PARAMS_ERR);
chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, chan_type, &counterparty_htlcpubkey, &holder_htlcpubkey, &revocation_pubkey)
Expand Down Expand Up @@ -1337,10 +1337,10 @@ impl EcdsaChannelSigner for InMemorySigner {
);
let counterparty_keys = self.counterparty_pubkeys().expect(MISSING_PARAMS_ERR);
let counterparty_htlcpubkey = HtlcKey::from_basepoint(
&secp_ctx, &HtlcBasepoint::from(counterparty_keys.htlc_basepoint.to_owned()), &per_commitment_point,
&secp_ctx, &HtlcBasepoint::from(counterparty_keys.htlc_basepoint), &per_commitment_point,
);
let htlcpubkey = HtlcKey::from_basepoint(
&secp_ctx, &HtlcBasepoint::from(self.pubkeys().htlc_basepoint.to_owned()), &per_commitment_point,
&secp_ctx, &HtlcBasepoint::from(self.pubkeys().htlc_basepoint), &per_commitment_point,
);
let chan_type = self.channel_type_features().expect(MISSING_PARAMS_ERR);
let witness_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, chan_type, &counterparty_htlcpubkey, &htlcpubkey, &revocation_pubkey);
Expand Down
2 changes: 1 addition & 1 deletion lightning/src/util/test_channel_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ impl EcdsaChannelSigner for TestChannelSigner {
input, &witness_script, htlc_descriptor.htlc.amount_msat / 1000, sighash_type
).unwrap();
let countersignatory_htlc_key = HtlcKey::from_basepoint(
&secp_ctx,&HtlcBasepoint::from(self.inner.counterparty_pubkeys().unwrap().htlc_basepoint.to_owned()), &htlc_descriptor.per_commitment_point,
&secp_ctx,&HtlcBasepoint::from(self.inner.counterparty_pubkeys().unwrap().htlc_basepoint), &htlc_descriptor.per_commitment_point,
);

secp_ctx.verify_ecdsa(
Expand Down

0 comments on commit 90e24a5

Please sign in to comment.