@ -24,10 +24,7 @@ pub use self::{
state ::* ,
swap ::{ run , run_until } ,
} ;
use crate ::{
config ::Config , database ::Database , network ::transport ::build , protocol ::StartingBalances ,
seed ::Seed ,
} ;
use crate ::{ config ::Config , database ::Database , network ::transport ::build , seed ::Seed } ;
use libp2p ::{ core ::Multiaddr , identity ::Keypair } ;
use rand ::rngs ::OsRng ;
use std ::{ path ::PathBuf , sync ::Arc } ;
@ -53,7 +50,7 @@ pub struct Swap {
pub db : Database ,
}
pub struct SwapFactory {
pub struct Builder {
swap_id : Uuid ,
identity : Keypair ,
peer_id : PeerId ,
@ -64,9 +61,16 @@ pub struct SwapFactory {
bitcoin_wallet : Arc < bitcoin ::Wallet > ,
monero_wallet : Arc < monero ::Wallet > ,
init_params : InitParams ,
}
impl SwapFactory {
enum InitParams {
None ,
New { swap_amounts : SwapAmounts } ,
}
impl Builder {
pub async fn new (
seed : Seed ,
config : Config ,
@ -89,71 +93,83 @@ impl SwapFactory {
listen_address ,
bitcoin_wallet ,
monero_wallet ,
init_params : InitParams ::None ,
}
}
pub async fn new_swap ( self , swap_amounts : SwapAmounts ) -> Result < ( Swap , EventLoop ) > {
let initial_state = init_alice_state (
swap_amounts . btc ,
swap_amounts . xmr ,
self . bitcoin_wallet . clone ( ) ,
self . config ,
)
. await ? ;
let ( event_loop , event_loop_handle ) = init_alice_event_loop (
self . listen_address . clone ( ) ,
self . identity . clone ( ) ,
self . peer_id . clone ( ) ,
) ? ;
let db = Database ::open ( self . db_path . as_path ( ) ) ? ;
Ok ( (
Swap {
event_loop_handle ,
bitcoin_wallet : self . bitcoin_wallet . clone ( ) ,
monero_wallet : self . monero_wallet . clone ( ) ,
config : self . config ,
db ,
state : initial_state ,
swap_id : self . swap_id ,
} ,
event_loop ,
) )
pub fn with_init_params ( self , swap_amounts : SwapAmounts ) -> Self {
Self {
init_params : InitParams ::New { swap_amounts } ,
.. self
}
}
pub async fn resume ( self ) -> Result < ( Swap , EventLoop ) > {
// reopen the existing database
let db = Database ::open ( self . db_path . as_path ( ) ) ? ;
let resume_state = if let database ::Swap ::Alice ( state ) = db . get_state ( self . swap_id ) ? {
state . into ( )
} else {
bail ! (
"Trying to load swap with id {} for the wrong direction." ,
self . swap_id
)
} ;
let ( event_loop , event_loop_handle ) = init_alice_event_loop (
self . listen_address . clone ( ) ,
self . identity . clone ( ) ,
self . peer_id . clone ( ) ,
) ? ;
Ok ( (
Swap {
state : resume_state ,
event_loop_handle ,
bitcoin_wallet : self . bitcoin_wallet . clone ( ) ,
monero_wallet : self . monero_wallet . clone ( ) ,
config : self . config ,
swap_id : self . swap_id ,
db ,
} ,
event_loop ,
) )
pub async fn build ( self ) -> Result < ( Swap , EventLoop ) > {
match self . init_params {
InitParams ::New { swap_amounts } = > {
let initial_state = init_alice_state (
swap_amounts . btc ,
swap_amounts . xmr ,
self . bitcoin_wallet . clone ( ) ,
self . config ,
)
. await ? ;
let ( event_loop , event_loop_handle ) = init_alice_event_loop (
self . listen_address . clone ( ) ,
self . identity . clone ( ) ,
self . peer_id . clone ( ) ,
) ? ;
let db = Database ::open ( self . db_path . as_path ( ) ) ? ;
Ok ( (
Swap {
event_loop_handle ,
bitcoin_wallet : self . bitcoin_wallet . clone ( ) ,
monero_wallet : self . monero_wallet . clone ( ) ,
config : self . config ,
db ,
state : initial_state ,
swap_id : self . swap_id ,
} ,
event_loop ,
) )
}
InitParams ::None = > {
// reopen the existing database
let db = Database ::open ( self . db_path . as_path ( ) ) ? ;
let resume_state =
if let database ::Swap ::Alice ( state ) = db . get_state ( self . swap_id ) ? {
state . into ( )
} else {
bail ! (
"Trying to load swap with id {} for the wrong direction." ,
self . swap_id
)
} ;
let ( event_loop , event_loop_handle ) = init_alice_event_loop (
self . listen_address . clone ( ) ,
self . identity . clone ( ) ,
self . peer_id . clone ( ) ,
) ? ;
Ok ( (
Swap {
state : resume_state ,
event_loop_handle ,
bitcoin_wallet : self . bitcoin_wallet . clone ( ) ,
monero_wallet : self . monero_wallet . clone ( ) ,
config : self . config ,
swap_id : self . swap_id ,
db ,
} ,
event_loop ,
) )
}
}
}
pub fn peer_id ( & self ) -> PeerId {