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-23 03:33:29 +00:00
|
|
|
#![allow(non_snake_case)]
|
2020-10-15 22:14:39 +00:00
|
|
|
|
2021-01-14 00:35:10 +00:00
|
|
|
use crate::cli::{Command, Options, Resume};
|
2021-01-18 10:57:17 +00:00
|
|
|
use anyhow::{Context, Result};
|
2020-12-04 05:27:17 +00:00
|
|
|
use prettytable::{row, Table};
|
2020-12-23 01:14:36 +00:00
|
|
|
use std::sync::Arc;
|
2020-12-04 05:27:17 +00:00
|
|
|
use structopt::StructOpt;
|
|
|
|
use swap::{
|
2021-01-05 03:08:36 +00:00
|
|
|
bitcoin,
|
|
|
|
config::Config,
|
2021-01-18 10:57:17 +00:00
|
|
|
database::Database,
|
|
|
|
monero,
|
2021-01-19 04:21:40 +00:00
|
|
|
protocol::{alice, bob, bob::Builder},
|
2020-12-15 10:26:02 +00:00
|
|
|
trace::init_tracing,
|
2021-01-20 02:29:46 +00:00
|
|
|
SwapAmounts,
|
2020-12-04 05:27:17 +00:00
|
|
|
};
|
|
|
|
use tracing::{info, log::LevelFilter};
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
2021-01-14 00:35:10 +00:00
|
|
|
mod cli;
|
|
|
|
|
2020-12-04 05:27:17 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate prettytable;
|
2020-10-15 22:14:39 +00:00
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<()> {
|
2021-01-11 06:38:59 +00:00
|
|
|
init_tracing(LevelFilter::Info).expect("initialize tracing");
|
2020-12-04 05:27:17 +00:00
|
|
|
|
|
|
|
let opt = Options::from_args();
|
2021-01-04 09:29:11 +00:00
|
|
|
let config = Config::testnet();
|
2020-12-04 05:27:17 +00:00
|
|
|
|
2021-01-08 01:04:48 +00:00
|
|
|
info!(
|
|
|
|
"Database and Seed will be stored in directory: {}",
|
|
|
|
opt.data_dir
|
|
|
|
);
|
|
|
|
let data_dir = std::path::Path::new(opt.data_dir.as_str()).to_path_buf();
|
2021-01-18 10:24:13 +00:00
|
|
|
let db_path = data_dir.join("database");
|
|
|
|
|
2021-01-08 01:04:48 +00:00
|
|
|
let seed = swap::config::seed::Seed::from_file_or_generate(&data_dir)
|
|
|
|
.expect("Could not retrieve/initialize seed")
|
|
|
|
.into();
|
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,
|
|
|
|
} => {
|
2021-01-18 10:24:13 +00:00
|
|
|
let swap_amounts = SwapAmounts {
|
|
|
|
xmr: send_monero,
|
|
|
|
btc: receive_bitcoin,
|
|
|
|
};
|
|
|
|
|
2021-01-19 03:48:07 +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-07 01:47:21 +00:00
|
|
|
let swap_id = Uuid::new_v4();
|
|
|
|
|
2021-01-18 10:57:17 +00:00
|
|
|
info!(
|
2021-01-20 02:30:35 +00:00
|
|
|
"Swap sending {} and receiving {} started with ID {}",
|
2021-01-18 10:57:17 +00:00
|
|
|
send_monero, receive_bitcoin, swap_id
|
|
|
|
);
|
|
|
|
|
2021-01-19 04:21:40 +00:00
|
|
|
let alice_factory = alice::Builder::new(
|
2021-01-18 10:24:13 +00:00
|
|
|
seed,
|
|
|
|
config,
|
2020-12-15 10:26:02 +00:00
|
|
|
swap_id,
|
2021-01-20 02:36:38 +00:00
|
|
|
Arc::new(bitcoin_wallet),
|
|
|
|
Arc::new(monero_wallet),
|
2021-01-18 10:24:13 +00:00
|
|
|
db_path,
|
|
|
|
listen_addr,
|
2020-12-15 10:26:02 +00:00
|
|
|
)
|
2021-01-18 10:24:13 +00:00
|
|
|
.await;
|
2021-01-19 04:21:40 +00:00
|
|
|
let (swap, mut event_loop) =
|
|
|
|
alice_factory.with_init_params(swap_amounts).build().await?;
|
2021-01-18 10:24:13 +00:00
|
|
|
|
|
|
|
tokio::spawn(async move { event_loop.run().await });
|
|
|
|
alice::run(swap).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,
|
|
|
|
} => {
|
2021-01-18 10:57:17 +00:00
|
|
|
let swap_amounts = SwapAmounts {
|
|
|
|
btc: send_bitcoin,
|
|
|
|
xmr: receive_monero,
|
|
|
|
};
|
|
|
|
|
2021-01-19 03:48:07 +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-10 03:59:09 +00:00
|
|
|
let swap_id = Uuid::new_v4();
|
2021-01-18 10:57:17 +00:00
|
|
|
|
2020-12-10 03:59:09 +00:00
|
|
|
info!(
|
2021-01-20 02:30:35 +00:00
|
|
|
"Swap sending {} and receiving {} started with ID {}",
|
2020-12-10 03:59:09 +00:00
|
|
|
send_bitcoin, receive_monero, swap_id
|
|
|
|
);
|
|
|
|
|
2021-01-19 04:21:40 +00:00
|
|
|
let bob_factory = Builder::new(
|
2021-01-18 10:57:17 +00:00
|
|
|
seed,
|
|
|
|
db_path,
|
2020-12-18 06:39:04 +00:00
|
|
|
swap_id,
|
2021-01-20 02:36:38 +00:00
|
|
|
Arc::new(bitcoin_wallet),
|
|
|
|
Arc::new(monero_wallet),
|
2020-12-18 06:39:04 +00:00
|
|
|
alice_addr,
|
2021-01-18 10:57:17 +00:00
|
|
|
alice_peer_id,
|
2021-01-19 01:43:20 +00:00
|
|
|
config,
|
2021-01-18 10:57:17 +00:00
|
|
|
);
|
2021-01-19 01:43:20 +00:00
|
|
|
let (swap, event_loop) = bob_factory.with_init_params(swap_amounts).build().await?;
|
2021-01-18 10:57:17 +00:00
|
|
|
|
|
|
|
tokio::spawn(async move { event_loop.run().await });
|
|
|
|
bob::run(swap).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"]);
|
|
|
|
|
2021-01-18 10:57:17 +00:00
|
|
|
let db = Database::open(db_path.as_path()).context("Could not open database")?;
|
|
|
|
|
2020-12-04 05:27:17 +00:00
|
|
|
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
|
|
|
}) => {
|
2021-01-19 03:48:07 +00:00
|
|
|
let (bitcoin_wallet, monero_wallet) = setup_wallets(
|
2020-12-21 03:43:44 +00:00
|
|
|
bitcoind_url,
|
|
|
|
bitcoin_wallet_name.as_str(),
|
|
|
|
monero_wallet_rpc_url,
|
|
|
|
config,
|
|
|
|
)
|
|
|
|
.await?;
|
2021-01-18 10:24:13 +00:00
|
|
|
|
2021-01-19 04:21:40 +00:00
|
|
|
let alice_factory = alice::Builder::new(
|
2021-01-18 10:24:13 +00:00
|
|
|
seed,
|
|
|
|
config,
|
2020-12-21 03:43:44 +00:00
|
|
|
swap_id,
|
2021-01-20 02:36:38 +00:00
|
|
|
Arc::new(bitcoin_wallet),
|
|
|
|
Arc::new(monero_wallet),
|
2021-01-18 10:24:13 +00:00
|
|
|
db_path,
|
|
|
|
listen_addr,
|
2020-12-21 03:43:44 +00:00
|
|
|
)
|
2021-01-18 10:24:13 +00:00
|
|
|
.await;
|
2021-01-19 04:21:40 +00:00
|
|
|
let (swap, mut event_loop) = alice_factory.build().await?;
|
2021-01-18 10:24:13 +00:00
|
|
|
|
|
|
|
tokio::spawn(async move { event_loop.run().await });
|
|
|
|
alice::run(swap).await?;
|
2020-12-21 03:43:44 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
}) => {
|
2021-01-19 03:48:07 +00:00
|
|
|
let (bitcoin_wallet, monero_wallet) = setup_wallets(
|
2020-12-21 03:43:44 +00:00
|
|
|
bitcoind_url,
|
|
|
|
bitcoin_wallet_name.as_str(),
|
|
|
|
monero_wallet_rpc_url,
|
|
|
|
config,
|
|
|
|
)
|
|
|
|
.await?;
|
2021-01-18 10:57:17 +00:00
|
|
|
|
2021-01-19 04:21:40 +00:00
|
|
|
let bob_factory = Builder::new(
|
2021-01-18 10:57:17 +00:00
|
|
|
seed,
|
|
|
|
db_path,
|
2020-12-21 03:43:44 +00:00
|
|
|
swap_id,
|
2021-01-20 02:36:38 +00:00
|
|
|
Arc::new(bitcoin_wallet),
|
|
|
|
Arc::new(monero_wallet),
|
2020-12-21 03:43:44 +00:00
|
|
|
alice_addr,
|
2021-01-18 10:57:17 +00:00
|
|
|
alice_peer_id,
|
2021-01-19 01:43:20 +00:00
|
|
|
config,
|
2021-01-18 10:57:17 +00:00
|
|
|
);
|
2021-01-19 04:21:40 +00:00
|
|
|
let (swap, event_loop) = bob_factory.build().await?;
|
2021-01-18 10:57:17 +00:00
|
|
|
|
|
|
|
tokio::spawn(async move { event_loop.run().await });
|
|
|
|
bob::run(swap).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,
|
2021-01-20 02:36:38 +00:00
|
|
|
) -> Result<(swap::bitcoin::Wallet, swap::monero::Wallet)> {
|
2020-12-15 10:26:02 +00:00
|
|
|
let bitcoin_wallet =
|
2021-01-05 03:08:36 +00:00
|
|
|
swap::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
|
|
|
|
);
|
|
|
|
|
2021-01-04 09:29:11 +00:00
|
|
|
let monero_wallet = monero::Wallet::new(monero_wallet_rpc_url, config.monero_network);
|
2020-12-15 10:26:02 +00:00
|
|
|
let monero_balance = monero_wallet.get_balance().await?;
|
|
|
|
info!(
|
|
|
|
"Connection to Monero wallet succeeded, balance: {}",
|
|
|
|
monero_balance
|
|
|
|
);
|
|
|
|
|
2021-01-19 03:48:07 +00:00
|
|
|
Ok((bitcoin_wallet, monero_wallet))
|
2020-12-15 10:26:02 +00:00
|
|
|
}
|