Skip to content

Commit 4220b37

Browse files
committed
feat: add draining wallet API
1 parent 2d7aed8 commit 4220b37

1 file changed

Lines changed: 21 additions & 1 deletion

File tree

wallet/src/bmp_wallet.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{fs, vec};
44
use base64::engine::general_purpose;
55
use base64::Engine as _;
66
use bdk_electrum::bdk_core::bitcoin::{absolute, Address, FeeRate, OutPoint};
7-
use bdk_kyoto::bip157::{tokio, Builder};
7+
use bdk_kyoto::bip157::{tokio, Builder, Transaction};
88
use bdk_kyoto::{BuilderExt as _, LightClient, Requester, ScanType, TrustedPeer, UpdateSubscriber};
99
use bdk_wallet::bitcoin::bip32::Xpriv;
1010
use bdk_wallet::bitcoin::hex::DisplayHex as _;
@@ -358,6 +358,8 @@ pub trait WalletApi {
358358
scan_type: ScanType,
359359
peers: Vec<TrustedPeer>,
360360
) -> impl Future<Output = anyhow::Result<()>> + Send;
361+
362+
fn drain(&mut self) -> anyhow::Result<Transaction>;
361363
}
362364

363365
impl WalletApi for BMPWallet<Connection> {
@@ -728,6 +730,18 @@ impl WalletApi for BMPWallet<Connection> {
728730
db: encrypted_conn,
729731
})
730732
}
733+
734+
fn drain(&mut self) -> anyhow::Result<Transaction> {
735+
let drain_to_address = self.next_address(KeychainKind::External)?;
736+
let imported_balance = self.imported_balance.trusted_spendable();
737+
let mut tx = self.build_tx();
738+
tx.add_recipient(drain_to_address.script_pubkey(), imported_balance);
739+
740+
let mut psbt = tx.finish()?;
741+
self.sign(&mut psbt, SignOptions::default())?;
742+
743+
Ok(psbt.extract_tx()?)
744+
}
731745
}
732746

733747
impl Deref for BMPWallet<Connection> {
@@ -747,6 +761,7 @@ impl DerefMut for BMPWallet<Connection> {
747761
mod tests {
748762
use std::sync::{Arc, LazyLock};
749763

764+
use anyhow::Ok;
750765
use bdk_wallet::bitcoin::hashes::Hash as _;
751766
use bdk_wallet::bitcoin::{psbt, Address, AddressType, Amount, BlockHash, Network, Weight};
752767
use bdk_wallet::chain::{self, BlockId};
@@ -1123,4 +1138,9 @@ mod tests {
11231138

11241139
assert_eq!(lw.get_seed_phrase().unwrap(), seed);
11251140
}
1141+
1142+
#[test]
1143+
fn drain_wallet() -> anyhow::Result<()> {
1144+
Ok(())
1145+
}
11261146
}

0 commit comments

Comments
 (0)