gui: prevent spend with duplicate addresses

Previously, if duplicate addresses were entered when creating
a new spend, the user could click Next and only the second
amount would be used for the draft PSBT.

This prevents the user clicking Next if there are duplicate
addresses and stops the redraft function running.
This commit is contained in:
jp1ac4 2024-02-22 12:20:30 +00:00
parent c1e2d26c63
commit 8639f9b94f
No known key found for this signature in database
GPG Key ID: A7ACD32423568D7B
2 changed files with 16 additions and 10 deletions

View File

@ -170,22 +170,28 @@ impl DefineSpend {
&& self.recipients.iter().all(|r| r.valid()) && self.recipients.iter().all(|r| r.valid())
} }
fn exists_duplicate(&self) -> bool {
for (i, recipient) in self.recipients.iter().enumerate() {
if !recipient.address.value.is_empty()
&& self.recipients[..i]
.iter()
.any(|r| r.address.value == recipient.address.value)
{
return true;
}
}
false
}
fn check_valid(&mut self) { fn check_valid(&mut self) {
self.is_valid = self.is_valid =
self.form_values_are_valid() && self.coins.iter().any(|(_, selected)| *selected); self.form_values_are_valid() && self.coins.iter().any(|(_, selected)| *selected);
self.is_duplicate = false; self.is_duplicate = self.exists_duplicate();
for (i, recipient) in self.recipients.iter().enumerate() {
if !self.is_duplicate && !recipient.address.value.is_empty() {
self.is_duplicate = self.recipients[..i]
.iter()
.any(|r| r.address.value == recipient.address.value);
}
}
} }
/// redraft calculates the amount left to select and auto selects coins /// redraft calculates the amount left to select and auto selects coins
/// if the user did not select a coin manually /// if the user did not select a coin manually
fn redraft(&mut self, daemon: Arc<dyn Daemon + Sync + Send>) { fn redraft(&mut self, daemon: Arc<dyn Daemon + Sync + Send>) {
if !self.form_values_are_valid() || self.recipients.is_empty() { if !self.form_values_are_valid() || self.exists_duplicate() || self.recipients.is_empty() {
return; return;
} }

View File

@ -253,7 +253,7 @@ pub fn create_spend_tx<'a>(
.width(Length::Fixed(100.0)), .width(Length::Fixed(100.0)),
) )
.push( .push(
if is_valid if is_valid && !duplicate
&& (is_self_send && (is_self_send
|| (total_amount < *balance_available || (total_amount < *balance_available
&& Some(&Amount::from_sat(0)) == amount_left)) && Some(&Amount::from_sat(0)) == amount_left))