2020-11-25 00:16:04 -05:00
|
|
|
use crate::{
|
|
|
|
alice::{amounts, OutEvent, Swarm},
|
2020-11-25 00:27:57 -05:00
|
|
|
bitcoin, monero,
|
2020-11-25 00:16:04 -05:00
|
|
|
network::request_response::AliceToBob,
|
|
|
|
SwapAmounts, PUNISH_TIMELOCK, REFUND_TIMELOCK,
|
|
|
|
};
|
2020-11-25 00:27:57 -05:00
|
|
|
use anyhow::{bail, Context, Result};
|
|
|
|
use ecdsa_fun::{adaptor::Adaptor, nonce::Deterministic};
|
|
|
|
use futures::{
|
|
|
|
future::{select, Either},
|
|
|
|
pin_mut,
|
|
|
|
};
|
2020-11-25 00:16:04 -05:00
|
|
|
use libp2p::request_response::ResponseChannel;
|
2020-11-25 00:27:57 -05:00
|
|
|
use sha2::Sha256;
|
|
|
|
use std::{sync::Arc, time::Duration};
|
|
|
|
use tokio::time::timeout;
|
2020-12-01 18:00:00 -05:00
|
|
|
use tracing::trace;
|
2020-11-25 00:16:04 -05:00
|
|
|
use xmr_btc::{
|
|
|
|
alice,
|
|
|
|
alice::{State0, State3},
|
2020-11-25 00:27:57 -05:00
|
|
|
bitcoin::{
|
|
|
|
poll_until_block_height_is_gte, BlockHeight, BroadcastSignedTransaction,
|
|
|
|
EncryptedSignature, GetRawTransaction, TransactionBlockHeight, TxCancel, TxLock, TxRefund,
|
|
|
|
WaitForTransactionFinality, WatchForRawTransaction,
|
|
|
|
},
|
2020-12-01 18:00:00 -05:00
|
|
|
config::Config,
|
2020-11-25 00:16:04 -05:00
|
|
|
cross_curve_dleq,
|
|
|
|
monero::Transfer,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub async fn negotiate(
|
|
|
|
amounts: SwapAmounts,
|
2020-11-25 00:27:57 -05:00
|
|
|
a: bitcoin::SecretKey,
|
2020-11-25 00:16:04 -05:00
|
|
|
s_a: cross_curve_dleq::Scalar,
|
2020-11-25 00:27:57 -05:00
|
|
|
v_a: monero::PrivateViewKey,
|
2020-11-25 00:16:04 -05:00
|
|
|
swarm: &mut Swarm,
|
2020-11-25 00:27:57 -05:00
|
|
|
bitcoin_wallet: Arc<bitcoin::Wallet>,
|
2020-12-01 18:00:00 -05:00
|
|
|
config: Config,
|
2020-11-25 00:27:57 -05:00
|
|
|
) -> Result<(ResponseChannel<AliceToBob>, State3)> {
|
2020-12-01 18:00:00 -05:00
|
|
|
trace!("Starting negotiate");
|
|
|
|
let event = timeout(config.bob_time_to_act, swarm.next())
|
2020-11-25 00:27:57 -05:00
|
|
|
.await
|
|
|
|
.context("Failed to receive dial connection from Bob")?;
|
|
|
|
match event {
|
2020-11-25 00:16:04 -05:00
|
|
|
OutEvent::ConnectionEstablished(_bob_peer_id) => {}
|
|
|
|
other => bail!("Unexpected event received: {:?}", other),
|
2020-11-25 00:27:57 -05:00
|
|
|
}
|
2020-11-25 00:16:04 -05:00
|
|
|
|
2020-12-01 18:00:00 -05:00
|
|
|
let event = timeout(config.bob_time_to_act, swarm.next())
|
2020-11-25 00:27:57 -05:00
|
|
|
.await
|
|
|
|
.context("Failed to receive amounts from Bob")?;
|
|
|
|
let (btc, channel) = match event {
|
2020-11-25 00:16:04 -05:00
|
|
|
OutEvent::Request(amounts::OutEvent::Btc { btc, channel }) => (btc, channel),
|
|
|
|
other => bail!("Unexpected event received: {:?}", other),
|
|
|
|
};
|
|
|
|
|
|
|
|
if btc != amounts.btc {
|
|
|
|
bail!(
|
|
|
|
"Bob proposed a different amount; got {}, expected: {}",
|
|
|
|
btc,
|
|
|
|
amounts.btc
|
|
|
|
);
|
|
|
|
}
|
2020-11-25 00:27:57 -05:00
|
|
|
// TODO: get an ack from libp2p2
|
2020-11-25 00:16:04 -05:00
|
|
|
swarm.send_amounts(channel, amounts);
|
|
|
|
|
|
|
|
let redeem_address = bitcoin_wallet.as_ref().new_address().await?;
|
|
|
|
let punish_address = redeem_address.clone();
|
|
|
|
|
|
|
|
let state0 = State0::new(
|
|
|
|
a,
|
|
|
|
s_a,
|
|
|
|
v_a,
|
2020-11-30 18:41:10 -05:00
|
|
|
amounts.btc,
|
|
|
|
amounts.xmr,
|
2020-11-25 00:16:04 -05:00
|
|
|
REFUND_TIMELOCK,
|
|
|
|
PUNISH_TIMELOCK,
|
|
|
|
redeem_address,
|
|
|
|
punish_address,
|
|
|
|
);
|
|
|
|
|
2020-11-30 18:41:10 -05:00
|
|
|
// TODO(Franck): Understand why this is needed.
|
|
|
|
swarm.set_state0(state0.clone());
|
|
|
|
|
2020-12-01 18:00:00 -05:00
|
|
|
let event = timeout(config.bob_time_to_act, swarm.next())
|
2020-11-30 18:41:10 -05:00
|
|
|
.await
|
|
|
|
.context("Failed to receive message 0 from Bob")?;
|
|
|
|
let message0 = match event {
|
|
|
|
OutEvent::Message0(msg) => msg,
|
|
|
|
other => bail!("Unexpected event received: {:?}", other),
|
|
|
|
};
|
|
|
|
|
2020-11-25 00:16:04 -05:00
|
|
|
let state1 = state0.receive(message0)?;
|
|
|
|
|
2020-12-01 18:00:00 -05:00
|
|
|
let event = timeout(config.bob_time_to_act, swarm.next())
|
2020-11-25 00:27:57 -05:00
|
|
|
.await
|
|
|
|
.context("Failed to receive message 1 from Bob")?;
|
|
|
|
let (msg, channel) = match event {
|
|
|
|
OutEvent::Message1 { msg, channel } => (msg, channel),
|
2020-11-25 00:16:04 -05:00
|
|
|
other => bail!("Unexpected event: {:?}", other),
|
|
|
|
};
|
|
|
|
|
2020-11-25 00:27:57 -05:00
|
|
|
let state2 = state1.receive(msg);
|
|
|
|
|
2020-11-25 00:16:04 -05:00
|
|
|
let message1 = state2.next_message();
|
|
|
|
swarm.send_message1(channel, message1);
|
|
|
|
|
2020-12-01 18:00:00 -05:00
|
|
|
let event = timeout(config.bob_time_to_act, swarm.next())
|
2020-11-25 00:27:57 -05:00
|
|
|
.await
|
|
|
|
.context("Failed to receive message 2 from Bob")?;
|
|
|
|
let (msg, channel) = match event {
|
|
|
|
OutEvent::Message2 { msg, channel } => (msg, channel),
|
2020-11-25 00:16:04 -05:00
|
|
|
other => bail!("Unexpected event: {:?}", other),
|
|
|
|
};
|
|
|
|
|
2020-11-25 00:27:57 -05:00
|
|
|
let state3 = state2.receive(msg)?;
|
|
|
|
|
|
|
|
Ok((channel, state3))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn wait_for_locked_bitcoin<W>(
|
|
|
|
lock_bitcoin_txid: bitcoin::Txid,
|
|
|
|
bitcoin_wallet: Arc<W>,
|
2020-12-01 18:00:00 -05:00
|
|
|
config: Config,
|
2020-11-25 00:27:57 -05:00
|
|
|
) -> Result<()>
|
|
|
|
where
|
|
|
|
W: WatchForRawTransaction + WaitForTransactionFinality,
|
|
|
|
{
|
|
|
|
// We assume we will see Bob's transaction in the mempool first.
|
|
|
|
timeout(
|
2020-12-01 18:00:00 -05:00
|
|
|
config.bob_time_to_act,
|
2020-11-25 00:27:57 -05:00
|
|
|
bitcoin_wallet.watch_for_raw_transaction(lock_bitcoin_txid),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.context("Failed to find lock Bitcoin tx")?;
|
|
|
|
|
2020-11-30 22:30:02 -05:00
|
|
|
// // We saw the transaction in the mempool, waiting for it to be confirmed.
|
|
|
|
// bitcoin_wallet
|
2020-12-01 18:00:00 -05:00
|
|
|
// .wait_for_transaction_finality(lock_bitcoin_txid, config)
|
2020-11-30 22:30:02 -05:00
|
|
|
// .await;
|
2020-11-25 00:27:57 -05:00
|
|
|
|
|
|
|
Ok(())
|
2020-11-25 00:16:04 -05:00
|
|
|
}
|
|
|
|
|
2020-11-25 00:27:57 -05:00
|
|
|
pub async fn lock_xmr<W>(
|
2020-11-25 00:16:04 -05:00
|
|
|
channel: ResponseChannel<AliceToBob>,
|
|
|
|
amounts: SwapAmounts,
|
|
|
|
state3: State3,
|
|
|
|
swarm: &mut Swarm,
|
2020-11-25 00:27:57 -05:00
|
|
|
monero_wallet: Arc<W>,
|
|
|
|
) -> Result<()>
|
|
|
|
where
|
|
|
|
W: Transfer,
|
|
|
|
{
|
2020-11-25 00:16:04 -05:00
|
|
|
let S_a = monero::PublicKey::from_private_key(&monero::PrivateKey {
|
|
|
|
scalar: state3.s_a.into_ed25519(),
|
|
|
|
});
|
|
|
|
|
|
|
|
let public_spend_key = S_a + state3.S_b_monero;
|
|
|
|
let public_view_key = state3.v.public();
|
|
|
|
|
|
|
|
let (transfer_proof, _) = monero_wallet
|
|
|
|
.transfer(public_spend_key, public_view_key, amounts.xmr)
|
|
|
|
.await?;
|
|
|
|
|
2020-11-25 00:27:57 -05:00
|
|
|
// TODO(Franck): Wait for Monero to be confirmed once
|
|
|
|
|
2020-11-25 00:16:04 -05:00
|
|
|
swarm.send_message2(channel, alice::Message2 {
|
|
|
|
tx_lock_proof: transfer_proof,
|
|
|
|
});
|
|
|
|
|
2020-11-25 00:27:57 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-12-01 20:36:47 -05:00
|
|
|
pub async fn wait_for_bitcoin_encrypted_signature(
|
|
|
|
swarm: &mut Swarm,
|
|
|
|
timeout_duration: Duration,
|
|
|
|
) -> Result<EncryptedSignature> {
|
|
|
|
let event = timeout(timeout_duration, swarm.next())
|
2020-11-25 00:27:57 -05:00
|
|
|
.await
|
|
|
|
.context("Failed to receive Bitcoin encrypted signature from Bob")?;
|
|
|
|
|
|
|
|
match event {
|
|
|
|
OutEvent::Message3(msg) => Ok(msg.tx_redeem_encsig),
|
|
|
|
other => bail!(
|
|
|
|
"Expected Bob's Bitcoin redeem encrypted signature, got: {:?}",
|
|
|
|
other
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn build_bitcoin_redeem_transaction(
|
|
|
|
encrypted_signature: EncryptedSignature,
|
|
|
|
tx_lock: &TxLock,
|
|
|
|
a: bitcoin::SecretKey,
|
|
|
|
s_a: cross_curve_dleq::Scalar,
|
|
|
|
B: bitcoin::PublicKey,
|
|
|
|
redeem_address: &bitcoin::Address,
|
|
|
|
) -> Result<bitcoin::Transaction> {
|
|
|
|
let adaptor = Adaptor::<Sha256, Deterministic<Sha256>>::default();
|
|
|
|
|
|
|
|
let tx_redeem = bitcoin::TxRedeem::new(tx_lock, redeem_address);
|
|
|
|
|
|
|
|
bitcoin::verify_encsig(
|
2020-12-03 01:28:23 -05:00
|
|
|
B,
|
2020-11-25 00:27:57 -05:00
|
|
|
s_a.into_secp256k1().into(),
|
|
|
|
&tx_redeem.digest(),
|
|
|
|
&encrypted_signature,
|
|
|
|
)
|
|
|
|
.context("Invalid encrypted signature received")?;
|
|
|
|
|
|
|
|
let sig_a = a.sign(tx_redeem.digest());
|
2020-11-30 23:38:24 -05:00
|
|
|
let sig_b = adaptor.decrypt_signature(&s_a.into_secp256k1(), encrypted_signature);
|
2020-11-25 00:27:57 -05:00
|
|
|
|
|
|
|
let tx = tx_redeem
|
2020-11-30 23:38:24 -05:00
|
|
|
.add_signatures(&tx_lock, (a.public(), sig_a), (B, sig_b))
|
2020-11-25 00:27:57 -05:00
|
|
|
.context("sig_{a,b} are invalid for tx_redeem")?;
|
|
|
|
|
|
|
|
Ok(tx)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn publish_bitcoin_redeem_transaction<W>(
|
|
|
|
redeem_tx: bitcoin::Transaction,
|
|
|
|
bitcoin_wallet: Arc<W>,
|
2020-12-01 18:00:00 -05:00
|
|
|
config: Config,
|
2020-11-25 00:27:57 -05:00
|
|
|
) -> Result<()>
|
|
|
|
where
|
|
|
|
W: BroadcastSignedTransaction + WaitForTransactionFinality,
|
|
|
|
{
|
2020-12-01 18:00:00 -05:00
|
|
|
let tx_id = bitcoin_wallet
|
2020-11-25 00:27:57 -05:00
|
|
|
.broadcast_signed_transaction(redeem_tx)
|
|
|
|
.await?;
|
2020-11-25 00:16:04 -05:00
|
|
|
|
2020-12-01 18:00:00 -05:00
|
|
|
bitcoin_wallet
|
|
|
|
.wait_for_transaction_finality(tx_id, config)
|
|
|
|
.await
|
2020-11-25 00:16:04 -05:00
|
|
|
}
|
2020-11-25 00:27:57 -05:00
|
|
|
|
|
|
|
pub async fn publish_cancel_transaction<W>(
|
|
|
|
tx_lock: TxLock,
|
|
|
|
a: bitcoin::SecretKey,
|
|
|
|
B: bitcoin::PublicKey,
|
|
|
|
refund_timelock: u32,
|
|
|
|
tx_cancel_sig_bob: bitcoin::Signature,
|
|
|
|
bitcoin_wallet: Arc<W>,
|
|
|
|
) -> Result<bitcoin::TxCancel>
|
|
|
|
where
|
|
|
|
W: GetRawTransaction + TransactionBlockHeight + BlockHeight + BroadcastSignedTransaction,
|
|
|
|
{
|
|
|
|
// First wait for t1 to expire
|
|
|
|
let tx_lock_height = bitcoin_wallet
|
|
|
|
.transaction_block_height(tx_lock.txid())
|
|
|
|
.await;
|
|
|
|
poll_until_block_height_is_gte(bitcoin_wallet.as_ref(), tx_lock_height + refund_timelock).await;
|
|
|
|
|
2020-12-03 01:28:23 -05:00
|
|
|
let tx_cancel = bitcoin::TxCancel::new(&tx_lock, refund_timelock, a.public(), B);
|
2020-11-25 00:27:57 -05:00
|
|
|
|
|
|
|
// If Bob hasn't yet broadcasted the tx cancel, we do it
|
2020-11-30 23:38:24 -05:00
|
|
|
if bitcoin_wallet
|
|
|
|
.get_raw_transaction(tx_cancel.txid())
|
|
|
|
.await
|
|
|
|
.is_err()
|
|
|
|
{
|
2020-11-25 00:27:57 -05:00
|
|
|
// TODO(Franck): Maybe the cancel transaction is already mined, in this case,
|
|
|
|
// the broadcast will error out.
|
|
|
|
|
|
|
|
let sig_a = a.sign(tx_cancel.digest());
|
|
|
|
let sig_b = tx_cancel_sig_bob.clone();
|
|
|
|
|
|
|
|
let tx_cancel = tx_cancel
|
|
|
|
.clone()
|
2020-12-03 01:28:23 -05:00
|
|
|
.add_signatures(&tx_lock, (a.public(), sig_a), (B, sig_b))
|
2020-11-25 00:27:57 -05:00
|
|
|
.expect("sig_{a,b} to be valid signatures for tx_cancel");
|
|
|
|
|
|
|
|
// TODO(Franck): Error handling is delicate, why can't we broadcast?
|
|
|
|
bitcoin_wallet
|
|
|
|
.broadcast_signed_transaction(tx_cancel)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
// TODO(Franck): Wait until transaction is mined and returned mined
|
|
|
|
// block height
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(tx_cancel)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn wait_for_bitcoin_refund<W>(
|
|
|
|
tx_cancel: &TxCancel,
|
|
|
|
cancel_tx_height: u32,
|
|
|
|
punish_timelock: u32,
|
|
|
|
refund_address: &bitcoin::Address,
|
|
|
|
bitcoin_wallet: Arc<W>,
|
|
|
|
) -> Result<(bitcoin::TxRefund, Option<bitcoin::Transaction>)>
|
|
|
|
where
|
|
|
|
W: BlockHeight + WatchForRawTransaction,
|
|
|
|
{
|
|
|
|
let punish_timelock_expired =
|
|
|
|
poll_until_block_height_is_gte(bitcoin_wallet.as_ref(), cancel_tx_height + punish_timelock);
|
|
|
|
|
|
|
|
let tx_refund = bitcoin::TxRefund::new(tx_cancel, refund_address);
|
|
|
|
|
|
|
|
// TODO(Franck): This only checks the mempool, need to cater for the case where
|
|
|
|
// the transaction goes directly in a block
|
|
|
|
let seen_refund_tx = bitcoin_wallet.watch_for_raw_transaction(tx_refund.txid());
|
|
|
|
|
|
|
|
pin_mut!(punish_timelock_expired);
|
|
|
|
pin_mut!(seen_refund_tx);
|
|
|
|
|
|
|
|
match select(punish_timelock_expired, seen_refund_tx).await {
|
|
|
|
Either::Left(_) => Ok((tx_refund, None)),
|
|
|
|
Either::Right((published_refund_tx, _)) => Ok((tx_refund, Some(published_refund_tx))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn extract_monero_private_key(
|
|
|
|
published_refund_tx: bitcoin::Transaction,
|
|
|
|
tx_refund: TxRefund,
|
|
|
|
s_a: cross_curve_dleq::Scalar,
|
|
|
|
a: bitcoin::SecretKey,
|
|
|
|
S_b_bitcoin: bitcoin::PublicKey,
|
|
|
|
) -> Result<monero::PrivateKey> {
|
|
|
|
let s_a = monero::PrivateKey {
|
|
|
|
scalar: s_a.into_ed25519(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let tx_refund_sig = tx_refund
|
|
|
|
.extract_signature_by_key(published_refund_tx, a.public())
|
|
|
|
.context("Failed to extract signature from Bitcoin refund tx")?;
|
2020-12-03 01:28:23 -05:00
|
|
|
let tx_refund_encsig = a.encsign(S_b_bitcoin, tx_refund.digest());
|
2020-11-25 00:27:57 -05:00
|
|
|
|
|
|
|
let s_b = bitcoin::recover(S_b_bitcoin, tx_refund_sig, tx_refund_encsig)
|
|
|
|
.context("Failed to recover Monero secret key from Bitcoin signature")?;
|
|
|
|
let s_b = monero::private_key_from_secp256k1_scalar(s_b.into());
|
|
|
|
|
|
|
|
let spend_key = s_a + s_b;
|
|
|
|
|
|
|
|
Ok(spend_key)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn build_bitcoin_punish_transaction(
|
|
|
|
tx_lock: &TxLock,
|
|
|
|
refund_timelock: u32,
|
|
|
|
punish_address: &bitcoin::Address,
|
|
|
|
punish_timelock: u32,
|
|
|
|
tx_punish_sig_bob: bitcoin::Signature,
|
|
|
|
a: bitcoin::SecretKey,
|
|
|
|
B: bitcoin::PublicKey,
|
|
|
|
) -> Result<bitcoin::Transaction> {
|
2020-12-03 01:28:23 -05:00
|
|
|
let tx_cancel = bitcoin::TxCancel::new(&tx_lock, refund_timelock, a.public(), B);
|
2020-11-25 00:27:57 -05:00
|
|
|
let tx_punish = bitcoin::TxPunish::new(&tx_cancel, &punish_address, punish_timelock);
|
|
|
|
|
|
|
|
let sig_a = a.sign(tx_punish.digest());
|
2020-11-30 23:38:24 -05:00
|
|
|
let sig_b = tx_punish_sig_bob;
|
2020-11-25 00:27:57 -05:00
|
|
|
|
|
|
|
let signed_tx_punish = tx_punish
|
2020-11-30 23:38:24 -05:00
|
|
|
.add_signatures(&tx_cancel, (a.public(), sig_a), (B, sig_b))
|
2020-11-25 00:27:57 -05:00
|
|
|
.expect("sig_{a,b} to be valid signatures for tx_cancel");
|
|
|
|
|
|
|
|
Ok(signed_tx_punish)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn publish_bitcoin_punish_transaction<W>(
|
|
|
|
punish_tx: bitcoin::Transaction,
|
|
|
|
bitcoin_wallet: Arc<W>,
|
2020-12-01 18:00:00 -05:00
|
|
|
config: Config,
|
2020-11-25 00:27:57 -05:00
|
|
|
) -> Result<bitcoin::Txid>
|
|
|
|
where
|
|
|
|
W: BroadcastSignedTransaction + WaitForTransactionFinality,
|
|
|
|
{
|
|
|
|
let txid = bitcoin_wallet
|
|
|
|
.broadcast_signed_transaction(punish_tx)
|
|
|
|
.await?;
|
|
|
|
|
2020-12-01 18:00:00 -05:00
|
|
|
bitcoin_wallet
|
|
|
|
.wait_for_transaction_finality(txid, config)
|
|
|
|
.await?;
|
2020-11-25 00:27:57 -05:00
|
|
|
|
|
|
|
Ok(txid)
|
|
|
|
}
|