Emit message0 out of NB (both parties)

This commit is contained in:
Tobin C. Harding 2020-10-22 11:04:49 +11:00
parent ad0d563d93
commit ad0d8d5713
9 changed files with 77 additions and 43 deletions

View File

@ -26,13 +26,14 @@ use xmr_btc::{alice::State0, bob, monero};
pub type Swarm = libp2p::Swarm<Alice>; pub type Swarm = libp2p::Swarm<Alice>;
#[allow(unused_assignments)] // Due to the mutable message0?
pub async fn swap<R: RngCore + CryptoRng>( pub async fn swap<R: RngCore + CryptoRng>(
listen: Multiaddr, listen: Multiaddr,
rng: &mut R, rng: &mut R,
redeem_address: ::bitcoin::Address, redeem_address: ::bitcoin::Address,
punish_address: ::bitcoin::Address, punish_address: ::bitcoin::Address,
) -> Result<()> { ) -> Result<()> {
let message0: Option<bob::Message0> = None; let mut message0: Option<bob::Message0> = None;
let mut last_amounts: Option<SwapParams> = None; let mut last_amounts: Option<SwapParams> = None;
let mut swarm = new_swarm(listen)?; let mut swarm = new_swarm(listen)?;
@ -48,10 +49,10 @@ pub async fn swap<R: RngCore + CryptoRng>(
last_amounts = Some(p); last_amounts = Some(p);
swarm.send(channel, AliceToBob::Amounts(p)); swarm.send(channel, AliceToBob::Amounts(p));
} }
OutEvent::Message0 => { OutEvent::Message0(msg) => {
debug!("Got message0 from Bob"); debug!("Got message0 from Bob");
// TODO: Do this in a more Rusty/functional way. // TODO: Do this in a more Rusty/functional way.
// message0 = Some(msg); message0 = Some(msg);
break; break;
} }
}; };
@ -79,7 +80,7 @@ pub async fn swap<R: RngCore + CryptoRng>(
let _state1 = match message0 { let _state1 = match message0 {
Some(msg) => state0.receive(msg), Some(msg) => state0.receive(msg),
None => todo!("implement serde on Message0"), None => panic!("should have the message by here"),
}; };
tracing::warn!("parking thread ..."); tracing::warn!("parking thread ...");
@ -116,8 +117,7 @@ fn new_swarm(listen: Multiaddr) -> Result<Swarm> {
pub enum OutEvent { pub enum OutEvent {
ConnectionEstablished(PeerId), ConnectionEstablished(PeerId),
Request(amounts::OutEvent), Request(amounts::OutEvent),
// Message0(bob::Message0), Message0(bob::Message0),
Message0,
} }
impl From<peer_tracker::OutEvent> for OutEvent { impl From<peer_tracker::OutEvent> for OutEvent {
@ -139,8 +139,7 @@ impl From<amounts::OutEvent> for OutEvent {
impl From<message0::OutEvent> for OutEvent { impl From<message0::OutEvent> for OutEvent {
fn from(event: message0::OutEvent) -> Self { fn from(event: message0::OutEvent) -> Self {
match event { match event {
// message0::OutEvent::Msg(msg) => OutEvent::Message0(msg), message0::OutEvent::Msg(msg) => OutEvent::Message0(msg),
message0::OutEvent::Msg => OutEvent::Message0,
} }
} }
} }

View File

@ -7,6 +7,7 @@ use libp2p::{
swarm::{NetworkBehaviourAction, NetworkBehaviourEventProcess, PollParameters}, swarm::{NetworkBehaviourAction, NetworkBehaviourEventProcess, PollParameters},
NetworkBehaviour, NetworkBehaviour,
}; };
use rand::rngs::OsRng;
use std::{ use std::{
collections::VecDeque, collections::VecDeque,
task::{Context, Poll}, task::{Context, Poll},
@ -15,12 +16,11 @@ use std::{
use tracing::error; use tracing::error;
use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Protocol}; use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Protocol};
use xmr_btc::alice::State0; use xmr_btc::{alice::State0, bob};
#[derive(Debug)] #[derive(Debug)]
pub enum OutEvent { pub enum OutEvent {
// Msg(bob::Message0), Msg(bob::Message0),
Msg,
} }
/// A `NetworkBehaviour` that represents getting the amounts of an XMR/BTC swap. /// A `NetworkBehaviour` that represents getting the amounts of an XMR/BTC swap.
@ -85,18 +85,17 @@ impl NetworkBehaviourEventProcess<RequestResponseEvent<BobToAlice, AliceToBob>>
channel, channel,
}, },
} => match request { } => match request {
BobToAlice::Message0 => { BobToAlice::Message0(msg) => {
let response = match &self.state { let response = match &self.state {
None => panic!("No state, did you forget to set it?"), None => panic!("No state, did you forget to set it?"),
Some(_state) => { Some(state) => {
// TODO: Get OsRng from somewhere? // TODO: Get OsRng from somewhere?
// AliceToBob::Message0(state.next_message(&mut OsRng)) AliceToBob::Message0(state.next_message(&mut OsRng))
AliceToBob::Message0
} }
}; };
self.rr.send_response(channel, response); self.rr.send_response(channel, response);
self.events.push_back(OutEvent::Msg); self.events.push_back(OutEvent::Msg(msg));
} }
_ => panic!("unexpected request"), _ => panic!("unexpected request"),
}, },

View File

@ -23,6 +23,7 @@ use crate::{
Cmd, Rsp, PUNISH_TIMELOCK, REFUND_TIMELOCK, Cmd, Rsp, PUNISH_TIMELOCK, REFUND_TIMELOCK,
}; };
use xmr_btc::{ use xmr_btc::{
alice,
bitcoin::BuildTxLockPsbt, bitcoin::BuildTxLockPsbt,
bob::{self, State0}, bob::{self, State0},
}; };
@ -33,7 +34,7 @@ pub async fn swap<W>(
mut cmd_tx: Sender<Cmd>, mut cmd_tx: Sender<Cmd>,
mut rsp_rx: Receiver<Rsp>, mut rsp_rx: Receiver<Rsp>,
refund_address: ::bitcoin::Address, refund_address: ::bitcoin::Address,
_wallet: W, wallet: W,
) -> Result<()> ) -> Result<()>
where where
W: BuildTxLockPsbt + Send + Sync + 'static, W: BuildTxLockPsbt + Send + Sync + 'static,
@ -81,10 +82,8 @@ where
); );
swarm.send_message0(alice.clone(), state0.next_message(rng)); swarm.send_message0(alice.clone(), state0.next_message(rng));
let _state1 = match swarm.next().await { let _state1 = match swarm.next().await {
OutEvent::Message0 => { OutEvent::Message0(msg) => {
// state0.receive(wallet, msg) // TODO: More graceful error state0.receive(&wallet, msg) // TODO: More graceful error handling.
// handling.
println!("TODO: receive after serde is done for Message0")
} }
other => panic!("unexpected event: {:?}", other), other => panic!("unexpected event: {:?}", other),
}; };
@ -120,8 +119,7 @@ fn new_swarm() -> Result<Swarm> {
pub enum OutEvent { pub enum OutEvent {
ConnectionEstablished(PeerId), ConnectionEstablished(PeerId),
Amounts(amounts::OutEvent), Amounts(amounts::OutEvent),
// Message0(alice::Message0), Message0(alice::Message0),
Message0,
} }
impl From<peer_tracker::OutEvent> for OutEvent { impl From<peer_tracker::OutEvent> for OutEvent {
@ -143,8 +141,7 @@ impl From<amounts::OutEvent> for OutEvent {
impl From<message0::OutEvent> for OutEvent { impl From<message0::OutEvent> for OutEvent {
fn from(event: message0::OutEvent) -> Self { fn from(event: message0::OutEvent) -> Self {
match event { match event {
// message0::OutEvent::Msg(msg) => OutEvent::Message0(msg), message0::OutEvent::Msg(msg) => OutEvent::Message0(msg),
message0::OutEvent::Msg => OutEvent::Message0,
} }
} }
} }

View File

@ -85,7 +85,7 @@ impl NetworkBehaviourEventProcess<RequestResponseEvent<BobToAlice, AliceToBob>>
}, },
} => match response { } => match response {
AliceToBob::Amounts(p) => self.events.push_back(OutEvent::Amounts(p)), 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 { .. } => { RequestResponseEvent::InboundFailure { .. } => {

View File

@ -14,12 +14,11 @@ use std::{
use tracing::error; use tracing::error;
use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Protocol}; use crate::network::request_response::{AliceToBob, BobToAlice, Codec, Protocol};
use xmr_btc::bob; use xmr_btc::{alice, bob};
#[derive(Debug)] #[derive(Debug)]
pub enum OutEvent { pub enum OutEvent {
// Msg(alice::Message0), Msg(alice::Message0),
Msg,
} }
/// A `NetworkBehaviour` that represents send/recv of message 0. /// 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) { pub fn send(&mut self, alice: PeerId, msg: bob::Message0) {
// let msg = BobToAlice::Message0(msg); let msg = BobToAlice::Message0(msg);
let msg = BobToAlice::Message0;
let _id = self.rr.send_request(&alice, msg); let _id = self.rr.send_request(&alice, msg);
} }
@ -81,8 +79,7 @@ impl NetworkBehaviourEventProcess<RequestResponseEvent<BobToAlice, AliceToBob>>
request_id: _, request_id: _,
}, },
} => match response { } => match response {
// AliceToBob::Message0(msg) => self.events.push_back(OutEvent::Msg(msg)), AliceToBob::Message0(msg) => self.events.push_back(OutEvent::Msg(msg)),
AliceToBob::Message0 => self.events.push_back(OutEvent::Msg),
AliceToBob::Amounts(_) => panic!("shouldn't get amounts here"), AliceToBob::Amounts(_) => panic!("shouldn't get amounts here"),
}, },

