2020-09-28 16:18:50 +10:00
|
|
|
#![warn(
|
|
|
|
unused_extern_crates,
|
|
|
|
missing_debug_implementations,
|
|
|
|
missing_copy_implementations,
|
|
|
|
rust_2018_idioms,
|
|
|
|
clippy::cast_possible_truncation,
|
|
|
|
clippy::cast_sign_loss,
|
|
|
|
clippy::fallible_impl_from,
|
|
|
|
clippy::cast_precision_loss,
|
|
|
|
clippy::cast_possible_wrap,
|
|
|
|
clippy::dbg_macro
|
|
|
|
)]
|
|
|
|
#![cfg_attr(not(test), warn(clippy::unwrap_used))]
|
|
|
|
#![forbid(unsafe_code)]
|
|
|
|
#![allow(non_snake_case)]
|
|
|
|
|
2020-10-09 10:17:36 +11:00
|
|
|
#[macro_use]
|
|
|
|
mod utils {
|
|
|
|
|
|
|
|
macro_rules! impl_try_from_parent_enum {
|
|
|
|
($type:ident, $parent:ident) => {
|
|
|
|
impl TryFrom<$parent> for $type {
|
|
|
|
type Error = anyhow::Error;
|
|
|
|
fn try_from(from: $parent) -> Result<Self> {
|
|
|
|
if let $parent::$type(inner) = from {
|
|
|
|
Ok(inner)
|
|
|
|
} else {
|
|
|
|
Err(anyhow::anyhow!(
|
|
|
|
"Failed to convert parent state to child state"
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! impl_from_child_enum {
|
|
|
|
($type:ident, $parent:ident) => {
|
|
|
|
impl From<$type> for $parent {
|
|
|
|
fn from(from: $type) -> Self {
|
|
|
|
$parent::$type(from)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-28 16:18:50 +10:00
|
|
|
pub mod alice;
|
|
|
|
pub mod bitcoin;
|
|
|
|
pub mod bob;
|
|
|
|
pub mod monero;
|
2020-10-14 09:32:25 +11:00
|
|
|
pub mod serde;
|
2020-09-29 15:36:50 +10:00
|
|
|
pub mod transport;
|
2020-10-12 17:17:22 +11:00
|
|
|
|
2020-10-23 13:13:17 +11:00
|
|
|
pub use cross_curve_dleq::Scalar as CrossCurveScalar;
|
|
|
|
pub use curve25519_dalek::scalar::Scalar as Curve25519Scalar;
|
|
|
|
|
2020-10-15 18:34:55 +11:00
|
|
|
use async_trait::async_trait;
|
2020-10-12 17:17:22 +11:00
|
|
|
use ecdsa_fun::{adaptor::Adaptor, nonce::Deterministic};
|
2020-10-15 21:17:42 +11:00
|
|
|
use futures::{
|
|
|
|
future::{select, Either},
|
2020-10-15 21:48:08 +11:00
|
|
|
Future, FutureExt,
|
2020-10-15 21:17:42 +11:00
|
|
|
};
|
2020-10-12 17:17:22 +11:00
|
|
|
use genawaiter::sync::{Gen, GenBoxed};
|
|
|
|
use sha2::Sha256;
|
2020-10-22 10:57:42 +11:00
|
|
|
use std::{sync::Arc, time::Duration};
|
|
|
|
use tokio::time::timeout;
|
|
|
|
use tracing::error;
|
|
|
|
|
|
|
|
// TODO: Replace this with something configurable, such as an function argument.
|
|
|
|
/// Time that Bob has to publish the Bitcoin lock transaction before both
|
|
|
|
/// parties will abort, in seconds.
|
|
|
|
const SECS_TO_ACT_BOB: u64 = 60;
|
2020-10-12 17:17:22 +11:00
|
|
|
|
|
|
|
#[allow(clippy::large_enum_variant)]
|
|
|
|
#[derive(Debug)]
|
2020-10-16 17:04:03 +11:00
|
|
|
pub enum BobAction {
|
2020-10-12 17:17:22 +11:00
|
|
|
LockBitcoin(bitcoin::TxLock),
|
|
|
|
SendBitcoinRedeemEncsig(bitcoin::EncryptedSignature),
|
|
|
|
CreateMoneroWalletForOutput {
|
|
|
|
spend_key: monero::PrivateKey,
|
|
|
|
view_key: monero::PrivateViewKey,
|
|
|
|
},
|
2020-10-16 14:06:25 +11:00
|
|
|
CancelBitcoin(bitcoin::Transaction),
|
|
|
|
RefundBitcoin(bitcoin::Transaction),
|
2020-10-12 17:17:22 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: This could be moved to the monero module
|
2020-10-16 10:49:34 +11:00
|
|
|
#[async_trait]
|
2020-10-12 17:17:22 +11:00
|
|
|
pub trait ReceiveTransferProof {
|
2020-10-16 10:49:34 +11:00
|
|
|
async fn receive_transfer_proof(&mut self) -> monero::TransferProof;
|
2020-10-12 17:17:22 +11:00
|
|
|
}
|
|
|
|
|
2020-10-15 18:34:55 +11:00
|
|
|
#[async_trait]
|
2020-10-22 10:57:42 +11:00
|
|
|
pub trait BlockHeight {
|
|
|
|
async fn block_height(&self) -> u32;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
pub trait TransactionBlockHeight {
|
|
|
|
async fn transaction_block_height(&self, txid: bitcoin::Txid) -> u32;
|
2020-10-15 18:34:55 +11:00
|
|
|
}
|
|
|
|
|
2020-10-12 17:17:22 +11:00
|
|
|
/// Perform the on-chain protocol to swap monero and bitcoin as Bob.
|
|
|
|
///
|
|
|
|
/// This is called post handshake, after all the keys, addresses and most of the
|
|
|
|
/// signatures have been exchanged.
|
|
|
|
pub fn action_generator_bob<N, M, B>(
|
2020-10-22 10:57:42 +11:00
|
|
|
mut network: N,
|
|
|
|
monero_client: Arc<M>,
|
|
|
|
bitcoin_client: Arc<B>,
|
2020-10-12 17:17:22 +11:00
|
|
|
// TODO: Replace this with a new, slimmer struct?
|
|
|
|
bob::State2 {
|
|
|
|
A,
|
|
|
|
b,
|
|
|
|
s_b,
|
|
|
|
S_a_monero,
|
|
|
|
S_a_bitcoin,
|
|
|
|
v,
|
|
|
|
xmr,
|
|
|
|
refund_timelock,
|
|
|
|
redeem_address,
|
|
|
|
refund_address,
|
|
|
|
tx_lock,
|
|
|
|
tx_cancel_sig_a,
|
|
|
|
tx_refund_encsig,
|
|
|
|
..
|
|
|
|
}: bob::State2,
|
2020-10-16 17:04:03 +11:00
|
|
|
) -> GenBoxed<BobAction, (), ()>
|
2020-10-12 17:17:22 +11:00
|
|
|
where
|
2020-10-22 10:57:42 +11:00
|
|
|
N: ReceiveTransferProof + Send + Sync + 'static,
|
|
|
|
M: monero::WatchForTransfer + Send + Sync + 'static,
|
|
|
|
B: BlockHeight
|
|
|
|
+ TransactionBlockHeight
|
|
|
|
+ bitcoin::WatchForRawTransaction
|
|
|
|
+ Send
|
|
|
|
+ Sync
|
|
|
|
+ 'static,
|
2020-10-12 17:17:22 +11:00
|
|
|
{
|
2020-10-22 10:57:42 +11:00
|
|
|
#[derive(Debug)]
|
2020-10-15 18:34:55 +11:00
|
|
|
enum SwapFailed {
|
2020-10-16 11:18:02 +11:00
|
|
|
BeforeBtcLock,
|
|
|
|
AfterBtcLock(Reason),
|
2020-10-16 11:43:24 +11:00
|
|
|
AfterBtcRedeem(Reason),
|
2020-10-16 11:18:02 +11:00
|
|
|
}
|
|
|
|
|
2020-10-16 11:31:16 +11:00
|
|
|
/// Reason why the swap has failed.
|
2020-10-22 10:57:42 +11:00
|
|
|
#[derive(Debug)]
|
2020-10-16 11:18:02 +11:00
|
|
|
enum Reason {
|
2020-10-16 11:31:16 +11:00
|
|
|
/// The refund timelock has been reached.
|
2020-10-16 11:18:02 +11:00
|
|
|
BtcExpired,
|
2020-10-16 11:31:16 +11:00
|
|
|
/// Alice did not lock up enough monero in the shared output.
|
2020-10-16 14:02:47 +11:00
|
|
|
InsufficientXmr(monero::InsufficientFunds),
|
2020-10-16 11:43:24 +11:00
|
|
|
/// Could not find Bob's signature on the redeem transaction witness
|
|
|
|
/// stack.
|
|
|
|
BtcRedeemSignature,
|
|
|
|
/// Could not recover secret `s_a` from Bob's redeem transaction
|
|
|
|
/// signature.
|
|
|
|
SecretRecovery,
|
2020-10-15 13:10:31 +11:00
|
|
|
}
|
|
|
|
|
2020-10-15 21:48:08 +11:00
|
|
|
async fn poll_until(condition_future: impl Future<Output = bool> + Clone) {
|
2020-10-15 18:34:55 +11:00
|
|
|
loop {
|
2020-10-15 21:48:08 +11:00
|
|
|
if condition_future.clone().await {
|
2020-10-15 18:34:55 +11:00
|
|
|
return;
|
|
|
|
}
|
2020-10-22 10:57:42 +11:00
|
|
|
|
|
|
|
tokio::time::delay_for(std::time::Duration::from_secs(1)).await;
|
2020-10-15 18:34:55 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-22 10:57:42 +11:00
|
|
|
async fn bitcoin_block_height_is_gte<B>(bitcoin_client: &B, n_blocks: u32) -> bool
|
2020-10-15 21:48:08 +11:00
|
|
|
where
|
2020-10-22 10:57:42 +11:00
|
|
|
B: BlockHeight,
|
2020-10-15 21:48:08 +11:00
|
|
|
{
|
2020-10-22 10:57:42 +11:00
|
|
|
bitcoin_client.block_height().await >= n_blocks
|
2020-10-15 21:48:08 +11:00
|
|
|
}
|
|
|
|
|
2020-10-12 17:17:22 +11:00
|
|
|
Gen::new_boxed(|co| async move {
|
2020-10-15 18:34:55 +11:00
|
|
|
let swap_result: Result<(), SwapFailed> = async {
|
2020-10-16 17:04:03 +11:00
|
|
|
co.yield_(BobAction::LockBitcoin(tx_lock.clone())).await;
|
2020-10-12 17:17:22 +11:00
|
|
|
|
2020-10-22 10:57:42 +11:00
|
|
|
timeout(
|
|
|
|
Duration::from_secs(SECS_TO_ACT_BOB),
|
2020-10-16 14:17:10 +11:00
|
|
|
bitcoin_client.watch_for_raw_transaction(tx_lock.txid()),
|
2020-10-16 11:25:41 +11:00
|
|
|
)
|
|
|
|
.await
|
2020-10-22 10:57:42 +11:00
|
|
|
.map(|tx| tx.txid())
|
|
|
|
.map_err(|_| SwapFailed::BeforeBtcLock)?;
|
|
|
|
|
|
|
|
let tx_lock_height = bitcoin_client
|
|
|
|
.transaction_block_height(tx_lock.txid())
|
|
|
|
.await;
|
|
|
|
let btc_has_expired = bitcoin_block_height_is_gte(
|
|
|
|
bitcoin_client.as_ref(),
|
|
|
|
tx_lock_height + refund_timelock,
|
|
|
|
)
|
|
|
|
.shared();
|
|
|
|
let poll_until_btc_has_expired = poll_until(btc_has_expired).shared();
|
|
|
|
futures::pin_mut!(poll_until_btc_has_expired);
|
2020-10-15 18:34:55 +11:00
|
|
|
|
2020-10-16 10:54:12 +11:00
|
|
|
let transfer_proof = match select(
|
|
|
|
network.receive_transfer_proof(),
|
|
|
|
poll_until_btc_has_expired.clone(),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
Either::Left((proof, _)) => proof,
|
2020-10-16 11:18:02 +11:00
|
|
|
Either::Right(_) => return Err(SwapFailed::AfterBtcLock(Reason::BtcExpired)),
|
2020-10-16 10:54:12 +11:00
|
|
|
};
|
2020-10-12 17:17:22 +11:00
|
|
|
|
|
|
|
let S_b_monero = monero::PublicKey::from_private_key(&monero::PrivateKey::from_scalar(
|
|
|
|
s_b.into_ed25519(),
|
|
|
|
));
|
|
|
|
let S = S_a_monero + S_b_monero;
|
|
|
|
|
2020-10-15 21:17:42 +11:00
|
|
|
match select(
|
2020-10-16 14:17:10 +11:00
|
|
|
monero_client.watch_for_transfer(
|
2020-10-15 18:34:55 +11:00
|
|
|
S,
|
|
|
|
v.public(),
|
|
|
|
transfer_proof,
|
|
|
|
xmr,
|
|
|
|
monero::MIN_CONFIRMATIONS,
|
|
|
|
),
|
2020-10-15 21:48:08 +11:00
|
|
|
poll_until_btc_has_expired.clone(),
|
2020-10-15 18:34:55 +11:00
|
|
|
)
|
|
|
|
.await
|
|
|
|
{
|
2020-10-16 11:18:02 +11:00
|
|
|
Either::Left((Err(e), _)) => {
|
2020-10-16 14:02:47 +11:00
|
|
|
return Err(SwapFailed::AfterBtcLock(Reason::InsufficientXmr(e)))
|
2020-10-16 11:18:02 +11:00
|
|
|
}
|
|
|
|
Either::Right(_) => return Err(SwapFailed::AfterBtcLock(Reason::BtcExpired)),
|
2020-10-15 18:34:55 +11:00
|
|
|
_ => {}
|
|
|
|
}
|
2020-10-12 17:17:22 +11:00
|
|
|
|
|
|
|
let tx_redeem = bitcoin::TxRedeem::new(&tx_lock, &redeem_address);
|
|
|
|
let tx_redeem_encsig = b.encsign(S_a_bitcoin.clone(), tx_redeem.digest());
|
|
|
|
|
2020-10-16 17:04:03 +11:00
|
|
|
co.yield_(BobAction::SendBitcoinRedeemEncsig(tx_redeem_encsig.clone()))
|
2020-10-12 17:17:22 +11:00
|
|
|
.await;
|
|
|
|
|
2020-10-15 21:17:42 +11:00
|
|
|
let tx_redeem_published = match select(
|
2020-10-16 14:17:10 +11:00
|
|
|
bitcoin_client.watch_for_raw_transaction(tx_redeem.txid()),
|
2020-10-15 21:48:08 +11:00
|
|
|
poll_until_btc_has_expired,
|
2020-10-15 21:17:42 +11:00
|
|
|
)
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
Either::Left((tx, _)) => tx,
|
2020-10-16 11:18:02 +11:00
|
|
|
Either::Right(_) => return Err(SwapFailed::AfterBtcLock(Reason::BtcExpired)),
|
2020-10-15 21:17:42 +11:00
|
|
|
};
|
2020-10-12 17:17:22 +11:00
|
|
|
|
|
|
|
let tx_redeem_sig = tx_redeem
|
|
|
|
.extract_signature_by_key(tx_redeem_published, b.public())
|
2020-10-16 11:43:24 +11:00
|
|
|
.map_err(|_| SwapFailed::AfterBtcRedeem(Reason::BtcRedeemSignature))?;
|
|
|
|
let s_a = bitcoin::recover(S_a_bitcoin, tx_redeem_sig, tx_redeem_encsig)
|
|
|
|
.map_err(|_| SwapFailed::AfterBtcRedeem(Reason::SecretRecovery))?;
|
2020-10-12 17:17:22 +11:00
|
|
|
let s_a = monero::PrivateKey::from_scalar(monero::Scalar::from_bytes_mod_order(
|
|
|
|
s_a.to_bytes(),
|
|
|
|
));
|
|
|
|
|
|
|
|
let s_b = monero::PrivateKey {
|
|
|
|
scalar: s_b.into_ed25519(),
|
|
|
|
};
|
|
|
|
|
2020-10-16 17:04:03 +11:00
|
|
|
co.yield_(BobAction::CreateMoneroWalletForOutput {
|
2020-10-12 17:17:22 +11:00
|
|
|
spend_key: s_a + s_b,
|
|
|
|
view_key: v,
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
|
|
|
|
Ok(())
|
2020-10-15 13:10:31 +11:00
|
|
|
}
|
|
|
|
.await;
|
2020-10-12 17:17:22 +11:00
|
|
|
|
2020-10-22 10:57:42 +11:00
|
|
|
if let Err(err @ SwapFailed::AfterBtcLock(_)) = swap_result {
|
|
|
|
error!("Swap failed, reason: {:?}", err);
|
|
|
|
|
2020-10-12 17:17:22 +11:00
|
|
|
let tx_cancel =
|
|
|
|
bitcoin::TxCancel::new(&tx_lock, refund_timelock, A.clone(), b.public());
|
2020-10-16 14:16:06 +11:00
|
|
|
let tx_cancel_txid = tx_cancel.txid();
|
2020-10-12 17:17:22 +11:00
|
|
|
let signed_tx_cancel = {
|
|
|
|
let sig_a = tx_cancel_sig_a.clone();
|
|
|
|
let sig_b = b.sign(tx_cancel.digest());
|
|
|
|
|
|
|
|
tx_cancel
|
|
|
|
.clone()
|
|
|
|
.add_signatures(&tx_lock, (A.clone(), sig_a), (b.public(), sig_b))
|
|
|
|
.expect("sig_{a,b} to be valid signatures for tx_cancel")
|
|
|
|
};
|
|
|
|
|
2020-10-16 17:04:03 +11:00
|
|
|
co.yield_(BobAction::CancelBitcoin(signed_tx_cancel)).await;
|
2020-10-16 14:06:25 +11:00
|
|
|
|
2020-10-16 14:17:10 +11:00
|
|
|
let _ = bitcoin_client
|
2020-10-16 14:16:06 +11:00
|
|
|
.watch_for_raw_transaction(tx_cancel_txid)
|
|
|
|
.await;
|
2020-10-16 14:06:25 +11:00
|
|
|
|
2020-10-16 14:16:06 +11:00
|
|
|
let tx_refund = bitcoin::TxRefund::new(&tx_cancel, &refund_address);
|
|
|
|
let tx_refund_txid = tx_refund.txid();
|
|
|
|
let signed_tx_refund = {
|
2020-10-12 17:17:22 +11:00
|
|
|
let adaptor = Adaptor::<Sha256, Deterministic<Sha256>>::default();
|
|
|
|
|
|
|
|
let sig_a =
|
|
|
|
adaptor.decrypt_signature(&s_b.into_secp256k1(), tx_refund_encsig.clone());
|
|
|
|
let sig_b = b.sign(tx_refund.digest());
|
|
|
|
|
|
|
|
tx_refund
|
|
|
|
.add_signatures(&tx_cancel, (A.clone(), sig_a), (b.public(), sig_b))
|
|
|
|
.expect("sig_{a,b} to be valid signatures for tx_refund")
|
|
|
|
};
|
|
|
|
|
2020-10-16 17:04:03 +11:00
|
|
|
co.yield_(BobAction::RefundBitcoin(signed_tx_refund)).await;
|
2020-10-16 14:16:06 +11:00
|
|
|
|
2020-10-16 14:17:10 +11:00
|
|
|
let _ = bitcoin_client
|
2020-10-16 14:16:06 +11:00
|
|
|
.watch_for_raw_transaction(tx_refund_txid)
|
|
|
|
.await;
|
2020-10-12 17:17:22 +11:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-10-16 17:04:03 +11:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum AliceAction {
|
|
|
|
// This action also includes proving to Bob that this has happened, given that our current
|
|
|
|
// protocol requires a transfer proof to verify that the coins have been locked on Monero
|
|
|
|
LockXmr {
|
|
|
|
amount: monero::Amount,
|
|
|
|
public_spend_key: monero::PublicKey,
|
|
|
|
public_view_key: monero::PublicViewKey,
|
|
|
|
},
|
|
|
|
RedeemBtc(bitcoin::Transaction),
|
|
|
|
CreateMoneroWalletForOutput {
|
|
|
|
spend_key: monero::PrivateKey,
|
|
|
|
view_key: monero::PrivateViewKey,
|
|
|
|
},
|
|
|
|
CancelBtc(bitcoin::Transaction),
|
|
|
|
PunishBtc(bitcoin::Transaction),
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: This could be moved to the bitcoin module
|
|
|
|
#[async_trait]
|
|
|
|
pub trait ReceiveBitcoinRedeemEncsig {
|
|
|
|
async fn receive_bitcoin_redeem_encsig(&mut self) -> bitcoin::EncryptedSignature;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Perform the on-chain protocol to swap monero and bitcoin as Alice.
|
|
|
|
///
|
|
|
|
/// This is called post handshake, after all the keys, addresses and most of the
|
|
|
|
/// signatures have been exchanged.
|
2020-10-22 10:57:42 +11:00
|
|
|
pub fn action_generator_alice<N, B>(
|
|
|
|
mut network: N,
|
|
|
|
bitcoin_client: Arc<B>,
|
2020-10-16 17:04:03 +11:00
|
|
|
// TODO: Replace this with a new, slimmer struct?
|
|
|
|
alice::State3 {
|
|
|
|
a,
|
|
|
|
B,
|
|
|
|
s_a,
|
|
|
|
S_b_monero,
|
|
|
|
S_b_bitcoin,
|
|
|
|
v,
|
|
|
|
xmr,
|
|
|
|
refund_timelock,
|
|
|
|
punish_timelock,
|
|
|
|
refund_address,
|
|
|
|
redeem_address,
|
|
|
|
punish_address,
|
|
|
|
tx_lock,
|
|
|
|
tx_punish_sig_bob,
|
|
|
|
tx_cancel_sig_bob,
|
|
|
|
..
|
|
|
|
}: alice::State3,
|
|
|
|
) -> GenBoxed<AliceAction, (), ()>
|
|
|
|
where
|
2020-10-22 10:57:42 +11:00
|
|
|
N: ReceiveBitcoinRedeemEncsig + Send + Sync + 'static,
|
|
|
|
B: BlockHeight
|
|
|
|
+ TransactionBlockHeight
|
|
|
|
+ bitcoin::WatchForRawTransaction
|
|
|
|
+ Send
|
|
|
|
+ Sync
|
|
|
|
+ 'static,
|
2020-10-16 17:04:03 +11:00
|
|
|
{
|
2020-10-22 10:57:42 +11:00
|
|
|
#[derive(Debug)]
|
2020-10-16 17:04:03 +11:00
|
|
|
enum SwapFailed {
|
|
|
|
BeforeBtcLock,
|
|
|
|
AfterXmrLock(Reason),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reason why the swap has failed.
|
2020-10-22 10:57:42 +11:00
|
|
|
#[derive(Debug)]
|
2020-10-16 17:04:03 +11:00
|
|
|
enum Reason {
|
|
|
|
/// The refund timelock has been reached.
|
|
|
|
BtcExpired,
|
|
|
|
}
|
|
|
|
|
|
|
|
enum RefundFailed {
|
|
|
|
BtcPunishable {
|
|
|
|
tx_cancel_was_published: bool,
|
|
|
|
},
|
|
|
|
/// Could not find Alice's signature on the refund transaction witness
|
|
|
|
/// stack.
|
|
|
|
BtcRefundSignature,
|
|
|
|
/// Could not recover secret `s_b` from Alice's refund transaction
|
|
|
|
/// signature.
|
|
|
|
SecretRecovery,
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn poll_until(condition_future: impl Future<Output = bool> + Clone) {
|
|
|
|
loop {
|
|
|
|
if condition_future.clone().await {
|
|
|
|
return;
|
|
|
|
}
|
2020-10-22 10:57:42 +11:00
|
|
|
|
|
|
|
tokio::time::delay_for(std::time::Duration::from_secs(1)).await;
|
2020-10-16 17:04:03 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-22 10:57:42 +11:00
|
|
|
async fn bitcoin_block_height_is_gte<B>(bitcoin_client: &B, n_blocks: u32) -> bool
|
2020-10-16 17:04:03 +11:00
|
|
|
where
|
2020-10-22 10:57:42 +11:00
|
|
|
B: BlockHeight,
|
2020-10-16 17:04:03 +11:00
|
|
|
{
|
2020-10-22 10:57:42 +11:00
|
|
|
bitcoin_client.block_height().await >= n_blocks
|
2020-10-16 17:04:03 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
Gen::new_boxed(|co| async move {
|
|
|
|
let swap_result: Result<(), SwapFailed> = async {
|
2020-10-22 10:57:42 +11:00
|
|
|
timeout(
|
|
|
|
Duration::from_secs(SECS_TO_ACT_BOB),
|
2020-10-16 17:04:03 +11:00
|
|
|
bitcoin_client.watch_for_raw_transaction(tx_lock.txid()),
|
|
|
|
)
|
|
|
|
.await
|
2020-10-22 10:57:42 +11:00
|
|
|
.map_err(|_| SwapFailed::BeforeBtcLock)?;
|
|
|
|
|
|
|
|
let tx_lock_height = bitcoin_client
|
|
|
|
.transaction_block_height(tx_lock.txid())
|
|
|
|
.await;
|
|
|
|
let btc_has_expired = bitcoin_block_height_is_gte(
|
|
|
|
bitcoin_client.as_ref(),
|
|
|
|
tx_lock_height + refund_timelock,
|
|
|
|
)
|
|
|
|
.shared();
|
|
|
|
let poll_until_btc_has_expired = poll_until(btc_has_expired).shared();
|
|
|
|
futures::pin_mut!(poll_until_btc_has_expired);
|
2020-10-16 17:04:03 +11:00
|
|
|
|
|
|
|
let S_a = monero::PublicKey::from_private_key(&monero::PrivateKey {
|
|
|
|
scalar: s_a.into_ed25519(),
|
|
|
|
});
|
|
|
|
|
|
|
|
co.yield_(AliceAction::LockXmr {
|
|
|
|
amount: xmr,
|
|
|
|
public_spend_key: S_a + S_b_monero,
|
|
|
|
public_view_key: v.public(),
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
|
|
|
|
// TODO: Watch for LockXmr using watch-only wallet. Doing so will prevent Alice
|
|
|
|
// from cancelling/refunding unnecessarily.
|
|
|
|
|
|
|
|
let tx_redeem_encsig = match select(
|
|
|
|
network.receive_bitcoin_redeem_encsig(),
|
|
|
|
poll_until_btc_has_expired.clone(),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
Either::Left((encsig, _)) => encsig,
|
|
|
|
Either::Right(_) => return Err(SwapFailed::AfterXmrLock(Reason::BtcExpired)),
|
|
|
|
};
|
|
|
|
|
|
|
|
let (signed_tx_redeem, tx_redeem_txid) = {
|
|
|
|
let adaptor = Adaptor::<Sha256, Deterministic<Sha256>>::default();
|
|
|
|
|
|
|
|
let tx_redeem = bitcoin::TxRedeem::new(&tx_lock, &redeem_address);
|
|
|
|
|
|
|
|
let sig_a = a.sign(tx_redeem.digest());
|
|
|
|
let sig_b =
|
|
|
|
adaptor.decrypt_signature(&s_a.into_secp256k1(), tx_redeem_encsig.clone());
|
|
|
|
|
|
|
|
let tx = tx_redeem
|
|
|
|
.add_signatures(&tx_lock, (a.public(), sig_a), (B.clone(), sig_b))
|
|
|
|
.expect("sig_{a,b} to be valid signatures for tx_redeem");
|
|
|
|
let txid = tx.txid();
|
|
|
|
|
|
|
|
(tx, txid)
|
|
|
|
};
|
|
|
|
|
|
|
|
co.yield_(AliceAction::RedeemBtc(signed_tx_redeem)).await;
|
|
|
|
|
|
|
|
match select(
|
|
|
|
bitcoin_client.watch_for_raw_transaction(tx_redeem_txid),
|
|
|
|
poll_until_btc_has_expired,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
Either::Left(_) => {}
|
|
|
|
Either::Right(_) => return Err(SwapFailed::AfterXmrLock(Reason::BtcExpired)),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
.await;
|
|
|
|
|
|
|
|
if let Err(SwapFailed::AfterXmrLock(Reason::BtcExpired)) = swap_result {
|
|
|
|
let refund_result: Result<(), RefundFailed> = async {
|
|
|
|
let bob_can_be_punished =
|
2020-10-22 10:57:42 +11:00
|
|
|
bitcoin_block_height_is_gte(bitcoin_client.as_ref(), punish_timelock).shared();
|
2020-10-16 17:04:03 +11:00
|
|
|
let poll_until_bob_can_be_punished = poll_until(bob_can_be_punished).shared();
|
|
|
|
futures::pin_mut!(poll_until_bob_can_be_punished);
|
|
|
|
|
|
|
|
let tx_cancel =
|
|
|
|
bitcoin::TxCancel::new(&tx_lock, refund_timelock, a.public(), B.clone());
|
|
|
|
match select(
|
|
|
|
bitcoin_client.watch_for_raw_transaction(tx_cancel.txid()),
|
|
|
|
poll_until_bob_can_be_punished.clone(),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
Either::Left(_) => {}
|
|
|
|
Either::Right(_) => {
|
|
|
|
return Err(RefundFailed::BtcPunishable {
|
|
|
|
tx_cancel_was_published: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let tx_refund = bitcoin::TxRefund::new(&tx_cancel, &refund_address);
|
|
|
|
let tx_refund_published = match select(
|
|
|
|
bitcoin_client.watch_for_raw_transaction(tx_refund.txid()),
|
|
|
|
poll_until_bob_can_be_punished,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
Either::Left((tx, _)) => tx,
|
|
|
|
Either::Right(_) => {
|
|
|
|
return Err(RefundFailed::BtcPunishable {
|
|
|
|
tx_cancel_was_published: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let s_a = monero::PrivateKey {
|
|
|
|
scalar: s_a.into_ed25519(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let tx_refund_sig = tx_refund
|
|
|
|
.extract_signature_by_key(tx_refund_published, B.clone())
|
|
|
|
.map_err(|_| RefundFailed::BtcRefundSignature)?;
|
|
|
|
let tx_refund_encsig = a.encsign(S_b_bitcoin.clone(), tx_refund.digest());
|
|
|
|
|
|
|
|
let s_b = bitcoin::recover(S_b_bitcoin, tx_refund_sig, tx_refund_encsig)
|
|
|
|
.map_err(|_| RefundFailed::SecretRecovery)?;
|
|
|
|
let s_b = monero::PrivateKey::from_scalar(monero::Scalar::from_bytes_mod_order(
|
|
|
|
s_b.to_bytes(),
|
|
|
|
));
|
|
|
|
|
|
|
|
co.yield_(AliceAction::CreateMoneroWalletForOutput {
|
|
|
|
spend_key: s_a + s_b,
|
|
|
|
view_key: v,
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
.await;
|
|
|
|
|
|
|
|
// LIMITATION: When approaching the punish scenario, Bob could theoretically
|
|
|
|
// wake up in between Alice's publication of tx cancel and beat Alice's punish
|
|
|
|
// transaction with his refund transaction. Alice would then need to carry on
|
|
|
|
// with the refund on Monero. Doing so may be too verbose with the current,
|
|
|
|
// linear approach. A different design may be required
|
|
|
|
if let Err(RefundFailed::BtcPunishable {
|
|
|
|
tx_cancel_was_published,
|
|
|
|
}) = refund_result
|
|
|
|
{
|
|
|
|
let tx_cancel =
|
|
|
|
bitcoin::TxCancel::new(&tx_lock, refund_timelock, a.public(), B.clone());
|
|
|
|
|
|
|
|
if !tx_cancel_was_published {
|
|
|
|
let tx_cancel_txid = tx_cancel.txid();
|
|
|
|
let signed_tx_cancel = {
|
|
|
|
let sig_a = a.sign(tx_cancel.digest());
|
|
|
|
let sig_b = tx_cancel_sig_bob;
|
|
|
|
|
|
|
|
tx_cancel
|
|
|
|
.clone()
|
|
|
|
.add_signatures(&tx_lock, (a.public(), sig_a), (B.clone(), sig_b))
|
|
|
|
.expect("sig_{a,b} to be valid signatures for tx_cancel")
|
|
|
|
};
|
|
|
|
|
|
|
|
co.yield_(AliceAction::CancelBtc(signed_tx_cancel)).await;
|
|
|
|
|
|
|
|
let _ = bitcoin_client
|
|
|
|
.watch_for_raw_transaction(tx_cancel_txid)
|
|
|
|
.await;
|
|
|
|
}
|
|
|
|
|
|
|
|
let tx_punish =
|
|
|
|
bitcoin::TxPunish::new(&tx_cancel, &punish_address, punish_timelock);
|
|
|
|
let tx_punish_txid = tx_punish.txid();
|
|
|
|
let signed_tx_punish = {
|
|
|
|
let sig_a = a.sign(tx_punish.digest());
|
|
|
|
let sig_b = tx_punish_sig_bob;
|
|
|
|
|
|
|
|
tx_punish
|
|
|
|
.add_signatures(&tx_cancel, (a.public(), sig_a), (B, sig_b))
|
|
|
|
.expect("sig_{a,b} to be valid signatures for tx_cancel")
|
|
|
|
};
|
|
|
|
|
|
|
|
co.yield_(AliceAction::PunishBtc(signed_tx_punish)).await;
|
|
|
|
|
|
|
|
let _ = bitcoin_client
|
|
|
|
.watch_for_raw_transaction(tx_punish_txid)
|
|
|
|
.await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|