Add swap/

Add a binary crate `swap` that implements two nodes (Alice and Bob). With this
applied we can start up a node for each role and do:

- Bob: Requests current amounts using BTC is input
- Alice: Responds with amounts
- Bob: (mock) get user input to Ok the amounts

... continue with swap (TODO)
This commit is contained in:
Tobin C. Harding 2020-10-16 09:14:39 +11:00
parent 4723626fc0
commit 05766d3146
14 changed files with 1141 additions and 1 deletions

95
swap/src/lib.rs Normal file
View file

@ -0,0 +1,95 @@
use serde::{Deserialize, Serialize};
pub mod alice;
pub mod bob;
pub mod network;
pub const ONE_BTC: u64 = 100_000_000;
pub type Never = std::convert::Infallible;
/// Commands sent from Bob to the main task.
#[derive(Debug)]
pub enum Cmd {
VerifyAmounts(SwapParams),
}
/// Responses send from the main task back to Bob.
#[derive(Debug, PartialEq)]
pub enum Rsp {
Verified,
Abort,
}
/// XMR/BTC swap parameters.
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
pub struct SwapParams {
/// Amount of BTC to swap.
pub btc: bitcoin::Amount,
/// Amount of XMR to swap.
pub xmr: monero::Amount,
}
// FIXME: Amount modules are a quick hack so we can derive serde.
pub mod monero {
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
pub struct Amount(u64);
impl Amount {
/// Create an [Amount] with piconero precision and the given number of
/// piconeros.
///
/// A piconero (a.k.a atomic unit) is equal to 1e-12 XMR.
pub fn from_piconero(amount: u64) -> Self {
Amount(amount)
}
pub fn as_piconero(&self) -> u64 {
self.0
}
}
impl From<Amount> for u64 {
fn from(from: Amount) -> u64 {
from.0
}
}
impl fmt::Display for Amount {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} piconeros", self.0)
}
}
}
pub mod bitcoin {
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
pub struct Amount(u64);
impl Amount {
/// The zero amount.
pub const ZERO: Amount = Amount(0);
/// Exactly one satoshi.
pub const ONE_SAT: Amount = Amount(1);
/// Exactly one bitcoin.
pub const ONE_BTC: Amount = Amount(100_000_000);
/// Create an [Amount] with satoshi precision and the given number of
/// satoshis.
pub fn from_sat(satoshi: u64) -> Amount {
Amount(satoshi)
}
}
impl fmt::Display for Amount {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} satoshis", self.0)
}
}
}