Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change TxBuilder.drain_to argument to Script instead of address String #279

Merged
merged 2 commits into from
Dec 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api-docs/kotlin/src/main/kotlin/org/bitcoindevkit/bdk.kt
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ class TxBuilder() {
* [drainWallet] to spend all of them. When bumping the fees of a transaction made with this option,
* you probably want to use [BumpFeeTxBuilder.allowShrinking] to allow this output to be reduced to pay for the extra fees.
*/
fun drainTo(address: String): TxBuilder {}
fun drainTo(script: Script): TxBuilder {}

/** Enable signaling RBF. This will use the default `nsequence` value of `0xFFFFFFFD`. */
fun enableRbf(): TxBuilder {}
Expand Down
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