@@ -75,15 +75,23 @@ fn sbtc<F: Into<f64>>(btc: F) -> SignedAmount {
75
75
SignedAmount :: from_btc ( btc. into ( ) ) . unwrap ( )
76
76
}
77
77
78
- fn main ( ) {
79
- let rpc_url = std:: env:: var ( "RPC_URL" ) . expect ( "RPC_URL must be set" ) ;
80
- let auth = if let Ok ( cookie) = std:: env:: var ( "RPC_COOKIE" ) {
81
- Auth :: CookieFile ( cookie. into ( ) )
78
+ fn get_rpc_url ( ) -> String {
79
+ return std:: env:: var ( "RPC_URL" ) . expect ( "RPC_URL must be set" ) ;
80
+ }
81
+
82
+ fn get_auth ( ) -> bitcoincore_rpc:: Auth {
83
+ if let Ok ( cookie) = std:: env:: var ( "RPC_COOKIE" ) {
84
+ return Auth :: CookieFile ( cookie. into ( ) ) ;
82
85
} else if let Ok ( user) = std:: env:: var ( "RPC_USER" ) {
83
- Auth :: UserPass ( user, std:: env:: var ( "RPC_PASS" ) . unwrap_or_default ( ) )
86
+ return Auth :: UserPass ( user, std:: env:: var ( "RPC_PASS" ) . unwrap_or_default ( ) ) ;
84
87
} else {
85
88
panic ! ( "Either RPC_COOKIE or RPC_USER + RPC_PASS must be set." ) ;
86
89
} ;
90
+ }
91
+
92
+ fn main ( ) {
93
+ let rpc_url = get_rpc_url ( ) ;
94
+ let auth = get_auth ( ) ;
87
95
88
96
let cl = Client :: new ( rpc_url, auth) . unwrap ( ) ;
89
97
@@ -137,6 +145,7 @@ fn main() {
137
145
test_ping ( & cl) ;
138
146
test_get_peer_info ( & cl) ;
139
147
test_rescan_blockchain ( & cl) ;
148
+ test_create_wallet ( & cl) ;
140
149
//TODO import_multi(
141
150
//TODO verify_message(
142
151
//TODO wait_for_new_block(&self, timeout: u64) -> Result<json::BlockRef> {
@@ -148,7 +157,6 @@ fn main() {
148
157
//TODO add_multisig_address(
149
158
//TODO load_wallet(&self, wallet: &str) -> Result<json::LoadWalletResult> {
150
159
//TODO unload_wallet(&self, wallet: Option<&str>) -> Result<()> {
151
- //TODO create_wallet(
152
160
//TODO backup_wallet(&self, destination: Option<&str>) -> Result<()> {
153
161
test_stop ( cl) ;
154
162
}
@@ -778,6 +786,97 @@ fn test_rescan_blockchain(cl: &Client) {
778
786
assert_eq ! ( stop, Some ( count - 1 ) ) ;
779
787
}
780
788
789
+ struct WalletParams < ' a > {
790
+ name : & ' a str ,
791
+ disable_private_keys : Option < bool > ,
792
+ blank : Option < bool > ,
793
+ passphrase : Option < & ' a str > ,
794
+ avoid_reuse : Option < bool > ,
795
+ }
796
+
797
+ fn build_wallet_params < ' a > (
798
+ name : & ' a str ,
799
+ disable_private_keys : Option < bool > ,
800
+ blank : Option < bool > ,
801
+ passphrase : Option < & ' a str > ,
802
+ avoid_reuse : Option < bool > ,
803
+ ) -> WalletParams < ' a > {
804
+ WalletParams {
805
+ name,
806
+ disable_private_keys,
807
+ blank,
808
+ passphrase,
809
+ avoid_reuse,
810
+ }
811
+ }
812
+
813
+ fn test_create_single_wallet ( cl : & Client , wallet_param : WalletParams ) {
814
+ let result = cl
815
+ . create_wallet (
816
+ wallet_param. name ,
817
+ wallet_param. disable_private_keys ,
818
+ wallet_param. blank ,
819
+ wallet_param. passphrase ,
820
+ wallet_param. avoid_reuse ,
821
+ )
822
+ . unwrap ( ) ;
823
+
824
+ assert_eq ! ( result. name, wallet_param. name) ;
825
+ let expected_warning = match ( wallet_param. passphrase , wallet_param. avoid_reuse ) {
826
+ ( None , Some ( true ) ) => Some ( "Empty string given as passphrase, wallet will not be encrypted." . to_string ( ) ) ,
827
+ _ => Some ( "" . to_string ( ) ) ,
828
+ } ;
829
+ assert_eq ! ( result. warning, expected_warning) ;
830
+
831
+ let wallet_client_url = format ! ( "{}{}{}" , get_rpc_url( ) , "/wallet/" , wallet_param. name) ;
832
+ let wallet_client = Client :: new ( wallet_client_url, get_auth ( ) ) . unwrap ( ) ;
833
+ let wallet_info = wallet_client. get_wallet_info ( ) . unwrap ( ) ;
834
+
835
+ assert_eq ! ( wallet_info. wallet_name, wallet_param. name) ;
836
+
837
+ fn option_to_bool ( option : Option < bool > ) -> bool {
838
+ match option {
839
+ None | Some ( false ) => false ,
840
+ Some ( true ) => true ,
841
+ }
842
+ }
843
+
844
+ let has_private_keys = !option_to_bool ( wallet_param. disable_private_keys ) ;
845
+ assert_eq ! ( wallet_info. private_keys_enabled, has_private_keys) ;
846
+ let has_hd_seed = has_private_keys && !option_to_bool ( wallet_param. blank ) ;
847
+ assert_eq ! ( wallet_info. hd_seed_id. is_some( ) , has_hd_seed) ;
848
+ let has_avoid_reuse = option_to_bool ( wallet_param. avoid_reuse ) ;
849
+ assert_eq ! ( option_to_bool( wallet_info. avoid_reuse) , has_avoid_reuse) ;
850
+ }
851
+
852
+ fn test_create_wallet ( cl : & Client ) {
853
+ let wallet_names = vec ! [ "alice" , "bob" , "carol" , "denise" , "emily" ] ;
854
+ let mut wallet_params = vec ! [
855
+ build_wallet_params( wallet_names[ 0 ] , None , None , None , None ) ,
856
+ build_wallet_params( wallet_names[ 1 ] , Some ( true ) , None , None , None ) ,
857
+ build_wallet_params( wallet_names[ 2 ] , None , Some ( true ) , None , None ) ,
858
+ ] ;
859
+
860
+ if version ( ) >= 190000 {
861
+ wallet_params. push ( build_wallet_params ( wallet_names[ 3 ] , None , None , Some ( "pass" ) , None ) ) ;
862
+ wallet_params. push ( build_wallet_params ( wallet_names[ 4 ] , None , None , None , Some ( true ) ) ) ;
863
+ }
864
+
865
+ for param in wallet_params {
866
+ test_create_single_wallet ( & cl, param) ;
867
+ }
868
+
869
+ let mut wallet_list = cl. list_wallets ( ) . unwrap ( ) ;
870
+
871
+ wallet_list. sort ( ) ;
872
+
873
+ // Default wallet
874
+ assert_eq ! ( wallet_list. remove( 0 ) , "" ) ;
875
+
876
+ // Created wallets
877
+ assert ! ( wallet_list. iter( ) . zip( wallet_names) . all( |( a, b) | a == b) ) ;
878
+ }
879
+
781
880
fn test_stop ( cl : Client ) {
782
881
println ! ( "Stopping: '{}'" , cl. stop( ) . unwrap( ) ) ;
783
882
}
0 commit comments