Correctly re-export types from bitcoin and monero modules

This commit is contained in:
Lucas Soriano del Pino 2020-11-07 06:57:35 +11:00 committed by rishflab
parent f5ff50157e
commit 483c819e17
10 changed files with 67 additions and 77 deletions

View File

@ -75,7 +75,7 @@ pub async fn swap(
// to `ConstantBackoff`. // to `ConstantBackoff`.
#[async_trait] #[async_trait]
impl ReceiveBitcoinRedeemEncsig for Network { impl ReceiveBitcoinRedeemEncsig for Network {
async fn receive_bitcoin_redeem_encsig(&mut self) -> xmr_btc::bitcoin::EncryptedSignature { async fn receive_bitcoin_redeem_encsig(&mut self) -> bitcoin::EncryptedSignature {
#[derive(Debug)] #[derive(Debug)]
struct UnexpectedMessage; struct UnexpectedMessage;

View File

@ -8,13 +8,13 @@ use bitcoin_harness::bitcoind_rpc::PsbtBase64;
use reqwest::Url; use reqwest::Url;
use tokio::time; use tokio::time;
use xmr_btc::bitcoin::{ use xmr_btc::bitcoin::{
Amount, BlockHeight, BroadcastSignedTransaction, BuildTxLockPsbt, SignTxLock, BlockHeight, BroadcastSignedTransaction, BuildTxLockPsbt, SignTxLock, TransactionBlockHeight,
TransactionBlockHeight, TxLock, Txid, WatchForRawTransaction, WatchForRawTransaction,
}; };
pub const TX_LOCK_MINE_TIMEOUT: u64 = 3600; pub use xmr_btc::bitcoin::*;
// This is cut'n'paste from xmr_btc/tests/harness/wallet/bitcoin.rs pub const TX_LOCK_MINE_TIMEOUT: u64 = 3600;
#[derive(Debug)] #[derive(Debug)]
pub struct Wallet(pub bitcoin_harness::Wallet); pub struct Wallet(pub bitcoin_harness::Wallet);

View File

@ -34,10 +34,10 @@ pub enum Rsp {
pub struct SwapAmounts { pub struct SwapAmounts {
/// Amount of BTC to swap. /// Amount of BTC to swap.
#[serde(with = "::bitcoin::util::amount::serde::as_sat")] #[serde(with = "::bitcoin::util::amount::serde::as_sat")]
pub btc: ::bitcoin::Amount, pub btc: bitcoin::Amount,
/// Amount of XMR to swap. /// Amount of XMR to swap.
#[serde(with = "xmr_btc::serde::monero_amount")] #[serde(with = "xmr_btc::serde::monero_amount")]
pub xmr: xmr_btc::monero::Amount, pub xmr: monero::Amount,
} }
// TODO: Display in XMR and BTC (not picos and sats). // TODO: Display in XMR and BTC (not picos and sats).

View File

@ -1,15 +1,11 @@
use anyhow::Result; use anyhow::Result;
use async_trait::async_trait; use async_trait::async_trait;
use backoff::{backoff::Constant as ConstantBackoff, future::FutureOperation as _}; use backoff::{backoff::Constant as ConstantBackoff, future::FutureOperation as _};
use monero::{Address, Network, PrivateKey};
use monero_harness::rpc::wallet; use monero_harness::rpc::wallet;
use std::{str::FromStr, time::Duration}; use std::{str::FromStr, time::Duration};
use url::Url; use url::Url;
pub use xmr_btc::monero::{
Amount, CreateWalletForOutput, InsufficientFunds, PrivateViewKey, PublicKey, PublicViewKey, pub use xmr_btc::monero::*;
Transfer, TransferProof, TxHash, WatchForTransfer, *,
};
pub struct Wallet(pub wallet::Client); pub struct Wallet(pub wallet::Client);

View File

@ -11,6 +11,7 @@
//! was deemed too complicated for the time being. //! was deemed too complicated for the time being.
use crate::{ use crate::{
bitcoin, monero,
monero::CreateWalletForOutput, monero::CreateWalletForOutput,
state::{Alice, Bob, Swap}, state::{Alice, Bob, Swap},
}; };
@ -23,13 +24,13 @@ use futures::{
use sha2::Sha256; use sha2::Sha256;
use tracing::info; use tracing::info;
use xmr_btc::bitcoin::{ use xmr_btc::bitcoin::{
poll_until_block_height_is_gte, BroadcastSignedTransaction, TransactionBlockHeight, TxCancel, poll_until_block_height_is_gte, BroadcastSignedTransaction, TransactionBlockHeight,
TxPunish, TxRedeem, TxRefund, WatchForRawTransaction, WatchForRawTransaction,
}; };
pub async fn recover( pub async fn recover(
bitcoin_wallet: crate::bitcoin::Wallet, bitcoin_wallet: bitcoin::Wallet,
monero_wallet: crate::monero::Wallet, monero_wallet: monero::Wallet,
state: Swap, state: Swap,
) -> Result<()> { ) -> Result<()> {
match state { match state {
@ -39,8 +40,8 @@ pub async fn recover(
} }
pub async fn alice_recover( pub async fn alice_recover(
bitcoin_wallet: crate::bitcoin::Wallet, bitcoin_wallet: bitcoin::Wallet,
monero_wallet: crate::monero::Wallet, monero_wallet: monero::Wallet,
state: Alice, state: Alice,
) -> Result<()> { ) -> Result<()> {
match state { match state {
@ -50,7 +51,7 @@ pub async fn alice_recover(
Alice::XmrLocked(state) => { Alice::XmrLocked(state) => {
info!("Monero still locked up"); info!("Monero still locked up");
let tx_cancel = TxCancel::new( let tx_cancel = bitcoin::TxCancel::new(
&state.tx_lock, &state.tx_lock,
state.refund_timelock, state.refund_timelock,
state.a.public(), state.a.public(),
@ -104,7 +105,7 @@ pub async fn alice_recover(
); );
pin_mut!(poll_until_bob_can_be_punished); pin_mut!(poll_until_bob_can_be_punished);
let tx_refund = TxRefund::new(&tx_cancel, &state.refund_address); let tx_refund = bitcoin::TxRefund::new(&tx_cancel, &state.refund_address);
info!("Waiting for either Bitcoin refund or punish timelock"); info!("Waiting for either Bitcoin refund or punish timelock");
match select( match select(
@ -126,13 +127,9 @@ pub async fn alice_recover(
.a .a
.encsign(state.S_b_bitcoin.clone(), tx_refund.digest()); .encsign(state.S_b_bitcoin.clone(), tx_refund.digest());
let s_b = xmr_btc::bitcoin::recover( let s_b = bitcoin::recover(state.S_b_bitcoin, tx_refund_sig, tx_refund_encsig)?;
state.S_b_bitcoin,
tx_refund_sig,
tx_refund_encsig,
)?;
let s_b = monero::PrivateKey::from_scalar( let s_b = monero::PrivateKey::from_scalar(
xmr_btc::monero::Scalar::from_bytes_mod_order(s_b.to_bytes()), monero::Scalar::from_bytes_mod_order(s_b.to_bytes()),
); );
monero_wallet monero_wallet
@ -143,8 +140,11 @@ pub async fn alice_recover(
Either::Right(_) => { Either::Right(_) => {
info!("Punish timelock reached, attempting to punish Bob"); info!("Punish timelock reached, attempting to punish Bob");
let tx_punish = let tx_punish = bitcoin::TxPunish::new(
TxPunish::new(&tx_cancel, &state.punish_address, state.punish_timelock); &tx_cancel,
&state.punish_address,
state.punish_timelock,
);
let sig_a = state.a.sign(tx_punish.digest()); let sig_a = state.a.sign(tx_punish.digest());
let sig_b = state.tx_punish_sig_bob.clone(); let sig_b = state.tx_punish_sig_bob.clone();
@ -183,7 +183,7 @@ pub async fn alice_recover(
} else { } else {
info!("Refund timelock reached"); info!("Refund timelock reached");
let tx_cancel = TxCancel::new( let tx_cancel = bitcoin::TxCancel::new(
&state.tx_lock, &state.tx_lock,
state.refund_timelock, state.refund_timelock,
state.a.public(), state.a.public(),
@ -226,7 +226,7 @@ pub async fn alice_recover(
); );
pin_mut!(poll_until_bob_can_be_punished); pin_mut!(poll_until_bob_can_be_punished);
let tx_refund = TxRefund::new(&tx_cancel, &state.refund_address); let tx_refund = bitcoin::TxRefund::new(&tx_cancel, &state.refund_address);
info!("Waiting for either Bitcoin refund or punish timelock"); info!("Waiting for either Bitcoin refund or punish timelock");
match select( match select(
@ -248,13 +248,10 @@ pub async fn alice_recover(
.a .a
.encsign(state.S_b_bitcoin.clone(), tx_refund.digest()); .encsign(state.S_b_bitcoin.clone(), tx_refund.digest());
let s_b = xmr_btc::bitcoin::recover( let s_b =
state.S_b_bitcoin, bitcoin::recover(state.S_b_bitcoin, tx_refund_sig, tx_refund_encsig)?;
tx_refund_sig,
tx_refund_encsig,
)?;
let s_b = monero::PrivateKey::from_scalar( let s_b = monero::PrivateKey::from_scalar(
xmr_btc::monero::Scalar::from_bytes_mod_order(s_b.to_bytes()), monero::Scalar::from_bytes_mod_order(s_b.to_bytes()),
); );
monero_wallet monero_wallet
@ -265,8 +262,11 @@ pub async fn alice_recover(
Either::Right(_) => { Either::Right(_) => {
info!("Punish timelock reached, attempting to punish Bob"); info!("Punish timelock reached, attempting to punish Bob");
let tx_punish = let tx_punish = bitcoin::TxPunish::new(
TxPunish::new(&tx_cancel, &state.punish_address, state.punish_timelock); &tx_cancel,
&state.punish_address,
state.punish_timelock,
);
let sig_a = state.a.sign(tx_punish.digest()); let sig_a = state.a.sign(tx_punish.digest());
let sig_b = state.tx_punish_sig_bob.clone(); let sig_b = state.tx_punish_sig_bob.clone();
@ -288,13 +288,13 @@ pub async fn alice_recover(
Alice::BtcPunishable(state) => { Alice::BtcPunishable(state) => {
info!("Punish timelock reached, attempting to punish Bob"); info!("Punish timelock reached, attempting to punish Bob");
let tx_cancel = TxCancel::new( let tx_cancel = bitcoin::TxCancel::new(
&state.tx_lock, &state.tx_lock,
state.refund_timelock, state.refund_timelock,
state.a.public(), state.a.public(),
state.B.clone(), state.B.clone(),
); );
let tx_refund = TxRefund::new(&tx_cancel, &state.refund_address); let tx_refund = bitcoin::TxRefund::new(&tx_cancel, &state.refund_address);
info!("Checking if Bitcoin has already been refunded"); info!("Checking if Bitcoin has already been refunded");
@ -314,13 +314,9 @@ pub async fn alice_recover(
.a .a
.encsign(state.S_b_bitcoin.clone(), tx_refund.digest()); .encsign(state.S_b_bitcoin.clone(), tx_refund.digest());
let s_b = xmr_btc::bitcoin::recover( let s_b = bitcoin::recover(state.S_b_bitcoin, tx_refund_sig, tx_refund_encsig)?;
state.S_b_bitcoin,
tx_refund_sig,
tx_refund_encsig,
)?;
let s_b = monero::PrivateKey::from_scalar( let s_b = monero::PrivateKey::from_scalar(
xmr_btc::monero::Scalar::from_bytes_mod_order(s_b.to_bytes()), monero::Scalar::from_bytes_mod_order(s_b.to_bytes()),
); );
monero_wallet monero_wallet
@ -331,8 +327,11 @@ pub async fn alice_recover(
Err(_) => { Err(_) => {
info!("Bitcoin not yet refunded"); info!("Bitcoin not yet refunded");
let tx_punish = let tx_punish = bitcoin::TxPunish::new(
TxPunish::new(&tx_cancel, &state.punish_address, state.punish_timelock); &tx_cancel,
&state.punish_address,
state.punish_timelock,
);
let sig_a = state.a.sign(tx_punish.digest()); let sig_a = state.a.sign(tx_punish.digest());
let sig_b = state.tx_punish_sig_bob.clone(); let sig_b = state.tx_punish_sig_bob.clone();
@ -379,7 +378,7 @@ pub async fn bob_recover(
Bob::BtcLocked(state) | Bob::XmrLocked(state) | Bob::BtcRefundable(state) => { Bob::BtcLocked(state) | Bob::XmrLocked(state) | Bob::BtcRefundable(state) => {
info!("Bitcoin may still be locked up, attempting to refund"); info!("Bitcoin may still be locked up, attempting to refund");
let tx_cancel = TxCancel::new( let tx_cancel = bitcoin::TxCancel::new(
&state.tx_lock, &state.tx_lock,
state.refund_timelock, state.refund_timelock,
state.A.clone(), state.A.clone(),
@ -420,10 +419,11 @@ pub async fn bob_recover(
bitcoin_wallet bitcoin_wallet
.broadcast_signed_transaction(tx_cancel) .broadcast_signed_transaction(tx_cancel)
.await?; .await?;
info!("Successfully published Bitcoin cancel transaction");
} }
let tx_refund = TxRefund::new(&tx_cancel, &state.refund_address); info!("Confirmed that Bitcoin cancel transaction is on the blockchain");
let tx_refund = bitcoin::TxRefund::new(&tx_cancel, &state.refund_address);
let signed_tx_refund = { let signed_tx_refund = {
let adaptor = Adaptor::<Sha256, Deterministic<Sha256>>::default(); let adaptor = Adaptor::<Sha256, Deterministic<Sha256>>::default();
let sig_a = adaptor let sig_a = adaptor
@ -449,7 +449,7 @@ pub async fn bob_recover(
Bob::BtcRedeemed(state) => { Bob::BtcRedeemed(state) => {
info!("Bitcoin was redeemed, attempting to redeem monero"); info!("Bitcoin was redeemed, attempting to redeem monero");
let tx_redeem = TxRedeem::new(&state.tx_lock, &state.redeem_address); let tx_redeem = bitcoin::TxRedeem::new(&state.tx_lock, &state.redeem_address);
let tx_redeem_published = bitcoin_wallet let tx_redeem_published = bitcoin_wallet
.0 .0
.get_raw_transaction(tx_redeem.txid()) .get_raw_transaction(tx_redeem.txid())
@ -461,11 +461,10 @@ pub async fn bob_recover(
let tx_redeem_sig = let tx_redeem_sig =
tx_redeem.extract_signature_by_key(tx_redeem_published, state.b.public())?; tx_redeem.extract_signature_by_key(tx_redeem_published, state.b.public())?;
let s_a = let s_a = bitcoin::recover(state.S_a_bitcoin, tx_redeem_sig, tx_redeem_encsig)?;
xmr_btc::bitcoin::recover(state.S_a_bitcoin, tx_redeem_sig, tx_redeem_encsig)?; let s_a = monero::PrivateKey::from_scalar(monero::Scalar::from_bytes_mod_order(
let s_a = monero::PrivateKey::from_scalar( s_a.to_bytes(),
xmr_btc::monero::Scalar::from_bytes_mod_order(s_a.to_bytes()), ));
);
let s_b = monero::PrivateKey { let s_b = monero::PrivateKey {
scalar: state.s_b.into_ed25519(), scalar: state.s_b.into_ed25519(),

View File

@ -6,6 +6,7 @@ use std::sync::Arc;
use swap::{alice, bob, network::transport::build, storage::Database}; use swap::{alice, bob, network::transport::build, storage::Database};
use tempfile::tempdir; use tempfile::tempdir;
use testcontainers::clients::Cli; use testcontainers::clients::Cli;
use xmr_btc::bitcoin;
// NOTE: For some reason running these tests overflows the stack. In order to // NOTE: For some reason running these tests overflows the stack. In order to
// mitigate this run them with: // mitigate this run them with:
@ -114,7 +115,7 @@ async fn swap() {
assert_eq!( assert_eq!(
btc_alice_final, btc_alice_final,
btc_alice + btc - bitcoin::Amount::from_sat(xmr_btc::bitcoin::TX_FEE) btc_alice + btc - bitcoin::Amount::from_sat(bitcoin::TX_FEE)
); );
assert!(btc_bob_final <= btc_bob - btc); assert!(btc_bob_final <= btc_bob - btc);

View File

@ -2,12 +2,7 @@ pub mod transactions;
use anyhow::{anyhow, bail, Result}; use anyhow::{anyhow, bail, Result};
use async_trait::async_trait; use async_trait::async_trait;
use bitcoin::{ use bitcoin::hashes::{hex::ToHex, Hash};
hashes::{hex::ToHex, Hash},
secp256k1,
util::psbt::PartiallySignedTransaction,
SigHash,
};
use ecdsa_fun::{adaptor::Adaptor, fun::Point, nonce::Deterministic, ECDSA}; use ecdsa_fun::{adaptor::Adaptor, fun::Point, nonce::Deterministic, ECDSA};
use miniscript::{Descriptor, Segwitv0}; use miniscript::{Descriptor, Segwitv0};
use rand::{CryptoRng, RngCore}; use rand::{CryptoRng, RngCore};
@ -15,9 +10,9 @@ use serde::{Deserialize, Serialize};
use sha2::Sha256; use sha2::Sha256;
use std::str::FromStr; use std::str::FromStr;
pub use crate::bitcoin::transactions::{TxCancel, TxLock, TxPunish, TxRedeem, TxRefund}; pub use bitcoin::{util::psbt::PartiallySignedTransaction, *};
pub use bitcoin::{Address, Amount, OutPoint, Transaction, Txid};
pub use ecdsa_fun::{adaptor::EncryptedSignature, fun::Scalar, Signature}; pub use ecdsa_fun::{adaptor::EncryptedSignature, fun::Scalar, Signature};
pub use transactions::{TxCancel, TxLock, TxPunish, TxRedeem, TxRefund};
pub const TX_FEE: u64 = 10_000; pub const TX_FEE: u64 = 10_000;

View File

@ -1,12 +1,13 @@
use crate::serde::monero_private_key; use crate::serde::monero_private_key;
use anyhow::Result; use anyhow::Result;
use async_trait::async_trait; use async_trait::async_trait;
pub use curve25519_dalek::scalar::Scalar;
pub use monero::{Address, PrivateKey, PublicKey};
use rand::{CryptoRng, RngCore}; use rand::{CryptoRng, RngCore};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::ops::{Add, Sub}; use std::ops::{Add, Sub};
pub use curve25519_dalek::scalar::Scalar;
pub use monero::*;
pub const MIN_CONFIRMATIONS: u32 = 10; pub const MIN_CONFIRMATIONS: u32 = 10;
pub fn random_private_key<R: RngCore + CryptoRng>(rng: &mut R) -> PrivateKey { pub fn random_private_key<R: RngCore + CryptoRng>(rng: &mut R) -> PrivateKey {

View File

@ -1,12 +1,11 @@
use anyhow::Result; use anyhow::Result;
use async_trait::async_trait; use async_trait::async_trait;
use backoff::{backoff::Constant as ConstantBackoff, future::FutureOperation as _}; use backoff::{backoff::Constant as ConstantBackoff, future::FutureOperation as _};
use monero::{Address, Network, PrivateKey};
use monero_harness::rpc::wallet; use monero_harness::rpc::wallet;
use std::{str::FromStr, time::Duration}; use std::{str::FromStr, time::Duration};
use xmr_btc::monero::{ use xmr_btc::monero::{
Amount, CreateWalletForOutput, InsufficientFunds, PrivateViewKey, PublicKey, PublicViewKey, Address, Amount, CreateWalletForOutput, InsufficientFunds, Network, PrivateKey, PrivateViewKey,
Transfer, TransferProof, TxHash, WatchForTransfer, PublicKey, PublicViewKey, Transfer, TransferProof, TxHash, WatchForTransfer,
}; };
pub struct Wallet(pub wallet::Client); pub struct Wallet(pub wallet::Client);

View File

@ -20,7 +20,7 @@ use tokio::sync::Mutex;
use tracing::info; use tracing::info;
use xmr_btc::{ use xmr_btc::{
alice::{self, ReceiveBitcoinRedeemEncsig}, alice::{self, ReceiveBitcoinRedeemEncsig},
bitcoin::{BroadcastSignedTransaction, EncryptedSignature, SignTxLock}, bitcoin::{self, BroadcastSignedTransaction, EncryptedSignature, SignTxLock},
bob::{self, ReceiveTransferProof}, bob::{self, ReceiveTransferProof},
monero::{CreateWalletForOutput, Transfer, TransferProof}, monero::{CreateWalletForOutput, Transfer, TransferProof},
}; };
@ -309,8 +309,7 @@ async fn on_chain_happy_path() {
assert_eq!( assert_eq!(
alice_final_btc_balance, alice_final_btc_balance,
initial_balances.alice_btc + swap_amounts.btc initial_balances.alice_btc + swap_amounts.btc - bitcoin::Amount::from_sat(bitcoin::TX_FEE)
- bitcoin::Amount::from_sat(xmr_btc::bitcoin::TX_FEE)
); );
assert_eq!( assert_eq!(
bob_final_btc_balance, bob_final_btc_balance,
@ -411,7 +410,7 @@ async fn on_chain_both_refund_if_alice_never_redeems() {
bob_final_btc_balance, bob_final_btc_balance,
// The 2 * TX_FEE corresponds to tx_refund and tx_cancel. // The 2 * TX_FEE corresponds to tx_refund and tx_cancel.
initial_balances.bob_btc initial_balances.bob_btc
- bitcoin::Amount::from_sat(2 * xmr_btc::bitcoin::TX_FEE) - bitcoin::Amount::from_sat(2 * bitcoin::TX_FEE)
- lock_tx_bitcoin_fee - lock_tx_bitcoin_fee
); );
@ -508,7 +507,7 @@ async fn on_chain_alice_punishes_if_bob_never_acts_after_fund() {
assert_eq!( assert_eq!(
alice_final_btc_balance, alice_final_btc_balance,
initial_balances.alice_btc + swap_amounts.btc initial_balances.alice_btc + swap_amounts.btc
- bitcoin::Amount::from_sat(2 * xmr_btc::bitcoin::TX_FEE) - bitcoin::Amount::from_sat(2 * bitcoin::TX_FEE)
); );
assert_eq!( assert_eq!(
bob_final_btc_balance, bob_final_btc_balance,