xmr-btc-swap/swap/src/bob.rs

186 lines
4.9 KiB
Rust
Raw Normal View History

//! Run an XMR/BTC swap in the role of Bob.
//! Bob holds BTC and wishes receive XMR.
use self::{amounts::*, message0::*, message1::*, message2::*, message3::*};
use crate::{
network::{
peer_tracker::{self, PeerTracker},
transport::SwapTransport,
TokioExecutor,
},
2020-12-10 03:20:27 +00:00
SwapAmounts,
};
2020-12-10 03:20:27 +00:00
use anyhow::Result;
use libp2p::{
core::{identity::Keypair, Multiaddr},
NetworkBehaviour, PeerId,
};
2020-12-10 03:20:27 +00:00
use tracing::{debug, info};
2020-10-21 03:41:50 +00:00
use xmr_btc::{
2020-10-22 00:04:49 +00:00
alice,
2020-12-10 03:20:27 +00:00
bitcoin::EncryptedSignature,
bob::{self},
};
2020-12-10 03:20:27 +00:00
mod amounts;
pub mod event_loop;
mod message0;
mod message1;
mod message2;
mod message3;
pub mod swap;
2020-11-30 02:25:11 +00:00
pub type Swarm = libp2p::Swarm<Behaviour>;
2020-11-30 02:25:11 +00:00
pub fn new_swarm(transport: SwapTransport, behaviour: Behaviour) -> Result<Swarm> {
let local_peer_id = behaviour.peer_id();
let swarm = libp2p::swarm::SwarmBuilder::new(transport, behaviour, local_peer_id.clone())
.executor(Box::new(TokioExecutor {
handle: tokio::runtime::Handle::current(),
}))
.build();
info!("Initialized swarm with identity {}", local_peer_id);
Ok(swarm)
}
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone)]
pub enum OutEvent {
ConnectionEstablished(PeerId),
Amounts(SwapAmounts),
2020-10-22 00:04:49 +00:00
Message0(alice::Message0),
2020-10-22 02:23:12 +00:00
Message1(alice::Message1),
Message2(alice::Message2),
Message3,
}
impl From<peer_tracker::OutEvent> for OutEvent {
fn from(event: peer_tracker::OutEvent) -> Self {
match event {
peer_tracker::OutEvent::ConnectionEstablished(id) => {
OutEvent::ConnectionEstablished(id)
}
}
}
}
2020-10-21 03:41:50 +00:00
impl From<amounts::OutEvent> for OutEvent {
fn from(event: amounts::OutEvent) -> Self {
match event {
amounts::OutEvent::Amounts(amounts) => OutEvent::Amounts(amounts),
}
2020-10-21 03:41:50 +00:00
}
}
impl From<message0::OutEvent> for OutEvent {
fn from(event: message0::OutEvent) -> Self {
match event {
2020-10-22 00:04:49 +00:00
message0::OutEvent::Msg(msg) => OutEvent::Message0(msg),
2020-10-21 03:41:50 +00:00
}
}
}
2020-10-22 02:23:12 +00:00
impl From<message1::OutEvent> for OutEvent {
fn from(event: message1::OutEvent) -> Self {
match event {
message1::OutEvent::Msg(msg) => OutEvent::Message1(msg),
}
}
}
impl From<message2::OutEvent> for OutEvent {
fn from(event: message2::OutEvent) -> Self {
match event {
message2::OutEvent::Msg(msg) => OutEvent::Message2(msg),
}
}
}
impl From<message3::OutEvent> for OutEvent {
fn from(event: message3::OutEvent) -> Self {
match event {
message3::OutEvent::Msg => OutEvent::Message3,
}
}
}
/// A `NetworkBehaviour` that represents an XMR/BTC swap node as Bob.
#[derive(NetworkBehaviour)]
#[behaviour(out_event = "OutEvent", event_process = false)]
#[allow(missing_debug_implementations)]
2020-11-30 02:25:11 +00:00
pub struct Behaviour {
pt: PeerTracker,
2020-10-21 03:41:50 +00:00
amounts: Amounts,
message0: Message0,
2020-10-22 02:23:12 +00:00
message1: Message1,
message2: Message2,
message3: Message3,
#[behaviour(ignore)]
identity: Keypair,
}
2020-11-30 02:25:11 +00:00
impl Behaviour {
pub fn identity(&self) -> Keypair {
self.identity.clone()
}
pub fn peer_id(&self) -> PeerId {
PeerId::from(self.identity.public())
}
/// Sends a message to Alice to get current amounts based on `btc`.
2020-10-21 03:41:50 +00:00
pub fn request_amounts(&mut self, alice: PeerId, btc: u64) {
let btc = ::bitcoin::Amount::from_sat(btc);
let _id = self.amounts.request_amounts(alice.clone(), btc);
2020-10-30 04:51:46 +00:00
info!("Requesting amounts from: {}", alice);
}
2020-10-22 02:23:12 +00:00
/// Sends Bob's first message to Alice.
2020-10-21 03:41:50 +00:00
pub fn send_message0(&mut self, alice: PeerId, msg: bob::Message0) {
2020-10-28 00:46:45 +00:00
self.message0.send(alice, msg);
2020-10-30 04:51:46 +00:00
debug!("Sent Message0");
2020-10-21 03:41:50 +00:00
}
2020-10-22 02:23:12 +00:00
/// Sends Bob's second message to Alice.
pub fn send_message1(&mut self, alice: PeerId, msg: bob::Message1) {
2020-10-30 04:51:46 +00:00
self.message1.send(alice, msg);
debug!("Sent Message1");
2020-10-22 02:23:12 +00:00
}
/// Sends Bob's third message to Alice.
pub fn send_message2(&mut self, alice: PeerId, msg: bob::Message2) {
2020-10-30 04:51:46 +00:00
self.message2.send(alice, msg);
debug!("Sent Message2");
}
/// Sends Bob's fourth message to Alice.
pub fn send_message3(&mut self, alice: PeerId, tx_redeem_encsig: EncryptedSignature) {
let msg = bob::Message3 { tx_redeem_encsig };
2020-10-30 04:51:46 +00:00
self.message3.send(alice, msg);
debug!("Sent Message3");
}
/// Add a known address for the given peer
pub fn add_address(&mut self, peer_id: PeerId, address: Multiaddr) {
self.pt.add_address(peer_id, address)
}
}
2020-11-30 02:25:11 +00:00
impl Default for Behaviour {
fn default() -> Behaviour {
let identity = Keypair::generate_ed25519();
Self {
pt: PeerTracker::default(),
amounts: Amounts::default(),
message0: Message0::default(),
message1: Message1::default(),
message2: Message2::default(),
message3: Message3::default(),
identity,
}
}
}