From 9e1c16bc3fef5aa01a62cb34304cc161d4623468 Mon Sep 17 00:00:00 2001 From: edouard Date: Tue, 20 Dec 2022 16:28:32 +0100 Subject: [PATCH] Check the address network during spend creation --- gui/src/app/state/spend/step.rs | 25 ++++++++++++++++--------- gui/src/app/view/spend/step.rs | 2 +- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/gui/src/app/state/spend/step.rs b/gui/src/app/state/spend/step.rs index 64b71909..890ec0ad 100644 --- a/gui/src/app/state/spend/step.rs +++ b/gui/src/app/state/spend/step.rs @@ -5,7 +5,9 @@ use std::sync::Arc; use iced::{Command, Element}; use liana::{ config::Config as DaemonConfig, - miniscript::bitcoin::{self, util::psbt::Psbt, Address, Amount, Denomination, OutPoint}, + miniscript::bitcoin::{ + self, util::psbt::Psbt, Address, Amount, Denomination, Network, OutPoint, + }, }; use crate::{ @@ -75,7 +77,7 @@ impl ChooseRecipients { impl Step for ChooseRecipients { fn update( &mut self, - _daemon: Arc, + daemon: Arc, _cache: &Cache, _draft: &TransactionDraft, message: Message, @@ -89,7 +91,10 @@ impl Step for ChooseRecipients { self.recipients.remove(*i); } view::CreateSpendMessage::RecipientEdited(i, _, _) => { - self.recipients.get_mut(*i).unwrap().update(msg); + self.recipients + .get_mut(*i) + .unwrap() + .update(daemon.config().bitcoin_config.network, msg); } _ => {} } @@ -166,18 +171,20 @@ impl Recipient { && self.amount.valid } - fn update(&mut self, message: view::CreateSpendMessage) { + fn update(&mut self, network: Network, message: view::CreateSpendMessage) { match message { view::CreateSpendMessage::RecipientEdited(_, "address", address) => { self.address.value = address; - if self.address.value.is_empty() { - // Make the error disappear if we deleted the invalid address - self.address.valid = true; - } else if Address::from_str(&self.address.value).is_ok() { - self.address.valid = true; + if let Ok(address) = Address::from_str(&self.address.value) { + self.address.valid = address.network == network + || (address.network == bitcoin::Network::Testnet + && network == bitcoin::Network::Signet); if !self.amount.value.is_empty() { self.amount.valid = self.amount().is_ok(); } + } else if self.address.value.is_empty() { + // Make the error disappear if we deleted the invalid address + self.address.valid = true; } else { self.address.valid = false; } diff --git a/gui/src/app/view/spend/step.rs b/gui/src/app/view/spend/step.rs index 0e467e9c..93051760 100644 --- a/gui/src/app/view/spend/step.rs +++ b/gui/src/app/view/spend/step.rs @@ -84,7 +84,7 @@ pub fn recipient_view<'a>( form::Form::new("Address", address, move |msg| { CreateSpendMessage::RecipientEdited(index, "address", msg) }) - .warning("Please enter correct bitcoin address") + .warning("Please enter correct bitcoin address for the current network") .size(20) .padding(10), )