From eed5e8e9a478ff465db41d867881972d6668df7a Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 22 Oct 2020 13:54:13 +1100 Subject: [PATCH] Move monero serde code to lib --- swap/src/lib.rs | 3 +-- swap/src/monero.rs | 27 --------------------------- xmr-btc/src/serde.rs | 35 ++++++++++++++++++++++++++++------- 3 files changed, 29 insertions(+), 36 deletions(-) delete mode 100644 swap/src/monero.rs diff --git a/swap/src/lib.rs b/swap/src/lib.rs index 99778b79..ad67aca7 100644 --- a/swap/src/lib.rs +++ b/swap/src/lib.rs @@ -4,7 +4,6 @@ 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; @@ -34,7 +33,7 @@ pub struct SwapAmounts { #[serde(with = "::bitcoin::util::amount::serde::as_sat")] pub btc: ::bitcoin::Amount, /// Amount of XMR to swap. - #[serde(with = "crate::monero::amount_serde")] + #[serde(with = "xmr_btc::serde::monero_amount")] pub xmr: xmr_btc::monero::Amount, } diff --git a/swap/src/monero.rs b/swap/src/monero.rs deleted file mode 100644 index c9ca0d30..00000000 --- a/swap/src/monero.rs +++ /dev/null @@ -1,27 +0,0 @@ -//! Monero stuff, for now just serde. - -// This has to be in a sub-module to use with serde derive. -pub mod amount_serde { - use serde::{de::Error, Deserialize, Deserializer, Serializer}; - use std::str::FromStr; - use xmr_btc::monero::Amount; - - pub fn serialize(value: &Amount, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str(&value.as_piconero().to_string()) - } - - pub fn deserialize<'de, D>(deserializer: D) -> Result>::Error> - where - D: Deserializer<'de>, - { - let value = String::deserialize(deserializer)?; - let value = - u64::from_str(value.as_str()).map_err(>::Error::custom)?; - let amount = Amount::from_piconero(value); - - Ok(amount) - } -} diff --git a/xmr-btc/src/serde.rs b/xmr-btc/src/serde.rs index e64cfd10..f8fa4ab9 100644 --- a/xmr-btc/src/serde.rs +++ b/xmr-btc/src/serde.rs @@ -132,23 +132,44 @@ pub mod monero_private_key { } pub mod bitcoin_amount { + use bitcoin::Amount; use serde::{Deserialize, Deserializer, Serializer}; - pub fn serialize(value: &bitcoin::Amount, serializer: S) -> Result + pub fn serialize(x: &Amount, s: S) -> Result where S: Serializer, { - serializer.serialize_u64(value.as_sat()) + s.serialize_u64(x.as_sat()) } - pub fn deserialize<'de, D>( - deserializer: D, - ) -> Result>::Error> + pub fn deserialize<'de, D>(deserializer: D) -> Result>::Error> where D: Deserializer<'de>, { - let value = u64::deserialize(deserializer)?; - let amount = bitcoin::Amount::from_sat(value); + let sats = u64::deserialize(deserializer)?; + let amount = Amount::from_sat(sats); + + Ok(amount) + } +} + +pub mod monero_amount { + use crate::monero::Amount; + use serde::{Deserialize, Deserializer, Serializer}; + + pub fn serialize(x: &Amount, s: S) -> Result + where + S: Serializer, + { + s.serialize_u64(x.as_piconero()) + } + + pub fn deserialize<'de, D>(deserializer: D) -> Result>::Error> + where + D: Deserializer<'de>, + { + let picos = u64::deserialize(deserializer)?; + let amount = Amount::from_piconero(picos); Ok(amount) }