Skip to content

Commit ac1f119

Browse files
committed
Add V2 constructors to ChannelId
1 parent 7fc8a56 commit ac1f119

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
@@ -16,10 +16,15 @@ use crate::ln::msgs::DecodeError;
1616
use crate::sign::EntropySource;
1717
use crate::util::ser::{Readable, Writeable, Writer};
1818

19+
use bitcoin::hashes::Hash as _;
20+
use bitcoin::hashes::sha256::Hash as Sha256;
21+
1922
use crate::io;
2023
use core::fmt;
2124
use core::ops::Deref;
2225

26+
use super::channel_keys::RevocationBasepoint;
27+
2328
/// A unique 32-byte identifier for a channel.
2429
/// Depending on how the ID is generated, several varieties are distinguished
2530
/// (but all are stored as 32 bytes):
@@ -69,6 +74,28 @@ impl ChannelId {
6974
pub fn is_zero(&self) -> bool {
7075
self.0[..] == [0; 32]
7176
}
77+
78+
/// Create _v2_ channel ID by concatenating the holder revocation basepoint with the counterparty
79+
/// revocation basepoint and hashing the result. The basepoints will be concatenated in increasing
80+
/// sorted order.
81+
pub fn v2_from_revocation_basepoints(
82+
ours: &RevocationBasepoint,
83+
theirs: &RevocationBasepoint,
84+
) -> Self {
85+
let (lesser_point, greater_point) = if *ours < *theirs {
86+
(ours, theirs)
87+
} else {
88+
(theirs, ours)
89+
};
90+
91+
Self(Sha256::hash(&[lesser_point.0.serialize(), greater_point.0.serialize()].concat()).to_byte_array())
92+
}
93+
94+
/// Create temporary _v2_ channel ID by concatenating a zeroed out basepoint with the holder
95+
/// revocation basepoint and hashing the result.
96+
pub fn temporary_v2_from_revocation_basepoint(our_revocation_basepoint: &RevocationBasepoint) -> Self {
97+
Self(Sha256::hash(&[[0u8; 33], our_revocation_basepoint.0.serialize()].concat()).to_byte_array())
98+
}
7299
}
73100

74101
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)