Verify amounts with user

This commit is contained in:
Tobin C. Harding 2020-10-16 10:43:32 +11:00
parent 05766d3146
commit 3492c46e71
4 changed files with 55 additions and 28 deletions

View File

@ -6,8 +6,8 @@ use libp2p::{
request_response::ResponseChannel, request_response::ResponseChannel,
NetworkBehaviour, PeerId, NetworkBehaviour, PeerId,
}; };
use std::{thread, time::Duration}; use std::time::Duration;
use tracing::{debug, warn}; use tracing::debug;
mod messenger; mod messenger;
@ -27,9 +27,10 @@ pub type Swarm = libp2p::Swarm<Alice>;
pub async fn swap(listen: Multiaddr) -> Result<()> { pub async fn swap(listen: Multiaddr) -> Result<()> {
let mut swarm = new_swarm(listen)?; let mut swarm = new_swarm(listen)?;
loop {
match swarm.next().await { match swarm.next().await {
BehaviourOutEvent::Request(messenger::BehaviourOutEvent::Btc { btc, channel }) => { BehaviourOutEvent::Request(messenger::BehaviourOutEvent::Btc { btc, channel }) => {
debug!("Got request from Bob"); debug!("Got request from Bob to swap {}", btc);
let params = SwapParams { let params = SwapParams {
btc, btc,
// TODO: Do a real calculation. // TODO: Do a real calculation.
@ -41,10 +42,7 @@ pub async fn swap(listen: Multiaddr) -> Result<()> {
} }
other => panic!("unexpected event: {:?}", other), other => panic!("unexpected event: {:?}", other),
} }
}
warn!("parking thread ...");
thread::park();
Ok(())
} }
fn new_swarm(listen: Multiaddr) -> Result<Swarm> { fn new_swarm(listen: Multiaddr) -> Result<Swarm> {

View File

@ -12,7 +12,7 @@ use std::{
task::{Context, Poll}, task::{Context, Poll},
time::Duration, time::Duration,
}; };
use tracing::{debug, error}; use tracing::error;
use crate::{ use crate::{
bitcoin, bitcoin,
@ -55,10 +55,8 @@ impl Messenger {
alice: PeerId, alice: PeerId,
btc: bitcoin::Amount, btc: bitcoin::Amount,
) -> Result<RequestId> { ) -> Result<RequestId> {
debug!("Sending request ...");
let msg = BobToAlice::AmountsFromBtc(btc); let msg = BobToAlice::AmountsFromBtc(btc);
let id = self.rr.send_request(&alice, msg); let id = self.rr.send_request(&alice, msg);
debug!("Sent.");
Ok(id) Ok(id)
} }

View File

@ -1,4 +1,5 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::fmt::{self, Display};
pub mod alice; pub mod alice;
pub mod bob; pub mod bob;
@ -9,13 +10,13 @@ pub const ONE_BTC: u64 = 100_000_000;
pub type Never = std::convert::Infallible; pub type Never = std::convert::Infallible;
/// Commands sent from Bob to the main task. /// Commands sent from Bob to the main task.
#[derive(Debug)] #[derive(Clone, Copy, Debug)]
pub enum Cmd { pub enum Cmd {
VerifyAmounts(SwapParams), VerifyAmounts(SwapParams),
} }
/// Responses send from the main task back to Bob. /// Responses send from the main task back to Bob.
#[derive(Debug, PartialEq)] #[derive(Clone, Copy, Debug, PartialEq)]
pub enum Rsp { pub enum Rsp {
Verified, Verified,
Abort, Abort,
@ -30,6 +31,12 @@ pub struct SwapParams {
pub xmr: monero::Amount, pub xmr: monero::Amount,
} }
impl Display for SwapParams {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} for {}", self.btc, self.xmr)
}
}
// FIXME: Amount modules are a quick hack so we can derive serde. // FIXME: Amount modules are a quick hack so we can derive serde.
pub mod monero { pub mod monero {

View File

@ -16,9 +16,9 @@ use anyhow::{bail, Result};
use futures::{channel::mpsc, StreamExt}; use futures::{channel::mpsc, StreamExt};
use libp2p::Multiaddr; use libp2p::Multiaddr;
use log::LevelFilter; use log::LevelFilter;
use std::{io, io::Write, process};
use structopt::StructOpt; use structopt::StructOpt;
use tracing::info; use tracing::info;
mod cli; mod cli;
mod trace; mod trace;
@ -77,8 +77,10 @@ async fn swap_as_bob(sats: u64, addr: Multiaddr) -> Result<()> {
match read { match read {
Some(cmd) => match cmd { Some(cmd) => match cmd {
Cmd::VerifyAmounts(p) => { Cmd::VerifyAmounts(p) => {
if verified(p) { let rsp = verify(p);
rsp_tx.try_send(Rsp::Verified)?; rsp_tx.try_send(rsp)?;
if rsp == Rsp::Abort {
process::exit(0);
} }
} }
}, },
@ -90,7 +92,29 @@ async fn swap_as_bob(sats: u64, addr: Multiaddr) -> Result<()> {
} }
} }
fn verified(_p: SwapParams) -> bool { fn verify(p: SwapParams) -> Rsp {
// TODO: Read input from the shell. let mut s = String::new();
true println!("Got rate from Alice for XMR/BTC swap\n");
println!("{}", p);
print!("Would you like to continue with this swap [y/N]: ");
let _ = io::stdout().flush();
io::stdin()
.read_line(&mut s)
.expect("Did not enter a correct string");
if let Some('\n') = s.chars().next_back() {
s.pop();
}
if let Some('\r') = s.chars().next_back() {
s.pop();
}
if !is_yes(&s) {
println!("No worries, try again later - Alice updates her rate regularly");
return Rsp::Abort;
}
Rsp::Verified
}
fn is_yes(s: &str) -> bool {
matches!(s, "y" | "Y" | "yes" | "YES" | "Yes")
} }