ddk_manager/channel/
mod.rs1use 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#[derive(Clone)]
23#[allow(clippy::large_enum_variant)]
24pub enum Channel {
25 Offered(OfferedChannel),
27 Accepted(AcceptedChannel),
29 Signed(SignedChannel),
31 FailedAccept(FailedAccept),
34 FailedSign(FailedSign),
37 Cancelled(OfferedChannel),
39 Closing(ClosingChannel),
43 Closed(ClosedChannel),
46 CounterClosed(ClosedChannel),
49 ClosedPunished(ClosedPunishedChannel),
54 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 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#[derive(Clone)]
100pub struct FailedAccept {
101 pub counter_party: PublicKey,
103 pub temporary_channel_id: ChannelId,
105 pub error_message: String,
108 pub accept_message: AcceptChannel,
110}
111
112#[derive(Clone)]
115pub struct FailedSign {
116 pub counter_party: PublicKey,
118 pub channel_id: ChannelId,
120 pub error_message: String,
123 pub sign_message: SignChannel,
125}
126
127#[derive(Clone)]
128pub struct ClosingChannel {
130 pub counter_party: PublicKey,
132 pub temporary_channel_id: ChannelId,
134 pub channel_id: ChannelId,
136 pub rollback_state: Option<SignedChannel>,
139 pub buffer_transaction: Transaction,
141 pub contract_id: ContractId,
144 pub is_closer: bool,
146}
147
148#[derive(Clone)]
149pub struct ClosedChannel {
151 pub counter_party: PublicKey,
153 pub temporary_channel_id: ChannelId,
155 pub channel_id: ChannelId,
157}
158
159#[derive(Clone)]
160pub struct ClosedPunishedChannel {
163 pub counter_party: PublicKey,
165 pub temporary_channel_id: ChannelId,
167 pub channel_id: ChannelId,
169 pub punish_txid: Txid,
171}
172
173impl Channel {
174 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 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 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
219pub 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}