2021-02-11 04:07:01 +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)]
|
|
|
|
#![allow(non_snake_case)]
|
|
|
|
|
2021-05-10 08:06:10 +00:00
|
|
|
use anyhow::{bail, Context, Result};
|
2021-04-22 01:33:36 +00:00
|
|
|
use libp2p::core::multiaddr::Protocol;
|
|
|
|
use libp2p::core::Multiaddr;
|
2021-03-23 05:56:04 +00:00
|
|
|
use libp2p::Swarm;
|
2021-02-11 04:07:01 +00:00
|
|
|
use prettytable::{row, Table};
|
2021-04-22 01:33:36 +00:00
|
|
|
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
|
2021-03-04 00:28:58 +00:00
|
|
|
use std::sync::Arc;
|
2021-02-11 04:07:01 +00:00
|
|
|
use structopt::StructOpt;
|
2021-04-27 04:51:53 +00:00
|
|
|
use swap::asb::command::{Arguments, Command, ManualRecovery, RecoverCommandParams};
|
2021-03-04 00:28:58 +00:00
|
|
|
use swap::asb::config::{
|
2021-05-10 08:06:10 +00:00
|
|
|
initial_setup, query_user_for_initial_config, read_config, Config, ConfigNotInitialized,
|
|
|
|
GetDefaults,
|
2021-02-11 04:07:01 +00:00
|
|
|
};
|
2021-03-04 00:28:58 +00:00
|
|
|
use swap::database::Database;
|
|
|
|
use swap::monero::Amount;
|
2021-03-23 05:56:04 +00:00
|
|
|
use swap::network::swarm;
|
2021-04-27 04:51:53 +00:00
|
|
|
use swap::protocol::alice;
|
2021-04-01 02:00:15 +00:00
|
|
|
use swap::protocol::alice::event_loop::KrakenRate;
|
2021-04-28 06:13:04 +00:00
|
|
|
use swap::protocol::alice::{redeem, run, EventLoop};
|
2021-03-04 00:28:58 +00:00
|
|
|
use swap::seed::Seed;
|
2021-04-22 01:33:36 +00:00
|
|
|
use swap::tor::AuthenticatedClient;
|
|
|
|
use swap::{asb, bitcoin, env, kraken, monero, tor};
|
2021-05-05 03:49:11 +00:00
|
|
|
use tracing::{debug, info, warn};
|
2021-02-22 01:41:04 +00:00
|
|
|
use tracing_subscriber::filter::LevelFilter;
|
2021-02-11 04:07:01 +00:00
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
extern crate prettytable;
|
|
|
|
|
2021-02-19 04:31:39 +00:00
|
|
|
const DEFAULT_WALLET_NAME: &str = "asb-wallet";
|
2021-02-11 04:07:01 +00:00
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<()> {
|
2021-05-10 08:06:10 +00:00
|
|
|
let Arguments {
|
|
|
|
testnet,
|
|
|
|
json,
|
|
|
|
config,
|
|
|
|
cmd,
|
|
|
|
} = Arguments::from_args();
|
|
|
|
asb::tracing::init(LevelFilter::DEBUG, json).expect("initialize tracing");
|
2021-02-11 04:07:01 +00:00
|
|
|
|
2021-05-10 08:06:10 +00:00
|
|
|
let config_path = if let Some(config_path) = config {
|
2021-02-11 04:07:01 +00:00
|
|
|
config_path
|
2021-05-10 08:06:10 +00:00
|
|
|
} else if testnet {
|
|
|
|
env::Testnet::getConfigFileDefaults()?.config_path
|
2021-02-11 04:07:01 +00:00
|
|
|
} else {
|
2021-05-10 08:06:10 +00:00
|
|
|
env::Mainnet::getConfigFileDefaults()?.config_path
|
2021-02-11 04:07:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let config = match read_config(config_path.clone())? {
|
|
|
|
Ok(config) => config,
|
|
|
|
Err(ConfigNotInitialized {}) => {
|
2021-05-13 03:56:40 +00:00
|
|
|
initial_setup(config_path.clone(), query_user_for_initial_config(testnet)?)?;
|
2021-02-11 04:07:01 +00:00
|
|
|
read_config(config_path)?.expect("after initial setup config can be read")
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-05-12 01:25:45 +00:00
|
|
|
let env_config = env::new(testnet, &config);
|
2021-05-10 08:06:10 +00:00
|
|
|
|
|
|
|
if config.monero.network != env_config.monero_network {
|
|
|
|
bail!(format!(
|
|
|
|
"Expected monero network in config file to be {:?} but was {:?}",
|
|
|
|
env_config.monero_network, config.monero.network
|
|
|
|
));
|
|
|
|
}
|
|
|
|
if config.bitcoin.network != env_config.bitcoin_network {
|
|
|
|
bail!(format!(
|
|
|
|
"Expected bitcoin network in config file to be {:?} but was {:?}",
|
|
|
|
env_config.bitcoin_network, config.bitcoin.network
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2021-02-11 04:07:01 +00:00
|
|
|
info!(
|
2021-05-05 03:49:11 +00:00
|
|
|
db_folder = %config.data.dir.display(),
|
|
|
|
"Database and Seed will be stored in",
|
2021-02-11 04:07:01 +00:00
|
|
|
);
|
|
|
|
|
2021-02-01 23:39:34 +00:00
|
|
|
let db_path = config.data.dir.join("database");
|
|
|
|
|
|
|
|
let db = Database::open(config.data.dir.join(db_path).as_path())
|
2021-02-11 04:07:01 +00:00
|
|
|
.context("Could not open database")?;
|
|
|
|
|
2021-03-31 05:40:40 +00:00
|
|
|
let seed =
|
|
|
|
Seed::from_file_or_generate(&config.data.dir).expect("Could not retrieve/initialize seed");
|
2021-02-11 04:07:01 +00:00
|
|
|
|
2021-05-10 08:06:10 +00:00
|
|
|
match cmd {
|
2021-05-07 03:25:25 +00:00
|
|
|
Command::Start { resume_only } => {
|
2021-03-31 05:38:54 +00:00
|
|
|
let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config).await?;
|
2021-05-05 03:49:11 +00:00
|
|
|
|
2021-03-31 05:38:54 +00:00
|
|
|
let monero_wallet = init_monero_wallet(&config, env_config).await?;
|
2021-02-11 04:07:01 +00:00
|
|
|
|
2021-03-31 06:03:51 +00:00
|
|
|
let bitcoin_balance = bitcoin_wallet.balance().await?;
|
2021-05-05 03:49:11 +00:00
|
|
|
info!(%bitcoin_balance, "Initialized Bitcoin wallet");
|
2021-03-31 06:03:51 +00:00
|
|
|
|
|
|
|
let monero_balance = monero_wallet.get_balance().await?;
|
|
|
|
if monero_balance == Amount::ZERO {
|
2021-05-05 03:49:11 +00:00
|
|
|
let monero_address = monero_wallet.get_main_address();
|
2021-03-31 06:03:51 +00:00
|
|
|
warn!(
|
2021-05-05 03:49:11 +00:00
|
|
|
%monero_address,
|
2021-05-11 06:06:44 +00:00
|
|
|
"The Monero balance is 0, make sure to deposit funds at",
|
2021-03-31 06:03:51 +00:00
|
|
|
)
|
|
|
|
} else {
|
2021-05-05 03:49:11 +00:00
|
|
|
info!(%monero_balance, "Initialized Monero wallet");
|
2021-03-31 06:03:51 +00:00
|
|
|
}
|
|
|
|
|
2021-04-01 01:09:51 +00:00
|
|
|
let kraken_price_updates = kraken::connect()?;
|
2021-02-16 05:37:44 +00:00
|
|
|
|
2021-04-22 01:33:36 +00:00
|
|
|
// setup Tor hidden services
|
|
|
|
let tor_client =
|
|
|
|
tor::Client::new(config.tor.socks5_port).with_control_port(config.tor.control_port);
|
|
|
|
let _ac = match tor_client.assert_tor_running().await {
|
|
|
|
Ok(_) => {
|
2021-05-05 03:49:11 +00:00
|
|
|
tracing::info!("Tor found. Setting up hidden service");
|
2021-04-22 01:33:36 +00:00
|
|
|
let ac =
|
|
|
|
register_tor_services(config.network.clone().listen, tor_client, &seed)
|
|
|
|
.await?;
|
|
|
|
Some(ac)
|
|
|
|
}
|
|
|
|
Err(_) => {
|
2021-05-05 03:49:11 +00:00
|
|
|
tracing::warn!("Tor not found. Running on clear net");
|
2021-04-22 01:33:36 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-05-04 04:57:50 +00:00
|
|
|
let current_balance = monero_wallet.get_balance().await?;
|
|
|
|
let lock_fee = monero_wallet.static_tx_fee_estimate();
|
2021-05-07 03:25:25 +00:00
|
|
|
let kraken_rate = KrakenRate::new(config.maker.ask_spread, kraken_price_updates);
|
2021-05-04 04:57:50 +00:00
|
|
|
let mut swarm = swarm::alice(
|
|
|
|
&seed,
|
|
|
|
current_balance,
|
|
|
|
lock_fee,
|
2021-05-07 09:06:58 +00:00
|
|
|
config.maker.min_buy_btc,
|
2021-05-07 03:25:25 +00:00
|
|
|
config.maker.max_buy_btc,
|
2021-05-04 04:57:50 +00:00
|
|
|
kraken_rate.clone(),
|
|
|
|
resume_only,
|
2021-05-11 12:22:59 +00:00
|
|
|
env_config,
|
2021-05-04 04:57:50 +00:00
|
|
|
)?;
|
2021-04-08 00:02:13 +00:00
|
|
|
|
|
|
|
for listen in config.network.listen {
|
|
|
|
Swarm::listen_on(&mut swarm, listen.clone())
|
|
|
|
.with_context(|| format!("Failed to listen on network interface {}", listen))?;
|
|
|
|
}
|
2021-03-23 05:56:04 +00:00
|
|
|
|
2021-03-16 06:08:19 +00:00
|
|
|
let (event_loop, mut swap_receiver) = EventLoop::new(
|
2021-03-23 05:56:04 +00:00
|
|
|
swarm,
|
2021-03-17 03:55:42 +00:00
|
|
|
env_config,
|
2021-02-11 04:07:01 +00:00
|
|
|
Arc::new(bitcoin_wallet),
|
|
|
|
Arc::new(monero_wallet),
|
|
|
|
Arc::new(db),
|
2021-05-05 03:43:46 +00:00
|
|
|
kraken_rate.clone(),
|
2021-05-07 09:06:58 +00:00
|
|
|
config.maker.min_buy_btc,
|
2021-05-07 03:25:25 +00:00
|
|
|
config.maker.max_buy_btc,
|
2021-02-11 04:07:01 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
2021-03-16 06:08:19 +00:00
|
|
|
tokio::spawn(async move {
|
|
|
|
while let Some(swap) = swap_receiver.recv().await {
|
2021-05-05 03:43:46 +00:00
|
|
|
let rate = kraken_rate.clone();
|
2021-03-16 06:08:19 +00:00
|
|
|
tokio::spawn(async move {
|
|
|
|
let swap_id = swap.swap_id;
|
2021-05-05 03:43:46 +00:00
|
|
|
match run(swap, rate).await {
|
2021-03-16 06:08:19 +00:00
|
|
|
Ok(state) => {
|
2021-05-05 03:49:11 +00:00
|
|
|
tracing::debug!(%swap_id, %state, "Swap finished with state")
|
2021-03-16 06:08:19 +00:00
|
|
|
}
|
2021-05-05 03:49:11 +00:00
|
|
|
Err(error) => {
|
2021-05-11 01:08:33 +00:00
|
|
|
tracing::error!(%swap_id, "Swap failed. Error {:#}", error)
|
2021-03-16 06:08:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-05-05 03:49:11 +00:00
|
|
|
info!(perr_id = %event_loop.peer_id(), "Our peer id");
|
2021-02-11 04:07:01 +00:00
|
|
|
|
|
|
|
event_loop.run().await;
|
|
|
|
}
|
|
|
|
Command::History => {
|
|
|
|
let mut table = Table::new();
|
|
|
|
|
|
|
|
table.add_row(row!["SWAP ID", "STATE"]);
|
|
|
|
|
2021-03-26 04:16:19 +00:00
|
|
|
for (swap_id, state) in db.all_alice()? {
|
2021-02-11 04:07:01 +00:00
|
|
|
table.add_row(row![swap_id, state]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print the table to stdout
|
|
|
|
table.printstd();
|
|
|
|
}
|
2021-03-31 05:55:28 +00:00
|
|
|
Command::WithdrawBtc { amount, address } => {
|
|
|
|
let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config).await?;
|
|
|
|
|
|
|
|
let amount = match amount {
|
|
|
|
Some(amount) => amount,
|
|
|
|
None => {
|
|
|
|
bitcoin_wallet
|
|
|
|
.max_giveable(address.script_pubkey().len())
|
|
|
|
.await?
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let psbt = bitcoin_wallet.send_to_address(address, amount).await?;
|
|
|
|
let signed_tx = bitcoin_wallet.sign_and_finalize(psbt).await?;
|
|
|
|
|
|
|
|
bitcoin_wallet.broadcast(signed_tx, "withdraw").await?;
|
|
|
|
}
|
2021-03-31 06:03:51 +00:00
|
|
|
Command::Balance => {
|
|
|
|
let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config).await?;
|
|
|
|
let monero_wallet = init_monero_wallet(&config, env_config).await?;
|
|
|
|
|
|
|
|
let bitcoin_balance = bitcoin_wallet.balance().await?;
|
|
|
|
let monero_balance = monero_wallet.get_balance().await?;
|
|
|
|
|
2021-05-05 03:49:11 +00:00
|
|
|
tracing::info!(
|
|
|
|
%bitcoin_balance,
|
|
|
|
%monero_balance,
|
|
|
|
"Current balance");
|
2021-03-31 06:03:51 +00:00
|
|
|
}
|
2021-04-27 04:51:53 +00:00
|
|
|
Command::ManualRecovery(ManualRecovery::Cancel {
|
|
|
|
cancel_params: RecoverCommandParams { swap_id, force },
|
|
|
|
}) => {
|
|
|
|
let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config).await?;
|
|
|
|
|
|
|
|
let (txid, _) =
|
|
|
|
alice::cancel(swap_id, Arc::new(bitcoin_wallet), Arc::new(db), force).await??;
|
|
|
|
|
|
|
|
tracing::info!("Cancel transaction successfully published with id {}", txid);
|
|
|
|
}
|
|
|
|
Command::ManualRecovery(ManualRecovery::Refund {
|
|
|
|
refund_params: RecoverCommandParams { swap_id, force },
|
|
|
|
}) => {
|
|
|
|
let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config).await?;
|
|
|
|
let monero_wallet = init_monero_wallet(&config, env_config).await?;
|
|
|
|
|
|
|
|
alice::refund(
|
|
|
|
swap_id,
|
|
|
|
Arc::new(bitcoin_wallet),
|
|
|
|
Arc::new(monero_wallet),
|
|
|
|
Arc::new(db),
|
|
|
|
force,
|
|
|
|
)
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
tracing::info!("Monero successfully refunded");
|
|
|
|
}
|
2021-04-28 06:13:04 +00:00
|
|
|
Command::ManualRecovery(ManualRecovery::Punish {
|
|
|
|
punish_params: RecoverCommandParams { swap_id, force },
|
|
|
|
}) => {
|
|
|
|
let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config).await?;
|
|
|
|
|
|
|
|
let (txid, _) =
|
|
|
|
alice::punish(swap_id, Arc::new(bitcoin_wallet), Arc::new(db), force).await??;
|
|
|
|
|
|
|
|
tracing::info!("Punish transaction successfully published with id {}", txid);
|
|
|
|
}
|
|
|
|
Command::ManualRecovery(ManualRecovery::SafelyAbort { swap_id }) => {
|
|
|
|
alice::safely_abort(swap_id, Arc::new(db)).await?;
|
|
|
|
|
|
|
|
tracing::info!("Swap safely aborted");
|
|
|
|
}
|
|
|
|
Command::ManualRecovery(ManualRecovery::Redeem {
|
|
|
|
redeem_params: RecoverCommandParams { swap_id, force },
|
|
|
|
do_not_await_finality,
|
|
|
|
}) => {
|
|
|
|
let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config).await?;
|
|
|
|
|
|
|
|
let (txid, _) = alice::redeem(
|
|
|
|
swap_id,
|
|
|
|
Arc::new(bitcoin_wallet),
|
|
|
|
Arc::new(db),
|
|
|
|
force,
|
|
|
|
redeem::Finality::from_bool(do_not_await_finality),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
tracing::info!("Redeem transaction successfully published with id {}", txid);
|
|
|
|
}
|
2021-02-11 04:07:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-03-31 05:38:54 +00:00
|
|
|
async fn init_bitcoin_wallet(
|
|
|
|
config: &Config,
|
|
|
|
seed: &Seed,
|
|
|
|
env_config: swap::env::Config,
|
|
|
|
) -> Result<bitcoin::Wallet> {
|
2021-05-05 03:49:11 +00:00
|
|
|
debug!("Opening Bitcoin wallet");
|
2021-03-31 05:38:54 +00:00
|
|
|
let wallet_dir = config.data.dir.join("wallet");
|
|
|
|
|
|
|
|
let wallet = bitcoin::Wallet::new(
|
|
|
|
config.bitcoin.electrum_rpc_url.clone(),
|
|
|
|
&wallet_dir,
|
|
|
|
seed.derive_extended_private_key(env_config.bitcoin_network)?,
|
2021-03-17 03:55:42 +00:00
|
|
|
env_config,
|
2021-05-02 23:48:40 +00:00
|
|
|
config.bitcoin.target_block,
|
2021-02-11 04:07:01 +00:00
|
|
|
)
|
2021-03-31 05:38:54 +00:00
|
|
|
.await
|
|
|
|
.context("Failed to initialize Bitcoin wallet")?;
|
2021-02-16 23:56:39 +00:00
|
|
|
|
2021-03-31 05:38:54 +00:00
|
|
|
wallet.sync().await?;
|
2021-02-16 23:56:39 +00:00
|
|
|
|
2021-03-31 05:38:54 +00:00
|
|
|
Ok(wallet)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn init_monero_wallet(
|
|
|
|
config: &Config,
|
|
|
|
env_config: swap::env::Config,
|
|
|
|
) -> Result<monero::Wallet> {
|
2021-05-05 03:49:11 +00:00
|
|
|
debug!("Opening Monero wallet");
|
2021-03-31 05:38:54 +00:00
|
|
|
let wallet = monero::Wallet::open_or_create(
|
2021-02-24 07:00:07 +00:00
|
|
|
config.monero.wallet_rpc_url.clone(),
|
|
|
|
DEFAULT_WALLET_NAME.to_string(),
|
2021-03-17 03:55:42 +00:00
|
|
|
env_config,
|
2021-03-16 08:24:41 +00:00
|
|
|
)
|
|
|
|
.await?;
|
2021-02-11 04:07:01 +00:00
|
|
|
|
2021-03-31 05:38:54 +00:00
|
|
|
Ok(wallet)
|
2021-02-11 04:07:01 +00:00
|
|
|
}
|
2021-04-22 01:33:36 +00:00
|
|
|
|
|
|
|
/// Registers a hidden service for each network.
|
|
|
|
/// Note: Once ac goes out of scope, the services will be de-registered.
|
|
|
|
async fn register_tor_services(
|
|
|
|
networks: Vec<Multiaddr>,
|
|
|
|
tor_client: tor::Client,
|
|
|
|
seed: &Seed,
|
|
|
|
) -> Result<AuthenticatedClient> {
|
|
|
|
let mut ac = tor_client.into_authenticated_client().await?;
|
|
|
|
|
|
|
|
let hidden_services_details = networks
|
|
|
|
.iter()
|
|
|
|
.flat_map(|network| {
|
|
|
|
network.iter().map(|protocol| match protocol {
|
|
|
|
Protocol::Tcp(port) => Some((
|
|
|
|
port,
|
|
|
|
SocketAddr::new(IpAddr::from(Ipv4Addr::new(127, 0, 0, 1)), port),
|
|
|
|
)),
|
|
|
|
_ => {
|
|
|
|
// We only care for Tcp for now.
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.filter_map(|details| details)
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let key = seed.derive_torv3_key();
|
|
|
|
|
|
|
|
ac.add_services(&hidden_services_details, &key).await?;
|
|
|
|
|
|
|
|
let onion_address = key
|
|
|
|
.public()
|
|
|
|
.get_onion_address()
|
|
|
|
.get_address_without_dot_onion();
|
|
|
|
|
|
|
|
hidden_services_details.iter().for_each(|(port, _)| {
|
2021-05-05 03:49:11 +00:00
|
|
|
let onion_address = format!("/onion3/{}:{}", onion_address, port);
|
|
|
|
tracing::info!(%onion_address);
|
2021-04-22 01:33:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Ok(ac)
|
|
|
|
}
|