2020-10-15 22:14:39 +00:00
|
|
|
#![warn(
|
|
|
|
unused_extern_crates,
|
|
|
|
missing_debug_implementations,
|
|
|
|
missing_copy_implementations,
|
|
|
|
rust_2018_idioms,
|
|
|
|
clippy::cast_possible_truncation,
|
|
|
|
clippy::cast_sign_loss,
|
|
|
|
clippy::fallible_impl_from,
|
|
|
|
clippy::cast_precision_loss,
|
|
|
|
clippy::cast_possible_wrap,
|
|
|
|
clippy::dbg_macro
|
|
|
|
)]
|
|
|
|
#![forbid(unsafe_code)]
|
|
|
|
|
2020-12-17 08:10:24 +00:00
|
|
|
use anyhow::{Context, Result};
|
2020-12-18 06:39:04 +00:00
|
|
|
use libp2p::{core::Multiaddr, PeerId};
|
2020-12-04 05:27:17 +00:00
|
|
|
use prettytable::{row, Table};
|
|
|
|
use rand::rngs::OsRng;
|
2020-12-15 10:26:02 +00:00
|
|
|
use std::{convert::TryFrom, sync::Arc};
|
2020-12-04 05:27:17 +00:00
|
|
|
use structopt::StructOpt;
|
|
|
|
use swap::{
|
2020-12-15 10:26:02 +00:00
|
|
|
alice,
|
|
|
|
alice::swap::AliceState,
|
|
|
|
bitcoin, bob,
|
|
|
|
bob::swap::BobState,
|
2020-12-21 03:43:44 +00:00
|
|
|
cli::{Command, Options, Resume},
|
2020-12-15 10:26:02 +00:00
|
|
|
monero,
|
|
|
|
network::transport::build,
|
|
|
|
storage::Database,
|
|
|
|
trace::init_tracing,
|
|
|
|
SwapAmounts,
|
2020-12-04 05:27:17 +00:00
|
|
|
};
|
|
|
|
use tracing::{info, log::LevelFilter};
|
|
|
|
use uuid::Uuid;
|
|
|
|
use xmr_btc::{alice::State0, config::Config, cross_curve_dleq};
|
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
extern crate prettytable;
|
2020-10-15 22:14:39 +00:00
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<()> {
|
2020-12-04 05:27:17 +00:00
|
|
|
init_tracing(LevelFilter::Trace).expect("initialize tracing");
|
|
|
|
|
|
|
|
let opt = Options::from_args();
|
|
|
|
|
|
|
|
let config = Config::mainnet();
|
|
|
|
|
2020-12-15 10:26:02 +00:00
|
|
|
info!("Database: {}", opt.db_path);
|
2020-12-17 08:10:24 +00:00
|
|
|
let db = Database::open(std::path::Path::new(opt.db_path.as_str()))
|
|
|
|
.context("Could not open database")?;
|
2020-12-15 10:26:02 +00:00
|
|
|
|
|
|
|
match opt.cmd {
|
|
|
|
Command::SellXmr {
|
2020-12-04 05:27:17 +00:00
|
|
|
bitcoind_url,
|
|
|
|
bitcoin_wallet_name,
|
|
|
|
monero_wallet_rpc_url,
|
|
|
|
listen_addr,
|
|
|
|
send_monero,
|
|
|
|
receive_bitcoin,
|
|
|
|
} => {
|
2020-12-15 10:26:02 +00:00
|
|
|
let (bitcoin_wallet, monero_wallet) = setup_wallets(
|
2020-12-04 05:27:17 +00:00
|
|
|
bitcoind_url,
|
2020-12-15 10:26:02 +00:00
|
|
|
bitcoin_wallet_name.as_str(),
|
|
|
|
monero_wallet_rpc_url,
|
|
|
|
config,
|
2020-12-04 05:27:17 +00:00
|
|
|
)
|
2020-12-15 10:26:02 +00:00
|
|
|
.await?;
|
2020-12-04 05:27:17 +00:00
|
|
|
|
|
|
|
let amounts = SwapAmounts {
|
|
|
|
btc: receive_bitcoin,
|
|
|
|
xmr: send_monero,
|
|
|
|
};
|
|
|
|
|
2020-12-15 10:26:02 +00:00
|
|
|
let alice_state = {
|
2020-12-04 05:27:17 +00:00
|
|
|
let rng = &mut OsRng;
|
|
|
|
let a = bitcoin::SecretKey::new_random(rng);
|
|
|
|
let s_a = cross_curve_dleq::Scalar::random(rng);
|
|
|
|
let v_a = xmr_btc::monero::PrivateViewKey::new_random(rng);
|
2020-12-17 08:10:24 +00:00
|
|
|
let redeem_address = bitcoin_wallet.as_ref().new_address().await?;
|
2020-12-04 05:27:17 +00:00
|
|
|
let punish_address = redeem_address.clone();
|
|
|
|
let state0 = State0::new(
|
|
|
|
a,
|
|
|
|
s_a,
|
|
|
|
v_a,
|
|
|
|
amounts.btc,
|
|
|
|
amounts.xmr,
|
|
|
|
config.bitcoin_refund_timelock,
|
|
|
|
config.bitcoin_punish_timelock,
|
|
|
|
redeem_address,
|
|
|
|
punish_address,
|
|
|
|
);
|
|
|
|
|
2020-12-15 10:26:02 +00:00
|
|
|
AliceState::Started { amounts, state0 }
|
2020-12-04 05:27:17 +00:00
|
|
|
};
|
|
|
|
|
2020-12-07 01:47:21 +00:00
|
|
|
let swap_id = Uuid::new_v4();
|
2020-12-10 03:59:09 +00:00
|
|
|
info!(
|
|
|
|
"Swap sending {} and receiving {} started with ID {}",
|
|
|
|
send_monero, receive_bitcoin, swap_id
|
|
|
|
);
|
2020-12-07 01:47:21 +00:00
|
|
|
|
2020-12-15 10:26:02 +00:00
|
|
|
alice_swap(
|
|
|
|
swap_id,
|
2020-12-04 05:27:17 +00:00
|
|
|
alice_state,
|
2020-12-15 10:26:02 +00:00
|
|
|
listen_addr,
|
|
|
|
bitcoin_wallet,
|
|
|
|
monero_wallet,
|
2020-12-04 05:27:17 +00:00
|
|
|
config,
|
2020-12-07 01:47:21 +00:00
|
|
|
db,
|
2020-12-15 10:26:02 +00:00
|
|
|
)
|
|
|
|
.await?;
|
2020-12-04 05:27:17 +00:00
|
|
|
}
|
2020-12-15 10:26:02 +00:00
|
|
|
Command::BuyXmr {
|
2020-12-18 06:39:04 +00:00
|
|
|
alice_peer_id,
|
2020-12-04 05:27:17 +00:00
|
|
|
alice_addr,
|
|
|
|
bitcoind_url,
|
|
|
|
bitcoin_wallet_name,
|
|
|
|
monero_wallet_rpc_url,
|
|
|
|
send_bitcoin,
|
|
|
|
receive_monero,
|
|
|
|
} => {
|
2020-12-15 10:26:02 +00:00
|
|
|
let (bitcoin_wallet, monero_wallet) = setup_wallets(
|
2020-12-04 05:27:17 +00:00
|
|
|
bitcoind_url,
|
2020-12-15 10:26:02 +00:00
|
|
|
bitcoin_wallet_name.as_str(),
|
|
|
|
monero_wallet_rpc_url,
|
|
|
|
config,
|
2020-12-04 05:27:17 +00:00
|
|
|
)
|
2020-12-15 10:26:02 +00:00
|
|
|
.await?;
|
2020-12-04 05:27:17 +00:00
|
|
|
|
2020-12-17 08:10:24 +00:00
|
|
|
let refund_address = bitcoin_wallet.new_address().await?;
|
2020-12-04 05:27:17 +00:00
|
|
|
let state0 = xmr_btc::bob::State0::new(
|
|
|
|
&mut OsRng,
|
|
|
|
send_bitcoin,
|
|
|
|
receive_monero,
|
|
|
|
config.bitcoin_refund_timelock,
|
|
|
|
config.bitcoin_punish_timelock,
|
|
|
|
refund_address,
|
|
|
|
);
|
|
|
|
|
|
|
|
let amounts = SwapAmounts {
|
|
|
|
btc: send_bitcoin,
|
|
|
|
xmr: receive_monero,
|
|
|
|
};
|
|
|
|
|
2020-12-22 02:47:05 +00:00
|
|
|
let bob_state = BobState::Started { state0, amounts };
|
2020-12-04 05:27:17 +00:00
|
|
|
|
2020-12-10 03:59:09 +00:00
|
|
|
let swap_id = Uuid::new_v4();
|
|
|
|
info!(
|
|
|
|
"Swap sending {} and receiving {} started with ID {}",
|
|
|
|
send_bitcoin, receive_monero, swap_id
|
|
|
|
);
|
|
|
|
|
2020-12-18 06:39:04 +00:00
|
|
|
bob_swap(
|
|
|
|
swap_id,
|
|
|
|
bob_state,
|
|
|
|
bitcoin_wallet,
|
|
|
|
monero_wallet,
|
|
|
|
db,
|
|
|
|
alice_peer_id,
|
|
|
|
alice_addr,
|
|
|
|
)
|
|
|
|
.await?;
|
2020-12-04 05:27:17 +00:00
|
|
|
}
|
2020-12-15 10:26:02 +00:00
|
|
|
Command::History => {
|
2020-12-04 05:27:17 +00:00
|
|
|
let mut table = Table::new();
|
|
|
|
|
|
|
|
table.add_row(row!["SWAP ID", "STATE"]);
|
|
|
|
|
|
|
|
for (swap_id, state) in db.all()? {
|
|
|
|
table.add_row(row![swap_id, state]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print the table to stdout
|
|
|
|
table.printstd();
|
|
|
|
}
|
2020-12-21 03:43:44 +00:00
|
|
|
Command::Resume(Resume::SellXmr {
|
2020-12-15 10:26:02 +00:00
|
|
|
swap_id,
|
|
|
|
bitcoind_url,
|
|
|
|
bitcoin_wallet_name,
|
|
|
|
monero_wallet_rpc_url,
|
|
|
|
listen_addr,
|
2020-12-21 03:43:44 +00:00
|
|
|
}) => {
|
|
|
|
let db_swap = db.get_state(swap_id)?;
|
|
|
|
|
|
|
|
let alice_state = AliceState::try_from(db_swap.clone())?;
|
|
|
|
let (bitcoin_wallet, monero_wallet) = setup_wallets(
|
|
|
|
bitcoind_url,
|
|
|
|
bitcoin_wallet_name.as_str(),
|
|
|
|
monero_wallet_rpc_url,
|
|
|
|
config,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
alice_swap(
|
|
|
|
swap_id,
|
|
|
|
alice_state,
|
|
|
|
listen_addr,
|
|
|
|
bitcoin_wallet,
|
|
|
|
monero_wallet,
|
|
|
|
config,
|
|
|
|
db,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
Command::Resume(Resume::BuyXmr {
|
|
|
|
swap_id,
|
|
|
|
bitcoind_url,
|
|
|
|
bitcoin_wallet_name,
|
|
|
|
monero_wallet_rpc_url,
|
2020-12-18 06:39:04 +00:00
|
|
|
alice_peer_id,
|
|
|
|
alice_addr,
|
2020-12-21 03:43:44 +00:00
|
|
|
}) => {
|
2020-12-15 10:26:02 +00:00
|
|
|
let db_swap = db.get_state(swap_id)?;
|
|
|
|
|
2020-12-21 03:43:44 +00:00
|
|
|
let bob_state = BobState::try_from(db_swap)?;
|
|
|
|
|
|
|
|
let (bitcoin_wallet, monero_wallet) = setup_wallets(
|
|
|
|
bitcoind_url,
|
|
|
|
bitcoin_wallet_name.as_str(),
|
|
|
|
monero_wallet_rpc_url,
|
|
|
|
config,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
bob_swap(
|
|
|
|
swap_id,
|
|
|
|
bob_state,
|
|
|
|
bitcoin_wallet,
|
|
|
|
monero_wallet,
|
|
|
|
db,
|
|
|
|
alice_peer_id,
|
|
|
|
alice_addr,
|
|
|
|
)
|
|
|
|
.await?;
|
2020-12-15 10:26:02 +00:00
|
|
|
}
|
2020-12-21 03:43:44 +00:00
|
|
|
};
|
2020-12-04 05:27:17 +00:00
|
|
|
|
|
|
|
Ok(())
|
2020-10-15 22:14:39 +00:00
|
|
|
}
|
2020-12-15 10:26:02 +00:00
|
|
|
|
|
|
|
async fn setup_wallets(
|
|
|
|
bitcoind_url: url::Url,
|
|
|
|
bitcoin_wallet_name: &str,
|
|
|
|
monero_wallet_rpc_url: url::Url,
|
|
|
|
config: Config,
|
|
|
|
) -> Result<(Arc<bitcoin::Wallet>, Arc<monero::Wallet>)> {
|
|
|
|
let bitcoin_wallet =
|
2020-12-17 08:10:24 +00:00
|
|
|
bitcoin::Wallet::new(bitcoin_wallet_name, bitcoind_url, config.bitcoin_network).await?;
|
2020-12-15 10:26:02 +00:00
|
|
|
let bitcoin_balance = bitcoin_wallet.balance().await?;
|
|
|
|
info!(
|
|
|
|
"Connection to Bitcoin wallet succeeded, balance: {}",
|
|
|
|
bitcoin_balance
|
|
|
|
);
|
|
|
|
let bitcoin_wallet = Arc::new(bitcoin_wallet);
|
|
|
|
|
|
|
|
let monero_wallet = monero::Wallet::new(monero_wallet_rpc_url);
|
|
|
|
let monero_balance = monero_wallet.get_balance().await?;
|
|
|
|
info!(
|
|
|
|
"Connection to Monero wallet succeeded, balance: {}",
|
|
|
|
monero_balance
|
|
|
|
);
|
|
|
|
let monero_wallet = Arc::new(monero_wallet);
|
|
|
|
|
|
|
|
Ok((bitcoin_wallet, monero_wallet))
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn alice_swap(
|
|
|
|
swap_id: Uuid,
|
|
|
|
state: AliceState,
|
|
|
|
listen_addr: Multiaddr,
|
|
|
|
bitcoin_wallet: Arc<bitcoin::Wallet>,
|
|
|
|
monero_wallet: Arc<monero::Wallet>,
|
|
|
|
config: Config,
|
|
|
|
db: Database,
|
|
|
|
) -> Result<AliceState> {
|
|
|
|
let alice_behaviour = alice::Behaviour::default();
|
|
|
|
|
|
|
|
let alice_peer_id = alice_behaviour.peer_id();
|
|
|
|
info!("Own Peer-ID: {}", alice_peer_id);
|
|
|
|
|
|
|
|
let alice_transport = build(alice_behaviour.identity())?;
|
|
|
|
|
|
|
|
let (mut event_loop, handle) =
|
|
|
|
alice::event_loop::EventLoop::new(alice_transport, alice_behaviour, listen_addr)?;
|
|
|
|
|
|
|
|
let swap = alice::swap::swap(
|
|
|
|
state,
|
|
|
|
handle,
|
|
|
|
bitcoin_wallet.clone(),
|
|
|
|
monero_wallet.clone(),
|
|
|
|
config,
|
|
|
|
swap_id,
|
|
|
|
db,
|
|
|
|
);
|
|
|
|
|
2020-12-17 08:10:24 +00:00
|
|
|
tokio::spawn(async move { event_loop.run().await });
|
|
|
|
swap.await
|
2020-12-15 10:26:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn bob_swap(
|
|
|
|
swap_id: Uuid,
|
|
|
|
state: BobState,
|
|
|
|
bitcoin_wallet: Arc<bitcoin::Wallet>,
|
|
|
|
monero_wallet: Arc<monero::Wallet>,
|
|
|
|
db: Database,
|
2020-12-18 06:39:04 +00:00
|
|
|
alice_peer_id: PeerId,
|
|
|
|
alice_addr: Multiaddr,
|
2020-12-15 10:26:02 +00:00
|
|
|
) -> Result<BobState> {
|
|
|
|
let bob_behaviour = bob::Behaviour::default();
|
|
|
|
let bob_transport = build(bob_behaviour.identity())?;
|
|
|
|
|
2020-12-22 02:47:05 +00:00
|
|
|
let (event_loop, handle) =
|
|
|
|
bob::event_loop::EventLoop::new(bob_transport, bob_behaviour, alice_peer_id, alice_addr)?;
|
2020-12-15 10:26:02 +00:00
|
|
|
|
|
|
|
let swap = bob::swap::swap(
|
|
|
|
state,
|
|
|
|
handle,
|
|
|
|
db,
|
|
|
|
bitcoin_wallet.clone(),
|
|
|
|
monero_wallet.clone(),
|
|
|
|
OsRng,
|
|
|
|
swap_id,
|
|
|
|
);
|
|
|
|
|
2020-12-17 08:10:24 +00:00
|
|
|
tokio::spawn(async move { event_loop.run().await });
|
|
|
|
swap.await
|
2020-12-15 10:26:02 +00:00
|
|
|
}
|