Skip to content

Commit 6795a54

Browse files
committed
Add V2 constructors to ChannelId
1 parent 5bf58f0 commit 6795a54

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lightning/src/ln/channel_id.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ use crate::io;
1414
use crate::ln::msgs::DecodeError;
1515
use crate::sign::EntropySource;
1616
use crate::util::ser::{Readable, Writeable, Writer};
17+
use super::channel_keys::RevocationBasepoint;
1718

1819
use bitcoin::hashes::Hash as _;
20+
use bitcoin::hashes::sha256::Hash as Sha256;
1921
use core::fmt;
2022
use core::ops::Deref;
2123

@@ -68,6 +70,28 @@ impl ChannelId {
6870
pub fn is_zero(&self) -> bool {
6971
self.0[..] == [0; 32]
7072
}
73+
74+
/// Create _v2_ channel ID by concatenating the holder revocation basepoint with the counterparty
75+
/// revocation basepoint and hashing the result. The basepoints will be concatenated in increasing
76+
/// sorted order.
77+
pub fn v2_from_revocation_basepoints(
78+
ours: &RevocationBasepoint,
79+
theirs: &RevocationBasepoint,
80+
) -> Self {
81+
let (lesser_point, greater_point) = if *ours < *theirs {
82+
(ours, theirs)
83+
} else {
84+
(theirs, ours)
85+
};
86+
87+
Self(Sha256::hash(&[lesser_point.0.serialize(), greater_point.0.serialize()].concat()).to_byte_array())
88+
}
89+
90+
/// Create temporary _v2_ channel ID by concatenating a zeroed out basepoint with the holder
91+
/// revocation basepoint and hashing the result.
92+
pub fn temporary_v2_from_revocation_basepoint(our_revocation_basepoint: &RevocationBasepoint) -> Self {
93+
Self(Sha256::hash(&[[0u8; 33], our_revocation_basepoint.0.serialize()].concat()).to_byte_array())
94+
}
7195
}
7296

7397
impl Writeable for ChannelId {

lightning/src/ln/channel_keys.rs

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

165165
/// 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.
166166
/// A watcher can be given a [RevocationBasepoint] to generate per commitment [RevocationKey] to create justice transactions.
167-
#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)]
167+
#[derive(PartialEq, PartialOrd, Eq, Clone, Copy, Debug, Hash)]
168168
pub struct RevocationBasepoint(pub PublicKey);
169169
basepoint_impl!(RevocationBasepoint);
170170
key_read_write!(RevocationBasepoint);

0 commit comments

Comments
 (0)