Skip to content

Commit fbe9b3c

Browse files
committed
Add new ChannelId struct, unused
1 parent 8f02430 commit fbe9b3c

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
@@ -601,6 +601,78 @@ impl_writeable_tlv_based!(PendingChannelMonitorUpdate, {
601601
(0, update, required),
602602
});
603603

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

0 commit comments

Comments
 (0)