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 wallet::Wallet;
|
||||
|
||||
use crate::{bitcoin::timelocks::BlockHeight, config::Config, ExpiredTimelocks};
|
||||
use crate::{bitcoin::timelocks::BlockHeight, config::Config};
|
||||
use ::bitcoin::{
|
||||
hashes::{hex::ToHex, Hash},
|
||||
secp256k1,
|
||||
@ -25,6 +25,7 @@ use rand::{CryptoRng, RngCore};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sha2::Sha256;
|
||||
use std::str::FromStr;
|
||||
use timelocks::ExpiredTimelocks;
|
||||
|
||||
// TODO: Configurable tx-fee (note: parties have to agree prior to swapping)
|
||||
// Current reasoning:
|
||||
|
@ -1,6 +1,7 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::ops::Add;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Represent a timelock, expressed in relative block height as defined in
|
||||
/// [BIP68](https://github.com/bitcoin/bips/blob/master/bip-0068.mediawiki).
|
||||
/// 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)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum ExpiredTimelocks {
|
||||
None,
|
||||
Cancel,
|
||||
Punish,
|
||||
}
|
||||
|
@ -2,8 +2,7 @@ use crate::{
|
||||
bitcoin::{EncryptedSignature, TxCancel, TxRefund},
|
||||
monero,
|
||||
monero::monero_private_key,
|
||||
protocol::{alice, alice::AliceState},
|
||||
SwapAmounts,
|
||||
protocol::{alice, alice::AliceState, SwapAmounts},
|
||||
};
|
||||
use ::bitcoin::hashes::core::fmt::Display;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
@ -1,7 +1,6 @@
|
||||
use crate::{
|
||||
monero::TransferProof,
|
||||
protocol::{bob, bob::BobState},
|
||||
SwapAmounts,
|
||||
protocol::{bob, bob::BobState, SwapAmounts},
|
||||
};
|
||||
use ::bitcoin::hashes::core::fmt::Display;
|
||||
use monero_harness::rpc::wallet::BlockHeight;
|
||||
|
@ -26,39 +26,3 @@ pub mod seed;
|
||||
pub mod trace;
|
||||
|
||||
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(
|
||||
unused_extern_crates,
|
||||
missing_debug_implementations,
|
||||
missing_copy_implementations,
|
||||
rust_2018_idioms,
|
||||
clippy::cast_possible_truncation,
|
||||
@ -15,22 +14,26 @@
|
||||
|
||||
use crate::cli::{Command, Options, Resume};
|
||||
use anyhow::{Context, Result};
|
||||
use config::Config;
|
||||
use database::Database;
|
||||
use prettytable::{row, Table};
|
||||
use protocol::{alice, bob, bob::Builder, SwapAmounts};
|
||||
use std::sync::Arc;
|
||||
use structopt::StructOpt;
|
||||
use swap::{
|
||||
bitcoin,
|
||||
config::Config,
|
||||
database::Database,
|
||||
monero,
|
||||
protocol::{alice, bob, bob::Builder},
|
||||
trace::init_tracing,
|
||||
SwapAmounts,
|
||||
};
|
||||
use trace::init_tracing;
|
||||
use tracing::{info, log::LevelFilter};
|
||||
use uuid::Uuid;
|
||||
|
||||
pub mod bitcoin;
|
||||
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]
|
||||
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 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")
|
||||
.into();
|
||||
|
||||
@ -227,10 +230,9 @@ async fn setup_wallets(
|
||||
bitcoin_wallet_name: &str,
|
||||
monero_wallet_rpc_url: url::Url,
|
||||
config: Config,
|
||||
) -> Result<(swap::bitcoin::Wallet, swap::monero::Wallet)> {
|
||||
) -> Result<(bitcoin::Wallet, monero::Wallet)> {
|
||||
let bitcoin_wallet =
|
||||
swap::bitcoin::Wallet::new(bitcoin_wallet_name, bitcoind_url, config.bitcoin_network)
|
||||
.await?;
|
||||
bitcoin::Wallet::new(bitcoin_wallet_name, bitcoind_url, config.bitcoin_network).await?;
|
||||
let bitcoin_balance = bitcoin_wallet.balance().await?;
|
||||
info!(
|
||||
"Connection to Bitcoin wallet succeeded, balance: {}",
|
||||
|
@ -1,3 +1,4 @@
|
||||
use crate::protocol::{alice, bob};
|
||||
use async_trait::async_trait;
|
||||
use futures::prelude::*;
|
||||
use libp2p::{
|
||||
@ -8,8 +9,6 @@ use serde::{Deserialize, Serialize};
|
||||
use std::{fmt::Debug, io, marker::PhantomData};
|
||||
use tracing::debug;
|
||||
|
||||
use crate::protocol::{alice, bob};
|
||||
|
||||
/// Time to wait for a response back once we send a request.
|
||||
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 bob;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub struct StartingBalances {
|
||||
pub xmr: crate::monero::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,
|
||||
Seed as NetworkSeed,
|
||||
},
|
||||
protocol::bob,
|
||||
protocol::{bob, SwapAmounts},
|
||||
seed::Seed,
|
||||
SwapAmounts,
|
||||
};
|
||||
use anyhow::{bail, Result};
|
||||
use libp2p::{
|
||||
|
@ -1,14 +1,15 @@
|
||||
use crate::{
|
||||
bitcoin,
|
||||
bitcoin::{
|
||||
current_epoch, timelocks::Timelock, wait_for_cancel_timelock_to_expire, GetBlockHeight,
|
||||
TransactionBlockHeight, TxCancel, TxRefund, WatchForRawTransaction,
|
||||
current_epoch,
|
||||
timelocks::{ExpiredTimelocks, Timelock},
|
||||
wait_for_cancel_timelock_to_expire, GetBlockHeight, TransactionBlockHeight, TxCancel,
|
||||
TxRefund, WatchForRawTransaction,
|
||||
},
|
||||
monero,
|
||||
monero::CreateWalletForOutput,
|
||||
network::request_response::AliceToBob,
|
||||
protocol::{alice, bob},
|
||||
ExpiredTimelocks, SwapAmounts,
|
||||
protocol::{alice, bob, SwapAmounts},
|
||||
};
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
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::{
|
||||
bitcoin,
|
||||
bitcoin::{
|
||||
@ -27,9 +14,21 @@ use crate::{
|
||||
protocol::{
|
||||
alice,
|
||||
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(
|
||||
state0: alice::State0,
|
||||
|
@ -2,7 +2,10 @@
|
||||
//! Alice holds XMR and wishes receive BTC.
|
||||
use crate::{
|
||||
bitcoin,
|
||||
bitcoin::{TransactionBlockHeight, WaitForTransactionFinality, WatchForRawTransaction},
|
||||
bitcoin::{
|
||||
timelocks::ExpiredTimelocks, TransactionBlockHeight, WaitForTransactionFinality,
|
||||
WatchForRawTransaction,
|
||||
},
|
||||
config::Config,
|
||||
database,
|
||||
database::Database,
|
||||
@ -22,7 +25,6 @@ use crate::{
|
||||
AliceState,
|
||||
},
|
||||
},
|
||||
ExpiredTimelocks,
|
||||
};
|
||||
use anyhow::{bail, Result};
|
||||
use async_recursion::async_recursion;
|
||||
|
@ -11,9 +11,8 @@ use crate::{
|
||||
peer_tracker::{self, PeerTracker},
|
||||
transport::build,
|
||||
},
|
||||
protocol::{alice, bob},
|
||||
protocol::{alice, bob, SwapAmounts},
|
||||
seed::Seed,
|
||||
SwapAmounts,
|
||||
};
|
||||
use anyhow::{bail, Result};
|
||||
use libp2p::{core::Multiaddr, identity::Keypair, NetworkBehaviour, PeerId};
|
||||
|
@ -1,14 +1,15 @@
|
||||
use crate::{
|
||||
bitcoin::{
|
||||
self, current_epoch, timelocks::Timelock, wait_for_cancel_timelock_to_expire,
|
||||
BroadcastSignedTransaction, BuildTxLockPsbt, GetBlockHeight, GetNetwork, GetRawTransaction,
|
||||
Transaction, TransactionBlockHeight, TxCancel, Txid, WatchForRawTransaction,
|
||||
self, current_epoch,
|
||||
timelocks::{ExpiredTimelocks, Timelock},
|
||||
wait_for_cancel_timelock_to_expire, BroadcastSignedTransaction, BuildTxLockPsbt,
|
||||
GetBlockHeight, GetNetwork, GetRawTransaction, Transaction, TransactionBlockHeight,
|
||||
TxCancel, Txid, WatchForRawTransaction,
|
||||
},
|
||||
config::Config,
|
||||
monero,
|
||||
monero::{monero_private_key, TransferProof},
|
||||
protocol::{alice, bob},
|
||||
ExpiredTimelocks, SwapAmounts,
|
||||
protocol::{alice, bob, SwapAmounts},
|
||||
};
|
||||
use anyhow::{anyhow, Result};
|
||||
use ecdsa_fun::{
|
||||
|
@ -1,10 +1,13 @@
|
||||
use crate::{
|
||||
bitcoin,
|
||||
bitcoin::timelocks::ExpiredTimelocks,
|
||||
config::Config,
|
||||
database::{Database, Swap},
|
||||
monero,
|
||||
protocol::bob::{self, event_loop::EventLoopHandle, state::*, SwapRequest},
|
||||
ExpiredTimelocks, SwapAmounts,
|
||||
protocol::{
|
||||
bob::{self, event_loop::EventLoopHandle, state::*, SwapRequest},
|
||||
SwapAmounts,
|
||||
},
|
||||
};
|
||||
use anyhow::{bail, Result};
|
||||
use async_recursion::async_recursion;
|
||||
|
@ -19,7 +19,7 @@ use std::{
|
||||
};
|
||||
use tracing::{debug, error};
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
|
||||
pub struct SwapRequest {
|
||||
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
|
||||
pub btc_amount: bitcoin::Amount,
|
||||
|
@ -9,9 +9,8 @@ use swap::{
|
||||
bitcoin,
|
||||
config::Config,
|
||||
monero,
|
||||
protocol::{alice, alice::AliceState, bob, bob::BobState},
|
||||
protocol::{alice, alice::AliceState, bob, bob::BobState, SwapAmounts},
|
||||
seed::Seed,
|
||||
SwapAmounts,
|
||||
};
|
||||
use tempfile::tempdir;
|
||||
use testcontainers::{clients::Cli, Container};
|
||||
|
Loading…
Reference in New Issue
Block a user