torvyon_core/
atomic_swap.rs1use zeroize::Zeroize;
16
17use crate::{
18 conditions::{Condition, ConditionTemplates, HtlcTemplate},
19 hash::sha256,
20 Error, PublicKey, Result,
21};
22
23pub struct SwapSecret {
25 pub secret: [u8; 32],
27 pub hash: [u8; 32],
29}
30
31impl Drop for SwapSecret {
32 fn drop(&mut self) {
33 self.secret.zeroize();
34 }
35}
36
37#[derive(Debug, Clone, PartialEq, Eq)]
39pub struct AtomicSwapParams {
40 pub secret_hash: [u8; 32],
42 pub initiator_key: PublicKey,
44 pub participant_key: PublicKey,
46 pub initiator_refund_height: u64,
48 pub participant_refund_height: u64,
50 pub safety_margin_blocks: u64,
52}
53
54#[derive(Debug, Clone, PartialEq, Eq)]
56pub struct SwapLocks {
57 pub initiator_lock: Condition,
59 pub participant_lock: Condition,
61}
62
63#[derive(Debug, Clone, Copy, PartialEq, Eq)]
65pub struct ChainClock {
66 pub tip: u64,
68 pub block_seconds: u64,
70}
71
72impl ChainClock {
73 pub fn height_after(&self, seconds: u64) -> u64 {
75 let blocks = seconds.div_ceil(self.block_seconds.max(1));
76 self.tip.saturating_add(blocks)
77 }
78
79 pub fn seconds_until(&self, height: u64) -> u64 {
81 height.saturating_sub(self.tip).saturating_mul(self.block_seconds)
82 }
83}
84
85#[derive(Debug, Clone, Copy, PartialEq, Eq)]
88pub struct SwapLeg {
89 pub clock: ChainClock,
91 pub refund_height: u64,
93}
94
95#[derive(Debug, Clone, Copy, PartialEq, Eq)]
97pub struct SwapTimeouts {
98 pub initiator_refund_seconds: u64,
100 pub participant_refund_seconds: u64,
102}
103
104#[derive(Debug, Clone, Copy)]
106pub struct AtomicSwap;
107
108impl AtomicSwap {
109 pub fn new_secret() -> Result<SwapSecret> {
111 let mut secret = [0u8; 32];
112 getrandom::getrandom(&mut secret).map_err(|_| Error::InvalidSwap("no randomness".into()))?;
113 let hash = sha256(&secret);
114 Ok(SwapSecret { secret, hash })
115 }
116
117 pub fn locks(p: AtomicSwapParams) -> Result<SwapLocks> {
120 if p.initiator_refund_height < p.participant_refund_height.saturating_add(p.safety_margin_blocks) {
121 return Err(Error::InvalidSwap(
122 "initiator refund height must be at least safety_margin_blocks after the participant refund height".into(),
123 ));
124 }
125 Ok(SwapLocks {
126 initiator_lock: Self::funding_lock(
127 p.initiator_key,
128 p.participant_key,
129 p.secret_hash,
130 p.initiator_refund_height,
131 ),
132 participant_lock: Self::funding_lock(
133 p.participant_key,
134 p.initiator_key,
135 p.secret_hash,
136 p.participant_refund_height,
137 ),
138 })
139 }
140
141 pub fn funding_lock(
145 funder: PublicKey,
146 counterparty: PublicKey,
147 secret_hash: [u8; 32],
148 refund_height: u64,
149 ) -> Condition {
150 ConditionTemplates::htlc(HtlcTemplate {
151 recipient_key: counterparty,
152 refund_key: funder,
153 secret_hash,
154 refund_height,
155 })
156 }
157
158 pub fn check_cross_chain(
162 initiator: SwapLeg,
163 participant: SwapLeg,
164 safety_margin_seconds: u64,
165 ) -> Result<SwapTimeouts> {
166 let t = SwapTimeouts {
167 initiator_refund_seconds: initiator.clock.seconds_until(initiator.refund_height),
168 participant_refund_seconds: participant.clock.seconds_until(participant.refund_height),
169 };
170 if t.participant_refund_seconds == 0 {
171 return Err(Error::InvalidSwap(
172 "participant refund height is not in the future".into(),
173 ));
174 }
175 if t.initiator_refund_seconds < t.participant_refund_seconds.saturating_add(safety_margin_seconds) {
176 return Err(Error::InvalidSwap(format!(
177 "initiator refund opens in ~{}s, must be at least {}s after the participant refund (~{}s)",
178 t.initiator_refund_seconds, safety_margin_seconds, t.participant_refund_seconds
179 )));
180 }
181 Ok(t)
182 }
183
184 pub fn revealed_secret<'a>(
186 preimages: impl IntoIterator<Item = &'a [u8]>,
187 secret_hash: &[u8; 32],
188 ) -> Option<Vec<u8>> {
189 preimages
190 .into_iter()
191 .find(|p| &sha256(p) == secret_hash)
192 .map(<[u8]>::to_vec)
193 }
194}