ddk_manager/channel/
mod.rs

1//! # Module containing structures and methods for working with DLC channels.
2
3use bitcoin::{hashes::Hash, Transaction, Txid};
4use dlc_messages::channel::{AcceptChannel, SignChannel};
5use secp256k1_zkp::PublicKey;
6
7use crate::{ChannelId, ContractId};
8
9use self::{
10    accepted_channel::AcceptedChannel, offered_channel::OfferedChannel,
11    signed_channel::SignedChannel,
12};
13
14pub mod accepted_channel;
15pub mod offered_channel;
16pub mod party_points;
17pub mod ser;
18pub mod signed_channel;
19mod utils;
20
21/// Enumeration containing the possible state a DLC channel can be in.
22#[derive(Clone)]
23#[allow(clippy::large_enum_variant)]
24pub enum Channel {
25    /// A channel that has been offered.
26    Offered(OfferedChannel),
27    /// A channel that has been accepted.
28    Accepted(AcceptedChannel),
29    /// A channel whose fund outputs have been signed by the offer party.
30    Signed(SignedChannel),
31    /// A channel that failed when validating an
32    /// [`dlc_messages::channel::AcceptChannel`] message.
33    FailedAccept(FailedAccept),
34    /// A channel that failed when validating an
35    /// [`dlc_messages::channel::SignChannel`] message.
36    FailedSign(FailedSign),
37    /// A [`OfferedChannel`] that got rejected by the counterparty.
38    Cancelled(OfferedChannel),
39    /// A [`Channel`] is in `Closing` state when the local party
40    /// has broadcast a buffer transaction and is waiting to finalize the
41    /// closing of the channel by broadcasting a CET.
42    Closing(ClosingChannel),
43    /// A [`Channel`] is in `Closed` state when it was force closed by
44    /// the local party.
45    Closed(ClosedChannel),
46    /// A [`Channel`] is in `CounterClosed` state when it was force
47    /// closed by the counter party.
48    CounterClosed(ClosedChannel),
49    /// A [`Channel`] is in `ClosedPunished` state when the local
50    /// party broadcast a punishment transaction in response to the counter
51    /// party broadcasting a settle or buffer transaction for a revoked channel
52    /// state.
53    ClosedPunished(ClosedPunishedChannel),
54    /// A [`SignedChannel`] is in `CollaborativelyClosed` state when it was
55    /// collaboratively closed.
56    CollaborativelyClosed(ClosedChannel),
57}
58
59impl std::fmt::Debug for Channel {
60    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61        let state = match self {
62            Channel::Offered(_) => "offered",
63            Channel::Accepted(_) => "accepted",
64            Channel::Signed(_) => "signed",
65            Channel::FailedAccept(_) => "failed accept",
66            Channel::FailedSign(_) => "failed sign",
67            Channel::Cancelled(_) => "cancelled",
68            Channel::Closing(_) => "closing",
69            Channel::Closed(_) => "closed",
70            Channel::CounterClosed(_) => "counter closed",
71            Channel::ClosedPunished(_) => "closed punished",
72            Channel::CollaborativelyClosed(_) => "collaboratively closed",
73        };
74        f.debug_struct("Contract").field("state", &state).finish()
75    }
76}
77
78impl Channel {
79    /// Returns the public key of the counter party's node.
80    pub fn get_counter_party_id(&self) -> PublicKey {
81        match self {
82            Channel::Offered(o) => o.counter_party,
83            Channel::Accepted(a) => a.counter_party,
84            Channel::Signed(s) => s.counter_party,
85            Channel::FailedAccept(f) => f.counter_party,
86            Channel::FailedSign(f) => f.counter_party,
87            Channel::Cancelled(o) => o.counter_party,
88            Channel::Closing(c) => c.counter_party,
89            Channel::Closed(c) | Channel::CounterClosed(c) | Channel::CollaborativelyClosed(c) => {
90                c.counter_party
91            }
92            Channel::ClosedPunished(c) => c.counter_party,
93        }
94    }
95}
96
97/// A channel that failed when validating an
98/// [`dlc_messages::channel::AcceptChannel`] message.
99#[derive(Clone)]
100pub struct FailedAccept {
101    /// The [`secp256k1_zkp::PublicKey`] of the counter party.
102    pub counter_party: PublicKey,
103    /// The temporary [`crate::ChannelId`] of the channel.
104    pub temporary_channel_id: ChannelId,
105    /// An message describing the error encountered while validating the
106    /// [`dlc_messages::channel::AcceptChannel`] message.
107    pub error_message: String,
108    /// The [`dlc_messages::channel::AcceptChannel`] that was received.
109    pub accept_message: AcceptChannel,
110}
111
112/// A channel that failed when validating an
113/// [`dlc_messages::channel::SignChannel`] message.
114#[derive(Clone)]
115pub struct FailedSign {
116    /// The [`secp256k1_zkp::PublicKey`] of the counter party.
117    pub counter_party: PublicKey,
118    /// The [`crate::ChannelId`] of the channel.
119    pub channel_id: ChannelId,
120    /// An message describing the error encountered while validating the
121    /// [`dlc_messages::channel::SignChannel`] message.
122    pub error_message: String,
123    /// The [`dlc_messages::channel::SignChannel`] that was received.
124    pub sign_message: SignChannel,
125}
126
127#[derive(Clone)]
128/// A channel is closing when its buffer transaction was broadcast or detected on chain.
129pub struct ClosingChannel {
130    /// The [`secp256k1_zkp::PublicKey`] of the counter party.
131    pub counter_party: PublicKey,
132    /// The temporary [`crate::ChannelId`] of the channel.
133    pub temporary_channel_id: ChannelId,
134    /// The [`crate::ChannelId`] for the channel.
135    pub channel_id: ChannelId,
136    /// The previous state the channel was before being closed, if that state was the `Signed` one,
137    /// otherwise is `None`.
138    pub rollback_state: Option<SignedChannel>,
139    /// The buffer transaction that was broadcast.
140    pub buffer_transaction: Transaction,
141    /// The [`crate::ContractId`] of the contract that was used to close
142    /// the channel.
143    pub contract_id: ContractId,
144    /// Whether the local party initiated the closing of the channel.
145    pub is_closer: bool,
146}
147
148#[derive(Clone)]
149/// A channel is closed when its buffer transaction has been spent.
150pub struct ClosedChannel {
151    /// The [`secp256k1_zkp::PublicKey`] of the counter party.
152    pub counter_party: PublicKey,
153    /// The temporary [`crate::ChannelId`] of the channel.
154    pub temporary_channel_id: ChannelId,
155    /// The [`crate::ChannelId`] for the channel.
156    pub channel_id: ChannelId,
157}
158
159#[derive(Clone)]
160/// A channel is closed punished when the counter party broadcast a revoked transaction triggering
161/// the broadcast of a punishment transaction by the local party.
162pub struct ClosedPunishedChannel {
163    /// The [`secp256k1_zkp::PublicKey`] of the counter party.
164    pub counter_party: PublicKey,
165    /// The temporary [`crate::ChannelId`] of the channel.
166    pub temporary_channel_id: ChannelId,
167    /// The [`crate::ChannelId`] for the channel.
168    pub channel_id: ChannelId,
169    /// The transaction id of the punishment transaction that was broadcast.
170    pub punish_txid: Txid,
171}
172
173impl Channel {
174    /// Returns the temporary [`crate::ChannelId`] for the channel.
175    pub fn get_temporary_id(&self) -> ChannelId {
176        match self {
177            Channel::Offered(o) => o.temporary_channel_id,
178            Channel::Accepted(a) => a.temporary_channel_id,
179            Channel::Signed(s) => s.temporary_channel_id,
180            Channel::FailedAccept(f) => f.temporary_channel_id,
181            Channel::Closed(c) | Channel::CounterClosed(c) | Channel::CollaborativelyClosed(c) => {
182                c.temporary_channel_id
183            }
184            Channel::ClosedPunished(c) => c.temporary_channel_id,
185            _ => unimplemented!(),
186        }
187    }
188
189    /// Returns the [`crate::ChannelId`] for the channel.
190    pub fn get_id(&self) -> ChannelId {
191        match self {
192            Channel::Offered(o) => o.temporary_channel_id,
193            Channel::Accepted(a) => a.channel_id,
194            Channel::Signed(s) => s.channel_id,
195            Channel::FailedAccept(f) => f.temporary_channel_id,
196            Channel::FailedSign(f) => f.channel_id,
197            Channel::Closing(c) => c.channel_id,
198            Channel::Closed(c) | Channel::CounterClosed(c) | Channel::CollaborativelyClosed(c) => {
199                c.channel_id
200            }
201            Channel::ClosedPunished(c) => c.channel_id,
202            Channel::Cancelled(c) => c.temporary_channel_id,
203        }
204    }
205
206    /// Returns the contract id associated with the channel if in a state where a contract is set.
207    pub fn get_contract_id(&self) -> Option<ContractId> {
208        match self {
209            Channel::Offered(o) => Some(o.offered_contract_id),
210            Channel::Accepted(a) => Some(a.accepted_contract_id),
211            Channel::Signed(s) => s.get_contract_id(),
212            Channel::FailedAccept(_) => None,
213            Channel::FailedSign(_) => None,
214            _ => None,
215        }
216    }
217}
218
219/// Generate a temporary contract id for a DLC based on the channel id and the update index of the DLC channel.
220pub fn generate_temporary_contract_id(
221    channel_id: ChannelId,
222    channel_update_idx: u64,
223) -> ContractId {
224    let mut data = Vec::with_capacity(65);
225    data.extend_from_slice(&channel_id);
226    data.extend_from_slice(&channel_update_idx.to_be_bytes());
227    bitcoin::hashes::sha256::Hash::hash(&data).to_byte_array()
228}