diff --git a/swap/src/bob/swap.rs b/swap/src/bob/swap.rs index a7a26021..4d398458 100644 --- a/swap/src/bob/swap.rs +++ b/swap/src/bob/swap.rs @@ -1,15 +1,15 @@ use crate::{ bob::{event_loop::EventLoopHandle, negotiate::negotiate}, state, - state::Bob, + state::{Bob, Swap}, storage::Database, SwapAmounts, }; -use anyhow::Result; +use anyhow::{bail, Result}; use async_recursion::async_recursion; use libp2p::{core::Multiaddr, PeerId}; use rand::{CryptoRng, RngCore}; -use std::{fmt, sync::Arc}; +use std::{convert::TryFrom, fmt, sync::Arc}; use tracing::info; use uuid::Uuid; use xmr_btc::{ @@ -77,16 +77,24 @@ impl From for state::Bob { } } -impl From for BobState { - fn from(bob: Bob) -> Self { - match bob { - Bob::Negotiated { state2, peer_id } => BobState::Negotiated(state2, peer_id), - Bob::BtcLocked { state3, peer_id } => BobState::BtcLocked(state3, peer_id), - Bob::XmrLocked { state4, peer_id } => BobState::XmrLocked(state4, peer_id), - Bob::EncSigSent { state4, peer_id } => BobState::EncSigSent(state4, peer_id), - Bob::BtcRedeemed(state5) => BobState::BtcRedeemed(state5), - Bob::BtcCancelled(state4) => BobState::Cancelled(state4), - Bob::SwapComplete => BobState::SafelyAborted, +impl TryFrom for BobState { + type Error = anyhow::Error; + + fn try_from(db_state: state::Swap) -> Result { + if let Swap::Bob(state) = db_state { + let bob_State = match state { + Bob::Negotiated { state2, peer_id } => BobState::Negotiated(state2, peer_id), + Bob::BtcLocked { state3, peer_id } => BobState::BtcLocked(state3, peer_id), + Bob::XmrLocked { state4, peer_id } => BobState::XmrLocked(state4, peer_id), + Bob::EncSigSent { state4, peer_id } => BobState::EncSigSent(state4, peer_id), + Bob::BtcRedeemed(state5) => BobState::BtcRedeemed(state5), + Bob::BtcCancelled(state4) => BobState::Cancelled(state4), + Bob::SwapComplete => BobState::SafelyAborted, + }; + + Ok(bob_State) + } else { + bail!("Bob swap state expected.") } } } @@ -116,6 +124,32 @@ where .await } +pub async fn recover( + event_loop_handle: EventLoopHandle, + db: Database, + bitcoin_wallet: Arc, + monero_wallet: Arc, + rng: R, + swap_id: Uuid, +) -> Result +where + R: RngCore + CryptoRng + Send, +{ + let db_swap = db.get_state(swap_id)?; + let start_state = BobState::try_from(db_swap)?; + let state = swap( + start_state, + event_loop_handle, + db, + bitcoin_wallet, + monero_wallet, + rng, + swap_id, + ) + .await?; + Ok(state) +} + pub fn is_complete(state: &BobState) -> bool { matches!( state,