Skip to content

Commit a4e0374

Browse files
committed
create wallet and return RPC client
1 parent 6713290 commit a4e0374

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ jobs:
3636
- uses: actions-rs/cargo@v1
3737
with:
3838
command: test
39+
if: ${{ matrix.feature != '0_18_1' && matrix.feature != '0_18_0' && matrix.feature != '0_17_1' }}
3940

4041
cosmetics:
4142
runs-on: ubuntu-20.04

src/lib.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,17 @@ impl BitcoinD {
214214
format!("http://{}", self.params.rpc_socket)
215215
}
216216

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+
217228
/// Returns the [P2P] enum to connect to this node p2p port
218229
pub fn p2p_connect(&self, listen: bool) -> Option<P2P> {
219230
self.params.p2p_socket.map(|s| P2P::Connect(s, listen))
@@ -224,6 +235,19 @@ impl BitcoinD {
224235
self.client.stop()?;
225236
Ok(self.process.wait()?)
226237
}
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+
}
227251
}
228252

229253
impl Drop for BitcoinD {
@@ -373,6 +397,62 @@ mod test {
373397
assert_eq!(node3_peers.len(), 1, "listen false but more than 1 peer");
374398
}
375399

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+
376456
fn exe_path() -> String {
377457
if let Some(downloaded_exe_path) = downloaded_exe_path() {
378458
downloaded_exe_path

0 commit comments

Comments
 (0)