use zeroize::Zeroize;
use crate::{
conditions::{Condition, ConditionTemplates, HtlcTemplate},
hash::sha256,
Error, PublicKey, Result,
};
pub struct SwapSecret {
pub secret: [u8; 32],
pub hash: [u8; 32],
}
impl Drop for SwapSecret {
fn drop(&mut self) {
self.secret.zeroize();
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AtomicSwapParams {
pub secret_hash: [u8; 32],
pub initiator_key: PublicKey,
pub participant_key: PublicKey,
pub initiator_refund_height: u64,
pub participant_refund_height: u64,
pub safety_margin_blocks: u64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SwapLocks {
pub initiator_lock: Condition,
pub participant_lock: Condition,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ChainClock {
pub tip: u64,
pub block_seconds: u64,
}
impl ChainClock {
pub fn height_after(&self, seconds: u64) -> u64 {
let blocks = seconds.div_ceil(self.block_seconds.max(1));
self.tip.saturating_add(blocks)
}
pub fn seconds_until(&self, height: u64) -> u64 {
height.saturating_sub(self.tip).saturating_mul(self.block_seconds)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SwapLeg {
pub clock: ChainClock,
pub refund_height: u64,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SwapTimeouts {
pub initiator_refund_seconds: u64,
pub participant_refund_seconds: u64,
}
#[derive(Debug, Clone, Copy)]
pub struct AtomicSwap;
impl AtomicSwap {
pub fn new_secret() -> Result<SwapSecret> {
let mut secret = [0u8; 32];
getrandom::getrandom(&mut secret).map_err(|_| Error::InvalidSwap("no randomness".into()))?;
let hash = sha256(&secret);
Ok(SwapSecret { secret, hash })
}
pub fn locks(p: AtomicSwapParams) -> Result<SwapLocks> {
if p.initiator_refund_height < p.participant_refund_height.saturating_add(p.safety_margin_blocks) {
return Err(Error::InvalidSwap(
"initiator refund height must be at least safety_margin_blocks after the participant refund height".into(),
));
}
Ok(SwapLocks {
initiator_lock: Self::funding_lock(
p.initiator_key,
p.participant_key,
p.secret_hash,
p.initiator_refund_height,
),
participant_lock: Self::funding_lock(
p.participant_key,
p.initiator_key,
p.secret_hash,
p.participant_refund_height,
),
})
}
pub fn funding_lock(
funder: PublicKey,
counterparty: PublicKey,
secret_hash: [u8; 32],
refund_height: u64,
) -> Condition {
ConditionTemplates::htlc(HtlcTemplate {
recipient_key: counterparty,
refund_key: funder,
secret_hash,
refund_height,
})
}
pub fn check_cross_chain(
initiator: SwapLeg,
participant: SwapLeg,
safety_margin_seconds: u64,
) -> Result<SwapTimeouts> {
let t = SwapTimeouts {
initiator_refund_seconds: initiator.clock.seconds_until(initiator.refund_height),
participant_refund_seconds: participant.clock.seconds_until(participant.refund_height),
};
if t.participant_refund_seconds == 0 {
return Err(Error::InvalidSwap(
"participant refund height is not in the future".into(),
));
}
if t.initiator_refund_seconds < t.participant_refund_seconds.saturating_add(safety_margin_seconds) {
return Err(Error::InvalidSwap(format!(
"initiator refund opens in ~{}s, must be at least {}s after the participant refund (~{}s)",
t.initiator_refund_seconds, safety_margin_seconds, t.participant_refund_seconds
)));
}
Ok(t)
}
pub fn revealed_secret<'a>(
preimages: impl IntoIterator<Item = &'a [u8]>,
secret_hash: &[u8; 32],
) -> Option<Vec<u8>> {
preimages
.into_iter()
.find(|p| &sha256(p) == secret_hash)
.map(<[u8]>::to_vec)
}
}