mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-02-22 15:49:52 -05:00
Use external serialisation implementation
This commit is contained in:
parent
03dee7a3ea
commit
a29786e175
@ -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,
|
||||
|
@ -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,
|
||||
|
@ -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<E>(self, s: &[u8]) -> Result<Self::Value, E>
|
||||
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<S>(x: &cross_curve_dleq::Scalar, s: S) -> Result<S::Ok, S::Error>
|
||||
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<cross_curve_dleq::Scalar, <D as Deserializer<'de>>::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)));
|
||||
|
@ -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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user