|
|
|
@ -1,7 +1,12 @@
|
|
|
|
|
use crate::SwapAmounts;
|
|
|
|
|
use crate::{alice::swap::AliceState, bob::swap::BobState, SwapAmounts};
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
use std::fmt::Display;
|
|
|
|
|
use xmr_btc::{alice, bitcoin::EncryptedSignature, bob, monero, serde::monero_private_key};
|
|
|
|
|
use xmr_btc::{
|
|
|
|
|
alice,
|
|
|
|
|
bitcoin::{EncryptedSignature, TxCancel, TxRefund},
|
|
|
|
|
bob, monero,
|
|
|
|
|
serde::monero_private_key,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
|
|
|
|
|
pub enum Swap {
|
|
|
|
@ -44,6 +49,117 @@ pub enum AliceEndState {
|
|
|
|
|
BtcPunished,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<&AliceState> for Alice {
|
|
|
|
|
fn from(alice_state: &AliceState) -> Self {
|
|
|
|
|
match alice_state {
|
|
|
|
|
AliceState::Negotiated { state3, .. } => Alice::Negotiated(state3.as_ref().clone()),
|
|
|
|
|
AliceState::BtcLocked { state3, .. } => Alice::BtcLocked(state3.as_ref().clone()),
|
|
|
|
|
AliceState::XmrLocked { state3 } => Alice::XmrLocked(state3.as_ref().clone()),
|
|
|
|
|
AliceState::EncSigLearned {
|
|
|
|
|
state3,
|
|
|
|
|
encrypted_signature,
|
|
|
|
|
} => Alice::EncSigLearned {
|
|
|
|
|
state3: state3.as_ref().clone(),
|
|
|
|
|
encrypted_signature: encrypted_signature.clone(),
|
|
|
|
|
},
|
|
|
|
|
AliceState::BtcRedeemed => Alice::Done(AliceEndState::BtcRedeemed),
|
|
|
|
|
AliceState::BtcCancelled { state3, .. } => Alice::BtcCancelled(state3.as_ref().clone()),
|
|
|
|
|
AliceState::BtcRefunded { spend_key, state3 } => Alice::BtcRefunded {
|
|
|
|
|
spend_key: *spend_key,
|
|
|
|
|
state3: state3.as_ref().clone(),
|
|
|
|
|
},
|
|
|
|
|
AliceState::BtcPunishable { state3, .. } => {
|
|
|
|
|
Alice::BtcPunishable(state3.as_ref().clone())
|
|
|
|
|
}
|
|
|
|
|
AliceState::XmrRefunded => Alice::Done(AliceEndState::XmrRefunded),
|
|
|
|
|
AliceState::CancelTimelockExpired { state3 } => {
|
|
|
|
|
Alice::CancelTimelockExpired(state3.as_ref().clone())
|
|
|
|
|
}
|
|
|
|
|
AliceState::BtcPunished => Alice::Done(AliceEndState::BtcPunished),
|
|
|
|
|
AliceState::SafelyAborted => Alice::Done(AliceEndState::SafelyAborted),
|
|
|
|
|
AliceState::Started { amounts, state0 } => Alice::Started {
|
|
|
|
|
amounts: *amounts,
|
|
|
|
|
state0: state0.clone(),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<Alice> for AliceState {
|
|
|
|
|
fn from(db_state: Alice) -> Self {
|
|
|
|
|
match db_state {
|
|
|
|
|
Alice::Started { amounts, state0 } => AliceState::Started { amounts, state0 },
|
|
|
|
|
Alice::Negotiated(state3) => AliceState::Negotiated {
|
|
|
|
|
channel: None,
|
|
|
|
|
amounts: SwapAmounts {
|
|
|
|
|
btc: state3.btc,
|
|
|
|
|
xmr: state3.xmr,
|
|
|
|
|
},
|
|
|
|
|
state3: Box::new(state3),
|
|
|
|
|
},
|
|
|
|
|
Alice::BtcLocked(state3) => AliceState::BtcLocked {
|
|
|
|
|
channel: None,
|
|
|
|
|
amounts: SwapAmounts {
|
|
|
|
|
btc: state3.btc,
|
|
|
|
|
xmr: state3.xmr,
|
|
|
|
|
},
|
|
|
|
|
state3: Box::new(state3),
|
|
|
|
|
},
|
|
|
|
|
Alice::XmrLocked(state3) => AliceState::XmrLocked {
|
|
|
|
|
state3: Box::new(state3),
|
|
|
|
|
},
|
|
|
|
|
Alice::EncSigLearned {
|
|
|
|
|
state3: state,
|
|
|
|
|
encrypted_signature,
|
|
|
|
|
} => AliceState::EncSigLearned {
|
|
|
|
|
state3: Box::new(state),
|
|
|
|
|
encrypted_signature,
|
|
|
|
|
},
|
|
|
|
|
Alice::CancelTimelockExpired(state3) => AliceState::CancelTimelockExpired {
|
|
|
|
|
state3: Box::new(state3),
|
|
|
|
|
},
|
|
|
|
|
Alice::BtcCancelled(state) => {
|
|
|
|
|
let tx_cancel = TxCancel::new(
|
|
|
|
|
&state.tx_lock,
|
|
|
|
|
state.cancel_timelock,
|
|
|
|
|
state.a.public(),
|
|
|
|
|
state.B,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
AliceState::BtcCancelled {
|
|
|
|
|
state3: Box::new(state),
|
|
|
|
|
tx_cancel,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Alice::BtcPunishable(state3) => {
|
|
|
|
|
let tx_cancel = TxCancel::new(
|
|
|
|
|
&state3.tx_lock,
|
|
|
|
|
state3.cancel_timelock,
|
|
|
|
|
state3.a.public(),
|
|
|
|
|
state3.B,
|
|
|
|
|
);
|
|
|
|
|
let tx_refund = TxRefund::new(&tx_cancel, &state3.refund_address);
|
|
|
|
|
AliceState::BtcPunishable {
|
|
|
|
|
tx_refund,
|
|
|
|
|
state3: Box::new(state3),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Alice::BtcRefunded {
|
|
|
|
|
state3, spend_key, ..
|
|
|
|
|
} => AliceState::BtcRefunded {
|
|
|
|
|
spend_key,
|
|
|
|
|
state3: Box::new(state3),
|
|
|
|
|
},
|
|
|
|
|
Alice::Done(end_state) => match end_state {
|
|
|
|
|
AliceEndState::SafelyAborted => AliceState::SafelyAborted,
|
|
|
|
|
AliceEndState::BtcRedeemed => AliceState::BtcRedeemed,
|
|
|
|
|
AliceEndState::XmrRefunded => AliceState::XmrRefunded,
|
|
|
|
|
AliceEndState::BtcPunished => AliceState::BtcPunished,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
|
|
|
|
|
pub enum Bob {
|
|
|
|
|
Started {
|
|
|
|
@ -76,6 +192,46 @@ pub enum BobEndState {
|
|
|
|
|
BtcPunished,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<BobState> for Bob {
|
|
|
|
|
fn from(bob_state: BobState) -> Self {
|
|
|
|
|
match bob_state {
|
|
|
|
|
BobState::Started { state0, amounts } => Bob::Started { state0, amounts },
|
|
|
|
|
BobState::Negotiated(state2) => Bob::Negotiated { state2 },
|
|
|
|
|
BobState::BtcLocked(state3) => Bob::BtcLocked { state3 },
|
|
|
|
|
BobState::XmrLocked(state4) => Bob::XmrLocked { state4 },
|
|
|
|
|
BobState::EncSigSent(state4) => Bob::EncSigSent { state4 },
|
|
|
|
|
BobState::BtcRedeemed(state5) => Bob::BtcRedeemed(state5),
|
|
|
|
|
BobState::CancelTimelockExpired(state4) => Bob::CancelTimelockExpired(state4),
|
|
|
|
|
BobState::BtcCancelled(state4) => Bob::BtcCancelled(state4),
|
|
|
|
|
BobState::BtcRefunded(state4) => Bob::Done(BobEndState::BtcRefunded(Box::new(state4))),
|
|
|
|
|
BobState::XmrRedeemed => Bob::Done(BobEndState::XmrRedeemed),
|
|
|
|
|
BobState::BtcPunished => Bob::Done(BobEndState::BtcPunished),
|
|
|
|
|
BobState::SafelyAborted => Bob::Done(BobEndState::SafelyAborted),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<Bob> for BobState {
|
|
|
|
|
fn from(db_state: Bob) -> Self {
|
|
|
|
|
match db_state {
|
|
|
|
|
Bob::Started { state0, amounts } => BobState::Started { state0, amounts },
|
|
|
|
|
Bob::Negotiated { state2 } => BobState::Negotiated(state2),
|
|
|
|
|
Bob::BtcLocked { state3 } => BobState::BtcLocked(state3),
|
|
|
|
|
Bob::XmrLocked { state4 } => BobState::XmrLocked(state4),
|
|
|
|
|
Bob::EncSigSent { state4 } => BobState::EncSigSent(state4),
|
|
|
|
|
Bob::BtcRedeemed(state5) => BobState::BtcRedeemed(state5),
|
|
|
|
|
Bob::CancelTimelockExpired(state4) => BobState::CancelTimelockExpired(state4),
|
|
|
|
|
Bob::BtcCancelled(state4) => BobState::BtcCancelled(state4),
|
|
|
|
|
Bob::Done(end_state) => match end_state {
|
|
|
|
|
BobEndState::SafelyAborted => BobState::SafelyAborted,
|
|
|
|
|
BobEndState::XmrRedeemed => BobState::XmrRedeemed,
|
|
|
|
|
BobEndState::BtcRefunded(state4) => BobState::BtcRefunded(*state4),
|
|
|
|
|
BobEndState::BtcPunished => BobState::BtcPunished,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<Alice> for Swap {
|
|
|
|
|
fn from(from: Alice) -> Self {
|
|
|
|
|
Swap::Alice(from)
|
|
|
|
|