Skip to content

Commit 395a29d

Browse files
committed
Add V2 constructors to ChannelId
1 parent 73da722 commit 395a29d

File tree

1 file changed

+65
-1
lines changed

1 file changed

+65
-1
lines changed

lightning/src/ln/channel_id.rs

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@ 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

18-
use bitcoin::hashes::Hash as _;
19+
use bitcoin::hashes::{
20+
Hash as _,
21+
HashEngine as _,
22+
sha256::Hash as Sha256,
23+
};
1924
use core::fmt;
2025
use core::ops::Deref;
2126

@@ -68,6 +73,32 @@ impl ChannelId {
6873
pub fn is_zero(&self) -> bool {
6974
self.0[..] == [0; 32]
7075
}
76+
77+
/// Create _v2_ channel ID by concatenating the holder revocation basepoint with the counterparty
78+
/// revocation basepoint and hashing the result. The basepoints will be concatenated in increasing
79+
/// sorted order.
80+
pub fn v2_from_revocation_basepoints(
81+
ours: &RevocationBasepoint,
82+
theirs: &RevocationBasepoint,
83+
) -> Self {
84+
let ours = ours.0.serialize();
85+
let theirs = theirs.0.serialize();
86+
let (lesser, greater) = if ours < theirs {
87+
(ours, theirs)
88+
} else {
89+
(theirs, ours)
90+
};
91+
let mut engine = Sha256::engine();
92+
engine.input(&lesser[..]);
93+
engine.input(&greater[..]);
94+
Self(Sha256::from_engine(engine).to_byte_array())
95+
}
96+
97+
/// Create temporary _v2_ channel ID by concatenating a zeroed out basepoint with the holder
98+
/// revocation basepoint and hashing the result.
99+
pub fn temporary_v2_from_revocation_basepoint(our_revocation_basepoint: &RevocationBasepoint) -> Self {
100+
Self(Sha256::hash(&[[0u8; 33], our_revocation_basepoint.0.serialize()].concat()).to_byte_array())
101+
}
71102
}
72103

73104
impl Writeable for ChannelId {
@@ -91,9 +122,17 @@ impl fmt::Display for ChannelId {
91122

92123
#[cfg(test)]
93124
mod tests {
125+
use bitcoin::hashes::{
126+
Hash as _,
127+
HashEngine as _,
128+
hex::FromHex as _,
129+
sha256::Hash as Sha256,
130+
};
131+
use bitcoin::secp256k1::PublicKey;
94132
use hex::DisplayHex;
95133

96134
use crate::ln::ChannelId;
135+
use crate::ln::channel_keys::RevocationBasepoint;
97136
use crate::util::ser::{Readable, Writeable};
98137
use crate::util::test_utils;
99138
use crate::prelude::*;
@@ -139,4 +178,29 @@ mod tests {
139178
let channel_id = ChannelId::v1_from_funding_txid(&[2; 32], 1);
140179
assert_eq!(format!("{}", &channel_id), "0202020202020202020202020202020202020202020202020202020202020203");
141180
}
181+
182+
#[test]
183+
fn test_channel_id_v2_from_basepoints() {
184+
// Ours greater than theirs
185+
let ours = RevocationBasepoint(PublicKey::from_slice(&<Vec<u8>>::from_hex("0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c").unwrap()[..]).unwrap());
186+
let theirs = RevocationBasepoint(PublicKey::from_slice(&<Vec<u8>>::from_hex("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap());
187+
188+
let mut engine = Sha256::engine();
189+
engine.input(&theirs.0.serialize());
190+
engine.input(&ours.0.serialize());
191+
let expected_id = ChannelId(Sha256::from_engine(engine).to_byte_array());
192+
193+
assert_eq!(ChannelId::v2_from_revocation_basepoints(&ours, &theirs), expected_id);
194+
195+
// Theirs greater than ours
196+
let ours = RevocationBasepoint(PublicKey::from_slice(&<Vec<u8>>::from_hex("027f31ebc5462c1fdce1b737ecff52d37d75dea43ce11c74d25aa297165faa2007").unwrap()[..]).unwrap());
197+
let theirs = RevocationBasepoint(PublicKey::from_slice(&<Vec<u8>>::from_hex("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap());
198+
199+
let mut engine = Sha256::engine();
200+
engine.input(&ours.0.serialize());
201+
engine.input(&theirs.0.serialize());
202+
let expected_id = ChannelId(Sha256::from_engine(engine).to_byte_array());
203+
204+
assert_eq!(ChannelId::v2_from_revocation_basepoints(&ours, &theirs), expected_id);
205+
}
142206
}

0 commit comments

Comments
 (0)