2020-11-26 19:30:07 -05:00
|
|
|
//! Run an XMR/BTC swap in the role of Alice.
|
|
|
|
//! Alice holds XMR and wishes receive BTC.
|
|
|
|
use crate::{
|
|
|
|
alice::{
|
2020-12-09 21:19:18 -05:00
|
|
|
event_loop::EventLoopHandle,
|
2020-12-15 00:49:38 -05:00
|
|
|
steps::{
|
2020-11-25 00:27:57 -05:00
|
|
|
build_bitcoin_punish_transaction, build_bitcoin_redeem_transaction,
|
|
|
|
extract_monero_private_key, lock_xmr, negotiate, publish_bitcoin_punish_transaction,
|
|
|
|
publish_bitcoin_redeem_transaction, publish_cancel_transaction,
|
|
|
|
wait_for_bitcoin_encrypted_signature, wait_for_bitcoin_refund, wait_for_locked_bitcoin,
|
|
|
|
},
|
2020-11-26 19:30:07 -05:00
|
|
|
},
|
2020-12-06 20:47:21 -05:00
|
|
|
bitcoin,
|
2020-11-25 00:27:57 -05:00
|
|
|
bitcoin::EncryptedSignature,
|
2020-11-26 19:30:07 -05:00
|
|
|
network::request_response::AliceToBob,
|
2020-12-06 20:47:21 -05:00
|
|
|
state,
|
2020-12-22 00:28:14 -05:00
|
|
|
state::{Alice, EndState, Swap},
|
2020-12-06 20:47:21 -05:00
|
|
|
storage::Database,
|
2020-11-26 19:30:07 -05:00
|
|
|
SwapAmounts,
|
|
|
|
};
|
2020-12-22 00:36:27 -05:00
|
|
|
use anyhow::Result;
|
2020-11-26 19:30:07 -05:00
|
|
|
use async_recursion::async_recursion;
|
|
|
|
use futures::{
|
|
|
|
future::{select, Either},
|
|
|
|
pin_mut,
|
|
|
|
};
|
|
|
|
use libp2p::request_response::ResponseChannel;
|
|
|
|
use rand::{CryptoRng, RngCore};
|
2020-12-22 00:36:27 -05:00
|
|
|
use std::{fmt, sync::Arc};
|
2020-12-01 20:36:47 -05:00
|
|
|
use tracing::info;
|
2020-12-06 20:47:21 -05:00
|
|
|
use uuid::Uuid;
|
2020-11-26 19:30:07 -05:00
|
|
|
use xmr_btc::{
|
2020-12-09 19:18:45 -05:00
|
|
|
alice::{State0, State3},
|
2020-11-25 00:27:57 -05:00
|
|
|
bitcoin::{TransactionBlockHeight, TxCancel, TxRefund, WatchForRawTransaction},
|
2020-12-01 18:00:00 -05:00
|
|
|
config::Config,
|
2020-11-26 19:30:07 -05:00
|
|
|
monero::CreateWalletForOutput,
|
2020-12-21 23:47:09 -05:00
|
|
|
ExpiredTimelocks,
|
2020-11-26 19:30:07 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
trait Rng: RngCore + CryptoRng + Send {}
|
|
|
|
|
|
|
|
impl<T> Rng for T where T: RngCore + CryptoRng + Send {}
|
|
|
|
|
2020-11-30 23:38:24 -05:00
|
|
|
#[allow(clippy::large_enum_variant)]
|
2020-11-26 19:30:07 -05:00
|
|
|
pub enum AliceState {
|
|
|
|
Started {
|
|
|
|
amounts: SwapAmounts,
|
2020-12-09 19:18:45 -05:00
|
|
|
state0: State0,
|
2020-11-26 19:30:07 -05:00
|
|
|
},
|
|
|
|
Negotiated {
|
2020-12-01 22:58:17 -05:00
|
|
|
channel: Option<ResponseChannel<AliceToBob>>,
|
2020-11-26 19:30:07 -05:00
|
|
|
amounts: SwapAmounts,
|
|
|
|
state3: State3,
|
|
|
|
},
|
|
|
|
BtcLocked {
|
2020-12-01 22:58:17 -05:00
|
|
|
channel: Option<ResponseChannel<AliceToBob>>,
|
2020-11-26 19:30:07 -05:00
|
|
|
amounts: SwapAmounts,
|
|
|
|
state3: State3,
|
|
|
|
},
|
|
|
|
XmrLocked {
|
|
|
|
state3: State3,
|
|
|
|
},
|
2020-12-21 22:53:21 -05:00
|
|
|
EncSigLearned {
|
2020-11-26 19:30:07 -05:00
|
|
|
state3: State3,
|
|
|
|
encrypted_signature: EncryptedSignature,
|
|
|
|
},
|
|
|
|
BtcRedeemed,
|
|
|
|
BtcCancelled {
|
|
|
|
state3: State3,
|
|
|
|
tx_cancel: TxCancel,
|
|
|
|
},
|
|
|
|
BtcRefunded {
|
2020-12-02 17:48:18 -05:00
|
|
|
spend_key: monero::PrivateKey,
|
2020-11-26 19:30:07 -05:00
|
|
|
state3: State3,
|
|
|
|
},
|
|
|
|
BtcPunishable {
|
|
|
|
tx_refund: TxRefund,
|
|
|
|
state3: State3,
|
|
|
|
},
|
|
|
|
XmrRefunded,
|
2020-12-21 23:47:09 -05:00
|
|
|
CancelTimelockExpired {
|
2020-11-26 19:30:07 -05:00
|
|
|
state3: State3,
|
|
|
|
},
|
2020-12-21 23:49:30 -05:00
|
|
|
BtcPunished,
|
2020-11-26 19:30:07 -05:00
|
|
|
SafelyAborted,
|
|
|
|
}
|
|
|
|
|
2020-12-01 20:36:47 -05:00
|
|
|
impl fmt::Display for AliceState {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
AliceState::Started { .. } => write!(f, "started"),
|
|
|
|
AliceState::Negotiated { .. } => write!(f, "negotiated"),
|
2020-12-21 23:49:30 -05:00
|
|
|
AliceState::BtcLocked { .. } => write!(f, "btc is locked"),
|
|
|
|
AliceState::XmrLocked { .. } => write!(f, "xmr is locked"),
|
|
|
|
AliceState::EncSigLearned { .. } => write!(f, "encrypted signature is learned"),
|
|
|
|
AliceState::BtcRedeemed => write!(f, "btc is redeemed"),
|
|
|
|
AliceState::BtcCancelled { .. } => write!(f, "btc is cancelled"),
|
|
|
|
AliceState::BtcRefunded { .. } => write!(f, "btc is refunded"),
|
|
|
|
AliceState::BtcPunished => write!(f, "btc is punished"),
|
|
|
|
AliceState::SafelyAborted => write!(f, "safely aborted"),
|
|
|
|
AliceState::BtcPunishable { .. } => write!(f, "btc is punishable"),
|
|
|
|
AliceState::XmrRefunded => write!(f, "xmr is refunded"),
|
2020-12-21 23:47:09 -05:00
|
|
|
AliceState::CancelTimelockExpired { .. } => write!(f, "cancel timelock is expired"),
|
2020-12-01 20:36:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-08 01:02:58 -05:00
|
|
|
impl From<&AliceState> for state::Alice {
|
|
|
|
fn from(alice_state: &AliceState) -> Self {
|
|
|
|
match alice_state {
|
|
|
|
AliceState::Negotiated { state3, .. } => Alice::Negotiated(state3.clone()),
|
|
|
|
AliceState::BtcLocked { state3, .. } => Alice::BtcLocked(state3.clone()),
|
|
|
|
AliceState::XmrLocked { state3 } => Alice::XmrLocked(state3.clone()),
|
2020-12-21 22:53:21 -05:00
|
|
|
AliceState::EncSigLearned {
|
2020-12-06 20:47:21 -05:00
|
|
|
state3,
|
2020-12-21 23:47:09 -05:00
|
|
|
encrypted_signature,
|
2020-12-21 22:53:21 -05:00
|
|
|
} => Alice::EncSigLearned {
|
2020-12-06 20:47:21 -05:00
|
|
|
state: state3.clone(),
|
|
|
|
encrypted_signature: encrypted_signature.clone(),
|
|
|
|
},
|
2020-12-22 00:28:14 -05:00
|
|
|
AliceState::BtcRedeemed => Alice::Done(EndState::BtcRedeemed),
|
2020-12-08 01:02:58 -05:00
|
|
|
AliceState::BtcCancelled { state3, .. } => Alice::BtcCancelled(state3.clone()),
|
2020-12-22 00:28:14 -05:00
|
|
|
AliceState::BtcRefunded { spend_key, state3 } => Alice::BtcRefunded {
|
|
|
|
spend_key: *spend_key,
|
|
|
|
state3: state3.clone(),
|
|
|
|
},
|
2020-12-08 01:02:58 -05:00
|
|
|
AliceState::BtcPunishable { state3, .. } => Alice::BtcPunishable(state3.clone()),
|
2020-12-22 00:28:14 -05:00
|
|
|
AliceState::XmrRefunded => Alice::Done(EndState::XmrRefunded),
|
2020-12-21 23:47:09 -05:00
|
|
|
AliceState::CancelTimelockExpired { state3 } => {
|
|
|
|
Alice::CancelTimelockExpired(state3.clone())
|
|
|
|
}
|
2020-12-22 00:28:14 -05:00
|
|
|
AliceState::BtcPunished => Alice::Done(EndState::BtcPunished),
|
|
|
|
AliceState::SafelyAborted => Alice::Done(EndState::SafelyAborted),
|
|
|
|
AliceState::Started { amounts, state0 } => Alice::Started {
|
|
|
|
amounts: *amounts,
|
|
|
|
state0: state0.clone(),
|
|
|
|
},
|
2020-12-08 01:02:58 -05:00
|
|
|
}
|
2020-12-06 20:47:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-22 00:36:27 -05:00
|
|
|
impl From<state::Alice> for AliceState {
|
|
|
|
fn from(db_state: state::Alice) -> Self {
|
2020-12-06 20:47:21 -05:00
|
|
|
use AliceState::*;
|
2020-12-22 00:36:27 -05:00
|
|
|
match db_state {
|
|
|
|
Alice::Started { amounts, state0 } => Started { amounts, state0 },
|
|
|
|
Alice::Negotiated(state3) => Negotiated {
|
|
|
|
channel: None,
|
|
|
|
amounts: SwapAmounts {
|
|
|
|
btc: state3.btc,
|
|
|
|
xmr: state3.xmr,
|
2020-12-06 20:47:21 -05:00
|
|
|
},
|
2020-12-22 00:36:27 -05:00
|
|
|
state3,
|
|
|
|
},
|
|
|
|
Alice::BtcLocked(state3) => BtcLocked {
|
|
|
|
channel: None,
|
|
|
|
amounts: SwapAmounts {
|
|
|
|
btc: state3.btc,
|
|
|
|
xmr: state3.xmr,
|
2020-12-06 20:47:21 -05:00
|
|
|
},
|
2020-12-22 00:36:27 -05:00
|
|
|
state3,
|
|
|
|
},
|
|
|
|
Alice::XmrLocked(state3) => XmrLocked { state3 },
|
|
|
|
Alice::EncSigLearned {
|
|
|
|
state,
|
|
|
|
encrypted_signature,
|
|
|
|
} => EncSigLearned {
|
|
|
|
state3: state,
|
|
|
|
encrypted_signature,
|
|
|
|
},
|
|
|
|
Alice::CancelTimelockExpired(state3) => AliceState::CancelTimelockExpired { state3 },
|
|
|
|
Alice::BtcCancelled(state) => {
|
|
|
|
let tx_cancel = bitcoin::TxCancel::new(
|
|
|
|
&state.tx_lock,
|
|
|
|
state.cancel_timelock,
|
|
|
|
state.a.public(),
|
|
|
|
state.B,
|
|
|
|
);
|
2020-12-06 20:47:21 -05:00
|
|
|
|
2020-12-22 00:36:27 -05:00
|
|
|
BtcCancelled {
|
2020-12-22 00:28:14 -05:00
|
|
|
state3: state,
|
2020-12-22 00:36:27 -05:00
|
|
|
tx_cancel,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Alice::BtcPunishable(state) => {
|
|
|
|
let tx_cancel = bitcoin::TxCancel::new(
|
|
|
|
&state.tx_lock,
|
|
|
|
state.cancel_timelock,
|
|
|
|
state.a.public(),
|
|
|
|
state.B,
|
|
|
|
);
|
|
|
|
let tx_refund = bitcoin::TxRefund::new(&tx_cancel, &state.refund_address);
|
|
|
|
BtcPunishable {
|
|
|
|
tx_refund,
|
2020-12-06 20:47:21 -05:00
|
|
|
state3: state,
|
2020-12-22 00:36:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Alice::BtcRefunded {
|
|
|
|
state3: state,
|
|
|
|
spend_key,
|
|
|
|
..
|
|
|
|
} => BtcRefunded {
|
|
|
|
spend_key,
|
|
|
|
state3: state,
|
|
|
|
},
|
|
|
|
Alice::Done(end_state) => match end_state {
|
|
|
|
EndState::SafelyAborted => SafelyAborted,
|
|
|
|
EndState::BtcRedeemed => BtcRedeemed,
|
|
|
|
EndState::XmrRefunded => XmrRefunded,
|
|
|
|
EndState::BtcPunished => BtcPunished,
|
|
|
|
},
|
2020-12-06 20:47:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-01 20:36:47 -05:00
|
|
|
pub async fn swap(
|
2020-12-01 20:34:47 -05:00
|
|
|
state: AliceState,
|
2020-12-09 21:55:29 -05:00
|
|
|
event_loop_handle: EventLoopHandle,
|
2020-12-01 20:34:47 -05:00
|
|
|
bitcoin_wallet: Arc<crate::bitcoin::Wallet>,
|
|
|
|
monero_wallet: Arc<crate::monero::Wallet>,
|
2020-12-01 20:36:47 -05:00
|
|
|
config: Config,
|
2020-12-06 20:47:21 -05:00
|
|
|
swap_id: Uuid,
|
|
|
|
db: Database,
|
2020-12-14 05:30:45 -05:00
|
|
|
) -> Result<AliceState> {
|
2020-12-01 20:36:47 -05:00
|
|
|
run_until(
|
|
|
|
state,
|
|
|
|
is_complete,
|
2020-12-09 21:55:29 -05:00
|
|
|
event_loop_handle,
|
2020-12-01 20:36:47 -05:00
|
|
|
bitcoin_wallet,
|
|
|
|
monero_wallet,
|
|
|
|
config,
|
2020-12-06 20:47:21 -05:00
|
|
|
swap_id,
|
|
|
|
db,
|
2020-12-01 20:36:47 -05:00
|
|
|
)
|
|
|
|
.await
|
2020-12-01 20:34:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_complete(state: &AliceState) -> bool {
|
2020-12-01 20:36:47 -05:00
|
|
|
matches!(
|
|
|
|
state,
|
|
|
|
AliceState::XmrRefunded
|
|
|
|
| AliceState::BtcRedeemed
|
2020-12-21 23:49:30 -05:00
|
|
|
| AliceState::BtcPunished
|
2020-12-01 20:36:47 -05:00
|
|
|
| AliceState::SafelyAborted
|
|
|
|
)
|
2020-12-01 20:34:47 -05:00
|
|
|
}
|
|
|
|
|
2020-12-01 20:36:47 -05:00
|
|
|
pub fn is_xmr_locked(state: &AliceState) -> bool {
|
|
|
|
matches!(
|
|
|
|
state,
|
|
|
|
AliceState::XmrLocked{..}
|
|
|
|
)
|
|
|
|
}
|
2020-12-01 20:34:47 -05:00
|
|
|
|
2020-12-09 22:59:09 -05:00
|
|
|
pub fn is_encsig_learned(state: &AliceState) -> bool {
|
|
|
|
matches!(
|
|
|
|
state,
|
2020-12-21 22:53:21 -05:00
|
|
|
AliceState::EncSigLearned{..}
|
2020-12-09 22:59:09 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-11-26 19:30:07 -05:00
|
|
|
// State machine driver for swap execution
|
|
|
|
#[async_recursion]
|
2020-12-06 20:47:21 -05:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2020-12-01 20:34:47 -05:00
|
|
|
pub async fn run_until(
|
2020-11-26 19:30:07 -05:00
|
|
|
state: AliceState,
|
2020-12-01 20:36:47 -05:00
|
|
|
is_target_state: fn(&AliceState) -> bool,
|
2020-12-09 21:55:29 -05:00
|
|
|
mut event_loop_handle: EventLoopHandle,
|
2020-11-26 19:30:07 -05:00
|
|
|
bitcoin_wallet: Arc<crate::bitcoin::Wallet>,
|
|
|
|
monero_wallet: Arc<crate::monero::Wallet>,
|
2020-12-01 18:00:00 -05:00
|
|
|
config: Config,
|
2020-12-06 20:47:21 -05:00
|
|
|
swap_id: Uuid,
|
|
|
|
db: Database,
|
2020-12-14 01:24:23 -05:00
|
|
|
// TODO: Remove EventLoopHandle!
|
2020-12-14 05:30:45 -05:00
|
|
|
) -> Result<AliceState> {
|
2020-12-06 21:55:13 -05:00
|
|
|
info!("Current state:{}", state);
|
2020-12-01 20:36:47 -05:00
|
|
|
if is_target_state(&state) {
|
2020-12-14 05:30:45 -05:00
|
|
|
Ok(state)
|
2020-12-01 20:34:47 -05:00
|
|
|
} else {
|
|
|
|
match state {
|
2020-12-09 19:18:45 -05:00
|
|
|
AliceState::Started { amounts, state0 } => {
|
2020-12-09 21:55:29 -05:00
|
|
|
let (channel, state3) =
|
|
|
|
negotiate(state0, amounts, &mut event_loop_handle, config).await?;
|
2020-11-26 19:30:07 -05:00
|
|
|
|
2020-12-06 20:47:21 -05:00
|
|
|
let state = AliceState::Negotiated {
|
|
|
|
channel: Some(channel),
|
|
|
|
amounts,
|
|
|
|
state3,
|
|
|
|
};
|
|
|
|
|
2020-12-08 01:02:58 -05:00
|
|
|
let db_state = (&state).into();
|
|
|
|
db.insert_latest_state(swap_id, Swap::Alice(db_state))
|
|
|
|
.await?;
|
2020-12-01 20:36:47 -05:00
|
|
|
run_until(
|
2020-12-06 20:47:21 -05:00
|
|
|
state,
|
2020-12-01 20:36:47 -05:00
|
|
|
is_target_state,
|
2020-12-09 21:55:29 -05:00
|
|
|
event_loop_handle,
|
2020-12-01 20:36:47 -05:00
|
|
|
bitcoin_wallet,
|
|
|
|
monero_wallet,
|
|
|
|
config,
|
2020-12-06 20:47:21 -05:00
|
|
|
swap_id,
|
|
|
|
db,
|
2020-12-01 20:36:47 -05:00
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
AliceState::Negotiated {
|
|
|
|
state3,
|
2020-11-26 19:30:07 -05:00
|
|
|
channel,
|
|
|
|
amounts,
|
2020-12-01 20:36:47 -05:00
|
|
|
} => {
|
2020-12-06 20:47:21 -05:00
|
|
|
let state = match channel {
|
2020-12-01 22:58:17 -05:00
|
|
|
Some(channel) => {
|
|
|
|
let _ = wait_for_locked_bitcoin(
|
|
|
|
state3.tx_lock.txid(),
|
|
|
|
bitcoin_wallet.clone(),
|
|
|
|
config,
|
|
|
|
)
|
2020-12-01 20:36:47 -05:00
|
|
|
.await?;
|
2020-11-26 19:30:07 -05:00
|
|
|
|
2020-12-06 20:47:21 -05:00
|
|
|
AliceState::BtcLocked {
|
|
|
|
channel: Some(channel),
|
|
|
|
amounts,
|
|
|
|
state3,
|
|
|
|
}
|
2020-12-01 22:58:17 -05:00
|
|
|
}
|
|
|
|
None => {
|
|
|
|
tracing::info!("Cannot resume swap from negotiated state, aborting");
|
|
|
|
|
|
|
|
// Alice did not lock Xmr yet
|
2020-12-06 20:47:21 -05:00
|
|
|
AliceState::SafelyAborted
|
2020-12-01 22:58:17 -05:00
|
|
|
}
|
2020-12-06 20:47:21 -05:00
|
|
|
};
|
|
|
|
|
2020-12-08 01:02:58 -05:00
|
|
|
let db_state = (&state).into();
|
|
|
|
db.insert_latest_state(swap_id, Swap::Alice(db_state))
|
|
|
|
.await?;
|
2020-12-06 20:47:21 -05:00
|
|
|
run_until(
|
|
|
|
state,
|
|
|
|
is_target_state,
|
|
|
|
event_loop_handle,
|
|
|
|
bitcoin_wallet,
|
|
|
|
monero_wallet,
|
|
|
|
config,
|
|
|
|
swap_id,
|
|
|
|
db,
|
|
|
|
)
|
|
|
|
.await
|
2020-12-01 20:36:47 -05:00
|
|
|
}
|
|
|
|
AliceState::BtcLocked {
|
|
|
|
channel,
|
|
|
|
amounts,
|
|
|
|
state3,
|
2020-12-06 20:47:21 -05:00
|
|
|
} => {
|
|
|
|
let state = match channel {
|
|
|
|
Some(channel) => {
|
|
|
|
lock_xmr(
|
|
|
|
channel,
|
|
|
|
amounts,
|
|
|
|
state3.clone(),
|
|
|
|
&mut event_loop_handle,
|
|
|
|
monero_wallet.clone(),
|
|
|
|
)
|
|
|
|
.await?;
|
2020-12-01 20:36:47 -05:00
|
|
|
|
2020-12-06 20:47:21 -05:00
|
|
|
AliceState::XmrLocked { state3 }
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
tracing::info!("Cannot resume swap from BTC locked state, aborting");
|
|
|
|
|
|
|
|
// Alice did not lock Xmr yet
|
|
|
|
AliceState::SafelyAborted
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-08 01:02:58 -05:00
|
|
|
let db_state = (&state).into();
|
|
|
|
db.insert_latest_state(swap_id, Swap::Alice(db_state))
|
|
|
|
.await?;
|
2020-12-06 20:47:21 -05:00
|
|
|
run_until(
|
|
|
|
state,
|
|
|
|
is_target_state,
|
|
|
|
event_loop_handle,
|
|
|
|
bitcoin_wallet,
|
|
|
|
monero_wallet,
|
|
|
|
config,
|
|
|
|
swap_id,
|
|
|
|
db,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
2020-12-01 20:36:47 -05:00
|
|
|
AliceState::XmrLocked { state3 } => {
|
2020-12-21 23:47:09 -05:00
|
|
|
// todo: match statement and wait for cancel timelock to expire can probably be
|
|
|
|
// expressed more cleanly
|
|
|
|
let state = match state3.expired_timelocks(bitcoin_wallet.as_ref()).await? {
|
|
|
|
ExpiredTimelocks::None => {
|
2020-12-11 00:49:19 -05:00
|
|
|
let wait_for_enc_sig = wait_for_bitcoin_encrypted_signature(
|
|
|
|
&mut event_loop_handle,
|
|
|
|
config.monero_max_finality_time,
|
|
|
|
);
|
2020-12-06 20:47:21 -05:00
|
|
|
let state3_clone = state3.clone();
|
2020-12-21 23:47:09 -05:00
|
|
|
let cancel_timelock_expires = state3_clone
|
|
|
|
.wait_for_cancel_timelock_to_expire(bitcoin_wallet.as_ref());
|
2020-12-06 20:47:21 -05:00
|
|
|
|
|
|
|
pin_mut!(wait_for_enc_sig);
|
2020-12-21 23:47:09 -05:00
|
|
|
pin_mut!(cancel_timelock_expires);
|
2020-12-06 20:47:21 -05:00
|
|
|
|
2020-12-21 23:47:09 -05:00
|
|
|
match select(cancel_timelock_expires, wait_for_enc_sig).await {
|
|
|
|
Either::Left(_) => AliceState::CancelTimelockExpired { state3 },
|
2020-12-21 22:53:21 -05:00
|
|
|
Either::Right((enc_sig, _)) => AliceState::EncSigLearned {
|
2020-12-06 20:47:21 -05:00
|
|
|
state3,
|
|
|
|
encrypted_signature: enc_sig?,
|
|
|
|
},
|
2020-12-11 00:49:19 -05:00
|
|
|
}
|
2020-12-01 20:36:47 -05:00
|
|
|
}
|
2020-12-21 23:47:09 -05:00
|
|
|
_ => AliceState::CancelTimelockExpired { state3 },
|
2020-12-06 20:47:21 -05:00
|
|
|
};
|
2020-12-11 00:49:19 -05:00
|
|
|
|
2020-12-08 01:02:58 -05:00
|
|
|
let db_state = (&state).into();
|
|
|
|
db.insert_latest_state(swap_id, Swap::Alice(db_state))
|
|
|
|
.await?;
|
2020-12-06 20:47:21 -05:00
|
|
|
run_until(
|
|
|
|
state,
|
|
|
|
is_target_state,
|
|
|
|
event_loop_handle,
|
|
|
|
bitcoin_wallet.clone(),
|
|
|
|
monero_wallet,
|
|
|
|
config,
|
|
|
|
swap_id,
|
|
|
|
db,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
2020-12-21 22:53:21 -05:00
|
|
|
AliceState::EncSigLearned {
|
2020-12-01 20:36:47 -05:00
|
|
|
state3,
|
2020-11-25 00:27:57 -05:00
|
|
|
encrypted_signature,
|
2020-12-01 20:36:47 -05:00
|
|
|
} => {
|
2020-12-21 01:33:18 -05:00
|
|
|
// TODO: Evaluate if it is correct for Alice to Redeem no matter what.
|
2020-12-21 23:47:09 -05:00
|
|
|
// If cancel timelock expired she should potentially not try redeem. (The
|
|
|
|
// implementation gives her an advantage.)
|
2020-12-21 01:33:18 -05:00
|
|
|
|
2020-12-01 20:36:47 -05:00
|
|
|
let signed_tx_redeem = match build_bitcoin_redeem_transaction(
|
|
|
|
encrypted_signature,
|
|
|
|
&state3.tx_lock,
|
|
|
|
state3.a.clone(),
|
|
|
|
state3.s_a,
|
|
|
|
state3.B,
|
|
|
|
&state3.redeem_address,
|
|
|
|
) {
|
|
|
|
Ok(tx) => tx,
|
|
|
|
Err(_) => {
|
2020-12-21 23:47:09 -05:00
|
|
|
state3
|
|
|
|
.wait_for_cancel_timelock_to_expire(bitcoin_wallet.as_ref())
|
|
|
|
.await?;
|
2020-12-14 18:26:53 -05:00
|
|
|
|
2020-12-21 23:47:09 -05:00
|
|
|
let state = AliceState::CancelTimelockExpired { state3 };
|
2020-12-08 01:02:58 -05:00
|
|
|
let db_state = (&state).into();
|
|
|
|
db.insert_latest_state(swap_id, Swap::Alice(db_state))
|
|
|
|
.await?;
|
2020-12-01 20:36:47 -05:00
|
|
|
return run_until(
|
2020-12-06 20:47:21 -05:00
|
|
|
state,
|
2020-12-01 20:36:47 -05:00
|
|
|
is_target_state,
|
2020-12-09 21:55:29 -05:00
|
|
|
event_loop_handle,
|
2020-12-01 20:36:47 -05:00
|
|
|
bitcoin_wallet,
|
|
|
|
monero_wallet,
|
|
|
|
config,
|
2020-12-06 20:47:21 -05:00
|
|
|
swap_id,
|
|
|
|
db,
|
2020-12-01 20:36:47 -05:00
|
|
|
)
|
|
|
|
.await;
|
|
|
|
}
|
|
|
|
};
|
2020-11-26 19:30:07 -05:00
|
|
|
|
2020-12-01 20:36:47 -05:00
|
|
|
// TODO(Franck): Error handling is delicate here.
|
|
|
|
// If Bob sees this transaction he can redeem Monero
|
|
|
|
// e.g. If the Bitcoin node is down then the user needs to take action.
|
|
|
|
publish_bitcoin_redeem_transaction(
|
|
|
|
signed_tx_redeem,
|
|
|
|
bitcoin_wallet.clone(),
|
|
|
|
config,
|
|
|
|
)
|
2020-12-01 18:00:00 -05:00
|
|
|
.await?;
|
2020-11-26 19:30:07 -05:00
|
|
|
|
2020-12-06 20:47:21 -05:00
|
|
|
let state = AliceState::BtcRedeemed;
|
2020-12-08 01:02:58 -05:00
|
|
|
let db_state = (&state).into();
|
|
|
|
db.insert_latest_state(swap_id, Swap::Alice(db_state))
|
|
|
|
.await?;
|
2020-12-01 20:36:47 -05:00
|
|
|
run_until(
|
2020-12-06 20:47:21 -05:00
|
|
|
state,
|
2020-12-01 20:36:47 -05:00
|
|
|
is_target_state,
|
2020-12-09 21:55:29 -05:00
|
|
|
event_loop_handle,
|
2020-12-01 20:36:47 -05:00
|
|
|
bitcoin_wallet,
|
|
|
|
monero_wallet,
|
|
|
|
config,
|
2020-12-06 20:47:21 -05:00
|
|
|
swap_id,
|
|
|
|
db,
|
2020-12-01 20:36:47 -05:00
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
2020-12-21 23:47:09 -05:00
|
|
|
AliceState::CancelTimelockExpired { state3 } => {
|
2020-12-01 20:36:47 -05:00
|
|
|
let tx_cancel = publish_cancel_transaction(
|
|
|
|
state3.tx_lock.clone(),
|
|
|
|
state3.a.clone(),
|
|
|
|
state3.B,
|
2020-12-21 23:47:09 -05:00
|
|
|
state3.cancel_timelock,
|
2020-12-01 20:36:47 -05:00
|
|
|
state3.tx_cancel_sig_bob.clone(),
|
|
|
|
bitcoin_wallet.clone(),
|
|
|
|
)
|
|
|
|
.await?;
|
2020-11-26 19:30:07 -05:00
|
|
|
|
2020-12-06 20:47:21 -05:00
|
|
|
let state = AliceState::BtcCancelled { state3, tx_cancel };
|
2020-12-08 01:02:58 -05:00
|
|
|
let db_state = (&state).into();
|
|
|
|
db.insert_latest_state(swap_id, Swap::Alice(db_state))
|
|
|
|
.await?;
|
2020-12-01 20:36:47 -05:00
|
|
|
run_until(
|
2020-12-06 20:47:21 -05:00
|
|
|
state,
|
2020-12-01 20:36:47 -05:00
|
|
|
is_target_state,
|
2020-12-09 21:55:29 -05:00
|
|
|
event_loop_handle,
|
2020-12-01 20:36:47 -05:00
|
|
|
bitcoin_wallet,
|
|
|
|
monero_wallet,
|
|
|
|
config,
|
2020-12-06 20:47:21 -05:00
|
|
|
swap_id,
|
|
|
|
db,
|
2020-12-01 20:36:47 -05:00
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
AliceState::BtcCancelled { state3, tx_cancel } => {
|
|
|
|
let tx_cancel_height = bitcoin_wallet
|
|
|
|
.transaction_block_height(tx_cancel.txid())
|
|
|
|
.await;
|
2020-11-26 19:30:07 -05:00
|
|
|
|
2020-12-01 20:36:47 -05:00
|
|
|
let (tx_refund, published_refund_tx) = wait_for_bitcoin_refund(
|
|
|
|
&tx_cancel,
|
|
|
|
tx_cancel_height,
|
|
|
|
state3.punish_timelock,
|
|
|
|
&state3.refund_address,
|
|
|
|
bitcoin_wallet.clone(),
|
|
|
|
)
|
|
|
|
.await?;
|
2020-11-26 19:30:07 -05:00
|
|
|
|
2020-12-01 20:36:47 -05:00
|
|
|
// TODO(Franck): Review error handling
|
|
|
|
match published_refund_tx {
|
|
|
|
None => {
|
2020-12-06 20:47:21 -05:00
|
|
|
let state = AliceState::BtcPunishable { tx_refund, state3 };
|
2020-12-08 01:02:58 -05:00
|
|
|
let db_state = (&state).into();
|
|
|
|
db.insert_latest_state(swap_id, Swap::Alice(db_state))
|
|
|
|
.await?;
|
2020-12-06 20:47:21 -05:00
|
|
|
swap(
|
|
|
|
state,
|
2020-12-09 21:55:29 -05:00
|
|
|
event_loop_handle,
|
2020-12-01 20:36:47 -05:00
|
|
|
bitcoin_wallet.clone(),
|
|
|
|
monero_wallet,
|
|
|
|
config,
|
2020-12-06 20:47:21 -05:00
|
|
|
swap_id,
|
|
|
|
db,
|
2020-12-01 20:36:47 -05:00
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
Some(published_refund_tx) => {
|
2020-12-02 17:48:18 -05:00
|
|
|
let spend_key = extract_monero_private_key(
|
|
|
|
published_refund_tx,
|
|
|
|
tx_refund,
|
|
|
|
state3.s_a,
|
|
|
|
state3.a.clone(),
|
|
|
|
state3.S_b_bitcoin,
|
|
|
|
)?;
|
|
|
|
|
2020-12-06 20:47:21 -05:00
|
|
|
let state = AliceState::BtcRefunded { spend_key, state3 };
|
2020-12-08 01:02:58 -05:00
|
|
|
let db_state = (&state).into();
|
|
|
|
db.insert_latest_state(swap_id, Swap::Alice(db_state))
|
|
|
|
.await?;
|
2020-12-01 20:36:47 -05:00
|
|
|
run_until(
|
2020-12-06 20:47:21 -05:00
|
|
|
state,
|
2020-12-01 20:36:47 -05:00
|
|
|
is_target_state,
|
2020-12-09 21:55:29 -05:00
|
|
|
event_loop_handle,
|
2020-12-01 20:36:47 -05:00
|
|
|
bitcoin_wallet.clone(),
|
|
|
|
monero_wallet,
|
|
|
|
config,
|
2020-12-06 20:47:21 -05:00
|
|
|
swap_id,
|
|
|
|
db,
|
2020-12-01 20:36:47 -05:00
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
2020-11-26 19:30:07 -05:00
|
|
|
}
|
|
|
|
}
|
2020-12-02 17:48:18 -05:00
|
|
|
AliceState::BtcRefunded { spend_key, state3 } => {
|
2020-12-01 20:36:47 -05:00
|
|
|
let view_key = state3.v;
|
2020-11-26 19:30:07 -05:00
|
|
|
|
2020-12-01 20:36:47 -05:00
|
|
|
monero_wallet
|
|
|
|
.create_and_load_wallet_for_output(spend_key, view_key)
|
|
|
|
.await?;
|
2020-11-26 19:30:07 -05:00
|
|
|
|
2020-12-06 20:47:21 -05:00
|
|
|
let state = AliceState::XmrRefunded;
|
2020-12-08 01:02:58 -05:00
|
|
|
let db_state = (&state).into();
|
|
|
|
db.insert_latest_state(swap_id, Swap::Alice(db_state))
|
|
|
|
.await?;
|
2020-12-14 05:30:45 -05:00
|
|
|
Ok(state)
|
2020-12-01 20:36:47 -05:00
|
|
|
}
|
|
|
|
AliceState::BtcPunishable { tx_refund, state3 } => {
|
|
|
|
let signed_tx_punish = build_bitcoin_punish_transaction(
|
|
|
|
&state3.tx_lock,
|
2020-12-21 23:47:09 -05:00
|
|
|
state3.cancel_timelock,
|
2020-12-01 20:36:47 -05:00
|
|
|
&state3.punish_address,
|
|
|
|
state3.punish_timelock,
|
|
|
|
state3.tx_punish_sig_bob.clone(),
|
|
|
|
state3.a.clone(),
|
|
|
|
state3.B,
|
|
|
|
)?;
|
2020-11-26 19:30:07 -05:00
|
|
|
|
2020-12-01 20:36:47 -05:00
|
|
|
let punish_tx_finalised = publish_bitcoin_punish_transaction(
|
|
|
|
signed_tx_punish,
|
|
|
|
bitcoin_wallet.clone(),
|
|
|
|
config,
|
|
|
|
);
|
2020-11-26 19:30:07 -05:00
|
|
|
|
2020-12-01 20:36:47 -05:00
|
|
|
let refund_tx_seen = bitcoin_wallet.watch_for_raw_transaction(tx_refund.txid());
|
2020-11-26 19:30:07 -05:00
|
|
|
|
2020-12-01 20:36:47 -05:00
|
|
|
pin_mut!(punish_tx_finalised);
|
|
|
|
pin_mut!(refund_tx_seen);
|
2020-11-26 19:30:07 -05:00
|
|
|
|
2020-12-01 20:36:47 -05:00
|
|
|
match select(punish_tx_finalised, refund_tx_seen).await {
|
|
|
|
Either::Left(_) => {
|
2020-12-21 23:49:30 -05:00
|
|
|
let state = AliceState::BtcPunished;
|
2020-12-08 01:02:58 -05:00
|
|
|
let db_state = (&state).into();
|
|
|
|
db.insert_latest_state(swap_id, Swap::Alice(db_state))
|
|
|
|
.await?;
|
2020-12-01 20:36:47 -05:00
|
|
|
run_until(
|
2020-12-06 20:47:21 -05:00
|
|
|
state,
|
2020-12-01 20:36:47 -05:00
|
|
|
is_target_state,
|
2020-12-09 21:55:29 -05:00
|
|
|
event_loop_handle,
|
2020-12-01 20:36:47 -05:00
|
|
|
bitcoin_wallet.clone(),
|
|
|
|
monero_wallet,
|
|
|
|
config,
|
2020-12-06 20:47:21 -05:00
|
|
|
swap_id,
|
|
|
|
db,
|
2020-12-01 20:36:47 -05:00
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
Either::Right((published_refund_tx, _)) => {
|
2020-12-02 17:48:18 -05:00
|
|
|
let spend_key = extract_monero_private_key(
|
|
|
|
published_refund_tx,
|
|
|
|
tx_refund,
|
|
|
|
state3.s_a,
|
|
|
|
state3.a.clone(),
|
|
|
|
state3.S_b_bitcoin,
|
|
|
|
)?;
|
2020-12-06 20:47:21 -05:00
|
|
|
let state = AliceState::BtcRefunded { spend_key, state3 };
|
2020-12-08 01:02:58 -05:00
|
|
|
let db_state = (&state).into();
|
|
|
|
db.insert_latest_state(swap_id, Swap::Alice(db_state))
|
|
|
|
.await?;
|
2020-12-01 20:36:47 -05:00
|
|
|
run_until(
|
2020-12-06 20:47:21 -05:00
|
|
|
state,
|
2020-12-01 20:36:47 -05:00
|
|
|
is_target_state,
|
2020-12-09 21:55:29 -05:00
|
|
|
event_loop_handle,
|
2020-12-01 20:36:47 -05:00
|
|
|
bitcoin_wallet.clone(),
|
|
|
|
monero_wallet,
|
|
|
|
config,
|
2020-12-06 20:47:21 -05:00
|
|
|
swap_id,
|
|
|
|
db,
|
2020-12-01 20:36:47 -05:00
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
2020-11-26 19:30:07 -05:00
|
|
|
}
|
|
|
|
}
|
2020-12-14 05:30:45 -05:00
|
|
|
AliceState::XmrRefunded => Ok(AliceState::XmrRefunded),
|
|
|
|
AliceState::BtcRedeemed => Ok(AliceState::BtcRedeemed),
|
2020-12-21 23:49:30 -05:00
|
|
|
AliceState::BtcPunished => Ok(AliceState::BtcPunished),
|
2020-12-14 05:30:45 -05:00
|
|
|
AliceState::SafelyAborted => Ok(AliceState::SafelyAborted),
|
2020-11-26 19:30:07 -05:00
|
|
|
}
|
2020-12-01 20:34:47 -05:00
|
|
|
}
|
2020-11-26 19:30:07 -05:00
|
|
|
}
|