Remove custom implementation of bitcoin amount serde

This is unnecessary as rust-bitcoin provides it.
This commit is contained in:
Franck Royer 2020-10-22 15:06:05 +11:00 committed by rishflab
parent 3b14a0e4b3
commit 9ea708b2a5
4 changed files with 17 additions and 53 deletions

View File

@ -81,10 +81,7 @@ mod tests {
use rand::rngs::OsRng; use rand::rngs::OsRng;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::str::FromStr; use std::str::FromStr;
use xmr_btc::{ use xmr_btc::serde::monero_private_key;
serde::{bitcoin_amount, monero_private_key},
CrossCurveScalar, Curve25519Scalar,
};
#[derive(Debug, Serialize, Deserialize, PartialEq)] #[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct TestState { pub struct TestState {
@ -96,7 +93,7 @@ mod tests {
S_a_monero: ::monero::PublicKey, S_a_monero: ::monero::PublicKey,
S_a_bitcoin: xmr_btc::bitcoin::PublicKey, S_a_bitcoin: xmr_btc::bitcoin::PublicKey,
v: xmr_btc::monero::PrivateViewKey, v: xmr_btc::monero::PrivateViewKey,
#[serde(with = "bitcoin_amount")] #[serde(with = "::bitcoin::util::amount::serde::as_sat")]
btc: ::bitcoin::Amount, btc: ::bitcoin::Amount,
xmr: xmr_btc::monero::Amount, xmr: xmr_btc::monero::Amount,
refund_timelock: u32, refund_timelock: u32,

View File

@ -3,7 +3,6 @@ use crate::{
bitcoin::{BroadcastSignedTransaction, WatchForRawTransaction}, bitcoin::{BroadcastSignedTransaction, WatchForRawTransaction},
bob, monero, bob, monero,
monero::{CreateWalletForOutput, Transfer}, monero::{CreateWalletForOutput, Transfer},
serde::bitcoin_amount,
transport::{ReceiveMessage, SendMessage}, transport::{ReceiveMessage, SendMessage},
}; };
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
@ -137,7 +136,7 @@ pub struct State0 {
//#[serde(with = "cross_curve_dleq_scalar")] //#[serde(with = "cross_curve_dleq_scalar")]
s_a: cross_curve_dleq::Scalar, s_a: cross_curve_dleq::Scalar,
v_a: monero::PrivateViewKey, v_a: monero::PrivateViewKey,
#[serde(with = "bitcoin_amount")] #[serde(with = "::bitcoin::util::amount::serde::as_sat")]
btc: bitcoin::Amount, btc: bitcoin::Amount,
xmr: monero::Amount, xmr: monero::Amount,
refund_timelock: u32, refund_timelock: u32,
@ -227,7 +226,7 @@ pub struct State1 {
S_b_monero: monero::PublicKey, S_b_monero: monero::PublicKey,
S_b_bitcoin: bitcoin::PublicKey, S_b_bitcoin: bitcoin::PublicKey,
v: monero::PrivateViewKey, v: monero::PrivateViewKey,
#[serde(with = "bitcoin_amount")] #[serde(with = "::bitcoin::util::amount::serde::as_sat")]
btc: bitcoin::Amount, btc: bitcoin::Amount,
xmr: monero::Amount, xmr: monero::Amount,
refund_timelock: u32, refund_timelock: u32,
@ -266,7 +265,7 @@ pub struct State2 {
S_b_monero: monero::PublicKey, S_b_monero: monero::PublicKey,
S_b_bitcoin: bitcoin::PublicKey, S_b_bitcoin: bitcoin::PublicKey,
v: monero::PrivateViewKey, v: monero::PrivateViewKey,
#[serde(with = "bitcoin_amount")] #[serde(with = "::bitcoin::util::amount::serde::as_sat")]
btc: bitcoin::Amount, btc: bitcoin::Amount,
xmr: monero::Amount, xmr: monero::Amount,
refund_timelock: u32, refund_timelock: u32,
@ -342,7 +341,7 @@ pub struct State3 {
pub S_b_monero: monero::PublicKey, pub S_b_monero: monero::PublicKey,
pub S_b_bitcoin: bitcoin::PublicKey, pub S_b_bitcoin: bitcoin::PublicKey,
pub v: monero::PrivateViewKey, pub v: monero::PrivateViewKey,
#[serde(with = "bitcoin_amount")] #[serde(with = "::bitcoin::util::amount::serde::as_sat")]
pub btc: bitcoin::Amount, pub btc: bitcoin::Amount,
pub xmr: monero::Amount, pub xmr: monero::Amount,
pub refund_timelock: u32, pub refund_timelock: u32,
@ -396,7 +395,7 @@ pub struct State4 {
S_b_monero: monero::PublicKey, S_b_monero: monero::PublicKey,
S_b_bitcoin: bitcoin::PublicKey, S_b_bitcoin: bitcoin::PublicKey,
v: monero::PrivateViewKey, v: monero::PrivateViewKey,
#[serde(with = "bitcoin_amount")] #[serde(with = "::bitcoin::util::amount::serde::as_sat")]
btc: bitcoin::Amount, btc: bitcoin::Amount,
xmr: monero::Amount, xmr: monero::Amount,
refund_timelock: u32, refund_timelock: u32,
@ -500,7 +499,7 @@ pub struct State5 {
S_b_monero: monero::PublicKey, S_b_monero: monero::PublicKey,
S_b_bitcoin: bitcoin::PublicKey, S_b_bitcoin: bitcoin::PublicKey,
v: monero::PrivateViewKey, v: monero::PrivateViewKey,
#[serde(with = "bitcoin_amount")] #[serde(with = "::bitcoin::util::amount::serde::as_sat")]
btc: bitcoin::Amount, btc: bitcoin::Amount,
xmr: monero::Amount, xmr: monero::Amount,
refund_timelock: u32, refund_timelock: u32,
@ -594,7 +593,7 @@ pub struct State6 {
S_b_monero: monero::PublicKey, S_b_monero: monero::PublicKey,
S_b_bitcoin: bitcoin::PublicKey, S_b_bitcoin: bitcoin::PublicKey,
v: monero::PrivateViewKey, v: monero::PrivateViewKey,
#[serde(with = "bitcoin_amount")] #[serde(with = "::bitcoin::util::amount::serde::as_sat")]
btc: bitcoin::Amount, btc: bitcoin::Amount,
xmr: monero::Amount, xmr: monero::Amount,
refund_timelock: u32, refund_timelock: u32,

View File

@ -5,8 +5,7 @@ use crate::{
WatchForRawTransaction, WatchForRawTransaction,
}, },
monero, monero,
monero::{CreateWalletForOutput, WatchForTransfer}, serde::monero_private_key,
serde::{bitcoin_amount, monero_private_key},
transport::{ReceiveMessage, SendMessage}, transport::{ReceiveMessage, SendMessage},
}; };
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
@ -21,6 +20,7 @@ use sha2::Sha256;
use std::convert::{TryFrom, TryInto}; use std::convert::{TryFrom, TryInto};
pub mod message; pub mod message;
use crate::monero::{CreateWalletForOutput, WatchForTransfer};
pub use message::{Message, Message0, Message1, Message2, Message3}; pub use message::{Message, Message0, Message1, Message2, Message3};
// There are no guarantees that send_message and receive_massage do not block // There are no guarantees that send_message and receive_massage do not block
@ -111,7 +111,7 @@ pub struct State0 {
b: bitcoin::SecretKey, b: bitcoin::SecretKey,
s_b: cross_curve_dleq::Scalar, s_b: cross_curve_dleq::Scalar,
v_b: monero::PrivateViewKey, v_b: monero::PrivateViewKey,
#[serde(with = "bitcoin_amount")] #[serde(with = "::bitcoin::util::amount::serde::as_sat")]
btc: bitcoin::Amount, btc: bitcoin::Amount,
xmr: monero::Amount, xmr: monero::Amount,
refund_timelock: u32, refund_timelock: u32,
@ -203,7 +203,7 @@ pub struct State1 {
S_a_monero: monero::PublicKey, S_a_monero: monero::PublicKey,
S_a_bitcoin: bitcoin::PublicKey, S_a_bitcoin: bitcoin::PublicKey,
v: monero::PrivateViewKey, v: monero::PrivateViewKey,
#[serde(with = "bitcoin_amount")] #[serde(with = "::bitcoin::util::amount::serde::as_sat")]
btc: bitcoin::Amount, btc: bitcoin::Amount,
xmr: monero::Amount, xmr: monero::Amount,
refund_timelock: u32, refund_timelock: u32,
@ -267,7 +267,7 @@ pub struct State2 {
pub S_a_monero: monero::PublicKey, pub S_a_monero: monero::PublicKey,
pub S_a_bitcoin: bitcoin::PublicKey, pub S_a_bitcoin: bitcoin::PublicKey,
pub v: monero::PrivateViewKey, pub v: monero::PrivateViewKey,
#[serde(with = "bitcoin_amount")] #[serde(with = "::bitcoin::util::amount::serde::as_sat")]
btc: bitcoin::Amount, btc: bitcoin::Amount,
pub xmr: monero::Amount, pub xmr: monero::Amount,
pub refund_timelock: u32, pub refund_timelock: u32,
@ -339,7 +339,7 @@ pub struct State3 {
S_a_monero: monero::PublicKey, S_a_monero: monero::PublicKey,
S_a_bitcoin: bitcoin::PublicKey, S_a_bitcoin: bitcoin::PublicKey,
v: monero::PrivateViewKey, v: monero::PrivateViewKey,
#[serde(with = "bitcoin_amount")] #[serde(with = "::bitcoin::util::amount::serde::as_sat")]
btc: bitcoin::Amount, btc: bitcoin::Amount,
xmr: monero::Amount, xmr: monero::Amount,
refund_timelock: u32, refund_timelock: u32,
@ -451,7 +451,7 @@ pub struct State4 {
S_a_monero: monero::PublicKey, S_a_monero: monero::PublicKey,
S_a_bitcoin: bitcoin::PublicKey, S_a_bitcoin: bitcoin::PublicKey,
v: monero::PrivateViewKey, v: monero::PrivateViewKey,
#[serde(with = "bitcoin_amount")] #[serde(with = "::bitcoin::util::amount::serde::as_sat")]
btc: bitcoin::Amount, btc: bitcoin::Amount,
xmr: monero::Amount, xmr: monero::Amount,
refund_timelock: u32, refund_timelock: u32,
@ -521,7 +521,7 @@ pub struct State5 {
S_a_monero: monero::PublicKey, S_a_monero: monero::PublicKey,
S_a_bitcoin: bitcoin::PublicKey, S_a_bitcoin: bitcoin::PublicKey,
v: monero::PrivateViewKey, v: monero::PrivateViewKey,
#[serde(with = "bitcoin_amount")] #[serde(with = "::bitcoin::util::amount::serde::as_sat")]
btc: bitcoin::Amount, btc: bitcoin::Amount,
xmr: monero::Amount, xmr: monero::Amount,
refund_timelock: u32, refund_timelock: u32,

View File

@ -41,28 +41,6 @@ pub mod monero_private_key {
} }
} }
pub mod bitcoin_amount {
use bitcoin::Amount;
use serde::{Deserialize, Deserializer, Serializer};
pub fn serialize<S>(x: &Amount, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
s.serialize_u64(x.as_sat())
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<Amount, <D as Deserializer<'de>>::Error>
where
D: Deserializer<'de>,
{
let sats = u64::deserialize(deserializer)?;
let amount = Amount::from_sat(sats);
Ok(amount)
}
}
pub mod monero_amount { pub mod monero_amount {
use crate::monero::Amount; use crate::monero::Amount;
use serde::{Deserialize, Deserializer, Serializer}; use serde::{Deserialize, Deserializer, Serializer};
@ -95,9 +73,6 @@ mod tests {
#[derive(Debug, Serialize, Deserialize, PartialEq)] #[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct MoneroPrivateKey(#[serde(with = "monero_private_key")] crate::monero::PrivateKey); pub struct MoneroPrivateKey(#[serde(with = "monero_private_key")] crate::monero::PrivateKey);
#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct BitcoinAmount(#[serde(with = "bitcoin_amount")] ::bitcoin::Amount);
#[test] #[test]
fn serde_monero_private_key() { fn serde_monero_private_key() {
let key = MoneroPrivateKey(monero::PrivateKey::from_scalar(Scalar::random(&mut OsRng))); let key = MoneroPrivateKey(monero::PrivateKey::from_scalar(Scalar::random(&mut OsRng)));
@ -105,11 +80,4 @@ mod tests {
let decoded: MoneroPrivateKey = serde_cbor::from_slice(&encoded).unwrap(); let decoded: MoneroPrivateKey = serde_cbor::from_slice(&encoded).unwrap();
assert_eq!(key, decoded); assert_eq!(key, decoded);
} }
#[test]
fn serde_bitcoin_amount() {
let amount = BitcoinAmount(::bitcoin::Amount::from_sat(100));
let encoded = serde_cbor::to_vec(&amount).unwrap();
let decoded: BitcoinAmount = serde_cbor::from_slice(&encoded).unwrap();
assert_eq!(amount, decoded);
}
} }