mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2024-10-01 01:45:40 -04:00
Merge #154
154: Clean up `lib.rs` r=D4nte a=D4nte Do not remove it just yet as there are still issues with the CI but proceed with some clean up. Merging this now to avoid further merge conflicts. Co-authored-by: Franck Royer <franck@coblox.tech>
This commit is contained in:
commit
91fe18a796
@ -10,7 +10,7 @@ pub use ::bitcoin::{util::amount::Amount, Address, Network, Transaction, Txid};
|
|||||||
pub use ecdsa_fun::{adaptor::EncryptedSignature, fun::Scalar, Signature};
|
pub use ecdsa_fun::{adaptor::EncryptedSignature, fun::Scalar, Signature};
|
||||||
pub use wallet::Wallet;
|
pub use wallet::Wallet;
|
||||||
|
|
||||||
use crate::{bitcoin::timelocks::BlockHeight, config::Config, ExpiredTimelocks};
|
use crate::{bitcoin::timelocks::BlockHeight, config::Config};
|
||||||
use ::bitcoin::{
|
use ::bitcoin::{
|
||||||
hashes::{hex::ToHex, Hash},
|
hashes::{hex::ToHex, Hash},
|
||||||
secp256k1,
|
secp256k1,
|
||||||
@ -25,6 +25,7 @@ use rand::{CryptoRng, RngCore};
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use sha2::Sha256;
|
use sha2::Sha256;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
use timelocks::ExpiredTimelocks;
|
||||||
|
|
||||||
// TODO: Configurable tx-fee (note: parties have to agree prior to swapping)
|
// TODO: Configurable tx-fee (note: parties have to agree prior to swapping)
|
||||||
// Current reasoning:
|
// Current reasoning:
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use std::ops::Add;
|
use std::ops::Add;
|
||||||
|
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
/// Represent a timelock, expressed in relative block height as defined in
|
/// Represent a timelock, expressed in relative block height as defined in
|
||||||
/// [BIP68](https://github.com/bitcoin/bips/blob/master/bip-0068.mediawiki).
|
/// [BIP68](https://github.com/bitcoin/bips/blob/master/bip-0068.mediawiki).
|
||||||
/// E.g. The timelock expires 10 blocks after the reference transaction is
|
/// E.g. The timelock expires 10 blocks after the reference transaction is
|
||||||
@ -47,3 +48,10 @@ impl Add<Timelock> for BlockHeight {
|
|||||||
BlockHeight(self.0 + rhs.0)
|
BlockHeight(self.0 + rhs.0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub enum ExpiredTimelocks {
|
||||||
|
None,
|
||||||
|
Cancel,
|
||||||
|
Punish,
|
||||||
|
}
|
||||||
|
@ -2,8 +2,7 @@ use crate::{
|
|||||||
bitcoin::{EncryptedSignature, TxCancel, TxRefund},
|
bitcoin::{EncryptedSignature, TxCancel, TxRefund},
|
||||||
monero,
|
monero,
|
||||||
monero::monero_private_key,
|
monero::monero_private_key,
|
||||||
protocol::{alice, alice::AliceState},
|
protocol::{alice, alice::AliceState, SwapAmounts},
|
||||||
SwapAmounts,
|
|
||||||
};
|
};
|
||||||
use ::bitcoin::hashes::core::fmt::Display;
|
use ::bitcoin::hashes::core::fmt::Display;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
monero::TransferProof,
|
monero::TransferProof,
|
||||||
protocol::{bob, bob::BobState},
|
protocol::{bob, bob::BobState, SwapAmounts},
|
||||||
SwapAmounts,
|
|
||||||
};
|
};
|
||||||
use ::bitcoin::hashes::core::fmt::Display;
|
use ::bitcoin::hashes::core::fmt::Display;
|
||||||
use monero_harness::rpc::wallet::BlockHeight;
|
use monero_harness::rpc::wallet::BlockHeight;
|
||||||
|
@ -26,39 +26,3 @@ pub mod seed;
|
|||||||
pub mod trace;
|
pub mod trace;
|
||||||
|
|
||||||
mod fs;
|
mod fs;
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use std::fmt::{self, Display};
|
|
||||||
|
|
||||||
pub type Never = std::convert::Infallible;
|
|
||||||
|
|
||||||
/// XMR/BTC swap amounts.
|
|
||||||
#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq)]
|
|
||||||
// TODO(Franck): review necessity of this struct
|
|
||||||
pub struct SwapAmounts {
|
|
||||||
/// Amount of BTC to swap.
|
|
||||||
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
|
|
||||||
pub btc: bitcoin::Amount,
|
|
||||||
/// Amount of XMR to swap.
|
|
||||||
#[serde(with = "monero::monero_amount")]
|
|
||||||
pub xmr: monero::Amount,
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Display in XMR and BTC (not picos and sats).
|
|
||||||
impl Display for SwapAmounts {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
write!(
|
|
||||||
f,
|
|
||||||
"{} sats for {} piconeros",
|
|
||||||
self.btc.as_sat(),
|
|
||||||
self.xmr.as_piconero()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
|
||||||
pub enum ExpiredTimelocks {
|
|
||||||
None,
|
|
||||||
Cancel,
|
|
||||||
Punish,
|
|
||||||
}
|
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
#![warn(
|
#![warn(
|
||||||
unused_extern_crates,
|
unused_extern_crates,
|
||||||
missing_debug_implementations,
|
|
||||||
missing_copy_implementations,
|
missing_copy_implementations,
|
||||||
rust_2018_idioms,
|
rust_2018_idioms,
|
||||||
clippy::cast_possible_truncation,
|
clippy::cast_possible_truncation,
|
||||||
@ -15,22 +14,26 @@
|
|||||||
|
|
||||||
use crate::cli::{Command, Options, Resume};
|
use crate::cli::{Command, Options, Resume};
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
|
use config::Config;
|
||||||
|
use database::Database;
|
||||||
use prettytable::{row, Table};
|
use prettytable::{row, Table};
|
||||||
|
use protocol::{alice, bob, bob::Builder, SwapAmounts};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
use swap::{
|
use trace::init_tracing;
|
||||||
bitcoin,
|
|
||||||
config::Config,
|
|
||||||
database::Database,
|
|
||||||
monero,
|
|
||||||
protocol::{alice, bob, bob::Builder},
|
|
||||||
trace::init_tracing,
|
|
||||||
SwapAmounts,
|
|
||||||
};
|
|
||||||
use tracing::{info, log::LevelFilter};
|
use tracing::{info, log::LevelFilter};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
pub mod bitcoin;
|
||||||
mod cli;
|
mod cli;
|
||||||
|
pub mod config;
|
||||||
|
pub mod database;
|
||||||
|
mod fs;
|
||||||
|
pub mod monero;
|
||||||
|
pub mod network;
|
||||||
|
pub mod protocol;
|
||||||
|
pub mod seed;
|
||||||
|
pub mod trace;
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate prettytable;
|
extern crate prettytable;
|
||||||
@ -49,7 +52,7 @@ async fn main() -> Result<()> {
|
|||||||
let data_dir = std::path::Path::new(opt.data_dir.as_str()).to_path_buf();
|
let data_dir = std::path::Path::new(opt.data_dir.as_str()).to_path_buf();
|
||||||
let db_path = data_dir.join("database");
|
let db_path = data_dir.join("database");
|
||||||
|
|
||||||
let seed = swap::config::seed::Seed::from_file_or_generate(&data_dir)
|
let seed = config::seed::Seed::from_file_or_generate(&data_dir)
|
||||||
.expect("Could not retrieve/initialize seed")
|
.expect("Could not retrieve/initialize seed")
|
||||||
.into();
|
.into();
|
||||||
|
|
||||||
@ -227,10 +230,9 @@ async fn setup_wallets(
|
|||||||
bitcoin_wallet_name: &str,
|
bitcoin_wallet_name: &str,
|
||||||
monero_wallet_rpc_url: url::Url,
|
monero_wallet_rpc_url: url::Url,
|
||||||
config: Config,
|
config: Config,
|
||||||
) -> Result<(swap::bitcoin::Wallet, swap::monero::Wallet)> {
|
) -> Result<(bitcoin::Wallet, monero::Wallet)> {
|
||||||
let bitcoin_wallet =
|
let bitcoin_wallet =
|
||||||
swap::bitcoin::Wallet::new(bitcoin_wallet_name, bitcoind_url, config.bitcoin_network)
|
bitcoin::Wallet::new(bitcoin_wallet_name, bitcoind_url, config.bitcoin_network).await?;
|
||||||
.await?;
|
|
||||||
let bitcoin_balance = bitcoin_wallet.balance().await?;
|
let bitcoin_balance = bitcoin_wallet.balance().await?;
|
||||||
info!(
|
info!(
|
||||||
"Connection to Bitcoin wallet succeeded, balance: {}",
|
"Connection to Bitcoin wallet succeeded, balance: {}",
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
use crate::protocol::{alice, bob};
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use futures::prelude::*;
|
use futures::prelude::*;
|
||||||
use libp2p::{
|
use libp2p::{
|
||||||
@ -8,8 +9,6 @@ use serde::{Deserialize, Serialize};
|
|||||||
use std::{fmt::Debug, io, marker::PhantomData};
|
use std::{fmt::Debug, io, marker::PhantomData};
|
||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
|
|
||||||
use crate::protocol::{alice, bob};
|
|
||||||
|
|
||||||
/// Time to wait for a response back once we send a request.
|
/// Time to wait for a response back once we send a request.
|
||||||
pub const TIMEOUT: u64 = 3600; // One hour.
|
pub const TIMEOUT: u64 = 3600; // One hour.
|
||||||
|
|
||||||
|
@ -1,8 +1,35 @@
|
|||||||
|
use crate::monero;
|
||||||
|
use bitcoin::hashes::core::{fmt, fmt::Display};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
pub mod alice;
|
pub mod alice;
|
||||||
pub mod bob;
|
pub mod bob;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Copy, Clone)]
|
||||||
pub struct StartingBalances {
|
pub struct StartingBalances {
|
||||||
pub xmr: crate::monero::Amount,
|
pub xmr: crate::monero::Amount,
|
||||||
pub btc: bitcoin::Amount,
|
pub btc: bitcoin::Amount,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// XMR/BTC swap amounts.
|
||||||
|
#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq)]
|
||||||
|
pub struct SwapAmounts {
|
||||||
|
/// Amount of BTC to swap.
|
||||||
|
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
|
||||||
|
pub btc: bitcoin::Amount,
|
||||||
|
/// Amount of XMR to swap.
|
||||||
|
#[serde(with = "monero::monero_amount")]
|
||||||
|
pub xmr: crate::monero::Amount,
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Display in XMR and BTC (not picos and sats).
|
||||||
|
impl Display for SwapAmounts {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"{} sats for {} piconeros",
|
||||||
|
self.btc.as_sat(),
|
||||||
|
self.xmr.as_piconero()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -21,9 +21,8 @@ use crate::{
|
|||||||
transport::build,
|
transport::build,
|
||||||
Seed as NetworkSeed,
|
Seed as NetworkSeed,
|
||||||
},
|
},
|
||||||
protocol::bob,
|
protocol::{bob, SwapAmounts},
|
||||||
seed::Seed,
|
seed::Seed,
|
||||||
SwapAmounts,
|
|
||||||
};
|
};
|
||||||
use anyhow::{bail, Result};
|
use anyhow::{bail, Result};
|
||||||
use libp2p::{
|
use libp2p::{
|
||||||
|
@ -1,14 +1,15 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
bitcoin,
|
bitcoin,
|
||||||
bitcoin::{
|
bitcoin::{
|
||||||
current_epoch, timelocks::Timelock, wait_for_cancel_timelock_to_expire, GetBlockHeight,
|
current_epoch,
|
||||||
TransactionBlockHeight, TxCancel, TxRefund, WatchForRawTransaction,
|
timelocks::{ExpiredTimelocks, Timelock},
|
||||||
|
wait_for_cancel_timelock_to_expire, GetBlockHeight, TransactionBlockHeight, TxCancel,
|
||||||
|
TxRefund, WatchForRawTransaction,
|
||||||
},
|
},
|
||||||
monero,
|
monero,
|
||||||
monero::CreateWalletForOutput,
|
monero::CreateWalletForOutput,
|
||||||
network::request_response::AliceToBob,
|
network::request_response::AliceToBob,
|
||||||
protocol::{alice, bob},
|
protocol::{alice, bob, SwapAmounts},
|
||||||
ExpiredTimelocks, SwapAmounts,
|
|
||||||
};
|
};
|
||||||
use anyhow::{anyhow, Context, Result};
|
use anyhow::{anyhow, Context, Result};
|
||||||
use ecdsa_fun::{
|
use ecdsa_fun::{
|
||||||
|
@ -1,16 +1,3 @@
|
|||||||
use anyhow::{Context, Result};
|
|
||||||
use ecdsa_fun::{adaptor::Adaptor, nonce::Deterministic};
|
|
||||||
use futures::{
|
|
||||||
future::{select, Either},
|
|
||||||
pin_mut,
|
|
||||||
};
|
|
||||||
use libp2p::request_response::ResponseChannel;
|
|
||||||
use rand::rngs::OsRng;
|
|
||||||
use sha2::Sha256;
|
|
||||||
use std::sync::Arc;
|
|
||||||
use tokio::time::timeout;
|
|
||||||
use tracing::{info, trace};
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
bitcoin,
|
bitcoin,
|
||||||
bitcoin::{
|
bitcoin::{
|
||||||
@ -27,9 +14,21 @@ use crate::{
|
|||||||
protocol::{
|
protocol::{
|
||||||
alice,
|
alice,
|
||||||
alice::{event_loop::EventLoopHandle, SwapResponse},
|
alice::{event_loop::EventLoopHandle, SwapResponse},
|
||||||
},
|
|
||||||
SwapAmounts,
|
SwapAmounts,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
use anyhow::{Context, Result};
|
||||||
|
use ecdsa_fun::{adaptor::Adaptor, nonce::Deterministic};
|
||||||
|
use futures::{
|
||||||
|
future::{select, Either},
|
||||||
|
pin_mut,
|
||||||
|
};
|
||||||
|
use libp2p::request_response::ResponseChannel;
|
||||||
|
use rand::rngs::OsRng;
|
||||||
|
use sha2::Sha256;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use tokio::time::timeout;
|
||||||
|
use tracing::{info, trace};
|
||||||
|
|
||||||
pub async fn negotiate(
|
pub async fn negotiate(
|
||||||
state0: alice::State0,
|
state0: alice::State0,
|
||||||
|
@ -2,7 +2,10 @@
|
|||||||
//! Alice holds XMR and wishes receive BTC.
|
//! Alice holds XMR and wishes receive BTC.
|
||||||
use crate::{
|
use crate::{
|
||||||
bitcoin,
|
bitcoin,
|
||||||
bitcoin::{TransactionBlockHeight, WaitForTransactionFinality, WatchForRawTransaction},
|
bitcoin::{
|
||||||
|
timelocks::ExpiredTimelocks, TransactionBlockHeight, WaitForTransactionFinality,
|
||||||
|
WatchForRawTransaction,
|
||||||
|
},
|
||||||
config::Config,
|
config::Config,
|
||||||
database,
|
database,
|
||||||
database::Database,
|
database::Database,
|
||||||
@ -22,7 +25,6 @@ use crate::{
|
|||||||
AliceState,
|
AliceState,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
ExpiredTimelocks,
|
|
||||||
};
|
};
|
||||||
use anyhow::{bail, Result};
|
use anyhow::{bail, Result};
|
||||||
use async_recursion::async_recursion;
|
use async_recursion::async_recursion;
|
||||||
|
@ -11,9 +11,8 @@ use crate::{
|
|||||||
peer_tracker::{self, PeerTracker},
|
peer_tracker::{self, PeerTracker},
|
||||||
transport::build,
|
transport::build,
|
||||||
},
|
},
|
||||||
protocol::{alice, bob},
|
protocol::{alice, bob, SwapAmounts},
|
||||||
seed::Seed,
|
seed::Seed,
|
||||||
SwapAmounts,
|
|
||||||
};
|
};
|
||||||
use anyhow::{bail, Result};
|
use anyhow::{bail, Result};
|
||||||
use libp2p::{core::Multiaddr, identity::Keypair, NetworkBehaviour, PeerId};
|
use libp2p::{core::Multiaddr, identity::Keypair, NetworkBehaviour, PeerId};
|
||||||
|
@ -1,14 +1,15 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
bitcoin::{
|
bitcoin::{
|
||||||
self, current_epoch, timelocks::Timelock, wait_for_cancel_timelock_to_expire,
|
self, current_epoch,
|
||||||
BroadcastSignedTransaction, BuildTxLockPsbt, GetBlockHeight, GetNetwork, GetRawTransaction,
|
timelocks::{ExpiredTimelocks, Timelock},
|
||||||
Transaction, TransactionBlockHeight, TxCancel, Txid, WatchForRawTransaction,
|
wait_for_cancel_timelock_to_expire, BroadcastSignedTransaction, BuildTxLockPsbt,
|
||||||
|
GetBlockHeight, GetNetwork, GetRawTransaction, Transaction, TransactionBlockHeight,
|
||||||
|
TxCancel, Txid, WatchForRawTransaction,
|
||||||
},
|
},
|
||||||
config::Config,
|
config::Config,
|
||||||
monero,
|
monero,
|
||||||
monero::{monero_private_key, TransferProof},
|
monero::{monero_private_key, TransferProof},
|
||||||
protocol::{alice, bob},
|
protocol::{alice, bob, SwapAmounts},
|
||||||
ExpiredTimelocks, SwapAmounts,
|
|
||||||
};
|
};
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
use ecdsa_fun::{
|
use ecdsa_fun::{
|
||||||
|
@ -1,10 +1,13 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
bitcoin,
|
bitcoin,
|
||||||
|
bitcoin::timelocks::ExpiredTimelocks,
|
||||||
config::Config,
|
config::Config,
|
||||||
database::{Database, Swap},
|
database::{Database, Swap},
|
||||||
monero,
|
monero,
|
||||||
protocol::bob::{self, event_loop::EventLoopHandle, state::*, SwapRequest},
|
protocol::{
|
||||||
ExpiredTimelocks, SwapAmounts,
|
bob::{self, event_loop::EventLoopHandle, state::*, SwapRequest},
|
||||||
|
SwapAmounts,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
use anyhow::{bail, Result};
|
use anyhow::{bail, Result};
|
||||||
use async_recursion::async_recursion;
|
use async_recursion::async_recursion;
|
||||||
|
@ -19,7 +19,7 @@ use std::{
|
|||||||
};
|
};
|
||||||
use tracing::{debug, error};
|
use tracing::{debug, error};
|
||||||
|
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
|
||||||
pub struct SwapRequest {
|
pub struct SwapRequest {
|
||||||
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
|
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
|
||||||
pub btc_amount: bitcoin::Amount,
|
pub btc_amount: bitcoin::Amount,
|
||||||
|
@ -9,9 +9,8 @@ use swap::{
|
|||||||
bitcoin,
|
bitcoin,
|
||||||
config::Config,
|
config::Config,
|
||||||
monero,
|
monero,
|
||||||
protocol::{alice, alice::AliceState, bob, bob::BobState},
|
protocol::{alice, alice::AliceState, bob, bob::BobState, SwapAmounts},
|
||||||
seed::Seed,
|
seed::Seed,
|
||||||
SwapAmounts,
|
|
||||||
};
|
};
|
||||||
use tempfile::tempdir;
|
use tempfile::tempdir;
|
||||||
use testcontainers::{clients::Cli, Container};
|
use testcontainers::{clients::Cli, Container};
|
||||||
|
Loading…
Reference in New Issue
Block a user