@@ -214,6 +214,17 @@ impl BitcoinD {
214
214
format ! ( "http://{}" , self . params. rpc_socket)
215
215
}
216
216
217
+ #[ cfg( not( any( feature = "0_17_1" , feature = "0_18_0" , feature = "0_18_1" ) ) ) ]
218
+ /// Returns the rpc URL including the schema and the given `wallet_name`
219
+ /// eg. http://127.0.0.1:44842/wallet/my_wallet
220
+ pub fn rpc_url_with_wallet < T : AsRef < str > > ( & self , wallet_name : T ) -> String {
221
+ format ! (
222
+ "http://{}/wallet/{}" ,
223
+ self . params. rpc_socket,
224
+ wallet_name. as_ref( )
225
+ )
226
+ }
227
+
217
228
/// Returns the [P2P] enum to connect to this node p2p port
218
229
pub fn p2p_connect ( & self , listen : bool ) -> Option < P2P > {
219
230
self . params . p2p_socket . map ( |s| P2P :: Connect ( s, listen) )
@@ -224,6 +235,19 @@ impl BitcoinD {
224
235
self . client . stop ( ) ?;
225
236
Ok ( self . process . wait ( ) ?)
226
237
}
238
+
239
+ #[ cfg( not( any( feature = "0_17_1" , feature = "0_18_0" , feature = "0_18_1" ) ) ) ]
240
+ /// Create a new wallet in the running node, and return an RPC client connected to the just
241
+ /// created wallet
242
+ pub fn create_wallet < T : AsRef < str > > ( & self , wallet : T ) -> Result < Client , Error > {
243
+ let _ = self
244
+ . client
245
+ . create_wallet ( wallet. as_ref ( ) , None , None , None , None ) ?;
246
+ Ok ( Client :: new (
247
+ self . rpc_url_with_wallet ( wallet) ,
248
+ Auth :: CookieFile ( self . params . cookie_file . clone ( ) ) ,
249
+ ) ?)
250
+ }
227
251
}
228
252
229
253
impl Drop for BitcoinD {
@@ -373,6 +397,62 @@ mod test {
373
397
assert_eq ! ( node3_peers. len( ) , 1 , "listen false but more than 1 peer" ) ;
374
398
}
375
399
400
+ #[ cfg( not( any( feature = "0_17_1" , feature = "0_18_0" , feature = "0_18_1" ) ) ) ]
401
+ #[ test]
402
+ fn test_multi_wallet ( ) {
403
+ use bitcoincore_rpc:: bitcoin:: Amount ;
404
+ let exe = init ( ) ;
405
+ let bitcoind = BitcoinD :: new ( exe) . unwrap ( ) ;
406
+ let alice = bitcoind. create_wallet ( "alice" ) . unwrap ( ) ;
407
+ let alice_address = alice. get_new_address ( None , None ) . unwrap ( ) ;
408
+ let bob = bitcoind. create_wallet ( "bob" ) . unwrap ( ) ;
409
+ let bob_address = bob. get_new_address ( None , None ) . unwrap ( ) ;
410
+ bitcoind
411
+ . client
412
+ . generate_to_address ( 1 , & alice_address)
413
+ . unwrap ( ) ;
414
+ bitcoind
415
+ . client
416
+ . generate_to_address ( 101 , & bob_address)
417
+ . unwrap ( ) ;
418
+ assert_eq ! (
419
+ Amount :: from_btc( 50.0 ) . unwrap( ) ,
420
+ alice. get_balances( ) . unwrap( ) . mine. trusted
421
+ ) ;
422
+ assert_eq ! (
423
+ Amount :: from_btc( 50.0 ) . unwrap( ) ,
424
+ bob. get_balances( ) . unwrap( ) . mine. trusted
425
+ ) ;
426
+ assert_eq ! (
427
+ Amount :: from_btc( 5000.0 ) . unwrap( ) ,
428
+ bob. get_balances( ) . unwrap( ) . mine. immature
429
+ ) ;
430
+ alice
431
+ . send_to_address (
432
+ & bob_address,
433
+ Amount :: from_btc ( 1.0 ) . unwrap ( ) ,
434
+ None ,
435
+ None ,
436
+ None ,
437
+ None ,
438
+ None ,
439
+ None ,
440
+ )
441
+ . unwrap ( ) ;
442
+ assert ! (
443
+ alice. get_balances( ) . unwrap( ) . mine. trusted < Amount :: from_btc( 49.0 ) . unwrap( )
444
+ && alice. get_balances( ) . unwrap( ) . mine. trusted > Amount :: from_btc( 48.9 ) . unwrap( )
445
+ ) ;
446
+ assert_eq ! (
447
+ Amount :: from_btc( 1.0 ) . unwrap( ) ,
448
+ bob. get_balances( ) . unwrap( ) . mine. untrusted_pending
449
+ ) ;
450
+ assert ! (
451
+ bitcoind. create_wallet( "bob" ) . is_err( ) ,
452
+ "wallet already exist"
453
+ ) ;
454
+ }
455
+
376
456
fn exe_path ( ) -> String {
377
457
if let Some ( downloaded_exe_path) = downloaded_exe_path ( ) {
378
458
downloaded_exe_path
0 commit comments