diff --git a/swap/src/alice.rs b/swap/src/alice.rs index ac46a8ba..d0cedd23 100644 --- a/swap/src/alice.rs +++ b/swap/src/alice.rs @@ -26,13 +26,14 @@ use xmr_btc::{alice::State0, bob, monero}; pub type Swarm = libp2p::Swarm; +#[allow(unused_assignments)] // Due to the mutable message0? pub async fn swap( listen: Multiaddr, rng: &mut R, redeem_address: ::bitcoin::Address, punish_address: ::bitcoin::Address, ) -> Result<()> { - let message0: Option = None; + let mut message0: Option = None; let mut last_amounts: Option = None; let mut swarm = new_swarm(listen)?; @@ -48,10 +49,10 @@ pub async fn swap( last_amounts = Some(p); swarm.send(channel, AliceToBob::Amounts(p)); } - OutEvent::Message0 => { + OutEvent::Message0(msg) => { debug!("Got message0 from Bob"); // TODO: Do this in a more Rusty/functional way. - // message0 = Some(msg); + message0 = Some(msg); break; } }; @@ -79,7 +80,7 @@ pub async fn swap( let _state1 = match message0 { Some(msg) => state0.receive(msg), - None => todo!("implement serde on Message0"), + None => panic!("should have the message by here"), }; tracing::warn!("parking thread ..."); @@ -116,8 +117,7 @@ fn new_swarm(listen: Multiaddr) -> Result { pub enum OutEvent { ConnectionEstablished(PeerId), Request(amounts::OutEvent), - // Message0(bob::Message0), - Message0, + Message0(bob::Message0), } impl From for OutEvent { @@ -139,8 +139,7 @@ impl From for OutEvent { impl From for OutEvent { fn from(event: message0::OutEvent) -> Self { match event { - // message0::OutEvent::Msg(msg) => OutEvent::Message0(msg), - message0::OutEvent::Msg => OutEvent::Message0, + message0::OutEvent::Msg(msg) => OutEvent::Message0(msg), } } } diff --git a/swap/src/alice/message0.rs b/swap/src/alice/message0.rs index 699a7114..ad62586c 100644 --- a/swap/src/alice/message0.rs +++ b/swap/src/alice/message0.rs @@ -7,6 +7,7 @@ use libp2p::{ swarm::{NetworkBehaviourAction, NetworkBehaviourEventProcess, PollParameters}, NetworkBehaviour, }; +use rand::rngs::OsRng; use std::{ collections::VecDeque, task::{Context, Poll}, @@ -15,12 +16,11 @@ use std::{ use tracing::error; use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Protocol}; -use xmr_btc::alice::State0; +use xmr_btc::{alice::State0, bob}; #[derive(Debug)] pub enum OutEvent { - // Msg(bob::Message0), - Msg, + Msg(bob::Message0), } /// A `NetworkBehaviour` that represents getting the amounts of an XMR/BTC swap. @@ -85,18 +85,17 @@ impl NetworkBehaviourEventProcess> channel, }, } => match request { - BobToAlice::Message0 => { + BobToAlice::Message0(msg) => { let response = match &self.state { None => panic!("No state, did you forget to set it?"), - Some(_state) => { + Some(state) => { // TODO: Get OsRng from somewhere? - // AliceToBob::Message0(state.next_message(&mut OsRng)) - AliceToBob::Message0 + AliceToBob::Message0(state.next_message(&mut OsRng)) } }; self.rr.send_response(channel, response); - self.events.push_back(OutEvent::Msg); + self.events.push_back(OutEvent::Msg(msg)); } _ => panic!("unexpected request"), }, diff --git a/swap/src/bob.rs b/swap/src/bob.rs index 4504b3c9..ed38cf87 100644 --- a/swap/src/bob.rs +++ b/swap/src/bob.rs @@ -23,6 +23,7 @@ use crate::{ Cmd, Rsp, PUNISH_TIMELOCK, REFUND_TIMELOCK, }; use xmr_btc::{ + alice, bitcoin::BuildTxLockPsbt, bob::{self, State0}, }; @@ -33,7 +34,7 @@ pub async fn swap( mut cmd_tx: Sender, mut rsp_rx: Receiver, refund_address: ::bitcoin::Address, - _wallet: W, + wallet: W, ) -> Result<()> where W: BuildTxLockPsbt + Send + Sync + 'static, @@ -81,10 +82,8 @@ where ); swarm.send_message0(alice.clone(), state0.next_message(rng)); let _state1 = match swarm.next().await { - OutEvent::Message0 => { - // state0.receive(wallet, msg) // TODO: More graceful error - // handling. - println!("TODO: receive after serde is done for Message0") + OutEvent::Message0(msg) => { + state0.receive(&wallet, msg) // TODO: More graceful error handling. } other => panic!("unexpected event: {:?}", other), }; @@ -120,8 +119,7 @@ fn new_swarm() -> Result { pub enum OutEvent { ConnectionEstablished(PeerId), Amounts(amounts::OutEvent), - // Message0(alice::Message0), - Message0, + Message0(alice::Message0), } impl From for OutEvent { @@ -143,8 +141,7 @@ impl From for OutEvent { impl From for OutEvent { fn from(event: message0::OutEvent) -> Self { match event { - // message0::OutEvent::Msg(msg) => OutEvent::Message0(msg), - message0::OutEvent::Msg => OutEvent::Message0, + message0::OutEvent::Msg(msg) => OutEvent::Message0(msg), } } } diff --git a/swap/src/bob/amounts.rs b/swap/src/bob/amounts.rs index 18071985..4443a1e1 100644 --- a/swap/src/bob/amounts.rs +++ b/swap/src/bob/amounts.rs @@ -85,7 +85,7 @@ impl NetworkBehaviourEventProcess> }, } => match response { AliceToBob::Amounts(p) => self.events.push_back(OutEvent::Amounts(p)), - AliceToBob::Message0 => panic!("shouldn't get message0 here"), + AliceToBob::Message0(_) => panic!("shouldn't get message0 here"), }, RequestResponseEvent::InboundFailure { .. } => { diff --git a/swap/src/bob/message0.rs b/swap/src/bob/message0.rs index 7aba6f23..8e3a0ad8 100644 --- a/swap/src/bob/message0.rs +++ b/swap/src/bob/message0.rs @@ -14,12 +14,11 @@ use std::{ use tracing::error; use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Protocol}; -use xmr_btc::bob; +use xmr_btc::{alice, bob}; #[derive(Debug)] pub enum OutEvent { - // Msg(alice::Message0), - Msg, + Msg(alice::Message0), } /// A `NetworkBehaviour` that represents send/recv of message 0. @@ -47,9 +46,8 @@ impl Message0 { } } - pub fn send(&mut self, alice: PeerId, _msg: bob::Message0) { - // let msg = BobToAlice::Message0(msg); - let msg = BobToAlice::Message0; + pub fn send(&mut self, alice: PeerId, msg: bob::Message0) { + let msg = BobToAlice::Message0(msg); let _id = self.rr.send_request(&alice, msg); } @@ -81,8 +79,7 @@ impl NetworkBehaviourEventProcess> request_id: _, }, } => match response { - // AliceToBob::Message0(msg) => self.events.push_back(OutEvent::Msg(msg)), - AliceToBob::Message0 => self.events.push_back(OutEvent::Msg), + AliceToBob::Message0(msg) => self.events.push_back(OutEvent::Msg(msg)), AliceToBob::Amounts(_) => panic!("shouldn't get amounts here"), }, diff --git a/swap/src/network/request_response.rs b/swap/src/network/request_response.rs index 86420bee..b6e582c5 100644 --- a/swap/src/network/request_response.rs +++ b/swap/src/network/request_response.rs @@ -8,27 +8,27 @@ use serde::{Deserialize, Serialize}; use std::{fmt::Debug, io}; use crate::SwapParams; -use xmr_btc::monero; +use xmr_btc::{alice, bob, monero}; /// Time to wait for a response back once we send a request. pub const TIMEOUT: u64 = 3600; // One hour. /// Messages Bob sends to Alice. #[derive(Clone, Debug, Serialize, Deserialize)] +#[allow(clippy::large_enum_variant)] pub enum BobToAlice { #[serde(with = "::bitcoin::util::amount::serde::as_sat")] AmountsFromBtc(::bitcoin::Amount), AmountsFromXmr(monero::Amount), - Message0, - // Message0(bob::Message0), + Message0(bob::Message0), } /// Messages Alice sends to Bob. #[derive(Clone, Debug, Serialize, Deserialize)] +#[allow(clippy::large_enum_variant)] pub enum AliceToBob { Amounts(SwapParams), - Message0, - // Message0(alice::Message0), + Message0(alice::Message0), } #[derive(Debug, Clone, Copy, Default)] diff --git a/xmr-btc/src/alice/message.rs b/xmr-btc/src/alice/message.rs index 052731b1..ada3636f 100644 --- a/xmr-btc/src/alice/message.rs +++ b/xmr-btc/src/alice/message.rs @@ -1,8 +1,9 @@ use anyhow::Result; use ecdsa_fun::{adaptor::EncryptedSignature, Signature}; +use serde::{Deserialize, Serialize}; use std::convert::TryFrom; -use crate::{bitcoin, monero}; +use crate::{bitcoin, monero, serde::cross_curve_dleq_proof}; #[derive(Debug)] pub enum Message { @@ -11,11 +12,12 @@ pub enum Message { Message2(Message2), } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct Message0 { pub(crate) A: bitcoin::PublicKey, pub(crate) S_a_monero: monero::PublicKey, pub(crate) S_a_bitcoin: bitcoin::PublicKey, + #[serde(with = "cross_curve_dleq_proof")] pub(crate) dleq_proof_s_a: cross_curve_dleq::Proof, pub(crate) v_a: monero::PrivateViewKey, pub(crate) redeem_address: bitcoin::Address, diff --git a/xmr-btc/src/bob/message.rs b/xmr-btc/src/bob/message.rs index f45c3b92..465241f6 100644 --- a/xmr-btc/src/bob/message.rs +++ b/xmr-btc/src/bob/message.rs @@ -1,6 +1,7 @@ -use crate::{bitcoin, monero}; +use crate::{bitcoin, monero, serde::cross_curve_dleq_proof}; use anyhow::Result; use ecdsa_fun::{adaptor::EncryptedSignature, Signature}; +use serde::{Deserialize, Serialize}; use std::convert::TryFrom; #[derive(Clone, Debug)] @@ -11,11 +12,12 @@ pub enum Message { Message3(Message3), } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct Message0 { pub(crate) B: bitcoin::PublicKey, pub(crate) S_b_monero: monero::PublicKey, pub(crate) S_b_bitcoin: bitcoin::PublicKey, + #[serde(with = "cross_curve_dleq_proof")] pub(crate) dleq_proof_s_b: cross_curve_dleq::Proof, pub(crate) v_b: monero::PrivateViewKey, pub(crate) refund_address: bitcoin::Address, diff --git a/xmr-btc/src/serde.rs b/xmr-btc/src/serde.rs index e64cfd10..5e6e7052 100644 --- a/xmr-btc/src/serde.rs +++ b/xmr-btc/src/serde.rs @@ -88,6 +88,44 @@ pub mod cross_curve_dleq_scalar { } } +pub mod cross_curve_dleq_proof { + use serde::{de, de::Visitor, Deserializer, Serializer}; + use std::fmt; + + struct MyVisitor; + + impl<'de> Visitor<'de> for MyVisitor { + type Value = cross_curve_dleq::Proof; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(formatter, "todo") + } + + fn visit_bytes(self, _s: &[u8]) -> Result + where + E: de::Error, + { + todo!("visit_bytes") + } + } + + pub fn serialize(_x: &cross_curve_dleq::Proof, _s: S) -> Result + where + S: Serializer, + { + todo!("serialize") + } + + pub fn deserialize<'de, D>( + _deserializer: D, + ) -> Result>::Error> + where + D: Deserializer<'de>, + { + todo!("deserialize") + } +} + pub mod monero_private_key { use serde::{de, de::Visitor, Deserializer, Serializer}; use std::fmt;