2020-09-29 01:36:50 -04:00
|
|
|
use crate::{
|
|
|
|
bitcoin,
|
2020-10-23 08:05:34 -04:00
|
|
|
bitcoin::{poll_until_block_height_is_gte, BroadcastSignedTransaction, WatchForRawTransaction},
|
2020-09-29 01:36:50 -04:00
|
|
|
bob, monero,
|
2020-10-07 20:27:54 -04:00
|
|
|
monero::{CreateWalletForOutput, Transfer},
|
2020-10-08 18:46:24 -04:00
|
|
|
transport::{ReceiveMessage, SendMessage},
|
2020-12-11 00:49:19 -05:00
|
|
|
Epoch,
|
2020-09-29 01:36:50 -04:00
|
|
|
};
|
2020-10-07 20:07:37 -04:00
|
|
|
use anyhow::{anyhow, Result};
|
2020-10-23 08:05:34 -04:00
|
|
|
use async_trait::async_trait;
|
2020-10-07 20:07:37 -04:00
|
|
|
use ecdsa_fun::{
|
|
|
|
adaptor::{Adaptor, EncryptedSignature},
|
|
|
|
nonce::Deterministic,
|
|
|
|
};
|
2020-10-23 08:05:34 -04:00
|
|
|
use futures::{
|
|
|
|
future::{select, Either},
|
2020-10-25 20:36:52 -04:00
|
|
|
pin_mut, FutureExt,
|
2020-10-23 08:05:34 -04:00
|
|
|
};
|
|
|
|
use genawaiter::sync::{Gen, GenBoxed};
|
2020-10-07 20:07:37 -04:00
|
|
|
use rand::{CryptoRng, RngCore};
|
2020-10-13 18:32:25 -04:00
|
|
|
use serde::{Deserialize, Serialize};
|
2020-09-28 02:18:50 -04:00
|
|
|
use sha2::Sha256;
|
2020-10-23 08:05:34 -04:00
|
|
|
use std::{
|
|
|
|
convert::{TryFrom, TryInto},
|
|
|
|
sync::Arc,
|
|
|
|
time::Duration,
|
|
|
|
};
|
2020-10-26 21:11:03 -04:00
|
|
|
use tokio::{sync::Mutex, time::timeout};
|
2020-10-27 20:46:45 -04:00
|
|
|
use tracing::{error, info};
|
2020-09-29 01:36:50 -04:00
|
|
|
pub mod message;
|
2020-12-22 01:08:17 -05:00
|
|
|
use crate::bitcoin::{current_epoch, wait_for_t1, BlockHeight, TransactionBlockHeight};
|
2020-10-08 19:17:36 -04:00
|
|
|
pub use message::{Message, Message0, Message1, Message2};
|
2020-09-29 01:36:50 -04:00
|
|
|
|
2020-10-23 08:05:34 -04:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Action {
|
|
|
|
// 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-25 21:03:14 -04:00
|
|
|
///
|
|
|
|
/// The argument `bitcoin_tx_lock_timeout` is used to determine how long we will
|
|
|
|
/// wait for Bob, the counterparty, to lock up the bitcoin.
|
2020-10-25 19:59:28 -04:00
|
|
|
pub fn action_generator<N, B>(
|
2020-10-26 21:11:03 -04:00
|
|
|
network: Arc<Mutex<N>>,
|
2020-10-23 08:05:34 -04:00
|
|
|
bitcoin_client: Arc<B>,
|
|
|
|
// TODO: Replace this with a new, slimmer struct?
|
|
|
|
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,
|
|
|
|
..
|
|
|
|
}: State3,
|
2020-10-25 21:03:14 -04:00
|
|
|
bitcoin_tx_lock_timeout: u64,
|
2020-10-23 08:05:34 -04:00
|
|
|
) -> GenBoxed<Action, (), ()>
|
|
|
|
where
|
2020-10-26 21:11:03 -04:00
|
|
|
N: ReceiveBitcoinRedeemEncsig + Send + 'static,
|
2020-10-23 08:05:34 -04:00
|
|
|
B: bitcoin::BlockHeight
|
|
|
|
+ bitcoin::TransactionBlockHeight
|
|
|
|
+ bitcoin::WatchForRawTransaction
|
|
|
|
+ Send
|
|
|
|
+ Sync
|
|
|
|
+ 'static,
|
|
|
|
{
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum SwapFailed {
|
2020-10-25 20:33:51 -04:00
|
|
|
BeforeBtcLock(Reason),
|
2020-11-03 01:08:31 -05:00
|
|
|
AfterXmrLock(Reason),
|
2020-10-23 08:05:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Reason why the swap has failed.
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum Reason {
|
2020-10-25 20:33:51 -04:00
|
|
|
/// Bob was too slow to lock the bitcoin.
|
|
|
|
InactiveBob,
|
2020-10-25 20:27:35 -04:00
|
|
|
/// Bob's encrypted signature on the Bitcoin redeem transaction is
|
|
|
|
/// invalid.
|
|
|
|
InvalidEncryptedSignature,
|
2020-10-23 08:05:34 -04:00
|
|
|
/// The refund timelock has been reached.
|
|
|
|
BtcExpired,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum RefundFailed {
|
2020-11-03 01:08:31 -05:00
|
|
|
BtcPunishable,
|
2020-10-23 08:05:34 -04:00
|
|
|
/// 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,
|
|
|
|
}
|
|
|
|
|
|
|
|
Gen::new_boxed(|co| async move {
|
|
|
|
let swap_result: Result<(), SwapFailed> = async {
|
|
|
|
timeout(
|
2020-10-25 21:03:14 -04:00
|
|
|
Duration::from_secs(bitcoin_tx_lock_timeout),
|
2020-10-23 08:05:34 -04:00
|
|
|
bitcoin_client.watch_for_raw_transaction(tx_lock.txid()),
|
|
|
|
)
|
|
|
|
.await
|
2020-10-25 20:33:51 -04:00
|
|
|
.map_err(|_| SwapFailed::BeforeBtcLock(Reason::InactiveBob))?;
|
2020-10-23 08:05:34 -04:00
|
|
|
|
|
|
|
let tx_lock_height = bitcoin_client
|
|
|
|
.transaction_block_height(tx_lock.txid())
|
|
|
|
.await;
|
|
|
|
let poll_until_btc_has_expired = poll_until_block_height_is_gte(
|
|
|
|
bitcoin_client.as_ref(),
|
|
|
|
tx_lock_height + refund_timelock,
|
|
|
|
)
|
|
|
|
.shared();
|
2020-10-25 20:36:52 -04:00
|
|
|
pin_mut!(poll_until_btc_has_expired);
|
2020-10-23 08:05:34 -04:00
|
|
|
|
|
|
|
let S_a = monero::PublicKey::from_private_key(&monero::PrivateKey {
|
|
|
|
scalar: s_a.into_ed25519(),
|
|
|
|
});
|
|
|
|
|
|
|
|
co.yield_(Action::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.
|
|
|
|
|
2020-10-26 21:11:03 -04:00
|
|
|
let tx_redeem_encsig = {
|
|
|
|
let mut guard = network.as_ref().lock().await;
|
|
|
|
let tx_redeem_encsig = match select(
|
|
|
|
guard.receive_bitcoin_redeem_encsig(),
|
|
|
|
poll_until_btc_has_expired.clone(),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
Either::Left((encsig, _)) => encsig,
|
2020-11-03 01:08:31 -05:00
|
|
|
Either::Right(_) => return Err(SwapFailed::AfterXmrLock(Reason::BtcExpired)),
|
2020-10-26 21:11:03 -04:00
|
|
|
};
|
|
|
|
|
2020-10-29 18:26:52 -04:00
|
|
|
tracing::debug!("select returned redeem encsig from message");
|
|
|
|
|
2020-10-26 21:11:03 -04:00
|
|
|
tx_redeem_encsig
|
2020-10-23 08:05:34 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
let (signed_tx_redeem, tx_redeem_txid) = {
|
|
|
|
let adaptor = Adaptor::<Sha256, Deterministic<Sha256>>::default();
|
|
|
|
|
|
|
|
let tx_redeem = bitcoin::TxRedeem::new(&tx_lock, &redeem_address);
|
|
|
|
|
2020-10-25 20:27:35 -04:00
|
|
|
bitcoin::verify_encsig(
|
2020-12-03 01:28:23 -05:00
|
|
|
B,
|
2020-10-25 20:27:35 -04:00
|
|
|
s_a.into_secp256k1().into(),
|
|
|
|
&tx_redeem.digest(),
|
|
|
|
&tx_redeem_encsig,
|
|
|
|
)
|
2020-11-03 01:08:31 -05:00
|
|
|
.map_err(|_| SwapFailed::AfterXmrLock(Reason::InvalidEncryptedSignature))?;
|
2020-10-25 20:27:35 -04:00
|
|
|
|
2020-10-23 08:05:34 -04:00
|
|
|
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
|
2020-12-03 01:28:23 -05:00
|
|
|
.add_signatures(&tx_lock, (a.public(), sig_a), (B, sig_b))
|
2020-10-23 08:05:34 -04:00
|
|
|
.expect("sig_{a,b} to be valid signatures for tx_redeem");
|
|
|
|
let txid = tx.txid();
|
|
|
|
|
|
|
|
(tx, txid)
|
|
|
|
};
|
|
|
|
|
|
|
|
co.yield_(Action::RedeemBtc(signed_tx_redeem)).await;
|
|
|
|
|
|
|
|
match select(
|
|
|
|
bitcoin_client.watch_for_raw_transaction(tx_redeem_txid),
|
|
|
|
poll_until_btc_has_expired,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
Either::Left(_) => {}
|
2020-11-03 01:08:31 -05:00
|
|
|
Either::Right(_) => return Err(SwapFailed::AfterXmrLock(Reason::BtcExpired)),
|
2020-10-23 08:05:34 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
.await;
|
|
|
|
|
|
|
|
if let Err(ref err) = swap_result {
|
|
|
|
error!("swap failed: {:?}", err);
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:31 -05:00
|
|
|
if let Err(SwapFailed::AfterXmrLock(Reason::BtcExpired)) = swap_result {
|
2020-10-23 08:05:34 -04:00
|
|
|
let refund_result: Result<(), RefundFailed> = async {
|
2020-12-03 01:28:23 -05:00
|
|
|
let tx_cancel = bitcoin::TxCancel::new(&tx_lock, refund_timelock, a.public(), B);
|
2020-10-23 08:05:34 -04:00
|
|
|
let signed_tx_cancel = {
|
|
|
|
let sig_a = a.sign(tx_cancel.digest());
|
|
|
|
let sig_b = tx_cancel_sig_bob.clone();
|
|
|
|
|
|
|
|
tx_cancel
|
|
|
|
.clone()
|
2020-12-03 01:28:23 -05:00
|
|
|
.add_signatures(&tx_lock, (a.public(), sig_a), (B, sig_b))
|
2020-10-23 08:05:34 -04:00
|
|
|
.expect("sig_{a,b} to be valid signatures for tx_cancel")
|
|
|
|
};
|
|
|
|
|
|
|
|
co.yield_(Action::CancelBtc(signed_tx_cancel)).await;
|
|
|
|
|
2020-11-03 01:08:31 -05:00
|
|
|
bitcoin_client
|
|
|
|
.watch_for_raw_transaction(tx_cancel.txid())
|
|
|
|
.await;
|
|
|
|
|
|
|
|
let tx_cancel_height = bitcoin_client
|
|
|
|
.transaction_block_height(tx_cancel.txid())
|
|
|
|
.await;
|
|
|
|
let poll_until_bob_can_be_punished = poll_until_block_height_is_gte(
|
|
|
|
bitcoin_client.as_ref(),
|
|
|
|
tx_cancel_height + punish_timelock,
|
2020-10-23 08:05:34 -04:00
|
|
|
)
|
2020-11-03 01:08:31 -05:00
|
|
|
.shared();
|
|
|
|
pin_mut!(poll_until_bob_can_be_punished);
|
2020-10-23 08:05:34 -04:00
|
|
|
|
|
|
|
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,
|
2020-11-03 01:08:31 -05:00
|
|
|
Either::Right(_) => return Err(RefundFailed::BtcPunishable),
|
2020-10-23 08:05:34 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
let s_a = monero::PrivateKey {
|
|
|
|
scalar: s_a.into_ed25519(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let tx_refund_sig = tx_refund
|
|
|
|
.extract_signature_by_key(tx_refund_published, a.public())
|
|
|
|
.map_err(|_| RefundFailed::BtcRefundSignature)?;
|
2020-12-03 01:28:23 -05:00
|
|
|
let tx_refund_encsig = a.encsign(S_b_bitcoin, tx_refund.digest());
|
2020-10-23 08:05:34 -04:00
|
|
|
|
|
|
|
let s_b = bitcoin::recover(S_b_bitcoin, tx_refund_sig, tx_refund_encsig)
|
|
|
|
.map_err(|_| RefundFailed::SecretRecovery)?;
|
2020-11-08 19:07:06 -05:00
|
|
|
let s_b = monero::private_key_from_secp256k1_scalar(s_b.into());
|
2020-10-23 08:05:34 -04:00
|
|
|
|
|
|
|
co.yield_(Action::CreateMoneroWalletForOutput {
|
|
|
|
spend_key: s_a + s_b,
|
|
|
|
view_key: v,
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
.await;
|
|
|
|
|
|
|
|
if let Err(ref err) = refund_result {
|
|
|
|
error!("refund failed: {:?}", err);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2020-11-03 01:08:31 -05:00
|
|
|
if let Err(RefundFailed::BtcPunishable) = refund_result {
|
2020-12-03 01:28:23 -05:00
|
|
|
let tx_cancel = bitcoin::TxCancel::new(&tx_lock, refund_timelock, a.public(), B);
|
2020-10-23 08:05:34 -04:00
|
|
|
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_(Action::PunishBtc(signed_tx_punish)).await;
|
|
|
|
|
|
|
|
let _ = bitcoin_client
|
|
|
|
.watch_for_raw_transaction(tx_punish_txid)
|
|
|
|
.await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-11 17:53:21 -04:00
|
|
|
// There are no guarantees that send_message and receive_massage do not block
|
|
|
|
// the flow of execution. Therefore they must be paired between Alice/Bob, one
|
|
|
|
// send to one receive in the correct order.
|
2020-09-29 01:36:50 -04:00
|
|
|
pub async fn next_state<
|
|
|
|
R: RngCore + CryptoRng,
|
2020-10-07 19:53:30 -04:00
|
|
|
B: WatchForRawTransaction + BroadcastSignedTransaction,
|
2020-10-07 20:27:54 -04:00
|
|
|
M: CreateWalletForOutput + Transfer,
|
2020-10-08 18:46:24 -04:00
|
|
|
T: SendMessage<Message> + ReceiveMessage<bob::Message>,
|
2020-09-29 01:36:50 -04:00
|
|
|
>(
|
|
|
|
bitcoin_wallet: &B,
|
|
|
|
monero_wallet: &M,
|
|
|
|
transport: &mut T,
|
|
|
|
state: State,
|
|
|
|
rng: &mut R,
|
|
|
|
) -> Result<State> {
|
|
|
|
match state {
|
|
|
|
State::State0(state0) => {
|
2020-10-11 17:53:21 -04:00
|
|
|
let alice_message0 = state0.next_message(rng).into();
|
2020-09-29 01:36:50 -04:00
|
|
|
|
2020-10-08 17:48:00 -04:00
|
|
|
let bob_message0 = transport.receive_message().await?.try_into()?;
|
2020-10-11 17:53:21 -04:00
|
|
|
transport.send_message(alice_message0).await?;
|
|
|
|
|
2020-09-29 01:36:50 -04:00
|
|
|
let state1 = state0.receive(bob_message0)?;
|
|
|
|
Ok(state1.into())
|
|
|
|
}
|
|
|
|
State::State1(state1) => {
|
2020-10-08 17:48:00 -04:00
|
|
|
let bob_message1 = transport.receive_message().await?.try_into()?;
|
2020-09-29 01:36:50 -04:00
|
|
|
let state2 = state1.receive(bob_message1);
|
2020-10-08 17:48:00 -04:00
|
|
|
let alice_message1 = state2.next_message();
|
2020-09-29 01:36:50 -04:00
|
|
|
transport.send_message(alice_message1.into()).await?;
|
|
|
|
Ok(state2.into())
|
|
|
|
}
|
|
|
|
State::State2(state2) => {
|
2020-10-08 17:48:00 -04:00
|
|
|
let bob_message2 = transport.receive_message().await?.try_into()?;
|
2020-09-29 01:36:50 -04:00
|
|
|
let state3 = state2.receive(bob_message2)?;
|
|
|
|
Ok(state3.into())
|
|
|
|
}
|
|
|
|
State::State3(state3) => {
|
|
|
|
tracing::info!("alice is watching for locked btc");
|
|
|
|
let state4 = state3.watch_for_lock_btc(bitcoin_wallet).await?;
|
|
|
|
Ok(state4.into())
|
|
|
|
}
|
|
|
|
State::State4(state4) => {
|
|
|
|
let state5 = state4.lock_xmr(monero_wallet).await?;
|
|
|
|
tracing::info!("alice has locked xmr");
|
|
|
|
Ok(state5.into())
|
|
|
|
}
|
|
|
|
State::State5(state5) => {
|
|
|
|
transport.send_message(state5.next_message().into()).await?;
|
|
|
|
// todo: pass in state4b as a parameter somewhere in this call to prevent the
|
|
|
|
// user from waiting for a message that wont be sent
|
2020-10-08 17:48:00 -04:00
|
|
|
let message3 = transport.receive_message().await?.try_into()?;
|
2020-09-29 01:36:50 -04:00
|
|
|
let state6 = state5.receive(message3);
|
|
|
|
tracing::info!("alice has received bob message 3");
|
|
|
|
tracing::info!("alice is redeeming btc");
|
2020-10-08 01:18:34 -04:00
|
|
|
state6.redeem_btc(bitcoin_wallet).await?;
|
2020-09-29 01:36:50 -04:00
|
|
|
Ok(state6.into())
|
|
|
|
}
|
|
|
|
State::State6(state6) => Ok(state6.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-08 01:18:34 -04:00
|
|
|
#[allow(clippy::large_enum_variant)]
|
2020-10-21 22:34:01 -04:00
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
2020-09-29 01:36:50 -04:00
|
|
|
pub enum State {
|
|
|
|
State0(State0),
|
|
|
|
State1(State1),
|
|
|
|
State2(State2),
|
|
|
|
State3(State3),
|
|
|
|
State4(State4),
|
|
|
|
State5(State5),
|
|
|
|
State6(State6),
|
|
|
|
}
|
|
|
|
|
2020-10-08 19:17:36 -04:00
|
|
|
impl_try_from_parent_enum!(State0, State);
|
|
|
|
impl_try_from_parent_enum!(State1, State);
|
|
|
|
impl_try_from_parent_enum!(State2, State);
|
|
|
|
impl_try_from_parent_enum!(State3, State);
|
|
|
|
impl_try_from_parent_enum!(State4, State);
|
|
|
|
impl_try_from_parent_enum!(State5, State);
|
|
|
|
impl_try_from_parent_enum!(State6, State);
|
2020-09-28 02:18:50 -04:00
|
|
|
|
2020-10-08 19:17:36 -04:00
|
|
|
impl_from_child_enum!(State0, State);
|
|
|
|
impl_from_child_enum!(State1, State);
|
|
|
|
impl_from_child_enum!(State2, State);
|
|
|
|
impl_from_child_enum!(State3, State);
|
|
|
|
impl_from_child_enum!(State4, State);
|
|
|
|
impl_from_child_enum!(State5, State);
|
|
|
|
impl_from_child_enum!(State6, State);
|
2020-09-29 01:36:50 -04:00
|
|
|
|
|
|
|
impl State {
|
|
|
|
pub fn new<R: RngCore + CryptoRng>(
|
|
|
|
rng: &mut R,
|
|
|
|
btc: bitcoin::Amount,
|
|
|
|
xmr: monero::Amount,
|
|
|
|
refund_timelock: u32,
|
|
|
|
punish_timelock: u32,
|
|
|
|
redeem_address: bitcoin::Address,
|
|
|
|
punish_address: bitcoin::Address,
|
|
|
|
) -> Self {
|
2020-11-24 22:37:37 -05:00
|
|
|
let a = bitcoin::SecretKey::new_random(rng);
|
|
|
|
let s_a = cross_curve_dleq::Scalar::random(rng);
|
|
|
|
let v_a = monero::PrivateViewKey::new_random(rng);
|
|
|
|
|
2020-09-29 01:36:50 -04:00
|
|
|
Self::State0(State0::new(
|
2020-11-24 22:37:37 -05:00
|
|
|
a,
|
|
|
|
s_a,
|
|
|
|
v_a,
|
2020-09-29 01:36:50 -04:00
|
|
|
btc,
|
|
|
|
xmr,
|
|
|
|
refund_timelock,
|
|
|
|
punish_timelock,
|
|
|
|
redeem_address,
|
|
|
|
punish_address,
|
|
|
|
))
|
|
|
|
}
|
2020-09-28 02:18:50 -04:00
|
|
|
}
|
|
|
|
|
2020-10-21 18:18:57 -04:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
2020-09-28 02:18:50 -04:00
|
|
|
pub struct State0 {
|
2020-12-14 17:11:53 -05:00
|
|
|
pub a: bitcoin::SecretKey,
|
|
|
|
pub s_a: cross_curve_dleq::Scalar,
|
|
|
|
pub v_a: monero::PrivateViewKey,
|
2020-10-22 00:06:05 -04:00
|
|
|
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
|
2020-12-14 01:24:23 -05:00
|
|
|
pub btc: bitcoin::Amount,
|
|
|
|
pub xmr: monero::Amount,
|
|
|
|
pub refund_timelock: u32,
|
|
|
|
pub punish_timelock: u32,
|
|
|
|
pub redeem_address: bitcoin::Address,
|
|
|
|
pub punish_address: bitcoin::Address,
|
2020-09-28 02:18:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl State0 {
|
2020-11-30 23:38:24 -05:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2020-11-24 22:37:37 -05:00
|
|
|
pub fn new(
|
|
|
|
a: bitcoin::SecretKey,
|
|
|
|
s_a: cross_curve_dleq::Scalar,
|
|
|
|
v_a: monero::PrivateViewKey,
|
2020-09-28 02:18:50 -04:00
|
|
|
btc: bitcoin::Amount,
|
|
|
|
xmr: monero::Amount,
|
|
|
|
refund_timelock: u32,
|
|
|
|
punish_timelock: u32,
|
|
|
|
redeem_address: bitcoin::Address,
|
|
|
|
punish_address: bitcoin::Address,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
a,
|
|
|
|
s_a,
|
|
|
|
v_a,
|
|
|
|
redeem_address,
|
|
|
|
punish_address,
|
|
|
|
btc,
|
|
|
|
xmr,
|
|
|
|
refund_timelock,
|
|
|
|
punish_timelock,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn next_message<R: RngCore + CryptoRng>(&self, rng: &mut R) -> Message0 {
|
2020-10-27 20:46:45 -04:00
|
|
|
info!("Producing first message");
|
2020-09-28 02:18:50 -04:00
|
|
|
let dleq_proof_s_a = cross_curve_dleq::Proof::new(rng, &self.s_a);
|
|
|
|
|
|
|
|
Message0 {
|
|
|
|
A: self.a.public(),
|
|
|
|
S_a_monero: monero::PublicKey::from_private_key(&monero::PrivateKey {
|
|
|
|
scalar: self.s_a.into_ed25519(),
|
|
|
|
}),
|
|
|
|
S_a_bitcoin: self.s_a.into_secp256k1().into(),
|
|
|
|
dleq_proof_s_a,
|
|
|
|
v_a: self.v_a,
|
|
|
|
redeem_address: self.redeem_address.clone(),
|
|
|
|
punish_address: self.punish_address.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn receive(self, msg: bob::Message0) -> Result<State1> {
|
|
|
|
msg.dleq_proof_s_b.verify(
|
2020-10-21 00:28:50 -04:00
|
|
|
msg.S_b_bitcoin.clone().into(),
|
2020-09-28 02:18:50 -04:00
|
|
|
msg.S_b_monero
|
|
|
|
.point
|
|
|
|
.decompress()
|
|
|
|
.ok_or_else(|| anyhow!("S_b is not a monero curve point"))?,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let v = self.v_a + msg.v_b;
|
|
|
|
|
|
|
|
Ok(State1 {
|
|
|
|
a: self.a,
|
|
|
|
B: msg.B,
|
|
|
|
s_a: self.s_a,
|
|
|
|
S_b_monero: msg.S_b_monero,
|
|
|
|
S_b_bitcoin: msg.S_b_bitcoin,
|
|
|
|
v,
|
|
|
|
btc: self.btc,
|
|
|
|
xmr: self.xmr,
|
|
|
|
refund_timelock: self.refund_timelock,
|
|
|
|
punish_timelock: self.punish_timelock,
|
|
|
|
refund_address: msg.refund_address,
|
|
|
|
redeem_address: self.redeem_address,
|
|
|
|
punish_address: self.punish_address,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-21 18:18:57 -04:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
2020-09-28 02:18:50 -04:00
|
|
|
pub struct State1 {
|
|
|
|
a: bitcoin::SecretKey,
|
|
|
|
B: bitcoin::PublicKey,
|
|
|
|
s_a: cross_curve_dleq::Scalar,
|
|
|
|
S_b_monero: monero::PublicKey,
|
|
|
|
S_b_bitcoin: bitcoin::PublicKey,
|
|
|
|
v: monero::PrivateViewKey,
|
2020-10-22 00:06:05 -04:00
|
|
|
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
|
2020-09-28 02:18:50 -04:00
|
|
|
btc: bitcoin::Amount,
|
|
|
|
xmr: monero::Amount,
|
|
|
|
refund_timelock: u32,
|
|
|
|
punish_timelock: u32,
|
|
|
|
refund_address: bitcoin::Address,
|
|
|
|
redeem_address: bitcoin::Address,
|
|
|
|
punish_address: bitcoin::Address,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl State1 {
|
|
|
|
pub fn receive(self, msg: bob::Message1) -> State2 {
|
|
|
|
State2 {
|
|
|
|
a: self.a,
|
|
|
|
B: self.B,
|
|
|
|
s_a: self.s_a,
|
|
|
|
S_b_monero: self.S_b_monero,
|
|
|
|
S_b_bitcoin: self.S_b_bitcoin,
|
|
|
|
v: self.v,
|
|
|
|
btc: self.btc,
|
|
|
|
xmr: self.xmr,
|
|
|
|
refund_timelock: self.refund_timelock,
|
|
|
|
punish_timelock: self.punish_timelock,
|
|
|
|
refund_address: self.refund_address,
|
|
|
|
redeem_address: self.redeem_address,
|
|
|
|
punish_address: self.punish_address,
|
|
|
|
tx_lock: msg.tx_lock,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-21 18:18:57 -04:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
2020-09-28 02:18:50 -04:00
|
|
|
pub struct State2 {
|
|
|
|
a: bitcoin::SecretKey,
|
|
|
|
B: bitcoin::PublicKey,
|
|
|
|
s_a: cross_curve_dleq::Scalar,
|
|
|
|
S_b_monero: monero::PublicKey,
|
|
|
|
S_b_bitcoin: bitcoin::PublicKey,
|
|
|
|
v: monero::PrivateViewKey,
|
2020-10-22 00:06:05 -04:00
|
|
|
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
|
2020-09-28 02:18:50 -04:00
|
|
|
btc: bitcoin::Amount,
|
|
|
|
xmr: monero::Amount,
|
|
|
|
refund_timelock: u32,
|
|
|
|
punish_timelock: u32,
|
|
|
|
refund_address: bitcoin::Address,
|
|
|
|
redeem_address: bitcoin::Address,
|
|
|
|
punish_address: bitcoin::Address,
|
|
|
|
tx_lock: bitcoin::TxLock,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl State2 {
|
|
|
|
pub fn next_message(&self) -> Message1 {
|
2020-12-03 01:28:23 -05:00
|
|
|
let tx_cancel =
|
|
|
|
bitcoin::TxCancel::new(&self.tx_lock, self.refund_timelock, self.a.public(), self.B);
|
2020-09-28 02:18:50 -04:00
|
|
|
|
|
|
|
let tx_refund = bitcoin::TxRefund::new(&tx_cancel, &self.refund_address);
|
|
|
|
// Alice encsigns the refund transaction(bitcoin) digest with Bob's monero
|
|
|
|
// pubkey(S_b). The refund transaction spends the output of
|
|
|
|
// tx_lock_bitcoin to Bob's refund address.
|
|
|
|
// recover(encsign(a, S_b, d), sign(a, d), S_b) = s_b where d is a digest, (a,
|
|
|
|
// A) is alice's keypair and (s_b, S_b) is bob's keypair.
|
2020-12-03 01:28:23 -05:00
|
|
|
let tx_refund_encsig = self.a.encsign(self.S_b_bitcoin, tx_refund.digest());
|
2020-09-28 02:18:50 -04:00
|
|
|
|
|
|
|
let tx_cancel_sig = self.a.sign(tx_cancel.digest());
|
|
|
|
Message1 {
|
|
|
|
tx_refund_encsig,
|
|
|
|
tx_cancel_sig,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn receive(self, msg: bob::Message2) -> Result<State3> {
|
2020-12-03 01:28:23 -05:00
|
|
|
let tx_cancel =
|
|
|
|
bitcoin::TxCancel::new(&self.tx_lock, self.refund_timelock, self.a.public(), self.B);
|
2020-09-28 02:18:50 -04:00
|
|
|
bitcoin::verify_sig(&self.B, &tx_cancel.digest(), &msg.tx_cancel_sig)?;
|
|
|
|
let tx_punish =
|
|
|
|
bitcoin::TxPunish::new(&tx_cancel, &self.punish_address, self.punish_timelock);
|
|
|
|
bitcoin::verify_sig(&self.B, &tx_punish.digest(), &msg.tx_punish_sig)?;
|
|
|
|
|
|
|
|
Ok(State3 {
|
|
|
|
a: self.a,
|
|
|
|
B: self.B,
|
|
|
|
s_a: self.s_a,
|
|
|
|
S_b_monero: self.S_b_monero,
|
|
|
|
S_b_bitcoin: self.S_b_bitcoin,
|
|
|
|
v: self.v,
|
2020-11-18 00:27:50 -05:00
|
|
|
// TODO(Franck): Review if these amounts are actually needed
|
2020-09-28 02:18:50 -04:00
|
|
|
btc: self.btc,
|
|
|
|
xmr: self.xmr,
|
|
|
|
refund_timelock: self.refund_timelock,
|
|
|
|
punish_timelock: self.punish_timelock,
|
|
|
|
refund_address: self.refund_address,
|
|
|
|
redeem_address: self.redeem_address,
|
|
|
|
punish_address: self.punish_address,
|
|
|
|
tx_lock: self.tx_lock,
|
|
|
|
tx_punish_sig_bob: msg.tx_punish_sig,
|
|
|
|
tx_cancel_sig_bob: msg.tx_cancel_sig,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 23:26:47 -05:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
|
2020-09-28 02:18:50 -04:00
|
|
|
pub struct State3 {
|
2020-10-16 02:04:03 -04:00
|
|
|
pub a: bitcoin::SecretKey,
|
|
|
|
pub B: bitcoin::PublicKey,
|
|
|
|
pub s_a: cross_curve_dleq::Scalar,
|
|
|
|
pub S_b_monero: monero::PublicKey,
|
|
|
|
pub S_b_bitcoin: bitcoin::PublicKey,
|
|
|
|
pub v: monero::PrivateViewKey,
|
2020-10-22 00:06:05 -04:00
|
|
|
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
|
2020-10-22 04:25:54 -04:00
|
|
|
pub btc: bitcoin::Amount,
|
|
|
|
pub xmr: monero::Amount,
|
|
|
|
pub refund_timelock: u32,
|
|
|
|
pub punish_timelock: u32,
|
|
|
|
pub refund_address: bitcoin::Address,
|
|
|
|
pub redeem_address: bitcoin::Address,
|
|
|
|
pub punish_address: bitcoin::Address,
|
|
|
|
pub tx_lock: bitcoin::TxLock,
|
|
|
|
pub tx_punish_sig_bob: bitcoin::Signature,
|
|
|
|
pub tx_cancel_sig_bob: bitcoin::Signature,
|
2020-09-28 02:18:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl State3 {
|
|
|
|
pub async fn watch_for_lock_btc<W>(self, bitcoin_wallet: &W) -> Result<State4>
|
|
|
|
where
|
2020-10-07 19:53:30 -04:00
|
|
|
W: bitcoin::WatchForRawTransaction,
|
2020-09-28 02:18:50 -04:00
|
|
|
{
|
2020-10-08 17:49:17 -04:00
|
|
|
tracing::info!("watching for lock btc with txid: {}", self.tx_lock.txid());
|
2020-09-29 01:36:50 -04:00
|
|
|
let tx = bitcoin_wallet
|
2020-10-07 19:53:30 -04:00
|
|
|
.watch_for_raw_transaction(self.tx_lock.txid())
|
2020-10-15 06:17:42 -04:00
|
|
|
.await;
|
2020-09-28 02:18:50 -04:00
|
|
|
|
2020-10-08 17:49:17 -04:00
|
|
|
tracing::info!("tx lock seen with txid: {}", tx.txid());
|
2020-09-29 01:36:50 -04:00
|
|
|
|
2020-09-28 02:18:50 -04:00
|
|
|
Ok(State4 {
|
|
|
|
a: self.a,
|
|
|
|
B: self.B,
|
|
|
|
s_a: self.s_a,
|
|
|
|
S_b_monero: self.S_b_monero,
|
|
|
|
S_b_bitcoin: self.S_b_bitcoin,
|
|
|
|
v: self.v,
|
|
|
|
btc: self.btc,
|
|
|
|
xmr: self.xmr,
|
|
|
|
refund_timelock: self.refund_timelock,
|
|
|
|
punish_timelock: self.punish_timelock,
|
|
|
|
refund_address: self.refund_address,
|
|
|
|
redeem_address: self.redeem_address,
|
|
|
|
punish_address: self.punish_address,
|
|
|
|
tx_lock: self.tx_lock,
|
|
|
|
tx_punish_sig_bob: self.tx_punish_sig_bob,
|
|
|
|
tx_cancel_sig_bob: self.tx_cancel_sig_bob,
|
|
|
|
})
|
|
|
|
}
|
2020-12-11 00:49:19 -05:00
|
|
|
|
|
|
|
pub async fn wait_for_t1<W>(&self, bitcoin_wallet: &W) -> Result<()>
|
|
|
|
where
|
|
|
|
W: WatchForRawTransaction + TransactionBlockHeight + BlockHeight,
|
|
|
|
{
|
2020-12-22 01:08:17 -05:00
|
|
|
wait_for_t1(bitcoin_wallet, self.refund_timelock, self.tx_lock.txid()).await
|
2020-12-11 00:49:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn current_epoch<W>(&self, bitcoin_wallet: &W) -> Result<Epoch>
|
|
|
|
where
|
|
|
|
W: WatchForRawTransaction + TransactionBlockHeight + BlockHeight,
|
|
|
|
{
|
2020-12-22 00:45:30 -05:00
|
|
|
current_epoch(
|
2020-12-21 01:33:18 -05:00
|
|
|
bitcoin_wallet,
|
|
|
|
self.refund_timelock,
|
|
|
|
self.punish_timelock,
|
|
|
|
self.tx_lock.txid(),
|
|
|
|
)
|
|
|
|
.await
|
2020-12-11 00:49:19 -05:00
|
|
|
}
|
2020-09-28 02:18:50 -04:00
|
|
|
}
|
|
|
|
|
2020-10-21 18:18:57 -04:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
2020-09-28 02:18:50 -04:00
|
|
|
pub struct State4 {
|
|
|
|
a: bitcoin::SecretKey,
|
|
|
|
B: bitcoin::PublicKey,
|
|
|
|
s_a: cross_curve_dleq::Scalar,
|
|
|
|
S_b_monero: monero::PublicKey,
|
|
|
|
S_b_bitcoin: bitcoin::PublicKey,
|
|
|
|
v: monero::PrivateViewKey,
|
2020-10-22 00:06:05 -04:00
|
|
|
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
|
2020-09-28 02:18:50 -04:00
|
|
|
btc: bitcoin::Amount,
|
|
|
|
xmr: monero::Amount,
|
|
|
|
refund_timelock: u32,
|
|
|
|
punish_timelock: u32,
|
|
|
|
refund_address: bitcoin::Address,
|
|
|
|
redeem_address: bitcoin::Address,
|
|
|
|
punish_address: bitcoin::Address,
|
|
|
|
tx_lock: bitcoin::TxLock,
|
|
|
|
tx_punish_sig_bob: bitcoin::Signature,
|
|
|
|
tx_cancel_sig_bob: bitcoin::Signature,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl State4 {
|
2020-09-29 01:36:50 -04:00
|
|
|
pub async fn lock_xmr<W>(self, monero_wallet: &W) -> Result<State5>
|
2020-09-28 02:18:50 -04:00
|
|
|
where
|
|
|
|
W: monero::Transfer,
|
|
|
|
{
|
|
|
|
let S_a = monero::PublicKey::from_private_key(&monero::PrivateKey {
|
|
|
|
scalar: self.s_a.into_ed25519(),
|
|
|
|
});
|
|
|
|
let S_b = self.S_b_monero;
|
|
|
|
|
|
|
|
let (tx_lock_proof, fee) = monero_wallet
|
|
|
|
.transfer(S_a + S_b, self.v.public(), self.xmr)
|
|
|
|
.await?;
|
|
|
|
|
2020-09-29 01:36:50 -04:00
|
|
|
Ok(State5 {
|
|
|
|
a: self.a,
|
|
|
|
B: self.B,
|
|
|
|
s_a: self.s_a,
|
|
|
|
S_b_monero: self.S_b_monero,
|
|
|
|
S_b_bitcoin: self.S_b_bitcoin,
|
|
|
|
v: self.v,
|
|
|
|
btc: self.btc,
|
|
|
|
xmr: self.xmr,
|
|
|
|
refund_timelock: self.refund_timelock,
|
|
|
|
punish_timelock: self.punish_timelock,
|
|
|
|
refund_address: self.refund_address,
|
|
|
|
redeem_address: self.redeem_address,
|
|
|
|
punish_address: self.punish_address,
|
|
|
|
tx_lock: self.tx_lock,
|
|
|
|
tx_lock_proof,
|
|
|
|
tx_punish_sig_bob: self.tx_punish_sig_bob,
|
|
|
|
tx_cancel_sig_bob: self.tx_cancel_sig_bob,
|
|
|
|
lock_xmr_fee: fee,
|
|
|
|
})
|
2020-09-28 02:18:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn punish<W: bitcoin::BroadcastSignedTransaction>(
|
|
|
|
&self,
|
|
|
|
bitcoin_wallet: &W,
|
|
|
|
) -> Result<()> {
|
2020-12-03 01:28:23 -05:00
|
|
|
let tx_cancel =
|
|
|
|
bitcoin::TxCancel::new(&self.tx_lock, self.refund_timelock, self.a.public(), self.B);
|
2020-09-28 02:18:50 -04:00
|
|
|
let tx_punish =
|
|
|
|
bitcoin::TxPunish::new(&tx_cancel, &self.punish_address, self.punish_timelock);
|
|
|
|
|
|
|
|
{
|
|
|
|
let sig_a = self.a.sign(tx_cancel.digest());
|
|
|
|
let sig_b = self.tx_cancel_sig_bob.clone();
|
|
|
|
|
|
|
|
let signed_tx_cancel = tx_cancel.clone().add_signatures(
|
|
|
|
&self.tx_lock,
|
|
|
|
(self.a.public(), sig_a),
|
2020-12-03 01:28:23 -05:00
|
|
|
(self.B, sig_b),
|
2020-09-28 02:18:50 -04:00
|
|
|
)?;
|
|
|
|
|
|
|
|
let _ = bitcoin_wallet
|
|
|
|
.broadcast_signed_transaction(signed_tx_cancel)
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let sig_a = self.a.sign(tx_punish.digest());
|
|
|
|
let sig_b = self.tx_punish_sig_bob.clone();
|
|
|
|
|
2020-12-03 01:28:23 -05:00
|
|
|
let signed_tx_punish =
|
|
|
|
tx_punish.add_signatures(&tx_cancel, (self.a.public(), sig_a), (self.B, sig_b))?;
|
2020-09-28 02:18:50 -04:00
|
|
|
|
|
|
|
let _ = bitcoin_wallet
|
|
|
|
.broadcast_signed_transaction(signed_tx_punish)
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-21 18:18:57 -04:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
2020-09-29 01:36:50 -04:00
|
|
|
pub struct State5 {
|
2020-09-28 02:18:50 -04:00
|
|
|
a: bitcoin::SecretKey,
|
|
|
|
B: bitcoin::PublicKey,
|
|
|
|
s_a: cross_curve_dleq::Scalar,
|
|
|
|
S_b_monero: monero::PublicKey,
|
|
|
|
S_b_bitcoin: bitcoin::PublicKey,
|
|
|
|
v: monero::PrivateViewKey,
|
2020-10-22 00:06:05 -04:00
|
|
|
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
|
2020-09-28 02:18:50 -04:00
|
|
|
btc: bitcoin::Amount,
|
|
|
|
xmr: monero::Amount,
|
|
|
|
refund_timelock: u32,
|
|
|
|
punish_timelock: u32,
|
|
|
|
refund_address: bitcoin::Address,
|
|
|
|
redeem_address: bitcoin::Address,
|
|
|
|
punish_address: bitcoin::Address,
|
|
|
|
tx_lock: bitcoin::TxLock,
|
|
|
|
tx_lock_proof: monero::TransferProof,
|
2020-10-21 18:52:57 -04:00
|
|
|
|
2020-09-28 02:18:50 -04:00
|
|
|
tx_punish_sig_bob: bitcoin::Signature,
|
2020-10-21 18:52:57 -04:00
|
|
|
|
2020-09-28 02:18:50 -04:00
|
|
|
tx_cancel_sig_bob: bitcoin::Signature,
|
2020-09-29 01:36:50 -04:00
|
|
|
lock_xmr_fee: monero::Amount,
|
2020-09-28 02:18:50 -04:00
|
|
|
}
|
|
|
|
|
2020-09-29 01:36:50 -04:00
|
|
|
impl State5 {
|
2020-09-28 02:18:50 -04:00
|
|
|
pub fn next_message(&self) -> Message2 {
|
|
|
|
Message2 {
|
|
|
|
tx_lock_proof: self.tx_lock_proof.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-29 01:36:50 -04:00
|
|
|
pub fn receive(self, msg: bob::Message3) -> State6 {
|
|
|
|
State6 {
|
2020-09-28 02:18:50 -04:00
|
|
|
a: self.a,
|
|
|
|
B: self.B,
|
|
|
|
s_a: self.s_a,
|
|
|
|
S_b_monero: self.S_b_monero,
|
|
|
|
S_b_bitcoin: self.S_b_bitcoin,
|
|
|
|
v: self.v,
|
|
|
|
btc: self.btc,
|
|
|
|
xmr: self.xmr,
|
|
|
|
refund_timelock: self.refund_timelock,
|
|
|
|
punish_timelock: self.punish_timelock,
|
|
|
|
refund_address: self.refund_address,
|
|
|
|
redeem_address: self.redeem_address,
|
|
|
|
punish_address: self.punish_address,
|
|
|
|
tx_lock: self.tx_lock,
|
|
|
|
tx_punish_sig_bob: self.tx_punish_sig_bob,
|
|
|
|
tx_redeem_encsig: msg.tx_redeem_encsig,
|
2020-09-29 01:36:50 -04:00
|
|
|
lock_xmr_fee: self.lock_xmr_fee,
|
2020-09-28 02:18:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// watch for refund on btc, recover s_b and refund xmr
|
|
|
|
pub async fn refund_xmr<B, M>(self, bitcoin_wallet: &B, monero_wallet: &M) -> Result<()>
|
|
|
|
where
|
2020-10-07 19:53:30 -04:00
|
|
|
B: WatchForRawTransaction,
|
2020-10-07 20:27:54 -04:00
|
|
|
M: CreateWalletForOutput,
|
2020-09-28 02:18:50 -04:00
|
|
|
{
|
2020-12-03 01:28:23 -05:00
|
|
|
let tx_cancel =
|
|
|
|
bitcoin::TxCancel::new(&self.tx_lock, self.refund_timelock, self.a.public(), self.B);
|
2020-09-28 02:18:50 -04:00
|
|
|
|
|
|
|
let tx_refund = bitcoin::TxRefund::new(&tx_cancel, &self.refund_address);
|
|
|
|
|
2020-12-03 01:28:23 -05:00
|
|
|
let tx_refund_encsig = self.a.encsign(self.S_b_bitcoin, tx_refund.digest());
|
2020-09-28 02:18:50 -04:00
|
|
|
|
2020-10-07 19:53:30 -04:00
|
|
|
let tx_refund_candidate = bitcoin_wallet
|
|
|
|
.watch_for_raw_transaction(tx_refund.txid())
|
2020-10-15 06:17:42 -04:00
|
|
|
.await;
|
2020-09-28 02:18:50 -04:00
|
|
|
|
|
|
|
let tx_refund_sig =
|
|
|
|
tx_refund.extract_signature_by_key(tx_refund_candidate, self.a.public())?;
|
|
|
|
|
|
|
|
let s_b = bitcoin::recover(self.S_b_bitcoin, tx_refund_sig, tx_refund_encsig)?;
|
2020-11-08 19:07:06 -05:00
|
|
|
let s_b = monero::private_key_from_secp256k1_scalar(s_b.into());
|
2020-09-28 02:18:50 -04:00
|
|
|
|
|
|
|
let s = s_b.scalar + self.s_a.into_ed25519();
|
|
|
|
|
|
|
|
// NOTE: This actually generates and opens a new wallet, closing the currently
|
|
|
|
// open one.
|
|
|
|
monero_wallet
|
2020-10-07 20:27:54 -04:00
|
|
|
.create_and_load_wallet_for_output(monero::PrivateKey::from_scalar(s), self.v)
|
2020-09-28 02:18:50 -04:00
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-21 18:18:57 -04:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
2020-09-29 01:36:50 -04:00
|
|
|
pub struct State6 {
|
2020-09-28 02:18:50 -04:00
|
|
|
a: bitcoin::SecretKey,
|
|
|
|
B: bitcoin::PublicKey,
|
|
|
|
s_a: cross_curve_dleq::Scalar,
|
|
|
|
S_b_monero: monero::PublicKey,
|
|
|
|
S_b_bitcoin: bitcoin::PublicKey,
|
|
|
|
v: monero::PrivateViewKey,
|
2020-10-22 00:06:05 -04:00
|
|
|
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
|
2020-09-28 02:18:50 -04:00
|
|
|
btc: bitcoin::Amount,
|
|
|
|
xmr: monero::Amount,
|
|
|
|
refund_timelock: u32,
|
|
|
|
punish_timelock: u32,
|
|
|
|
refund_address: bitcoin::Address,
|
|
|
|
redeem_address: bitcoin::Address,
|
|
|
|
punish_address: bitcoin::Address,
|
|
|
|
tx_lock: bitcoin::TxLock,
|
2020-10-21 18:52:57 -04:00
|
|
|
|
2020-09-28 02:18:50 -04:00
|
|
|
tx_punish_sig_bob: bitcoin::Signature,
|
|
|
|
tx_redeem_encsig: EncryptedSignature,
|
2020-09-29 01:36:50 -04:00
|
|
|
lock_xmr_fee: monero::Amount,
|
2020-09-28 02:18:50 -04:00
|
|
|
}
|
|
|
|
|
2020-09-29 01:36:50 -04:00
|
|
|
impl State6 {
|
2020-09-28 02:18:50 -04:00
|
|
|
pub async fn redeem_btc<W: bitcoin::BroadcastSignedTransaction>(
|
|
|
|
&self,
|
|
|
|
bitcoin_wallet: &W,
|
|
|
|
) -> Result<()> {
|
|
|
|
let adaptor = Adaptor::<Sha256, Deterministic<Sha256>>::default();
|
|
|
|
|
|
|
|
let tx_redeem = bitcoin::TxRedeem::new(&self.tx_lock, &self.redeem_address);
|
|
|
|
|
|
|
|
let sig_a = self.a.sign(tx_redeem.digest());
|
|
|
|
let sig_b =
|
|
|
|
adaptor.decrypt_signature(&self.s_a.into_secp256k1(), self.tx_redeem_encsig.clone());
|
|
|
|
|
2020-12-03 01:28:23 -05:00
|
|
|
let sig_tx_redeem =
|
|
|
|
tx_redeem.add_signatures(&self.tx_lock, (self.a.public(), sig_a), (self.B, sig_b))?;
|
2020-09-28 02:18:50 -04:00
|
|
|
bitcoin_wallet
|
|
|
|
.broadcast_signed_transaction(sig_tx_redeem)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-09-29 01:36:50 -04:00
|
|
|
|
|
|
|
pub fn lock_xmr_fee(&self) -> monero::Amount {
|
|
|
|
self.lock_xmr_fee
|
|
|
|
}
|
2020-09-28 02:18:50 -04:00
|
|
|
}
|