From a29786e175f3049df8afb0d346415ce264cb1ded Mon Sep 17 00:00:00 2001 From: rishflab Date: Thu, 22 Oct 2020 19:41:52 +1100 Subject: [PATCH] Use external serialisation implementation --- xmr-btc/src/alice.rs | 10 ++---- xmr-btc/src/bob.rs | 8 +---- xmr-btc/src/serde.rs | 58 -------------------------------- xmr-btc/tests/harness/storage.rs | 3 +- 4 files changed, 4 insertions(+), 75 deletions(-) diff --git a/xmr-btc/src/alice.rs b/xmr-btc/src/alice.rs index bb512b14..e94062f1 100644 --- a/xmr-btc/src/alice.rs +++ b/xmr-btc/src/alice.rs @@ -3,7 +3,7 @@ use crate::{ bitcoin::{BroadcastSignedTransaction, WatchForRawTransaction}, bob, monero, monero::{CreateWalletForOutput, Transfer}, - serde::{bitcoin_amount, cross_curve_dleq_scalar}, + serde::bitcoin_amount, transport::{ReceiveMessage, SendMessage}, }; use anyhow::{anyhow, Result}; @@ -134,7 +134,7 @@ impl State { #[derive(Clone, Debug, Deserialize, Serialize)] pub struct State0 { a: bitcoin::SecretKey, - #[serde(with = "cross_curve_dleq_scalar")] + //#[serde(with = "cross_curve_dleq_scalar")] s_a: cross_curve_dleq::Scalar, v_a: monero::PrivateViewKey, #[serde(with = "bitcoin_amount")] @@ -223,7 +223,6 @@ impl State0 { pub struct State1 { a: bitcoin::SecretKey, B: bitcoin::PublicKey, - #[serde(with = "cross_curve_dleq_scalar")] s_a: cross_curve_dleq::Scalar, S_b_monero: monero::PublicKey, S_b_bitcoin: bitcoin::PublicKey, @@ -263,7 +262,6 @@ impl State1 { pub struct State2 { a: bitcoin::SecretKey, B: bitcoin::PublicKey, - #[serde(with = "cross_curve_dleq_scalar")] s_a: cross_curve_dleq::Scalar, S_b_monero: monero::PublicKey, S_b_bitcoin: bitcoin::PublicKey, @@ -340,7 +338,6 @@ impl State2 { pub struct State3 { pub a: bitcoin::SecretKey, pub B: bitcoin::PublicKey, - #[serde(with = "cross_curve_dleq_scalar")] pub s_a: cross_curve_dleq::Scalar, pub S_b_monero: monero::PublicKey, pub S_b_bitcoin: bitcoin::PublicKey, @@ -395,7 +392,6 @@ impl State3 { pub struct State4 { a: bitcoin::SecretKey, B: bitcoin::PublicKey, - #[serde(with = "cross_curve_dleq_scalar")] s_a: cross_curve_dleq::Scalar, S_b_monero: monero::PublicKey, S_b_bitcoin: bitcoin::PublicKey, @@ -500,7 +496,6 @@ impl State4 { pub struct State5 { a: bitcoin::SecretKey, B: bitcoin::PublicKey, - #[serde(with = "cross_curve_dleq_scalar")] s_a: cross_curve_dleq::Scalar, S_b_monero: monero::PublicKey, S_b_bitcoin: bitcoin::PublicKey, @@ -595,7 +590,6 @@ impl State5 { pub struct State6 { a: bitcoin::SecretKey, B: bitcoin::PublicKey, - #[serde(with = "cross_curve_dleq_scalar")] s_a: cross_curve_dleq::Scalar, S_b_monero: monero::PublicKey, S_b_bitcoin: bitcoin::PublicKey, diff --git a/xmr-btc/src/bob.rs b/xmr-btc/src/bob.rs index 60da0e69..d2df8558 100644 --- a/xmr-btc/src/bob.rs +++ b/xmr-btc/src/bob.rs @@ -6,7 +6,7 @@ use crate::{ }, monero, monero::{CreateWalletForOutput, WatchForTransfer}, - serde::{bitcoin_amount, cross_curve_dleq_scalar, monero_private_key}, + serde::{bitcoin_amount, monero_private_key}, transport::{ReceiveMessage, SendMessage}, }; use anyhow::{anyhow, Result}; @@ -109,7 +109,6 @@ impl_from_child_enum!(State5, State); #[derive(Debug, Deserialize, Serialize)] pub struct State0 { b: bitcoin::SecretKey, - #[serde(with = "cross_curve_dleq_scalar")] s_b: cross_curve_dleq::Scalar, v_b: monero::PrivateViewKey, #[serde(with = "bitcoin_amount")] @@ -200,7 +199,6 @@ impl State0 { pub struct State1 { A: bitcoin::PublicKey, b: bitcoin::SecretKey, - #[serde(with = "cross_curve_dleq_scalar")] s_b: cross_curve_dleq::Scalar, S_a_monero: monero::PublicKey, S_a_bitcoin: bitcoin::PublicKey, @@ -265,7 +263,6 @@ impl State1 { pub struct State2 { pub A: bitcoin::PublicKey, pub b: bitcoin::SecretKey, - #[serde(with = "cross_curve_dleq_scalar")] pub s_b: cross_curve_dleq::Scalar, pub S_a_monero: monero::PublicKey, pub S_a_bitcoin: bitcoin::PublicKey, @@ -338,7 +335,6 @@ impl State2 { pub struct State3 { A: bitcoin::PublicKey, b: bitcoin::SecretKey, - #[serde(with = "cross_curve_dleq_scalar")] s_b: cross_curve_dleq::Scalar, S_a_monero: monero::PublicKey, S_a_bitcoin: bitcoin::PublicKey, @@ -451,7 +447,6 @@ impl State3 { pub struct State4 { A: bitcoin::PublicKey, b: bitcoin::SecretKey, - #[serde(with = "cross_curve_dleq_scalar")] s_b: cross_curve_dleq::Scalar, S_a_monero: monero::PublicKey, S_a_bitcoin: bitcoin::PublicKey, @@ -522,7 +517,6 @@ pub struct State5 { b: bitcoin::SecretKey, #[serde(with = "monero_private_key")] s_a: monero::PrivateKey, - #[serde(with = "cross_curve_dleq_scalar")] s_b: cross_curve_dleq::Scalar, S_a_monero: monero::PublicKey, S_a_bitcoin: bitcoin::PublicKey, diff --git a/xmr-btc/src/serde.rs b/xmr-btc/src/serde.rs index 78b1730b..a9b5a595 100644 --- a/xmr-btc/src/serde.rs +++ b/xmr-btc/src/serde.rs @@ -1,48 +1,3 @@ -pub mod cross_curve_dleq_scalar { - use serde::{de, de::Visitor, Deserializer, Serializer}; - use std::{convert::TryFrom, fmt}; - - struct Bytes32Visitor; - - impl<'de> Visitor<'de> for Bytes32Visitor { - type Value = cross_curve_dleq::Scalar; - - fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(formatter, "a string containing 32 bytes") - } - - fn visit_bytes(self, s: &[u8]) -> Result - where - E: de::Error, - { - if let Ok(value) = <[u8; 32]>::try_from(s) { - Ok(cross_curve_dleq::Scalar::from(value)) - } else { - Err(de::Error::invalid_length(s.len(), &self)) - } - } - } - - pub fn serialize(x: &cross_curve_dleq::Scalar, s: S) -> Result - where - S: Serializer, - { - // Serialise as ed25519 because the inner bytes are private - // TODO: Open PR in cross_curve_dleq to allow accessing the inner bytes - s.serialize_bytes(&x.into_ed25519().to_bytes()) - } - - pub fn deserialize<'de, D>( - deserializer: D, - ) -> Result>::Error> - where - D: Deserializer<'de>, - { - let dleq = deserializer.deserialize_bytes(Bytes32Visitor)?; - Ok(dleq) - } -} - pub mod monero_private_key { use serde::{de, de::Visitor, Deserializer, Serializer}; use std::fmt; @@ -137,25 +92,12 @@ mod tests { use rand::rngs::OsRng; use serde::{Deserialize, Serialize}; - #[derive(Debug, Serialize, Deserialize, PartialEq)] - pub struct CrossCurveDleqScalar( - #[serde(with = "cross_curve_dleq_scalar")] cross_curve_dleq::Scalar, - ); - #[derive(Debug, Serialize, Deserialize, PartialEq)] 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] - fn serde_cross_curv_dleq_scalar() { - let scalar = CrossCurveDleqScalar(cross_curve_dleq::Scalar::random(&mut OsRng)); - let encoded = serde_cbor::to_vec(&scalar).unwrap(); - let decoded: CrossCurveDleqScalar = serde_cbor::from_slice(&encoded).unwrap(); - assert_eq!(scalar, decoded); - } - #[test] fn serde_monero_private_key() { let key = MoneroPrivateKey(monero::PrivateKey::from_scalar(Scalar::random(&mut OsRng))); diff --git a/xmr-btc/tests/harness/storage.rs b/xmr-btc/tests/harness/storage.rs index 4b46c9d3..5fbcc40f 100644 --- a/xmr-btc/tests/harness/storage.rs +++ b/xmr-btc/tests/harness/storage.rs @@ -81,13 +81,12 @@ mod tests { use curve25519_dalek::scalar::Scalar; use ecdsa_fun::fun::rand_core::OsRng; use std::str::FromStr; - use xmr_btc::serde::{bitcoin_amount, cross_curve_dleq_scalar, monero_private_key}; + use xmr_btc::serde::{bitcoin_amount, monero_private_key}; #[derive(Debug, Serialize, Deserialize, PartialEq)] pub struct TestState { A: xmr_btc::bitcoin::PublicKey, a: xmr_btc::bitcoin::SecretKey, - #[serde(with = "cross_curve_dleq_scalar")] s_a: ::cross_curve_dleq::Scalar, #[serde(with = "monero_private_key")] s_b: monero::PrivateKey,