use super::channel_keys::RevocationBasepoint;
use crate::chain::transaction::OutPoint;
use crate::io;
use crate::ln::msgs::DecodeError;
use crate::sign::EntropySource;
use crate::util::ser::{Readable, Writeable, Writer};
#[allow(unused_imports)]
use crate::prelude::*;
use bitcoin::hashes::{sha256::Hash as Sha256, Hash as _, HashEngine as _};
use bitcoin::hex::display::impl_fmt_traits;
use core::borrow::Borrow;
use core::ops::Deref;
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct ChannelId(pub [u8; 32]);
impl ChannelId {
pub fn v1_from_funding_txid(txid: &[u8; 32], output_index: u16) -> Self {
let mut res = [0; 32];
res[..].copy_from_slice(&txid[..]);
res[30] ^= ((output_index >> 8) & 0xff) as u8;
res[31] ^= ((output_index >> 0) & 0xff) as u8;
Self(res)
}
pub fn v1_from_funding_outpoint(outpoint: OutPoint) -> Self {
Self::v1_from_funding_txid(outpoint.txid.as_byte_array(), outpoint.index)
}
pub fn temporary_from_entropy_source<ES: Deref>(entropy_source: &ES) -> Self
where
ES::Target: EntropySource,
{
Self(entropy_source.get_secure_random_bytes())
}
pub fn from_bytes(data: [u8; 32]) -> Self {
Self(data)
}
pub fn new_zero() -> Self {
Self([0; 32])
}
pub fn is_zero(&self) -> bool {
self.0[..] == [0; 32]
}
pub fn v2_from_revocation_basepoints(
ours: &RevocationBasepoint, theirs: &RevocationBasepoint,
) -> Self {
let ours = ours.0.serialize();
let theirs = theirs.0.serialize();
let (lesser, greater) = if ours < theirs { (ours, theirs) } else { (theirs, ours) };
let mut engine = Sha256::engine();
engine.input(&lesser[..]);
engine.input(&greater[..]);
Self(Sha256::from_engine(engine).to_byte_array())
}
pub fn temporary_v2_from_revocation_basepoint(
our_revocation_basepoint: &RevocationBasepoint,
) -> Self {
let our_revocation_point_bytes = our_revocation_basepoint.0.serialize();
Self(Sha256::hash(&[[0u8; 33], our_revocation_point_bytes].concat()).to_byte_array())
}
pub fn is_v2_channel_id(
&self, ours: &RevocationBasepoint, theirs: &RevocationBasepoint,
) -> bool {
*self == Self::v2_from_revocation_basepoints(ours, theirs)
}
}
impl Writeable for ChannelId {
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
self.0.write(w)
}
}
impl Readable for ChannelId {
fn read<R: io::Read>(r: &mut R) -> Result<Self, DecodeError> {
let buf: [u8; 32] = Readable::read(r)?;
Ok(ChannelId(buf))
}
}
impl Borrow<[u8]> for ChannelId {
fn borrow(&self) -> &[u8] {
&self.0[..]
}
}
impl_fmt_traits! {
impl fmt_traits for ChannelId {
const LENGTH: usize = 32;
}
}
#[cfg(test)]
mod tests {
use bitcoin::hashes::{sha256::Hash as Sha256, Hash as _, HashEngine as _};
use bitcoin::hex::DisplayHex;
use bitcoin::secp256k1::PublicKey;
use super::ChannelId;
use crate::io;
use crate::ln::channel_keys::RevocationBasepoint;
use crate::prelude::*;
use crate::util::ser::{Readable, Writeable};
use crate::util::test_utils;
use core::str::FromStr;
#[test]
fn test_channel_id_v1_from_funding_txid() {
let channel_id = ChannelId::v1_from_funding_txid(&[2; 32], 1);
let expected = "0202020202020202020202020202020202020202020202020202020202020203";
assert_eq!(channel_id.0.as_hex().to_string(), expected);
}
#[test]
fn test_channel_id_new_from_data() {
let data: [u8; 32] = [2; 32];
let channel_id = ChannelId::from_bytes(data.clone());
assert_eq!(channel_id.0, data);
}
#[test]
fn test_channel_id_equals() {
let channel_id11 = ChannelId::v1_from_funding_txid(&[2; 32], 2);
let channel_id12 = ChannelId::v1_from_funding_txid(&[2; 32], 2);
let channel_id21 = ChannelId::v1_from_funding_txid(&[2; 32], 42);
assert_eq!(channel_id11, channel_id12);
assert_ne!(channel_id11, channel_id21);
}
#[test]
fn test_channel_id_write_read() {
let data: [u8; 32] = [2; 32];
let channel_id = ChannelId::from_bytes(data.clone());
let mut w = test_utils::TestVecWriter(Vec::new());
channel_id.write(&mut w).unwrap();
let channel_id_2 = ChannelId::read(&mut io::Cursor::new(&w.0)).unwrap();
assert_eq!(channel_id_2, channel_id);
assert_eq!(channel_id_2.0, data);
}
#[test]
fn test_channel_id_display() {
let channel_id = ChannelId::v1_from_funding_txid(&[2; 32], 1);
let expected = "0202020202020202020202020202020202020202020202020202020202020203";
assert_eq!(format!("{}", &channel_id), expected);
}
#[test]
fn test_channel_id_v2_from_basepoints() {
let our_pk = "0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c";
let ours = RevocationBasepoint(PublicKey::from_str(&our_pk).unwrap());
let their_pk = "02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619";
let theirs = RevocationBasepoint(PublicKey::from_str(&their_pk).unwrap());
let mut engine = Sha256::engine();
engine.input(&theirs.0.serialize());
engine.input(&ours.0.serialize());
let expected_id = ChannelId(Sha256::from_engine(engine).to_byte_array());
assert_eq!(ChannelId::v2_from_revocation_basepoints(&ours, &theirs), expected_id);
let our_pk = "027f31ebc5462c1fdce1b737ecff52d37d75dea43ce11c74d25aa297165faa2007";
let ours = RevocationBasepoint(PublicKey::from_str(&our_pk).unwrap());
let their_pk = "02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619";
let theirs = RevocationBasepoint(PublicKey::from_str(&their_pk).unwrap());
let mut engine = Sha256::engine();
engine.input(&ours.0.serialize());
engine.input(&theirs.0.serialize());
let expected_id = ChannelId(Sha256::from_engine(engine).to_byte_array());
assert_eq!(ChannelId::v2_from_revocation_basepoints(&ours, &theirs), expected_id);
}
#[test]
fn test_is_v2_channel_id() {
let our_pk = "0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c";
let ours = RevocationBasepoint(PublicKey::from_str(&our_pk).unwrap());
let their_pk = "02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619";
let theirs = RevocationBasepoint(PublicKey::from_str(&their_pk).unwrap());
let channel_id = ChannelId::v2_from_revocation_basepoints(&ours, &theirs);
assert!(channel_id.is_v2_channel_id(&ours, &theirs));
let channel_id = ChannelId::v1_from_funding_txid(&[2; 32], 1);
assert!(!channel_id.is_v2_channel_id(&ours, &theirs))
}
}