2020-10-15 18:14:39 -04:00
|
|
|
//! Run an XMR/BTC swap in the role of Bob.
|
|
|
|
//! Bob holds BTC and wishes receive XMR.
|
2021-01-18 05:57:17 -05:00
|
|
|
use anyhow::Result;
|
2021-01-18 04:41:58 -05:00
|
|
|
use libp2p::{core::Multiaddr, NetworkBehaviour, PeerId};
|
2020-12-09 22:20:27 -05:00
|
|
|
use tracing::{debug, info};
|
2021-01-04 22:08:36 -05:00
|
|
|
|
|
|
|
use crate::{
|
2021-01-18 03:56:43 -05:00
|
|
|
bitcoin,
|
2020-12-09 22:20:27 -05:00
|
|
|
bitcoin::EncryptedSignature,
|
2021-01-18 05:57:17 -05:00
|
|
|
database, monero, network,
|
2021-01-18 04:41:58 -05:00
|
|
|
network::peer_tracker::{self, PeerTracker},
|
2021-01-04 22:08:36 -05:00
|
|
|
protocol::{alice, bob},
|
2021-01-18 05:57:17 -05:00
|
|
|
StartingBalances, SwapAmounts,
|
2020-10-15 18:14:39 -04:00
|
|
|
};
|
|
|
|
|
2021-01-07 20:33:23 -05:00
|
|
|
pub use self::{
|
2021-01-14 18:42:49 -05:00
|
|
|
amounts::*,
|
|
|
|
event_loop::{EventLoop, EventLoopHandle},
|
|
|
|
message0::Message0,
|
|
|
|
message1::Message1,
|
|
|
|
message2::Message2,
|
|
|
|
message3::Message3,
|
|
|
|
state::*,
|
2021-01-18 03:56:43 -05:00
|
|
|
swap::{run, run_until},
|
2021-01-07 20:33:23 -05:00
|
|
|
};
|
2021-01-18 05:57:17 -05:00
|
|
|
use crate::{config::Config, database::Database, network::transport::build, seed::Seed};
|
|
|
|
use libp2p::identity::Keypair;
|
|
|
|
use rand::rngs::OsRng;
|
|
|
|
use std::{path::PathBuf, sync::Arc};
|
2021-01-18 03:56:43 -05:00
|
|
|
use uuid::Uuid;
|
2021-01-07 20:33:23 -05:00
|
|
|
|
2020-12-09 22:20:27 -05:00
|
|
|
mod amounts;
|
|
|
|
pub mod event_loop;
|
|
|
|
mod message0;
|
|
|
|
mod message1;
|
|
|
|
mod message2;
|
|
|
|
mod message3;
|
2021-01-04 22:08:36 -05:00
|
|
|
pub mod state;
|
2020-12-09 22:20:27 -05:00
|
|
|
pub mod swap;
|
2020-10-15 18:14:39 -04:00
|
|
|
|
2021-01-18 03:56:43 -05:00
|
|
|
pub struct Swap {
|
|
|
|
pub state: BobState,
|
|
|
|
pub event_loop_handle: bob::EventLoopHandle,
|
|
|
|
pub db: Database,
|
|
|
|
pub bitcoin_wallet: Arc<bitcoin::Wallet>,
|
|
|
|
pub monero_wallet: Arc<monero::Wallet>,
|
|
|
|
pub swap_id: Uuid,
|
|
|
|
}
|
|
|
|
|
2021-01-18 17:43:50 -05:00
|
|
|
pub struct SwapFactory {
|
|
|
|
swap_id: Uuid,
|
2021-01-18 05:57:17 -05:00
|
|
|
identity: Keypair,
|
|
|
|
peer_id: PeerId,
|
|
|
|
db_path: PathBuf,
|
|
|
|
config: Config,
|
|
|
|
|
|
|
|
alice_connect_address: Multiaddr,
|
|
|
|
alice_connect_peer_id: PeerId,
|
2021-01-18 17:43:50 -05:00
|
|
|
|
|
|
|
pub bitcoin_wallet: Arc<bitcoin::Wallet>,
|
|
|
|
pub monero_wallet: Arc<monero::Wallet>,
|
|
|
|
pub starting_balances: StartingBalances,
|
2021-01-18 05:57:17 -05:00
|
|
|
}
|
|
|
|
|
2021-01-18 17:43:50 -05:00
|
|
|
impl SwapFactory {
|
2021-01-18 05:57:17 -05:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
|
|
pub fn new(
|
|
|
|
seed: Seed,
|
|
|
|
db_path: PathBuf,
|
|
|
|
swap_id: Uuid,
|
|
|
|
bitcoin_wallet: Arc<bitcoin::Wallet>,
|
|
|
|
monero_wallet: Arc<monero::Wallet>,
|
|
|
|
config: Config,
|
|
|
|
starting_balances: StartingBalances,
|
|
|
|
alice_connect_address: Multiaddr,
|
|
|
|
alice_connect_peer_id: PeerId,
|
|
|
|
) -> Self {
|
|
|
|
let identity = network::Seed::new(seed).derive_libp2p_identity();
|
|
|
|
let peer_id = identity.public().into_peer_id();
|
|
|
|
|
|
|
|
Self {
|
2021-01-18 17:43:50 -05:00
|
|
|
swap_id,
|
2021-01-18 05:57:17 -05:00
|
|
|
identity,
|
|
|
|
peer_id,
|
|
|
|
db_path,
|
|
|
|
config,
|
|
|
|
alice_connect_address,
|
|
|
|
alice_connect_peer_id,
|
2021-01-18 17:43:50 -05:00
|
|
|
bitcoin_wallet,
|
|
|
|
monero_wallet,
|
|
|
|
starting_balances,
|
2021-01-18 05:57:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn new_swap_as_bob(
|
|
|
|
&self,
|
|
|
|
swap_amounts: SwapAmounts,
|
|
|
|
) -> Result<(bob::Swap, bob::EventLoop)> {
|
|
|
|
let initial_state = init_bob_state(
|
|
|
|
swap_amounts.btc,
|
|
|
|
swap_amounts.xmr,
|
|
|
|
self.bitcoin_wallet.clone(),
|
|
|
|
self.config,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
let (event_loop, event_loop_handle) = init_bob_event_loop(
|
|
|
|
self.identity.clone(),
|
|
|
|
self.peer_id.clone(),
|
|
|
|
self.alice_connect_peer_id.clone(),
|
|
|
|
self.alice_connect_address.clone(),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let db = Database::open(self.db_path.as_path())?;
|
|
|
|
|
|
|
|
Ok((
|
|
|
|
Swap {
|
|
|
|
state: initial_state,
|
|
|
|
event_loop_handle,
|
|
|
|
db,
|
|
|
|
bitcoin_wallet: self.bitcoin_wallet.clone(),
|
|
|
|
monero_wallet: self.monero_wallet.clone(),
|
|
|
|
swap_id: self.swap_id,
|
|
|
|
},
|
|
|
|
event_loop,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn recover_bob_from_db(&self) -> Result<(bob::Swap, bob::EventLoop)> {
|
|
|
|
// reopen the existing database
|
|
|
|
let db = Database::open(self.db_path.clone().as_path())?;
|
|
|
|
|
|
|
|
let resume_state = if let database::Swap::Bob(state) = db.get_state(self.swap_id)? {
|
|
|
|
state.into()
|
|
|
|
} else {
|
|
|
|
unreachable!()
|
|
|
|
};
|
|
|
|
|
|
|
|
let (event_loop, event_loop_handle) = init_bob_event_loop(
|
|
|
|
self.identity.clone(),
|
|
|
|
self.peer_id.clone(),
|
|
|
|
self.alice_connect_peer_id.clone(),
|
|
|
|
self.alice_connect_address.clone(),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
Ok((
|
|
|
|
Swap {
|
|
|
|
state: resume_state,
|
|
|
|
event_loop_handle,
|
|
|
|
db,
|
|
|
|
bitcoin_wallet: self.bitcoin_wallet.clone(),
|
|
|
|
monero_wallet: self.monero_wallet.clone(),
|
|
|
|
swap_id: self.swap_id,
|
|
|
|
},
|
|
|
|
event_loop,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn init_bob_state(
|
|
|
|
btc_to_swap: bitcoin::Amount,
|
|
|
|
xmr_to_swap: monero::Amount,
|
|
|
|
bob_btc_wallet: Arc<bitcoin::Wallet>,
|
|
|
|
config: Config,
|
|
|
|
) -> Result<BobState> {
|
|
|
|
let amounts = SwapAmounts {
|
|
|
|
btc: btc_to_swap,
|
|
|
|
xmr: xmr_to_swap,
|
|
|
|
};
|
|
|
|
|
|
|
|
let refund_address = bob_btc_wallet.new_address().await?;
|
|
|
|
let state0 = bob::State0::new(
|
|
|
|
&mut OsRng,
|
|
|
|
btc_to_swap,
|
|
|
|
xmr_to_swap,
|
|
|
|
config.bitcoin_cancel_timelock,
|
|
|
|
config.bitcoin_punish_timelock,
|
|
|
|
refund_address,
|
|
|
|
config.monero_finality_confirmations,
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(BobState::Started { state0, amounts })
|
|
|
|
}
|
|
|
|
|
|
|
|
fn init_bob_event_loop(
|
|
|
|
identity: Keypair,
|
|
|
|
peer_id: PeerId,
|
|
|
|
alice_peer_id: PeerId,
|
|
|
|
alice_addr: Multiaddr,
|
|
|
|
) -> Result<(bob::event_loop::EventLoop, bob::event_loop::EventLoopHandle)> {
|
|
|
|
let bob_behaviour = bob::Behaviour::default();
|
|
|
|
let bob_transport = build(identity)?;
|
|
|
|
|
|
|
|
bob::event_loop::EventLoop::new(
|
|
|
|
bob_transport,
|
|
|
|
bob_behaviour,
|
|
|
|
peer_id,
|
|
|
|
alice_peer_id,
|
|
|
|
alice_addr,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-10-27 02:18:19 -04:00
|
|
|
#[derive(Debug, Clone)]
|
2020-10-19 19:10:28 -04:00
|
|
|
pub enum OutEvent {
|
2020-10-15 18:14:39 -04:00
|
|
|
ConnectionEstablished(PeerId),
|
2020-10-21 23:12:49 -04:00
|
|
|
Amounts(SwapAmounts),
|
2020-12-22 23:06:43 -05:00
|
|
|
Message0(Box<alice::Message0>),
|
|
|
|
Message1(Box<alice::Message1>),
|
2020-10-26 21:11:03 -04:00
|
|
|
Message2(alice::Message2),
|
2020-10-29 18:26:52 -04:00
|
|
|
Message3,
|
2020-10-15 18:14:39 -04:00
|
|
|
}
|
|
|
|
|
2020-10-19 19:10:28 -04:00
|
|
|
impl From<peer_tracker::OutEvent> for OutEvent {
|
|
|
|
fn from(event: peer_tracker::OutEvent) -> Self {
|
2020-10-15 18:14:39 -04:00
|
|
|
match event {
|
2020-10-19 19:10:28 -04:00
|
|
|
peer_tracker::OutEvent::ConnectionEstablished(id) => {
|
|
|
|
OutEvent::ConnectionEstablished(id)
|
2020-10-15 18:14:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-20 23:41:50 -04:00
|
|
|
impl From<amounts::OutEvent> for OutEvent {
|
|
|
|
fn from(event: amounts::OutEvent) -> Self {
|
2020-10-21 23:12:49 -04:00
|
|
|
match event {
|
|
|
|
amounts::OutEvent::Amounts(amounts) => OutEvent::Amounts(amounts),
|
|
|
|
}
|
2020-10-20 23:41:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<message0::OutEvent> for OutEvent {
|
|
|
|
fn from(event: message0::OutEvent) -> Self {
|
|
|
|
match event {
|
2020-12-22 23:06:43 -05:00
|
|
|
message0::OutEvent::Msg(msg) => OutEvent::Message0(Box::new(msg)),
|
2020-10-20 23:41:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-21 22:23:12 -04:00
|
|
|
impl From<message1::OutEvent> for OutEvent {
|
|
|
|
fn from(event: message1::OutEvent) -> Self {
|
|
|
|
match event {
|
2020-12-22 23:06:43 -05:00
|
|
|
message1::OutEvent::Msg(msg) => OutEvent::Message1(Box::new(msg)),
|
2020-10-21 22:23:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-26 21:11:03 -04:00
|
|
|
impl From<message2::OutEvent> for OutEvent {
|
|
|
|
fn from(event: message2::OutEvent) -> Self {
|
|
|
|
match event {
|
|
|
|
message2::OutEvent::Msg(msg) => OutEvent::Message2(msg),
|
|
|
|
}
|
2020-10-25 23:27:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-29 18:26:52 -04:00
|
|
|
impl From<message3::OutEvent> for OutEvent {
|
|
|
|
fn from(event: message3::OutEvent) -> Self {
|
|
|
|
match event {
|
|
|
|
message3::OutEvent::Msg => OutEvent::Message3,
|
|
|
|
}
|
2020-10-26 22:26:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-15 18:14:39 -04:00
|
|
|
/// A `NetworkBehaviour` that represents an XMR/BTC swap node as Bob.
|
|
|
|
#[derive(NetworkBehaviour)]
|
2020-10-19 19:10:28 -04:00
|
|
|
#[behaviour(out_event = "OutEvent", event_process = false)]
|
2020-10-15 18:14:39 -04:00
|
|
|
#[allow(missing_debug_implementations)]
|
2020-11-29 21:25:11 -05:00
|
|
|
pub struct Behaviour {
|
2020-10-15 18:14:39 -04:00
|
|
|
pt: PeerTracker,
|
2020-10-20 23:41:50 -04:00
|
|
|
amounts: Amounts,
|
2021-01-07 20:33:23 -05:00
|
|
|
message0: message0::Behaviour,
|
|
|
|
message1: message1::Behaviour,
|
|
|
|
message2: message2::Behaviour,
|
|
|
|
message3: message3::Behaviour,
|
2020-10-15 18:14:39 -04:00
|
|
|
}
|
|
|
|
|
2020-11-29 21:25:11 -05:00
|
|
|
impl Behaviour {
|
2020-10-15 18:14:39 -04:00
|
|
|
/// Sends a message to Alice to get current amounts based on `btc`.
|
2020-10-20 23:41:50 -04:00
|
|
|
pub fn request_amounts(&mut self, alice: PeerId, btc: u64) {
|
|
|
|
let btc = ::bitcoin::Amount::from_sat(btc);
|
|
|
|
let _id = self.amounts.request_amounts(alice.clone(), btc);
|
2020-10-30 00:51:46 -04:00
|
|
|
info!("Requesting amounts from: {}", alice);
|
2020-10-15 18:14:39 -04:00
|
|
|
}
|
|
|
|
|
2020-10-21 22:23:12 -04:00
|
|
|
/// Sends Bob's first message to Alice.
|
2020-10-20 23:41:50 -04:00
|
|
|
pub fn send_message0(&mut self, alice: PeerId, msg: bob::Message0) {
|
2020-10-27 20:46:45 -04:00
|
|
|
self.message0.send(alice, msg);
|
2020-10-30 00:51:46 -04:00
|
|
|
debug!("Sent Message0");
|
2020-10-20 23:41:50 -04:00
|
|
|
}
|
|
|
|
|
2020-10-21 22:23:12 -04:00
|
|
|
/// Sends Bob's second message to Alice.
|
|
|
|
pub fn send_message1(&mut self, alice: PeerId, msg: bob::Message1) {
|
2020-10-30 00:51:46 -04:00
|
|
|
self.message1.send(alice, msg);
|
|
|
|
debug!("Sent Message1");
|
2020-10-21 22:23:12 -04:00
|
|
|
}
|
|
|
|
|
2020-10-25 23:27:41 -04:00
|
|
|
/// Sends Bob's third message to Alice.
|
|
|
|
pub fn send_message2(&mut self, alice: PeerId, msg: bob::Message2) {
|
2020-10-30 00:51:46 -04:00
|
|
|
self.message2.send(alice, msg);
|
|
|
|
debug!("Sent Message2");
|
2020-10-25 23:27:41 -04:00
|
|
|
}
|
|
|
|
|
2020-10-26 22:26:40 -04:00
|
|
|
/// Sends Bob's fourth message to Alice.
|
|
|
|
pub fn send_message3(&mut self, alice: PeerId, tx_redeem_encsig: EncryptedSignature) {
|
|
|
|
let msg = bob::Message3 { tx_redeem_encsig };
|
2020-10-30 00:51:46 -04:00
|
|
|
self.message3.send(alice, msg);
|
|
|
|
debug!("Sent Message3");
|
2020-10-26 22:26:40 -04:00
|
|
|
}
|
|
|
|
|
2020-12-18 01:39:04 -05:00
|
|
|
/// Add a known address for the given peer
|
|
|
|
pub fn add_address(&mut self, peer_id: PeerId, address: Multiaddr) {
|
|
|
|
self.pt.add_address(peer_id, address)
|
2020-10-15 18:14:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-29 21:25:11 -05:00
|
|
|
impl Default for Behaviour {
|
|
|
|
fn default() -> Behaviour {
|
2020-10-15 18:14:39 -04:00
|
|
|
Self {
|
|
|
|
pt: PeerTracker::default(),
|
2020-10-21 23:12:49 -04:00
|
|
|
amounts: Amounts::default(),
|
2021-01-07 20:33:23 -05:00
|
|
|
message0: message0::Behaviour::default(),
|
|
|
|
message1: message1::Behaviour::default(),
|
|
|
|
message2: message2::Behaviour::default(),
|
|
|
|
message3: message3::Behaviour::default(),
|
2020-10-15 18:14:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|