1
1
#![ cfg_attr( not( test) , warn( clippy:: unwrap_used) ) ]
2
2
3
3
use crate :: bitcoin:: util:: psbt:: PartiallySignedTransaction ;
4
+ use crate :: bitcoin:: Network ;
4
5
use crate :: bitcoin:: Txid ;
5
6
use crate :: listen_protocols:: TAKER_LISTEN_PROTOCOLS ;
7
+ use anyhow:: anyhow;
6
8
use anyhow:: bail;
7
9
use anyhow:: Context as _;
8
10
use anyhow:: Result ;
@@ -30,7 +32,9 @@ use parse_display::Display;
30
32
use ping_pong:: ping;
31
33
use ping_pong:: pong;
32
34
use seed:: Identities ;
35
+ use serde:: Serialize ;
33
36
use std:: collections:: HashSet ;
37
+ use std:: path:: PathBuf ;
34
38
use std:: sync:: Arc ;
35
39
use std:: time:: Duration ;
36
40
use time:: ext:: NumericalDuration ;
@@ -78,7 +82,7 @@ pub const N_PAYOUTS: usize = 200;
78
82
79
83
pub struct TakerActorSystem < O , W , P > {
80
84
pub cfd_actor : Address < taker_cfd:: Actor > ,
81
- wallet_actor : Address < W > ,
85
+ pub wallet_actor : Address < W > ,
82
86
_oracle_actor : Address < O > ,
83
87
pub auto_rollover_actor : Address < auto_rollover:: Actor > ,
84
88
pub price_feed_actor : Address < P > ,
@@ -93,6 +97,8 @@ pub struct TakerActorSystem<O, W, P> {
93
97
pub identify_info_feed_receiver : watch:: Receiver < Option < PeerInfo > > ,
94
98
95
99
_tasks : Tasks ,
100
+
101
+ db : sqlite_db:: Connection ,
96
102
}
97
103
98
104
impl < O , W , P > TakerActorSystem < O , W , P >
@@ -105,6 +111,7 @@ where
105
111
W : Handler < wallet:: BuildPartyParams , Return = Result < maia_core:: PartyParams > >
106
112
+ Handler < wallet:: Sign , Return = Result < PartiallySignedTransaction > >
107
113
+ Handler < wallet:: Withdraw , Return = Result < Txid > >
114
+ + Handler < wallet:: ImportSeed , Return = Result < ( ) > >
108
115
+ Handler < wallet:: Sync , Return = ( ) >
109
116
+ Actor < Stop = ( ) > ,
110
117
P : Handler <
@@ -333,7 +340,7 @@ where
333
340
let close_cfds_actor = archive_closed_cfds:: Actor :: new ( db. clone ( ) )
334
341
. create ( None )
335
342
. spawn ( & mut tasks) ;
336
- let archive_failed_cfds_actor = archive_failed_cfds:: Actor :: new ( db)
343
+ let archive_failed_cfds_actor = archive_failed_cfds:: Actor :: new ( db. clone ( ) )
337
344
. create ( None )
338
345
. spawn ( & mut tasks) ;
339
346
@@ -354,6 +361,7 @@ where
354
361
_online_status_actor : online_status_actor,
355
362
_pong_actor : pong_address,
356
363
_identify_dialer_actor : identify_dialer_actor,
364
+ db,
357
365
} )
358
366
}
359
367
@@ -445,14 +453,46 @@ where
445
453
self . wallet_actor . send ( wallet:: Sync ) . await ?;
446
454
Ok ( ( ) )
447
455
}
456
+
457
+ #[ instrument( skip( self , seed) , err) ]
458
+ pub async fn import_seed (
459
+ & self ,
460
+ seed : Vec < u8 > ,
461
+ data_dir : PathBuf ,
462
+ network : Network ,
463
+ ) -> Result < ( ) > {
464
+ let open_cfd_ids = self . db . load_open_cfd_ids ( ) . await ?;
465
+
466
+ if !open_cfd_ids. is_empty ( ) {
467
+ tracing:: warn!( "Rejecting imported seed as open CFDs exist." ) ;
468
+ // in case of open cfds we do not accept an imported seed.
469
+ return Err ( anyhow ! (
470
+ "Can not accept imported seed since open CFDs exist."
471
+ ) ) ;
472
+ }
473
+
474
+ // recreate the wallet within the wallet actor
475
+ self . wallet_actor
476
+ . send ( wallet:: ImportSeed {
477
+ seed,
478
+ seed_path : data_dir. join ( seed:: TAKER_WALLET_SEED_FILE ) ,
479
+ network,
480
+ } )
481
+ . await ??;
482
+
483
+ // sync imported wallet
484
+ self . wallet_actor . send ( wallet:: Sync ) . await ?;
485
+
486
+ Ok ( ( ) )
487
+ }
448
488
}
449
489
450
490
/// A struct defining our environment
451
491
///
452
492
/// We can run on all kinds of environment, hence this is just a wrapper around string.
453
493
/// However, for backwards compatibility with <=0.6.x we need to support to support
454
494
/// `Unknown`. For all other we format the string to lowercase.
455
- #[ derive( Debug , Clone , Display , PartialEq , Eq ) ]
495
+ #[ derive( Debug , Clone , Display , PartialEq , Eq , Serialize ) ]
456
496
pub struct Environment ( String ) ;
457
497
458
498
impl Environment {
0 commit comments