View File

@ -8,27 +8,27 @@ use serde::{Deserialize, Serialize};
use std::{fmt::Debug, io}; use std::{fmt::Debug, io};
use crate::SwapParams; 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. /// Time to wait for a response back once we send a request.
pub const TIMEOUT: u64 = 3600; // One hour. pub const TIMEOUT: u64 = 3600; // One hour.
/// Messages Bob sends to Alice. /// Messages Bob sends to Alice.
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
#[allow(clippy::large_enum_variant)]
pub enum BobToAlice { pub enum BobToAlice {
#[serde(with = "::bitcoin::util::amount::serde::as_sat")] #[serde(with = "::bitcoin::util::amount::serde::as_sat")]
AmountsFromBtc(::bitcoin::Amount), AmountsFromBtc(::bitcoin::Amount),
AmountsFromXmr(monero::Amount), AmountsFromXmr(monero::Amount),
Message0, Message0(bob::Message0),
// Message0(bob::Message0),
} }
/// Messages Alice sends to Bob. /// Messages Alice sends to Bob.
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
#[allow(clippy::large_enum_variant)]
pub enum AliceToBob { pub enum AliceToBob {
Amounts(SwapParams), Amounts(SwapParams),
Message0, Message0(alice::Message0),
// Message0(alice::Message0),
} }
#[derive(Debug, Clone, Copy, Default)] #[derive(Debug, Clone, Copy, Default)]

