Skip to content

Commit c611d6f

Browse files
committed
Add V2 constructors to ChannelId
1 parent 6b43153 commit c611d6f

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

lightning/src/ln/channel_id.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,15 @@ use crate::ln::msgs::DecodeError;
1313
use crate::sign::EntropySource;
1414
use crate::util::ser::{Readable, Writeable, Writer};
1515

16+
use bitcoin::hashes::Hash;
17+
use bitcoin::hashes::sha256::Hash as Sha256;
18+
1619
use crate::io;
1720
use core::fmt;
1821
use core::ops::Deref;
1922

23+
use super::channel_keys::RevocationBasepoint;
24+
2025
/// A unique 32-byte identifier for a channel.
2126
/// Depending on how the ID is generated, several varieties are distinguished
2227
/// (but all are stored as 32 bytes):
@@ -61,6 +66,28 @@ impl ChannelId {
6166
pub fn is_zero(&self) -> bool {
6267
self.0[..] == [0; 32]
6368
}
69+
70+
/// Create _v2_ channel ID by concatenating the holder revocation basepoint with the counterparty
71+
/// revocation basepoint and hashing the result. The basepoints will be concatenated in increasing
72+
/// sorted order.
73+
pub fn v2_from_revocation_basepoints(
74+
ours: &RevocationBasepoint,
75+
theirs: &RevocationBasepoint,
76+
) -> Self {
77+
let (lesser_point, greater_point) = if *ours < *theirs {
78+
(ours, theirs)
79+
} else {
80+
(theirs, ours)
81+
};
82+
83+
Self(Sha256::hash(&[lesser_point.0.serialize(), greater_point.0.serialize()].concat()).to_byte_array())
84+
}
85+
86+
/// Create temporary _v2_ channel ID by concatenating a zeroed out basepoint with the holder
87+
/// revocation basepoint and hashing the result.
88+
pub fn temporary_v2_from_revocation_basepoint(our_revocation_basepoint: &RevocationBasepoint) -> Self {
89+
Self(Sha256::hash(&[[0u8; 33], our_revocation_basepoint.0.serialize()].concat()).to_byte_array())
90+
}
6491
}
6592

6693
impl Writeable for ChannelId {

lightning/src/ln/channel_keys.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ fn derive_public_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, per_commitm
166166

167167
/// Master key used in conjunction with per_commitment_point to generate [htlcpubkey](https://github.com/lightning/bolts/blob/master/03-transactions.md#key-derivation) for the latest state of a channel.
168168
/// A watcher can be given a [RevocationBasepoint] to generate per commitment [RevocationKey] to create justice transactions.
169-
#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)]
169+
#[derive(PartialEq, PartialOrd, Eq, Clone, Copy, Debug, Hash)]
170170
pub struct RevocationBasepoint(pub PublicKey);
171171
basepoint_impl!(RevocationBasepoint);
172172
key_read_write!(RevocationBasepoint);

0 commit comments

Comments
 (0)