1
2pub extern crate bitcoin;
3
4#[macro_use] extern crate serde;
5#[macro_use] extern crate lazy_static;
6
7#[macro_use] pub mod util;
8
9pub mod address;
10pub mod arkoor;
11pub mod attestations;
12pub mod board;
13pub mod connectors;
14pub mod encode;
15pub mod error;
16pub mod fees;
17pub mod forfeit;
18pub mod lightning;
19pub mod mailbox;
20pub mod musig;
21pub mod offboard;
22pub mod rounds;
23pub mod time;
24pub mod tree;
25pub mod vtxo;
26pub mod integration;
27
28pub use crate::address::Address;
29pub use crate::encode::{ProtocolEncoding, WriteExt, ReadExt, ProtocolDecodingError};
30pub use crate::vtxo::{Vtxo, VtxoId, VtxoPolicy, ServerVtxoPolicy, ServerVtxo};
31
32#[cfg(test)]
33mod napkin;
34#[cfg(any(test, feature = "test-util"))]
35pub mod test_util;
36
37
38use std::time::Duration;
39
40use bitcoin::{Amount, FeeRate, Network};
41use bitcoin::secp256k1::{self, PublicKey};
42
43use bitcoin_ext::BlockDelta;
44
45use crate::fees::FeeSchedule;
46
47lazy_static! {
48 pub static ref SECP: secp256k1::Secp256k1<secp256k1::All> = secp256k1::Secp256k1::new();
50}
51
52#[derive(Debug, Clone, PartialEq, Eq)]
53pub struct ArkInfo {
54 pub network: Network,
56 pub server_pubkey: PublicKey,
58 pub mailbox_pubkey: PublicKey,
60 pub round_interval: Duration,
62 pub nb_round_nonces: usize,
64 pub vtxo_exit_delta: BlockDelta,
66 pub vtxo_expiry_delta: BlockDelta,
68 pub htlc_send_expiry_delta: BlockDelta,
70 pub htlc_expiry_delta: BlockDelta,
72 pub max_vtxo_amount: Option<Amount>,
74 pub required_board_confirmations: usize,
76 pub max_user_invoice_cltv_delta: u16,
79 pub min_board_amount: Amount,
81
82 pub offboard_feerate: FeeRate,
86
87 pub ln_receive_anti_dos_required: bool,
91
92 pub fees: FeeSchedule,
94
95 pub max_vtxo_exit_depth: u16,
100}
101
102#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)]
104pub struct VtxoRequest {
105 pub amount: Amount,
106 #[serde(with = "crate::encode::serde")]
107 pub policy: VtxoPolicy,
108}
109
110impl AsRef<VtxoRequest> for VtxoRequest {
111 fn as_ref(&self) -> &VtxoRequest {
112 self
113 }
114}
115
116#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)]
118pub struct SignedVtxoRequest {
119 pub vtxo: VtxoRequest,
121 pub cosign_pubkey: PublicKey,
124 pub nonces: Vec<musig::PublicNonce>,
126}
127
128impl AsRef<VtxoRequest> for SignedVtxoRequest {
129 fn as_ref(&self) -> &VtxoRequest {
130 &self.vtxo
131 }
132}
133
134pub mod scripts {
135 use bitcoin::{opcodes, ScriptBuf, TapSighash, TapTweakHash, Transaction};
136 use bitcoin::hashes::{sha256, ripemd160, Hash};
137 use bitcoin::secp256k1::{schnorr, PublicKey, XOnlyPublicKey};
138
139 use bitcoin_ext::{BlockDelta, BlockHeight, TAPROOT_KEYSPEND_WEIGHT};
140
141 use crate::musig;
142
143 pub fn delayed_sign(delay_blocks: BlockDelta, pubkey: XOnlyPublicKey) -> ScriptBuf {
145 let csv = bitcoin::Sequence::from_height(delay_blocks);
146 bitcoin::Script::builder()
147 .push_int(csv.to_consensus_u32() as i64)
148 .push_opcode(opcodes::all::OP_CSV)
149 .push_opcode(opcodes::all::OP_DROP)
150 .push_x_only_key(&pubkey)
151 .push_opcode(opcodes::all::OP_CHECKSIG)
152 .into_script()
153 }
154
155 pub fn timelock_sign(timelock_height: BlockHeight, pubkey: XOnlyPublicKey) -> ScriptBuf {
157 let lt = bitcoin::absolute::LockTime::from_height(timelock_height).unwrap();
158 bitcoin::Script::builder()
159 .push_int(lt.to_consensus_u32() as i64)
160 .push_opcode(opcodes::all::OP_CLTV)
161 .push_opcode(opcodes::all::OP_DROP)
162 .push_x_only_key(&pubkey)
163 .push_opcode(opcodes::all::OP_CHECKSIG)
164 .into_script()
165 }
166
167 pub fn delay_timelock_sign(
169 delay_blocks: BlockDelta,
170 timelock_height: BlockHeight,
171 pubkey: XOnlyPublicKey,
172 ) -> ScriptBuf {
173 let csv = bitcoin::Sequence::from_height(delay_blocks);
174 let lt = bitcoin::absolute::LockTime::from_height(timelock_height).unwrap();
175 bitcoin::Script::builder()
176 .push_int(lt.to_consensus_u32().try_into().unwrap())
177 .push_opcode(opcodes::all::OP_CLTV)
178 .push_opcode(opcodes::all::OP_DROP)
179 .push_int(csv.to_consensus_u32().try_into().unwrap())
180 .push_opcode(opcodes::all::OP_CSV)
181 .push_opcode(opcodes::all::OP_DROP)
182 .push_x_only_key(&pubkey)
183 .push_opcode(opcodes::all::OP_CHECKSIG)
184 .into_script()
185 }
186
187 pub fn hash_and_sign(hash: sha256::Hash, pubkey: XOnlyPublicKey) -> ScriptBuf {
193 let hash_160 = ripemd160::Hash::hash(&hash[..]);
194
195 bitcoin::Script::builder()
196 .push_opcode(opcodes::all::OP_HASH160)
197 .push_slice(hash_160.as_byte_array())
198 .push_opcode(opcodes::all::OP_EQUALVERIFY)
199 .push_x_only_key(&pubkey)
200 .push_opcode(opcodes::all::OP_CHECKSIG)
201 .into_script()
202 }
203
204 pub fn hash_delay_sign(
205 hash: sha256::Hash,
206 delay_blocks: BlockDelta,
207 pubkey: XOnlyPublicKey,
208 ) -> ScriptBuf {
209 let hash_160 = ripemd160::Hash::hash(&hash[..]);
210 let csv = bitcoin::Sequence::from_height(delay_blocks);
211
212 bitcoin::Script::builder()
213 .push_int(csv.to_consensus_u32().try_into().unwrap())
214 .push_opcode(opcodes::all::OP_CSV)
215 .push_opcode(opcodes::all::OP_DROP)
216 .push_opcode(opcodes::all::OP_HASH160)
217 .push_slice(hash_160.as_byte_array())
218 .push_opcode(opcodes::all::OP_EQUALVERIFY)
219 .push_x_only_key(&pubkey)
220 .push_opcode(opcodes::all::OP_CHECKSIG)
221 .into_script()
222 }
223
224 pub fn fill_taproot_sigs(tx: &mut Transaction, sigs: &[schnorr::Signature]) {
229 assert_eq!(tx.input.len(), sigs.len());
230 for (input, sig) in tx.input.iter_mut().zip(sigs.iter()) {
231 assert!(input.witness.is_empty());
232 input.witness.push(&sig[..]);
233 debug_assert_eq!(TAPROOT_KEYSPEND_WEIGHT, input.witness.size());
234 }
235 }
236
237 pub fn verify_partial_sig(
239 sighash: TapSighash,
240 tweak: TapTweakHash,
241 signer: (PublicKey, &musig::PublicNonce),
242 other: (PublicKey, &musig::PublicNonce),
243 partial_signature: &musig::PartialSignature,
244 ) -> bool {
245 let agg_nonce = musig::nonce_agg(&[&signer.1, &other.1]);
246 let agg_pk = musig::tweaked_key_agg([signer.0, other.0], tweak.to_byte_array()).0;
247
248 let session = musig::Session::new(&agg_pk, agg_nonce, &sighash.to_byte_array());
249 session.partial_verify(
250 &agg_pk, partial_signature, signer.1, musig::pubkey_to(signer.0),
251 )
252 }
253}