diff --git a/swap/Cargo.toml b/swap/Cargo.toml index cb22e3d1..aa9973df 100644 --- a/swap/Cargo.toml +++ b/swap/Cargo.toml @@ -9,7 +9,7 @@ description = "XMR/BTC trustless atomic swaps." anyhow = "1" async-trait = "0.1" atty = "0.2" -bitcoin = "0.25" # TODO: Upgrade other crates in this repo to use this version. +bitcoin = { version = "0.25", features = ["rand", "use-serde"] } # TODO: Upgrade other crates in this repo to use this version. derivative = "2" futures = { version = "0.3", default-features = false } libp2p = { version = "0.28", default-features = false, features = ["tcp-tokio", "yamux", "mplex", "dns", "noise", "request-response"] } diff --git a/swap/src/bitcoin.rs b/swap/src/bitcoin.rs new file mode 100644 index 00000000..70bed6cf --- /dev/null +++ b/swap/src/bitcoin.rs @@ -0,0 +1 @@ +pub use bitcoin::Amount; diff --git a/swap/src/lib.rs b/swap/src/lib.rs index 11f688d6..9fded336 100644 --- a/swap/src/lib.rs +++ b/swap/src/lib.rs @@ -2,7 +2,9 @@ use serde::{Deserialize, Serialize}; use std::fmt::{self, Display}; pub mod alice; +pub mod bitcoin; pub mod bob; +pub mod monero; pub mod network; pub const ONE_BTC: u64 = 100_000_000; @@ -26,6 +28,7 @@ pub enum Rsp { #[derive(Copy, Clone, Debug, Serialize, Deserialize)] pub struct SwapParams { /// Amount of BTC to swap. + #[serde(with = "::bitcoin::util::amount::serde::as_sat")] pub btc: bitcoin::Amount, /// Amount of XMR to swap. pub xmr: monero::Amount, @@ -36,71 +39,3 @@ impl Display for SwapParams { write!(f, "{} for {}", self.btc, self.xmr) } } - -// FIXME: Amount modules are a quick hack so we can derive serde. - -pub mod monero { - use serde::{Deserialize, Serialize}; - use std::fmt; - - #[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq)] - pub struct Amount(u64); - - impl Amount { - /// Create an [Amount] with piconero precision and the given number of - /// piconeros. - /// - /// A piconero (a.k.a atomic unit) is equal to 1e-12 XMR. - pub fn from_piconero(amount: u64) -> Self { - Amount(amount) - } - pub fn as_piconero(&self) -> u64 { - self.0 - } - } - - impl From for u64 { - fn from(from: Amount) -> u64 { - from.0 - } - } - - impl fmt::Display for Amount { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{} piconeros", self.0) - } - } -} - -pub mod bitcoin { - use serde::{Deserialize, Serialize}; - use std::fmt; - - #[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq)] - pub struct Amount(u64); - - impl Amount { - /// The zero amount. - pub const ZERO: Amount = Amount(0); - /// Exactly one satoshi. - pub const ONE_SAT: Amount = Amount(1); - /// Exactly one bitcoin. - pub const ONE_BTC: Amount = Amount(100_000_000); - - /// Create an [Amount] with satoshi precision and the given number of - /// satoshis. - pub fn from_sat(satoshi: u64) -> Amount { - Amount(satoshi) - } - - pub fn as_sat(&self) -> u64 { - self.0 - } - } - - impl fmt::Display for Amount { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{} satoshis", self.0) - } - } -} diff --git a/swap/src/monero.rs b/swap/src/monero.rs new file mode 100644 index 00000000..940a1922 --- /dev/null +++ b/swap/src/monero.rs @@ -0,0 +1,30 @@ +use serde::{Deserialize, Serialize}; +use std::fmt; + +#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq)] +pub struct Amount(u64); + +impl Amount { + /// Create an [Amount] with piconero precision and the given number of + /// piconeros. + /// + /// A piconero (a.k.a atomic unit) is equal to 1e-12 XMR. + pub fn from_piconero(amount: u64) -> Self { + Amount(amount) + } + pub fn as_piconero(&self) -> u64 { + self.0 + } +} + +impl From for u64 { + fn from(from: Amount) -> u64 { + from.0 + } +} + +impl fmt::Display for Amount { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{} piconeros", self.0) + } +} diff --git a/swap/src/network/request_response.rs b/swap/src/network/request_response.rs index c986e2a9..deb19b73 100644 --- a/swap/src/network/request_response.rs +++ b/swap/src/network/request_response.rs @@ -15,6 +15,7 @@ pub const TIMEOUT: u64 = 3600; // One hour. /// Messages Bob sends to Alice. #[derive(Clone, Debug, Serialize, Deserialize)] pub enum BobToAlice { + #[serde(with = "::bitcoin::util::amount::serde::as_sat")] AmountsFromBtc(bitcoin::Amount), AmountsFromXmr(monero::Amount), /* TODO: How are we going to do this when the messages are not Clone?