@@ -242,7 +242,10 @@ pub trait Rpc {
242
242
async fn wallet_export ( & self , name : & str ) -> Result < WalletExport , ErrorObjectOwned > ;
243
243
244
244
#[ method( name = "walletcreate" ) ]
245
- async fn wallet_create ( & self , name : & str ) -> Result < ( ) , ErrorObjectOwned > ;
245
+ async fn wallet_create ( & self , name : & str ) -> Result < String , ErrorObjectOwned > ;
246
+
247
+ #[ method( name = "walletrecover" ) ]
248
+ async fn wallet_recover ( & self , name : & str , mnemonic : String ) -> Result < ( ) , ErrorObjectOwned > ;
246
249
247
250
#[ method( name = "walletsendrequest" ) ]
248
251
async fn wallet_send_request (
@@ -503,11 +506,18 @@ impl WalletManager {
503
506
Ok ( export)
504
507
}
505
508
506
- pub async fn create_wallet ( & self , client : & reqwest:: Client , name : & str ) -> anyhow:: Result < ( ) > {
509
+ pub async fn create_wallet ( & self , client : & reqwest:: Client , name : & str ) -> anyhow:: Result < String > {
507
510
let mnemonic: GeneratedKey < _ , Tap > =
508
511
Mnemonic :: generate ( ( WordCount :: Words12 , Language :: English ) )
509
512
. map_err ( |_| anyhow ! ( "Mnemonic generation error" ) ) ?;
510
513
514
+ let start_block = self . get_wallet_start_block ( client) . await ?;
515
+ self . setup_new_wallet ( name. to_string ( ) , mnemonic. to_string ( ) , start_block) ?;
516
+ self . load_wallet ( name) . await ?;
517
+ Ok ( mnemonic. to_string ( ) )
518
+ }
519
+
520
+ pub async fn recover_wallet ( & self , client : & reqwest:: Client , name : & str , mnemonic : & str ) -> anyhow:: Result < ( ) > {
511
521
let start_block = self . get_wallet_start_block ( client) . await ?;
512
522
self . setup_new_wallet ( name. to_string ( ) , mnemonic. to_string ( ) , start_block) ?;
513
523
self . load_wallet ( name) . await ?;
@@ -525,7 +535,7 @@ impl WalletManager {
525
535
return Err ( anyhow ! ( format!( "Wallet `{}` already exists" , name) ) ) ;
526
536
}
527
537
528
- let export = self . wallet_from_mnemonic ( name. clone ( ) , mnemonic. to_string ( ) , start_block) ?;
538
+ let export = self . wallet_from_mnemonic ( name. clone ( ) , mnemonic, start_block) ?;
529
539
fs:: create_dir_all ( & wallet_path) ?;
530
540
let wallet_export_path = wallet_path. join ( "wallet.json" ) ;
531
541
let mut file = fs:: File :: create ( wallet_export_path) ?;
@@ -540,7 +550,7 @@ impl WalletManager {
540
550
start_block : BlockId ,
541
551
) -> anyhow:: Result < WalletExport > {
542
552
let ( network, _) = self . fallback_network ( ) ;
543
- let xpriv = Self :: descriptor_from_mnemonic ( network, & mnemonic. to_string ( ) ) ?;
553
+ let xpriv = Self :: descriptor_from_mnemonic ( network, & mnemonic) ?;
544
554
545
555
let ( external, internal) = Self :: default_descriptors ( xpriv) ;
546
556
let tmp = bdk:: Wallet :: create ( external, internal)
@@ -666,7 +676,7 @@ impl WalletManager {
666
676
}
667
677
668
678
fn descriptor_from_mnemonic ( network : Network , m : & str ) -> anyhow:: Result < Xpriv > {
669
- let mnemonic = Mnemonic :: parse ( m) . unwrap ( ) ;
679
+ let mnemonic = Mnemonic :: parse ( m) ? ;
670
680
let xkey: ExtendedKey = mnemonic. clone ( ) . into_extended_key ( ) ?;
671
681
Ok ( xkey. into_xprv ( network) . expect ( "xpriv" ) )
672
682
}
@@ -924,7 +934,7 @@ impl RpcServer for RpcServerImpl {
924
934
} )
925
935
}
926
936
927
- async fn wallet_create ( & self , name : & str ) -> Result < ( ) , ErrorObjectOwned > {
937
+ async fn wallet_create ( & self , name : & str ) -> Result < String , ErrorObjectOwned > {
928
938
self . wallet_manager
929
939
. create_wallet ( & self . client , name)
930
940
. await
@@ -933,6 +943,15 @@ impl RpcServer for RpcServerImpl {
933
943
} )
934
944
}
935
945
946
+ async fn wallet_recover ( & self , name : & str , mnemonic : String ) -> Result < ( ) , ErrorObjectOwned > {
947
+ self . wallet_manager
948
+ . recover_wallet ( & self . client , name, & mnemonic)
949
+ . await
950
+ . map_err ( |error| {
951
+ ErrorObjectOwned :: owned ( RPC_WALLET_NOT_LOADED , error. to_string ( ) , None :: < String > )
952
+ } )
953
+ }
954
+
936
955
async fn wallet_send_request (
937
956
& self ,
938
957
wallet : & str ,
0 commit comments