View File

@ -1,8 +1,9 @@
use anyhow::Result; use anyhow::Result;
use ecdsa_fun::{adaptor::EncryptedSignature, Signature}; use ecdsa_fun::{adaptor::EncryptedSignature, Signature};
use serde::{Deserialize, Serialize};
use std::convert::TryFrom; use std::convert::TryFrom;
use crate::{bitcoin, monero}; use crate::{bitcoin, monero, serde::cross_curve_dleq_proof};
#[derive(Debug)] #[derive(Debug)]
pub enum Message { pub enum Message {
@ -11,11 +12,12 @@ pub enum Message {
Message2(Message2), Message2(Message2),
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Message0 { pub struct Message0 {
pub(crate) A: bitcoin::PublicKey, pub(crate) A: bitcoin::PublicKey,
pub(crate) S_a_monero: monero::PublicKey, pub(crate) S_a_monero: monero::PublicKey,
pub(crate) S_a_bitcoin: bitcoin::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) dleq_proof_s_a: cross_curve_dleq::Proof,
pub(crate) v_a: monero::PrivateViewKey, pub(crate) v_a: monero::PrivateViewKey,
pub(crate) redeem_address: bitcoin::Address, pub(crate) redeem_address: bitcoin::Address,

View File

@ -1,6 +1,7 @@
use crate::{bitcoin, monero}; use crate::{bitcoin, monero, serde::cross_curve_dleq_proof};
use anyhow::Result; use anyhow::Result;
use ecdsa_fun::{adaptor::EncryptedSignature, Signature}; use ecdsa_fun::{adaptor::EncryptedSignature, Signature};
use serde::{Deserialize, Serialize};
use std::convert::TryFrom; use std::convert::TryFrom;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -11,11 +12,12 @@ pub enum Message {
Message3(Message3), Message3(Message3),
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Message0 { pub struct Message0 {
pub(crate) B: bitcoin::PublicKey, pub(crate) B: bitcoin::PublicKey,
pub(crate) S_b_monero: monero::PublicKey, pub(crate) S_b_monero: monero::PublicKey,
pub(crate) S_b_bitcoin: bitcoin::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) dleq_proof_s_b: cross_curve_dleq::Proof,
pub(crate) v_b: monero::PrivateViewKey, pub(crate) v_b: monero::PrivateViewKey,
pub(crate) refund_address: bitcoin::Address, pub(crate) refund_address: bitcoin::Address,

View File

@ -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<E>(self, _s: &[u8]) -> Result<Self::Value, E>
where
E: de::Error,
{
todo!("visit_bytes")
}
}
pub fn serialize<S>(_x: &cross_curve_dleq::Proof, _s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
todo!("serialize")
}
pub fn deserialize<'de, D>(
_deserializer: D,
) -> Result<cross_curve_dleq::Proof, <D as Deserializer<'de>>::Error>
where
D: Deserializer<'de>,
{
todo!("deserialize")
}
}
pub mod monero_private_key { pub mod monero_private_key {
use serde::{de, de::Visitor, Deserializer, Serializer}; use serde::{de, de::Visitor, Deserializer, Serializer};
use std::fmt; use std::fmt;