Skip to content

Commit

Permalink
Change TxBuilder.drain_to argument to Script instead of address String
Browse files Browse the repository at this point in the history
  • Loading branch information
notmandatory committed Dec 7, 2022
1 parent a408387 commit 7d1a450
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
2 changes: 1 addition & 1 deletion bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ interface TxBuilder {

TxBuilder drain_wallet();

TxBuilder drain_to(string address);
TxBuilder drain_to(Script script);

TxBuilder enable_rbf();

Expand Down
33 changes: 17 additions & 16 deletions bdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,12 +501,6 @@ impl Wallet {
}
}

fn to_script_pubkey(address: &str) -> Result<BdkScript, BdkError> {
BdkAddress::from_str(address)
.map(|x| x.script_pubkey())
.map_err(|e| BdkError::Generic(e.to_string()))
}

/// A Bitcoin address.
struct Address {
address: BdkAddress,
Expand All @@ -527,7 +521,7 @@ impl Address {
}

/// A Bitcoin script.
#[derive(Clone)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Script {
script: BdkScript,
}
Expand Down Expand Up @@ -565,7 +559,7 @@ struct TxBuilder {
fee_rate: Option<f32>,
fee_absolute: Option<u64>,
drain_wallet: bool,
drain_to: Option<String>,
drain_to: Option<BdkScript>,
rbf: Option<RbfValue>,
data: Vec<u8>,
}
Expand Down Expand Up @@ -703,9 +697,9 @@ impl TxBuilder {
/// either provide the utxos that the transaction should spend via add_utxos, or set drain_wallet to spend all of them.
/// When bumping the fees of a transaction made with this option, you probably want to use BumpFeeTxBuilder.allow_shrinking
/// to allow this output to be reduced to pay for the extra fees.
fn drain_to(&self, address: String) -> Arc<Self> {
fn drain_to(&self, script: Arc<Script>) -> Arc<Self> {
Arc::new(TxBuilder {
drain_to: Some(address),
drain_to: Some(script.script.clone()),
..self.clone()
})
}
Expand Down Expand Up @@ -766,8 +760,8 @@ impl TxBuilder {
if self.drain_wallet {
tx_builder.drain_wallet();
}
if let Some(address) = &self.drain_to {
tx_builder.drain_to(to_script_pubkey(address)?);
if let Some(script) = &self.drain_to {
tx_builder.drain_to(script.clone());
}
if let Some(rbf) = &self.rbf {
match *rbf {
Expand Down Expand Up @@ -1125,11 +1119,14 @@ mod test {
wallet_mutex: Mutex::new(funded_wallet),
};
let drain_to_address = "tb1ql7w62elx9ucw4pj5lgw4l028hmuw80sndtntxt".to_string();
let drain_to_script = crate::Address::new(drain_to_address)
.unwrap()
.script_pubkey();
let tx_builder = TxBuilder::new()
.drain_wallet()
.drain_to(drain_to_address.clone());
.drain_to(drain_to_script.clone());
assert!(tx_builder.drain_wallet);
assert_eq!(tx_builder.drain_to, Some(drain_to_address));
assert_eq!(tx_builder.drain_to, Some(drain_to_script.script.clone()));

let tx_builder_result = tx_builder.finish(&test_wallet).unwrap();
let psbt = tx_builder_result.psbt.internal.lock().unwrap().clone();
Expand Down Expand Up @@ -1317,13 +1314,17 @@ mod test {
wallet_mutex: Mutex::new(funded_wallet),
};
let drain_to_address = "tb1ql7w62elx9ucw4pj5lgw4l028hmuw80sndtntxt".to_string();
let drain_to_script = crate::Address::new(drain_to_address)
.unwrap()
.script_pubkey();

let tx_builder = TxBuilder::new()
.fee_rate(2.0)
.drain_wallet()
.drain_to(drain_to_address.clone());
.drain_to(drain_to_script.clone());
//dbg!(&tx_builder);
assert!(tx_builder.drain_wallet);
assert_eq!(tx_builder.drain_to, Some(drain_to_address));
assert_eq!(tx_builder.drain_to, Some(drain_to_script.script.clone()));

let tx_builder_result = tx_builder.finish(&test_wallet).unwrap();

Expand Down

0 comments on commit 7d1a450

Please sign in to comment.