Skip to content

Commit 4550c75

Browse files
committed
feat: allow creating wallets with specified directory
1 parent 41fab0d commit 4550c75

3 files changed

Lines changed: 86 additions & 0 deletions

File tree

testenv/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,15 @@ impl TestEnv {
161161
Self::new_with_conf(Config::default())
162162
}
163163

164+
/// Generate a new temporary directory
165+
/// Note: The caller is responsible of calling `.close()` on the returned tmp dir
166+
/// else this will result in many created and undeleted temporary folders
167+
pub fn get_tmp_dir(&self) -> anyhow::Result<TempDir> {
168+
let mut dir = TempDir::new()?;
169+
dir.disable_cleanup(true);
170+
Ok(dir)
171+
}
172+
164173
/// Create a new test environment with ZMQ enabled on bitcoind.
165174
///
166175
/// The ZMQ socket addresses are available via

wallet/src/bmp_wallet.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,11 @@ pub trait WalletApi {
331331
where
332332
Self: Sized;
333333

334+
/// Allow creating the wallet at the specified path
335+
fn new_with_dir(path: &str, password: &str, network: Network) -> anyhow::Result<Self>
336+
where
337+
Self: Sized;
338+
334339
fn load_wallet(network: Network, password: &str) -> anyhow::Result<Self>
335340
where
336341
Self: Sized;
@@ -642,6 +647,14 @@ impl WalletApi for BMPWallet<Connection> {
642647
})
643648
}
644649

650+
fn new_with_dir(path: &str, password: &str, network: Network) -> anyhow::Result<Self>
651+
where
652+
Self: Sized,
653+
{
654+
std::env::set_current_dir(path).unwrap();
655+
Self::new(password, network)
656+
}
657+
645658
// For already created wallets this will load stored data
646659
// This will also load the imported keys
647660
fn load_wallet(network: Network, password: &str) -> anyhow::Result<Self> {
@@ -1152,4 +1165,28 @@ mod tests {
11521165

11531166
Ok(())
11541167
}
1168+
1169+
#[test]
1170+
fn test_wallet_with_path_creation() -> anyhow::Result<()> {
1171+
let _permit = SEMAPHORE.acquire();
1172+
let dir_one = TempDir::with_prefix("one-")?;
1173+
let dir_two = TempDir::with_prefix("two-")?;
1174+
1175+
let client = MockedBDKElectrum {};
1176+
1177+
tracing::debug!("Wallet path {:?}", dir_one);
1178+
tracing::debug!("Wallet 2 path {:?}", dir_two);
1179+
1180+
let mut w1 =
1181+
BMPWallet::new_with_dir(dir_one.path().to_str().unwrap(), "", Network::Regtest)?;
1182+
let w2 = BMPWallet::new_with_dir(dir_two.path().to_str().unwrap(), "", Network::Regtest)?;
1183+
1184+
tracing::debug!("Wallet one balance before syncing {}", w1.balance());
1185+
assert_eq!(w1.balance(), Amount::from_int_btc(0));
1186+
let _ = w1.sync_all(&client);
1187+
1188+
assert_eq!(w1.balance(), Amount::from_int_btc(1));
1189+
assert_eq!(w2.balance(), Amount::ZERO);
1190+
Ok(())
1191+
}
11551192
}

wallet/tests/wallet_integration_test.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,3 +401,43 @@ async fn test_drain_wallet_no_balance() {
401401
.drain_imported_balance(FeeRate::from_sat_per_vb(10).unwrap())
402402
.unwrap();
403403
}
404+
405+
#[tokio::test]
406+
async fn test_multi_wallet_path() -> anyhow::Result<()> {
407+
let env = TestEnv::new().unwrap();
408+
env.mine_block().unwrap();
409+
410+
let client = env.bdk_electrum_client();
411+
412+
let dir_one = env.get_tmp_dir()?;
413+
let dir_two = env.get_tmp_dir()?;
414+
415+
let mut w1 = BMPWallet::new_with_dir(dir_one.path().to_str().unwrap(), "", Network::Regtest)?;
416+
let mut w2 = BMPWallet::new_with_dir(
417+
dir_two.path().to_str().unwrap(),
418+
"password",
419+
Network::Regtest,
420+
)?;
421+
422+
let amount_to_send_w1 = Amount::from_sat(100_000);
423+
let amount_to_send_w2 = Amount::from_sat(80_000);
424+
425+
let addr_w1 = w1.next_unused_address(KeychainKind::External);
426+
let addr_w2 = w2.next_unused_address(KeychainKind::External);
427+
428+
env.fund_address(&addr_w1, amount_to_send_w1)?;
429+
env.fund_address(&addr_w2, amount_to_send_w2)?;
430+
431+
env.mine_block()?;
432+
433+
w1.sync_all(client)?;
434+
w2.sync_all(client)?;
435+
436+
assert_eq!(w1.balance(), amount_to_send_w1);
437+
assert_eq!(w2.balance(), amount_to_send_w2);
438+
439+
dir_one.close()?;
440+
dir_two.close()?;
441+
442+
Ok(())
443+
}

0 commit comments

Comments
 (0)