Merge pull request #18 from comit-network/db-dupe-code

Improve serde implementations
This commit is contained in:
rishflab 2020-10-26 12:16:46 +11:00 committed by GitHub
commit 964640154d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 44 additions and 65 deletions

View File

@ -81,22 +81,19 @@ 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::{cross_curve_dleq, curve25519_dalek, 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 {
A: xmr_btc::bitcoin::PublicKey, A: xmr_btc::bitcoin::PublicKey,
a: xmr_btc::bitcoin::SecretKey, a: xmr_btc::bitcoin::SecretKey,
s_a: CrossCurveScalar, s_a: cross_curve_dleq::Scalar,
#[serde(with = "monero_private_key")] #[serde(with = "monero_private_key")]
s_b: monero::PrivateKey, s_b: monero::PrivateKey,
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,
@ -111,8 +108,9 @@ mod tests {
let db = Database::open(db_dir.path()).unwrap(); let db = Database::open(db_dir.path()).unwrap();
let a = xmr_btc::bitcoin::SecretKey::new_random(&mut OsRng); let a = xmr_btc::bitcoin::SecretKey::new_random(&mut OsRng);
let s_a = CrossCurveScalar::random(&mut OsRng); let s_a = cross_curve_dleq::Scalar::random(&mut OsRng);
let s_b = monero::PrivateKey::from_scalar(Curve25519Scalar::random(&mut OsRng)); let s_b =
monero::PrivateKey::from_scalar(curve25519_dalek::scalar::Scalar::random(&mut OsRng));
let v_a = xmr_btc::monero::PrivateViewKey::new_random(&mut OsRng); let v_a = xmr_btc::monero::PrivateViewKey::new_random(&mut OsRng);
let S_a_monero = monero::PublicKey::from_private_key(&monero::PrivateKey { let S_a_monero = monero::PublicKey::from_private_key(&monero::PrivateKey {
scalar: s_a.into_ed25519(), scalar: s_a.into_ed25519(),

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};
@ -134,10 +133,9 @@ impl State {
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
pub struct State0 { pub struct State0 {
a: bitcoin::SecretKey, a: bitcoin::SecretKey,
//#[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 +225,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 +264,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 +340,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 +394,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 +498,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 +592,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

@ -52,8 +52,8 @@ pub mod monero;
pub mod serde; pub mod serde;
pub mod transport; pub mod transport;
pub use cross_curve_dleq::Scalar as CrossCurveScalar; pub use cross_curve_dleq;
pub use curve25519_dalek::scalar::Scalar as Curve25519Scalar; pub use curve25519_dalek;
use async_trait::async_trait; use async_trait::async_trait;
use ecdsa_fun::{adaptor::Adaptor, nonce::Deterministic}; use ecdsa_fun::{adaptor::Adaptor, nonce::Deterministic};

View File

@ -1,38 +1,42 @@
pub mod monero_private_key { pub mod monero_private_key {
use serde::{de, de::Visitor, Deserializer, Serializer}; use monero::{
use std::fmt; consensus::{Decodable, Encodable},
PrivateKey,
};
use serde::{de, de::Visitor, ser::Error, Deserializer, Serializer};
use std::{fmt, io::Cursor};
struct BytesVisitor; struct BytesVisitor;
impl<'de> Visitor<'de> for BytesVisitor { impl<'de> Visitor<'de> for BytesVisitor {
type Value = monero::PrivateKey; type Value = PrivateKey;
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "a string containing 32 bytes") write!(formatter, "a byte array representing a Monero private key")
} }
fn visit_bytes<E>(self, s: &[u8]) -> Result<Self::Value, E> fn visit_bytes<E>(self, s: &[u8]) -> Result<Self::Value, E>
where where
E: de::Error, E: de::Error,
{ {
if let Ok(key) = monero::PrivateKey::from_slice(s) { let mut s = s;
Ok(key) PrivateKey::consensus_decode(&mut s).map_err(|err| E::custom(format!("{:?}", err)))
} else {
Err(de::Error::invalid_length(s.len(), &self))
}
} }
} }
pub fn serialize<S>(x: &monero::PrivateKey, s: S) -> Result<S::Ok, S::Error> pub fn serialize<S>(x: &PrivateKey, s: S) -> Result<S::Ok, S::Error>
where where
S: Serializer, S: Serializer,
{ {
s.serialize_bytes(x.as_bytes()) let mut bytes = Cursor::new(vec![]);
x.consensus_encode(&mut bytes)
.map_err(|err| S::Error::custom(format!("{:?}", err)))?;
s.serialize_bytes(bytes.into_inner().as_ref())
} }
pub fn deserialize<'de, D>( pub fn deserialize<'de, D>(
deserializer: D, deserializer: D,
) -> Result<monero::PrivateKey, <D as Deserializer<'de>>::Error> ) -> Result<PrivateKey, <D as Deserializer<'de>>::Error>
where where
D: Deserializer<'de>, D: Deserializer<'de>,
{ {
@ -41,28 +45,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};
@ -96,7 +78,7 @@ mod tests {
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)] #[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct BitcoinAmount(#[serde(with = "bitcoin_amount")] ::bitcoin::Amount); pub struct MoneroAmount(#[serde(with = "monero_amount")] crate::monero::Amount);
#[test] #[test]
fn serde_monero_private_key() { fn serde_monero_private_key() {
@ -105,11 +87,12 @@ 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] #[test]
fn serde_bitcoin_amount() { fn serde_monero_amount() {
let amount = BitcoinAmount(::bitcoin::Amount::from_sat(100)); let amount = MoneroAmount(crate::monero::Amount::from_piconero(1000));
let encoded = serde_cbor::to_vec(&amount).unwrap(); let encoded = serde_cbor::to_vec(&amount).unwrap();
let decoded: BitcoinAmount = serde_cbor::from_slice(&encoded).unwrap(); let decoded: MoneroAmount = serde_cbor::from_slice(&encoded).unwrap();
assert_eq!(amount, decoded); assert_eq!(amount, decoded);
} }
} }