Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lightning-types/src/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
//! - `DnsResolver` - supports resolving DNS names to TXT DNSSEC proofs for BIP 353 payments
//! (see [bLIP 32](https://github.com/lightning/blips/blob/master/blip-0032.md) for more information).
//! - `ProvideStorage` - Indicates that we offer the capability to store data of our peers
//! (see https://github.com/lightning/bolts/pull/1110 for more info).
//! (see [BOLT PR #1110](https://github.com/lightning/bolts/pull/1110) for more info).
//! - `Quiescence` - protocol to quiesce a channel by indicating that "SomeThing Fundamental is Underway"
//! (see [BOLT-2](https://github.com/lightning/bolts/blob/master/02-peer-protocol.md#channel-quiescence) for more information).
//!
Expand Down
64 changes: 33 additions & 31 deletions lightning/src/ln/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,20 @@

//! Various wrapper types (most around 32-byte arrays) for use in lightning.

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};
use super::channel_keys::RevocationBasepoint;

#[allow(unused_imports)]
use crate::prelude::*;

use bitcoin::hashes::{
Hash as _,
HashEngine as _,
sha256::Hash as Sha256,
};
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;

Expand Down Expand Up @@ -56,7 +54,9 @@ impl ChannelId {

/// Create a _temporary_ channel ID randomly, based on an entropy source.
pub fn temporary_from_entropy_source<ES: Deref>(entropy_source: &ES) -> Self
where ES::Target: EntropySource {
where
ES::Target: EntropySource,
{
Self(entropy_source.get_secure_random_bytes())
}

Expand All @@ -80,16 +80,11 @@ impl ChannelId {
/// revocation basepoint and hashing the result. The basepoints will be concatenated in increasing
/// sorted order.
pub fn v2_from_revocation_basepoints(
ours: &RevocationBasepoint,
theirs: &RevocationBasepoint,
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 (lesser, greater) = if ours < theirs { (ours, theirs) } else { (theirs, ours) };
let mut engine = Sha256::engine();
engine.input(&lesser[..]);
engine.input(&greater[..]);
Expand All @@ -98,8 +93,11 @@ impl ChannelId {

/// Create temporary _v2_ channel ID by concatenating a zeroed out basepoint with the holder
/// revocation basepoint and hashing the result.
pub fn temporary_v2_from_revocation_basepoint(our_revocation_basepoint: &RevocationBasepoint) -> Self {
Self(Sha256::hash(&[[0u8; 33], our_revocation_basepoint.0.serialize()].concat()).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())
}
}

Expand Down Expand Up @@ -130,26 +128,25 @@ impl_fmt_traits! {

#[cfg(test)]
mod tests {
use bitcoin::hashes::{
Hash as _,
HashEngine as _,
hex::FromHex as _,
sha256::Hash as Sha256,
};
use bitcoin::secp256k1::PublicKey;
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 crate::prelude::*;
use crate::io;

use core::str::FromStr;

#[test]
fn test_channel_id_v1_from_funding_txid() {
let channel_id = ChannelId::v1_from_funding_txid(&[2; 32], 1);
assert_eq!(channel_id.0.as_hex().to_string(), "0202020202020202020202020202020202020202020202020202020202020203");
let expected = "0202020202020202020202020202020202020202020202020202020202020203";
assert_eq!(channel_id.0.as_hex().to_string(), expected);
}

#[test]
Expand Down Expand Up @@ -184,14 +181,17 @@ mod tests {
#[test]
fn test_channel_id_display() {
let channel_id = ChannelId::v1_from_funding_txid(&[2; 32], 1);
assert_eq!(format!("{}", &channel_id), "0202020202020202020202020202020202020202020202020202020202020203");
let expected = "0202020202020202020202020202020202020202020202020202020202020203";
assert_eq!(format!("{}", &channel_id), expected);
}

#[test]
fn test_channel_id_v2_from_basepoints() {
// Ours greater than theirs
let ours = RevocationBasepoint(PublicKey::from_slice(&<Vec<u8>>::from_hex("0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c").unwrap()[..]).unwrap());
let theirs = RevocationBasepoint(PublicKey::from_slice(&<Vec<u8>>::from_hex("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap());
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());
Expand All @@ -201,8 +201,10 @@ mod tests {
assert_eq!(ChannelId::v2_from_revocation_basepoints(&ours, &theirs), expected_id);

// Theirs greater than ours
let ours = RevocationBasepoint(PublicKey::from_slice(&<Vec<u8>>::from_hex("027f31ebc5462c1fdce1b737ecff52d37d75dea43ce11c74d25aa297165faa2007").unwrap()[..]).unwrap());
let theirs = RevocationBasepoint(PublicKey::from_slice(&<Vec<u8>>::from_hex("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap());
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());
Expand Down
Loading
Loading