1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#![crate_name = "dlc_manager"]
#![forbid(unsafe_code)]
#![deny(non_upper_case_globals)]
#![deny(non_camel_case_types)]
#![deny(non_snake_case)]
#![deny(unused_mut)]
#![deny(dead_code)]
#![deny(unused_imports)]
#![deny(missing_docs)]
extern crate async_trait;
extern crate bitcoin;
extern crate dlc;
#[macro_use]
extern crate dlc_messages;
extern crate core;
extern crate dlc_trie;
extern crate lightning;
extern crate log;
#[cfg(feature = "fuzztarget")]
extern crate rand_chacha;
extern crate secp256k1_zkp;
pub mod chain_monitor;
pub mod channel;
pub mod channel_updater;
pub mod contract;
pub mod contract_updater;
mod conversion_utils;
pub mod error;
pub mod manager;
pub mod payout_curve;
mod utils;
use bitcoin::{Address, Block, OutPoint, Script, Transaction, TxOut, Txid};
use chain_monitor::ChainMonitor;
use channel::offered_channel::OfferedChannel;
use channel::signed_channel::{SignedChannel, SignedChannelStateType};
use channel::Channel;
use contract::PreClosedContract;
use contract::{offered_contract::OfferedContract, signed_contract::SignedContract, Contract};
use dlc_messages::oracle_msgs::{OracleAnnouncement, OracleAttestation};
use dlc_messages::ser_impls::{read_address, write_address};
use error::Error;
use lightning::ln::msgs::DecodeError;
use lightning::util::ser::{Readable, Writeable, Writer};
use secp256k1_zkp::XOnlyPublicKey;
use secp256k1_zkp::{PublicKey, SecretKey};
pub type ContractId = [u8; 32];
pub type ChannelId = [u8; 32];
pub trait Time {
fn unix_time_now(&self) -> u64;
}
pub struct SystemTimeProvider {}
impl Time for SystemTimeProvider {
fn unix_time_now(&self) -> u64 {
let now = std::time::SystemTime::now();
now.duration_since(std::time::UNIX_EPOCH)
.expect("Unexpected time error")
.as_secs()
}
}
pub trait Signer {
fn sign_tx_input(
&self,
tx: &mut Transaction,
input_index: usize,
tx_out: &TxOut,
redeem_script: Option<Script>,
) -> Result<(), Error>;
fn get_secret_key_for_pubkey(&self, pubkey: &PublicKey) -> Result<SecretKey, Error>;
}
pub trait Wallet: Signer {
fn get_new_address(&self) -> Result<Address, Error>;
fn get_new_secret_key(&self) -> Result<SecretKey, Error>;
fn get_utxos_for_amount(
&self,
amount: u64,
fee_rate: Option<u64>,
lock_utxos: bool,
) -> Result<Vec<Utxo>, Error>;
fn import_address(&self, address: &Address) -> Result<(), Error>;
}
pub trait Blockchain {
fn send_transaction(&self, transaction: &Transaction) -> Result<(), Error>;
fn get_network(&self) -> Result<bitcoin::network::constants::Network, Error>;
fn get_blockchain_height(&self) -> Result<u64, Error>;
fn get_block_at_height(&self, height: u64) -> Result<Block, Error>;
fn get_transaction(&self, tx_id: &Txid) -> Result<Transaction, Error>;
fn get_transaction_confirmations(&self, tx_id: &Txid) -> Result<u32, Error>;
}
pub trait Storage {
fn get_contract(&self, id: &ContractId) -> Result<Option<Contract>, Error>;
fn get_contracts(&self) -> Result<Vec<Contract>, Error>;
fn create_contract(&self, contract: &OfferedContract) -> Result<(), Error>;
fn delete_contract(&self, id: &ContractId) -> Result<(), Error>;
fn update_contract(&self, contract: &Contract) -> Result<(), Error>;
fn get_contract_offers(&self) -> Result<Vec<OfferedContract>, Error>;
fn get_signed_contracts(&self) -> Result<Vec<SignedContract>, Error>;
fn get_confirmed_contracts(&self) -> Result<Vec<SignedContract>, Error>;
fn get_preclosed_contracts(&self) -> Result<Vec<PreClosedContract>, Error>;
fn upsert_channel(&self, channel: Channel, contract: Option<Contract>) -> Result<(), Error>;
fn delete_channel(&self, channel_id: &ChannelId) -> Result<(), Error>;
fn get_channel(&self, channel_id: &ChannelId) -> Result<Option<Channel>, Error>;
fn get_signed_channels(
&self,
channel_state: Option<SignedChannelStateType>,
) -> Result<Vec<SignedChannel>, Error>;
fn get_offered_channels(&self) -> Result<Vec<OfferedChannel>, Error>;
fn persist_chain_monitor(&self, monitor: &ChainMonitor) -> Result<(), Error>;
fn get_chain_monitor(&self) -> Result<Option<ChainMonitor>, Error>;
}
pub trait Oracle {
fn get_public_key(&self) -> XOnlyPublicKey;
fn get_announcement(&self, event_id: &str) -> Result<OracleAnnouncement, Error>;
fn get_attestation(&self, event_id: &str) -> Result<OracleAttestation, Error>;
}
#[derive(Clone, Debug)]
pub struct Utxo {
pub tx_out: TxOut,
pub outpoint: OutPoint,
pub address: Address,
pub redeem_script: Script,
pub reserved: bool,
}
impl_dlc_writeable!(Utxo, {
(tx_out, writeable),
(outpoint, writeable),
(address, {cb_writeable, write_address, read_address}),
(redeem_script, writeable),
(reserved, writeable)
});