@@ -230,7 +230,10 @@ pub trait Rpc {
230
230
async fn wallet_export ( & self , name : & str ) -> Result < WalletExport , ErrorObjectOwned > ;
231
231
232
232
#[ method( name = "walletcreate" ) ]
233
- async fn wallet_create ( & self , name : & str ) -> Result < ( ) , ErrorObjectOwned > ;
233
+ async fn wallet_create ( & self , name : & str ) -> Result < String , ErrorObjectOwned > ;
234
+
235
+ #[ method( name = "walletrestore" ) ]
236
+ async fn wallet_restore ( & self , name : & str , mnemonic : String ) -> Result < ( ) , ErrorObjectOwned > ;
234
237
235
238
#[ method( name = "walletsendrequest" ) ]
236
239
async fn wallet_send_request (
@@ -491,11 +494,18 @@ impl WalletManager {
491
494
Ok ( export)
492
495
}
493
496
494
- pub async fn create_wallet ( & self , client : & reqwest:: Client , name : & str ) -> anyhow:: Result < ( ) > {
497
+ pub async fn create_wallet ( & self , client : & reqwest:: Client , name : & str ) -> anyhow:: Result < String > {
495
498
let mnemonic: GeneratedKey < _ , Tap > =
496
499
Mnemonic :: generate ( ( WordCount :: Words12 , Language :: English ) )
497
500
. map_err ( |_| anyhow ! ( "Mnemonic generation error" ) ) ?;
498
501
502
+ let start_block = self . get_wallet_start_block ( client) . await ?;
503
+ self . setup_new_wallet ( name. to_string ( ) , mnemonic. to_string ( ) , start_block) ?;
504
+ self . load_wallet ( name) . await ?;
505
+ Ok ( mnemonic. to_string ( ) )
506
+ }
507
+
508
+ pub async fn restore_wallet ( & self , client : & reqwest:: Client , name : & str , mnemonic : & str ) -> anyhow:: Result < ( ) > {
499
509
let start_block = self . get_wallet_start_block ( client) . await ?;
500
510
self . setup_new_wallet ( name. to_string ( ) , mnemonic. to_string ( ) , start_block) ?;
501
511
self . load_wallet ( name) . await ?;
@@ -513,7 +523,7 @@ impl WalletManager {
513
523
return Err ( anyhow ! ( format!( "Wallet `{}` already exists" , name) ) ) ;
514
524
}
515
525
516
- let export = self . wallet_from_mnemonic ( name. clone ( ) , mnemonic. to_string ( ) , start_block) ?;
526
+ let export = self . wallet_from_mnemonic ( name. clone ( ) , mnemonic, start_block) ?;
517
527
fs:: create_dir_all ( & wallet_path) ?;
518
528
let wallet_export_path = wallet_path. join ( "wallet.json" ) ;
519
529
let mut file = fs:: File :: create ( wallet_export_path) ?;
@@ -528,7 +538,7 @@ impl WalletManager {
528
538
start_block : BlockId ,
529
539
) -> anyhow:: Result < WalletExport > {
530
540
let ( network, _) = self . fallback_network ( ) ;
531
- let xpriv = Self :: descriptor_from_mnemonic ( network, & mnemonic. to_string ( ) ) ?;
541
+ let xpriv = Self :: descriptor_from_mnemonic ( network, & mnemonic) ?;
532
542
533
543
let ( external, internal) = Self :: default_descriptors ( xpriv) ;
534
544
let tmp = bdk:: Wallet :: create ( external, internal)
@@ -653,7 +663,7 @@ impl WalletManager {
653
663
}
654
664
655
665
fn descriptor_from_mnemonic ( network : Network , m : & str ) -> anyhow:: Result < Xpriv > {
656
- let mnemonic = Mnemonic :: parse ( m) . unwrap ( ) ;
666
+ let mnemonic = Mnemonic :: parse ( m) ? ;
657
667
let xkey: ExtendedKey = mnemonic. clone ( ) . into_extended_key ( ) ?;
658
668
Ok ( xkey. into_xprv ( network) . expect ( "xpriv" ) )
659
669
}
@@ -908,7 +918,7 @@ impl RpcServer for RpcServerImpl {
908
918
} )
909
919
}
910
920
911
- async fn wallet_create ( & self , name : & str ) -> Result < ( ) , ErrorObjectOwned > {
921
+ async fn wallet_create ( & self , name : & str ) -> Result < String , ErrorObjectOwned > {
912
922
self . wallet_manager
913
923
. create_wallet ( & self . client , name)
914
924
. await
@@ -917,6 +927,15 @@ impl RpcServer for RpcServerImpl {
917
927
} )
918
928
}
919
929
930
+ async fn wallet_restore ( & self , name : & str , mnemonic : String ) -> Result < ( ) , ErrorObjectOwned > {
931
+ self . wallet_manager
932
+ . restore_wallet ( & self . client , name, & mnemonic)
933
+ . await
934
+ . map_err ( |error| {
935
+ ErrorObjectOwned :: owned ( RPC_WALLET_NOT_LOADED , error. to_string ( ) , None :: < String > )
936
+ } )
937
+ }
938
+
920
939
async fn wallet_send_request (
921
940
& self ,
922
941
wallet : & str ,
0 commit comments