1#![crate_name = "ddk_manager"]
5#![forbid(unsafe_code)]
7#![deny(non_upper_case_globals)]
8#![deny(non_camel_case_types)]
9#![deny(non_snake_case)]
10#![deny(unused_mut)]
11#![deny(dead_code)]
12#![deny(unused_imports)]
13#![deny(missing_docs)]
14
15#[macro_use]
16extern crate ddk_messages;
17
18pub mod chain_monitor;
19pub mod channel;
20pub mod channel_updater;
21pub mod contract;
22pub mod contract_updater;
23mod conversion_utils;
24mod dlc_input;
25pub mod error;
26pub mod manager;
27pub mod payout_curve;
28mod utils;
29
30use bitcoin::psbt::Psbt;
31use bitcoin::{Address, Amount, Block, OutPoint, ScriptBuf, Transaction, TxOut, Txid};
32use chain_monitor::ChainMonitor;
33use channel::offered_channel::OfferedChannel;
34use channel::signed_channel::{SignedChannel, SignedChannelStateType};
35use channel::Channel;
36use contract::PreClosedContract;
37use contract::{offered_contract::OfferedContract, signed_contract::SignedContract, Contract};
38use ddk_messages::impl_dlc_writeable;
39use ddk_messages::oracle_msgs::{OracleAnnouncement, OracleAttestation};
40use ddk_messages::ser_impls::{read_address, write_address};
41use error::Error;
42use lightning::ln::msgs::DecodeError;
43use lightning::util::ser::{Readable, Writeable, Writer};
44use secp256k1_zkp::{PublicKey, SecretKey, Signing};
45use secp256k1_zkp::{Secp256k1, XOnlyPublicKey};
46use std::collections::HashMap;
47use std::ops::Deref;
48use std::sync::RwLock;
49
50pub type ContractId = [u8; 32];
52
53pub type KeysId = [u8; 32];
55
56pub type ChannelId = [u8; 32];
58
59pub trait Time {
61 fn unix_time_now(&self) -> u64;
63}
64
65#[derive(Debug)]
67pub struct SystemTimeProvider {}
68
69impl Time for SystemTimeProvider {
70 fn unix_time_now(&self) -> u64 {
71 let now = std::time::SystemTime::now();
72 now.duration_since(std::time::UNIX_EPOCH)
73 .expect("Unexpected time error")
74 .as_secs()
75 }
76}
77
78pub trait ContractSigner: Clone {
80 fn get_public_key<C: Signing>(&self, secp: &Secp256k1<C>) -> Result<PublicKey, Error>;
82 fn get_secret_key(&self) -> Result<SecretKey, Error>;
85}
86
87#[derive(Debug, Copy, Clone)]
89pub struct SimpleSigner {
90 secret_key: SecretKey,
91}
92
93impl SimpleSigner {
94 pub fn new(secret_key: SecretKey) -> Self {
96 Self { secret_key }
97 }
98}
99
100impl ContractSigner for SimpleSigner {
101 fn get_public_key<C: Signing>(&self, secp: &Secp256k1<C>) -> Result<PublicKey, Error> {
102 Ok(self.secret_key.public_key(secp))
103 }
104
105 fn get_secret_key(&self) -> Result<SecretKey, Error> {
106 Ok(self.secret_key)
107 }
108}
109
110impl ContractSigner for SecretKey {
111 fn get_public_key<C: Signing>(&self, secp: &Secp256k1<C>) -> Result<PublicKey, Error> {
112 Ok(self.public_key(secp))
113 }
114
115 fn get_secret_key(&self) -> Result<SecretKey, Error> {
116 Ok(*self)
117 }
118}
119
120pub trait ContractSignerProvider {
122 type Signer: ContractSigner;
124
125 fn derive_signer_key_id(&self, is_offer_party: bool, temp_id: [u8; 32]) -> [u8; 32];
127
128 fn derive_contract_signer(&self, key_id: [u8; 32]) -> Result<Self::Signer, Error>;
130
131 fn get_secret_key_for_pubkey(&self, pubkey: &PublicKey) -> Result<SecretKey, Error>;
135 fn get_new_secret_key(&self) -> Result<SecretKey, Error>;
139}
140
141#[async_trait::async_trait]
142pub trait Wallet {
145 async fn get_new_address(&self) -> Result<Address, Error>;
147 async fn get_new_change_address(&self) -> Result<Address, Error>;
149 async fn get_utxos_for_amount(
151 &self,
152 amount: Amount,
153 fee_rate: u64,
154 lock_utxos: bool,
155 ) -> Result<Vec<Utxo>, Error>;
156 async fn sign_psbt_input(&self, psbt: &mut Psbt, input_index: usize) -> Result<(), Error>;
158 fn import_address(&self, address: &Address) -> Result<(), Error>;
160 fn unreserve_utxos(&self, outpoints: &[OutPoint]) -> Result<(), Error>;
162}
163
164#[async_trait::async_trait]
165pub trait Blockchain {
167 async fn send_transaction(&self, transaction: &Transaction) -> Result<(), Error>;
169 fn get_network(&self) -> Result<bitcoin::Network, Error>;
171 async fn get_blockchain_height(&self) -> Result<u64, Error>;
173 async fn get_block_at_height(&self, height: u64) -> Result<Block, Error>;
175 async fn get_transaction(&self, tx_id: &Txid) -> Result<Transaction, Error>;
177 async fn get_transaction_confirmations(&self, tx_id: &Txid) -> Result<u32, Error>;
179}
180
181#[async_trait::async_trait]
182pub trait Storage {
184 async fn get_contract(&self, id: &ContractId) -> Result<Option<Contract>, Error>;
186 async fn get_contracts(&self) -> Result<Vec<Contract>, Error>;
188 async fn create_contract(&self, contract: &OfferedContract) -> Result<(), Error>;
190 async fn delete_contract(&self, id: &ContractId) -> Result<(), Error>;
192 async fn update_contract(&self, contract: &Contract) -> Result<(), Error>;
194 async fn get_contract_offers(&self) -> Result<Vec<OfferedContract>, Error>;
196 async fn get_signed_contracts(&self) -> Result<Vec<SignedContract>, Error>;
198 async fn get_confirmed_contracts(&self) -> Result<Vec<SignedContract>, Error>;
200 async fn get_preclosed_contracts(&self) -> Result<Vec<PreClosedContract>, Error>;
203 async fn upsert_channel(
206 &self,
207 channel: Channel,
208 contract: Option<Contract>,
209 ) -> Result<(), Error>;
210 async fn delete_channel(&self, channel_id: &ChannelId) -> Result<(), Error>;
212 async fn get_channel(&self, channel_id: &ChannelId) -> Result<Option<Channel>, Error>;
214 async fn get_signed_channels(
217 &self,
218 channel_state: Option<SignedChannelStateType>,
219 ) -> Result<Vec<SignedChannel>, Error>;
220 async fn get_offered_channels(&self) -> Result<Vec<OfferedChannel>, Error>;
222 async fn persist_chain_monitor(&self, monitor: &ChainMonitor) -> Result<(), Error>;
224 async fn get_chain_monitor(&self) -> Result<Option<ChainMonitor>, Error>;
226}
227
228#[async_trait::async_trait]
229pub trait Oracle {
231 fn get_public_key(&self) -> XOnlyPublicKey;
233 async fn get_announcement(&self, event_id: &str) -> Result<OracleAnnouncement, Error>;
235 async fn get_attestation(&self, event_id: &str) -> Result<OracleAttestation, Error>;
237}
238
239#[derive(Clone, Debug)]
241pub struct Utxo {
242 pub tx_out: TxOut,
244 pub outpoint: OutPoint,
246 pub address: Address,
248 pub redeem_script: ScriptBuf,
250 pub reserved: bool,
253}
254
255impl_dlc_writeable!(Utxo, {
256 (tx_out, writeable),
257 (outpoint, writeable),
258 (address, {cb_writeable, write_address, read_address}),
259 (redeem_script, writeable),
260 (reserved, writeable)
261});
262
263#[derive(Debug)]
265pub struct CachedContractSignerProvider<SP: Deref, X>
266where
267 SP::Target: ContractSignerProvider<Signer = X>,
268{
269 pub(crate) signer_provider: SP,
270 pub(crate) cache: RwLock<HashMap<KeysId, X>>,
271}
272
273impl<SP: Deref, X> CachedContractSignerProvider<SP, X>
274where
275 SP::Target: ContractSignerProvider<Signer = X>,
276{
277 pub fn new(signer_provider: SP) -> Self {
279 Self {
280 signer_provider,
281 cache: RwLock::new(HashMap::new()),
282 }
283 }
284}
285
286impl<SP: Deref, X: ContractSigner> ContractSignerProvider for CachedContractSignerProvider<SP, X>
287where
288 SP::Target: ContractSignerProvider<Signer = X>,
289{
290 type Signer = X;
291
292 fn derive_signer_key_id(&self, is_offer_party: bool, temp_id: [u8; 32]) -> KeysId {
293 self.signer_provider
294 .derive_signer_key_id(is_offer_party, temp_id)
295 }
296
297 fn derive_contract_signer(&self, key_id: KeysId) -> Result<Self::Signer, Error> {
298 match self.cache.try_read().unwrap().get(&key_id) {
299 Some(signer) => Ok(signer.clone()),
300 None => self.signer_provider.derive_contract_signer(key_id),
301 }
302 }
303
304 fn get_secret_key_for_pubkey(&self, pubkey: &PublicKey) -> Result<SecretKey, Error> {
305 self.signer_provider.get_secret_key_for_pubkey(pubkey)
306 }
307
308 fn get_new_secret_key(&self) -> Result<SecretKey, Error> {
309 self.signer_provider.get_new_secret_key()
310 }
311}