Skip to content

Commit 3f41e98

Browse files
committed
Add new ChannelId struct, unused
1 parent 0c25046 commit 3f41e98

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

lightning/src/ln/channel.rs

+72
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,78 @@ impl_writeable_tlv_based!(PendingChannelMonitorUpdate, {
602602
(0, update, required),
603603
});
604604

605+
/// A unique 32-byte identifier for a channel.
606+
/// Depending on how the ID is generated, several varieties are distinguished (but all are stored as 32 bytes):
607+
/// - v1: generated based on funding tx outpoint (txid&index)
608+
/// - temporary: generated randomly
609+
/// (later planned v2: based on revocation point)
610+
/// The variety (context) is not stored, it is relevant only at creation.
611+
/// This is not exported to bindings users as we just use [u8; 32] directly
612+
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
613+
pub struct ChannelId {
614+
// The 32-byte data of the ID
615+
data: [u8; 32],
616+
}
617+
618+
impl ChannelId {
619+
/// Create v1 channel ID based on a funding TX ID and output index
620+
pub fn v1_from_funding_txid(txid: &[u8; 32], output_index: u16) -> Self {
621+
let mut res = [0; 32];
622+
res[..].copy_from_slice(&txid[..]);
623+
res[30] ^= ((output_index >> 8) & 0xff) as u8;
624+
res[31] ^= ((output_index >> 0) & 0xff) as u8;
625+
Self::from_bytes(res)
626+
}
627+
628+
/// Create a temporary channel ID randomly, based on an entropy source.
629+
pub fn temporary_from_entropy_source<ES: Deref>(entropy_source: &ES) -> Self
630+
where ES::Target: EntropySource {
631+
Self::from_bytes(entropy_source.get_secure_random_bytes())
632+
}
633+
634+
/// Generic constructor; create a new channel ID from the provided data.
635+
/// Use a more specific *from_* constructor when possible.
636+
/// This constructor is useful for tests, and internally, e.g. when the channel ID is being deserialized.
637+
pub fn from_bytes(data: [u8; 32]) -> Self {
638+
Self{data}
639+
}
640+
641+
/// Create a channel ID consisting of all-zeros data (placeholder).
642+
pub fn new_zero() -> Self {
643+
Self::from_bytes([0; 32])
644+
}
645+
646+
/// Accessor for the channel ID data
647+
pub fn bytes(&self) -> &[u8; 32] {
648+
&self.data
649+
}
650+
}
651+
652+
impl Writeable for ChannelId {
653+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
654+
self.data.write(w)
655+
}
656+
}
657+
658+
impl Readable for ChannelId {
659+
fn read<R: io::Read>(r: &mut R) -> Result<Self, DecodeError> {
660+
let buf: [u8; 32] = Readable::read(r)?;
661+
Ok(ChannelId::from_bytes(buf))
662+
}
663+
}
664+
665+
impl ToHex for ChannelId {
666+
fn to_hex(&self) -> String {
667+
self.data.to_hex()
668+
}
669+
}
670+
671+
impl fmt::Display for ChannelId {
672+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
673+
crate::util::logger::DebugBytes(&self.data).fmt(f)
674+
}
675+
}
676+
605677
/// Contains all state common to unfunded inbound/outbound channels.
606678
pub(super) struct UnfundedChannelContext {
607679
/// A counter tracking how many ticks have elapsed since this unfunded channel was

0 commit comments

Comments
 (0)