Skip to content

Commit 527def6

Browse files
committed
fixup! prioritise unused change addresses for split
require split n be specified
1 parent d95e631 commit 527def6

File tree

1 file changed

+32
-48
lines changed

1 file changed

+32
-48
lines changed

src/cmd/wallet.rs

+32-48
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ pub struct SplitOpt {
574574
#[structopt(parse(try_from_str = FromCliStr::from_cli_str))]
575575
output_size: Amount,
576576
/// Number of outputs to create. If omitted it will use the maximum possible.
577-
n: Option<usize>,
577+
n: usize,
578578
#[structopt(flatten)]
579579
spend_opt: SpendOpt,
580580
}
@@ -596,54 +596,38 @@ pub fn run_split_cmd(wallet: &GunWallet, opt: SplitOpt) -> anyhow::Result<CmdOut
596596
builder.unspendable(already_correct.map(|utxo| utxo.outpoint).collect());
597597

598598
let txn_map = index_script_txns(&bdk_wallet.database().iter_txs(true)?)?;
599-
match n {
600-
Some(n) => {
601-
let last_unused_index = bdk_wallet
602-
.get_change_address(AddressIndex::LastUnused)?
603-
.index;
604-
let mut added_recipients = 0;
605-
// Check for unused change addresses up to most recent
606-
for check_used_index in 0..(last_unused_index + 1) {
607-
let check_script_pk = bdk_wallet
608-
.get_change_address(AddressIndex::Peek(check_used_index as u32))?
609-
.address
610-
.script_pubkey();
611-
612-
let used = txn_map.get(&check_script_pk).is_some();
613-
// Keep adding unused recipients until we have n
614-
if !used {
615-
builder.add_recipient(check_script_pk, output_size.as_sat());
616-
added_recipients += 1;
617-
if added_recipients == n {
618-
break;
619-
};
620-
}
621-
}
622-
// If we don't have enough unused addresses, fill remaining n-added with new
623-
for _ in 0..(n - added_recipients) {
624-
builder.add_recipient(
625-
bdk_wallet
626-
.get_change_address(AddressIndex::New)?
627-
.address
628-
.script_pubkey(),
629-
output_size.as_sat(),
630-
);
631-
}
632-
}
633-
None => {
634-
builder
635-
.drain_wallet()
636-
// add one recipient so we at least get one split utxo of the correct size.
637-
.add_recipient(
638-
bdk_wallet
639-
.get_change_address(AddressIndex::LastUnused)?
640-
.address
641-
.script_pubkey(),
642-
output_size.as_sat(),
643-
)
644-
.split_change(output_size.as_sat(), usize::MAX);
599+
600+
let last_unused_index = bdk_wallet
601+
.get_change_address(AddressIndex::LastUnused)?
602+
.index;
603+
let mut added_recipients = 0;
604+
// Check for unused change addresses up to most recent
605+
for check_used_index in 0..(last_unused_index + 1) {
606+
let check_script_pk = bdk_wallet
607+
.get_change_address(AddressIndex::Peek(check_used_index as u32))?
608+
.address
609+
.script_pubkey();
610+
611+
let used = txn_map.get(&check_script_pk).is_some();
612+
// Keep adding unused recipients until we have n
613+
if !used {
614+
builder.add_recipient(check_script_pk, output_size.as_sat());
615+
added_recipients += 1;
616+
if added_recipients == n {
617+
break;
618+
};
645619
}
646-
};
620+
}
621+
// If we don't have enough unused addresses, fill remaining n-added with new
622+
for _ in 0..(n - added_recipients) {
623+
builder.add_recipient(
624+
bdk_wallet
625+
.get_change_address(AddressIndex::New)?
626+
.address
627+
.script_pubkey(),
628+
output_size.as_sat(),
629+
);
630+
}
647631

648632
spend_opt.spend_coins(wallet, builder)
649633
}

0 commit comments

Comments
 (0)