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>;
#[allow(unused_assignments)] // Due to the mutable message0?
pub async fn swap<R: RngCore + CryptoRng>(
listen: Multiaddr,
rng: &mut R,
redeem_address: ::bitcoin::Address,
punish_address: ::bitcoin::Address,
) -> Result<()> {
let message0: Option<bob::Message0> = None;
let mut message0: Option<bob::Message0> = None;
let mut last_amounts: Option<SwapParams> = None;
let mut swarm = new_swarm(listen)?;
@ -48,10 +49,10 @@ pub async fn swap<R: RngCore + CryptoRng>(
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<R: RngCore + CryptoRng>(
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<Swarm> {
pub enum OutEvent {
ConnectionEstablished(PeerId),
Request(amounts::OutEvent),
// Message0(bob::Message0),
Message0,
Message0(bob::Message0),
}
impl From<peer_tracker::OutEvent> for OutEvent {
@ -139,8 +139,7 @@ impl From<amounts::OutEvent> for OutEvent {
impl From<message0::OutEvent> 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),
}
}
}

View File

@ -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<RequestResponseEvent<BobToAlice, AliceToBob>>
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"),
},

View File

@ -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<W>(
mut cmd_tx: Sender<Cmd>,
mut rsp_rx: Receiver<Rsp>,
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<Swarm> {
pub enum OutEvent {
ConnectionEstablished(PeerId),
Amounts(amounts::OutEvent),
// Message0(alice::Message0),
Message0,
Message0(alice::Message0),
}
impl From<peer_tracker::OutEvent> for OutEvent {
@ -143,8 +141,7 @@ impl From<amounts::OutEvent> for OutEvent {
impl From<message0::OutEvent> 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),
}
}
}

View File

@ -85,7 +85,7 @@ impl NetworkBehaviourEventProcess<RequestResponseEvent<BobToAlice, AliceToBob>>
},
} => 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 { .. } => {

View File

@ -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<RequestResponseEvent<BobToAlice, AliceToBob>>
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"),
},

View File

@ -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)]

View File

@ -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,

View File

@ -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,

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 {
use serde::{de, de::Visitor, Deserializer, Serializer};
use std::fmt;