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

Fix error message when importing desc for wrong network #933

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
49 changes: 27 additions & 22 deletions gui/src/installer/step/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1241,6 +1241,7 @@ pub struct ImportDescriptor {
change_network: bool,
data_dir: Option<PathBuf>,
imported_descriptor: form::Value<String>,
wrong_network: bool,
error: Option<String>,
}

Expand All @@ -1252,21 +1253,35 @@ impl ImportDescriptor {
network_valid: true,
data_dir: None,
imported_descriptor: form::Value::default(),
wrong_network: false,
error: None,
}
}

fn check_descriptor(&mut self) {
fn check_descriptor(&mut self, network: Network) -> Option<LianaDescriptor> {
if !self.imported_descriptor.value.is_empty() {
if let Ok(desc) = LianaDescriptor::from_str(&self.imported_descriptor.value) {
if self.network == Network::Bitcoin {
self.imported_descriptor.valid = desc.all_xpubs_net_is(self.network);
if network == Network::Bitcoin {
self.imported_descriptor.valid = desc.all_xpubs_net_is(network);
} else {
self.imported_descriptor.valid = desc.all_xpubs_net_is(Network::Testnet);
}
if self.imported_descriptor.valid {
self.wrong_network = false;
Some(desc)
} else {
self.wrong_network = true;
None
}
} else {
self.imported_descriptor.valid = false;
self.wrong_network = false;
None
}
} else {
self.wrong_network = false;
self.imported_descriptor.valid = true;
None
}
}
}
Expand All @@ -1281,18 +1296,21 @@ impl Step for ImportDescriptor {
let mut network_datadir = self.data_dir.clone().unwrap();
network_datadir.push(self.network.to_string());
self.network_valid = !network_datadir.exists();
self.check_descriptor();
self.check_descriptor(self.network);
}
Message::DefineDescriptor(message::DefineDescriptor::ImportDescriptor(desc)) => {
self.imported_descriptor.value = desc;
self.check_descriptor();
self.check_descriptor(self.network);
}
_ => {}
};
Command::none()
}

fn load_context(&mut self, ctx: &Context) {
if ctx.bitcoin_config.network != self.network {
self.check_descriptor(ctx.bitcoin_config.network);
}
self.network = ctx.bitcoin_config.network;
self.data_dir = Some(ctx.data_dir.clone());
let mut network_datadir = ctx.data_dir.clone();
Expand All @@ -1305,23 +1323,9 @@ impl Step for ImportDescriptor {
// Set to true in order to force the registration process to be shown to user.
ctx.hw_is_used = true;
// descriptor forms for import or creation cannot be both empty or filled.
if !self.imported_descriptor.value.is_empty() {
if let Ok(desc) = LianaDescriptor::from_str(&self.imported_descriptor.value) {
if self.network == Network::Bitcoin {
self.imported_descriptor.valid = desc.all_xpubs_net_is(self.network);
} else {
self.imported_descriptor.valid = desc.all_xpubs_net_is(Network::Testnet);
}
if self.imported_descriptor.valid {
ctx.descriptor = Some(desc);
true
} else {
false
}
} else {
self.imported_descriptor.valid = false;
false
}
if let Some(desc) = self.check_descriptor(self.network) {
ctx.descriptor = Some(desc);
true
} else {
false
}
Expand All @@ -1334,6 +1338,7 @@ impl Step for ImportDescriptor {
self.network,
self.network_valid,
&self.imported_descriptor,
self.wrong_network,
self.error.as_ref(),
)
}
Expand Down
7 changes: 6 additions & 1 deletion gui/src/installer/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ pub fn import_descriptor<'a>(
network: bitcoin::Network,
network_valid: bool,
imported_descriptor: &form::Value<String>,
wrong_network: bool,
error: Option<&String>,
) -> Element<'a, Message> {
let row_network = Row::new()
Expand Down Expand Up @@ -392,7 +393,11 @@ pub fn import_descriptor<'a>(
form::Form::new_trimmed("Descriptor", imported_descriptor, |msg| {
Message::DefineDescriptor(message::DefineDescriptor::ImportDescriptor(msg))
})
.warning("Incompatible descriptor.")
.warning(if wrong_network {
"The descriptor is for another network"
} else {
"Failed to read the descriptor"
})
.size(20)
.padding(10),
)
Expand Down
Loading