2020-10-15 22:14:39 +00:00
|
|
|
#![warn(
|
|
|
|
unused_extern_crates,
|
|
|
|
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-02-04 05:42:14 +00:00
|
|
|
use anyhow::{Context, Result};
|
|
|
|
use prettytable::{row, Table};
|
2021-02-24 07:08:25 +00:00
|
|
|
use reqwest::Url;
|
2021-02-26 03:31:09 +00:00
|
|
|
use std::{path::Path, sync::Arc, time::Duration};
|
2021-02-04 05:42:14 +00:00
|
|
|
use structopt::StructOpt;
|
|
|
|
use swap::{
|
|
|
|
bitcoin,
|
2021-03-01 04:35:45 +00:00
|
|
|
bitcoin::{Amount, TxLock},
|
2021-02-10 22:59:24 +00:00
|
|
|
cli::{
|
2021-02-28 23:53:43 +00:00
|
|
|
command::{Arguments, Command},
|
2021-02-24 23:53:50 +00:00
|
|
|
config::{read_config, Config},
|
2021-01-27 02:33:32 +00:00
|
|
|
},
|
2021-02-04 05:42:14 +00:00
|
|
|
database::Database,
|
|
|
|
execution_params,
|
2021-02-28 23:53:43 +00:00
|
|
|
execution_params::GetExecutionParams,
|
2021-02-04 05:42:14 +00:00
|
|
|
monero,
|
2021-02-24 23:34:22 +00:00
|
|
|
monero::{CreateWallet, OpenWallet},
|
2021-02-04 05:42:14 +00:00
|
|
|
protocol::{
|
|
|
|
bob,
|
|
|
|
bob::{cancel::CancelError, Builder},
|
|
|
|
},
|
2021-02-10 22:57:17 +00:00
|
|
|
seed::Seed,
|
2021-01-27 02:33:32 +00:00
|
|
|
};
|
2021-03-01 01:19:05 +00:00
|
|
|
use tracing::{debug, error, info, warn, Level};
|
2021-02-26 05:22:24 +00:00
|
|
|
use tracing_subscriber::FmtSubscriber;
|
2021-01-21 02:43:25 +00:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
2020-12-04 05:27:17 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate prettytable;
|
2020-10-15 22:14:39 +00:00
|
|
|
|
2021-02-10 04:06:26 +00:00
|
|
|
const MONERO_BLOCKCHAIN_MONITORING_WALLET_NAME: &str = "swap-tool-blockchain-monitoring-wallet";
|
2021-02-09 04:34:12 +00:00
|
|
|
|
2020-10-15 22:14:39 +00:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<()> {
|
2021-03-01 01:19:05 +00:00
|
|
|
let args = Arguments::from_args();
|
|
|
|
|
2021-02-26 05:22:24 +00:00
|
|
|
let is_terminal = atty::is(atty::Stream::Stderr);
|
2021-03-01 01:19:05 +00:00
|
|
|
let base_subscriber = |level| {
|
|
|
|
FmtSubscriber::builder()
|
|
|
|
.with_writer(std::io::stderr)
|
|
|
|
.with_ansi(is_terminal)
|
|
|
|
.with_target(false)
|
|
|
|
.with_env_filter(format!("swap={}", level))
|
|
|
|
};
|
2020-12-04 05:27:17 +00:00
|
|
|
|
2021-03-01 01:19:05 +00:00
|
|
|
if args.debug {
|
|
|
|
let subscriber = base_subscriber(Level::DEBUG)
|
|
|
|
.with_timer(tracing_subscriber::fmt::time::ChronoLocal::with_format(
|
|
|
|
"%F %T".to_owned(),
|
|
|
|
))
|
|
|
|
.finish();
|
|
|
|
|
|
|
|
tracing::subscriber::set_global_default(subscriber)?;
|
|
|
|
} else {
|
|
|
|
let subscriber = base_subscriber(Level::INFO)
|
|
|
|
.without_time()
|
|
|
|
.with_level(false)
|
|
|
|
.finish();
|
|
|
|
|
|
|
|
tracing::subscriber::set_global_default(subscriber)?;
|
|
|
|
}
|
2021-01-27 02:33:32 +00:00
|
|
|
|
2021-03-01 01:09:59 +00:00
|
|
|
let config = match args.config {
|
2021-02-24 23:53:50 +00:00
|
|
|
Some(config_path) => read_config(config_path)??,
|
|
|
|
None => Config::testnet(),
|
2021-01-27 02:33:32 +00:00
|
|
|
};
|
2020-12-04 05:27:17 +00:00
|
|
|
|
2021-02-26 06:03:07 +00:00
|
|
|
debug!(
|
|
|
|
"Database and seed will be stored in {}",
|
2021-02-11 04:21:34 +00:00
|
|
|
config.data.dir.display()
|
2021-01-08 01:04:48 +00:00
|
|
|
);
|
2021-01-18 10:24:13 +00:00
|
|
|
|
2021-02-11 04:21:34 +00:00
|
|
|
let db = Database::open(config.data.dir.join("database").as_path())
|
|
|
|
.context("Could not open database")?;
|
|
|
|
|
2021-02-01 23:39:34 +00:00
|
|
|
let wallet_data_dir = config.data.dir.join("wallet");
|
2021-02-11 04:21:34 +00:00
|
|
|
let seed =
|
|
|
|
Seed::from_file_or_generate(&config.data.dir).expect("Could not retrieve/initialize seed");
|
2020-12-15 10:26:02 +00:00
|
|
|
|
2021-01-27 02:33:32 +00:00
|
|
|
// hardcode to testnet/stagenet
|
|
|
|
let bitcoin_network = bitcoin::Network::Testnet;
|
|
|
|
let monero_network = monero::Network::Stagenet;
|
2021-01-29 06:27:50 +00:00
|
|
|
let execution_params = execution_params::Testnet::get_execution_params();
|
2021-01-27 02:33:32 +00:00
|
|
|
|
2021-02-24 07:08:25 +00:00
|
|
|
let monero_wallet_rpc = monero::WalletRpc::new(config.data.dir.join("monero")).await?;
|
|
|
|
|
|
|
|
let monero_wallet_rpc_process = monero_wallet_rpc
|
|
|
|
.run(monero_network, "stagenet.community.xmr.to")
|
|
|
|
.await?;
|
|
|
|
|
2021-03-01 01:09:59 +00:00
|
|
|
match args.cmd.unwrap_or_default() {
|
2021-02-28 23:53:43 +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,
|
2021-02-28 23:53:43 +00:00
|
|
|
} => {
|
|
|
|
let (bitcoin_wallet, monero_wallet) = init_wallets(
|
2021-02-09 06:23:13 +00:00
|
|
|
config,
|
|
|
|
bitcoin_network,
|
2021-02-26 04:24:13 +00:00
|
|
|
&wallet_data_dir,
|
2021-02-28 23:53:43 +00:00
|
|
|
monero_network,
|
2021-02-09 06:23:13 +00:00
|
|
|
seed,
|
2021-02-24 07:08:25 +00:00
|
|
|
monero_wallet_rpc_process.endpoint(),
|
2021-02-09 06:23:13 +00:00
|
|
|
)
|
|
|
|
.await?;
|
2021-02-28 23:53:43 +00:00
|
|
|
|
|
|
|
let swap_id = Uuid::new_v4();
|
|
|
|
|
|
|
|
// TODO: Also wait for more funds if balance < dust
|
|
|
|
if bitcoin_wallet.balance().await? == Amount::ZERO {
|
2021-03-01 01:19:05 +00:00
|
|
|
info!(
|
|
|
|
"Please deposit BTC to {}",
|
2021-02-28 23:53:43 +00:00
|
|
|
bitcoin_wallet.new_address().await?
|
|
|
|
);
|
|
|
|
|
|
|
|
while bitcoin_wallet.balance().await? == Amount::ZERO {
|
|
|
|
bitcoin_wallet.sync_wallet().await?;
|
|
|
|
|
|
|
|
tokio::time::sleep(Duration::from_secs(1)).await;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug!("Received {}", bitcoin_wallet.balance().await?);
|
2021-03-01 00:24:27 +00:00
|
|
|
} else {
|
2021-03-01 01:19:05 +00:00
|
|
|
info!(
|
2021-03-01 00:24:27 +00:00
|
|
|
"Still got {} left in wallet, swapping ...",
|
|
|
|
bitcoin_wallet.balance().await?
|
|
|
|
);
|
2021-02-28 23:53:43 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 04:35:45 +00:00
|
|
|
let send_bitcoin = bitcoin_wallet.max_giveable(TxLock::script_size()).await?;
|
2021-02-28 23:53:43 +00:00
|
|
|
|
|
|
|
let bob_factory = Builder::new(
|
2021-01-18 10:57:17 +00:00
|
|
|
seed,
|
2021-02-11 04:21:34 +00:00
|
|
|
db,
|
2021-02-28 23:53:43 +00:00
|
|
|
swap_id,
|
|
|
|
Arc::new(bitcoin_wallet),
|
|
|
|
Arc::new(monero_wallet),
|
|
|
|
alice_addr,
|
|
|
|
alice_peer_id,
|
2021-01-27 02:33:32 +00:00
|
|
|
execution_params,
|
2021-02-28 23:53:43 +00:00
|
|
|
);
|
|
|
|
let (swap, event_loop) = bob_factory.with_init_params(send_bitcoin).build().await?;
|
|
|
|
|
2021-02-26 05:11:14 +00:00
|
|
|
let handle = tokio::spawn(async move { event_loop.run().await });
|
|
|
|
let swap = bob::run(swap);
|
|
|
|
tokio::select! {
|
|
|
|
event_loop_result = handle => {
|
|
|
|
event_loop_result??;
|
|
|
|
},
|
|
|
|
swap_result = swap => {
|
|
|
|
swap_result?;
|
|
|
|
}
|
|
|
|
}
|
2020-12-04 05:27:17 +00:00
|
|
|
}
|
2021-02-28 23:53:43 +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();
|
|
|
|
}
|
2021-02-28 23:53:43 +00:00
|
|
|
Command::Resume {
|
2020-12-21 03:43:44 +00:00
|
|
|
swap_id,
|
2020-12-18 06:39:04 +00:00
|
|
|
alice_peer_id,
|
|
|
|
alice_addr,
|
2021-02-28 23:53:43 +00:00
|
|
|
} => {
|
2021-02-09 06:23:13 +00:00
|
|
|
let (bitcoin_wallet, monero_wallet) = init_wallets(
|
|
|
|
config,
|
|
|
|
bitcoin_network,
|
|
|
|
&wallet_data_dir,
|
|
|
|
monero_network,
|
|
|
|
seed,
|
2021-02-24 07:08:25 +00:00
|
|
|
monero_wallet_rpc_process.endpoint(),
|
2021-02-09 06:23:13 +00:00
|
|
|
)
|
|
|
|
.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,
|
2021-02-11 04:21:34 +00:00
|
|
|
db,
|
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-27 02:33:32 +00:00
|
|
|
execution_params,
|
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-02-26 05:11:14 +00:00
|
|
|
let handle = tokio::spawn(async move { event_loop.run().await });
|
|
|
|
let swap = bob::run(swap);
|
|
|
|
tokio::select! {
|
|
|
|
event_loop_result = handle => {
|
|
|
|
event_loop_result??;
|
|
|
|
},
|
|
|
|
swap_result = swap => {
|
|
|
|
swap_result?;
|
|
|
|
}
|
|
|
|
}
|
2020-12-15 10:26:02 +00:00
|
|
|
}
|
2021-02-28 23:53:43 +00:00
|
|
|
Command::Cancel {
|
2021-02-01 05:10:43 +00:00
|
|
|
swap_id,
|
|
|
|
alice_peer_id,
|
|
|
|
alice_addr,
|
2021-02-01 11:32:54 +00:00
|
|
|
force,
|
2021-02-28 23:53:43 +00:00
|
|
|
} => {
|
2021-02-01 05:10:43 +00:00
|
|
|
// TODO: Optimization: Only init the Bitcoin wallet, Monero wallet unnecessary
|
2021-02-09 06:23:13 +00:00
|
|
|
let (bitcoin_wallet, monero_wallet) = init_wallets(
|
|
|
|
config,
|
|
|
|
bitcoin_network,
|
|
|
|
&wallet_data_dir,
|
|
|
|
monero_network,
|
|
|
|
seed,
|
2021-02-24 07:08:25 +00:00
|
|
|
monero_wallet_rpc_process.endpoint(),
|
2021-02-09 06:23:13 +00:00
|
|
|
)
|
|
|
|
.await?;
|
2021-02-01 05:10:43 +00:00
|
|
|
|
|
|
|
let bob_factory = Builder::new(
|
|
|
|
seed,
|
2021-02-11 04:21:34 +00:00
|
|
|
db,
|
2021-02-01 05:10:43 +00:00
|
|
|
swap_id,
|
|
|
|
Arc::new(bitcoin_wallet),
|
|
|
|
Arc::new(monero_wallet),
|
|
|
|
alice_addr,
|
|
|
|
alice_peer_id,
|
|
|
|
execution_params,
|
|
|
|
);
|
|
|
|
let (swap, event_loop) = bob_factory.build().await?;
|
2021-02-26 05:11:14 +00:00
|
|
|
let handle = tokio::spawn(async move { event_loop.run().await });
|
2021-02-01 05:10:43 +00:00
|
|
|
|
2021-02-26 05:11:14 +00:00
|
|
|
let cancel = bob::cancel(
|
2021-02-01 11:32:54 +00:00
|
|
|
swap.swap_id,
|
|
|
|
swap.state,
|
|
|
|
swap.bitcoin_wallet,
|
|
|
|
swap.db,
|
|
|
|
force,
|
2021-02-26 05:11:14 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
tokio::select! {
|
|
|
|
event_loop_result = handle => {
|
|
|
|
event_loop_result??;
|
|
|
|
},
|
|
|
|
cancel_result = cancel => {
|
|
|
|
match cancel_result? {
|
|
|
|
Ok((txid, _)) => {
|
2021-02-26 06:03:07 +00:00
|
|
|
debug!("Cancel transaction successfully published with id {}", txid)
|
2021-02-26 05:11:14 +00:00
|
|
|
}
|
|
|
|
Err(CancelError::CancelTimelockNotExpiredYet) => error!(
|
|
|
|
"The Cancel Transaction cannot be published yet, \
|
|
|
|
because the timelock has not expired. Please try again later."
|
|
|
|
),
|
|
|
|
Err(CancelError::CancelTxAlreadyPublished) => {
|
|
|
|
warn!("The Cancel Transaction has already been published.")
|
|
|
|
}
|
|
|
|
}
|
2021-02-01 05:25:33 +00:00
|
|
|
}
|
2021-02-01 05:10:43 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-28 23:53:43 +00:00
|
|
|
Command::Refund {
|
2021-02-01 05:25:33 +00:00
|
|
|
swap_id,
|
|
|
|
alice_peer_id,
|
|
|
|
alice_addr,
|
2021-02-01 11:32:54 +00:00
|
|
|
force,
|
2021-02-28 23:53:43 +00:00
|
|
|
} => {
|
2021-02-09 06:23:13 +00:00
|
|
|
let (bitcoin_wallet, monero_wallet) = init_wallets(
|
|
|
|
config,
|
|
|
|
bitcoin_network,
|
|
|
|
&wallet_data_dir,
|
|
|
|
monero_network,
|
|
|
|
seed,
|
2021-02-24 07:08:25 +00:00
|
|
|
monero_wallet_rpc_process.endpoint(),
|
2021-02-09 06:23:13 +00:00
|
|
|
)
|
|
|
|
.await?;
|
2021-02-01 05:25:33 +00:00
|
|
|
|
|
|
|
// TODO: Optimize to only use the Bitcoin wallet, Monero wallet is unnecessary
|
|
|
|
let bob_factory = Builder::new(
|
|
|
|
seed,
|
2021-02-11 04:21:34 +00:00
|
|
|
db,
|
2021-02-01 05:25:33 +00:00
|
|
|
swap_id,
|
|
|
|
Arc::new(bitcoin_wallet),
|
|
|
|
Arc::new(monero_wallet),
|
|
|
|
alice_addr,
|
|
|
|
alice_peer_id,
|
|
|
|
execution_params,
|
|
|
|
);
|
|
|
|
let (swap, event_loop) = bob_factory.build().await?;
|
|
|
|
|
2021-02-26 05:11:14 +00:00
|
|
|
let handle = tokio::spawn(async move { event_loop.run().await });
|
|
|
|
let refund = bob::refund(
|
2021-02-01 05:25:33 +00:00
|
|
|
swap.swap_id,
|
|
|
|
swap.state,
|
|
|
|
swap.execution_params,
|
|
|
|
swap.bitcoin_wallet,
|
|
|
|
swap.db,
|
2021-02-01 11:32:54 +00:00
|
|
|
force,
|
2021-02-26 05:11:14 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
tokio::select! {
|
|
|
|
event_loop_result = handle => {
|
|
|
|
event_loop_result??;
|
|
|
|
},
|
|
|
|
refund_result = refund => {
|
|
|
|
refund_result??;
|
|
|
|
}
|
|
|
|
}
|
2021-02-01 05:25:33 +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
|
|
|
|
2021-01-27 02:33:32 +00:00
|
|
|
async fn init_wallets(
|
2021-02-11 04:21:34 +00:00
|
|
|
config: Config,
|
2021-01-27 02:33:32 +00:00
|
|
|
bitcoin_network: bitcoin::Network,
|
2021-02-01 23:39:34 +00:00
|
|
|
bitcoin_wallet_data_dir: &Path,
|
2021-01-27 02:33:32 +00:00
|
|
|
monero_network: monero::Network,
|
2021-02-09 06:23:13 +00:00
|
|
|
seed: Seed,
|
2021-02-24 07:08:25 +00:00
|
|
|
monero_wallet_rpc_url: Url,
|
2021-01-21 03:00:37 +00:00
|
|
|
) -> Result<(bitcoin::Wallet, monero::Wallet)> {
|
2021-01-27 02:33:32 +00:00
|
|
|
let bitcoin_wallet = bitcoin::Wallet::new(
|
2021-02-01 23:39:34 +00:00
|
|
|
config.bitcoin.electrum_rpc_url,
|
|
|
|
config.bitcoin.electrum_http_url,
|
2021-01-27 02:33:32 +00:00
|
|
|
bitcoin_network,
|
2021-02-01 23:39:34 +00:00
|
|
|
bitcoin_wallet_data_dir,
|
2021-03-02 06:10:29 +00:00
|
|
|
seed.extended_private_key(bitcoin_network)?,
|
2021-01-27 02:33:32 +00:00
|
|
|
)
|
|
|
|
.await?;
|
2021-02-01 23:39:34 +00:00
|
|
|
|
|
|
|
bitcoin_wallet
|
|
|
|
.sync_wallet()
|
|
|
|
.await
|
2021-02-26 06:04:51 +00:00
|
|
|
.context("failed to sync balance of bitcoin wallet")?;
|
2020-12-15 10:26:02 +00:00
|
|
|
|
2021-02-24 07:00:07 +00:00
|
|
|
let monero_wallet = monero::Wallet::new(
|
2021-02-24 07:08:25 +00:00
|
|
|
monero_wallet_rpc_url.clone(),
|
2021-02-24 07:00:07 +00:00
|
|
|
monero_network,
|
|
|
|
MONERO_BLOCKCHAIN_MONITORING_WALLET_NAME.to_string(),
|
|
|
|
);
|
2021-02-09 04:34:12 +00:00
|
|
|
|
|
|
|
// Setup the temporary Monero wallet necessary for monitoring the blockchain
|
2021-02-10 04:06:26 +00:00
|
|
|
let open_monitoring_wallet_response = monero_wallet
|
|
|
|
.open_wallet(MONERO_BLOCKCHAIN_MONITORING_WALLET_NAME)
|
2021-02-09 04:34:12 +00:00
|
|
|
.await;
|
2021-02-10 04:06:26 +00:00
|
|
|
if open_monitoring_wallet_response.is_err() {
|
2021-02-09 04:34:12 +00:00
|
|
|
monero_wallet
|
2021-02-10 04:06:26 +00:00
|
|
|
.create_wallet(MONERO_BLOCKCHAIN_MONITORING_WALLET_NAME)
|
2021-02-09 04:34:12 +00:00
|
|
|
.await
|
|
|
|
.context(format!(
|
2021-02-10 04:06:26 +00:00
|
|
|
"Unable to create Monero wallet for blockchain monitoring.\
|
2021-02-09 04:34:12 +00:00
|
|
|
Please ensure that the monero-wallet-rpc is available at {}",
|
2021-02-24 07:08:25 +00:00
|
|
|
monero_wallet_rpc_url
|
2021-02-09 04:34:12 +00:00
|
|
|
))?;
|
|
|
|
|
2021-02-26 06:03:07 +00:00
|
|
|
debug!(
|
2021-02-10 04:06:26 +00:00
|
|
|
"Created Monero wallet for blockchain monitoring with name {}",
|
|
|
|
MONERO_BLOCKCHAIN_MONITORING_WALLET_NAME
|
2021-02-09 04:34:12 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-02-26 06:04:51 +00:00
|
|
|
let _test_wallet_connection = monero_wallet
|
|
|
|
.block_height()
|
|
|
|
.await
|
|
|
|
.context("failed to validate connection to monero-wallet-rpc")?;
|
2020-12-15 10:26:02 +00:00
|
|
|
|
2021-01-19 03:48:07 +00:00
|
|
|
Ok((bitcoin_wallet, monero_wallet))
|
2020-12-15 10:26:02 +00:00
|
|
|
}
|